aboutsummaryrefslogtreecommitdiffstats
path: root/tests/socket/socket_test.c
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2017-04-08 20:52:33 +0200
committerHarald Welte <laforge@gnumonks.org>2017-04-08 21:26:12 +0200
commitff60be3726fc766e27ad7d5890b11405b8c70041 (patch)
treeaa9160f84ffaac2d7f9a1b93c50bfcda9a5d9ac6 /tests/socket/socket_test.c
parent18a62b04887d8c6f6d338404330391a66c337b69 (diff)
Add osmo_sock_init2() function, allowing both BIND *and* CONNECTlaforge/sock2
The old osmo_sock_init() function allows only either a bind (for a server socket), or a connect (for a client socket), but not both together. So there's no way to have a client socket that is bound to a specific local IP and/or port, which is needed for some use cases. Change-Id: Idab124bcca47872f55311a82d6818aed590965e6
Diffstat (limited to 'tests/socket/socket_test.c')
-rw-r--r--tests/socket/socket_test.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/tests/socket/socket_test.c b/tests/socket/socket_test.c
index 5b6abc42..57425ef5 100644
--- a/tests/socket/socket_test.c
+++ b/tests/socket/socket_test.c
@@ -73,6 +73,52 @@ static int test_sockinit(void)
return 0;
}
+static int test_sockinit2(void)
+{
+ int fd, rc;
+ char *name;
+
+ printf("Checking osmo_sock_init2() with bind to a random local UDP port\n");
+ fd = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
+ "0.0.0.0", 0, NULL, 0, OSMO_SOCK_F_BIND);
+ OSMO_ASSERT(fd >= 0);
+ name = osmo_sock_get_name(NULL, fd);
+ /* expect it to be not connected. We cannot match on INADDR_ANY,
+ * as apparently that won't work on FreeBSD if there's only one
+ * address (e.g. 127.0.0.1) assigned to the entire system, like
+ * the Osmocom FreeBSD build slaves */
+ OSMO_ASSERT(!strncmp(name, "(NULL<->", 7));
+ talloc_free(name);
+ /* expect it to be blocking */
+ rc = fcntl(fd, F_GETFL);
+ OSMO_ASSERT(!(rc & O_NONBLOCK));
+ close(fd);
+
+ printf("Checking osmo_sock_init2() for OSMO_SOCK_F_NONBLOCK\n");
+ fd = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP,
+ "0.0.0.0", 0, NULL, 0, OSMO_SOCK_F_BIND|OSMO_SOCK_F_NONBLOCK);
+ OSMO_ASSERT(fd >= 0);
+ /* expect it to be blocking */
+ rc = fcntl(fd, F_GETFL);
+ OSMO_ASSERT(rc & O_NONBLOCK);
+ close(fd);
+
+ printf("Checking osmo_sock_init2() for invalid flags\n");
+ fd = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, "0.0.0.0", 0, NULL, 0, 0);
+ OSMO_ASSERT(fd < 0);
+
+ printf("Checking osmo_sock_init2() for combined BIND + CONNECT\n");
+ fd = osmo_sock_init2(AF_INET, SOCK_DGRAM, IPPROTO_UDP, "127.0.0.1", 0, "127.0.0.1", 53,
+ OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT);
+ OSMO_ASSERT(fd >= 0);
+ name = osmo_sock_get_name(NULL, fd);
+ OSMO_ASSERT(!strncmp(name, "(127.0.0.1:53<->127.0.0.1", 25));
+ talloc_free(name);
+
+ return 0;
+}
+
+
const struct log_info_cat default_categories[] = {
};
@@ -88,6 +134,7 @@ int main(int argc, char *argv[])
log_set_print_filename(osmo_stderr_target, 0);
test_sockinit();
+ test_sockinit2();
return EXIT_SUCCESS;
}