Proxy source code analysis -- how to learn linux network programming
Source: Internet
Author: User
Article title: Proxy Source Code Analysis-talk about how to learn linux network programming. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Linux is a very reliable operating system, but all friends who have used Linux will feel that a "dummies" operating system like Linux and Windows (it doesn't mean to degrade Windows at all, on the contrary, this should be the advantage of Windows), the latter is undoubtedly easier to operate. But why are so many fans interested in Linux? of course, freedom is the most attractive. In addition, the powerful functions of Linux are also a very important reason, in particular, the powerful network functions of Linux are even more eye-catching. Looking at today's WAP business, banking network business, and e-commerce, which were once well-known, are increasingly reliant on Linux-based solutions. Therefore, network programming in Linux is very important. when we get started with network programming in Linux, we will find this very interesting thing, in the past, some network communication concepts appeared to be different, and they were very open in front of this segment of code. At the beginning of learning programming, it was a bit confusing, but as long as we read a few more pieces of code, we will soon be able to experience the fun of it. Next I will start with a piece of Proxy source code to talk about how to program Linux network.
First, I declare that this source code is not compiled by me. let's thank this Prawn named Carl Harris for compiling this code and spreading it online for your study and discussion. Although this code only describes the simplest proxy operation, it is indeed classic. it not only clearly describes the concepts of client/server systems, it also covers almost all aspects of Linux network programming and is very suitable for beginners of Linux network programming.
The usage of this Proxy program is as follows. we can use this proxy to log on to the service port of other hosts. If an executable file named Proxy is generated after compilation, The Command and its parameters are described as follows:
./Proxy
The proxy_port parameter is the proxy server port that we specify. The remote_host parameter indicates the host name of the remote host we want to connect to. the IP address is also valid. This host name should be unique on the network. if you are not sure about it, you can run the uname-n command on the remote host to check it. The service_port parameter is the service name provided by the remote host. you can also enter the port number of the service. The corresponding operation of this command is to bind the proxy_port port of the proxy server to the service_port of remote_host. Then we can access remote_host through the proxy_port port of the proxy server. For example, if the network host name of a computer is legends and the IP address is 10.10.8.221, run the following command on my computer:
[Root @ lee/root] #./proxy 8000 legends telnet
Then we can access the telnet port of legends through the following command.
-----------------------------------------------------------------
[Root @ lee/root] # telnet legends 8000
Trying 10.10.8.221...
Connected to legends (10.10.8.221 ).
Escape character is '^]'
Red Hat Linux release 6.2 (Zoot)
Kernel 2.2.14-5.0 on an i686
Login:
-----------------------------------------------------------------
The above binding operation can also use the following command:
[Root @ lee/root] #./proxy 8000 10.10.8.221 23
23 is the standard port number of the telnet service. you can view the corresponding port number of other services in/etc/services.
Next I will start from this code to talk about some of my rough understandings of Linux network programming. if not, I would like to ask you to criticize and correct me more.
◆ Main () function
-----------------------------------------------------------------
# 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. */
Extern char * sys_myerrlist [];
Void parse_args (int argc, char ** argv );
Void daemonize (int servfd );
Void do_proxy (int usersockfd );
Void reap_status (void );
Void errorout (char * msg );
/* This is my modification.
I'll tell you why we must do this later */
Typedef void Signal (int );
/*************************************** *************************
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.
Cils: 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) <0 ){
Fputs ("failed to create server socket \ r \ n", stderr );
Exit (1 );
}
/*... And bind our address and port to it */
If (bind (sockfd, (struct sockaddr_in *) & servaddr, sizeof (servaddr) <0 ){
Fputs ("faild to bind server socket to specified port \ r \ n", stderr );
Exit (1 );
}
/* Get ready to accept with at most 5 clients waiting to connect */
Listen (sockfd, 5 );
/* Turn ourselves into a daemon */
Daemonize (sockfd );
/* Fall into a loop to accept new connections and spawn children */
While (1 ){
/* Accept the next connection */
Clilen = sizeof (cliaddr );
Newsockfd = accept (sockfd, (struct sockaddr_in *) & cliaddr, & clilen );
If (newsockfd <0 & errno = EINTR)
Continue;
/* A signal might interrupt our accept () call */
Else if (newsockfd <0)
/* Something quite amiss -- kill the server */
Errorout ("failed to accept connection ");
/* Fork a child to handle this connection */
If (childpid = fork () = 0 ){
Close (sockfd );
Do_proxy (newsockfd );
Exit (0 );
}
/* If fork () failed, the connection is silently dropped -- oops! */
Lose (newsockfd );
}
}
-----------------------------------------------------------------
The above is the main program part of the Proxy source code. you may have seen this code on the Internet, but you will find that I have modified the above code in two places, both are in the pre-compiled part. When defining an external struct pointer array
Extern char * sys_errlist [];
Change
Extern char * sys_myerrlist []; the reason is that the header file "stdio. h" in my Linux environment has defined sys_errlist [] as follows:
Extern _ const char * _ const sys_errlist [];
Maybe the system didn't define sys_errlist [] when Carl Harris wrote this code in 94 years, but if we don't modify it now, the system will tell us that sys_errlist has a definition conflict.
In addition, I added a function type definition:
Typedef void Sigfunc (int );
I will explain the specific cause to you later.
Socket and socket address structure definition
This main program is a typical server program. The most important part of network communication is the use of sockets. the socket descriptor sockfd and newsockfd are defined at the beginning of the program. Next, define the client/server socket address structure cliaddr and servaddr to store the communication information of the client/server. Then, call the parse_args (argc, argv) function to process command parameters. We will introduce this parse_args () function later.
Create communication socket
The following describes how to create a server. The first operation of the server program is to create a socket. This is achieved by calling the socket () function. The socket () function is described as follows:
-----------------------------------------------------------------
# Include
# Include
Int soc
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.