📄 263.htm
字号:
hostaddr.sin_port = servp->s_port; <br>
else if (atoi(pargs.service_name) > 0) <br>
hostaddr.sin_port = htons(atoi(pargs.service_name)); <br>
else { <br>
printf("%s: invalid/unknown service name or port number\r\n", <br>
pargs.service_name); <br>
exit(1); <br>
} <br>
} <br>
<br>
<br>
<br>
/**************************************************************************** <br>
function: daemonize <br>
description: detach the server process from the current context, <br>
creating a pristine, predictable environment in which it <br>
will execute. <br>
arguments: <br>
servfd file descriptor in use by server. <br>
<br>
return value: none. <br>
calls: none. <br>
globals: none. <br>
****************************************************************************/ <br>
<br>
void daemonize (servfd) <br>
int servfd; <br>
{ <br>
{ <br>
int childpid, fd, fdtablesize; <br>
<br>
/* ignore terminal I/O, stop signals */ <br>
signal(SIGTTOU,SIG_IGN); <br>
signal(SIGTTIN,SIG_IGN); <br>
signal(SIGTSTP,SIG_IGN); <br>
<br>
/* fork to put us in the background (whether or not the user <br>
specified '&' on the command line */ <br>
<br>
if ((childpid = fork()) < 0) { <br>
fputs("failed to fork first child\r\n",stderr); <br>
exit(1); <br>
} <br>
else if (childpid > 0) <br>
exit(0); /* terminate parent, continue in child */ <br>
<br>
/* dissociate from process group */ <br>
if (setpgrp(0,getpid()) < 0) { <br>
fputs("failed to become process group leader\r\n",stderr); <br>
exit(1); <br>
} <br>
} <br>
<br>
/* lose controlling terminal */ <br>
if ((fd = open("/dev/tty",O_RDWR)) >= 0) { <br>
ioctl(fd,TIOCNOTTY,NULL); <br>
close(fd); <br>
} <br>
<br>
/* close any open file descriptors */ <br>
for (fd = 0, fdtablesize = getdtablesize(); fd < fdtablesize; fd++) <br>
if (fd != servfd) <br>
close(fd); <br>
<br>
/* set working directory to / to allow filesystems to be unmounted */ <br>
chdir("/"); <br>
<br>
/* clear the inherited umask */ <br>
umask(0); <br>
<br>
/* setup zombie prevention */ <br>
signal(SIGCLD,reap_status); <br>
<br>
} <br>
} <br>
<br>
<br>
<br>
/**************************************************************************** <br>
function: do_proxy <br>
description: does the actual work of virtually connecting a client to <br>
the telnet service on the isolated host. <br>
arguments: <br>
usersockfd socket to which the client is connected. <br>
<br>
return value: none. <br>
calls: none. <br>
globals: reads hostaddr. <br>
****************************************************************************/ <br>
<br>
void do_proxy (usersockfd) <br>
int usersockfd; <br>
{ <br>
int isosockfd; <br>
fd_set rdfdset; <br>
int connstat; <br>
int iolen; <br>
int iolen; <br>
char buf [2048]; <br>
<br>
/* open a socket to connect to the isolated host */ <br>
if ((isosockfd = socket(AF_INET,SOCK_STREAM,0)) < 0) <br>
errorout("failed to create socket to host"); <br>
<br>
/* attempt a connection */ <br>
connstat = connect(isosockfd, <br>
(struct sockaddr *) &hostaddr, <br>
sizeof(hostaddr)); <br>
switch (connstat) { <br>
case 0: <br>
break; <br>
case ETIMEDOUT: <br>
case ECONNREFUSED: <br>
case ENETUNREACH: <br>
strcpy(buf,sys_errlist[errno]); <br>
strcat(buf,"\r\n"); <br>
write(usersockfd,buf,strlen(buf)); <br>
close(usersockfd); <br>
exit(1); /* die peacefully if we can't establish a connection */ <br>
break; <br>
break; <br>
default: <br>
errorout("failed to connect to host"); <br>
} <br>
<br>
<br>
/* now we're connected, serve fall into the data echo loop */ <br>
while (1) { <br>
/* Select for readability on either of our two sockets */ <br>
FD_ZERO(&rdfdset); <br>
FD_SET(usersockfd,&rdfdset); <br>
FD_SET(isosockfd,&rdfdset); <br>
if (select(FD_SETSIZE,&rdfdset,NULL,NULL,NULL) < 0) <br>
errorout("select failed"); <br>
<br>
/* is the client sending data? */ <br>
if (FD_ISSET(usersockfd,&rdfdset)) { <br>
if ((iolen = read(usersockfd,buf,sizeof(buf))) <= 0) <br>
break; /* zero length means the client disconnected */ <br>
<br>
write(isosockfd,buf,iolen); /* copy to host -- blocking semantics */ <br>
} <br>
<br>
<br>
/* is the host sending data? */ <br>
if (FD_ISSET(isosockfd,&rdfdset)) { <br>
if ((iolen = read(isosockfd,buf,sizeof(buf))) <= 0) <br>
break; /* zero length means the host disconnected */ <br>
<br>
write(usersockfd,buf,iolen); /* copy to client -- blocking semantics */ <br>
} <br>
} <br>
<br>
/* we're done with the sockets */ <br>
close(isosockfd); <br>
close(usersockfd); <br>
} <br>
<br>
<br>
<br>
/**************************************************************************** <br>
function: errorout <br>
description: displays an error message on the console and kills the <br>
current process. <br>
arguments: <br>
msg message to be displayed. <br>
<br>
return value: none -- does not return. <br>
calls: none. <br>
globals: none. <br>
****************************************************************************/ <br>
<br>
void errorout (msg) <br>
char *msg; <br>
{ <br>
FILE *console; <br>
<br>
console = fopen("/dev/console","a"); <br>
fprintf(console,"proxyd: %s\r\n",msg); <br>
fclose(console); <br>
exit(1); <br>
} <br>
<br>
<br>
<br>
/**************************************************************************** <br>
function: reap_status <br>
description: handle a SIGCLD signal by reaping the exit status of the <br>
perished child, and discarding it. <br>
arguments: none. <br>
return value: none. <br>
calls: none. <br>
globals: none. <br>
****************************************************************************/ <br>
<br>
void reap_status () <br>
{ <br>
int pid; <br>
union wait status; <br>
<br>
while ((pid = wait3(&status,WNOHANG,NULL)) > 0) <br>
; /* loop while there are more dead children */ <br>
} <br>
<br>
<br>
-- <br>
</small><hr>
<p align="center">[<a href="index.htm">回到开始</a>][<a href="192.htm">上一层</a>][<a href="264.htm">下一篇</a>]
<p align="center"><a href="http://cterm.163.net">欢迎访问Cterm主页</a></p>
</table>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -