diff options
-rw-r--r-- | src/socket.c | 34 | ||||
-rw-r--r-- | tests/socket/socket_test.c | 2 | ||||
-rw-r--r-- | tests/socket/socket_test.err | 1 |
3 files changed, 22 insertions, 15 deletions
diff --git a/src/socket.c b/src/socket.c index e8a2ce25..56f01dc7 100644 --- a/src/socket.c +++ b/src/socket.c @@ -316,19 +316,27 @@ int osmo_sock_init2(uint16_t family, uint16_t type, uint8_t proto, } } - /* priotize ipv6 as per RFC */ - if (local_ipv6 && remote_ipv6) - family = AF_INET6; - else if (local_ipv4 && remote_ipv4) - family = AF_INET; - else { - if (local) - freeaddrinfo(local); - if (remote) - freeaddrinfo(remote); - LOGP(DLGLOBAL, LOGL_ERROR, "Unable to find a common protocol (IPv4 or IPv6) for local host: %s and remote host: %s.\n", - local_host, remote_host); - return -ENODEV; + if ((flags & OSMO_SOCK_F_BIND) && (flags & OSMO_SOCK_F_CONNECT)) { + /* prioritize ipv6 as per RFC */ + if (local_ipv6 && remote_ipv6) + family = AF_INET6; + else if (local_ipv4 && remote_ipv4) + family = AF_INET; + else { + if (local) + freeaddrinfo(local); + if (remote) + freeaddrinfo(remote); + LOGP(DLGLOBAL, LOGL_ERROR, + "Unable to find a common protocol (IPv4 or IPv6) " + "for local host: %s and remote host: %s.\n", + local_host, remote_host); + return -ENODEV; + } + } else if ((flags & OSMO_SOCK_F_BIND)) { + family = local_ipv6 ? AF_INET6 : AF_INET; + } else if ((flags & OSMO_SOCK_F_CONNECT)) { + family = remote_ipv6 ? AF_INET6 : AF_INET; } } diff --git a/tests/socket/socket_test.c b/tests/socket/socket_test.c index 5cf20b7e..ae771841 100644 --- a/tests/socket/socket_test.c +++ b/tests/socket/socket_test.c @@ -147,7 +147,7 @@ static int test_sockinit2(void) printf("Checking osmo_sock_init2(AF_UNSPEC) BIND on IPv4\n"); fd = osmo_sock_init2(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "127.0.0.1", 0, NULL, 0, OSMO_SOCK_F_BIND); - OSMO_ASSERT(fd == -ENODEV); /* BUG! */ + OSMO_ASSERT(fd >= 0); talloc_free(name); diff --git a/tests/socket/socket_test.err b/tests/socket/socket_test.err index 37504e77..0f0f8da8 100644 --- a/tests/socket/socket_test.err +++ b/tests/socket/socket_test.err @@ -2,4 +2,3 @@ invalid: both bind and connect flags set: 0.0.0.0:0 invalid: you have to specify either BIND or CONNECT flags Unable to find a common protocol (IPv4 or IPv6) for local host: 127.0.0.1 and remote host: ::1. Unable to find a common protocol (IPv4 or IPv6) for local host: ::1 and remote host: 127.0.0.1. -Unable to find a common protocol (IPv4 or IPv6) for local host: 127.0.0.1 and remote host: (null). |