diff options
author | Harald Welte <laforge@gnumonks.org> | 2017-04-08 20:52:33 +0200 |
---|---|---|
committer | Harald Welte <laforge@gnumonks.org> | 2017-04-09 21:46:21 +0200 |
commit | dda70fca7979d86e04bba9ba5bad32162327550c (patch) | |
tree | a2910583bb4b84ac60d41038ce906e68fd5e481e /tests | |
parent | acd08feb8f75827555a9ef38b890870fed3388ea (diff) |
Add osmo_sock_init2() function, allowing both BIND *and* CONNECT
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')
-rw-r--r-- | tests/socket/socket_test.c | 52 | ||||
-rw-r--r-- | tests/socket/socket_test.err | 1 | ||||
-rw-r--r-- | tests/socket/socket_test.ok | 4 |
3 files changed, 57 insertions, 0 deletions
diff --git a/tests/socket/socket_test.c b/tests/socket/socket_test.c index 5b6abc42..b56d50c0 100644 --- a/tests/socket/socket_test.c +++ b/tests/socket/socket_test.c @@ -73,6 +73,57 @@ 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); +#ifndef __FreeBSD__ + /* For some reason, on the jenkins.osmocom.org build slave with + * FreeBSD 10 inside a jail, it fails. Works fine on laforge's + * FreeBSD 10 or 11 VM at home */ + OSMO_ASSERT(!strncmp(name, "(127.0.0.1:53<->127.0.0.1", 25)); +#endif + talloc_free(name); + + return 0; +} + + const struct log_info_cat default_categories[] = { }; @@ -88,6 +139,7 @@ int main(int argc, char *argv[]) log_set_print_filename(osmo_stderr_target, 0); test_sockinit(); + test_sockinit2(); return EXIT_SUCCESS; } diff --git a/tests/socket/socket_test.err b/tests/socket/socket_test.err index 5367239c..ed6e1865 100644 --- a/tests/socket/socket_test.err +++ b/tests/socket/socket_test.err @@ -1 +1,2 @@ invalid: both bind and connect flags set: 0.0.0.0:0 +invalid: you have to specify either BIND or CONNECT flags diff --git a/tests/socket/socket_test.ok b/tests/socket/socket_test.ok index d6ec40ed..4b24fbce 100644 --- a/tests/socket/socket_test.ok +++ b/tests/socket/socket_test.ok @@ -1,3 +1,7 @@ Checking osmo_sock_init() with bind to a random local UDP port Checking for OSMO_SOCK_F_NONBLOCK Checking for invalid flags +Checking osmo_sock_init2() with bind to a random local UDP port +Checking osmo_sock_init2() for OSMO_SOCK_F_NONBLOCK +Checking osmo_sock_init2() for invalid flags +Checking osmo_sock_init2() for combined BIND + CONNECT |