上述4内核Python的网络链路组播通信 [英] Python Netlink Multicast Communication in Kernels above 4

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

问题描述

我试图从previous <一个重现的例子href=\"https://stackoverflow.com/questions/22691305/multicast-from-kernel-to-user-space-via-netlink-in-c\">SO帖子以上4内核(4.1):

 的#include&LT; Linux的/  -  module.h中GT;
#包括LT&;的Linux / kernel.h&GT;
#包括LT&; Linux的/ netlink.h&GT;
#包括LT&;净/ netlink.h&GT;
#包括LT&;净/ net_namespace.h&GT;/ *协议族,在这两个内核PROG和用户PROG一致。 * /
#定义MYPROTO NETLINK_USERSOCK
/ *组播组,在两个内核PROG和用户PROG一致。 * /
#定义MYGRP 31静态结构袜子* nl_sk = NULL;静态无效send_to_user(无效)
{
    结构的sk_buff * SKB;
    结构nlmsghdr * NLH;
    字符*味精=你好,从内核
    INT msg_size = strlen的(MSG)+ 1;
    中期业绩;    pr_info(创建SKB \\ n);
    SKB = nlmsg_new(NLMSG_ALIGN(msg_size + 1)GFP_KERNEL);
    如果(!SKB){
        pr_err(分配失败\\ n);
        返回;
    }    NLH = nlmsg_put(SKB,0,1,NLMSG_DONE,msg_size + 1,0);
    的strcpy(nlmsg_data(北大屿山医院),味精);    pr_info(发送SKB \\ n);
    RES = nlmsg_multicast(nl_sk,SKB,0,MYGRP,GFP_KERNEL);
    如果(RES℃,)
        pr_info(nlmsg_multicast()错误数:%d \\ n,RES);
    其他
        pr_info(成功\\ n);
}静态INT __init hello_init(无效)
{
    pr_info(插入你好模块\\ n);    nl_sk = netlink_kernel_create(安培; init_net,MYPROTO,NULL);
    如果(!nl_sk){
        pr_err(错误创建套接字\\ n);
        返回-10;
    }    send_to_user();    netlink_kernel_release(nl_sk);
    返回0;
}静态无效__exit hello_exit(无效)
{
    pr_info(退出你好模块\\ n);
}宏module_init(hello_init);
宏module_exit(hello_exit);MODULE_LICENSE(GPL);

不过,汇编工作正常,但是当我插入的模块,它返回:

  nlmsg_multicast()错误:-3

我不知道,我在哪里可以查找错误codeS学习,在这方面有什么意思-3(我搜索的这里,但无法找到任何有用的,对于错误code)。

只是可以肯定,我张贴的userland code(蟒蛇)也:

EDITED由于评论:(但仍无法正常工作)

 #!的/ usr /斌/包膜蟒蛇进口插座
进口OS
进口时间袜子= socket.socket(socket.AF_NETLINK,socket.SOCK_DGRAM,socket.NETLINK_USERSOCK)#270是SOL_NETLINK和1是NETLINK_ADD_MEMBERSHIP
sock.setsockopt(270,1,31)而1:
  尝试:
    打印sock.recvfrom(1024)
  除了socket.error,E:
    打印异常


解决方案

您忘了绑定套接字。 : - )

我不是很流利的与Python,所以只把这个当作一个起点(插座的setsockopt

  sock.bind((0,0))

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

 你好,从内核


顺便说一句:当 nlmsg_multicast()抛出 ESRCH ,它通常(或者总是)是因为有没有客户听。

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

否则,你总是可以忽略这个错误code这是有意义的您的使用情况。

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.\n");
    skb = nlmsg_new(NLMSG_ALIGN(msg_size + 1), GFP_KERNEL);
    if (!skb) {
        pr_err("Allocation failure.\n");
        return;
    }

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

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

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

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

    send_to_user();

    netlink_kernel_release(nl_sk);
    return 0;
}

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

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

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).

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

EDITED due to a comment: (but still not working)

#!/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. :-)

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


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的网络链路组播通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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