blob: e68243c65ef8db2745a02da79b34c1209117cdcd [file] [log] [blame]
Harald Welte4ffb43f2017-01-27 10:29:49 +01001/*
2 * (C) 2017 by Harald Welte <laforge@gnumonks.org>
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 General Public License as published by
7 * the Free Software Foundation; either version 2 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 General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 */
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <unistd.h>
24#include <string.h>
25#include <fcntl.h>
26
27#include <sys/socket.h>
28#include <arpa/inet.h>
29#include <netinet/in.h>
30
Philipp Maier6f0f5602017-02-09 14:09:06 +010031#include <osmocom/core/application.h>
Harald Welte4ffb43f2017-01-27 10:29:49 +010032#include <osmocom/core/utils.h>
33#include <osmocom/core/socket.h>
Philipp Maier6f0f5602017-02-09 14:09:06 +010034#include <osmocom/core/logging.h>
Harald Welte4ffb43f2017-01-27 10:29:49 +010035
36#include "../config.h"
37
Neels Hofmeyra829b452018-04-05 03:02:35 +020038void *ctx = NULL;
39
Harald Welte4ffb43f2017-01-27 10:29:49 +010040static int test_sockinit(void)
41{
42 int fd, rc;
43 char *name;
44
45 printf("Checking osmo_sock_init() with bind to a random local UDP port\n");
46 fd = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
47 "0.0.0.0", 0, OSMO_SOCK_F_BIND);
48 OSMO_ASSERT(fd >= 0);
Neels Hofmeyra829b452018-04-05 03:02:35 +020049 name = osmo_sock_get_name(ctx, fd);
Harald Welte4ffb43f2017-01-27 10:29:49 +010050 /* expect it to be not connected. We cannot match on INADDR_ANY,
51 * as apparently that won't work on FreeBSD if there's only one
52 * address (e.g. 127.0.0.1) assigned to the entire system, like
53 * the Osmocom FreeBSD build slaves */
Neels Hofmeyr1f82d0a2017-06-21 22:54:46 +020054 OSMO_ASSERT(!strncmp(name, "(r=NULL<->", 9));
Harald Welte4ffb43f2017-01-27 10:29:49 +010055 talloc_free(name);
56 /* expect it to be blocking */
57 rc = fcntl(fd, F_GETFL);
58 OSMO_ASSERT(!(rc & O_NONBLOCK));
59 close(fd);
60
61 printf("Checking for OSMO_SOCK_F_NONBLOCK\n");
62 fd = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
63 "0.0.0.0", 0, OSMO_SOCK_F_BIND|OSMO_SOCK_F_NONBLOCK);
64 OSMO_ASSERT(fd >= 0);
65 /* expect it to be blocking */
66 rc = fcntl(fd, F_GETFL);
67 OSMO_ASSERT(rc & O_NONBLOCK);
68 close(fd);
69
70 printf("Checking for invalid flags\n");
71 fd = osmo_sock_init(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
72 "0.0.0.0", 0, OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT);
73 OSMO_ASSERT(fd < 0);
74
75 return 0;
76}
77
Harald Weltedda70fc2017-04-08 20:52:33 +020078static int test_sockinit2(void)
79{
80 int fd, rc;
81 char *name;
82
83 printf("Checking osmo_sock_init2() with bind to a random local UDP port\n");
84 fd = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
85 "0.0.0.0", 0, NULL, 0, OSMO_SOCK_F_BIND);
86 OSMO_ASSERT(fd >= 0);
Neels Hofmeyra829b452018-04-05 03:02:35 +020087 name = osmo_sock_get_name(ctx, fd);
Harald Weltedda70fc2017-04-08 20:52:33 +020088 /* expect it to be not connected. We cannot match on INADDR_ANY,
89 * as apparently that won't work on FreeBSD if there's only one
90 * address (e.g. 127.0.0.1) assigned to the entire system, like
91 * the Osmocom FreeBSD build slaves */
Neels Hofmeyr1f82d0a2017-06-21 22:54:46 +020092 OSMO_ASSERT(!strncmp(name, "(r=NULL<->", 9));
Harald Weltedda70fc2017-04-08 20:52:33 +020093 talloc_free(name);
94 /* expect it to be blocking */
95 rc = fcntl(fd, F_GETFL);
96 OSMO_ASSERT(!(rc & O_NONBLOCK));
97 close(fd);
98
99 printf("Checking osmo_sock_init2() for OSMO_SOCK_F_NONBLOCK\n");
100 fd = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
101 "0.0.0.0", 0, NULL, 0, OSMO_SOCK_F_BIND|OSMO_SOCK_F_NONBLOCK);
102 OSMO_ASSERT(fd >= 0);
103 /* expect it to be blocking */
104 rc = fcntl(fd, F_GETFL);
105 OSMO_ASSERT(rc & O_NONBLOCK);
106 close(fd);
107
108 printf("Checking osmo_sock_init2() for invalid flags\n");
109 fd = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, "0.0.0.0", 0, NULL, 0, 0);
110 OSMO_ASSERT(fd < 0);
111
112 printf("Checking osmo_sock_init2() for combined BIND + CONNECT\n");
113 fd = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, "127.0.0.1", 0, "127.0.0.1", 53,
114 OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT);
115 OSMO_ASSERT(fd >= 0);
Neels Hofmeyra829b452018-04-05 03:02:35 +0200116 name = osmo_sock_get_name(ctx, fd);
Harald Weltedda70fc2017-04-08 20:52:33 +0200117#ifndef __FreeBSD__
118 /* For some reason, on the jenkins.osmocom.org build slave with
119 * FreeBSD 10 inside a jail, it fails. Works fine on laforge's
120 * FreeBSD 10 or 11 VM at home */
Neels Hofmeyr1f82d0a2017-06-21 22:54:46 +0200121 OSMO_ASSERT(!strncmp(name, "(r=127.0.0.1:53<->l=127.0.0.1", 29));
Harald Weltedda70fc2017-04-08 20:52:33 +0200122#endif
Alexander Couzens2c962f52020-06-03 00:28:02 +0200123
124 printf("Checking osmo_sock_init2(AF_UNSPEC) must fail on mixed IPv4 & IPv6\n");
125 fd = osmo_sock_init2(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "127.0.0.1", 0, "::1", 53,
126 OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT);
127 OSMO_ASSERT(fd < 0);
128
129 printf("Checking osmo_sock_init2(AF_UNSPEC) must fail on mixed IPv6 & IPv4\n");
130 fd = osmo_sock_init2(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "::1", 0, "127.0.0.1", 53,
131 OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT);
132 OSMO_ASSERT(fd < 0);
133
134 printf("Checking osmo_sock_init2(AF_UNSPEC) BIND + CONNECT on IPv4\n");
135 fd = osmo_sock_init2(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "127.0.0.1", 0, "127.0.0.1", 53,
136 OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT);
137 OSMO_ASSERT(fd >= 0);
138
139 printf("Checking osmo_sock_init2(AF_UNSPEC) BIND + CONNECT on IPv6\n");
140 fd = osmo_sock_init2(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "::1", 0, "::1", 53,
141 OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT);
142 OSMO_ASSERT(fd >= 0);
143
Harald Weltedda70fc2017-04-08 20:52:33 +0200144 talloc_free(name);
145
146 return 0;
147}
148
149
Philipp Maier6f0f5602017-02-09 14:09:06 +0100150const struct log_info_cat default_categories[] = {
151};
152
153static struct log_info info = {
154 .cat = default_categories,
155 .num_cat = ARRAY_SIZE(default_categories),
156};
157
Harald Welte4ffb43f2017-01-27 10:29:49 +0100158int main(int argc, char *argv[])
159{
Neels Hofmeyra829b452018-04-05 03:02:35 +0200160 ctx = talloc_named_const(NULL, 0, "socket_test");
161 osmo_init_logging2(ctx, &info);
Philipp Maier6f0f5602017-02-09 14:09:06 +0100162 log_set_use_color(osmo_stderr_target, 0);
163 log_set_print_filename(osmo_stderr_target, 0);
164
Harald Welte4ffb43f2017-01-27 10:29:49 +0100165 test_sockinit();
Harald Weltedda70fc2017-04-08 20:52:33 +0200166 test_sockinit2();
Philipp Maier6f0f5602017-02-09 14:09:06 +0100167
168 return EXIT_SUCCESS;
Harald Welte4ffb43f2017-01-27 10:29:49 +0100169}