Change in libosmocore[master]: osmo_sock_init2: improve support for AF_UNSPEC
laforge has submitted this change. ( https://gerrit.osmocom.org/c/libosmocore/+/19140 ) Change subject: osmo_sock_init2: improve support for AF_UNSPEC .. osmo_sock_init2: improve support for AF_UNSPEC osmo_sock_init2 abstract two calls of getaddrinfo into one. While there aren't problems with AF_INET or AF_INET6. When using AF_UNSPEC there are corner cases when this fails. E.g. calling local_host with "" and remote_host with an IPv6 only address results in setting up a local socket with AF_INET while trying to connect from there towards AF_INET6 will most likely fail. To prevent such cases with AF_UNSPEC, search prio calling any syscalls if local and remote site supports AF_INET or AF_INET6. In case both supported, prefer AF_INET6 Change-Id: I397c633931fd00d4f083955a3c49a40fb002d766 --- M src/socket.c M tests/socket/socket_test.c M tests/socket/socket_test.err M tests/socket/socket_test.ok 4 files changed, 105 insertions(+), 14 deletions(-) Approvals: laforge: Looks good to me, approved fixeria: Looks good to me, but someone else must approve Jenkins Builder: Verified diff --git a/src/socket.c b/src/socket.c index 7fa9ab3..9c60821 100644 --- a/src/socket.c +++ b/src/socket.c @@ -260,22 +260,85 @@ const char *local_host, uint16_t local_port, const char *remote_host, uint16_t remote_port, unsigned int flags) { - struct addrinfo *result, *rp; + struct addrinfo *local = NULL, *remote = NULL, *rp; int sfd = -1, rc, on = 1; + bool local_ipv4 = false, local_ipv6 = false; + bool remote_ipv4 = false, remote_ipv6 = false; + if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == 0) { LOGP(DLGLOBAL, LOGL_ERROR, "invalid: you have to specify either " "BIND or CONNECT flags\n"); return -EINVAL; } + /* figure out local address infos */ + if (flags & OSMO_SOCK_F_BIND) { + local = addrinfo_helper(family, type, proto, local_host, local_port, true); + if (!local) + return -EINVAL; + } + + /* figure out remote address infos */ + if (flags & OSMO_SOCK_F_CONNECT) { + remote = addrinfo_helper(family, type, proto, remote_host, remote_port, false); + if (!remote) { + if (local) + freeaddrinfo(local); + + return -EINVAL; + } + } + + /* It must do a full run to ensure AF_UNSPEC does not fail. +* In case first local valid entry is IPv4 and only remote valid entry +* is IPv6 or vice versa */ + if (family == AF_UNSPEC) { + for (rp = local; rp != NULL; rp = rp->ai_next) { + switch (rp->ai_family) { + case AF_INET: + local_ipv4 = true; + break; + case AF_INET6: + local_ipv6 = true; + break; + } + } + + for (rp = remote; rp != NULL; rp = rp->ai_next) { + switch (rp->ai_family) { + case AF_INET: + remote_ipv4 = true; + break; + case AF_INET6: + remote_ipv6 = true; + break; + } + } + + /* 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; + } + } + /* figure out local side of socket */ if (flags & OSMO_SOCK_F_BIND) { - result = addrinfo_helper(family, type, proto, local_host, local_port, true); - if (!result) - return -EINVAL; + for (rp = local; rp != NULL; rp = rp->ai_next) { + /* When called with AF_UNSPEC, family will set to IPv4 or IPv6 */ + if (rp->ai_family != family) + continue; - for (rp = result; rp != NULL; rp =
Change in libosmocore[master]: osmo_sock_init2: improve support for AF_UNSPEC
laforge has posted comments on this change. ( https://gerrit.osmocom.org/c/libosmocore/+/19140 ) Change subject: osmo_sock_init2: improve support for AF_UNSPEC .. Patch Set 8: Code-Review+2 -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/19140 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: I397c633931fd00d4f083955a3c49a40fb002d766 Gerrit-Change-Number: 19140 Gerrit-PatchSet: 8 Gerrit-Owner: lynxis lazus Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: fixeria Gerrit-Reviewer: laforge Gerrit-Reviewer: lynxis lazus Gerrit-Reviewer: pespin Gerrit-Comment-Date: Thu, 06 Aug 2020 16:40:54 + Gerrit-HasComments: No Gerrit-Has-Labels: Yes Gerrit-MessageType: comment
Change in libosmocore[master]: osmo_sock_init2: improve support for AF_UNSPEC
lynxis lazus has posted comments on this change. ( https://gerrit.osmocom.org/c/libosmocore/+/19140 ) Change subject: osmo_sock_init2: improve support for AF_UNSPEC .. Patch Set 8: > Patch Set 8: > > The unit test should probably be disabled for systems without IPv6 support. We don't even have --enable-ipv6 in our configure script. Every system these days have IPv6 enabled by default. The unit tests just need the ::1 (localhost) to be functional. -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/19140 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: I397c633931fd00d4f083955a3c49a40fb002d766 Gerrit-Change-Number: 19140 Gerrit-PatchSet: 8 Gerrit-Owner: lynxis lazus Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: fixeria Gerrit-Reviewer: laforge Gerrit-Reviewer: lynxis lazus Gerrit-Reviewer: pespin Gerrit-Comment-Date: Tue, 04 Aug 2020 10:37:02 + Gerrit-HasComments: No Gerrit-Has-Labels: No Gerrit-MessageType: comment
Change in libosmocore[master]: osmo_sock_init2: improve support for AF_UNSPEC
fixeria has posted comments on this change. ( https://gerrit.osmocom.org/c/libosmocore/+/19140 ) Change subject: osmo_sock_init2: improve support for AF_UNSPEC .. Patch Set 8: The unit test should probably be disabled for systems without IPv6 support. -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/19140 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: I397c633931fd00d4f083955a3c49a40fb002d766 Gerrit-Change-Number: 19140 Gerrit-PatchSet: 8 Gerrit-Owner: lynxis lazus Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: fixeria Gerrit-Reviewer: laforge Gerrit-Reviewer: lynxis lazus Gerrit-Reviewer: pespin Gerrit-Comment-Date: Mon, 03 Aug 2020 06:58:52 + Gerrit-HasComments: No Gerrit-Has-Labels: No Gerrit-MessageType: comment
Change in libosmocore[master]: osmo_sock_init2: improve support for AF_UNSPEC
fixeria has posted comments on this change. ( https://gerrit.osmocom.org/c/libosmocore/+/19140 ) Change subject: osmo_sock_init2: improve support for AF_UNSPEC .. Patch Set 8: Code-Review+1 -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/19140 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: I397c633931fd00d4f083955a3c49a40fb002d766 Gerrit-Change-Number: 19140 Gerrit-PatchSet: 8 Gerrit-Owner: lynxis lazus Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: fixeria Gerrit-Reviewer: laforge Gerrit-Reviewer: lynxis lazus Gerrit-Reviewer: pespin Gerrit-Comment-Date: Mon, 03 Aug 2020 06:55:38 + Gerrit-HasComments: No Gerrit-Has-Labels: Yes Gerrit-MessageType: comment
Change in libosmocore[master]: osmo_sock_init2: improve support for AF_UNSPEC
lynxis lazus has posted comments on this change. ( https://gerrit.osmocom.org/c/libosmocore/+/19140 ) Change subject: osmo_sock_init2: improve support for AF_UNSPEC .. Patch Set 8: (1 comment) https://gerrit.osmocom.org/c/libosmocore/+/19140/1/src/socket.c File src/socket.c: https://gerrit.osmocom.org/c/libosmocore/+/19140/1/src/socket.c@327 PS1, Line 327: return -ENODEV; > Missing freeaddrinfo. It may be helpful adding a goto and have the > freeaddinfo + return probably. Done -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/19140 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: I397c633931fd00d4f083955a3c49a40fb002d766 Gerrit-Change-Number: 19140 Gerrit-PatchSet: 8 Gerrit-Owner: lynxis lazus Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: laforge Gerrit-Reviewer: lynxis lazus Gerrit-Reviewer: pespin Gerrit-Comment-Date: Sat, 01 Aug 2020 20:09:15 + Gerrit-HasComments: Yes Gerrit-Has-Labels: No Comment-In-Reply-To: pespin Gerrit-MessageType: comment
Change in libosmocore[master]: osmo_sock_init2: improve support for AF_UNSPEC
lynxis lazus has posted comments on this change. ( https://gerrit.osmocom.org/c/libosmocore/+/19140 ) Change subject: osmo_sock_init2: improve support for AF_UNSPEC .. Patch Set 8: (1 comment) https://gerrit.osmocom.org/c/libosmocore/+/19140/7/src/socket.c File src/socket.c: https://gerrit.osmocom.org/c/libosmocore/+/19140/7/src/socket.c@386 PS7, Line 386: /* When called with AF_UNSPEC, family will set to IPv4 or IPv6 */ > The family is set to AF_INET or AF_INET6 when called with AF_UNSPEC. […] Done -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/19140 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: I397c633931fd00d4f083955a3c49a40fb002d766 Gerrit-Change-Number: 19140 Gerrit-PatchSet: 8 Gerrit-Owner: lynxis lazus Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: laforge Gerrit-Reviewer: lynxis lazus Gerrit-Reviewer: pespin Gerrit-Comment-Date: Sat, 01 Aug 2020 20:08:21 + Gerrit-HasComments: Yes Gerrit-Has-Labels: No Comment-In-Reply-To: lynxis lazus Comment-In-Reply-To: pespin Gerrit-MessageType: comment
Change in libosmocore[master]: osmo_sock_init2: improve support for AF_UNSPEC
lynxis lazus has posted comments on this change. ( https://gerrit.osmocom.org/c/libosmocore/+/19140 ) Change subject: osmo_sock_init2: improve support for AF_UNSPEC .. Patch Set 7: (1 comment) https://gerrit.osmocom.org/c/libosmocore/+/19140/7/src/socket.c File src/socket.c: https://gerrit.osmocom.org/c/libosmocore/+/19140/7/src/socket.c@386 PS7, Line 386: /* When called with AF_UNSPEC, family will set to IPv4 or IPv6 */ > I think I don't get this comment + if clause. […] The family is set to AF_INET or AF_INET6 when called with AF_UNSPEC. See line 275 -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/19140 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: I397c633931fd00d4f083955a3c49a40fb002d766 Gerrit-Change-Number: 19140 Gerrit-PatchSet: 7 Gerrit-Owner: lynxis lazus Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: laforge Gerrit-Reviewer: lynxis lazus Gerrit-Reviewer: pespin Gerrit-Comment-Date: Sat, 01 Aug 2020 18:23:59 + Gerrit-HasComments: Yes Gerrit-Has-Labels: No Comment-In-Reply-To: pespin Gerrit-MessageType: comment
Change in libosmocore[master]: osmo_sock_init2: improve support for AF_UNSPEC
pespin has posted comments on this change. ( https://gerrit.osmocom.org/c/libosmocore/+/19140 ) Change subject: osmo_sock_init2: improve support for AF_UNSPEC .. Patch Set 7: (1 comment) If I'm wrong please if possible improve that comment. https://gerrit.osmocom.org/c/libosmocore/+/19140/7/src/socket.c File src/socket.c: https://gerrit.osmocom.org/c/libosmocore/+/19140/7/src/socket.c@386 PS7, Line 386: /* When called with AF_UNSPEC, family will set to IPv4 or IPv6 */ I think I don't get this comment + if clause. IIUC ig family=AF_UNSPEC is passed, no result in rp will match AF_UNSPEC; because all will be either AF_INET or AF_INET6. -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/19140 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: I397c633931fd00d4f083955a3c49a40fb002d766 Gerrit-Change-Number: 19140 Gerrit-PatchSet: 7 Gerrit-Owner: lynxis lazus Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: laforge Gerrit-Reviewer: lynxis lazus Gerrit-Reviewer: pespin Gerrit-Comment-Date: Wed, 29 Jul 2020 11:42:10 + Gerrit-HasComments: Yes Gerrit-Has-Labels: No Gerrit-MessageType: comment
Change in libosmocore[master]: osmo_sock_init2: improve support for AF_UNSPEC
laforge has posted comments on this change. ( https://gerrit.osmocom.org/c/libosmocore/+/19140 ) Change subject: osmo_sock_init2: improve support for AF_UNSPEC .. Patch Set 7: Code-Review+1 -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/19140 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: I397c633931fd00d4f083955a3c49a40fb002d766 Gerrit-Change-Number: 19140 Gerrit-PatchSet: 7 Gerrit-Owner: lynxis lazus Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: laforge Gerrit-Reviewer: lynxis lazus Gerrit-Reviewer: pespin Gerrit-Comment-Date: Tue, 28 Jul 2020 20:07:11 + Gerrit-HasComments: No Gerrit-Has-Labels: Yes Gerrit-MessageType: comment
Change in libosmocore[master]: osmo_sock_init2: improve support for AF_UNSPEC
Hello pespin, Jenkins Builder, I'd like you to reexamine a change. Please visit https://gerrit.osmocom.org/c/libosmocore/+/19140 to look at the new patch set (#7). Change subject: osmo_sock_init2: improve support for AF_UNSPEC .. osmo_sock_init2: improve support for AF_UNSPEC osmo_sock_init2 abstract two calls of getaddrinfo into one. While there aren't problems with AF_INET or AF_INET6. When using AF_UNSPEC there are corner cases when this fails. E.g. calling local_host with "" and remote_host with an IPv6 only address results in setting up a local socket with AF_INET while trying to connect from there towards AF_INET6 will most likely fail. To prevent such cases with AF_UNSPEC, search prio calling any syscalls if local and remote site supports AF_INET or AF_INET6. In case both supported, prefer AF_INET6 Change-Id: I397c633931fd00d4f083955a3c49a40fb002d766 --- M src/socket.c M tests/socket/socket_test.c M tests/socket/socket_test.err M tests/socket/socket_test.ok 4 files changed, 105 insertions(+), 14 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/40/19140/7 -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/19140 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: I397c633931fd00d4f083955a3c49a40fb002d766 Gerrit-Change-Number: 19140 Gerrit-PatchSet: 7 Gerrit-Owner: lynxis lazus Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: lynxis lazus Gerrit-Reviewer: pespin Gerrit-CC: laforge Gerrit-MessageType: newpatchset
Change in libosmocore[master]: osmo_sock_init2: improve support for AF_UNSPEC
Hello pespin, Jenkins Builder, I'd like you to reexamine a change. Please visit https://gerrit.osmocom.org/c/libosmocore/+/19140 to look at the new patch set (#3). Change subject: osmo_sock_init2: improve support for AF_UNSPEC .. osmo_sock_init2: improve support for AF_UNSPEC osmo_sock_init2 abstract two calls of getaddrinfo into one. While there aren't problems with AF_INET or AF_INET6. When using AF_UNSPEC there are corner cases when this fails. E.g. calling local_host with "" and remote_host with an IPv6 only address results in setting up a local socket with AF_INET while trying to connect from there towards AF_INET6 will most likely fail. To prevent such cases with AF_UNSPEC, search prio calling any syscalls if local and remote site supports AF_INET or AF_INET6. In case both supported, prefer AF_INET6 Change-Id: I397c633931fd00d4f083955a3c49a40fb002d766 --- M src/socket.c M tests/socket/socket_test.c M tests/socket/socket_test.err M tests/socket/socket_test.ok 4 files changed, 101 insertions(+), 14 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/40/19140/3 -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/19140 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: I397c633931fd00d4f083955a3c49a40fb002d766 Gerrit-Change-Number: 19140 Gerrit-PatchSet: 3 Gerrit-Owner: lynxis lazus Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: lynxis lazus Gerrit-Reviewer: pespin Gerrit-CC: laforge Gerrit-MessageType: newpatchset
Change in libosmocore[master]: osmo_sock_init2: improve support for AF_UNSPEC
laforge has posted comments on this change. ( https://gerrit.osmocom.org/c/libosmocore/+/19140 ) Change subject: osmo_sock_init2: improve support for AF_UNSPEC .. Patch Set 1: I'm a bit puzzled by the build failure. For sure all our build slave should have IPv6 configured. The specific one that failed (host2-debian8) clearly has IPv6 enabled and ::1 on loopback: root@deb8build-ansible:~# ip a 1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/19140 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: I397c633931fd00d4f083955a3c49a40fb002d766 Gerrit-Change-Number: 19140 Gerrit-PatchSet: 1 Gerrit-Owner: lynxis lazus Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: lynxis lazus Gerrit-Reviewer: pespin Gerrit-CC: laforge Gerrit-Comment-Date: Tue, 07 Jul 2020 07:29:03 + Gerrit-HasComments: No Gerrit-Has-Labels: No Gerrit-MessageType: comment
Change in libosmocore[master]: osmo_sock_init2: improve support for AF_UNSPEC
pespin has posted comments on this change. ( https://gerrit.osmocom.org/c/libosmocore/+/19140 ) Change subject: osmo_sock_init2: improve support for AF_UNSPEC .. Patch Set 1: (1 comment) https://gerrit.osmocom.org/c/libosmocore/+/19140/1/src/socket.c File src/socket.c: https://gerrit.osmocom.org/c/libosmocore/+/19140/1/src/socket.c@327 PS1, Line 327: return -ENODEV; Missing freeaddrinfo. It may be helpful adding a goto and have the freeaddinfo + return probably. -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/19140 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: I397c633931fd00d4f083955a3c49a40fb002d766 Gerrit-Change-Number: 19140 Gerrit-PatchSet: 1 Gerrit-Owner: lynxis lazus Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: lynxis lazus Gerrit-CC: pespin Gerrit-Comment-Date: Mon, 06 Jul 2020 08:44:34 + Gerrit-HasComments: Yes Gerrit-Has-Labels: No Gerrit-MessageType: comment
Change in libosmocore[master]: osmo_sock_init2: improve support for AF_UNSPEC
pespin has posted comments on this change. ( https://gerrit.osmocom.org/c/libosmocore/+/19140 ) Change subject: osmo_sock_init2: improve support for AF_UNSPEC .. Patch Set 1: Code-Review-1 -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/19140 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: I397c633931fd00d4f083955a3c49a40fb002d766 Gerrit-Change-Number: 19140 Gerrit-PatchSet: 1 Gerrit-Owner: lynxis lazus Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: lynxis lazus Gerrit-Reviewer: pespin Gerrit-Comment-Date: Mon, 06 Jul 2020 08:44:37 + Gerrit-HasComments: No Gerrit-Has-Labels: Yes Gerrit-MessageType: comment
Change in libosmocore[master]: osmo_sock_init2: improve support for AF_UNSPEC
lynxis lazus has posted comments on this change. ( https://gerrit.osmocom.org/c/libosmocore/+/19140 ) Change subject: osmo_sock_init2: improve support for AF_UNSPEC .. Patch Set 1: jenkins fails because it doesn't have a IPv6 address (not even ::1) -- To view, visit https://gerrit.osmocom.org/c/libosmocore/+/19140 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Change-Id: I397c633931fd00d4f083955a3c49a40fb002d766 Gerrit-Change-Number: 19140 Gerrit-PatchSet: 1 Gerrit-Owner: lynxis lazus Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: lynxis lazus Gerrit-Comment-Date: Sun, 05 Jul 2020 10:48:51 + Gerrit-HasComments: No Gerrit-Has-Labels: No Gerrit-MessageType: comment
Change in libosmocore[master]: osmo_sock_init2: improve support for AF_UNSPEC
lynxis lazus has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/19140 ) Change subject: osmo_sock_init2: improve support for AF_UNSPEC .. osmo_sock_init2: improve support for AF_UNSPEC osmo_sock_init2 abstract two calls of getaddrinfo into one. While there aren't problems with AF_INET or AF_INET6. When using AF_UNSPEC there are corner cases when this fails. E.g. calling local_host with "" and remote_host with an IPv6 only address results in setting up a local socket with AF_INET while trying to connect from there towards AF_INET6 will most likely fail. To prevent such cases with AF_UNSPEC, search prio calling any syscalls if local and remote site supports AF_INET or AF_INET6. In case both supported, prefer AF_INET6 Change-Id: I397c633931fd00d4f083955a3c49a40fb002d766 --- M src/socket.c M tests/socket/socket_test.c M tests/socket/socket_test.ok 3 files changed, 99 insertions(+), 14 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/40/19140/1 diff --git a/src/socket.c b/src/socket.c index 503ceaf..359e24d 100644 --- a/src/socket.c +++ b/src/socket.c @@ -260,22 +260,81 @@ const char *local_host, uint16_t local_port, const char *remote_host, uint16_t remote_port, unsigned int flags) { - struct addrinfo *result, *rp; + struct addrinfo *local = NULL, *remote = NULL, *rp; int sfd = -1, rc, on = 1; + bool local_ipv4 = false, local_ipv6 = false; + bool remote_ipv4 = false, remote_ipv6 = false; + if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) == 0) { LOGP(DLGLOBAL, LOGL_ERROR, "invalid: you have to specify either " "BIND or CONNECT flags\n"); return -EINVAL; } + /* figure out local address infos */ + if (flags & OSMO_SOCK_F_BIND) { + local = addrinfo_helper(family, type, proto, local_host, local_port, true); + if (!local) + return -EINVAL; + } + + /* figure out remote address infos */ + if (flags & OSMO_SOCK_F_CONNECT) { + remote = addrinfo_helper(family, type, proto, remote_host, remote_port, false); + if (!remote) { + if (local) + freeaddrinfo(local); + + return -EINVAL; + } + } + + /* It must do a full run to ensure AF_UNSPEC does not fail. +* In case first local valid entry is IPv4 and only remote valid entry +* is IPv6 or vice versa */ + if (family == AF_UNSPEC) { + for (rp = local; rp != NULL; rp = rp->ai_next) { + switch (rp->ai_family) { + case AF_INET: + local_ipv4 = true; + break; + case AF_INET6: + local_ipv6 = true; + break; + } + } + + for (rp = remote; rp != NULL; rp = rp->ai_next) { + switch (rp->ai_family) { + case AF_INET: + remote_ipv4 = true; + break; + case AF_INET6: + remote_ipv6 = true; + break; + } + } + + /* priotize ipv6 as per RFC */ + if (local_ipv6 && remote_ipv6) + family = AF_INET6; + else if (local_ipv4 && remote_ipv4) + family = AF_INET; + else { + 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; + } + } + /* figure out local side of socket */ if (flags & OSMO_SOCK_F_BIND) { - result = addrinfo_helper(family, type, proto, local_host, local_port, true); - if (!result) - return -EINVAL; + for (rp = local; rp != NULL; rp = rp->ai_next) { + /* When called with AF_UNSPEC, family will set to IPv4 or IPv6 */ + if (rp->ai_family != family) + continue; - for (rp = result; rp != NULL; rp = rp->ai_next) { sfd = socket_helper(rp, flags); if (sfd < 0) continue; @@ -302,8 +361,11 @@ } break; } -