diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2006-11-22 23:22:06 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2006-11-22 23:22:06 +0000 |
commit | 48237b0c88343154d58854020c3a9c8b07c61b10 (patch) | |
tree | b36bc84f22dd797b45c8d665e50e2f6c690e1370 | |
parent | b40bdb383a6b7a7f0fd36d0b1cc24deb42cd5f0d (diff) |
introduce setsockopt_reuseaddr(int fd), setsockopt_broadcast(int fd),
use them where appropriate. 200 bytes saved
-rw-r--r-- | include/libbb.h | 2 | ||||
-rw-r--r-- | libbb/xconnect.c | 13 | ||||
-rw-r--r-- | networking/arping.c | 2 | ||||
-rw-r--r-- | networking/dnsd.c | 5 | ||||
-rw-r--r-- | networking/ether-wake.c | 3 | ||||
-rw-r--r-- | networking/fakeidentd.c | 9 | ||||
-rw-r--r-- | networking/httpd.c | 9 | ||||
-rw-r--r-- | networking/inetd.c | 6 | ||||
-rw-r--r-- | networking/nc.c | 3 | ||||
-rw-r--r-- | networking/ping.c | 4 | ||||
-rw-r--r-- | networking/ping6.c | 4 | ||||
-rw-r--r-- | networking/telnet.c | 2 | ||||
-rw-r--r-- | networking/traceroute.c | 3 | ||||
-rw-r--r-- | networking/udhcp/arpping.c | 7 | ||||
-rw-r--r-- | networking/udhcp/packet.c | 3 | ||||
-rw-r--r-- | networking/udhcp/socket.c | 7 |
16 files changed, 40 insertions, 42 deletions
diff --git a/include/libbb.h b/include/libbb.h index c30c5a73d..c96ef6f94 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -244,6 +244,8 @@ typedef union { } sockaddr_inet; extern int dotted2sockaddr(const char *dotted, struct sockaddr* sp, int socklen); extern int create_and_bind_socket_ip4or6(const char *hostaddr, int port); +extern int setsockopt_reuseaddr(int fd); +extern int setsockopt_broadcast(int fd); extern char *xstrdup(const char *s); diff --git a/libbb/xconnect.c b/libbb/xconnect.c index 9f95e38f8..b85648007 100644 --- a/libbb/xconnect.c +++ b/libbb/xconnect.c @@ -67,6 +67,16 @@ int xconnect_tcp_v4(struct sockaddr_in *s_addr) return s; } +static const int one = 1; +int setsockopt_reuseaddr(int fd) +{ + return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); +} +int setsockopt_broadcast(int fd) +{ + return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one)); +} + int dotted2sockaddr(const char *dotted, struct sockaddr* sp, int socklen) { union { @@ -116,7 +126,6 @@ int xsocket_stream_ip4or6(sa_family_t *fp) int create_and_bind_socket_ip4or6(const char *hostaddr, int port) { - static const int on = 1; int fd; sockaddr_inet sa; @@ -128,7 +137,7 @@ int create_and_bind_socket_ip4or6(const char *hostaddr, int port) fd = xsocket(sa.sa.sa_family, SOCK_STREAM, 0); } else fd = xsocket_stream_ip4or6(&sa.sa.sa_family); - setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); + setsockopt_reuseaddr(fd); /* if (port >= 0) { */ #if ENABLE_FEATURE_IPV6 diff --git a/networking/arping.c b/networking/arping.c index e4c9b86a9..2d92bf4be 100644 --- a/networking/arping.c +++ b/networking/arping.c @@ -354,7 +354,7 @@ int arping_main(int argc, char **argv) bb_error_msg_and_die("bind"); } } else if (!(cfg&dad)) { - int on = 1; + static const int on = 1; socklen_t alen = sizeof(saddr); saddr.sin_port = htons(1025); diff --git a/networking/dnsd.c b/networking/dnsd.c index 6f9dc5d67..5e9cf52f1 100644 --- a/networking/dnsd.c +++ b/networking/dnsd.c @@ -200,12 +200,9 @@ static int listen_socket(char *iface_addr, int listen_port) struct sockaddr_in a; char msg[100]; int s; - int yes = 1; s = xsocket(PF_INET, SOCK_DGRAM, 0); -#ifdef SO_REUSEADDR - if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes)) < 0) + if (setsockopt_reuseaddr(s) < 0) bb_perror_msg_and_die("setsockopt() failed"); -#endif memset(&a, 0, sizeof(a)); a.sin_port = htons(listen_port); a.sin_family = AF_INET; diff --git a/networking/ether-wake.c b/networking/ether-wake.c index cc2fb62a3..f870f6a5f 100644 --- a/networking/ether-wake.c +++ b/networking/ether-wake.c @@ -175,8 +175,7 @@ int ether_wake_main(int argc, char *argv[]) /* This is necessary for broadcasts to work */ if (flags /*& 1 [OPT_BROADCAST]*/) { - int one = 1; - if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, (void *)&one, sizeof(one)) < 0) + if (setsockopt_broadcast(s) < 0) bb_perror_msg("SO_BROADCAST"); } diff --git a/networking/fakeidentd.c b/networking/fakeidentd.c index 0e543e772..04138cca3 100644 --- a/networking/fakeidentd.c +++ b/networking/fakeidentd.c @@ -77,17 +77,16 @@ static void inetbind(void) int s, port; struct sockaddr_in addr; int len = sizeof(addr); - int one = 1; struct servent *se; - if ((se = getservbyname("identd", "tcp")) == NULL) - port = IDENT_PORT; - else + se = getservbyname("identd", "tcp"); + port = IDENT_PORT; + if (se) port = se->s_port; s = xsocket(AF_INET, SOCK_STREAM, 0); - setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)); + setsockopt_reuseaddr(s); memset(&addr, 0, sizeof(addr)); addr.sin_addr.s_addr = inet_addr(bind_ip_address); diff --git a/networking/httpd.c b/networking/httpd.c index b82e9f995..97b04fb03 100644 --- a/networking/httpd.c +++ b/networking/httpd.c @@ -802,7 +802,6 @@ static int openServer(void) { struct sockaddr_in lsocket; int fd; - int on = 1; /* create the socket right now */ /* inet_addr() returns a value that is already in network order */ @@ -814,9 +813,13 @@ static int openServer(void) /* tell the OS it's OK to reuse a previous address even though */ /* it may still be in a close down state. Allows bind to succeed. */ #ifdef SO_REUSEPORT - setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *)&on, sizeof(on)); + { + static const int on = 1; + setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, + (void *)&on, sizeof(on)); + } #else - setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)); + setsockopt_reuseaddr(fd); #endif xbind(fd, (struct sockaddr *)&lsocket, sizeof(lsocket)); xlisten(fd, 9); diff --git a/networking/inetd.c b/networking/inetd.c index 75b2b14a7..ec7b2e8f7 100644 --- a/networking/inetd.c +++ b/networking/inetd.c @@ -419,7 +419,6 @@ static int bump_nofile (void) static void setup(servtab_t *sep) { - int on = 1; int r; sep->se_fd = socket(sep->se_family, sep->se_socktype, 0); @@ -427,11 +426,8 @@ static void setup(servtab_t *sep) bb_perror_msg("%s/%s: socket", sep->se_service, sep->se_proto); return; } -#define turnon(fd, opt) \ -setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof(on)) - if (turnon(sep->se_fd, SO_REUSEADDR) < 0) + if (setsockopt_reuseaddr(sep->se_fd) < 0) bb_perror_msg("setsockopt(SO_REUSEADDR)"); -#undef turnon #ifdef CONFIG_FEATURE_INETD_RPC if (isrpcservice(sep)) { diff --git a/networking/nc.c b/networking/nc.c index 2f8a36bee..5fd9242cc 100644 --- a/networking/nc.c +++ b/networking/nc.c @@ -85,8 +85,7 @@ int nc_main(int argc, char **argv) if (!cfd) { sfd = xsocket(AF_INET, SOCK_STREAM, 0); fcntl(sfd, F_SETFD, FD_CLOEXEC); - opt = 1; - setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); + setsockopt_reuseaddr(sfd); address.sin_family = AF_INET; // Set local port. diff --git a/networking/ping.c b/networking/ping.c index 400d565d9..fc01b5e1d 100644 --- a/networking/ping.c +++ b/networking/ping.c @@ -351,9 +351,7 @@ static void ping(const char *host) memcpy(&pingaddr.sin_addr, hostent->h_addr, sizeof(pingaddr.sin_addr)); /* enable broadcast pings */ - sockopt = 1; - setsockopt(pingsock, SOL_SOCKET, SO_BROADCAST, (char *) &sockopt, - sizeof(sockopt)); + setsockopt_broadcast(pingsock); /* set recv buf for broadcast pings */ sockopt = 48 * 1024; diff --git a/networking/ping6.c b/networking/ping6.c index 6ab9ce0f1..9f0509e66 100644 --- a/networking/ping6.c +++ b/networking/ping6.c @@ -356,9 +356,7 @@ static void ping(const char *host) #endif /*ICMP6_FILTER*/ /* enable broadcast pings */ - sockopt = 1; - setsockopt(pingsock, SOL_SOCKET, SO_BROADCAST, (char *) &sockopt, - sizeof(sockopt)); + setsockopt_broadcast(pingsock); /* set recv buf for broadcast pings */ sockopt = 48 * 1024; diff --git a/networking/telnet.c b/networking/telnet.c index 628e2e6e3..6085d885a 100644 --- a/networking/telnet.c +++ b/networking/telnet.c @@ -103,7 +103,7 @@ static void telopt(byte c); static int subneg(byte c); /* Some globals */ -static int one = 1; +static const int one = 1; #ifdef CONFIG_FEATURE_TELNET_TTYPE static char *ttype; diff --git a/networking/traceroute.c b/networking/traceroute.c index c4f050abb..490076543 100644 --- a/networking/traceroute.c +++ b/networking/traceroute.c @@ -888,13 +888,14 @@ getaddr(u_int32_t *ap, const char *host) int traceroute_main(int argc, char *argv[]) { + static const int on = 1; + int code, n; unsigned char *outp; u_int32_t *ap; struct sockaddr_in *from = (struct sockaddr_in *)&wherefrom; struct sockaddr_in *to = (struct sockaddr_in *)&whereto; struct hostinfo *hi; - int on = 1; int ttl, probe, i; int seq = 0; int tos = 0; diff --git a/networking/udhcp/arpping.c b/networking/udhcp/arpping.c index 471e36d62..9c8b9c562 100644 --- a/networking/udhcp/arpping.c +++ b/networking/udhcp/arpping.c @@ -44,9 +44,7 @@ struct arpMsg { /* FIXME: match response against chaddr */ int arpping(uint32_t yiaddr, uint32_t ip, uint8_t *mac, char *interface) { - int timeout = 2; - int optval = 1; int s; /* socket */ int rv = 1; /* return value */ struct sockaddr addr; /* for interface name */ @@ -56,12 +54,13 @@ int arpping(uint32_t yiaddr, uint32_t ip, uint8_t *mac, char *interface) time_t prevTime; - if ((s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP))) == -1) { + s = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ARP)); + if (s == -1) { bb_perror_msg(bb_msg_can_not_create_raw_socket); return -1; } - if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval)) == -1) { + if (setsockopt_broadcast(s) == -1) { bb_perror_msg("cannot setsocketopt on raw socket"); close(s); return -1; diff --git a/networking/udhcp/packet.c b/networking/udhcp/packet.c index 85910447b..dec9d0ab3 100644 --- a/networking/udhcp/packet.c +++ b/networking/udhcp/packet.c @@ -173,7 +173,6 @@ int udhcp_kernel_packet(struct dhcpMessage *payload, uint32_t source_ip, int source_port, uint32_t dest_ip, int dest_port) { - int n = 1; int fd, result; struct sockaddr_in client; @@ -181,7 +180,7 @@ int udhcp_kernel_packet(struct dhcpMessage *payload, if (fd < 0) return -1; - if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &n, sizeof(n)) == -1) { + if (setsockopt_reuseaddr(fd) == -1) { close(fd); return -1; } diff --git a/networking/udhcp/socket.c b/networking/udhcp/socket.c index c19131d65..b27dca300 100644 --- a/networking/udhcp/socket.c +++ b/networking/udhcp/socket.c @@ -93,7 +93,6 @@ int listen_socket(uint32_t ip, int port, char *inf) struct ifreq interface; int fd; struct sockaddr_in addr; - int n = 1; DEBUG("Opening listen socket on 0x%08x:%d %s", ip, port, inf); fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); @@ -107,17 +106,17 @@ int listen_socket(uint32_t ip, int port, char *inf) addr.sin_port = htons(port); addr.sin_addr.s_addr = ip; - if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &n, sizeof(n)) == -1) { + if (setsockopt_reuseaddr(fd) == -1) { close(fd); return -1; } - if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char *) &n, sizeof(n)) == -1) { + if (setsockopt_broadcast(fd) == -1) { close(fd); return -1; } strncpy(interface.ifr_name, inf, IFNAMSIZ); - if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,(char *)&interface, sizeof(interface)) < 0) { + if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, (char *)&interface, sizeof(interface)) < 0) { close(fd); return -1; } |