diff options
author | Elliott Hughes <enh@google.com> | 2019-07-08 16:02:56 -0700 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2019-07-11 22:02:12 -0500 |
commit | a57721d14642291696be4ffd5d8bb0915fbe861b (patch) | |
tree | 11a18894601df31ce1eb9c46c5d1faaa59e2f9e8 /toys/pending | |
parent | 34f095fcbf0829719f9a130a359ebf2a3e223ca0 (diff) | |
download | toybox-a57721d14642291696be4ffd5d8bb0915fbe861b.tar.gz |
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.
Diffstat (limited to 'toys/pending')
-rw-r--r-- | toys/pending/arping.c | 9 | ||||
-rw-r--r-- | toys/pending/dhcp6.c | 10 | ||||
-rw-r--r-- | toys/pending/host.c | 5 | ||||
-rw-r--r-- | toys/pending/tcpsvd.c | 5 | ||||
-rw-r--r-- | toys/pending/telnet.c | 2 | ||||
-rw-r--r-- | toys/pending/telnetd.c | 7 | ||||
-rw-r--r-- | toys/pending/tftpd.c | 5 | ||||
-rw-r--r-- | toys/pending/traceroute.c | 16 |
8 files changed, 19 insertions, 40 deletions
diff --git a/toys/pending/arping.c b/toys/pending/arping.c index 6007845b..68ee5eac 100644 --- a/toys/pending/arping.c +++ b/toys/pending/arping.c @@ -242,15 +242,13 @@ void arping_main(void) saddr.sin_family = AF_INET; if (src_addr.s_addr) { saddr.sin_addr = src_addr; - if (bind(p_fd, (struct sockaddr*)&saddr, sizeof(saddr))) - perror_exit("bind"); + xbind(p_fd, (struct sockaddr*)&saddr, sizeof(saddr)); } else { uint32_t oip; saddr.sin_port = htons(1025); saddr.sin_addr = dest_addr; - if (connect(p_fd, (struct sockaddr *) &saddr, sizeof(saddr))) - perror_exit("cannot connect to remote host"); + xconnect(p_fd, (struct sockaddr *) &saddr, sizeof(saddr)); get_interface(TT.iface, NULL, &oip, NULL); src_addr.s_addr = htonl(oip); } @@ -259,8 +257,7 @@ void arping_main(void) src_pk.sll_family = AF_PACKET; src_pk.sll_protocol = htons(ETH_P_ARP); - if (bind(TT.sockfd, (struct sockaddr *)&src_pk, sizeof(src_pk))) - perror_exit("bind"); + xbind(TT.sockfd, (struct sockaddr *)&src_pk, sizeof(src_pk)); socklen_t alen = sizeof(src_pk); getsockname(TT.sockfd, (struct sockaddr *)&src_pk, &alen); diff --git a/toys/pending/dhcp6.c b/toys/pending/dhcp6.c index c69c4aed..728dc7d1 100644 --- a/toys/pending/dhcp6.c +++ b/toys/pending/dhcp6.c @@ -249,10 +249,7 @@ static void mode_raw() sockll.sll_family = AF_PACKET; sockll.sll_protocol = htons(ETH_P_IPV6); sockll.sll_ifindex = if_nametoindex(TT.interface_name); - if (bind(TT.sock, (struct sockaddr *) &sockll, sizeof(sockll))) { - xclose(TT.sock); - error_exit("MODE RAW : Bind fail.\n"); - } + xbind(TT.sock, (struct sockaddr *) &sockll, sizeof(sockll)); if (setsockopt(TT.sock, SOL_PACKET, PACKET_HOST,&constone, sizeof(int)) < 0) { if (errno != ENOPROTOOPT) error_exit("MODE RAW : Bind fail.\n"); } @@ -575,10 +572,7 @@ void dhcp6_main(void) xsetsockopt(TT.sock1, SOL_SOCKET, SO_REUSEADDR, &constone, sizeof(constone)); - if (bind(TT.sock1, (struct sockaddr *)&sinaddr6, sizeof(sinaddr6))) { - xclose(TT.sock1); - error_exit("bind failed"); - } + xbind(TT.sock1, (struct sockaddr *)&sinaddr6, sizeof(sinaddr6)); mode_raw(); set_timeout(0); diff --git a/toys/pending/host.c b/toys/pending/host.c index fa830a7d..fe0f23aa 100644 --- a/toys/pending/host.c +++ b/toys/pending/host.c @@ -121,9 +121,8 @@ void host_main(void) if ((ret = getaddrinfo(nsname, "53", &ns_hints, &ai)) < 0) error_exit("Error looking up server name: %s", gai_strerror(ret)); - int s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); - if (s < 0 || connect(s, ai->ai_addr, ai->ai_addrlen) < 0) - perror_exit("Socket error"); + int s = xsocket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); + xconnect(s, ai->ai_addr, ai->ai_addrlen); setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &(struct timeval){ .tv_sec = 5 }, sizeof(struct timeval)); printf("Using domain server %s:\n", nsname); diff --git a/toys/pending/tcpsvd.c b/toys/pending/tcpsvd.c index 31c27610..e5bd76b5 100644 --- a/toys/pending/tcpsvd.c +++ b/toys/pending/tcpsvd.c @@ -248,7 +248,7 @@ static int create_bind_sock(char *host, struct sockaddr *haddr) sockfd = xsocket(rp->ai_family, TT.udp ?SOCK_DGRAM :SOCK_STREAM, 0); setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &set, sizeof(set)); if (TT.udp) setsockopt(sockfd, IPPROTO_IP, IP_PKTINFO, &set, sizeof(set)); - if ((bind(sockfd, rp->ai_addr, rp->ai_addrlen)) < 0) perror_exit("Bind failed"); + xbind(sockfd, rp->ai_addr, rp->ai_addrlen); if(haddr) memcpy(haddr, rp->ai_addr, rp->ai_addrlen); freeaddrinfo(res); return sockfd; @@ -386,8 +386,7 @@ void tcpsvd_main(void) free(serv); free(clie); } - if (TT.udp && (connect(newfd, (struct sockaddr *)buf, sizeof(buf)) < 0)) - perror_exit("connect"); + if (TT.udp) xconnect(newfd, (struct sockaddr *)buf, sizeof(buf)); close(0); close(1); diff --git a/toys/pending/telnet.c b/toys/pending/telnet.c index e37b982c..b8c9c140 100644 --- a/toys/pending/telnet.c +++ b/toys/pending/telnet.c @@ -306,7 +306,7 @@ void telnet_main(void) } terminal_size(&TT.win_width, &TT.win_height); - TT.sfd = xconnect(xgetaddrinfo(*toys.optargs, port, 0, SOCK_STREAM, + TT.sfd = xconnectany(xgetaddrinfo(*toys.optargs, port, 0, SOCK_STREAM, IPPROTO_TCP, 0)); setsockopt(TT.sfd, SOL_SOCKET, SO_REUSEADDR, &set, sizeof(set)); setsockopt(TT.sfd, SOL_SOCKET, SO_KEEPALIVE, &set, sizeof(set)); diff --git a/toys/pending/telnetd.c b/toys/pending/telnetd.c index 4198e63f..ad39d8c0 100644 --- a/toys/pending/telnetd.c +++ b/toys/pending/telnetd.c @@ -143,11 +143,8 @@ static int listen_socket(void) if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes)) == -1) perror_exit("setsockopt"); - if (bind(s, (struct sockaddr *)buf, ((af == AF_INET)? - (sizeof(struct sockaddr_in)):(sizeof(struct sockaddr_in6)))) == -1) { - close(s); - perror_exit("bind"); - } + xbind(s, (struct sockaddr *)buf, ((af == AF_INET)? + (sizeof(struct sockaddr_in)):(sizeof(struct sockaddr_in6)))); if (listen(s, 1) < 0) perror_exit("listen"); return s; diff --git a/toys/pending/tftpd.c b/toys/pending/tftpd.c index 9791ae41..b5d0558f 100644 --- a/toys/pending/tftpd.c +++ b/toys/pending/tftpd.c @@ -252,9 +252,8 @@ void tftpd_main(void) TT.sfd = xsocket(dstaddr.ss_family, SOCK_DGRAM, 0); if (setsockopt(TT.sfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&set, sizeof(set)) < 0) perror_exit("setsockopt failed"); - if (bind(TT.sfd, (void *)&srcaddr, socklen)) perror_exit("bind"); - if (connect(TT.sfd, (void *)&dstaddr, socklen) < 0) - perror_exit("can't connect to remote host"); + xbind(TT.sfd, (void *)&srcaddr, socklen); + xconnect(TT.sfd, (void *)&dstaddr, socklen); // Error condition. if (recvmsg_len<4 || recvmsg_len>TFTPD_BLKSIZE || toybuf[recvmsg_len-1]) { send_errpkt((struct sockaddr*)&dstaddr, socklen, "packet format error"); diff --git a/toys/pending/traceroute.c b/toys/pending/traceroute.c index d5ead9e2..1cfdc480 100644 --- a/toys/pending/traceroute.c +++ b/toys/pending/traceroute.c @@ -587,8 +587,7 @@ void traceroute_main(void) if (setsockopt(TT.snd_sock, IPPROTO_IP, IP_MULTICAST_IF, (struct sockaddr*)&source, sizeof(struct sockaddr_in))) perror_exit("can't set multicast source interface"); - if (bind(TT.snd_sock,(struct sockaddr*)&source, - sizeof(struct sockaddr_in)) < 0) perror_exit("bind"); + xbind(TT.snd_sock,(struct sockaddr*)&source, sizeof(struct sockaddr_in)); } if(TT.first_ttl > TT.max_ttl) @@ -607,9 +606,7 @@ void traceroute_main(void) if(inet_pton(AF_INET6, TT.src_ip, &(source.sin6_addr)) <= 0) error_exit("bad address: %s", TT.src_ip); - if (bind(TT.snd_sock,(struct sockaddr*)&source, - sizeof(struct sockaddr_in6)) < 0) - error_exit("bind: Cannot assign requested address"); + xbind(TT.snd_sock,(struct sockaddr*)&source, sizeof(struct sockaddr_in6)); } else { struct sockaddr_in6 prb; socklen_t len = sizeof(prb); @@ -617,16 +614,13 @@ void traceroute_main(void) if (toys.optflags & FLAG_i) bind_to_interface(p_fd); ((struct sockaddr_in6 *)&dest)->sin6_port = htons(1025); - if (connect(p_fd, (struct sockaddr *)&dest, sizeof(struct sockaddr_in6)) < 0) - perror_exit("can't connect to remote host"); + xconnect(p_fd, (struct sockaddr *)&dest, sizeof(struct sockaddr_in6)); if(getsockname(p_fd, (struct sockaddr *)&prb, &len)) error_exit("probe addr failed"); close(p_fd); prb.sin6_port = 0; - if (bind(TT.snd_sock, (struct sockaddr*)&prb, - sizeof(struct sockaddr_in6))) perror_exit("bind"); - if (bind(TT.recv_sock, (struct sockaddr*)&prb, - sizeof(struct sockaddr_in6))) perror_exit("bind"); + xbind(TT.snd_sock, (struct sockaddr*)&prb, sizeof(struct sockaddr_in6)); + xbind(TT.recv_sock, (struct sockaddr*)&prb, sizeof(struct sockaddr_in6)); } inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&dest)->sin6_addr, |