20191324 reading notes 11

Chapter 13 TCP/IP and network programming

This chapter discusses TCP/IP and network programming, which is divided into two parts. The first part discusses TCP/IP protocol and its application, including TCP/IP stack, IP address, host name, DNS, IP packet and router; This paper introduces UDP and TCP protocol, port number and data flow in TCP/IP network; The server client computing model and socket programming interface are described; Network programming is demonstrated by using examples of UDP and TCP sockets. The first programming project can implement a pair of TCP server clients that perform file operations through the Internet, allowing users to define other communication protocols to reliably transmit file content. The second part of this chapter introduces Web and CGI programming, and explains HTTP programming model, Web page and Web browser; It shows how to configure Linux HTTPD server to support user Web page, PHP and CG1 programming; The dynamic Web pages of client and server are explained; Demonstrates how to create server-side dynamic Web pages using PHP and CGI. The second programming project allows readers to realize server-side dynamic Web pages through CGI programming on Linux HTTPD server.

  • TCP/IP is the foundation of the Internet. TCP stands for transmission control protocol and IP stands for internet protocol.

  • The data transmission on or above the transport layer between the process and the host is only logical transmission. The actual data transmission occurs at the Internet (IP) and link layers, which divide data packets into data frames for transmission between physical networks. The following figure shows the data flow path in the TCP/IP network.

  • A host is a computer or device that supports the TCP/IP protocol. Each host is identified by a 32-bit IP address. For convenience, 32-bit IP address numbers are usually represented by dotted notation, in which each byte is separated by a dot number.

  • We can find another one through DNS (domain name system) server, which converts IP address to host name and vice versa.

  • The IP address is divided into two parts: NetworkID field and HostID field. According to the division, 1P addresses are divided into A~E categories. For example, a class B JP address is divided into a 16 bit networklD, where the first two bits are 10, and then a 16 bit HostID field. Packets destined for IP addresses are first sent to routers with the same networklD. The router will forward the packet to a specific host in the network through the HostID. Each host has a local host name localhost, and the default IP address is 127.0.0.1. The link layer of the local host is a loopback virtual device, which routes each packet back to the same localhost0. This feature allows us to run TCP/IP applications on the same computer without actually connecting to the Internet.

  • ip protocol is used to send / receive data packets between ip hosts. The host only sends packets to the receiving host, but it cannot guarantee that the packets will be sent to their destination or sent in sequence. This means that ip is not a reliable protocol. If necessary, reliability must be realized above the ip layer.

  • ip packet consists of ip header, sender ip address, receiver ip address and data. The maximum size of each ip packet is 64KB. The 1P header contains more information about the packet, such as the total length of the packet, whether the packet uses TCP or UDP, time to live (TTL) count, checksum of error detection, etc.

  • UDP runs over IP and is used to send / receive datagrams. Similar to IP, UDP can not guarantee reliability, but it is fast and efficient. It can be used when reliability is not important.

  • ping is an application that sends time stamped UDP packets to the target host. After receiving a ping packet, the target host sends the UDP packet with time stamp back to the sender, so that the sender can calculate and display the round-trip time. If the target host does not exist or goes down, the router will discard the pinging UDP packet when the TTL is reduced to 0. In this case, the user will find that the target host does not respond. The user can try pinging again or conclude that the target host is down. In this case, it is best to use UDP because reliability is not required.

  • TCP (transmission control protocol) is a connection oriented protocol used to send / receive data streams. TCP can also run over IP, but it ensures reliable data transmission.

  • On each host, multiple applications (processes) can use TCP/UDP at the same time. Each application is uniquely identified by three components: application = (host IP, protocol, port number), where the protocol is TCP or UDP, and the port number is the only unsigned short integer assigned to the application. To use UDP or TCP, an application (process) must first select or obtain a port number. The first 1024 port numbers have been reserved. Other port numbers are available for general use. The application can select an available port number or let the operating system kernel assign a port number. Common port number:

Common port number of HTTP protocol proxy server: 80 / 8080 / 3128 / 8081 / 9098
SOCKS proxy protocol server common port number: 1080
FTP (file transfer) protocol proxy server common port number: 21
Telnet protocol proxy server common port number: 23
HTTP server, the default port number is 80/tcp (Trojan Executor opens this port)
HTTPS (secure transferring web pages) server. The default port number is 443/tcp 443/udp
Telnet (insecure text transfer), the default port number is 23/tcp (the port opened by the Trojan Tiny Telnet Server)
FTP, the default port number is 21/tcp (the ports opened by Doly Trojan, Fore, Invisible FTP, WebEx, WinCrash and Blade Runner)
TFTP (triple file transfer protocol), the default port number is 69/udp
SSH (secure login), SCP (file transfer), port number redirection. The default port number is 22/tcp
SMTP Simple Mail Transfer Protocol (E-mail), the default port number is 25/tcp (this port is open for Trojan horse Antigen, Email Password Sender, Haebu Coceda, shtrilitz wealth, WinPC and WinSpy)
POP3 Post Office Protocol (E-mail). The default port number is 110/tcp
Webshpere application, the default port number is 9080
webhpere management tool, default port number 9090
JBOSS, the default port number is 8080
TOMCAT, the default port number is 8080
WIN2003 remote login, the default port number is 3389
Symantec AV/Filter for MSE, the default port number is 8081
Oracle database, the default port number is 1521
ORACLE EMCTL, the default port number is 1158
Oracle XDB (XML database), the default port number is 8080
Oracle XDB FTP service. The default port number is 2100
MS SQLSERVER database server. The default port number is 1433/tcp 1433/udp
MS SQLSERVER database monitor. The default port number is 1434/tcp 1434/udp

Data flow in TCP/IP network

  • The data of the application layer is passed to the transport layer, which adds a TCP or UDP header to the data to identify the transport protocol used. The combined data is transferred to the IP network layer, and an IP header containing IP address is added to identify the sending and receiving hosts. Then, the combined data is transmitted to the network cone layer. The network link layer divides the data into multiple frames and adds the addresses of the sending and receiving networks for transmission between physical networks: the mapping from IP address to network address is performed by the address resolution protocol (ARP 1982). At the receiving end, the data encoding process is the opposite. Each layer unpacks the received data by stripping the data header, reassembles the data and passes the data to the upper layer. The original data of the application on the sending host will eventually be transferred to the corresponding application on the receiving host.

Socket programming

  • In network programming, TCP/IP user interface is implemented through a series of C language library functions and system calls, which are collectively referred to as socket API (rago 1993; Stevens et al., 2004) . in order to use socket API, we need socket address structure, which is used to identify server and client. netdb.h and sys/socket.h have the definition of socket address structure:
struct sockaddr_in ( sa_family_t sin_family; in_port_t sin_port; struct in_addr sin_addr;
);
struct in_addr {
uint32_t s_addr;

  • In the socket address structure, the sin_family of the TCP/IP network is always set to AF_INET,sm_port contains the port number in the order of network bytes, and sin_addr is the host IP address in the order of network bytes.
  • The server must create a socket and bind it to the socket address containing the server IP address and port number. It can use a fixed port number or let the operating system kernel choose a port number (if sin_port is 0)o in order to communicate with the server, the client must create ~ sockets. For UPD sockets, the socket can be bound to the server address. If the socket is not bound to any specific server, it must be in the subsequent sendto() /The recvfromo call provides a socket address containing the server IP and port number. The socket is given below. The system call creates a socket and returns a file descriptor.
  • UDP sockets use scndto()/recvfrom() to send / receive datagrams.
aendto(int aockfdr const void *bufr size.t len, lot flags,
const struct sockaddr •de8t_addrf socklen_t addrlen)|
asize_t recvfrora(int sock£d, void *buf, aiza_t len, int flags, struct sockaddr *Btc_addr, aocklen_t *addrlen};
  • After creating a socket and binding it to the server address, the TCP server uses listen() and acccpt() to receive the connection int iistcn (int sockfd, int backlog) from the client; listen() marks the socket referenced by sockfd as the socket that will be used to receive the incoming connection. The backlog parameter defines the maximum queue length waiting for the connection.
  • After the connection is established, both TCP hosts can use send()/write() to send data and recv()/read to receive data. The only difference between them is that the nag parameter in send and recv() is different, which can usually be set to 0.
ssize_t send(int Bockfd, const void *bufr size.t len« int flags);
write(sockfd/ void *buf, aize_t, l«n)
S0izo_t recv(int sockfd, void *buf# size_t len, int flags);
ssize_t read(sockfd, void *buf, size_t len);
  • The library function gethostname(char *name, sizeof(name)) returns the host name string of the computer in the name array, but it may not be the full formal name expressed in dotted notation or its IP address. The library function struct host * gethostbyname (void * addr, socklen_t len, int typeo) It can be used to obtain the full name and IP address of the computer. It will return a pointer to the host structure in < netdb.h >:
struct hostent {
char	*h_name;	
char	**h_aliases;	
int	h_addrtype;	
int	h_length;	
char	**h_addr_list;	
}
#define h_addr h_addr_list[0]
  • The following code snippet shows how to use gethostbyname() and getsockname() to get the server 1P address and port number (if dynamically assigned). The server must publish its host name or IP address and port number for clients to connect.
char myname[64];
struct sockaddr_in server_addr, sock_addr;
// gethostname(), gethostbyname() gethostname(myname,64);
struct hostent *hp = gethostbyname(myname); if (hp == 0)(
printf("unknown host %s\n", myname); exit(1);
// initialize the server_addr structure server_addr.sin_family = AF.INET;	// for TCP/IP
server_addr.Bin_addr・ s_addr = *(long *)hp->h_addr; server_addr.sin_port = 0; // let kernel assign port number
// create a TCP socket
int mysock = socket(AF_INET, SOCK_STREAM, 0);
bind socket with server_addr bind(mysock,(struct
to show port number assigned by kernel
(struct sockaddr *)&name_addr, &length)/
// show server host name and port number
printf("hostname=%s IP=%s port=%d\n", hp->h_name,
inet_ntoa(*(long *)hp->h_addr), ntohs(name_addr.sin_port));
struct sockaddr_in server_addr, sock_addr;
// 1. get server IP by name
struct hostent *hp = gethostbyname(argv[l]);
SERVER_IP = *(long *)hp->h_addr;
SERVER_PORT = atoi(argv[2]);
// 2. create TCP socket
int sock = socket(AF_INET, SOCK_STREAM, 0);
// 3. fill server_addr with server IP and PORT#
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = SERVER_IP;
server_addr.sin_port = htons(SERVER_PORT);
// 4. connect to server
connect(sock,(struct sockaddr *)&server_addr, sizeof(server_addr));
  • On the command line, depending on the command, the pathname can be a file or directory. Valid commands are:

mkdir: create a directory with a pathname.
rmdir: delete the directory named pathname.
rm: delete the file named pathname.
cd: change the current working directory (CWD) to the pathname.
pwd: displays the absolute pathname of the current working directory.
Is: lists the current working directory or pathname in the same format as Is -1 in Linux.
get: download the pathname file from the server.
put: upload the pathname file to the server.

  • For the get/put command that transfers the contents of a file, you cannot use special ASCII characters as the start and end marks of the file. This is because binary files may contain ASCII code.
  • Web pages written in standard HTML are static. When obtained from the server and displayed in the browser, the content of the web page will not change. To display web pages with different content, you must obtain different web page files from the server again.
  • There are two kinds of dynamic Web pages, namely client-side dynamic Web pages and server-side dynamic Web pages. The client-side dynamic Web page file contains code written by JavaScript, which is executed by the JavaScript interpreter on the client. It can locally modify the Web page in response to user input, time events, etc. without any interaction with the server. Server-side dynamic Web pages are real dynamic pages because they are dynamically generated according to user input in URL requests. The core of server-side dynamic Web pages lies in the ability of the server to execute PHP code in HTML files or CGI programs to generate HTML files through user input.
  • CGI stands for the general gateway interface. It is a protocol that allows the Web server to execute programs and dynamically generate Web pages according to user input. With CGI, the Web server does not have to maintain millions of static Web page files to meet client requests. On the contrary, it meets client requests by dynamically generating Web pages.

practice

#include <stdio.h>
#include <arpa/inet.h>//inet_addr() sockaddr_in
#include <string.h>//bzero()
#include <sys/socket.h>//socket
#include <unistd.h>
#include <stdlib.h>//exit()

#define BUFFER_SIZE 1024

int main() {

    char listen_addr_str[] = "0.0.0.0";
    size_t listen_addr = inet_addr(listen_addr_str);
    int port = 8080;
    int server_socket, client_socket;
    struct sockaddr_in server_addr, client_addr;
    socklen_t addr_size;
    char buffer[BUFFER_SIZE];//Buffer size
    int str_length;
    server_socket = socket(PF_INET, SOCK_STREAM, 0);//Create socket
    bzero(&server_addr, sizeof(server_addr));//initialization
    server_addr.sin_family = INADDR_ANY;
    server_addr.sin_port = htons(port);
    server_addr.sin_addr.s_addr = listen_addr;
    if (bind(server_socket, (struct sockaddr *) &server_addr, sizeof(server_addr)) == -1) {
        printf("Binding failed\n");

        exit(1);

    }

    if (listen(server_socket, 5) == -1) {

        printf("Listening failed\n");

        exit(1);

    }

    printf("establish tcp Server success\n");

 

 

    fd_set reads,copy_reads;

    int fd_max,fd_num;

    struct timeval timeout;

 

    FD_ZERO(&reads);//Initialize and empty socket collection

    FD_SET(server_socket,&reads);

    fd_max=server_socket;

 

 

    while (1) {

        copy_reads = reads;

        timeout.tv_sec = 5;

        timeout.tv_usec = 5000;

 

        //Infinite loop call select to monitor readable events

        if((fd_num = select(fd_max+1, &copy_reads, 0, 0, &timeout)) == -1) {

            perror("select error");

            break;

        }

        if (fd_num==0){//socket without change

            continue;

        }

 

        for(int i=0;i<fd_max+1;i++){

            if(FD_ISSET(i,&copy_reads)){

                if (i==server_socket){//The change of server_socket indicates that there is a new client connection

                    addr_size = sizeof(client_addr);

                    client_socket = accept(server_socket, (struct sockaddr *) &client_addr, &addr_size);

                    printf("%d Connection succeeded\n", client_socket);

                    char msg[] = "Congratulations on your successful connection";

                    write(client_socket, msg, sizeof(msg));

                    FD_SET(client_socket,&reads);

                    if(fd_max < client_socket){

                        fd_max=client_socket;

                    }

                }else{

                    memset(buffer, 0, sizeof(buffer));

                    str_length = read(i, buffer, BUFFER_SIZE);

                    if (str_length == 0)    //Close socket after reading data

                    {

                        close(i);

                        printf("The connection has been closed: %d \n", i);

                        FD_CLR(i, &reads);//Delete relevant information from reads

                    } else {

                        printf("%d Client sends data:%s \n", i, buffer);

                        write(i, buffer, str_length);//Send data back to client

                    }

                }

            }

        }

 

    }

 

    return 0;

}

Posted on Tue, 23 Nov 2021 09:36:21 -0500 by matt72