2009-03-22 05:30:00 -04:00
|
|
|
/* anet.c -- Basic TCP socket stuff made a bit less boring
|
|
|
|
*
|
2012-11-08 12:25:23 -05:00
|
|
|
* Copyright (c) 2006-2012, Salvatore Sanfilippo <antirez at gmail dot com>
|
2009-03-22 05:30:00 -04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of Redis nor the names of its contributors may be used
|
|
|
|
* to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2009-03-27 17:00:27 -04:00
|
|
|
#include "fmacros.h"
|
|
|
|
|
2009-03-22 05:30:00 -04:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2011-10-10 14:21:15 -04:00
|
|
|
#include <sys/stat.h>
|
2010-08-01 16:55:24 -04:00
|
|
|
#include <sys/un.h>
|
2009-03-22 05:30:00 -04:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "anet.h"
|
|
|
|
|
|
|
|
static void anetSetError(char *err, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
if (!err) return;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(err, ANET_ERR_LEN, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
int anetNonBlock(char *err, int fd)
|
|
|
|
{
|
|
|
|
int flags;
|
|
|
|
|
2013-01-16 12:00:20 -05:00
|
|
|
/* Set the socket non-blocking.
|
2009-03-22 05:30:00 -04:00
|
|
|
* Note that fcntl(2) for F_GETFL and F_SETFL can't be
|
|
|
|
* interrupted by a signal. */
|
|
|
|
if ((flags = fcntl(fd, F_GETFL)) == -1) {
|
2011-01-05 04:50:47 -05:00
|
|
|
anetSetError(err, "fcntl(F_GETFL): %s", strerror(errno));
|
2009-03-22 05:30:00 -04:00
|
|
|
return ANET_ERR;
|
|
|
|
}
|
|
|
|
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
|
2011-01-05 04:50:47 -05:00
|
|
|
anetSetError(err, "fcntl(F_SETFL,O_NONBLOCK): %s", strerror(errno));
|
2009-03-22 05:30:00 -04:00
|
|
|
return ANET_ERR;
|
|
|
|
}
|
|
|
|
return ANET_OK;
|
|
|
|
}
|
|
|
|
|
2013-02-08 10:30:21 -05:00
|
|
|
/* Set TCP keep alive option to detect dead peers. The interval option
|
|
|
|
* is only used for Linux as we are using Linux-specific APIs to set
|
|
|
|
* the probe send time, interval, and count. */
|
|
|
|
int anetKeepAlive(char *err, int fd, int interval)
|
|
|
|
{
|
|
|
|
int val = 1;
|
|
|
|
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) == -1)
|
|
|
|
{
|
|
|
|
anetSetError(err, "setsockopt SO_KEEPALIVE: %s", strerror(errno));
|
|
|
|
return ANET_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
/* Default settings are more or less garbage, with the keepalive time
|
|
|
|
* set to 7200 by default on Linux. Modify settings to make the feature
|
|
|
|
* actually useful. */
|
|
|
|
|
|
|
|
/* Send first probe after interval. */
|
|
|
|
val = interval;
|
|
|
|
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof(val)) < 0) {
|
|
|
|
anetSetError(err, "setsockopt TCP_KEEPIDLE: %s\n", strerror(errno));
|
|
|
|
return ANET_ERR;
|
|
|
|
}
|
|
|
|
|
2013-02-08 11:06:01 -05:00
|
|
|
/* Send next probes after the specified interval. Note that we set the
|
|
|
|
* delay as interval / 3, as we send three probes before detecting
|
|
|
|
* an error (see the next setsockopt call). */
|
|
|
|
val = interval/3;
|
|
|
|
if (val == 0) val = 1;
|
2013-02-08 10:30:21 -05:00
|
|
|
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof(val)) < 0) {
|
|
|
|
anetSetError(err, "setsockopt TCP_KEEPINTVL: %s\n", strerror(errno));
|
|
|
|
return ANET_ERR;
|
|
|
|
}
|
|
|
|
|
2013-02-08 11:06:01 -05:00
|
|
|
/* Consider the socket in error state after three we send three ACK
|
|
|
|
* probes without getting a reply. */
|
|
|
|
val = 3;
|
2013-02-08 10:30:21 -05:00
|
|
|
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof(val)) < 0) {
|
|
|
|
anetSetError(err, "setsockopt TCP_KEEPCNT: %s\n", strerror(errno));
|
|
|
|
return ANET_ERR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ANET_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-31 05:14:15 -05:00
|
|
|
static int anetSetTcpNoDelay(char *err, int fd, int val)
|
2009-03-22 05:30:00 -04:00
|
|
|
{
|
2013-01-31 05:14:15 -05:00
|
|
|
if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)) == -1)
|
2009-03-22 05:30:00 -04:00
|
|
|
{
|
2011-01-05 04:50:47 -05:00
|
|
|
anetSetError(err, "setsockopt TCP_NODELAY: %s", strerror(errno));
|
2009-03-22 05:30:00 -04:00
|
|
|
return ANET_ERR;
|
|
|
|
}
|
|
|
|
return ANET_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-31 05:14:15 -05:00
|
|
|
int anetEnableTcpNoDelay(char *err, int fd)
|
2013-01-30 22:09:16 -05:00
|
|
|
{
|
2013-01-31 05:14:15 -05:00
|
|
|
return anetSetTcpNoDelay(err, fd, 1);
|
2013-01-30 22:09:16 -05:00
|
|
|
}
|
|
|
|
|
2013-01-31 05:14:15 -05:00
|
|
|
int anetDisableTcpNoDelay(char *err, int fd)
|
2013-01-30 22:09:16 -05:00
|
|
|
{
|
2013-01-31 05:14:15 -05:00
|
|
|
return anetSetTcpNoDelay(err, fd, 0);
|
2013-01-30 22:09:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-22 05:30:00 -04:00
|
|
|
int anetSetSendBuffer(char *err, int fd, int buffsize)
|
|
|
|
{
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &buffsize, sizeof(buffsize)) == -1)
|
|
|
|
{
|
2011-01-05 04:50:47 -05:00
|
|
|
anetSetError(err, "setsockopt SO_SNDBUF: %s", strerror(errno));
|
2009-03-22 05:30:00 -04:00
|
|
|
return ANET_ERR;
|
|
|
|
}
|
|
|
|
return ANET_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int anetTcpKeepAlive(char *err, int fd)
|
|
|
|
{
|
|
|
|
int yes = 1;
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof(yes)) == -1) {
|
2011-01-05 04:50:47 -05:00
|
|
|
anetSetError(err, "setsockopt SO_KEEPALIVE: %s", strerror(errno));
|
2009-03-22 05:30:00 -04:00
|
|
|
return ANET_ERR;
|
|
|
|
}
|
|
|
|
return ANET_OK;
|
|
|
|
}
|
|
|
|
|
2011-06-16 19:29:49 -04:00
|
|
|
int anetResolve(char *err, char *host, char *ipbuf, size_t ipbuf_len)
|
2009-03-22 05:30:00 -04:00
|
|
|
{
|
2011-06-16 19:29:49 -04:00
|
|
|
struct addrinfo hints, *info;
|
|
|
|
int rv;
|
2009-03-22 05:30:00 -04:00
|
|
|
|
2011-06-16 19:29:49 -04:00
|
|
|
memset(&hints,0,sizeof(hints));
|
2011-06-18 08:25:31 -04:00
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM; /* specify socktype to avoid dups */
|
2011-06-16 19:29:49 -04:00
|
|
|
|
|
|
|
if ((rv = getaddrinfo(host, NULL, &hints, &info)) != 0) {
|
|
|
|
anetSetError(err, "%s", gai_strerror(rv));
|
|
|
|
return ANET_ERR;
|
|
|
|
}
|
|
|
|
if (info->ai_family == AF_INET) {
|
|
|
|
struct sockaddr_in *sa = (struct sockaddr_in *)info->ai_addr;
|
2011-06-18 08:25:31 -04:00
|
|
|
inet_ntop(AF_INET, &(sa->sin_addr), ipbuf, ipbuf_len);
|
|
|
|
} else {
|
|
|
|
struct sockaddr_in6 *sa = (struct sockaddr_in6 *)info->ai_addr;
|
|
|
|
inet_ntop(AF_INET6, &(sa->sin6_addr), ipbuf, ipbuf_len);
|
2009-03-22 05:30:00 -04:00
|
|
|
}
|
2011-06-16 19:29:49 -04:00
|
|
|
|
|
|
|
freeaddrinfo(info);
|
2009-03-22 05:30:00 -04:00
|
|
|
return ANET_OK;
|
|
|
|
}
|
|
|
|
|
2011-06-16 19:55:00 -04:00
|
|
|
static int anetSetReuseAddr(char *err, int fd) {
|
|
|
|
int yes = 1;
|
|
|
|
/* Make sure connection-intensive things like the redis benckmark
|
|
|
|
* will be able to close/open sockets a zillion of times */
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) {
|
|
|
|
anetSetError(err, "setsockopt SO_REUSEADDR: %s", strerror(errno));
|
|
|
|
return ANET_ERR;
|
|
|
|
}
|
|
|
|
return ANET_OK;
|
|
|
|
}
|
|
|
|
|
2010-10-13 10:33:27 -04:00
|
|
|
static int anetCreateSocket(char *err, int domain) {
|
2011-06-16 19:55:00 -04:00
|
|
|
int s;
|
2010-10-13 10:33:27 -04:00
|
|
|
if ((s = socket(domain, SOCK_STREAM, 0)) == -1) {
|
2011-01-05 04:50:47 -05:00
|
|
|
anetSetError(err, "creating socket: %s", strerror(errno));
|
2010-10-13 10:33:27 -04:00
|
|
|
return ANET_ERR;
|
|
|
|
}
|
|
|
|
|
2013-01-16 12:00:20 -05:00
|
|
|
/* Make sure connection-intensive things like the redis benchmark
|
2010-10-13 10:33:27 -04:00
|
|
|
* will be able to close/open sockets a zillion of times */
|
2011-06-16 19:55:00 -04:00
|
|
|
if (anetSetReuseAddr(err,s) == ANET_ERR) {
|
|
|
|
close(s);
|
2010-10-13 10:33:27 -04:00
|
|
|
return ANET_ERR;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2009-03-22 05:30:00 -04:00
|
|
|
#define ANET_CONNECT_NONE 0
|
|
|
|
#define ANET_CONNECT_NONBLOCK 1
|
|
|
|
static int anetTcpGenericConnect(char *err, char *addr, int port, int flags)
|
|
|
|
{
|
2011-06-16 20:06:19 -04:00
|
|
|
int s, rv;
|
|
|
|
char _port[6]; /* strlen("65535"); */
|
|
|
|
struct addrinfo hints, *servinfo, *p;
|
|
|
|
|
|
|
|
snprintf(_port,6,"%d",port);
|
|
|
|
memset(&hints,0,sizeof(hints));
|
2011-06-18 14:54:05 -04:00
|
|
|
hints.ai_family = AF_UNSPEC;
|
2011-06-16 20:06:19 -04:00
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
2009-03-22 05:30:00 -04:00
|
|
|
|
2011-06-16 20:06:19 -04:00
|
|
|
if ((rv = getaddrinfo(addr,_port,&hints,&servinfo)) != 0) {
|
|
|
|
anetSetError(err, "%s", gai_strerror(rv));
|
2009-03-22 05:30:00 -04:00
|
|
|
return ANET_ERR;
|
2011-06-16 20:06:19 -04:00
|
|
|
}
|
|
|
|
for (p = servinfo; p != NULL; p = p->ai_next) {
|
|
|
|
if ((s = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == -1)
|
|
|
|
continue;
|
2009-03-22 05:30:00 -04:00
|
|
|
|
2011-06-16 20:06:19 -04:00
|
|
|
/* if we set err then goto cleanup, otherwise next */
|
|
|
|
if (anetSetReuseAddr(err,s) == ANET_ERR) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (flags & ANET_CONNECT_NONBLOCK) {
|
|
|
|
if (anetNonBlock(err,s) != ANET_OK)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (connect(s,p->ai_addr,p->ai_addrlen) == -1) {
|
|
|
|
if (errno == EINPROGRESS &&
|
|
|
|
flags & ANET_CONNECT_NONBLOCK)
|
|
|
|
goto end;
|
2009-03-22 05:30:00 -04:00
|
|
|
|
|
|
|
close(s);
|
2011-06-16 20:06:19 -04:00
|
|
|
continue;
|
2009-03-22 05:30:00 -04:00
|
|
|
}
|
2011-06-16 20:06:19 -04:00
|
|
|
|
|
|
|
/* break with the socket */
|
|
|
|
goto end;
|
2009-03-22 05:30:00 -04:00
|
|
|
}
|
2011-06-16 20:06:19 -04:00
|
|
|
if (p == NULL) {
|
|
|
|
anetSetError(err, "creating socket: %s", strerror(errno));
|
|
|
|
goto error;
|
2009-03-22 05:30:00 -04:00
|
|
|
}
|
|
|
|
|
2011-06-16 20:06:19 -04:00
|
|
|
error:
|
|
|
|
s = ANET_ERR;
|
|
|
|
end:
|
|
|
|
freeaddrinfo(servinfo);
|
2009-03-22 05:30:00 -04:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
int anetTcpConnect(char *err, char *addr, int port)
|
|
|
|
{
|
|
|
|
return anetTcpGenericConnect(err,addr,port,ANET_CONNECT_NONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
int anetTcpNonBlockConnect(char *err, char *addr, int port)
|
|
|
|
{
|
|
|
|
return anetTcpGenericConnect(err,addr,port,ANET_CONNECT_NONBLOCK);
|
|
|
|
}
|
|
|
|
|
2010-08-01 16:55:24 -04:00
|
|
|
int anetUnixGenericConnect(char *err, char *path, int flags)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
struct sockaddr_un sa;
|
|
|
|
|
2010-10-13 10:33:27 -04:00
|
|
|
if ((s = anetCreateSocket(err,AF_LOCAL)) == ANET_ERR)
|
2010-08-01 16:55:24 -04:00
|
|
|
return ANET_ERR;
|
2010-10-13 10:33:27 -04:00
|
|
|
|
2010-08-01 16:55:24 -04:00
|
|
|
sa.sun_family = AF_LOCAL;
|
|
|
|
strncpy(sa.sun_path,path,sizeof(sa.sun_path)-1);
|
|
|
|
if (flags & ANET_CONNECT_NONBLOCK) {
|
|
|
|
if (anetNonBlock(err,s) != ANET_OK)
|
|
|
|
return ANET_ERR;
|
|
|
|
}
|
|
|
|
if (connect(s,(struct sockaddr*)&sa,sizeof(sa)) == -1) {
|
|
|
|
if (errno == EINPROGRESS &&
|
|
|
|
flags & ANET_CONNECT_NONBLOCK)
|
|
|
|
return s;
|
|
|
|
|
2011-01-05 04:50:47 -05:00
|
|
|
anetSetError(err, "connect: %s", strerror(errno));
|
2010-08-01 16:55:24 -04:00
|
|
|
close(s);
|
|
|
|
return ANET_ERR;
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
int anetUnixConnect(char *err, char *path)
|
|
|
|
{
|
|
|
|
return anetUnixGenericConnect(err,path,ANET_CONNECT_NONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
int anetUnixNonBlockConnect(char *err, char *path)
|
|
|
|
{
|
|
|
|
return anetUnixGenericConnect(err,path,ANET_CONNECT_NONBLOCK);
|
|
|
|
}
|
|
|
|
|
2009-03-22 05:30:00 -04:00
|
|
|
/* Like read(2) but make sure 'count' is read before to return
|
|
|
|
* (unless error or EOF condition is encountered) */
|
2009-03-27 15:48:32 -04:00
|
|
|
int anetRead(int fd, char *buf, int count)
|
2009-03-22 05:30:00 -04:00
|
|
|
{
|
|
|
|
int nread, totlen = 0;
|
|
|
|
while(totlen != count) {
|
|
|
|
nread = read(fd,buf,count-totlen);
|
|
|
|
if (nread == 0) return totlen;
|
|
|
|
if (nread == -1) return -1;
|
|
|
|
totlen += nread;
|
|
|
|
buf += nread;
|
|
|
|
}
|
|
|
|
return totlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Like write(2) but make sure 'count' is read before to return
|
|
|
|
* (unless error is encountered) */
|
2009-03-27 15:48:32 -04:00
|
|
|
int anetWrite(int fd, char *buf, int count)
|
2009-03-22 05:30:00 -04:00
|
|
|
{
|
|
|
|
int nwritten, totlen = 0;
|
|
|
|
while(totlen != count) {
|
|
|
|
nwritten = write(fd,buf,count-totlen);
|
|
|
|
if (nwritten == 0) return totlen;
|
|
|
|
if (nwritten == -1) return -1;
|
|
|
|
totlen += nwritten;
|
|
|
|
buf += nwritten;
|
|
|
|
}
|
|
|
|
return totlen;
|
|
|
|
}
|
|
|
|
|
2010-10-13 10:33:27 -04:00
|
|
|
static int anetListen(char *err, int s, struct sockaddr *sa, socklen_t len) {
|
|
|
|
if (bind(s,sa,len) == -1) {
|
2011-01-05 04:50:47 -05:00
|
|
|
anetSetError(err, "bind: %s", strerror(errno));
|
2010-10-13 10:33:27 -04:00
|
|
|
close(s);
|
2009-03-22 05:30:00 -04:00
|
|
|
return ANET_ERR;
|
|
|
|
}
|
2012-04-11 11:04:31 -04:00
|
|
|
|
|
|
|
/* Use a backlog of 512 entries. We pass 511 to the listen() call because
|
|
|
|
* the kernel does: backlogsize = roundup_pow_of_two(backlogsize + 1);
|
|
|
|
* which will thus give us a backlog of 512 entries */
|
|
|
|
if (listen(s, 511) == -1) {
|
2011-01-05 04:50:47 -05:00
|
|
|
anetSetError(err, "listen: %s", strerror(errno));
|
2009-03-22 05:30:00 -04:00
|
|
|
close(s);
|
|
|
|
return ANET_ERR;
|
|
|
|
}
|
2010-10-13 10:33:27 -04:00
|
|
|
return ANET_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-19 18:31:41 -04:00
|
|
|
static int anetV6Only(char *err, int s) {
|
|
|
|
int yes = 1;
|
|
|
|
if (setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,&yes,sizeof(yes)) == -1) {
|
|
|
|
anetSetError(err, "setsockopt: %s", strerror(errno));
|
|
|
|
close(s);
|
|
|
|
return ANET_ERR;
|
|
|
|
}
|
|
|
|
return ANET_OK;
|
|
|
|
}
|
|
|
|
|
2013-07-05 05:07:55 -04:00
|
|
|
int anetTcpServer(char *err, int port, char *bindaddr)
|
2010-10-13 10:33:27 -04:00
|
|
|
{
|
2011-06-16 20:49:21 -04:00
|
|
|
int s, rv;
|
|
|
|
char _port[6]; /* strlen("65535") */
|
|
|
|
struct addrinfo hints, *servinfo, *p;
|
2010-10-13 10:33:27 -04:00
|
|
|
|
2011-06-16 20:49:21 -04:00
|
|
|
snprintf(_port,6,"%d",port);
|
|
|
|
memset(&hints,0,sizeof(hints));
|
|
|
|
hints.ai_family = AF_INET;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_flags = AI_PASSIVE; /* No effect if bindaddr != NULL */
|
2010-10-13 10:33:27 -04:00
|
|
|
|
2011-06-16 20:49:21 -04:00
|
|
|
if ((rv = getaddrinfo(bindaddr,_port,&hints,&servinfo)) != 0) {
|
|
|
|
anetSetError(err, "%s", gai_strerror(rv));
|
2013-07-05 05:07:55 -04:00
|
|
|
return ANET_ERR;
|
2009-03-22 05:30:00 -04:00
|
|
|
}
|
2011-06-16 20:49:21 -04:00
|
|
|
for (p = servinfo; p != NULL; p = p->ai_next) {
|
|
|
|
if ((s = socket(p->ai_family,p->ai_socktype,p->ai_protocol)) == -1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (anetListen(err,s,p->ai_addr,p->ai_addrlen) == ANET_ERR)
|
|
|
|
goto error; /* could continue here? */
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (p == NULL) {
|
|
|
|
anetSetError(err, "unable to bind socket");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
|
|
|
s = ANET_ERR;
|
|
|
|
end:
|
|
|
|
freeaddrinfo(servinfo);
|
2009-03-22 05:30:00 -04:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2011-10-10 14:21:15 -04:00
|
|
|
int anetUnixServer(char *err, char *path, mode_t perm)
|
2010-08-01 16:55:24 -04:00
|
|
|
{
|
|
|
|
int s;
|
|
|
|
struct sockaddr_un sa;
|
|
|
|
|
2010-10-13 10:33:27 -04:00
|
|
|
if ((s = anetCreateSocket(err,AF_LOCAL)) == ANET_ERR)
|
2010-08-01 16:55:24 -04:00
|
|
|
return ANET_ERR;
|
2010-10-13 10:33:27 -04:00
|
|
|
|
2010-08-01 16:55:24 -04:00
|
|
|
memset(&sa,0,sizeof(sa));
|
|
|
|
sa.sun_family = AF_LOCAL;
|
|
|
|
strncpy(sa.sun_path,path,sizeof(sa.sun_path)-1);
|
2010-10-13 10:33:27 -04:00
|
|
|
if (anetListen(err,s,(struct sockaddr*)&sa,sizeof(sa)) == ANET_ERR)
|
2010-08-01 16:55:24 -04:00
|
|
|
return ANET_ERR;
|
2011-10-10 14:21:15 -04:00
|
|
|
if (perm)
|
|
|
|
chmod(sa.sun_path, perm);
|
2010-08-01 16:55:24 -04:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2010-10-13 12:34:24 -04:00
|
|
|
static int anetGenericAccept(char *err, int s, struct sockaddr *sa, socklen_t *len) {
|
2009-03-22 05:30:00 -04:00
|
|
|
int fd;
|
|
|
|
while(1) {
|
2010-10-13 12:34:24 -04:00
|
|
|
fd = accept(s,sa,len);
|
2009-03-22 05:30:00 -04:00
|
|
|
if (fd == -1) {
|
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
|
|
|
else {
|
2011-01-05 04:50:47 -05:00
|
|
|
anetSetError(err, "accept: %s", strerror(errno));
|
2009-03-22 05:30:00 -04:00
|
|
|
return ANET_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2010-10-13 12:34:24 -04:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2011-06-17 14:47:49 -04:00
|
|
|
int anetTcpAccept(char *err, int s, char *ip, size_t ip_len, int *port) {
|
2010-10-13 12:34:24 -04:00
|
|
|
int fd;
|
2011-06-18 09:47:38 -04:00
|
|
|
struct sockaddr_storage sa;
|
2010-10-13 12:34:24 -04:00
|
|
|
socklen_t salen = sizeof(sa);
|
|
|
|
if ((fd = anetGenericAccept(err,s,(struct sockaddr*)&sa,&salen)) == ANET_ERR)
|
|
|
|
return ANET_ERR;
|
|
|
|
|
2011-06-18 09:47:38 -04:00
|
|
|
if (sa.ss_family == AF_INET) {
|
|
|
|
struct sockaddr_in *s = (struct sockaddr_in *)&sa;
|
|
|
|
if (ip) inet_ntop(AF_INET,(void*)&(s->sin_addr),ip,ip_len);
|
|
|
|
if (port) *port = ntohs(s->sin_port);
|
|
|
|
} else {
|
|
|
|
struct sockaddr_in6 *s = (struct sockaddr_in6 *)&sa;
|
|
|
|
if (ip) inet_ntop(AF_INET6,(void*)&(s->sin6_addr),ip,ip_len);
|
|
|
|
if (port) *port = ntohs(s->sin6_port);
|
|
|
|
}
|
2009-03-22 05:30:00 -04:00
|
|
|
return fd;
|
|
|
|
}
|
2010-10-13 12:34:24 -04:00
|
|
|
|
2010-10-13 12:50:07 -04:00
|
|
|
int anetUnixAccept(char *err, int s) {
|
2010-10-13 12:34:24 -04:00
|
|
|
int fd;
|
|
|
|
struct sockaddr_un sa;
|
|
|
|
socklen_t salen = sizeof(sa);
|
|
|
|
if ((fd = anetGenericAccept(err,s,(struct sockaddr*)&sa,&salen)) == ANET_ERR)
|
|
|
|
return ANET_ERR;
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
2011-04-21 09:38:02 -04:00
|
|
|
|
2011-06-17 14:47:49 -04:00
|
|
|
int anetPeerToString(int fd, char *ip, size_t ip_len, int *port) {
|
2011-06-18 11:20:08 -04:00
|
|
|
struct sockaddr_storage sa;
|
2011-04-21 09:38:02 -04:00
|
|
|
socklen_t salen = sizeof(sa);
|
|
|
|
|
2012-03-07 05:30:30 -05:00
|
|
|
if (getpeername(fd,(struct sockaddr*)&sa,&salen) == -1) {
|
|
|
|
*port = 0;
|
|
|
|
ip[0] = '?';
|
|
|
|
ip[1] = '\0';
|
|
|
|
return -1;
|
|
|
|
}
|
2011-06-18 11:20:08 -04:00
|
|
|
if (sa.ss_family == AF_INET) {
|
|
|
|
struct sockaddr_in *s = (struct sockaddr_in *)&sa;
|
|
|
|
if (ip) inet_ntop(AF_INET,(void*)&(s->sin_addr),ip,ip_len);
|
|
|
|
if (port) *port = ntohs(s->sin_port);
|
|
|
|
} else {
|
|
|
|
struct sockaddr_in6 *s = (struct sockaddr_in6 *)&sa;
|
|
|
|
if (ip) inet_ntop(AF_INET6,(void*)&(s->sin6_addr),ip,ip_len);
|
|
|
|
if (port) *port = ntohs(s->sin6_port);
|
|
|
|
}
|
2011-04-21 09:38:02 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2012-07-23 06:54:52 -04:00
|
|
|
|
2011-06-17 14:47:49 -04:00
|
|
|
int anetSockName(int fd, char *ip, size_t ip_len, int *port) {
|
2011-06-18 11:20:08 -04:00
|
|
|
struct sockaddr_storage sa;
|
2012-07-23 06:54:52 -04:00
|
|
|
socklen_t salen = sizeof(sa);
|
|
|
|
|
|
|
|
if (getsockname(fd,(struct sockaddr*)&sa,&salen) == -1) {
|
|
|
|
*port = 0;
|
|
|
|
ip[0] = '?';
|
|
|
|
ip[1] = '\0';
|
|
|
|
return -1;
|
|
|
|
}
|
2011-06-18 11:20:08 -04:00
|
|
|
if (sa.ss_family == AF_INET) {
|
|
|
|
struct sockaddr_in *s = (struct sockaddr_in *)&sa;
|
|
|
|
if (ip) inet_ntop(AF_INET,(void*)&(s->sin_addr),ip,ip_len);
|
|
|
|
if (port) *port = ntohs(s->sin_port);
|
|
|
|
} else {
|
|
|
|
struct sockaddr_in6 *s = (struct sockaddr_in6 *)&sa;
|
|
|
|
if (ip) inet_ntop(AF_INET6,(void*)&(s->sin6_addr),ip,ip_len);
|
|
|
|
if (port) *port = ntohs(s->sin6_port);
|
|
|
|
}
|
2012-07-23 06:54:52 -04:00
|
|
|
return 0;
|
|
|
|
}
|