How to use Linux sockets?

  

We know that many applications, such as E-mail, Web, and instant messaging, rely on the web to do so. Each of these applications relies on a specific network protocol, but each protocol uses the same general network transport method. Many people are unaware that there are loopholes in the network protocol itself. This article will learn how to use sockets to make applications access the network and how to deal with common network vulnerabilities.

1. Sockets

Sockets are a standard way to accomplish network communication through the OS (OS). A socket can be thought of as a terminal connected to a connection, just like a socket on an operator panel. But these sockets are just abstract names for programmers, and they are responsible for all the basic details of the OSI model described in the text. For programmers, you can use a socket to send or receive data over the network. This data is transmitted at the lower layer (processed by the operating system
) at the session layer (5), which is responsible for routing. There are several different sockets that determine the structure of the transport layer. The most common types are stream sockets and datagram sockets.

Streaming sockets provide reliable two-way communication, which is similar to calling you and others. One party initiates a connection to the other party, and after establishing the connection, either party can communicate with the other party. In addition, the words you say actually get to the destination can be quickly confirmed. Stream sockets use a standard communication protocol called the Transmission Control Protocol (TCP), which exists in the transport layer (4) of the OSI model. On computer networks, data is usually transmitted in the form of large blocks of what we call packets. TCP is designed so that packets arrive at their destination in order and are error-free, just as when speaking on the phone, words arrive at the other end in the order in which they are spoken. Web servers, mail servers, and their respective client applications communicate using TCP and streaming sockets.

Another common type of socket is a datagram socket. Using datagram socket communication is more like mailing a letter instead of making a call. The connection is unidirectional and unreliable. If you send a few letters, you will not be sure that they arrive at their destination in the same order as they were sent, and even if they are delivered to their destination, there is no guarantee. Postal services are fairly reliable, but Internet is not reliable. The datagram socket uses a standard protocol called UDP instead of TCP on the transport layer (4). UDP stands for User Datagram Protocol, which means it can be used to create custom protocols. This protocol is very basic and lightweight, it only has a few built-in protections. It's not a real connection, it's just a basic way to send data from one end to the other. When using datagram sockets, the system overhead in the protocol is very small, but the protocol does not have much functionality. If the program needs to verify that the other party has received the packet, it must be programmed to have the other party send back an acknowledgement packet. In some cases, packet loss can be accepted. Datagram sockets and UDP are commonly used in online games and streaming media because developers can precisely tune their communications as needed without the inherent overhead of TCP.

2. Socket Functions

In C, sockets behave like files because they use file descriptors to identify themselves. The behavior of a socket is very similar to that of a file. In fact, using the socket file descriptor, you can receive and send data using the read() and write() functions. However, there are several functions that are specifically designed to handle sockets. There are definitions of these function prototypes in the /usr/include/sys/socket.h file.

extern int socket (int __domain, int __type, int __protocol) __THROW;

is used to create a new socket, returning a file descriptor after the socket is displayed. Return to -1 when the error occurs. Extern int connect (int __fd, __CONST_SOCKADDR_ARG __addr, socklen_t __len);

Connect a socket (specified by the file descriptor fd) to the remote host. Returns 0 successfully and the error returns -1.

Listen(int fd,int backlog_queue_size)

Listen incoming connections and queue connection requests: until the number reaches backlog_queue_size. Returns 0 successfully and the error returns -1.

extern int accept (int __fd, __SOCKADDR_ARG __addr, socklen_t *__restrict __addr_len);

An incoming connection is accepted on a bound port. The address information of the remote host is written into the remote_host structure, and the actual size of the address structure is written to addr_len. This function returns a new socket file descriptor to identify the connected socket, and the error returns -1.

extern ssize_t send (int __fd, __const void *__buf, size_t __n, int __flags);

send n bytes from *__buf to socket fd, the return value is sent The number of bytes, the error returns -1.

extern ssize_t recv (int __fd, void *__buf, size_t __n, int __flags);

Receive n bytes from socket fd into *__buf, the return value is received The number of bytes, the error returns -1.

When you create a socket using the socket() function, you must specify the domain, type, and protocol of the socket. A domain refers to a protocol family of sockets. Sockets can communicate using a variety of protocols, from standard Internet protocols used when browsing the Web to amateur radio protocols such as AX.25 (if you are a radio enthusiast). These protocol families are defined in bits/socket.h and are automatically included in sys/socket.h.

/usr/include/bits/socket.h Fragment 1

/* Protocol families. */

#define PF_UNSPEC 0 /* Unspecified. */

#define PF_LOCAL 1 /* Local to host (pipes and file-domain). */

#define PF_UNIX PF_LOCAL /* Old BSD name for PF_LOCAL. */

#define PF_FILE PF_LOCAL /* Another non-standard name for PF_LOCAL. */

#define PF_INET 2 /* IP protocol family. */

#define PF_AX25 3 /* Amateur Radio AX.25. */

#define PF_IPX 4 /* Novell Internet Protocol. */

#define PF_APPLETALK 5 /* Appletalk DDP. */

#define PF_NETROM 6 /* Amateur Radio NetROM. */

#define PF_BRIDGE 7 /* Multiprotocol bridge. */

#define PF_ATMPVC 8 ​​/* ATM PVCs. */

#define PF_X25 9 /* Reserved for X.25 project. */

#define PF_INET6 10 /* IP version 6. */

As mentioned earlier, although the stream is connected to the mold and the datagram is socketed. Words are used most often, but there are several other types of sockets. The type of socket is also defined in bits/socket.h (/*comment*/in the above code is another form of comment that will comment all content between asterisks). /usr/include/bits/socket.h Fragment 2

/* Types of sockets. */

enum __socket_type

{

SOCK_STREAM = 1 , /* Sequenced, reliable, connection-based

byte streams. */

#define SOCK_STREAM SOCK_STREAM

SOCK_DGRAM = 2, /* Connectionless, unreliable datagrams

of fixed maximum length. */

#define SOCK_DGRAM SOCK_DGRAM

Copyright © Windows knowledge All Rights Reserved