| version 1.1, 2005/01/27 14:58:56 |
version 1.2, 2007/11/08 12:01:52 |
|
|
| * 2005-01-27 - import into Platon SDG CVS repository (nepto) |
* 2005-01-27 - import into Platon SDG CVS repository (nepto) |
| */ |
*/ |
| |
|
| /* $Platon$ */ |
/* $Platon: libplaton/utils/server-backend/relayd.c,v 1.1 2005-01-27 14:58:56 nepto Exp $ */ |
| |
|
| /* |
/* |
| * relayd.c -- a relay daemon (using one targethost/port) |
* relayd.c -- a relay daemon (using one targethost/port) |
|
|
| |
|
| void |
void |
| usage() { |
usage() { |
| fprintf(stderr,"Usage: relayd <listenport> <targethost> [<targetport>]\n"); |
fprintf(stderr,"Usage: relayd <listenhost> <listenport> <targethost> [<targetport>]\n"); |
| |
fprintf(stderr," - listenhost: hostname or IP address where to bind and listen\n"); |
| |
fprintf(stderr," (use `*' for all interfaces)\n"); |
| |
fprintf(stderr," - listenport: port where to bind and listen\n"); |
| |
fprintf(stderr," - targethost: hostname wherefrom forward data\n"); |
| |
fprintf(stderr," - targetport: port for target hostname\n"); |
| |
fprintf(stderr," (default: same as listenport)\n"); |
| } |
} |
| |
|
| int |
int |
|
|
| char readbuf[1000],relaystring[1000]; |
char readbuf[1000],relaystring[1000]; |
| struct in_addr targetaddr; |
struct in_addr targetaddr; |
| struct hostent *hp; |
struct hostent *hp; |
| char *targethost; |
char *listenhost,*targethost; |
| int port,targetport; |
int listenport,targetport; |
| |
|
| #ifdef _WIN32 |
#ifdef _WIN32 |
| { |
{ |
|
|
| default: |
default: |
| case 1: |
case 1: |
| case 2: |
case 2: |
| |
case 3: |
| usage(); |
usage(); |
| return 1; |
return 1; |
| case 3: |
case 4: |
| if (!sscanf(argv[1],"%d",&port)) { |
if (!sscanf(argv[2],"%d",&listenport)) { |
| usage(); |
usage(); |
| return 1; |
return 1; |
| } |
} |
| targethost = argv[2]; |
listenhost=argv[1]; |
| targetport = 23; |
targethost = argv[3]; |
| |
targetport = listenport; |
| break; |
break; |
| case 4: |
case 5: |
| if (!sscanf(argv[1],"%d",&port)) { |
if (!sscanf(argv[2],"%d",&listenport)) { |
| usage(); |
usage(); |
| return 1; |
return 1; |
| } |
} |
| if (!sscanf(argv[3],"%d",&targetport)) { |
if (!sscanf(argv[4],"%d",&targetport)) { |
| usage(); |
usage(); |
| return 1; |
return 1; |
| } |
} |
| targethost=argv[2]; |
listenhost=argv[1]; |
| |
targethost=argv[3]; |
| break; |
break; |
| } |
} |
| strcpy(relaystring,FAILMESSAGE); |
strcpy(relaystring,FAILMESSAGE); |
|
|
| } |
} |
| |
|
| acsa.sin_family=AF_INET; |
acsa.sin_family=AF_INET; |
| acsa.sin_port=htons(port); |
acsa.sin_port=htons(listenport); |
| acsa.sin_addr.s_addr=INADDR_ANY; |
if (strcmp("*", listenhost)) { |
| |
acsa.sin_addr.s_addr=inet_addr(listenhost); ; |
| |
} else { |
| |
acsa.sin_addr.s_addr=INADDR_ANY; |
| |
} |
| if (-1==bind(acfd,(struct sockaddr*)&acsa,sizeof(struct sockaddr_in))) { |
if (-1==bind(acfd,(struct sockaddr*)&acsa,sizeof(struct sockaddr_in))) { |
| perror("bind"); |
perror("bind"); |
| return 1; |
return 1; |