- 论坛徽章:
- 0
|
/****************************************************************************
program: proxyd
module: proxyd.c
summary: provides proxy tcp service for a host on an isolated network.
programmer: Carl Harris (ceharris@vt.edu)
date: 22 Feb 94
description:
This code implements a daemon process which listens for tcp connec-
tions on a specified port number. When a connection is established,
a child is forked to handle the new client. The child then estab-
lishes a tcp connection to a port on the isolated host. The child
then falls into a loop in which it writes data to the isolated host
for the client and vice-versa. Once a child has been forked, the
parent resumes listening for additional connections.
The name of the isolated host and the port to serve as proxy for,
as well as the port number the server listen on are specified as
command line arguments.
http://www.ithao123.com/networkprog/0002.html
****************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define TCP_PROTO "tcp"
int proxy_port; /* port to listen for proxy connections on */
struct sockaddr_in hostaddr; /* host addr assembled from gethostbyname() */
extern int errno; /* defined by libc.a */
extern char *sys_errlist[]; /* defined by libc.a */
void parse_args (int argc, char **argv);
void daemonize (int servfd);
void do_proxy (int usersockfd);
void reap_status (void);
void errorout (char *msg);
/****************************************************************************
function: main
description: Main level driver. After daemonizing the process, a socket
is opened to listen for connections on the proxy port,
connections are accepted and children are spawned to handle
each new connection.
arguments:
argc,argv you know what those are.
return value: none.
calls: parse_args, do_proxy.
globals: reads proxy_port.
****************************************************************************/
main (argc,argv)
int argc;
char **argv;
{
int clilen;
int childpid;
int sockfd, newsockfd;
struct sockaddr_in servaddr, cliaddr;
parse_args(argc,argv);
/* prepare an address struct to listen for connections */
bzero((char *) &servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = proxy_port;
/* get a socket... */
if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) \r\n",
argv[0]);
exit(1);
}
strcpy(pargs.proxy_port,argv[1]);
strcpy(pargs.isolated_host,argv[2]);
strcpy(pargs.service_name,argv[3]);
for (i = 0; i h_addr,&hostaddr.sin_addr,hostp->h_length);
else {
printf("%s: unknown host\r\n",pargs.isolated_host);
exit(1);
}
if ((servp = getservbyname(pargs.service_name,TCP_PROTO)) != NULL)
hostaddr.sin_port = servp->s_port;
else if (atoi(pargs.service_name) > 0)
hostaddr.sin_port = htons(atoi(pargs.service_name));
else {
printf("%s: invalid/unknown service name or port number\r\n",
pargs.service_name);
exit(1);
}
}
/****************************************************************************
function: daemonize
description: detach the server process from the current context,
creating a pristine, predictable environment in which it
will execute.
arguments:
servfd file descriptor in use by server.
return value: none.
calls: none.
globals: none.
****************************************************************************/
void daemonize (servfd)
int servfd;
{
int childpid, fd, fdtablesize;
/* ignore terminal I/O, stop signals */
signal(SIGTTOU,SIG_IGN);
signal(SIGTTIN,SIG_IGN);
signal(SIGTSTP,SIG_IGN);
/* fork to put us in the background (whether or not the user
specified '&' on the command line */
if ((childpid = fork()) 0)
exit(0); /* terminate parent, continue in child */
/* dissociate from process group */
if (setpgrp(0,getpid()) = 0) {
ioctl(fd,TIOCNOTTY,NULL);
close(fd);
}
/* close any open file descriptors */
for (fd = 0, fdtablesize = getdtablesize(); fd 0)
; /* loop while there are more dead children */
}
/*Compiled by:
cc -O2 -object -s \
-arch m68k -arch i386 -arch hppa -arch sparc \
proxyd.c -o proxyd
usage: proxyd
Which means:
proxyd localport-on-this-machine the-host-this-is-to port-or-name
EXAMPLE:
Say you have a machine named MYHOST want to bind your port #777 to the TELNET port of the host REMOTEHOST.
Use: proxyd 777 REMOTEHOST telnet
Then if you did 'telnet MYHOST 777' you would actually connect to the TELNET port of the host REMOTEHOST.
As
far as the 'service-name/port-number' goes: services can be referred to
either by a number or a name. For example, NNTP requests are usually
on port 119, so if you wanted to connect directly to a machine to do
NNTP you could do either:
telnet REMOTEHOST nntp
or
telnet REMOTEHOST 119
NOTE/WARNING: Make sure to use an unused port on your machine! If you are not sure, do this:
nidump services .
to see a listing of all the services known on your machine.
This program does NOT always have to be run as root... Some ports are available even to regular users, it appears.
*/
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/40266/showart_313349.html |
|