Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1802265
  • 博文数量: 286
  • 博客积分: 3713
  • 博客等级: 少校
  • 技术积分: 2275
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-11 09:47
个人简介

http://blog.chinaunix.net/uid/16979052.html

文章分类

全部博文(286)

文章存档

2018年(1)

2017年(16)

2016年(9)

2015年(17)

2014年(15)

2013年(112)

2012年(116)

分类:

2015-07-31 07:10:14

原文地址:netlink接口 作者:@sky

#include <asm/atomic.h>
#include <asm/byteorder.h>
#include <asm/checksum.h>
#include <asm/unaligned.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/net.h>
#include <linux/in.h>
#include <linux/types.h>
#include <linux/kthread.h>
#include <linux/wait.h>
#include <linux/skbuff.h>
#include <linux/string.h>
#include <linux/sysctl.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/netfilter_arp.h>
#include <linux/ip.h>
#include <linux/workqueue.h>
#include <linux/jiffies.h>
#include <linux/route.h>
#include <linux/if_arp.h>
#include <linux/inetdevice.h>
#include <linux/slab.h>
#include <linux/stddef.h>
#include <linux/mutex.h>
#include <linux/compiler.h>
#include <linux/icmp.h>
#include <linux/jhash.h>
#include <linux/list.h>
#include <linux/inet.h>
#include <linux/ctype.h>
#include <linux/timer.h>
#include <linux/spinlock_types.h>
#include <linux/netlink.h>

#include <net/net_namespace.h>
#include <net/sock.h>
#include <net/route.h>
#include <net/inet_connection_sock.h>
#include <net/request_sock.h>
#include <net/icmp.h>
#include <net/ip.h>
#include <net/tcp.h>

#define err(msg) printk(KERN_ERR "%s failed.\n", msg)
#define SA struct sockaddr

#define NETLINK_SINO 27

static struct sock *sk;

static void netlink_handler(struct sk_buff *skb)
{
    struct nlmsghdr *hdr;
    char *data;
    struct sk_buff *nskb;
    int pid;

    hdr = nlmsg_hdr(skb);
    if (hdr == NULL)
    {
        err("nlmsg_hdr");
        goto out;
    }
    pid = hdr->nlmsg_pid;
    
    data = nlmsg_data(hdr);
    if (data)
        printk(KERN_INFO "data: %s\n", data);

    if ((nskb = nlmsg_new(1024, GFP_KERNEL | __GFP_ZERO)) == NULL)
    {
        err("nlmsg_new");
        goto out;
    }

    hdr = nlmsg_put(nskb, pid, 0, 0, 1024, 0);
    hdr->nlmsg_len     = NLMSG_LENGTH(1024);
    memcpy(nlmsg_data(hdr), "songtao\n", 8);
    nlmsg_unicast(sk, nskb, pid);
out:
    return;
}

static int main_init(void)
{
    sk = netlink_kernel_create(&init_net, NETLINK_SINO, 0, netlink_handler, NULL, THIS_MODULE);
    if (sk == NULL)
    {
        err("netlink_kernel_create");
        goto out;
    }

    return 0;
out:
    return -1;
}

static void main_exit(void)
{    
    netlink_kernel_release(sk);
}

module_init(main_init);
module_exit(main_exit);
MODULE_LICENSE("GPL");


#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <linux/netlink.h>

#define SA struct sockaddr
#define err(msg) perror(msg)
#define NETLINK_SINO 27

struct netlink_msg
{
    struct nlmsghdr hdr;
    char data[1024];
};

int main(int argc, char **argv)
{
    struct sockaddr_nl saddr, daddr;
    int fd;
    struct netlink_msg msg;

    if ((fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_SINO)) == -1)
    {
        err("socket");
        goto out;
    }

    memset(&msg, '\0', sizeof(msg));
    msg.hdr.nlmsg_len    = NLMSG_SPACE(10);
    msg.hdr.nlmsg_pid    = getpid();
    memcpy(NLMSG_DATA(&msg.hdr), "abcdefghi", 9);

    memset(&daddr, '\0', sizeof(daddr));
    daddr.nl_family    = AF_NETLINK;

    if (sendto(fd, &msg, msg.hdr.nlmsg_len, 0, (SA *)&daddr, sizeof(daddr)) == -1)
    {
        err("sendto");
        goto err;
    }
    char buff[1024];
    int len;

    memset(buff, '\0', sizeof(buff));
    if ((len = recvfrom(fd, buff, sizeof(buff), 0, NULL, 0)) == -1)
    {
        err("recvfrom");
        goto err;
    }
    char *data = NLMSG_DATA(buff);
    write(STDOUT_FILENO, data, strlen(data));
    
    return 0;
err:
    close(fd);
out:
    return -1;
}


阅读(1241) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~