📄 netlink_lib.c
字号:
/*
Copyright (c) 2008, Intel Corporation.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
* Neither the name of Intel Corporation nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/
#include "netlink_lib.h"#define NETLINK_DEBUG perror#define BUFLEN 8192//---------------------------------------------------------------// netlink_open//@control: input the control block of netlink//@protocol: the protocol which user want to create//// it create a socket with protocol and bind it to local address,// then it verify the socket.////@return 0 if success, < 0 if fail//---------------------------------------------------------------int netlink_open(netlink_control_t control, int protocol){ int ret; ret = socket(AF_NETLINK, SOCK_RAW, protocol); if(ret < 0) { NETLINK_DEBUG("socket call failed!"); return -1; } control->sock = ret; ret = bind(control->sock, (struct sockaddr*)&(control->local_addr), sizeof(control->local_addr)); if(ret < 0) { NETLINK_DEBUG("bind call failed!"); return -1; } return 0;}//---------------------------------------------------------------// netlink_close//@control: the control block of netlink// // it close the netlink socket////@return 0 if success, < 0 if fail//---------------------------------------------------------------int netlink_close(netlink_control_t control){ int ret; ret = close(control->sock); if(ret < 0) { NETLINK_DEBUG("close call failed!"); return -1; } return 0;}//---------------------------------------------------------------// netlink_send//@control: the control block//@nlhdr: the netlink message hdr pointer followed the body//// it send the netlink message to kernel on the socket// //@return > 0 if success, < 0 if fail//---------------------------------------------------------------int netlink_send(netlink_control_t control, struct nlmsghdr *nlhdr){ int ret; struct msghdr msg; struct iovec iov; memset(&iov, 0, sizeof(struct iovec)); iov.iov_base = (void*)nlhdr; iov.iov_len = nlhdr->nlmsg_len; memset(&msg, 0, sizeof(struct msghdr)); msg.msg_iov = &iov; msg.msg_iovlen = 1; msg.msg_name = (void *)&(control->remote_addr); msg.msg_namelen = sizeof(control->remote_addr); ret = sendmsg(control->sock, &msg, 0); if(ret < 0) { NETLINK_DEBUG("sendmsg call failed!"); return -1; } return ret;}//-----------------------------------------------------------------// netlink_recv//@control: the control block//@buf: return the buffer to user//// it recv data from kernel on netlink socket, data in the buf////@return > 0 if success, < 0 if fail//-----------------------------------------------------------------int netlink_recv(netlink_control_t control, char **buf){ int ret; char *tempbuf; struct iovec iov; struct msghdr msg; *buf = NULL; tempbuf = (char *)malloc(BUFLEN); memset(&iov, 0, sizeof(struct iovec)); iov.iov_base = (void*)tempbuf; iov.iov_len = BUFLEN; memset(&msg, 0, sizeof(struct msghdr)); msg.msg_name = (void *)&(control->remote_addr); msg.msg_namelen = sizeof(control->remote_addr); msg.msg_iov = &iov; msg.msg_iovlen = 1;do_it: ret = recvmsg(control->sock, &msg, 0); if(ret < 0) { if(errno == EINTR) goto do_it; NETLINK_DEBUG("recvmsg call failed!"); free(tempbuf); return -1; } else if(ret == 0) { NETLINK_DEBUG("EOF read!"); free(tempbuf); return -1; } *buf = tempbuf; return ret;}//------------------------------------------------------------------////------------------------------------------------------------------int netlink_listen(netlink_control_t control, int (*callback)(netlink_control_t control,struct nlmsghdr *nlhdr)){ int ret; char *buf; struct nlmsghdr *nl; while(1) { int remain = 0; ret = netlink_recv(control, &buf); if(ret < 0) return -1; remain = ret; //parse the message for(nl = (struct nlmsghdr *)buf; remain > sizeof(*nl);) { int err; int len = nl->nlmsg_len; int l = len - sizeof(*nl); if(l < 0 || remain < len) { NETLINK_DEBUG("message malformed!"); return -1; } err = callback(control, nl); if(err < 0) { if(buf) free(buf); return -1; } remain = remain - NLMSG_ALIGN(len); nl = (struct nlmsghdr*)((char*)nl + NLMSG_ALIGN(len)); } if(remain > 0) { NETLINK_DEBUG("message truncated!"); if(buf) free(buf); return -1; } if(buf) { free(buf); buf = NULL; } } return 0;}//---------------------------------------------------------------------------////---------------------------------------------------------------------------int netlink_init(netlink_control_t control, unsigned groups){ int ret; memset(control, 0, sizeof(struct netlink_control)); control->local_addr.nl_family = AF_NETLINK; control->local_addr.nl_groups = groups; control->remote_addr.nl_family = AF_NETLINK; return 0;}//--------------------------------------------------------------------------////--------------------------------------------------------------------------int netlink_build_hdr(struct nlmsghdr *nlhdr, int type, int flags){ int ret; nlhdr->nlmsg_len = ((int) NLMSG_ALIGN(sizeof(struct nlmsghdr))); nlhdr->nlmsg_type = type; nlhdr->nlmsg_flags = flags; return 0;}//--------------------------------------------------------------------------////--------------------------------------------------------------------------int netlink_hdr_append(struct nlmsghdr **nlhdr, void *data, size_t len){ int ret; struct nlmsghdr *tmpnlhdr; tmpnlhdr = *nlhdr; len = NLMSG_ALIGN(len); tmpnlhdr = (struct nlmsghdr*)realloc(tmpnlhdr, tmpnlhdr->nlmsg_len + len); if(tmpnlhdr == NULL) { NETLINK_DEBUG("realloc call fail!"); return -1; } memcpy((void *)tmpnlhdr + tmpnlhdr->nlmsg_len, data, len); tmpnlhdr->nlmsg_len += len; *nlhdr = tmpnlhdr; return 0;}//--------------------------------------------------------------------------////--------------------------------------------------------------------------int netlink_recv_cb(netlink_control_t control, int (*callback)(netlink_control_t control,struct nlmsghdr *nlhdr)){ int ret; char *buf; struct nlmsghdr *nl; int remain = 0; ret = netlink_recv(control, &buf); if(ret < 0) { if(buf) free(buf); return -1; } remain = ret; //parse the message for(nl = (struct nlmsghdr *)buf; remain > sizeof(*nl);) { int err; int len = nl->nlmsg_len; int l = len - sizeof(*nl); if(l < 0 || remain < len) { NETLINK_DEBUG("message malformed!"); if(buf) free(buf); return -1; } err = callback(control, nl); if(err < 0) { if(buf) free(buf); return -1; } remain = remain - NLMSG_ALIGN(len); nl = (struct nlmsghdr*)((char*)nl + NLMSG_ALIGN(len)); } if(remain > 0) { NETLINK_DEBUG("message truncated!"); if(buf) free(buf); return -1; } if(buf) { free(buf); buf = NULL; } return 0;}//--------------------------------------------------------------------------////--------------------------------------------------------------------------int rt_parse_attr(struct rtattr *attr_table[], struct rtattr *pattr, int len){ while(RTA_OK(pattr, len)) { if(pattr->rta_type < RTA_MAX) attr_table[pattr->rta_type] = pattr; pattr = RTA_NEXT(pattr, len); } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -