summaryrefslogtreecommitdiffstats
path: root/cacheserver
diff options
context:
space:
mode:
authorin2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2004-04-02 15:35:27 +0800
committerin2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2004-04-02 15:35:27 +0800
commit140de11241ddf5a5f394662579dfdd0de625ae5d (patch)
tree56622e859a8986fdb7f74a2c8ef34e0f2a8515f6 /cacheserver
parent890d75db21b5fcfa3c4aff77f761e8ca60878c3a (diff)
downloadpttbbs-140de11241ddf5a5f394662579dfdd0de625ae5d.tar
pttbbs-140de11241ddf5a5f394662579dfdd0de625ae5d.tar.gz
pttbbs-140de11241ddf5a5f394662579dfdd0de625ae5d.tar.bz2
pttbbs-140de11241ddf5a5f394662579dfdd0de625ae5d.tar.lz
pttbbs-140de11241ddf5a5f394662579dfdd0de625ae5d.tar.xz
pttbbs-140de11241ddf5a5f394662579dfdd0de625ae5d.tar.zst
pttbbs-140de11241ddf5a5f394662579dfdd0de625ae5d.zip
git-svn-id: http://opensvn.csie.org/pttbbs/branches/outtacache@1676 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
Diffstat (limited to 'cacheserver')
-rw-r--r--cacheserver/Makefile5
-rw-r--r--cacheserver/cacheserver.c88
2 files changed, 76 insertions, 17 deletions
diff --git a/cacheserver/Makefile b/cacheserver/Makefile
index 65a64f14..a3f14888 100644
--- a/cacheserver/Makefile
+++ b/cacheserver/Makefile
@@ -6,5 +6,8 @@ all: cacheserver
cacheserver: cacheserver.c
${CC} ${CFLAGS} ${LDFLAGS} -o cacheserver cacheserver.c avltree.c
+clean:
+ rm -f cacheserver *~
+
test: cacheserver
- ./cacheserver -p /home/backup/ptt/PASSWDS.NEW
+ ./cacheserver -f /home/backup/ptt2/PASSWDS.NEW -p 5120
diff --git a/cacheserver/cacheserver.c b/cacheserver/cacheserver.c
index f6d48b0f..bbe288a5 100644
--- a/cacheserver/cacheserver.c
+++ b/cacheserver/cacheserver.c
@@ -1,12 +1,9 @@
/* $Id$ */
#include "bbs.h"
#include <avltree.h>
+#include <err.h>
-void usage(void)
-{
- fprintf(stderr, "usage: cacheserver -p PASSWD\n");
-}
-
+int tobind(int);
AVL_IX_DESC avl;
typedef struct {
@@ -15,9 +12,14 @@ typedef struct {
char key[IDLEN + 1];
} rectype_t;
+void usage(void)
+{
+ fprintf(stderr, "usage: cacheserver -f PASSWD -p port\n");
+}
+
int loadpasswd(char *pfn)
{
- int fd, i;
+ int fd, i, nUsers = 0;
userec_t u;
rectype_t rec;
@@ -32,13 +34,15 @@ int loadpasswd(char *pfn)
avl_create_index(&avl, AVL_NO_DUP_KEYS, IDLEN + 1);
- for( i = 0 ; read(fd, &u, sizeof(u)) == sizeof(u) && i < 1024 ; ++i ){
- rec.uid = i;
- rec.count = 0;
- strlcpy(rec.key, u.userid, sizeof(rec.key));
- printf("add %s -> %d\n", rec.key, rec.uid);
- avl_add_key((AVL_IX_REC*)&rec, &avl);
- }
+ for( i = 0 ; read(fd, &u, sizeof(u)) == sizeof(u) ; ++i )
+ if( u.userid[0] ){
+ ++nUsers;
+ rec.uid = i;
+ rec.count = 0;
+ strlcpy(rec.key, u.userid, sizeof(rec.key));
+ avl_add_key((AVL_IX_REC*)&rec, &avl);
+ }
+ fprintf(stderr, "%s users added\n", nUsers);
#if 0
rec.uid = 0;
@@ -50,24 +54,76 @@ int loadpasswd(char *pfn)
return 0;
}
+int service(int sfd)
+{
+ int cfd, len, size;
+ struct sockaddr_in clientaddr;
+
+ char buf[10240];
+ while( 1 ){
+ if( (cfd = accept(sfd, (struct sockaddr *)&clientaddr, &len)) < 0 ){
+ if( errno != EINTR )
+ sleep(1);
+ continue;
+ }
+
+ read(cfd, &size, sizeof(size));
+ for( len = 0 ; len < size ;
+ len += read(cfd, &buf[len], sizeof(buf) - len) )
+ ;
+ printf("total %d bytes\n", len);
+ close(cfd);
+ }
+}
+
int main(int argc, char **argv)
{
- int ch;
+ int ch, port = 0, sfd;
char *pfn = NULL;
- while( (ch = getopt(argc, argv, "p:h")) != -1 )
+ while( (ch = getopt(argc, argv, "f:p:h")) != -1 )
switch( ch ){
- case 'p':
+ case 'f':
pfn = strdup(optarg);
break;
+ case 'p':
+ port = atoi(optarg);
+ break;
+
case 'h':
default:
usage();
return 1;
}
+ if( (sfd = tobind(port)) < 0 )
+ return 1;
+
if( loadpasswd(pfn) < 0 )
return 1;
+ service(sfd);
return 0;
}
+
+/* utils */
+int tobind(int port)
+{
+ int i, sockfd, val;
+ struct sockaddr_in servaddr;
+
+ if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
+ err(1, NULL);
+ setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
+ (char *)&val, sizeof(val));
+ bzero(&servaddr, sizeof(servaddr));
+ servaddr.sin_family = AF_INET;
+ servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
+ servaddr.sin_port = htons(port);
+ if( bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0 )
+ err(1, NULL);
+ if( listen(sockfd, 5) < 0 )
+ err(1, NULL);
+
+ return sockfd;
+}