• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
梦想照进灵魂
博客园    首页    新随笔    联系   管理    订阅  订阅
netlink

存在问题,在通信的时候可能会死机,不知道是


#define NETLINK_TEST 30

未在include/linux/netlink.h定义导致,还是其他问题(加锁)。

netlink.c

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <net/sock.h>
#include <net/netlink.h>

#define pr_inf(fmt, args ...) \
    printk("\033[31m[net] %s [%d] : " fmt "\033[0m\n", \
            __FUNCTION__, __LINE__, ##args)

#define NETLINK_TEST 30

struct sock *nl_sk = NULL;

void nl_data_ready(struct sk_buff *__skb)
{
    struct sk_buff *skb;
    struct nlmsghdr *nlh;
    u32 pid;
    int rc;
    int len = NLMSG_SPACE(1200);
    char str[64];

    pr_inf("net_link : data is ready to read.");

    skb = skb_get(__skb);

    if (skb->len >= NLMSG_SPACE(0)) {
        nlh = nlmsg_hdr(skb);
        pid = nlh->nlmsg_pid;
        pr_inf("net_link : recv %s pid %d.",
                (char *)NLMSG_DATA(nlh), pid);
        memcpy(str, NLMSG_DATA(nlh), sizeof(str));

        kfree(skb);

        skb = alloc_skb(len, GFP_ATOMIC);
        if (!skb) {
            pr_inf("net_link: allocate failed.");
            return;
        }

        nlh = nlmsg_put(skb, 0, 0, 0, 1200, 0);
        NETLINK_CB(skb).pid = 0;

        memcpy(NLMSG_DATA(nlh), str, sizeof(str));
        pr_inf("net_link : going to sen.");
        rc = netlink_unicast(nl_sk, skb, pid, MSG_DONTWAIT);
        if (rc < 0) {
            pr_inf("net_link : can not unicast skb (%d).", rc);
        }
        pr_inf("net_link:send is ok.");
    }
    return;
}

static int my_netlink(void)
{
    nl_sk = netlink_kernel_create(&init_net, NETLINK_TEST,
            0, nl_data_ready, NULL, THIS_MODULE);

    if (!nl_sk) {
        pr_inf("net_link : Cannot Create netlink socket.");
        return -EIO;
    }
    pr_inf("net_link : create socket ok.");
    return 0;
}

int __init hello_init(void)
{
    pr_inf();
    my_netlink();
    return 0;
}


void __exit hello_exit(void)
{
    if (nl_sk != NULL)
        sock_release(nl_sk->sk_socket);
    nl_sk = NULL;
    pr_inf("net_link : release");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Mark");


test.c

#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <asm/types.h>
#include <linux/netlink.h>
#include <linux/socket.h>

#define MAX_PAYLOAD 1024 /* maximum payload size*/

#define NETLINK_TEST 30

struct sockaddr_nl src_addr, dest_addr;
struct nlmsghdr *nlh = NULL;
struct iovec iov;
int sock_fd;
struct msghdr msg;

int main(int argc, char* argv[])
{
    sock_fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_TEST);
    memset(&msg, 0, sizeof(msg));
    memset(&src_addr, 0, sizeof(src_addr));
    src_addr.nl_family = AF_NETLINK;
    src_addr.nl_pid = getpid(); /* self pid */
    src_addr.nl_groups = 0; /* not in mcast groups */
    bind(sock_fd, (struct sockaddr*)&src_addr, sizeof(src_addr));
    memset(&dest_addr, 0, sizeof(dest_addr));
    dest_addr.nl_family = AF_NETLINK;
    dest_addr.nl_pid = 0; /* For Linux Kernel */
    dest_addr.nl_groups = 0; /* unicast */

    nlh=(struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD));
    /* Fill the netlink message header */
    nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD);
    nlh->nlmsg_pid = getpid(); /* self pid */
    nlh->nlmsg_flags = 0;
    /* Fill in the netlink message payload */
    strcpy(NLMSG_DATA(nlh), "Hello you!");

    iov.iov_base = (void *)nlh;
    iov.iov_len = nlh->nlmsg_len;
    msg.msg_name = (void *)&dest_addr;
    msg.msg_namelen = sizeof(dest_addr);
    msg.msg_iov = &iov;
    msg.msg_iovlen = 1;

    printf(" Sending message. ...\n");
    sendmsg(sock_fd, &msg, 0);

    /* Read message from kernel */
    memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD));
    printf(" Waiting message. ...\n");
    recvmsg(sock_fd, &msg, 0);
    printf(" Received message payload: %s\n", (char *)NLMSG_DATA(nlh));

    /* Close Netlink Socket */
    close(sock_fd);
}



posted on 2012-08-19 18:07  梦想照进灵魂  阅读(409)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3