summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorwens <wens@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2008-04-10 12:17:08 +0800
committerwens <wens@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2008-04-10 12:17:08 +0800
commit7c766bf44d652f4f4ffad199c3fc556f82195d1f (patch)
treeaf59791178a0c95d604038051fd24e8572adaeea /common
parent883bf638558fbc87bacccbf5ee2b065382337b4f (diff)
downloadpttbbs-7c766bf44d652f4f4ffad199c3fc556f82195d1f.tar
pttbbs-7c766bf44d652f4f4ffad199c3fc556f82195d1f.tar.gz
pttbbs-7c766bf44d652f4f4ffad199c3fc556f82195d1f.tar.bz2
pttbbs-7c766bf44d652f4f4ffad199c3fc556f82195d1f.tar.lz
pttbbs-7c766bf44d652f4f4ffad199c3fc556f82195d1f.tar.xz
pttbbs-7c766bf44d652f4f4ffad199c3fc556f82195d1f.tar.zst
pttbbs-7c766bf44d652f4f4ffad199c3fc556f82195d1f.zip
Change tobind/toconnect interface.
**OUTTACACHE is now UTMPD** git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@4121 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
Diffstat (limited to 'common')
-rw-r--r--common/sys/net.c41
1 files changed, 29 insertions, 12 deletions
diff --git a/common/sys/net.c b/common/sys/net.c
index 0e6d98c9..df880f9b 100644
--- a/common/sys/net.c
+++ b/common/sys/net.c
@@ -8,6 +8,7 @@
#include <arpa/inet.h>
#include <unistd.h>
#include <ctype.h>
+#include <assert.h>
#include "cmsys.h"
@@ -32,11 +33,13 @@ ipstr2int(const char *ip)
return val;
}
-int tobind(const char * host, int port)
+int tobind(const char * addr)
{
int sockfd, val = 1;
- if (host != NULL && !isdigit(host[0])) {
+ assert(addr != NULL);
+
+ if (!isdigit(addr[0] && addr[0] != ':')) {
struct sockaddr_un servaddr;
if ( (sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0 ) {
@@ -45,7 +48,7 @@ int tobind(const char * host, int port)
}
servaddr.sun_family = AF_UNIX;
- strlcpy(servaddr.sun_path, host, sizeof(servaddr.sun_path));
+ strlcpy(servaddr.sun_path, addr, sizeof(servaddr.sun_path));
if (bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
perror("bind()");
@@ -53,6 +56,7 @@ int tobind(const char * host, int port)
}
}
else {
+ char buf[64], *port;
struct sockaddr_in servaddr;
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
@@ -60,16 +64,20 @@ int tobind(const char * host, int port)
exit(1);
}
+ strlcpy(buf, addr, sizeof(buf));
+ if ( (port = strchr(addr, ':')) != NULL)
+ *port++ = '\0';
+
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
(char *)&val, sizeof(val));
servaddr.sin_family = AF_INET;
- if (host == NULL || host[0] == 0)
+ if (buf[0] == '\0')
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
- else if (inet_aton(host, &servaddr.sin_addr) == 0) {
+ if (inet_aton(buf, &servaddr.sin_addr) == 0) {
perror("inet_aton()");
exit(1);
}
- servaddr.sin_port = htons(port);
+ servaddr.sin_port = htons(atoi(port));
if( bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0 ) {
perror("bind()");
@@ -85,19 +93,22 @@ int tobind(const char * host, int port)
return sockfd;
}
-int toconnect(const char *host, int port)
+int toconnect(const char *addr)
{
- int sock;
+ int sock;
- if (!isdigit(host[0])) {
+ assert(addr != NULL);
+
+ if (!isdigit(addr[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));
+ strlcpy(serv_name.sun_path, addr, sizeof(serv_name.sun_path));
if (connect(sock, (struct sockaddr *)&serv_name, sizeof(serv_name)) < 0) {
close(sock);
@@ -105,15 +116,21 @@ int toconnect(const char *host, int port)
}
}
else {
+ char buf[64], *port;
struct sockaddr_in serv_name;
+
if( (sock = socket(AF_INET, SOCK_STREAM, 0)) < 0 ){
perror("socket");
return -1;
}
+ strlcpy(buf, addr, sizeof(buf));
+ if ( (port = strchr(addr, ':')) != NULL)
+ *port++ = '\0';
+
serv_name.sin_family = AF_INET;
- serv_name.sin_addr.s_addr = inet_addr(host);
- serv_name.sin_port = htons(port);
+ serv_name.sin_addr.s_addr = inet_addr(buf);
+ serv_name.sin_port = htons(atoi(port));
if( connect(sock, (struct sockaddr*)&serv_name, sizeof(serv_name)) < 0 ){
close(sock);
return -1;