X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/51335102acb364be4c0652ff4e91c9a080b1e7e0..7a0c72f34550c3811324464661f1b463ccfd362b:/src/anet.c diff --git a/src/anet.c b/src/anet.c index 4e16f2e4..ba4e6cce 100644 --- a/src/anet.c +++ b/src/anet.c @@ -32,6 +32,7 @@ #include #include +#include #include #include #include @@ -291,7 +292,7 @@ int anetTcpServer(char *err, int port, char *bindaddr) return s; } -int anetUnixServer(char *err, char *path) +int anetUnixServer(char *err, char *path, mode_t perm) { int s; struct sockaddr_un sa; @@ -304,6 +305,8 @@ int anetUnixServer(char *err, char *path) strncpy(sa.sun_path,path,sizeof(sa.sun_path)-1); if (anetListen(err,s,(struct sockaddr*)&sa,sizeof(sa)) == ANET_ERR) return ANET_ERR; + if (perm) + chmod(sa.sun_path, perm); return s; } @@ -345,3 +348,18 @@ int anetUnixAccept(char *err, int s) { return fd; } + +int anetPeerToString(int fd, char *ip, int *port) { + struct sockaddr_in sa; + socklen_t salen = sizeof(sa); + + if (getpeername(fd,(struct sockaddr*)&sa,&salen) == -1) { + *port = 0; + ip[0] = '?'; + ip[1] = '\0'; + return -1; + } + if (ip) strcpy(ip,inet_ntoa(sa.sin_addr)); + if (port) *port = ntohs(sa.sin_port); + return 0; +}