blob: 274e1e764c1638a8c0e35683ef589d05226ccf35 [file] [log] [blame]
Oliver Smith3a9f2672019-11-20 10:56:35 +01001/* Copyright 2019 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
2 *
3 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20#include <assert.h>
21#include <stdbool.h>
22#include <string.h>
23#include <unistd.h>
24#include <osmocom/core/select.h>
25#include <osmocom/core/application.h>
26#include <osmocom/hlr/logging.h>
27#include <osmocom/mslookup/mslookup.h>
28#include <osmocom/mslookup/mslookup_client.h>
29#include <osmocom/mslookup/mslookup_client_mdns.h>
30#include <osmocom/mslookup/mdns.h>
31#include <osmocom/mslookup/mdns_sock.h>
32
33void *ctx = NULL;
34
35#define TEST_IP OSMO_MSLOOKUP_MDNS_IP4
36#define TEST_PORT OSMO_MSLOOKUP_MDNS_PORT
37#define TEST_DOMAIN_SUFFIX "mslookup_client_mdns_test.dgsm.osmocom.org"
38
39/*
40 * Test server (emulates the mDNS server in OsmoHLR) and client
41 */
42struct osmo_mdns_sock *server_mc;
43
44
45static void server_reply(struct osmo_mslookup_query *query, uint16_t packet_id)
46{
47 struct osmo_mslookup_result result = {0};
48 struct msgb *msg;
49
50 result.rc = OSMO_MSLOOKUP_RC_RESULT;
51 result.age = 3;
52 osmo_sockaddr_str_from_str(&result.host_v4, "42.42.42.42", 444);
53 osmo_sockaddr_str_from_str(&result.host_v6, "1122:3344:5566:7788:99aa:bbcc:ddee:ff00", 666);
54
55 msg = osmo_mdns_result_encode(ctx, packet_id, query, &result, TEST_DOMAIN_SUFFIX);
56 OSMO_ASSERT(msg);
57 OSMO_ASSERT(osmo_mdns_sock_send(server_mc, msg) == 0);
58}
59
60static int server_recv(struct osmo_fd *osmo_fd, unsigned int what)
61{
62 int n;
63 uint8_t buffer[1024];
64 uint16_t packet_id;
65 struct osmo_mslookup_query *query;
66
67 fprintf(stderr, "%s\n", __func__);
68
69 /* Parse the message and print it */
70 n = read(osmo_fd->fd, buffer, sizeof(buffer));
71 OSMO_ASSERT(n >= 0);
72
73 query = osmo_mdns_query_decode(ctx, buffer, n, &packet_id, TEST_DOMAIN_SUFFIX);
74 if (!query)
75 return -1; /* server receiving own answer is expected */
76
77 fprintf(stderr, "received request\n");
78 server_reply(query, packet_id);
79 talloc_free(query);
80 return n;
81}
82
Harald Welte7a476532022-11-03 11:38:41 +010083static void server_init(void)
Oliver Smith3a9f2672019-11-20 10:56:35 +010084{
85 fprintf(stderr, "%s\n", __func__);
86 server_mc = osmo_mdns_sock_init(ctx, TEST_IP, TEST_PORT, server_recv, NULL, 0);
87 OSMO_ASSERT(server_mc);
88}
89
Harald Welte7a476532022-11-03 11:38:41 +010090static void server_stop(void)
Oliver Smith3a9f2672019-11-20 10:56:35 +010091{
92 fprintf(stderr, "%s\n", __func__);
93 OSMO_ASSERT(server_mc);
94 osmo_mdns_sock_cleanup(server_mc);
95 server_mc = NULL;
96}
97
98struct osmo_mslookup_client* client;
99struct osmo_mslookup_client_method* client_method;
100
Harald Welte7a476532022-11-03 11:38:41 +0100101static void client_init(void)
Oliver Smith3a9f2672019-11-20 10:56:35 +0100102{
103 fprintf(stderr, "%s\n", __func__);
104 client = osmo_mslookup_client_new(ctx);
105 OSMO_ASSERT(client);
106 client_method = osmo_mslookup_client_add_mdns(client, TEST_IP, TEST_PORT, 1337, TEST_DOMAIN_SUFFIX);
107 OSMO_ASSERT(client_method);
108}
109
110static void client_recv(struct osmo_mslookup_client *client, uint32_t request_handle,
111 const struct osmo_mslookup_query *query, const struct osmo_mslookup_result *result)
112{
113 char buf[256];
114 fprintf(stderr, "%s\n", __func__);
115 fprintf(stderr, "client_recv(): %s\n", osmo_mslookup_result_name_b(buf, sizeof(buf), query, result));
116
117 osmo_mslookup_client_request_cancel(client, request_handle);
118}
119
Harald Welte7a476532022-11-03 11:38:41 +0100120static void client_query(void)
Oliver Smith3a9f2672019-11-20 10:56:35 +0100121{
122 struct osmo_mslookup_id id = {.type = OSMO_MSLOOKUP_ID_IMSI,
123 .imsi = "123456789012345"};
124 const struct osmo_mslookup_query query = {
125 .service = "gsup.hlr",
126 .id = id,
127 };
128 struct osmo_mslookup_query_handling handling = {
129 .result_timeout_milliseconds = 2000,
130 .result_cb = client_recv,
131 };
132
133 fprintf(stderr, "%s\n", __func__);
134 osmo_mslookup_client_request(client, &query, &handling);
135}
136
Harald Welte7a476532022-11-03 11:38:41 +0100137static void client_stop(void)
Oliver Smith3a9f2672019-11-20 10:56:35 +0100138{
139 fprintf(stderr, "%s\n", __func__);
140 osmo_mslookup_client_free(client);
141 client = NULL;
142}
143const struct timeval fake_time_start_time = { 0, 0 };
144
145#define fake_time_passes(secs, usecs) do \
146{ \
147 struct timeval diff; \
148 osmo_gettimeofday_override_add(secs, usecs); \
149 osmo_clock_override_add(CLOCK_MONOTONIC, secs, usecs * 1000); \
150 timersub(&osmo_gettimeofday_override_time, &fake_time_start_time, &diff); \
151 LOGP(DMSLOOKUP, LOGL_DEBUG, "Total time passed: %d.%06d s\n", \
152 (int)diff.tv_sec, (int)diff.tv_usec); \
153 osmo_timers_prepare(); \
154 osmo_timers_update(); \
155} while (0)
156
Harald Welte7a476532022-11-03 11:38:41 +0100157static void fake_time_start(void)
Oliver Smith3a9f2672019-11-20 10:56:35 +0100158{
159 struct timespec *clock_override;
160
161 osmo_gettimeofday_override_time = fake_time_start_time;
162 osmo_gettimeofday_override = true;
163 clock_override = osmo_clock_override_gettimespec(CLOCK_MONOTONIC);
164 OSMO_ASSERT(clock_override);
165 clock_override->tv_sec = fake_time_start_time.tv_sec;
166 clock_override->tv_nsec = fake_time_start_time.tv_usec * 1000;
167 osmo_clock_override_enable(CLOCK_MONOTONIC, true);
168 fake_time_passes(0, 0);
169}
Harald Welte7a476532022-11-03 11:38:41 +0100170static void test_server_client(void)
Oliver Smith3a9f2672019-11-20 10:56:35 +0100171{
172 fprintf(stderr, "-- %s --\n", __func__);
173 server_init();
174 client_init();
175 client_query();
176
177 /* Let the server receive the query and indirectly call server_recv(). As side effect of using the same IP and
178 * port, the client will also receive its own question. The client will dismiss its own question, as it is just
179 * looking for answers. */
180 OSMO_ASSERT(osmo_select_main_ctx(1) == 1);
181
182 /* Let the mslookup client receive the answer (also same side effect as above). It does not call the callback
183 * (client_recv()) just yet, because it is waiting for the best result within two seconds. */
184 OSMO_ASSERT(osmo_select_main_ctx(1) == 1);
185
186 /* Time flies by, client_recv() gets called. */
187 fake_time_passes(5, 0);
188
189 server_stop();
190 client_stop();
191}
192
Harald Welte7a476532022-11-03 11:38:41 +0100193bool is_multicast_enabled(void)
Oliver Smith3a9f2672019-11-20 10:56:35 +0100194{
195 bool ret = true;
196 struct addrinfo *ai;
197 int sock;
198 struct addrinfo hints = {0};
199 struct ip_mreq multicast_req = {0};
200 in_addr_t iface = INADDR_ANY;
201
202 hints.ai_family = PF_UNSPEC;
203 hints.ai_socktype = SOCK_DGRAM;
204 hints.ai_flags = (AI_PASSIVE | AI_NUMERICHOST);
205 assert(getaddrinfo("239.192.23.42", "4266", &hints, &ai) == 0);
206
207 sock = socket(ai->ai_family, ai->ai_socktype, 0);
208 assert(sock != -1);
209 assert(setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, (char*)&iface, sizeof(iface)) != -1);
210
211 memcpy(&multicast_req.imr_multiaddr, &((struct sockaddr_in*)(ai->ai_addr))->sin_addr,
212 sizeof(multicast_req.imr_multiaddr));
213 multicast_req.imr_interface.s_addr = htonl(INADDR_ANY);
214
215 if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&multicast_req, sizeof(multicast_req)) == -1)
216 ret = false;
217
218 freeaddrinfo(ai);
219 return ret;
220}
221
222/*
223 * Run all tests
224 */
Harald Welte7a476532022-11-03 11:38:41 +0100225int main(int argc, char **argv)
Oliver Smith3a9f2672019-11-20 10:56:35 +0100226{
227 if (!is_multicast_enabled()) {
Oliver Smith5424dcb2020-01-30 10:12:17 +0100228 fprintf(stderr, "ERROR: multicast is disabled! (OS#4361)");
229 return 1;
Oliver Smith3a9f2672019-11-20 10:56:35 +0100230 }
231
232 talloc_enable_null_tracking();
233 ctx = talloc_named_const(NULL, 0, "main");
234 osmo_init_logging2(ctx, NULL);
235
Pau Espin Pedrold6993ea2021-02-19 13:20:18 +0100236 log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_NONE);
Oliver Smith3a9f2672019-11-20 10:56:35 +0100237 log_set_print_level(osmo_stderr_target, 0);
238 log_set_print_category(osmo_stderr_target, 0);
239 log_set_print_category_hex(osmo_stderr_target, 0);
240 log_set_use_color(osmo_stderr_target, 0);
241 log_set_category_filter(osmo_stderr_target, DMSLOOKUP, true, LOGL_DEBUG);
242
243 fake_time_start();
244
245 test_server_client();
246
247 log_fini();
248
249 OSMO_ASSERT(talloc_total_blocks(ctx) == 1);
250 talloc_free(ctx);
251 OSMO_ASSERT(talloc_total_blocks(NULL) == 1);
252 talloc_disable_null_tracking();
253
254 return 0;
255}