[LINUX] Network programming (basic)

Caution: It is a personal memo. We do not guarantee that the content is correct. Also, if you do not have knowledge of the usage system or C language, please do not imitate it. It is dangerous. (While editing)

Basics

Network programming

Interprocess communication may occur on the same computer, such as a pipe (pipe (2)) or FIFO (named pipe). In principle, this interprocess communication is performed on different computers and networks to which you do not belong. It means what is done according to the communication agreement.

Attention to architecture

Structure alignment, byte order, and how the address bus points to addresses Bus width etc. differ depending on the architecture used. C language entrusts C language users to know the difference. Be careful not to rely on the computer when communicating with different computers on the network. For example, on RISC-type processors, accessing odd-numbered addresses can result in a bus error.

System call

In network programming, an application is created using a system call (uapi). In user applications, the kernel operates in user mode and uses virtual memory. The operating system handles the network modules directly. This is done in kernel mode (privileged mode).

Network applications utilize an interface called sockets. Sockets can be treated as special files. Issue sokect (2) to create a socket.

Sockets are still files. Therefore, binary I / O functions such as read (2) and write (2) can also be used.

The created socket can be addressed by bind (2). Also, since sockets are a type of file, they are managed by the inode number.

The network module containing the socket has a buffer to store the data Queue the packet. To request the operating system to send data Issue send (2) in the sending application. (It is a request, not a guarantee) If there is free space in the buffer, it will be queued.

Data can be obtained by issuing recv (2) in the receiving application. Request data retrieval from the buffer. The program is blocked if the received data does not exist in the queue. Note that recv (2) is fetching data from the buffer even if the application is not running. Note that if it does not end normally, data will be retrieved indefinitely.

Interrupts are used by the NIC to send and receive data. This is taken care of by the kernel, In C language, abstract signals are prepared for users.

Redundancy

When there are requests from multiple clients to a network application The application may exceed the processing power. In this case, multi-process, multi-thread, select (2), etc. are used.

Dealing with errors

connect (2) is used to identify the communication partner at the application level. If the data transmission / reception process is not completed normally while the connection is established, whether it is TCP or UDP. If left untouched, processing may be blocked forever. If it is bad, even key input will not be accepted.

Therefore, time-out occurs in alerm (2), setitimer (2), etc. Appropriate signal handling with sigaction (2) etc.

Protocol header

Many protocols are represented by C language structures. Many of these details can be found in the following directories:

/usr/include/net/* /usr/include/netinet/*

TCP/IP

DGRAM type communication and STREAM type communication

UNIX domain and INET domain

data

In TCP / IP, communication is performed by packet switching.

A packet consists of a header and a payload.

header

The header contains information about the destination address, source address, payload, and so on.

payload

The payload is the data itself that you want to communicate with

message

An application for communication will be created on the OS, and the data exchanged between these applications is called a message.

Data type

The following data types in the header guarantee a bitwise size. This is also provided by POSIX-compliant OS.

/usr/include/stdint.h

<stdint.h> <inttypes.h> (C99)

Example:


uint32_t /*Unsigned 32-bit int type*/

How it is defined depends on the system. See the header directly for details.

Network byte order

TCP / IP protocol protocol headers on the network Unify application messages to big endian. The following functions can be used for endian conversion.


#include <arpa/inet.h> /*Depending on the system<net/inet.h> */

uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);


#include <stdio.h>
#include <arpa/inet.h>

int main(void)
{
    uint32_t s = 0xff;

    /* host to network endian */
    uint32_t r = htonl(s);

    printf("%#0.8x\n", r);
    printf("%#0.8x\n", s);
    return 0;
}

Result example


0xff000000
0x000000ff

IP address and domain name resolution

1 . Use / etc / hosts file


#include <netdb.h>
struct hostent *gethostbyname(const char *name);

#include <sys/socket.h>
struct hostent *gethostbyaddr(const void *addr, socklen_t len, int type);

2 . Use DNS


#include <sys/socket.h>
void sethostent(int stayopen);
void endhostent(void);

When resolving the name with the DNS server, register the address to the DNS server in /etc/resolv.conf in advance.

Protocol entry

/etc/protocols

#include <netdb.h>

struct protoent *getprotoent(void);
struct protoent *getprotobyname(const char *name);
struct protoent *getprotobynumber(int proto);

Service entry

/etc/services


#include <netdb.h>

struct servent *getservent(void);
struct servent *getservbyname(const char *name, const char *proto);
struct servent *getservbyport(int port, const char *proto);

Recommended Posts

Network programming (basic)
CentOS 7 basic settings after network settings
network
Network programming with Python Scapy
Numpy [Basic]
Basic writing of various programming languages (self-memo)
Poop programming
Basic commands
Basic level performance evaluation of programming languages
Graphic programming
Relational Network
Shell programming
Programming beginner Python3 engineer certification basic exam record