Module: kamailio
Branch: master
Commit: c33f1f494f1e7ff9deb887f58644fb21934f9b0e
URL: 
https://github.com/kamailio/kamailio/commit/c33f1f494f1e7ff9deb887f58644fb21934f9b0e

Author: Daniel-Constantin Mierla <mico...@gmail.com>
Committer: Daniel-Constantin Mierla <mico...@gmail.com>
Date: 2025-02-07T07:44:45+01:00

core: ksrxrand - framework to set custom rand/srand functions

---

Added: src/core/rand/ksrxrand.c
Added: src/core/rand/ksrxrand.h

---

Diff:  
https://github.com/kamailio/kamailio/commit/c33f1f494f1e7ff9deb887f58644fb21934f9b0e.diff
Patch: 
https://github.com/kamailio/kamailio/commit/c33f1f494f1e7ff9deb887f58644fb21934f9b0e.patch

---

diff --git a/src/core/rand/ksrxrand.c b/src/core/rand/ksrxrand.c
new file mode 100644
index 00000000000..e0938cf47d5
--- /dev/null
+++ b/src/core/rand/ksrxrand.c
@@ -0,0 +1,79 @@
+/*
+ * Copyright (C) 2025 Daniel-Constantin Mierla (asipto.com)
+ *
+ * This file is part of Kamailio, a free SIP server.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <string.h>
+
+#include "../dprint.h"
+
+#include "fastrand.h"
+#include "ksrxrand.h"
+
+/**
+ * wrapper function for rand()/random()
+ */
+int ksr_wrand(void)
+{
+#if RAND_MAX < INT_MAX
+       return (int)random();
+#else
+       return rand();
+#endif
+}
+
+/**
+ * wrapper function for srand()/srandom()
+ */
+void ksr_wsrand(unsigned int seed)
+{
+#if RAND_MAX < INT_MAX
+       return srandom(seed);
+#else
+       return srand(seed);
+#endif
+}
+
+/**
+ * wrapper function for fastrand()
+ */
+int ksr_wfastrand(void)
+{
+       return (int)(fastrand() % ((unsigned)KSR_XRAND_MAX + 1));
+}
+
+/**
+ * global with internal RAND API
+ */
+ksr_xrand_t _ksr_xrand_api = {.xrand = ksr_wrand, .xsrand = ksr_wsrand};
+
+/**
+ *
+ */
+int ksr_xrand_set(char *name)
+{
+       if(strcmp(name, "fast") == 0) {
+               _ksr_xrand_api.xrand = ksr_wfastrand;
+               _ksr_xrand_api.xsrand = fastrand_seed;
+               return 0;
+       }
+       if((strcmp(name, "rand") == 0) || (strcmp(name, "rand") == 0)) {
+               /* default - nothing to do */
+               return 0;
+       }
+       LM_WARN("unknown rand engine: %s\n", name);
+       return -1;
+}
diff --git a/src/core/rand/ksrxrand.h b/src/core/rand/ksrxrand.h
new file mode 100644
index 00000000000..51f026e9bc6
--- /dev/null
+++ b/src/core/rand/ksrxrand.h
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2025 Daniel-Constantin Mierla (asipto.com)
+ *
+ * This file is part of Kamailio, a free SIP server.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef __KSRXRAND_H__
+#define __KSRXRAND_H__
+
+#include <limits.h>
+#include <stdlib.h>
+
+#if RAND_MAX < INT_MAX
+#define KSR_XRAND_MAX ((int)(0x7FFFFFFF)) /* (1<<31) - 1 */
+#else
+#define KSR_XRAND_MAX RAND_MAX
+#endif
+
+typedef int (*ksr_xrand_f)(void);
+typedef void (*ksr_xsrand_f)(unsigned int seed);
+
+typedef struct ksr_xrand
+{
+       ksr_xrand_f xrand;
+       ksr_xsrand_f xsrand;
+} ksr_xrand_t;
+
+extern ksr_xrand_t _ksr_xrand_api;
+
+#define ksr_xrand() _ksr_xrand_api.xrand()
+#define ksr_xsrand(s) _ksr_xrand_api.xsrand(s)
+
+int ksr_xrand_set(char *name);
+
+#endif

_______________________________________________
Kamailio - Development Mailing List -- sr-dev@lists.kamailio.org
To unsubscribe send an email to sr-dev-le...@lists.kamailio.org
Important: keep the mailing list in the recipients, do not reply only to the 
sender!

Reply via email to