使用者進程與核心進程通訊netlink執行個體

來源:互聯網
上載者:User
為了方便應用程式使用netlink介面,一個叫libnl庫被開發.1 下載libnl2 http://packages.ubuntu.com/oneiric/libnl22 下載libnl2-devhttp://packages.ubuntu.com/precise/libnl2-dev3 先安裝libnl2 在安裝libnl2-dev 應用程式層代碼 #include <stdio.h>#include <stdlib.h> #include <netlink/netlink.h> #define MY_MSG_TYPE (0x10 + 2)  // + 2 is arbitrary but is the same for kern/usrintmain(int argc, char *argv[]){    struct nl_sock *nls;    char msg[] = { 0xde, 0xad, 0xbe, 0xef, 0x90, 0x0d, 0xbe, 0xef };    int ret;     nls = nl_socket_alloc();    if (!nls) {        printf("bad nl_socket_alloc\n");        return EXIT_FAILURE;    }     ret = nl_connect(nls, NETLINK_USERSOCK);    if (ret < 0) {        nl_perror(ret, "nl_connect");        nl_socket_free(nls);        return EXIT_FAILURE;    }     ret = nl_send_simple(nls, MY_MSG_TYPE, 0, msg, sizeof(msg));    if (ret < 0) {        nl_perror(ret, "nl_send_simple");        nl_close(nls);        nl_socket_free(nls);        return EXIT_FAILURE;    } else {        printf("sent %d bytes\n", ret);    }     nl_close(nls);    nl_socket_free(nls);     return EXIT_SUCCESS;} 核心代碼 #include <linux/kernel.h>#include <linux/module.h> #include <net/sock.h>#include <net/netlink.h> #define MY_MSG_TYPE (0x10 + 2)  // + 2 is arbitrary. same value for kern/usr static struct sock *my_nl_sock; DEFINE_MUTEX(my_mutex); static intmy_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh){    int type;    char *data;     type = nlh->nlmsg_type;    if (type != MY_MSG_TYPE) {        printk("%s: expect %#x got %#x\n", __func__, MY_MSG_TYPE, type);        return -EINVAL;    }     data = NLMSG_DATA(nlh);    printk("%s: x x x x x x x x\n", __func__,            data[0], data[1], data[2], data[3],            data[4], data[5], data[6], data[7]);    return 0;} static voidmy_nl_rcv_msg(struct sk_buff *skb){    mutex_lock(&my_mutex);    netlink_rcv_skb(skb, &my_rcv_msg);    mutex_unlock(&my_mutex);} static intmy_init(void){    my_nl_sock = netlink_kernel_create(&init_net, NETLINK_USERSOCK, 0,            my_nl_rcv_msg, NULL, THIS_MODULE);    if (!my_nl_sock) {        printk(KERN_ERR "%s: receive handler registration failed\n", __func__);        return -ENOMEM;    }     return 0;} static voidmy_exit(void){    if (my_nl_sock) {        netlink_kernel_release(my_nl_sock);    }} module_init(my_init);module_exit(my_exit);  系統:ubuntu 10.04 ,核心: linux-2.6.38
編譯應用程式層代碼需要加入動態連結程式庫 /usr/lib/libnl.so
即sudo gcc -o 1 1.c /usr/lib/libnl.so 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.