} 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);
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 {
int create_and_bind_socket_ip4or6(const char *hostaddr, int port)
{
- static const int on = 1;
int fd;
sockaddr_inet sa;
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
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);
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;
/* 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");
}
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);
{
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 */
/* 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);
static void setup(servtab_t *sep)
{
- int on = 1;
int r;
sep->se_fd = socket(sep->se_family, sep->se_socktype, 0);
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)) {
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.
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;
#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;
static int subneg(byte c);
/* Some globals */
-static int one = 1;
+static const int one = 1;
#ifdef CONFIG_FEATURE_TELNET_TTYPE
static char *ttype;
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;
/* 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 */
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;
uint32_t source_ip, int source_port,
uint32_t dest_ip, int dest_port)
{
- int n = 1;
int fd, result;
struct sockaddr_in client;
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;
}
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);
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;
}