Implement without following RFC1928.Only for interest.



Implement without following RFC1928.Only for interest.

共有五个函数,main,parse_args,init_daemon,do_proxy,error_quit.

parse_args : 处理命令行参数,初始化一些变量,即代理服务器本身端口proxy_port,
代理的远端主机sockaddr结构变量hostaddr.

init_daemon : 使用fork变成守护进程。

error_quit : 由于守护进程把stdin,stdout,stderr都关了,so need a function
to display error message on the console.

do_proxy : 代理处理。客户和代理服务器建立连接(usersockfd),代理服务器和客户
请求的服务器建立连接(remote_sockfd),所以此函数把一个socket的输出送到另一个
socket的输入,相反亦是如此。

1 /*
2 A simple proxy server
3 Copyright (C) 2005 by kf_701
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 The author can be reached as follows:
20 E_mail: kf_701@21cn.com
21 Address: hefei of china
22 Phone: 0551-2150103
23 */
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <ctype.h>
28 #include <errno.h>
29 #include <signal.h>
30 #include <unistd.h>
31 #include <sys/stat.h>
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/ioctl.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <sys/wait.h>
39 #include <netdb.h>
40
41 #define TCP_PROTO "tcp"
42 #define MAXFD 64
43
44 unsigned short proxy_port;
45 struct sockaddr_in hostaddr; /* remote host addr */
46
47 void parse_args(int argc,char **argv); /* deal with arguments */
48 void init_daemon(void); /* become daemon */
49 void do_proxy(int usersockfd); /* main proxy process */
50 void error_quit(char *msg); /* print error message for daemon */
51
52 int main(int argc,char **argv)
53 {
54 int clilen;
55 int childpid;
56 int sockfd,newsockfd;
57 struct sockaddr_in servaddr,cliaddr;
58
59 parse_args(argc,argv);
60 init_daemon();
61
62 /* prepare servaddr for listening client */
63 bzero((char*)&servaddr,sizeof(servaddr));
64 servaddr.sin_family = AF_INET;
65 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
66 servaddr.sin_port = proxy_port;
67
68 /* socket,bind,listen */
69 if((sockfd = socket(AF_INET,SOCK_STREAM,0))<0)
70 error_quit("failed to create server socket");
71 if(bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
72 error_quit("failed to bind server socket");
73 listen(sockfd,5);
74
75 while(1){
76 clilen = sizeof(cliaddr);
77 newsockfd = accept(sockfd,(struct sockaddr *)&cliaddr,&clilen);
78 if(newsockfd<0 && errno == EINTR)
79 continue;
80 else if(newsockfd < 0)
81 error_quit("failed to accept connection");
82
83 if((childpid = fork()) == 0){
84 close(sockfd);
85 do_proxy(newsockfd); /* proxy main process */
86 exit(0);
87 }
88 /* if fork failed,close connection */
89 close(newsockfd);
90 }
91 }
92

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!