| version 1.4, 2007/11/08 12:24:51 |
version 1.5, 2007/11/09 08:22:56 |
|
|
| /* |
/* |
| * relayd.c - a relay daemon |
* relayd.c - a relay daemon |
| * |
* |
| * Platon SDG mainteinance: |
* Platon Group maintenance: |
| * |
* |
| * 2001-10-04 - several modifications (nepto) |
* 2001-10-04 - several modifications (nepto) |
| * 2005-01-27 - import into Platon SDG CVS repository (nepto) |
* 2005-01-27 - import into Platon Group CVS repository (nepto) |
| |
* 2007-11-08 - implemented `listenhost' |
| |
* - fixed some warnings |
| |
* 2007-11-09 - added BUFSIZE constant for buffer sizes |
| |
* - write some debug information to STDERR |
| */ |
*/ |
| |
|
| /* $Platon: libplaton/utils/server-backend/relayd.c,v 1.3 2007-11-08 12:03:49 nepto Exp $ */ |
/* $Platon: libplaton/utils/server-backend/relayd.c,v 1.4 2007-11-08 12:24:51 nepto Exp $ */ |
| |
|
| /* |
/* |
| * relayd.c -- a relay daemon (using one targethost/port) |
* relayd.c -- a relay daemon (using one targethost/port) |
|
|
| * if failures occur, it will terminate the connection. |
* if failures occur, it will terminate the connection. |
| */ |
*/ |
| |
|
| |
/* size of read/write data bufer */ |
| |
#define BUFSIZE 10000 |
| |
|
| /* adjust this to a reasonable limit */ |
/* adjust this to a reasonable limit */ |
| #define MAXUSERS 120 |
#define MAXUSERS 20 |
| |
|
| /* message printed if all slots are used ... */ |
/* message printed if all slots are used ... */ |
| #define FAILMESSAGE "Sorry, all slots are full.\r\n" |
#define FAILMESSAGE "Sorry, all slots are full.\r\n" |
|
|
| int i,j,res; |
int i,j,res; |
| int acfd; |
int acfd; |
| struct sockaddr_in acsa; |
struct sockaddr_in acsa; |
| char readbuf[1000],relaystring[1000]; |
char readbuf[BUFSIZE],relaystring[BUFSIZE]; |
| struct in_addr targetaddr; |
struct in_addr targetaddr; |
| struct hostent *hp; |
struct hostent *hp; |
| char *listenhost,*targethost; |
char *listenhost,*targethost; |
|
|
| return 1; |
return 1; |
| } |
} |
| for (i=MAXUSERS;i--;) { |
for (i=MAXUSERS;i--;) { |
| inbuf[i]=malloc(10000*sizeof(char)); |
inbuf[i]=malloc(BUFSIZE*10*sizeof(char)); |
| outbuf[i]=malloc(10000*sizeof(char)); |
outbuf[i]=malloc(BUFSIZE*10*sizeof(char)); |
| inbuf[i][0]='\0';outbuf[i][0]='\0'; |
inbuf[i][0]='\0';outbuf[i][0]='\0'; |
| |
|
| insize[i]=10000;incur[i]=0; |
insize[i]=BUFSIZE*10;incur[i]=0; |
| |
|
| outsize[i]=10000;outcur[i]=0; |
outsize[i]=BUFSIZE*10;outcur[i]=0; |
| outfd[i]=infd[i]=-1; |
outfd[i]=infd[i]=-1; |
| } |
} |
| |
|
|
|
| if ((infd[i]==-1) && (outfd[i]==-1)) |
if ((infd[i]==-1) && (outfd[i]==-1)) |
| break; |
break; |
| if (i==-1) { |
if (i==-1) { |
| write(afd,relaystring,strlen(relaystring)); |
res=write(afd,relaystring,strlen(relaystring)); |
| |
#if defined(DEBUG) && DEBUG |
| |
fprintf(stderr, "[1] write() = %d\n", res); |
| |
#endif |
| close(afd); |
close(afd); |
| } else { |
} else { |
| char sendbuf[200]; |
char sendbuf[200]; |
|
|
| sprintf(readbuf,"Connect to %s failed: %s\n",targethost,strerror(errno)); |
sprintf(readbuf,"Connect to %s failed: %s\n",targethost,strerror(errno)); |
| #endif |
#endif |
| perror("connect"); |
perror("connect"); |
| if (-1==write(infd[i],readbuf,strlen(readbuf))) |
res=write(infd[i],readbuf,strlen(readbuf)); |
| |
#if defined(DEBUG) && DEBUG |
| |
fprintf(stderr, "[2] write() = %d\n", res); |
| |
#endif |
| |
if (-1==res) |
| perror("write"); |
perror("write"); |
| clean_connection(i); |
clean_connection(i); |
| continue; |
continue; |
|
|
| for (i=MAXUSERS;i--;) { |
for (i=MAXUSERS;i--;) { |
| if ((infd[i]>=0) && FD_ISSET(infd[i],&readfds)) { |
if ((infd[i]>=0) && FD_ISSET(infd[i],&readfds)) { |
| do { |
do { |
| if (-1==(res=read(infd[i],readbuf,1000))) { |
res=read(infd[i],readbuf,BUFSIZE); |
| |
#if defined(DEBUG) && DEBUG |
| |
fprintf(stderr, "[1] read() = %d\n", res); |
| |
#endif |
| |
if (-1==res) { |
| if (errno==EINTR) |
if (errno==EINTR) |
| continue; |
continue; |
| /* user side has broken the connection */ |
/* user side has broken the connection */ |
|
|
| } |
} |
| if ((outfd[i]>=0) && FD_ISSET(outfd[i],&readfds)) { |
if ((outfd[i]>=0) && FD_ISSET(outfd[i],&readfds)) { |
| do { |
do { |
| if (-1==(res=read(outfd[i],readbuf,1000))) { |
res=read(outfd[i],readbuf,BUFSIZE); |
| if (errno==EINTR) |
#if defined(DEBUG) && DEBUG |
| |
fprintf(stderr, "[2] read() = %d\n", res); |
| |
#endif |
| |
if (-1==res) { |
| |
if (errno==EINTR) { |
| |
fprintf(stderr, "EINTR\n"); |
| continue; |
continue; |
| |
} |
| /* the mudside has broken the |
/* the mudside has broken the |
| * connection. we still have |
* connection. we still have |
| * to transmit the rest of |
* to transmit the rest of |
|
|
| } |
} |
| if ((infd[i]>=0) && FD_ISSET(infd[i],&writefds)) { |
if ((infd[i]>=0) && FD_ISSET(infd[i],&writefds)) { |
| j=outcur[i]; |
j=outcur[i]; |
| if (-1==(res=write(infd[i],outbuf[i],j))) { |
res=write(infd[i],outbuf[i],j); |
| |
#if defined(DEBUG) && DEBUG |
| |
fprintf(stderr, "[3] write() = %d\n", res); |
| |
#endif |
| |
if (-1==res) { |
| if (errno!=EINTR) { |
if (errno!=EINTR) { |
| clean_connection(i); |
clean_connection(i); |
| } |
} |
|
|
| } |
} |
| if ((outfd[i]>=0) && FD_ISSET(outfd[i],&writefds)) { |
if ((outfd[i]>=0) && FD_ISSET(outfd[i],&writefds)) { |
| j=incur[i]; |
j=incur[i]; |
| if (-1==(res=write(outfd[i],inbuf[i],j))) { |
res=write(outfd[i],inbuf[i],j); |
| |
#if defined(DEBUG) && DEBUG |
| |
fprintf(stderr, "[4] write() = %d\n", res); |
| |
#endif |
| |
if (-1==res) { |
| if (errno!=EINTR) { |
if (errno!=EINTR) { |
| outfd[i]=-1; |
outfd[i]=-1; |
| /*clean_connection(i);*/ |
/*clean_connection(i);*/ |