#include #include #include #include #include #include #include "libnetlinku.h" #include "policyconf.h" #include "commuapinl.h" struct upmnl_handle cfgnlh; #if 0 static int process_msg(struct pdelivnl_ctrl_data *ctrl, struct nlmsghdr *n, void *arg) { // FILE *fp = arg; printf("pdelivery main process_msg begin:\r\n"); //if (timestamp) //print_timestamp(fp); switch (n->nlmsg_type) { case NLMSG_RECV_RAW_PKT: printf("netlink msg type RAW_pkt:\r\n"); printf_pkt(NLMSG_DATA(n),n->nlmsg_len-NLMSG_HDRLEN); break; default: return 0; } return 0; } #endif int set_user_policy() { unsigned char srcip[64]={"192.168.1.1"};/*需要发送的数据*/ int srcip_len = strlen((const char *)srcip); struct { struct nlmsghdr n; struct policyconfmsg ncm; char buf[1024]; } req = { .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct policyconfmsg)), .n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK, .n.nlmsg_type = COMMNMSG_POLICYCONF, .ncm.policy_id = 17, .n.nlmsg_pid = getpid(), }; /*可选属性*/ commnl_addattr_l(&req.n, sizeof(req), POLICYCONFA_SRCIP, srcip, srcip_len); printf("srcip_len =%d.req.n.nlmsg_type =%d\r\n",srcip_len,req.n.nlmsg_type); /*发送组装好的netlink消息*/ if (commcfg_talk(&req.n, NULL) < 0) return -2; return 0; } int set_user_policy2() { unsigned char srcip[64]={"192.168.1.1"};/*需要发送的数据*/ int srcip_len = strlen((const char *)srcip); struct { struct nlmsghdr n; struct policyconfmsg ncm; char buf[1024]; } req = { .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct policyconfmsg)), .n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK, .n.nlmsg_type = COMMNMSG_POLICYCONF, .ncm.policy_id = 17, }; /*可选属性*/ commnl_addattr_l(&req.n, sizeof(req), POLICYCONFA_SRCIP, srcip, srcip_len); printf("srcip_len =%d.req.n.nlmsg_type =%d\r\n",srcip_len,req.n.nlmsg_type); /*发送组装好的netlink消息*/ if (commcfg_talk(&req.n, NULL) < 0) return -2; return 0; } int main( int argc, char **argv) { int ret = -1; printf("cfgchannel main begin:\r\n"); /*创建通道*/ ret = commcfgnl_open(); if(ret < 0) { printf("pdelivnl_open fail,exit.\r\n"); return -1; } /*下发配置到内核态*/ ret = set_user_policy(); if(ret < 0) { printf("set_user_policy failed.\r\n"); return -1; } /*关闭netlink通道*/ commcfgnl_close(); printf("cfgchannel main exit!\r\n"); return 0; }