4 以上内核中的 Python Netlink 多播通信 [英] Python Netlink Multicast Communication in Kernels above 4

查看:38
本文介绍了4 以上内核中的 Python Netlink 多播通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从以前的 SO post 在 4 (4.1) 以上的内核上:

I was trying to reproduce the example from a previous SO post on a kernel above 4 (4.1):

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/netlink.h>
#include <net/netlink.h>
#include <net/net_namespace.h>

/* Protocol family, consistent in both kernel prog and user prog. */
#define MYPROTO NETLINK_USERSOCK
/* Multicast group, consistent in both kernel prog and user prog. */
#define MYGRP 31

static struct sock *nl_sk = NULL;

static void send_to_user(void)
{
    struct sk_buff *skb;
    struct nlmsghdr *nlh;
    char *msg = "Hello from kernel";
    int msg_size = strlen(msg) + 1;
    int res;

    pr_info("Creating skb.
");
    skb = nlmsg_new(NLMSG_ALIGN(msg_size + 1), GFP_KERNEL);
    if (!skb) {
        pr_err("Allocation failure.
");
        return;
    }

    nlh = nlmsg_put(skb, 0, 1, NLMSG_DONE, msg_size + 1, 0);
    strcpy(nlmsg_data(nlh), msg);

    pr_info("Sending skb.
");
    res = nlmsg_multicast(nl_sk, skb, 0, MYGRP, GFP_KERNEL);
    if (res < 0)
        pr_info("nlmsg_multicast() error: %d
", res);
    else
        pr_info("Success.
");
}

static int __init hello_init(void)
{
    pr_info("Inserting hello module.
");

    nl_sk = netlink_kernel_create(&init_net, MYPROTO, NULL);
    if (!nl_sk) {
        pr_err("Error creating socket.
");
        return -10;
    }

    send_to_user();

    netlink_kernel_release(nl_sk);
    return 0;
}

static void __exit hello_exit(void)
{
    pr_info("Exiting hello module.
");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");

然而,编译工作正常,但当我插入模块时,它返回:

However, compilation works fine, but when I insert the module, it returns:

nlmsg_multicast() error: -3

我什至不知道在哪里可以查找错误代码以了解 -3 在这种情况下的含义(我搜索了 here,但找不到任何有用的信息,关于错误代码).

I dont even know, where I can lookup the error codes to learn, what -3 means in this context (I searched here, but was unable to find anything useful, regarding the error code).

为了确定,我也发布了用户态代码 (Python):

Just to be sure, I post the userland code (Python) also:

由于评论而(但仍然无效)

#!/usr/bin/env python

import socket
import os
import time

sock = socket.socket(socket.AF_NETLINK, socket.SOCK_DGRAM, socket.NETLINK_USERSOCK)

# 270 is SOL_NETLINK and 1 is NETLINK_ADD_MEMBERSHIP
sock.setsockopt(270, 1, 31)

while 1:
  try:
    print sock.recvfrom(1024)
  except socket.error, e:
    print 'Exception'

推荐答案

您忘记绑定套接字.:-)

You forgot to bind the socket. :-)

我对 Python 不是很熟练,所以仅将其用作起点(在 socketsetsockopt 之间):

I'm not very fluent with Python, so use this only as a starting point (between the socket and the setsockopt):

sock.bind((0, 0))

这给我打印了一堆垃圾,我可以看到其中

That prints me a bunch of garbage, among which I can see

Hello from kernel

<小时>

顺便说一句:当 nlmsg_multicast() 抛出 ESRCH 时,通常(或可能总是)因为没有客户端在监听.


By the way: When nlmsg_multicast() throws ESRCH, it's usually (or maybe always) because there were no clients listening.

首先打开客户端,然后尝试从内核发送消息.

First open the client, then try to send the message from the kernel.

否则,您始终可以忽略对您的用例有意义的错误代码.

Otherwise you can always ignore that error code it that makes sense for your use case.

这篇关于4 以上内核中的 Python Netlink 多播通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆