Seventh Chapter: 7.1 Socket interprocess communication-system call socket

Source: Internet
Author: User
Research against the 4.8.6 code of the Linux kernel. In the history of 1:socket interprocess communication Unix, At&t's Bell Labs and the Berkeley Software Center (BSD) made significant contributions. At&t implementation of SysV IPC, BSD to the socket implementation. 2: System call socket like SYSV IPC, socket has a total entrance, sys_socketcall (); Socket implementation of the core code in the kernel directory for NET/SOCKET.C/*
* System call vectors.
*
* Argument checking cleaned up. Saved 20% in size.
* This function doesn ' t need to set the kernel lock because
* It is set by the callees. */
#define AL (x) ((x) * sizeof (unsigned long)//For subsequent settings get data size static const unsigned char nargs[21] = {
Al (0), AL (3), AL (3), AL (3), AL (2), AL (3),
Al (3), AL (3), AL (4), AL (4), AL (4), AL (6),
Al (6), AL (2), AL (5), AL (5), AL (3), AL (3),
Al (4), AL (5), AL (4)}; SYSCALL_DEFINE2 macro Definition converts incoming first parameter to sys_# #socketcall gets the corresponding calling interfaceSyscall_define2 (socketcall, int, call, unsigned long __user *, args) {
unsigned long A[auditsc_args];
Unsigned long a0, A1;
int err;
unsigned int len;

if (Call < 1 | |-call > SYS_SENDMMSG)
Return-einval;
len = Nargs[call]; Number of arguments multiplied by unsignd long if (len > sizeof (a))
Return-einval;

/* Copy_from_user should be SMP safe.           */if (Copy_from_user (A, args, Len))//Put all the parameters that need to be passed into the args, the parameter number is unrestricted, distribution is based on the specific port, take out different parameters. Return-efault;
Err = Audit_socketcall (Nargs[call]/sizeof (unsigned long), a); if (ERR)
return err;

a0 = a[0];   a1 = a[1]; Remove a parameter from a to facilitate later operation
Distribution comparison based on call value for example: #define SYS_SOCKET 1/* Sys_socket (2)//parameter is passed to the interface and converted according to the interface data type. Such an architecture is also a good idea when we write applications with uncertain parameters and distribute them. The kernel code can be used not only to learn the kernel operation principle, but also to learn how his code is implemented, and how it is structured. It's good for your code writing. switch (call) {
Case Sys_socket:
Err = Sys_socket (a0, A1, A[2]);
Break
Case Sys_bind:
Err = Sys_bind (a0, (struct sockaddr __user *) a1, a[2]);
Break
Case Sys_connect:
Err = Sys_connect (a0, (struct sockaddr __user *) a1, a[2]);
Break
Case Sys_listen:
Err = Sys_listen (a0, A1);
Break
Case SYS_ACCEPT:
Err = Sys_accept4 (a0, struct sockaddr __user *) A1,
(int __user *) a[2], 0);
Break
Case Sys_getsockname:
Err =
Sys_getsockname (a0, struct sockaddr __user *) A1,
(int __user *) a[2]);
Break
Case Sys_getpeername:
Err =
Sys_getpeername (a0, struct sockaddr __user *) A1,
(int __user *) a[2]);
Break
Case Sys_socketpair:
Err = Sys_socketpair (a0, A1, A[2], (int __user *) a[3]);
Break
Case Sys_send:
Err = Sys_send (a0, (void __user *) a1, A[2], a[3]);
Break
Case SYS_SENDTO:
Err = Sys_sendto (a0, (void __user *) a1, A[2], a[3],
(struct sockaddr __user *) a[4], a[5]);
Break
Case SYS_RECV:
Err = Sys_recv (a0, (void __user *) a1, A[2], a[3]);
Break
Case Sys_recvfrom:
Err = Sys_recvfrom (a0, (void __user *) a1, A[2], a[3],
(struct sockaddr __user *) a[4],
(int __user *) a[5]);
Break
Case Sys_shutdown:
Err = Sys_shutdown (a0, A1);
Break
Case SYS_SETSOCKOPT:
Err = sys_setsockopt (a0, A1, A[2], (char __user *) a[3], a[4]);
Break
Case SYS_GETSOCKOPT:
Err =
Sys_getsockopt (A0, A1, A[2], (char __user *) a[3],
(int __user *) a[4]);
Break
Case SYS_SENDMSG:
Err = sys_sendmsg (a0, (struct USER_MSGHDR __user *) a1, a[2]);
Break
Case SYS_SENDMMSG:
Err = sys_sendmmsg (a0, (struct MMSGHDR __user *) a1, A[2], a[3]);
Break
Case SYS_RECVMSG:
Err = sys_recvmsg (a0, (struct USER_MSGHDR __user *) a1, a[2]);
Break
Case SYS_RECVMMSG:
Err = sys_recvmmsg (a0, struct MMSGHDR __user *) a1, A[2], a[3],
(struct timespec __user *) a[4]);
Break
Case SYS_ACCEPT4:
Err = Sys_accept4 (a0, struct sockaddr __user *) A1,
(int __user *) a[2], a[3]);
Break
Default
err =-einval;
Break
}
return err;  The implementation of the 2.1 Sys_socket int socket (int domain,int type, int protocol); After the general first two parameters are determined, the function flow is determined, except for some special cases, the general setting of protocol is 0. The function scope is a positive integer, the file descriptor, which is associated with a data structure that represents the socket of the file, and is not associated with a file on disk. Following this file descriptor, you can find the associated data interface for some action.
Sys_bind implementation of the socket is not enough after the creation, only a file number, but also need an address, with the file number associated. For example, when you buy a mobile phone, you have to buy a phone number to communicate with the outside world. int bind (int sockfd, struct sockaddr *myaddr, socklen_t len);
Sys_listen implementation If you want to establish the status of the server, you must use the Listen interface, you can listen to all the other sockets to their own connection to the client to respond. int listen (int sockfd, int backlog);//Allow maximum connection queue.
The Sys_connect implementation Clent sends a connection request to the server. int connect (int sockfd, struct sockaddr *serv_addr, addrlen_t len);
The SYS_ACCEPT implementation server accepts the client's request. int accept (int sockfd, struct sockaddr *addr, socklen_t *len); The point here is to return a new FD, which is equivalent to opening a service door and the client of this address, which is treated by every client. A new service communicates with it.
Data transfer interface between C/s: Connected: Read (), write (), and sendto send recv recvfrom connectionless: SendTo send recv recvfrom. Because of the unreliable data transmission without connection, the order is uncertain and is not suitable for read and write.





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.