From 7c766bf44d652f4f4ffad199c3fc556f82195d1f Mon Sep 17 00:00:00 2001 From: wens Date: Thu, 10 Apr 2008 04:17:08 +0000 Subject: Change tobind/toconnect interface. **OUTTACACHE is now UTMPD** git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@4121 63ad8ddf-47c3-0310-b6dd-a9e9d9715204 --- common/sys/net.c | 41 +++++++++++++++++++++++++++++------------ daemon/fromd/fromd.c | 13 +++++-------- daemon/utmpd/authserver.c | 11 ++++------- daemon/utmpd/utmpserver.c | 13 +++++-------- daemon/utmpd/utmpserver2.c | 11 ++++------- daemon/utmpd/utmpserver3.c | 11 ++++------- daemon/utmpd/utmpsync.c | 2 +- include/cmsys.h | 4 ++-- include/pttstruct.h | 2 +- mbbsd/cal.c | 7 +++++-- mbbsd/mbbsd.c | 2 +- mbbsd/talk.c | 8 ++++---- sample/pttbbs.conf | 10 ++++------ 13 files changed, 69 insertions(+), 66 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 #include #include +#include #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; diff --git a/daemon/fromd/fromd.c b/daemon/fromd/fromd.c index 5e080f7c..de1ad44c 100644 --- a/daemon/fromd/fromd.c +++ b/daemon/fromd/fromd.c @@ -76,26 +76,23 @@ void daemonize() int main(int argc, char *argv[]) { - int ch, port = 5130, sfd; - char *iface_ip = NULL; + int ch, sfd; + char *iface_ip = ":5130"; Signal(SIGPIPE, SIG_IGN); - while ( (ch = getopt(argc, argv, "p:i:h")) != -1 ) + while ( (ch = getopt(argc, argv, "i:h")) != -1 ) switch( ch ){ - case 'p': - port = atoi(optarg); - break; case 'i': iface_ip = optarg; break; case 'h': default: - fprintf(stderr, "usage: %s [-i interface_ip] [-p port]\n", argv[0]); + fprintf(stderr, "usage: %s [-i [interface_ip]:port]\n", argv[0]); return 1; } - if ( (sfd = tobind(iface_ip, port)) < 0 ) + if ( (sfd = tobind(iface_ip)) < 0 ) return 1; daemonize(); diff --git a/daemon/utmpd/authserver.c b/daemon/utmpd/authserver.c index 5fbba03a..ce728fa9 100644 --- a/daemon/utmpd/authserver.c +++ b/daemon/utmpd/authserver.c @@ -206,21 +206,18 @@ void connection_accept(int fd, short event, void *arg) int main(int argc, char *argv[]) { - int ch, port = 5121, sfd; - char *iface_ip = NULL; + int ch, sfd; + char *iface_ip = ":5121"; Signal(SIGPIPE, SIG_IGN); - while( (ch = getopt(argc, argv, "p:i:h")) != -1 ) + while( (ch = getopt(argc, argv, "i:h")) != -1 ) switch( ch ){ - case 'p': - port = atoi(optarg); - break; case 'i': iface_ip = optarg; break; case 'h': default: - fprintf(stderr, "usage: authserver [-i interface_ip] [-p port]\n"); + fprintf(stderr, "usage: authserver [-i [interface_ip]:port]\n"); return 1; } diff --git a/daemon/utmpd/utmpserver.c b/daemon/utmpd/utmpserver.c index 19005a80..62b54a41 100644 --- a/daemon/utmpd/utmpserver.c +++ b/daemon/utmpd/utmpserver.c @@ -148,25 +148,22 @@ void flushwaitqueue(void) int main(int argc, char **argv) { struct sockaddr_in clientaddr; - int ch, port = 5120, sfd, cfd, len, index, uid; - char *iface_ip = NULL; + int ch, sfd, cfd, len, index, uid; + char *iface_ip = ":5120"; Signal(SIGPIPE, SIG_IGN); - while( (ch = getopt(argc, argv, "p:i:h")) != -1 ) + while( (ch = getopt(argc, argv, "i:h")) != -1 ) switch( ch ){ - case 'p': - port = atoi(optarg); - break; case 'i': iface_ip = optarg; break; case 'h': default: - fprintf(stderr, "usage: utmpserver [-i interface_ip] [-p port]\n"); + fprintf(stderr, "usage: utmpserver [-i [interface_ip]:port]\n"); return 1; } - if( (sfd = tobind(iface_ip, port)) < 0 ) + if( (sfd = tobind(iface_ip)) < 0 ) return 1; #ifdef NOFLOODING diff --git a/daemon/utmpd/utmpserver2.c b/daemon/utmpd/utmpserver2.c index 12c3a299..aca43245 100644 --- a/daemon/utmpd/utmpserver2.c +++ b/daemon/utmpd/utmpserver2.c @@ -183,8 +183,8 @@ void showstat(void) int main(int argc, char *argv[]) { struct sockaddr_in clientaddr; - int ch, port = 5120, sfd, cfd, len; - char *iface_ip = NULL; + int ch, sfd, cfd, len; + char *iface_ip = ":5120"; int cmd; int uid,index; int fail; @@ -199,17 +199,14 @@ int main(int argc, char *argv[]) #endif Signal(SIGPIPE, SIG_IGN); - while( (ch = getopt(argc, argv, "p:i:h")) != -1 ) + while( (ch = getopt(argc, argv, "i:h")) != -1 ) switch( ch ){ - case 'p': - port = atoi(optarg); - break; case 'i': iface_ip = optarg; break; case 'h': default: - fprintf(stderr, "usage: utmpserver [-i interface_ip] [-p port]\n"); + fprintf(stderr, "usage: utmpserver [-i [interface_ip]:port]\n"); return 1; } diff --git a/daemon/utmpd/utmpserver3.c b/daemon/utmpd/utmpserver3.c index 0e1eef02..de112802 100644 --- a/daemon/utmpd/utmpserver3.c +++ b/daemon/utmpd/utmpserver3.c @@ -304,8 +304,8 @@ void connection_accept(int fd, short event, void *arg) int main(int argc, char *argv[]) { - int ch, port = 5120, sfd; - char *iface_ip = NULL; + int ch, sfd; + char *iface_ip = ":5120"; #ifdef UTMPLOG logfp = fopen("utmp.log","a"); @@ -316,17 +316,14 @@ int main(int argc, char *argv[]) #endif Signal(SIGPIPE, SIG_IGN); - while( (ch = getopt(argc, argv, "p:i:h")) != -1 ) + while( (ch = getopt(argc, argv, "i:h")) != -1 ) switch( ch ){ - case 'p': - port = atoi(optarg); - break; case 'i': iface_ip = optarg; break; case 'h': default: - fprintf(stderr, "usage: utmpserver [-i interface_ip] [-p port]\n"); + fprintf(stderr, "usage: utmpserver [-i [interface_ip]:port]\n"); return 1; } diff --git a/daemon/utmpd/utmpsync.c b/daemon/utmpd/utmpsync.c index 69d1c623..53f61ac0 100644 --- a/daemon/utmpd/utmpsync.c +++ b/daemon/utmpd/utmpsync.c @@ -8,7 +8,7 @@ int main(int argc, char **argv) { int sfd, index, i; attach_SHM(); - if( (sfd = toconnect(OUTTACACHEHOST, OUTTACACHEPORT)) < 0 ) { + if( (sfd = toconnect(UTMPD_ADDR)) < 0 ) { printf("connect fail\n"); return 1; } diff --git a/include/cmsys.h b/include/cmsys.h index 7300183e..b077d627 100644 --- a/include/cmsys.h +++ b/include/cmsys.h @@ -64,8 +64,8 @@ extern void PttLock(int fd, int start, int size, int mode); /* net.c */ extern unsigned int ipstr2int(const char *ip); -extern int tobind(const char * host, int port); -extern int toconnect(const char *host, int port); +extern int tobind(const char *addr); +extern int toconnect(const char *addr); extern int toread(int fd, void *buf, int len); extern int towrite(int fd, const void *buf, int len); diff --git a/include/pttstruct.h b/include/pttstruct.h index ec116601..447ac05d 100644 --- a/include/pttstruct.h +++ b/include/pttstruct.h @@ -653,7 +653,7 @@ typedef struct { char y; } Horder_t; -#ifdef OUTTACACHE +#ifdef UTMPD typedef struct { int index; // 在 SHM->uinfo[index] int uid; // 避免在 cache server 上不同步, 再確認用. diff --git a/mbbsd/cal.c b/mbbsd/cal.c index 91ec3cf0..d37f1100 100644 --- a/mbbsd/cal.c +++ b/mbbsd/cal.c @@ -624,8 +624,11 @@ p_sysinfo(void) #ifdef CRITICAL_MEMORY " CRITICAL_MEMORY" #endif -#ifdef OUTTACACHE - " OUTTACACHE" +#ifdef UTMPD + " UTMPD" +#endif +#ifdef FROMD + " FROMD" #endif ); } diff --git a/mbbsd/mbbsd.c b/mbbsd/mbbsd.c index a45c2705..13e8159c 100644 --- a/mbbsd/mbbsd.c +++ b/mbbsd/mbbsd.c @@ -962,7 +962,7 @@ setup_utmp(int mode) #ifdef FROMD { int fd; - if ( (fd = toconnect(FROMD_HOST, FROMD_PORT)) >= 0 ) { + if ( (fd = toconnect(FROMD_ADDR)) >= 0 ) { write(fd, fromhost, strlen(fromhost)); // uinfo.from is zerod, so we don't care about read length read(fd, uinfo.from, sizeof(uinfo.from)); diff --git a/mbbsd/talk.c b/mbbsd/talk.c index ea694467..b4c92915 100644 --- a/mbbsd/talk.c +++ b/mbbsd/talk.c @@ -241,7 +241,7 @@ reverse_friend_stat(int stat) return stat1; } -#ifdef OUTTACACHE +#ifdef UTMPD int sync_outta_server(int sfd) { int i; @@ -309,14 +309,14 @@ void login_friend_online(void) int i, stat, stat1; int offset = (int)(currutmp - &SHM->uinfo[0]); -#ifdef OUTTACACHE +#ifdef UTMPD int sfd; - /* OUTTACACHE is TOO slow, let's prompt user here. */ + /* UTMPD is TOO slow, let's prompt user here. */ move(b_lines-2, 0); clrtobot(); outs("\n正在更新與同步線上使用者及好友名單,系統負荷量大時會需時較久...\n"); refresh(); - sfd = toconnect(OUTTACACHEHOST, OUTTACACHEPORT); + sfd = toconnect(UTMPD_ADDR); if(sfd>=0) { int res=sync_outta_server(sfd); if(res==0) // sfd will be closed if return 0 diff --git a/sample/pttbbs.conf b/sample/pttbbs.conf index 1d9802ed..278b4c91 100644 --- a/sample/pttbbs.conf +++ b/sample/pttbbs.conf @@ -266,17 +266,15 @@ /* 如果 time_t 是 8 bytes的話 (如 X86_64) */ //#define TIMET64 -/* 使用 cacheserver, 在外部運算好友資料, 如果您確定這個在做什麼才開啟 */ -//#define OUTTACACHE -//#define OUTTACACHEHOST "192.168.0.1" -//#define OUTTACACHEPORT 5120 +/* 使用 utmpd, 在外部運算好友資料, 如果您確定這個在做什麼才開啟 */ +//#define UTMPD +//#define UTMPD_ADDR "192.168.0.1:5120" /* 在 cacheserver 上面擋掉狂上下站的使用者 */ //#define NOFLOODING /* 使用 fromd, 使用外部daemon紀錄上站故鄉名稱 */ //#define FROMD -//#define FROMD_HOST "127.0.0.1" -//#define FROMD_PORT 5130 +//#define FROMD_ADDR "127.0.0.1:5130" /* 若定義, 則不允許註冊 guest */ //#define NO_GUEST_ACCOUNT_REG -- cgit v1.2.3