From: Pierre Habouzit Date: Sat, 1 Dec 2007 13:37:39 +0000 (+0100) Subject: Add new APIs X-Git-Url: http://git.madism.org/?a=commitdiff_plain;h=93ab2b3dc7658be50317765792a8720c54b29807;p=apps%2Fpfixtools.git Add new APIs Signed-off-by: Pierre Habouzit --- diff --git a/common.c b/common.c index 186ac43..3addd0e 100644 --- a/common.c +++ b/common.c @@ -88,7 +88,7 @@ static int setnonblock(int sock) return 0; } -int tcp_listen_nonblock(const struct sockaddr *addr, socklen_t len) +int tcp_bind(const struct sockaddr *addr, socklen_t len) { int sock; @@ -128,17 +128,32 @@ int tcp_listen_nonblock(const struct sockaddr *addr, socklen_t len) return -1; } - if (setnonblock(sock)) { + return sock; +} + +int tcp_listen(const struct sockaddr *addr, socklen_t len) +{ + int sock = tcp_bind(addr, len); + if (listen(sock, 0) < 0) { + UNIXERR("bind"); close(sock); return -1; } + return sock; +} +int tcp_listen_nonblock(const struct sockaddr *addr, socklen_t len) +{ + int sock = tcp_bind(addr, len); + if (setnonblock(sock)) { + close(sock); + return -1; + } if (listen(sock, 0) < 0) { UNIXERR("bind"); close(sock); return -1; } - return sock; } diff --git a/common.h b/common.h index f7dd978..fb70b75 100644 --- a/common.h +++ b/common.h @@ -74,6 +74,8 @@ extern sig_atomic_t sighup; void common_sighandler(int sig); +int tcp_bind(const struct sockaddr *addr, socklen_t len); +int tcp_listen(const struct sockaddr *addr, socklen_t len); int tcp_listen_nonblock(const struct sockaddr *addr, socklen_t len); int accept_nonblock(int fd); int xwrite(int fd, const char *s, size_t l);