- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试使用 nl_recvmsgs作为从内核模块接收 Netlink 消息的阻塞函数。在我的示例中,客户端向内核发送一条消息,然后调用 nl_recvmsgs_report()(等于 nl_recvmsgs)。然后内核模块发送一个返回信息。客户端成功接收到该消息。
现在我希望客户端在以后监听更多消息并再次调用 nl_recvmsgs_report()。内核没有发送任何第二条消息。但是客户端以某种方式收到了 ERRORMSG。这会导致客户端出现 SEGFAULT,因为他试图将消息解析为 ERRORMSG。
如果我检查消息类型是否为 2 并跳过消息解析,则 nl_recvmsgs_report() 的第三次调用完全没问题。
有人知道为什么客户端会收到这个 ERRORMSG 吗?
看看我的 github branch
。只需调用 make, sudo insmod nlk.ko, ./nlclient
我在这里只复制了相关部分。
// setup netlink socket
sk = nl_socket_alloc();
nl_socket_disable_seq_check(sk); // disable sequence number check
genl_connect(sk);
int id = genl_ctrl_resolve(sk, DEMO_FAMILY_NAME);
struct nl_msg * msg;
// create a messgae
msg = nlmsg_alloc();
genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, id, 0, // hdrlen
0, // flags
DEMO_CMD, // numeric command identifier
DEMO_VERSION // interface version
);
nla_put_string(msg, DEMO_ATTR1_STRING, "hola");
nla_put_u16(msg, DEMO_ATTR2_UINT16, 0xf1);
// send it
nl_send_auto(sk, msg);
// handle reply
struct nl_cb * cb = NULL;
cb = nl_cb_alloc(NL_CB_CUSTOM);
//nl_cb_set_all(cb, NL_CB_DEBUG, NULL, NULL);
nl_cb_set_all(cb, NL_CB_CUSTOM, cb_handler, &cbarg);
nl_cb_err(cb, NL_CB_DEBUG, NULL, NULL);
int nrecv = nl_recvmsgs_report(sk, cb);
printf("cbarg %d nrecv %d\n", cbarg, nrecv);
printf("First test if it blocks here for incoming messages:\n");
nrecv = nl_recvmsgs_report(sk, cb);
printf("cbarg %d nrecv %d\n", cbarg, nrecv);
printf("Second test if it blocks here for incoming messages:\n");
nrecv = nl_recvmsgs_report(sk, cb);
printf("cbarg %d nrecv %d\n", cbarg, nrecv);
struct nlmsghdr * hdr = nlmsg_hdr(msg);
struct genlmsghdr * gnlh = nlmsg_data(hdr);
nl_msg_dump(msg, stderr);
if (hdr->nlmsg_type == 2) {
printf("hdr->nlmsg_type is ERROR. Skipping message parsing!\n");
} else {
int valid =
genlmsg_validate(hdr, 0, DEMO_ATTR_MAX, demo_gnl_policy);
printf("valid %d %s\n", valid, valid ? "ERROR" : "OK");
// one way
struct nlattr * attrs[DEMO_ATTR_MAX + 1];
if (genlmsg_parse(hdr, 0, attrs, DEMO_ATTR_MAX, demo_gnl_policy) < 0)
{
printf("genlsmg_parse ERROR\n");
}
else
{
printf("genlsmg_parse OK\n");
printf("attr1 %s\n", nla_get_string(attrs[DEMO_ATTR1_STRING]));
printf("attr2 %x\n", nla_get_u16(attrs[DEMO_ATTR2_UINT16]));
struct attr_custom * cp = (struct attr_custom *) nla_data(attrs[DEMO_ATTR3_CUSTOM]);
printf("attr3 %d %ld %f %lf\n", cp->a, cp->b, cp->c,cp->d);
}
}
// another way
printf("gnlh->cmd %d\n", gnlh->cmd); //--- DEMO_CMD_ECHO
int remaining = genlmsg_attrlen(gnlh, 0);
struct nlattr * attr = genlmsg_attrdata(gnlh, 0);
while (nla_ok(attr, remaining))
{
printf("remaining %d\n", remaining);
printf("attr @ %p\n", attr); // nla_get_string(attr)
attr = nla_next(attr, &remaining);
}
/* send message back */
/* allocate some memory, since the size is not yet known use NLMSG_GOODSIZE */
skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
if (skb == NULL) {
goto out;
}
/* create the message */
msg_head =
genlmsg_put(skb, 0, info->snd_seq + 1, &demo_gnl_family, 0,
DEMO_CMD);
if (msg_head == NULL) {
rc = -ENOMEM;
goto out;
}
rc |= nla_put_string(skb, DEMO_ATTR1_STRING,"world");
rc |= nla_put_u16(skb, DEMO_ATTR2_UINT16, 0x1f);
cp.a = 1;
cp.b = 2;
cp.c = 3.0;
cp.d = 4.0;
rc |= nla_put(skb, DEMO_ATTR3_CUSTOM, sizeof(struct attr_custom), &cp);
if (rc != 0) {
goto out;
}
/* finalize the message */
genlmsg_end(skb, msg_head);
/* send the message back */
rc = genlmsg_unicast(&init_net, skb, info->snd_portid);
if (rc != 0) {
goto out;
}
return 0;
./nlclient
-------------------------- BEGIN NETLINK MESSAGE ---------------------------
[NETLINK HEADER] 16 octets
.nlmsg_len = 76
.type = 27 <0x1b>
.flags = 0 <>
.seq = 1458476257
.port = 0
[GENERIC NETLINK HEADER] 4 octets
.cmd = 1
.version = 1
.unused = 0
[PAYLOAD] 56 octets
0a 00 01 00 77 6f 72 6c 64 00 00 00 06 00 02 00 ....world.......
1f 00 00 00 24 00 03 00 01 00 00 00 ff ff ff ff ....$...........
02 00 00 00 00 00 00 00 00 00 40 40 04 88 ff ff ..........@@....
00 00 00 00 00 00 10 40 .......@
--------------------------- END NETLINK MESSAGE ---------------------------
valid 0 OK
genlsmg_parse OK
attr1 world
attr2 1f
attr3 1 2 3.000000 4.000000
gnlh->cmd 1
remaining 56
attr @ 0x10df344
remaining 44
attr @ 0x10df350
remaining 36
attr @ 0x10df358
cbarg 123 nrecv 1
First test if it blocks here for incoming messages:
-------------------------- BEGIN NETLINK MESSAGE ---------------------------
[NETLINK HEADER] 16 octets
.nlmsg_len = 36
.type = 2 <ERROR>
.flags = 0 <>
.seq = 1458476256
.port = -1061151077
[ERRORMSG] 20 octets
.error = 0 "Success"
[ORIGINAL MESSAGE] 16 octets
.nlmsg_len = 16
.type = 27 <0x1b>
.flags = 5 <REQUEST,ACK>
.seq = 1458476256
.port = -1061151077
--------------------------- END NETLINK MESSAGE ---------------------------
hdr->nlmsg_type is ERROR. Skipping message parsing!
gnlh->cmd 0
cbarg 123 nrecv 1
Second test if it blocks here for incoming messages:
kernel: [ 4694.318428] got demo_cmd
kernel: [ 4694.318430] attr1: hola
kernel: [ 4694.318431] attr2: f1
最佳答案
抱歉拖了这么久。
输出有些误导。那不是错误信息;这是一个自动ACK . Netlink defines ACKs to be "error" messages with error code 0 .
(零是 C 语言中成功的典型行话。)
由于您正在设计一个答案,所以您可能根本不需要 ACK。您可以通过添加对 nl_socket_disable_auto_ack()
的调用来阻止您的客户端请求 ACK。
我会破解它接近于禁用序列检查,因为它有点相似:
sk = nl_socket_alloc();
nl_socket_disable_seq_check(sk);
nl_socket_disable_auto_ack(sk);
genl_connect(sk);
关于c - 为什么我在使用 nl_recvmsgs 时收到 Netlink ERRORMSG?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36113862/
我有一个静态类。 static class AppDirectory { public static string PACSTEMP = Path.Combine(Path.GetTempPa
我已经设置了一个启用了推送通知的 iOS 应用。 我可以将消息推送到应用程序,例如角标(Badge)计数工作并相应更新。 但我从未在锁屏或其他地方看到标准的推送通知弹出窗口,但手机会振动,因此消息会通
我们有一个带有 Web 应用程序和一堆 Windows 服务的系统,它们在做一些后台工作。 每当我们需要对系统进行更实质性的更改时,我们最终不得不发出 IIS 重置,然后手动重新启动所有相关的 Win
我有以下几行 John SMith: A Pedro Smith: B Jonathan B: A John B: B Luis Diaz: A Scarlet Diaz: B 我需要获得所有获得
我正在编写一个 Java 客户端(在 weblogic 10.3 上)来调用一个安全的网络服务。我已获得安装在 cacerts、DemoIdentity.jks 和 DemoTrust,jks 中的客
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎偏离主题,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或 include a mini
我正在尝试调用void方法addToList,该方法将通过用户传递给它的两个字符串除外。我检查了dataSource类,以确保它确实接受了那些作为参数。问题是我在该方法调用上始终收到标识符>预期错误,
我的任务:使用scanner方法从一行数据中提取字符串、 float 和整数。 数据格式为: Random String, 240.5 51603 Another String, 41.6 59087
这个问题已经有答案了: What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? (25 个回答)
首先我实例化一个游戏状态 class GameState extends state{ ArrayList levels; int currentLevelID; public GameState()
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我有一个实现为单例的 Controller 对象,它有一个可以随时驱逐对象的缓存。当一个对象即将被删除时,我想通知任何使用此 Controller 的类,以便它们能够做出适当的响应。我对这种行为的第一
因此,我尝试跨集群发送消息,该消息将包含一个 User 对象,该对象是一个可序列化类。 当我发送 String 或 int 时,它工作正常,消息发送没有问题,并且集群上的所有 channel 都收到它
我试图创建的程序是一个基本游戏,用户输入网格大小,选择 block 接收增加分数的奖品、从分数中夺走分数的强盗或结束游戏的炸弹。我收到堆栈流错误,但我不明白为什么? 抱歉,代码量很大,我只是无法找到问
使用此代码我会得到什么ConcurrentModificationException?我有一个同步(监听器)锁。 private void notifyListeners(MediumRenditio
我想在捕获 DeadlineExceededError 后正确退出。我还剩下多少钱来清理? 例如, try: do_some_work() except DeadlineExceededError
我有 2 个 Intranet 站点: http://intranetv1/ http://intranetv2/ v1基于.NET 1.1,v2基于.NET 3.5 在 v1 上,我创建了一个网页,
我有一个在 Linux 3.12 上运行的 C 程序。该程序产生几个子进程。其中一个进程会生成一个线程,该线程运行一段时间然后终止。当该子进程运行时,它会执行 epoll_wait()。 epoll_
我能够将 APNS 集成到我的应用程序中。现在我想在用户点击它或用户在使用应用程序时收到通知时处理通知。我使用下面的代码在收到通知时显示警报对话框: func application(applicat
当我试图在浏览器上运行这段代码时,出现了以下错误。"错误响应错误代码:501消息:不支持的方法(“POST”)。错误码解释:501-服务器不支持该操作。" 浏览器控制台出现以下错误: "1.加载资源失
我是一名优秀的程序员,十分优秀!