From a57721d14642291696be4ffd5d8bb0915fbe861b Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Mon, 8 Jul 2019 16:02:56 -0700 Subject: bind/connect cleanup. Rename the existing xbind/xconnect to xbindany/xconnectany, to make room for new xbind/xconnect that are more like 'x' versions of the regular bind and connect. Move explicit bind/connect callers over to xbind/xconnect. Of the affected commands, only netcat is actually used by Android. It was the most recent patch to netcat that made the lack of a more traditional xbind/xconnect apparent. --- toys/net/ftpget.c | 4 ++-- toys/net/netcat.c | 19 +++++-------------- toys/net/ping.c | 2 +- toys/net/sntp.c | 2 +- 4 files changed, 9 insertions(+), 18 deletions(-) (limited to 'toys/net') diff --git a/toys/net/ftpget.c b/toys/net/ftpget.c index ad3c3030..05c53509 100644 --- a/toys/net/ftpget.c +++ b/toys/net/ftpget.c @@ -105,7 +105,7 @@ void ftpget_main(void) if (!remote) remote = toys.optargs[1]; // connect - TT.fd = xconnect(xgetaddrinfo(*toys.optargs, TT.p, 0, SOCK_STREAM, 0, + TT.fd = xconnectany(xgetaddrinfo(*toys.optargs, TT.p, 0, SOCK_STREAM, 0, AI_ADDRCONFIG)); if (getpeername(TT.fd, (void *)&si6, &sl)) perror_exit("getpeername"); @@ -147,7 +147,7 @@ void ftpget_main(void) if (!s || port<1 || port>65535) error_exit_raw(toybuf); si6.sin6_port = SWAP_BE16(port); // same field size/offset for v4 and v6 port = xsocket(si6.sin6_family, SOCK_STREAM, 0); - if (connect(port, (void *)&si6, sizeof(si6))) perror_exit("connect"); + xconnect(port, (void *)&si6, sizeof(si6)); // RETR blocks until file data read from data port, so use SIZE to check // if file exists before creating local copy diff --git a/toys/net/netcat.c b/toys/net/netcat.c index 65c41ace..0a235d1e 100644 --- a/toys/net/netcat.c +++ b/toys/net/netcat.c @@ -111,16 +111,10 @@ void netcat_main(void) sockaddr.sun_family = AF_UNIX; sockfd = xsocket(AF_UNIX, type | SOCK_CLOEXEC, 0); - if (connect(sockfd, (struct sockaddr*)&sockaddr, - sizeof(sockaddr)) != 0) { - perror_exit("could not bind to unix domain socket"); - } - + xconnect(sockfd, (struct sockaddr*)&sockaddr, sizeof(sockaddr)); } else { - struct addrinfo *addr = xgetaddrinfo(toys.optargs[0], toys.optargs[1], - family, type, 0, 0); - - sockfd = xconnect(addr); + sockfd = xconnectany(xgetaddrinfo(toys.optargs[0], toys.optargs[1], + family, type, 0, 0)); } // We have a connection. Disarm timeout. @@ -145,13 +139,10 @@ void netcat_main(void) sockaddr.sun_family = AF_UNIX; sockfd = xsocket(AF_UNIX, type | SOCK_CLOEXEC, 0); - if (bind(sockfd, (struct sockaddr*)&sockaddr, - sizeof(struct sockaddr_un)) != 0) { - perror_exit("unable to bind to UNIX domain socket"); - } + xbind(sockfd, (struct sockaddr*)&sockaddr, sizeof(sockaddr)); } else { sprintf(toybuf, "%ld", TT.p); - sockfd = xbind(xgetaddrinfo(TT.s, toybuf, family, type, 0, 0)); + sockfd = xbindany(xgetaddrinfo(TT.s, toybuf, family, type, 0, 0)); } if (listen(sockfd, 5)) error_exit("listen"); diff --git a/toys/net/ping.c b/toys/net/ping.c index 81dca99f..9ae7c856 100644 --- a/toys/net/ping.c +++ b/toys/net/ping.c @@ -155,7 +155,7 @@ void ping_main(void) } xexit(); } - if (TT.I && bind(TT.sock, sa, sizeof(srcaddr))) perror_exit("bind"); + if (TT.I) xbind(TT.sock, sa, sizeof(srcaddr)); if (toys.optflags&FLAG_m) { int mark = TT.m; diff --git a/toys/net/sntp.c b/toys/net/sntp.c index b1ecb1be..b1f3685f 100644 --- a/toys/net/sntp.c +++ b/toys/net/sntp.c @@ -88,7 +88,7 @@ void sntp_main(void) // Act as server if necessary if (FLAG(S)|FLAG(m)) { - fd = xbind(ai); + fd = xbindany(ai); if (TT.m) { struct ip_mreq group; -- cgit v1.2.3