{"id":4131,"date":"2012-08-18T14:36:38","date_gmt":"2012-08-18T06:36:38","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=4131"},"modified":"2012-10-26T09:40:42","modified_gmt":"2012-10-26T01:40:42","slug":"netlink%e7%bc%96%e7%a8%8b-%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/4131","title":{"rendered":"Netlink\u7f16\u7a0b-\u6570\u636e\u7ed3\u6784"},"content":{"rendered":"<p>\u5185\u6838\u6001\u4e0e\u7528\u6237\u6001\u8fdb\u884c\u6570\u636e\u4ea4\u4e92\u7684\u65b9\u6cd5\u5f88\u591a\uff0c\u5206\u4e3a\u7528\u6237\u7a0b\u5e8f\u4e3b\u52a8\u53d1\u8d77\u7684\u6d88\u606f\u4ea4\u4ea4\u4e92\u548c\u5185\u6838\u4e3b\u52a8\u53d1\u8d77\u7684\u4fe1\u606f\u4ea4\u4e92\u3002\u901a\u5e38\u6211\u4eec\u6240\u719f\u77e5\u7684\u7cfb\u7edf\u8c03\u7528\u3001\u5bf9\/proc\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u3001mmap\u548c\u7f16\u5199\u9a71\u52a8\u7a0b\u5e8f\u7b49\u90fd\u5c5e\u4e8e\u524d\u8005\uff0c\u800c\u7531\u5185\u6838\u4e3b\u52a8\u53d1\u8d77\u7684\u4fe1\u606f\u4ea4\u4e92\u65b9\u6cd5\u6bd4\u8f83\u5c11\uff0c\u6700\u5178\u578b\u7684\u65b9\u6cd5\u4e3a\u5185\u6838\u53d1\u9001\u4fe1\u53f7\u7ed9\u5f53\u524d\u8fdb\u7a0b\u3002\u4e0a\u8ff0\u8fd9\u4e9b\u65b9\u6cd5\u90fd\u662f\u5355\u5411\u901a\u4fe1\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\u8981\u4e48\u662f\u7528\u6237\u6001\u8fdb\u7a0b\u4e3b\u52a8\u53d1\u8d77\u6570\u636e\u4ea4\u4e92\u4f1a\u8bdd\uff0c\u8981\u4e48\u662f\u5185\u6838\u4e3b\u52a8\u53d1\u8d77\u4f1a\u8bdd\u3002\u6709\u6ca1\u6709\u4e00\u79cd\u53ef\u4ee5\u5728\u7528\u6237\u6001\u548c\u5185\u6838\u6001\u8fdb\u884c\u53cc\u5411\u6570\u636e\u4ea4\u4e92\u7684\u65b9\u6cd5\u5462\uff1f<\/p>\n<p>Netlink\u662f\u4e00\u79cd\u5728\u5185\u6838\u6001\u548c\u7528\u6237\u6001\u53ef\u4ee5\u8fdb\u884c\u53cc\u5411\u6570\u636e\u4ea4\u4e92\u7684\u901a\u4fe1\u673a\u5236\u3002\u5728\u7528\u6237\u6001\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06netlink\u770b\u4f5c\u662f\u4e00\u79cd\u7279\u6b8a\u7684socket\uff0c\u56e0\u6b64\u901a\u8fc7socket\u63a5\u53e3\u6765\u5c31\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528netlink\uff1b\u5728\u5185\u6838\u6001\uff0c\u5219\u9700\u8981\u901a\u8fc7\u4e00\u7ec4\u7279\u6b8a\u7684\u5185\u6838\u63a5\u53e3\u7f16\u5199\u5185\u6838\u6a21\u5757\u3002<\/p>\n<p>\u5982\u679c\u521d\u6b21\u7f16\u5199netlink\u7a0b\u5e8f\uff0c\u53ef\u80fd\u4f1a\u5bf9\u5b83\u6240\u6d89\u53ca\u7684\u6570\u636e\u7ed3\u6784\u611f\u5230\u56f0\u60d1\uff0c\u672c\u6587\u5c06\u7b80\u5355\u4ecb\u7ecd\u4e00\u4e0bnetlink\u7f16\u7a0b\u4e2d\u9047\u5230\u7684\u57fa\u672c\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<p><strong>struct msghdr<\/strong><\/p>\n<p>\u5982\u679c\u4f7f\u7528sendmsg()\u53d1\u9001\u6570\u636e\uff0c\u90a3\u4e48\u5fc5\u987b\u4f7f\u7528msghdr\u7ed3\u6784\uff0c\u8be5\u7ed3\u6784\u5185\u90e8\u5c01\u88c5\u4e86\u672c\u6b21\u53d1\u9001\u6570\u636e\u7684\u4e00\u4e9b\u53c2\u6570\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n           struct msghdr {\r\n               void         *msg_name;       \/* optional address *\/\r\n               socklen_t     msg_namelen;    \/* size of address *\/\r\n               struct iovec *msg_iov;        \/* scatter\/gather array *\/\r\n               size_t        msg_iovlen;     \/* # elements in msg_iov *\/\r\n               void         *msg_control;    \/* ancillary data, see below *\/\r\n               size_t        msg_controllen; \/* ancillary data buffer len *\/\r\n               int           msg_flags;      \/* flags on received message *\/\r\n           };\r\n<\/pre>\n<p>netlink\u63d0\u4f9b\u7684\u662f\u4e00\u79cd\u57fa\u4e8e\u6570\u636e\u62a5\u7684\u901a\u4fe1\u670d\u52a1\uff0c\u56e0\u6b64\u4e0eUDP\u901a\u4fe1\u534f\u8bae\u7c7b\u4f3c\uff0c\u5fc5\u987b\u901a\u8fc7msg_name\u6307\u660e\u76ee\u7684\u5957\u63a5\u5b57\u5730\u5740\u7ed3\u6784\uff0c\u800cmsg_namelen\u5219\u6307\u660e\u8be5\u5730\u5740\u7ed3\u6784\u7684\u957f\u5ea6\u3002msg_iov\u6307\u5b9a\u6570\u636e\u7f13\u51b2\u533a\u6570\u7ec4\uff0c\u800cmsg_iovlen\u6307\u660e\u4e86\u8be5\u6570\u7ec4\u7684\u5143\u7d20\u4e2a\u6570\u3002<\/p>\n<p><strong>struct iovec<\/strong><\/p>\n<p>iovec\u7ed3\u6784\u8868\u793a\u4e00\u4e2a\u5411\u91cf\u5143\u7d20\uff0c\u5b83\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6807\u51c6\u7684\u6570\u636e\u7f13\u51b2\u533a\u683c\u5f0f\u3002\u5176\u5305\u542b\u4e24\u4e2a\u5b57\u6bb5\uff1a\u6307\u5411\u6570\u636e\u7684\u6307\u9488\u548c\u6570\u636e\u957f\u5ea6\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nstruct iovec\r\n{\r\n    void *iov_base;     \/* Pointer to data.  *\/\r\n    size_t iov_len;     \/* Length of data.  *\/\r\n};\r\n<\/pre>\n<p>\u5f53\u4f7f\u7528iovec\u7ed3\u6784\u7c7b\u578b\u7684\u6570\u7ec4\u4f20\u9012\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u5c06\u591a\u4e2a\u6d88\u606f\u901a\u8fc7\u4e00\u6b21\u7cfb\u7edf\u8c03\u7528\u8fdb\u884c\u53d1\u9001\u3002<\/p>\n<p><strong>struct nlmsghdr<\/strong><\/p>\n<p>\u5982\u679c\u4f7f\u7528sendmsg\u51fd\u6570\u6765\u53d1\u9001netlink\u6570\u636e\u62a5\uff0c\u90a3\u4e48iovec\u7ed3\u6784\u4e2diov_base\u5b57\u6bb5\u5e94\u8be5\u6839\u636e\u5177\u4f53\u534f\u8bae\u6307\u5411\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u6570\u636e\u7ed3\u6784\u3002\u4e00\u822c\u8fd9\u4e2a\u81ea\u5b9a\u4e49\u7684\u6570\u636e\u7ed3\u6784\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nstruct req {\r\n        struct nlmsghdr *nlh;\r\n        struct special_struct *r;\r\n};\r\n<\/pre>\n<p>\u5176\u4e2dnlmsghdr\u7ed3\u6784\u4e3a\u5fc5\u987b\u6240\u6709\u7684\uff0c\u5b83\u7528\u6765\u63cf\u8ff0netlink\u6d88\u606f\u5934\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n           struct nlmsghdr {\r\n               __u32 nlmsg_len;    \/* Length of message including header. *\/\r\n               __u16 nlmsg_type;   \/* Type of message content. *\/\r\n               __u16 nlmsg_flags;  \/* Additional flags. *\/\r\n               __u32 nlmsg_seq;    \/* Sequence number. *\/\r\n               __u32 nlmsg_pid;    \/* PID of the sending process. *\/\r\n           };\r\n<\/pre>\n<p>\u5bf9\u4e8especial_struct\u7ed3\u6784\u5219\u8981\u6839\u636e\u5177\u4f53\u9700\u6c42\u800c\u5b9a\u3002\u5982\u679c\u4f7f\u7528netlink\u73b0\u6709\u7684\u534f\u8bae\uff0c\u6bd4\u5982NETLINK_INET_DIAG\uff0c\u90a3\u4e48special_struct\u7ed3\u6784\u5728\u7528\u6237\u7a0b\u5e8f\u5411\u5185\u6838\u53d1\u9001\u6d88\u606f\u65f6\u5e94\u8be5\u4e3ainet_diag_req\u7ed3\u6784\uff0c\u5982\u679c\u5f53\u5185\u6838\u53d1\u9001\u6d88\u606f\u7ed9\u7528\u6237\u8fdb\u7a0b\u90a3\u4e48special_struct\u7ed3\u6784\u53c8\u5fc5\u987b\u5b9a\u4e49\u4e3ainet_diag_msg\u3002\u8fd9\u4e9b\u7ed3\u6784\u4f53\u90fd\u662f\u4e8b\u5148\u5b9a\u4e49\u597d\u7684\uff0c\u6211\u4eec\u76f4\u63a5\u62ff\u6765\u7528\uff0c\u6309\u9700\u83b7\u53d6\u8fd9\u4e9b\u7ed3\u6784\u4e2d\u7684\u67d0\u4e9b\u5b57\u6bb5\u7684\u503c\u5373\u53ef\u3002<\/p>\n<p>\u5982\u679c\u662f\u81ea\u5df1\u5b9a\u4e49\u7684\u901a\u4fe1\u534f\u8bae\uff0c\u90a3\u4e48\u5219\u6839\u636e\u5177\u4f53\u9700\u6c42\u81ea\u5b9a\u4e49\u6570\u636e\u7ed3\u6784\u5373\u53ef\u3002\u6bd4\u5982\u7528\u6237\u8fdb\u7a0b\u60f3\u901a\u8fc7pid\u5728\u5185\u6838\u4e2d\u83b7\u53d6\u8be5\u8fdb\u7a0b\u4eb2\u5c5e\u7684pid\uff0c\u90a3\u4e48\u7528\u6237\u8fdb\u7a0b\u5728\u53d1\u9001\u6d88\u606f\u548c\u63a5\u53d7\u6d88\u606f\u65f6\u7684special_struct\u5206\u522b\u53ef\u4ee5\u6309\u7167\u5982\u4e0b\u5b9a\u4e49\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nstruct get_pid_req {\r\n        int pid;\r\n};\r\n\r\nstruct get_pid_msg {\r\n        int parent_pid;\r\n        int sibling_pid;\r\n        int child_pid;\r\n}\r\n<\/pre>\n<p>\u4e3anetlink\u6d88\u606f\u786e\u5b9a\u597d\u4e86\u7ed3\u6784\u4ee5\u540e\uff0c\u5219\u5c06\u8be5\u7ed3\u6784\u8d4b\u503c\u7ed9iovec\u7ed3\u6784\u7684iov_base\u5b57\u6bb5\u3002<\/p>\n<p><strong>struct sockaddr_nl<\/strong><\/p>\n<p>netlink\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u65e0\u8fde\u63a5\u5957\u63a5\u5b57\uff0c\u56e0\u6b64\u5fc5\u987b\u5728msghdr\u7ed3\u6784\u7684msg_name\u4e2d\u6307\u660e\u76ee\u7684netlink\u5957\u63a5\u5b57\u7684\u5730\u5740\u3002\u5957\u63a5\u5b57\u5730\u5740\u7684\u901a\u7528\u7ed3\u6784\u4e3astruct sockaddr\uff0c\u4f46\u5bf9\u4e8e\u5177\u4f53\u7c7b\u578b\u7684\u5957\u63a5\u5b57\u5219\u6709\u4e0d\u540c\u7684\u5730\u5740\u7ed3\u6784\uff0c\u6bd4\u5982TCP\/IP\u534f\u8bae\u65cf\u4e3asockaddr_in\uff0c\u5bf9\u4e8enetlink\u5219\u4e3asockaddr_nl\u7ed3\u6784\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n           struct sockaddr_nl {\r\n               sa_family_t     nl_family;  \/* AF_NETLINK *\/\r\n               unsigned short  nl_pad;     \/* Zero. *\/\r\n               pid_t           nl_pid;     \/* Process ID. *\/\r\n               __u32           nl_groups;  \/* Multicast groups mask. *\/\r\n           };\r\n<\/pre>\n<p>\u5176\u4e2d\u8be5\u7ed3\u6784\u4e2d\u7684nl_family\u5b57\u6bb5\u586b\u5145\u4e3aAF_NETLINK\u3002<\/p>\n<p>\u4ee5\u4e0a\u8fd9\u4e9b\u7ed3\u6784\u5728netlink\u7f16\u7a0b\u4e2d\u90fd\u4f1a\u6d89\u53ca\u5230\uff0c\u4f46\u662f\u901a\u8fc7\u4e0a\u9762\u63cf\u8ff0\u7684\u5173\u7cfb\u6765\u770b\uff0c\u5b83\u4eec\u6700\u7ec8\u90fd\u662f\u4e0emsghdr\u7ed3\u6784\u6709\u5173\u7684\u3002\u800cmsghdr\u7ed3\u6784\u4f5c\u4e3a\u53c2\u6570\u901a\u8fc7sendmsg\u51fd\u6570\u5c31\u53ef\u4ee5netlink\u6570\u636e\u5305\u53d1\u9001\u81f3\u5185\u6838\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5185\u6838\u6001\u4e0e\u7528\u6237\u6001\u8fdb\u884c\u6570\u636e\u4ea4\u4e92\u7684\u65b9\u6cd5\u5f88\u591a\uff0c\u5206\u4e3a\u7528\u6237\u7a0b\u5e8f\u4e3b\u52a8\u53d1\u8d77\u7684\u6d88\u606f\u4ea4\u4ea4\u4e92\u548c\u5185\u6838\u4e3b\u52a8\u53d1\u8d77\u7684\u4fe1\u606f\u4ea4\u4e92\u3002\u901a\u5e38\u6211\u4eec\u6240\u719f\u77e5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[353],"tags":[354,209,405],"class_list":["post-4131","post","type-post","status-publish","format-standard","hentry","category-netlink","tag-netlink","tag-socket","tag-405"],"views":8189,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/4131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=4131"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/4131\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=4131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=4131"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=4131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}