introduce setsockopt_reuseaddr(int fd), setsockopt_broadcast(int fd),

use them where appropriate. 200 bytes saved
This commit is contained in:
Denis Vlasenko 2006-11-22 23:22:06 +00:00
parent b40bdb383a
commit 48237b0c88
16 changed files with 40 additions and 42 deletions

View File

@ -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);

View File

@ -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

View File

@ -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);

View File

@ -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;

View File

@ -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");
}

View File

@ -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);

View File

@ -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);

View File

@ -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)) {

View File

@ -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.

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;
}

View File

@ -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;
}