Proxy source code analysis on Linux Network Programming Technology

Source: Internet
Author: User
Proxy source code analysis: Linux Network Programming Technology-Linux general technology-Linux programming and kernel information. The following is a detailed description. 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
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

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.