summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/libbb.h2
-rw-r--r--libbb/Makefile.in2
-rw-r--r--libbb/bb_xbind.c18
-rw-r--r--libbb/bb_xlisten.c17
-rw-r--r--networking/dnsd.c5
-rw-r--r--networking/fakeidentd.c4
-rw-r--r--networking/httpd.c9
-rw-r--r--networking/nc.c7
-rw-r--r--networking/telnetd.c10
-rw-r--r--networking/traceroute.c4
10 files changed, 50 insertions, 28 deletions
diff --git a/include/libbb.h b/include/libbb.h
index 650002b..d70c71c 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -147,6 +147,8 @@ extern void bb_fflush_stdout_and_exit(int retval) ATTRIBUTE_NORETURN;
extern void xstat(const char *filename, struct stat *buf);
extern int bb_xsocket(int domain, int type, int protocol);
extern void bb_xdaemon(int nochdir, int noclose);
+extern void bb_xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen);
+extern void bb_xlisten(int s, int backlog);
#define BB_GETOPT_ERROR 0x80000000UL
extern const char *bb_opt_complementally;
diff --git a/libbb/Makefile.in b/libbb/Makefile.in
index 3fb945e..f05bf80 100644
--- a/libbb/Makefile.in
+++ b/libbb/Makefile.in
@@ -30,7 +30,7 @@ LIBBB-y:= \
trim.c u_signal_names.c vdprintf.c verror_msg.c \
vherror_msg.c vperror_msg.c wfopen.c xconnect.c xgetcwd.c xstat.c \
xgethostbyname.c xgethostbyname2.c xreadlink.c xregcomp.c xgetlarg.c \
- bb_xsocket.c bb_xdaemon.c \
+ bb_xsocket.c bb_xdaemon.c bb_xbind.c bb_xlisten.c \
get_terminal_width_height.c fclose_nonstdin.c fflush_stdout_and_exit.c \
getopt_ulflags.c default_error_retval.c wfopen_input.c speed_table.c \
perror_nomsg_and_die.c perror_nomsg.c skip_whitespace.c bb_askpass.c \
diff --git a/libbb/bb_xbind.c b/libbb/bb_xbind.c
new file mode 100644
index 0000000..8a45af3
--- /dev/null
+++ b/libbb/bb_xbind.c
@@ -0,0 +1,18 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * bb_xbind.c - a bind() which dies on failure with error message
+ *
+ * Copyright (C) 2006 Denis Vlasenko
+ *
+ * Licensed under LGPL, see file docs/lesser.txt in this tarball for details.
+ */
+#include <sys/types.h>
+#include <sys/socket.h>
+#include "libbb.h"
+
+void bb_xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen)
+{
+ if (bind(sockfd, my_addr, addrlen))
+ bb_perror_msg_and_die("bind");
+}
+
diff --git a/libbb/bb_xlisten.c b/libbb/bb_xlisten.c
new file mode 100644
index 0000000..a42d61a
--- /dev/null
+++ b/libbb/bb_xlisten.c
@@ -0,0 +1,17 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * bb_xlisten.c - a listen() which dies on failure with error message
+ *
+ * Copyright (C) 2006 Denis Vlasenko
+ *
+ * Licensed under LGPL, see file docs/lesser.txt in this tarball for details.
+ */
+#include <sys/socket.h>
+#include "libbb.h"
+
+void bb_xlisten(int s, int backlog)
+{
+ if (listen(s, backlog))
+ bb_perror_msg_and_die("listen");
+}
+
diff --git a/networking/dnsd.c b/networking/dnsd.c
index d78ea04..b9d0221 100644
--- a/networking/dnsd.c
+++ b/networking/dnsd.c
@@ -216,9 +216,8 @@ listen_socket(char *iface_addr, int listen_port)
a.sin_family = AF_INET;
if (!inet_aton(iface_addr, &a.sin_addr))
bb_perror_msg_and_die("bad iface address");
- if (bind(s, (struct sockaddr *)&a, sizeof(a)) < 0)
- bb_perror_msg_and_die("bind() failed");
- listen(s, 50);
+ bb_xbind(s, (struct sockaddr *)&a, sizeof(a));
+ listen(s, 50); /* bb_xlisten? */
sprintf(msg, "accepting UDP packets on addr:port %s:%d\n",
iface_addr, (int)listen_port);
log_message(LOG_FILE, msg);
diff --git a/networking/fakeidentd.c b/networking/fakeidentd.c
index 26efdcb..cae6b81 100644
--- a/networking/fakeidentd.c
+++ b/networking/fakeidentd.c
@@ -113,10 +113,10 @@ static void inetbind(void)
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
- if (bind(s, (struct sockaddr *)&addr, len) < 0)
+ if (bind(s, (struct sockaddr *)&addr, len) < 0) /* bb_xbind? */
bb_perror_msg_and_die("Cannot bind() port %i", IDENT_PORT);
- if (listen(s, 5) < 0)
+ if (listen(s, 5) < 0) /* bb_xlisten? */
bb_perror_msg_and_die("Cannot listen() on port %i", IDENT_PORT);
movefd(s, 0);
diff --git a/networking/httpd.c b/networking/httpd.c
index 354c199..0f61741 100644
--- a/networking/httpd.c
+++ b/networking/httpd.c
@@ -958,12 +958,9 @@ static int openServer(void)
#else
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on)) ;
#endif
- if (bind(fd, (struct sockaddr *)&lsocket, sizeof(lsocket)) == 0) {
- listen(fd, 9);
- signal(SIGCHLD, SIG_IGN); /* prevent zombie (defunct) processes */
- } else {
- bb_perror_msg_and_die("bind");
- }
+ bb_xbind(fd, (struct sockaddr *)&lsocket, sizeof(lsocket));
+ listen(fd, 9); /* bb_xlisten? */
+ signal(SIGCHLD, SIG_IGN); /* prevent zombie (defunct) processes */
return fd;
}
#endif /* CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY */
diff --git a/networking/nc.c b/networking/nc.c
index 86f0b99..57b0917 100644
--- a/networking/nc.c
+++ b/networking/nc.c
@@ -90,16 +90,13 @@ int nc_main(int argc, char **argv)
memset(&address.sin_addr, 0, sizeof(address.sin_addr));
address.sin_port = lport;
- if (bind(sfd, (struct sockaddr *) &address, sizeof(address)) < 0)
- bb_perror_msg_and_die("bind");
+ bb_xbind(sfd, (struct sockaddr *) &address, sizeof(address));
}
if (do_listen) {
socklen_t addrlen = sizeof(address);
- if (listen(sfd, 1) < 0)
- bb_perror_msg_and_die("listen");
-
+ bb_xlisten(sfd, 1);
if ((tmpfd = accept(sfd, (struct sockaddr *) &address, &addrlen)) < 0)
bb_perror_msg_and_die("accept");
diff --git a/networking/telnetd.c b/networking/telnetd.c
index d53f56b..1a53c0c 100644
--- a/networking/telnetd.c
+++ b/networking/telnetd.c
@@ -461,14 +461,8 @@ telnetd_main(int argc, char **argv)
sa.sin_addr = bind_addr;
#endif
- if (bind(master_fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
- bb_perror_msg_and_die("bind");
- }
-
- if (listen(master_fd, 1) < 0) {
- bb_perror_msg_and_die("listen");
- }
-
+ bb_xbind(master_fd, (struct sockaddr *) &sa, sizeof(sa));
+ bb_xlisten(master_fd, 1);
bb_xdaemon(0, 0);
maxfd = master_fd;
diff --git a/networking/traceroute.c b/networking/traceroute.c
index 0abd904..4f7ebf1 100644
--- a/networking/traceroute.c
+++ b/networking/traceroute.c
@@ -1253,9 +1253,7 @@ traceroute_main(int argc, char *argv[])
outip->ip_src = from->sin_addr;
#ifndef IP_HDRINCL
- if (bind(sndsock, (struct sockaddr *)from, sizeof(*from)) < 0) {
- bb_perror_msg_and_die("bind");
- }
+ bb_xbind(sndsock, (struct sockaddr *)from, sizeof(*from));
#endif
fprintf(stderr, "traceroute to %s (%s)", hostname, inet_ntoa(to->sin_addr));