Debian Codegraph @

test/rist/test_oob_read.c

raw symbols SCIP
1 /* librist. Copyright © 2024 SipRadius LLC. All right reserved.
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Coverage for the polling out-of-band receive API (rist_oob_read).
5 * A sender pushes OOB packets to a receiver that has oob enabled with a
6 * NULL callback; the receiver must be able to drain them through
7 * rist_oob_read() and see the exact payloads back.
8 */
9
10 #include "librist/librist.h"
11 #include "rist-private.h"
12 #include <stdatomic.h>
13 #include <inttypes.h>
14 #include <string.h>
15
16 #ifdef _WIN32
17 #include <windows.h>
18 #define msleep(ms) Sleep(ms)
19 #else
20 #include <unistd.h>
21 #define msleep(ms) usleep((ms) * 1000)
22 #endif
23
24 #define OOB_PAYLOAD_PREFIX "RIST-OOB-TEST-PACKET-"
25 #define OOB_TARGET_COUNT 25
26
27 atomic_ulong stop;
28
29 struct rist_logging_settings *logging_settings_sender = NULL;
30 struct rist_logging_settings *logging_settings_receiver = NULL;
31 char *senderstring = "sender";
32 char *receiverstring = "receiver";
33
34 static struct rist_peer *sender_peer = NULL;
35
36 static int log_callback(void *arg, int level, const char *msg) {
37 /* Print but never fail the test on a log line: oob is best effort and a
38 * transient "no oob peer" during connection setup is expected. */
39 if (level <= RIST_LOG_ERROR)
40 fprintf(stdout, "[%s] [ERROR] %s", (char *)arg, msg);
41 else
42 fprintf(stdout, "[%s] %s", (char *)arg, msg);
43 return 0;
44 }
45
46 static struct rist_ctx *setup_rist_receiver(int profile, const char *url) {
47 struct rist_ctx *ctx;
48 if (rist_receiver_create(&ctx, profile, logging_settings_receiver) != 0)
49 return NULL;
50 struct rist_peer_config *peer_config = NULL;
51 if (rist_parse_address2(url, (void *)&peer_config))
52 return NULL;
53 struct rist_peer *peer;
54 if (rist_peer_create(ctx, &peer, peer_config) == -1) {
55 free((void *)peer_config);
56 return NULL;
57 }
58 free((void *)peer_config);
59 if (rist_oob_callback_set(ctx, NULL, NULL) != 0) {
60 rist_log(logging_settings_receiver, RIST_LOG_ERROR, "Could not enable oob on receiver\n");
61 return NULL;
62 }
63 if (rist_start(ctx) == -1)
64 return NULL;
65 return ctx;
66 }
67
68 static struct rist_ctx *setup_rist_sender(int profile, const char *url) {
69 struct rist_ctx *ctx;
70 if (rist_sender_create(&ctx, profile, 0, logging_settings_sender) != 0)
71 return NULL;
72 const struct rist_peer_config *peer_config = NULL;
73 if (rist_parse_address2(url, (void *)&peer_config))
74 return NULL;
75 if (rist_peer_create(ctx, &sender_peer, peer_config) == -1) {
76 free((void *)peer_config);
77 return NULL;
78 }
79 free((void *)peer_config);
80 if (rist_oob_callback_set(ctx, NULL, NULL) != 0) {
81 rist_log(logging_settings_sender, RIST_LOG_ERROR, "Could not enable oob on sender\n");
82 return NULL;
83 }
84 if (rist_start(ctx) == -1)
85 return NULL;
86 return ctx;
87 }
88
89 static PTHREAD_START_FUNC(send_oob, arg) {
90 struct rist_ctx *sender = arg;
91 char buffer[128];
92 int counter = 0;
93 /* ~6s worth of attempts at ~2ms spacing */
94 while (counter < 3000 && !atomic_load(&stop)) {
95 int len = snprintf(buffer, sizeof(buffer), "%s%d", OOB_PAYLOAD_PREFIX, counter);
96 struct rist_oob_block oob = { 0 };
97 oob.peer = sender_peer;
98 oob.payload = buffer;
99 oob.payload_len = (size_t)len + 1; /* include NUL for easy compare */
100 rist_oob_write(sender, &oob); /* best effort; ignore transient errors */
101 counter++;
102 msleep(2);
103 }
104 return 0;
105 }
106
107 int main(int argc, char *argv[]) {
108 int profile = (argc > 1) ? atoi(argv[1]) : 1;
109 const char *recv_url = (argc > 2) ? argv[2] : "rist://@127.0.0.1:8190";
110 const char *send_url = (argc > 3) ? argv[3] : "rist://127.0.0.1:8190";
111
112 atomic_init(&stop, 0);
113
114 fprintf(stdout, "Testing oob polling read on profile %d (recv %s, send %s)\n",
115 profile, recv_url, send_url);
116
117 if (rist_logging_set(&logging_settings_sender, RIST_LOG_INFO, log_callback, senderstring, NULL, stderr) != 0)
118 return 99;
119 if (rist_logging_set(&logging_settings_receiver, RIST_LOG_INFO, log_callback, receiverstring, NULL, stderr) != 0)
120 return 99;
121
122 struct rist_ctx *receiver_ctx = setup_rist_receiver(profile, recv_url);
123 struct rist_ctx *sender_ctx = setup_rist_sender(profile, send_url);
124 int ret = 0;
125 if (!receiver_ctx || !sender_ctx) {
126 ret = 99;
127 goto out;
128 }
129
130 pthread_t send_loop;
131 if (pthread_create(&send_loop, NULL, send_oob, (void *)sender_ctx) != 0) {
132 ret = 99;
133 goto out;
134 }
135
136 int received = 0;
137 int last_seen = -1;
138 bool ordered = true;
139 /* poll for up to ~8 seconds */
140 for (int i = 0; i < 4000 && received < OOB_TARGET_COUNT; i++) {
141 const struct rist_oob_block *oob = NULL;
142 int avail = rist_oob_read(receiver_ctx, &oob);
143 if (avail < 0) {
144 fprintf(stderr, "rist_oob_read returned error %d\n", avail);
145 ret = 1;
146 break;
147 }
148 if (avail == 0 || oob == NULL) {
149 msleep(2);
150 continue;
151 }
152 /* validate payload shape */
153 if (oob->payload_len < strlen(OOB_PAYLOAD_PREFIX) + 1 ||
154 memcmp(oob->payload, OOB_PAYLOAD_PREFIX, strlen(OOB_PAYLOAD_PREFIX)) != 0) {
155 fprintf(stderr, "Unexpected oob payload (len %zu)\n", oob->payload_len);
156 ret = 1;
157 break;
158 }
159 int seq = atoi((const char *)oob->payload + strlen(OOB_PAYLOAD_PREFIX));
160 if (seq <= last_seen)
161 ordered = false; /* not fatal: oob is unordered/best effort, just note */
162 last_seen = seq;
163 if (oob->peer == NULL) {
164 fprintf(stderr, "oob block missing peer\n");
165 ret = 1;
166 break;
167 }
168 received++;
169 }
170
171 atomic_store(&stop, 1);
172 pthread_join(send_loop, NULL);
173
174 fprintf(stdout, "Received %d oob packets (target %d), ordered=%s\n",
175 received, OOB_TARGET_COUNT, ordered ? "yes" : "no");
176 if (ret == 0 && received < OOB_TARGET_COUNT) {
177 fprintf(stderr, "FAIL: only %d/%d oob packets read back\n", received, OOB_TARGET_COUNT);
178 ret = 1;
179 }
180
181 out:
182 if (sender_ctx)
183 rist_destroy(sender_ctx);
184 if (receiver_ctx)
185 rist_destroy(receiver_ctx);
186 free(logging_settings_sender);
187 free(logging_settings_receiver);
188 if (ret > 0 && ret != 99) {
189 fprintf(stderr, "FAIL\n");
190 return ret;
191 }
192 if (ret == 99)
193 return 99;
194 fprintf(stdout, "OK\n");
195 return 0;
196 }