Harald Welte | 9104597 | 2020-03-08 22:23:30 +0100 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include <unistd.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <stdint.h> |
| 5 | #include <errno.h> |
| 6 | |
| 7 | #include <sys/socket.h> |
| 8 | #include <netinet/in.h> |
| 9 | #include <arpa/inet.h> |
| 10 | |
| 11 | #include <osmocom/core/utils.h> |
| 12 | #include <osmocom/core/talloc.h> |
| 13 | #include <osmocom/core/logging.h> |
| 14 | #include <osmocom/core/application.h> |
| 15 | #include <osmocom/trau/osmo_ortp.h> |
| 16 | |
| 17 | static void *tall_test; |
| 18 | |
| 19 | static struct osmo_rtp_socket *create_bind_any(void) |
| 20 | { |
| 21 | struct osmo_rtp_socket *rs; |
| 22 | int i; |
| 23 | |
| 24 | rs = osmo_rtp_socket_create(tall_test, 0); |
| 25 | for (i = 16384; i < 65535; i+=2) { |
| 26 | if (osmo_rtp_socket_bind(rs, "0.0.0.0", i) == 0) |
| 27 | return rs; |
| 28 | } |
| 29 | osmo_rtp_socket_free(rs); |
| 30 | return NULL; |
| 31 | } |
| 32 | |
| 33 | /* test if binding two sockets to the same local port fails (See OS#4444) */ |
| 34 | static void test_bind_fail(void) |
| 35 | { |
| 36 | struct osmo_rtp_socket *rs[2]; |
| 37 | uint32_t ip; |
| 38 | int rc, port[2]; |
| 39 | |
| 40 | printf("First bind to any random local port...\n"); |
| 41 | rs[0] = create_bind_any(); |
| 42 | OSMO_ASSERT(rs[0]); |
| 43 | |
| 44 | /* then obtain the local port */ |
| 45 | rc = osmo_rtp_get_bound_ip_port(rs[0], &ip, &port[0]); |
| 46 | OSMO_ASSERT(rc == 0); |
| 47 | |
| 48 | printf("Now try to bind another socket to the same port: "); |
| 49 | rs[1] = osmo_rtp_socket_create(tall_test, 0); |
| 50 | OSMO_ASSERT(rs[1]); |
| 51 | rc = osmo_rtp_socket_bind(rs[1], "0.0.0.0", port[0]); |
| 52 | if (rc == -1 && errno == EADDRINUSE) |
| 53 | printf("OK (EADDRINUSE)\n"); |
| 54 | else { |
| 55 | printf("FAILED - second bind to port %u worked\n", port[0]); |
| 56 | fflush(stdout); |
| 57 | //OSMO_ASSERT(0); |
| 58 | } |
| 59 | |
| 60 | osmo_rtp_socket_free(rs[0]); |
| 61 | osmo_rtp_socket_free(rs[1]); |
| 62 | } |
| 63 | |
| 64 | #define DTEST 0 |
| 65 | |
| 66 | static const struct log_info_cat bts_test_cat[] = { |
| 67 | [DTEST] = { |
| 68 | .name = "DTEST", |
| 69 | .description = "test", |
| 70 | .color = "\033[1;35m", |
| 71 | .enabled = 1, .loglevel = LOGL_NOTICE, |
| 72 | }, |
| 73 | }; |
| 74 | |
| 75 | static const struct log_info bts_test_log_info = { |
| 76 | .filter_fn = NULL, |
| 77 | .cat = bts_test_cat, |
| 78 | .num_cat = ARRAY_SIZE(bts_test_cat), |
| 79 | }; |
| 80 | |
| 81 | |
| 82 | int main(int argc, char **argv) |
| 83 | { |
| 84 | tall_test = talloc_named_const(NULL, 1, "rtp_test"); |
| 85 | osmo_init_logging2(tall_test, &bts_test_log_info); |
| 86 | osmo_rtp_init(tall_test); |
| 87 | |
| 88 | test_bind_fail(); |
| 89 | |
| 90 | exit(0); |
| 91 | } |
| 92 | |