diff options
author | in2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2004-04-04 17:59:40 +0800 |
---|---|---|
committer | in2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204> | 2004-04-04 17:59:40 +0800 |
commit | 983a7b8ef01b03cc7596751f431277ed7540c713 (patch) | |
tree | 9330d2b190001e4f884a8cf8fd58db84094ac39e /mbbsd/stuff.c | |
parent | eb86d8ff553917f392c556ef3a4b846910b1a17a (diff) | |
download | pttbbs-983a7b8ef01b03cc7596751f431277ed7540c713.tar pttbbs-983a7b8ef01b03cc7596751f431277ed7540c713.tar.gz pttbbs-983a7b8ef01b03cc7596751f431277ed7540c713.tar.bz2 pttbbs-983a7b8ef01b03cc7596751f431277ed7540c713.tar.lz pttbbs-983a7b8ef01b03cc7596751f431277ed7540c713.tar.xz pttbbs-983a7b8ef01b03cc7596751f431277ed7540c713.tar.zst pttbbs-983a7b8ef01b03cc7596751f431277ed7540c713.zip |
merge from outtacache branch
git-svn-id: http://opensvn.csie.org/pttbbs/trunk/pttbbs@1689 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
Diffstat (limited to 'mbbsd/stuff.c')
-rw-r--r-- | mbbsd/stuff.c | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/mbbsd/stuff.c b/mbbsd/stuff.c index 767fdca1..008f18f9 100644 --- a/mbbsd/stuff.c +++ b/mbbsd/stuff.c @@ -694,3 +694,72 @@ int qsort_intcompar(const void *a, const void *b) { return *(int *)a - *(int *)b; } + +#ifdef OUTTACACHE +#include <err.h> +int tobind(int port) +{ + int 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; +} + +int toconnect(char *host, int port) +{ + int sock; + 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; + } + return sock; +} + +int toread(int fd, void *buf, int len) +{ + int l; + for( l = 0 ; len > 0 ; ) + if( (l = read(fd, buf, len)) <= 0 ) + return -1; + else{ + buf += l; + len -= l; + } + return l; +} + +int towrite(int fd, void *buf, int len) +{ + int l; + for( l = 0 ; len > 0 ; ) + if( (l = write(fd, buf, len)) <= 0 ) + return -1; + else{ + buf += l; + len -= l; + } + return l; +} +#endif |