濂楁帴瀛椾箣sendmsg绯荤粺璋冪敤

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了濂楁帴瀛椾箣sendmsg绯荤粺璋冪敤相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/mat' title='mat'>mat   __user   soc   amp   bsp   div   pointer   娑堟伅   The   

sendmsg绯荤粺璋冪敤鍏佽鍦ㄧ敤鎴风┖闂存瀯閫犳秷鎭ご鍜屾帶鍒朵俊鎭紝鐢ㄦ鍑芥暟鍙互鍙戦€佸涓暟鎹紦鍐插尯鐨勬暟鎹紝骞舵敮鎸佹帶鍒朵俊鎭紱褰撹皟鐢ㄨ繘鍏ュ唴鏍稿悗锛屼細灏嗙敤鎴风鐨剈ser_msghdr瀵瑰簲鎷疯礉鍒板唴鏍哥殑msghdr涓紝鐒跺悗杩涜鏁版嵁鍙戦€侊紱

1 SYSCALL_DEFINE3(sendmsg, int, fd, struct user_msghdr __user *, msg, unsigned int, flags)
2 {
3     /* 涓嶆敮鎸?4浣嶉噰鐢?2浣嶅吋瀹规爣璁?*/
4     if (flags & MSG_CMSG_COMPAT)
5         return -EINVAL;
6 
7     /* 璋冪敤__sys_sendmsg */
8     return __sys_sendmsg(fd, msg, flags);
9 }

 

 1 /*
 2  *    BSD sendmsg interface
 3  */
 4 
 5 long __sys_sendmsg(int fd, struct user_msghdr __user *msg, unsigned flags)
 6 {
 7     int fput_needed, err;
 8     struct msghdr msg_sys;
 9     struct socket *sock;
10 
11     /* 鏌ユ壘socket */
12     sock = sockfd_lookup_light(fd, &err, &fput_needed);
13     if (!sock)
14         goto out;
15 
16     /* 鍙戦€佹暟鎹?*/
17     err = ___sys_sendmsg(sock, msg, &msg_sys, flags, NULL, 0);
18 
19     fput_light(sock->file, fput_needed);
20 out:
21     return err;
22 }

 

  1 static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
  2              struct msghdr *msg_sys, unsigned int flags,
  3              struct used_address *used_address,
  4              unsigned int allowed_msghdr_flags)
  5 {
  6     struct compat_msghdr __user *msg_compat =
  7         (struct compat_msghdr __user *)msg;
  8     struct sockaddr_storage address;
  9     struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
 10     unsigned char ctl[sizeof(struct cmsghdr) + 20]
 11                 __aligned(sizeof(__kernel_size_t));
 12     /* 20 is size of ipv6_pktinfo */
 13     unsigned char *ctl_buf = ctl;
 14     int ctl_len;
 15     ssize_t err;
 16 
 17     msg_sys->msg_name = &address;
 18 
 19     /* 闇€瑕佸仛64浣嶉噰鐢?2浣嶅吋瀹?*/
 20     if (MSG_CMSG_COMPAT & flags)
 21         /* 浠?4浣嶆秷鎭ご鎷疯礉鏁版嵁鍒?2浣嶆秷鎭ご */
 22         err = get_compat_msghdr(msg_sys, msg_compat, NULL, &iov);
 23     else
 24         /* 浠庣敤鎴风┖闂存嫹璐濇秷鎭暟鎹埌娑堟伅澶?*/
 25         err = copy_msghdr_from_user(msg_sys, msg, NULL, &iov);
 26     if (err < 0)
 27         return err;
 28 
 29     err = -ENOBUFS;
 30 
 31     /* 鎺у埗淇℃伅闀垮害閿欒 */
 32     if (msg_sys->msg_controllen > INT_MAX)
 33         goto out_freeiov;
 34 
 35     /* 鎷疯礉鎺у埗淇℃伅 */
 36     flags |= (msg_sys->msg_flags & allowed_msghdr_flags);
 37     ctl_len = msg_sys->msg_controllen;
 38     if ((MSG_CMSG_COMPAT & flags) && ctl_len) {
 39         err =
 40             cmsghdr_from_user_compat_to_kern(msg_sys, sock->sk, ctl,
 41                              sizeof(ctl));
 42         if (err)
 43             goto out_freeiov;
 44         ctl_buf = msg_sys->msg_control;
 45         ctl_len = msg_sys->msg_controllen;
 46     } else if (ctl_len) {
 47         BUILD_BUG_ON(sizeof(struct cmsghdr) !=
 48                  CMSG_ALIGN(sizeof(struct cmsghdr)));
 49         if (ctl_len > sizeof(ctl)) {
 50             ctl_buf = sock_kmalloc(sock->sk, ctl_len, GFP_KERNEL);
 51             if (ctl_buf == NULL)
 52                 goto out_freeiov;
 53         }
 54         err = -EFAULT;
 55         /*
 56          * Careful! Before this, msg_sys->msg_control contains a user pointer.
 57          * Afterwards, it will be a kernel pointer. Thus the compiler-assisted
 58          * checking falls down on this.
 59          */
 60         if (copy_from_user(ctl_buf,
 61                    (void __user __force *)msg_sys->msg_control,
 62                    ctl_len))
 63             goto out_freectl;
 64         msg_sys->msg_control = ctl_buf;
 65     }
 66 
 67     /* 璁剧疆鍙戦€佹爣璁?*/
 68     msg_sys->msg_flags = flags;
 69 
 70     /* 璁剧疆闈為樆濉炴爣璁?*/
 71     if (sock->file->f_flags & O_NONBLOCK)
 72         msg_sys->msg_flags |= MSG_DONTWAIT;
 73     /*
 74      * If this is sendmmsg() and current destination address is same as
 75      * previously succeeded address, omit asking LSM鈥榮 decision.
 76      * used_address->name_len is initialized to UINT_MAX so that the first
 77      * destination address never matches.
 78      */
 79     /* 濡傛灉杩欐鍙戦€佺殑鍦板潃璺熶笂娆℃垚鍔熷彂閫佺殑涓€鑷?*/
 80     if (used_address && msg_sys->msg_name &&
 81         used_address->name_len == msg_sys->msg_namelen &&
 82         !memcmp(&used_address->name, msg_sys->msg_name,
 83             used_address->name_len)) {
 84         /* 鏃犻渶杩涜妫€鏌ワ紝鐩存帴鍙戦€?*/
 85         err = sock_sendmsg_nosec(sock, msg_sys);
 86         goto out_freectl;
 87     }
 88 
 89     /* 杩涜瀹夊叏妯″潡妫€鏌ュ悗鍙戦€?*/
 90     err = sock_sendmsg(sock, msg_sys);
 91     /*
 92      * If this is sendmmsg() and sending to current destination address was
 93      * successful, remember it.
 94      */
 95 
 96     /* 鍙戦€佹垚鍔熼渶瑕佹洿鏂版垚鍔熷湴鍧€璁板綍 */
 97     if (used_address && err >= 0) {
 98         used_address->name_len = msg_sys->msg_namelen;
 99         if (msg_sys->msg_name)
100             memcpy(&used_address->name, msg_sys->msg_name,
101                    used_address->name_len);
102     }
103 
104 out_freectl:
105     if (ctl_buf != ctl)
106         sock_kfree_s(sock->sk, ctl_buf, ctl_len);
107 out_freeiov:
108     kfree(iov);
109     return err;
110 }

 

TCP灞傜殑sendmsg瀹炵幇涓簍cp_sendmsg锛岃鎯呰绉绘<TCP灞俿endmsg绯荤粺璋冪敤鐨勫疄鐜板垎鏋?gt;锛?/p>

以上是关于濂楁帴瀛椾箣sendmsg绯荤粺璋冪敤的主要内容,如果未能解决你的问题,请参考以下文章

Linux鍔ㄦ€佷负鍐呮牳娣诲姞鏂扮殑绯荤粺璋冪敤

缃戠粶缂栫▼ 濂楁帴瀛梥ocket 鍙? 绮樺寘

鑷範瀹ょ鐞嗙郴缁燂紝鍩轰簬B/S妯″紡涓嬬殑JAVA绯荤粺

Android ADB Server启动失败

OC璋冪敤Swift

Python 妯″潡璋冪敤