diff options
author | wens <wens@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2008-04-09 19:19:52 +0800 |
---|---|---|
committer | wens <wens@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2008-04-09 19:19:52 +0800 |
commit | 42b76714d581d697344a6bf4d047b1b9a62cba6b (patch) | |
tree | d41c342f908f491705eb9a825161f6eb490ab226 | |
parent | 8ece8c40c2e59d2a39e05be707babc8828b5ad52 (diff) | |
download | pttbbs-42b76714d581d697344a6bf4d047b1b9a62cba6b.tar pttbbs-42b76714d581d697344a6bf4d047b1b9a62cba6b.tar.gz pttbbs-42b76714d581d697344a6bf4d047b1b9a62cba6b.tar.bz2 pttbbs-42b76714d581d697344a6bf4d047b1b9a62cba6b.tar.lz pttbbs-42b76714d581d697344a6bf4d047b1b9a62cba6b.tar.xz pttbbs-42b76714d581d697344a6bf4d047b1b9a62cba6b.tar.zst pttbbs-42b76714d581d697344a6bf4d047b1b9a62cba6b.zip |
Support UNIX socket domains
git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@4116 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
-rw-r--r-- | common/sys/net.c | 100 |
1 files changed, 71 insertions, 29 deletions
diff --git a/common/sys/net.c b/common/sys/net.c index 26f3cc50..10f5a351 100644 --- a/common/sys/net.c +++ b/common/sys/net.c @@ -3,9 +3,11 @@ #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> +#include <sys/un.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> +#include <ctype.h> #include "cmsys.h" @@ -33,28 +35,49 @@ ipstr2int(const char *ip) int tobind(const char * host, int port) { int sockfd, val = 1; - struct sockaddr_in servaddr; - if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) { - perror("socket()"); - exit(1); - } - setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, - (char *)&val, sizeof(val)); - bzero(&servaddr, sizeof(servaddr)); - servaddr.sin_family = AF_INET; - if (host == NULL || host[0] == 0) - servaddr.sin_addr.s_addr = htonl(INADDR_ANY); - else if (inet_aton(host, &servaddr.sin_addr) == 0) { - perror("inet_aton()"); - exit(1); + if (host != NULL && isdigit(host[0])) { + struct sockaddr_un servaddr; + + if ( (sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0 ) { + perror("socket()"); + exit(1); + } + + servaddr.sun_family = AF_UNIX; + strlcpy(servaddr.sun_path, host, sizeof(servaddr.sun_path)); + + if (bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { + perror("bind()"); + exit(1); + } } - servaddr.sin_port = htons(port); - if( bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0 ) { - perror("bind()"); - exit(1); + else { + struct sockaddr_in servaddr; + + if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) { + perror("socket()"); + exit(1); + } + + setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, + (char *)&val, sizeof(val)); + servaddr.sin_family = AF_INET; + if (host == NULL || host[0] == 0) + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); + else if (inet_aton(host, &servaddr.sin_addr) == 0) { + perror("inet_aton()"); + exit(1); + } + servaddr.sin_port = htons(port); + + if( bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0 ) { + perror("bind()"); + exit(1); + } } - if( listen(sockfd, 5) < 0 ) { + + if (listen(sockfd, 10) < 0) { perror("listen()"); exit(1); } @@ -65,19 +88,38 @@ int tobind(const char * host, int port) int toconnect(const char *host, int port) { int sock; - struct sockaddr_in serv_name; - if( (sock = socket(AF_INET, SOCK_STREAM, 0)) < 0 ){ - perror("socket"); - return -1; + + if (isdigit(host[0])) { + struct sockaddr_un serv_name; + if ( (sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0 ) { + perror("socket"); + return -1; + } + + serv_name.sun_family = AF_UNIX; + strlcpy(serv_name.sun_path, host, sizeof(serv_name.sun_path)); + + if (connect(sock, (struct sockaddr *)&serv_name, sizeof(serv_name)) < 0) { + close(sock); + return -1; + } } + else { + struct sockaddr_in serv_name; + if( (sock = socket(AF_INET, SOCK_STREAM, 0)) < 0 ){ + perror("socket"); + return -1; + } - serv_name.sin_family = AF_INET; - serv_name.sin_addr.s_addr = inet_addr(host); - serv_name.sin_port = htons(port); - if( connect(sock, (struct sockaddr*)&serv_name, sizeof(serv_name)) < 0 ){ - close(sock); - return -1; + serv_name.sin_family = AF_INET; + serv_name.sin_addr.s_addr = inet_addr(host); + serv_name.sin_port = htons(port); + if( connect(sock, (struct sockaddr*)&serv_name, sizeof(serv_name)) < 0 ){ + close(sock); + return -1; + } } + return sock; } |