/src/rtpproxy/scripts/fuzz/rfz_utils.c
Line | Count | Source |
1 | | #include <sys/socket.h> |
2 | | #include <assert.h> |
3 | | #include <fcntl.h> |
4 | | #include <stdio.h> |
5 | | #include <stdint.h> |
6 | | #include <stdlib.h> |
7 | | #include <string.h> |
8 | | #include <threads.h> |
9 | | #include <unistd.h> |
10 | | |
11 | | #include <openssl/rand.h> |
12 | | |
13 | | #define HAVE_CONFIG_H 1 |
14 | | #include "config_pp.h" |
15 | | |
16 | | #include "rtpp_types.h" |
17 | | #include "rtpp_cfg.h" |
18 | | #include "rtpp_refcnt.h" |
19 | | #include "rtpp_log_stand.h" |
20 | | #include "rtpp_log_obj.h" |
21 | | #include "rtpp_proc_async.h" |
22 | | #include "rtpp_hash_table.h" |
23 | | #include "rtpp_weakref.h" |
24 | | #include "rtpp_sessinfo.h" |
25 | | #include "rtpp_stats.h" |
26 | | #include "rtpp_time.h" |
27 | | #include "rtpp_util.h" |
28 | | |
29 | | #include "librtpproxy.h" |
30 | | |
31 | | #include "rfz_utils.h" |
32 | | |
33 | 4 | #define howmany(x, y) (sizeof(x) / sizeof(y)) |
34 | | |
35 | | struct rtpp_conf gconf; |
36 | | |
37 | | #if defined(__linux__) |
38 | | static int optreset; /* Not present in linux */ |
39 | | #endif |
40 | | |
41 | | static struct opt_save { |
42 | | char *optarg; |
43 | | int optind; |
44 | | int optopt; |
45 | | int opterr; |
46 | | int optreset; |
47 | | } opt_save; |
48 | | |
49 | 4 | #define OPT_SAVE() (opt_save = (struct opt_save){optarg, optind, optopt, opterr, optreset}) |
50 | 4 | #define OPT_RESTORE() ({ \ |
51 | 4 | optarg = opt_save.optarg; \ |
52 | 4 | optind = opt_save.optind; \ |
53 | 4 | optopt = opt_save.optopt; \ |
54 | 4 | opterr = opt_save.opterr; \ |
55 | 4 | optreset = opt_save.optreset; \ |
56 | 4 | }) |
57 | | |
58 | | static void |
59 | | cleanupHandler(void) |
60 | 4 | { |
61 | 4 | printf("Cleaning up before exit...\n"); |
62 | 4 | rtpp_shutdown(gconf.cfsp); |
63 | 4 | close(gconf.tfd); |
64 | 4 | } |
65 | | |
66 | | static unsigned char deterministic_data[32] = { |
67 | | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
68 | | 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
69 | | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
70 | | 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F |
71 | | }; |
72 | | static size_t offset = 0; |
73 | | |
74 | | static int |
75 | 10.4k | dRAND_bytes(unsigned char *buf, int num) { |
76 | 376k | for (int i = 0; i < num; i++) { |
77 | 366k | buf[i] = deterministic_data[offset]; |
78 | 366k | offset = (offset + 1) % sizeof(deterministic_data); |
79 | 366k | } |
80 | 10.4k | return 1; // Return 1 to indicate success |
81 | 10.4k | } |
82 | | |
83 | | static int |
84 | | dRAND_status(void) |
85 | 0 | { |
86 | |
|
87 | 0 | return 1; |
88 | 0 | } |
89 | | |
90 | | RAND_METHOD dummy = { |
91 | | .bytes = &dRAND_bytes, |
92 | | .pseudorand = &dRAND_bytes, |
93 | | .status = &dRAND_status, |
94 | | }; |
95 | | |
96 | | void |
97 | | SeedRNGs(void) |
98 | 2.41k | { |
99 | 2.41k | offset = 0; |
100 | 2.41k | seedrandom(); |
101 | 2.41k | } |
102 | | |
103 | | struct RTPPInitializeParams RTPPInitializeParams = { |
104 | | .ttl = "1", |
105 | | .setup_ttl = "1", |
106 | | .socket = NULL, |
107 | | .debug_level = "crit", |
108 | | .notify_socket = "tcp:127.0.0.1:9642", |
109 | | .rec_spool_dir = "/tmp", |
110 | | .rec_final_dir = ".", |
111 | | .modules = {"acct_csv", "catch_dtmf", "dtls_gw", "ice_lite", NULL}, |
112 | | }; |
113 | | |
114 | | extern void __afl_manual_init(void) __attribute__((__weak__)); |
115 | | |
116 | | int |
117 | | RTPPInitialize(void) |
118 | 4 | { |
119 | 4 | const struct RTPPInitializeParams *rp = &RTPPInitializeParams; |
120 | 4 | const char *argv[] = { |
121 | 4 | "rtpproxy", |
122 | 4 | "-f", |
123 | 4 | "-T", rp->ttl, |
124 | 4 | "-W", rp->setup_ttl, |
125 | 4 | "-s", (rp->socket != NULL) ? rp->socket : tmpnam(NULL), |
126 | 4 | "-d", rp->debug_level, |
127 | 4 | "-n", rp->notify_socket, |
128 | 4 | "-S", rp->rec_spool_dir, |
129 | 4 | "-r", rp->rec_final_dir, |
130 | 4 | "--dso", rp->modules[0], |
131 | 4 | "--dso", rp->modules[1], |
132 | 4 | "--dso", rp->modules[2], |
133 | 4 | "--dso", rp->modules[3], |
134 | 4 | }; |
135 | 4 | int argc = howmany(argv, *argv); |
136 | 4 | struct rtpp_cfg *cfsp; |
137 | | |
138 | 4 | if (__afl_manual_init != NULL) |
139 | 0 | __afl_manual_init(); |
140 | | |
141 | 4 | OPT_SAVE(); |
142 | 4 | assert(RAND_set_rand_method(&dummy) == 1); |
143 | 4 | SeedRNGs(); |
144 | 4 | cfsp = rtpp_main(argc, argv); |
145 | 4 | OPT_RESTORE(); |
146 | 4 | if (cfsp == NULL) |
147 | 0 | goto e0; |
148 | 4 | cfsp->no_resolve = 1; |
149 | 4 | gconf.tfd = open("/dev/null", O_WRONLY, 0); |
150 | 4 | if (gconf.tfd < 0) |
151 | 0 | goto e1; |
152 | 4 | gconf.cfsp = cfsp; |
153 | 4 | atexit(cleanupHandler); |
154 | 4 | return (0); |
155 | 0 | e1: |
156 | 0 | cleanupHandler(); |
157 | 0 | e0: |
158 | 0 | return (-1); |
159 | 0 | } |