diff options
Diffstat (limited to 'util')
-rw-r--r-- | util/.cvsignore | 2 | ||||
-rw-r--r-- | util/Makefile | 14 | ||||
-rw-r--r-- | util/cacheserver.c | 120 | ||||
-rw-r--r-- | util/localserver.c | 116 |
4 files changed, 250 insertions, 2 deletions
diff --git a/util/.cvsignore b/util/.cvsignore index d33e906f..c72037ba 100644 --- a/util/.cvsignore +++ b/util/.cvsignore @@ -44,3 +44,5 @@ shmctl mdclean splitpasswd indexuser +cacheserver +localserver diff --git a/util/Makefile b/util/Makefile index 2a56cc0e..f704a564 100644 --- a/util/Makefile +++ b/util/Makefile @@ -1,4 +1,4 @@ -# $Id: Makefile,v 1.17 2002/08/15 09:44:36 in2 Exp $ +# $Id: Makefile,v 1.18 2002/08/17 12:31:56 in2 Exp $ BBSHOME?=$(HOME) OSTYPE!=uname @@ -30,7 +30,8 @@ CPROGS= bbsmail BM_money post account birth deluserfile expire mandex\ poststat showboard antispam countalldice webgrep bbsrf\ initbbs outmail xchatd userlist tunepasswd buildir reaper shmsweep\ merge_passwd merge_board inndBM buildAnnounce rmuid \ - toplazyBM jungo toplazyBBM shmctl mdclean splitpasswd indexuser + toplazyBM jungo toplazyBBM shmctl mdclean splitpasswd indexuser \ + cacheserver localserver testoc PROGS= $(CPROGS) BM_money.sh backpasswd.sh mailog.sh opendice.sh\ openticket.sh stock.sh topsong.sh weather.sh stock.perl weather.perl\ @@ -168,6 +169,15 @@ mdclean: mdclean.c splitpasswd: splitpasswd.c $(CC) $(CFLAGS) -o $@ $@.c +cacheserver: cacheserver.c + $(CC) $(CFLAGS) -I/usr/local/include -lavltree -L/usr/local/lib -o $@ $@.c + +localserver: localserver.c + $(CC) $(CFLAGS) -o $@ $@.c + +testoc: testoc.c + $(CC) $(CFLAGS) -o $@ $@.c + install: $(PROGS) install -d $(BBSHOME)/bin/ install -c -m 755 $(PROGS) $(BBSHOME)/bin/ diff --git a/util/cacheserver.c b/util/cacheserver.c new file mode 100644 index 00000000..14fe5909 --- /dev/null +++ b/util/cacheserver.c @@ -0,0 +1,120 @@ +#include "bbs.h" +#include <err.h> +#include <avltree.h> + +int acceptone(int port); +void usage(void); +ssize_t Read(int fd, void *buf, size_t nbytes); + +int main(int argc, char **argv) +{ + int sfd, len; + int port = -1; + char ch; + OCbuf_t OCbuf; + + AVL_IX_DESC ix; + AVL_IX_REC pe; + OCstore_t *store; + avl_create_index(&ix, AVL_COUNT_DUPS, OC_KEYLEN); + + while( (ch = getopt(argc, argv, "p:")) != -1 ) + switch( ch ){ + case 'p': + port = atoi(optarg); + break; + } + if( port == -1 ) + usage(); + + sfd = acceptone(port); + while( Read(sfd, &len, sizeof(len)) > 0 ){ + printf("reading %d bytes\n", len); + Read(sfd, &OCbuf, len); + printf("read! pid: %d\n", OCbuf.key.pid); + + memset(&pe, 0, sizeof(pe)); + if( OCbuf.key.pid <= 1 ){ + /* garbage collection */ + } + else if( OCbuf.key.pid < OC_pidadd ){ + /* store */ + puts("swapin"); + + len = OC_HEADERLEN + OCbuf.length; + store = (OCstore_t *)malloc(sizeof(time_t) + len); + store->mtime = time(NULL); + memcpy(&(store->data), &OCbuf, len); + + pe.recptr = (void *)store; + memcpy(pe.key, &OCbuf.key, OC_KEYLEN); + if( avl_add_key(&pe, &ix) != AVL_IX_OK ) + puts("add key error"); + } + else { + OCbuf.key.pid -= OC_pidadd; + memcpy(pe.key, &OCbuf.key, OC_KEYLEN); + if( avl_find_key(&pe, &ix) == AVL_IX_OK ){ + store = (OCstore_t *)pe.recptr; + + len = store->data.length + OC_HEADERLEN; + write(sfd, &len, sizeof(len)); + write(sfd, &(store->data), len); + free(store); + if( avl_delete_key(&pe, &ix) != AVL_IX_OK ) + puts("delete key error"); + } + else + puts("error"); + } + } + return 0; +} + +int acceptone(int port) +{ + int sockfd, val, cfd, len; + struct sockaddr_in servaddr, clientaddr; + + 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); + + len = sizeof(struct sockaddr_in); + + if( (cfd = accept(sockfd, (struct sockaddr *)&clientaddr, &len)) < 0 ) + err(1, NULL); + + close(sockfd); + return cfd; +} + +void usage(void) +{ + fprintf(stderr, + "usage:\tcacheserver [options]\n" + "-p port\tlistenport\n"); + exit(0); +} + +ssize_t Read(int fd, void *BUF, size_t nbytes) +{ + char *buf = (char *)BUF; + size_t thisgot, totalgot = nbytes; + while( nbytes > 0 ){ + if( (thisgot = read(fd, buf, nbytes)) <= 0 ) + err(1, "read from socket: "); + nbytes -= thisgot; + buf += thisgot; + } + return totalgot; +} diff --git a/util/localserver.c b/util/localserver.c new file mode 100644 index 00000000..eb90bd6f --- /dev/null +++ b/util/localserver.c @@ -0,0 +1,116 @@ +#include "bbs.h" +#include <err.h> + +void usage(void); +int connectserver(char *, int); +void listennetwork(int, int); +void listenmsgqueue(int, int); + +int main(int argc, char **argv) +{ + char *host = NULL; + int port = 0, ch, sfd, msto, mtos; + pid_t pid; + while( (ch = getopt(argc, argv, "s:p:")) != -1 ) + switch( ch ){ + case 's': + host = strdup(optarg); + break; + case 'p': + port = atoi(optarg); + break; + default: + usage(); + } + + if( host == NULL || port == 0 ) + usage(); + + printf("connecting to server %s:%d\n", host, port); + sfd = connectserver(host, port); + puts("connected"); + + puts("attaching message queue"); + if( (msto = msgget(OC_msto, 0600 | IPC_CREAT)) < 0 ) + err(1, "msgget OC_msto"); + if( (mtos = msgget(OC_mtos, 0600 | IPC_CREAT)) < 0 ) + err(1, "msgget OC_mtos"); + puts("attached"); + + if( (pid = fork()) < 0 ) + err(1, "fork()"); + else if( pid == 0 ) + listennetwork(sfd, msto); + listenmsgqueue(sfd, mtos); + + return 0; +} + +void usage(void) +{ + fprintf(stderr, "usage:\tlocalserver -s host -p port\n"); + exit(0); +} + +int connectserver(char *host, int port) +{ + struct sockaddr_in servaddr; + int fd; + + if( (fd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) + err(1, "socket"); + bzero(&servaddr, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + inet_pton(AF_INET, host, &servaddr.sin_addr); + servaddr.sin_port = htons(port); + if( connect(fd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0 ) + err(1, "connect"); + return fd; +} + +void listennetwork(int netfd, int msgfd) +{ + int len; + char buf[CACHE_BUFSIZE]; + printf("(%d)listening network %d => message queue %d\n", + getpid(), netfd, msgfd); + while( Read(netfd, &len, 4) > 0 ){ + if( Read(netfd, buf, len) < 0 ) + err(1, "(network)read()"); + if( msgsnd(msgfd, buf, len, 0) < 0 ) + err(1, "(network)msgsnd()"); + } + exit(0); +} + +void listenmsgqueue(int netfd, int msgfd) +{ + char buf[CACHE_BUFSIZE]; + OCbuf_t *ptr; + int len; + printf("(%d)listening message queue %d => network %d\n", + getpid(), msgfd, netfd); + while( msgrcv(msgfd, buf, CACHE_BUFSIZE, 0, 0) > 0 ){ + ptr = (OCbuf_t *)buf; + len = ptr->length + OC_HEADERLEN; + if( write(netfd, &len, sizeof(len)) < 0 || + write(netfd, ptr, ptr->length + OC_HEADERLEN) < 0 ) + err(1, "(msgqueue)write()"); + } + err(1, "(msgqueue)msgrcv()"); + exit(0); +} + +ssize_t Read(int fd, void *BUF, size_t nbytes) +{ + char *buf = (char *)BUF; + size_t thisgot, totalgot = nbytes; + while( nbytes > 0 ){ + if( (thisgot = read(fd, buf, nbytes)) <= 0 ) + err(1, "read from socket: "); + nbytes -= thisgot; + buf += thisgot; + } + return totalgot; +} + |