我尝试使用带有标志IPT_SO_SET_REPLACE的setsockopt,但我一直收到来自errno Protocol not available的有线错误,这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sched.h>
#include <linux/sched.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <unistd.h>
#include <sys/ptrace.h>
#include <netinet/in.h>
#include <net/if.h>
#include <linux/netfilter_ipv4/ip_tables.h>
#include <fcntl.h>
int main(void) {
int sock;
int ret;
void *data;
size_t size;
struct ipt_replace *repl;
sock = socket(PF_INET, SOCK_RAW, IPPROTO_RAW);
if (sock == -1) {
perror("socket");
return -1;
}
size = sizeof(struct ipt_replace);
data = malloc(size); Protocol not available
if (data == NULL) {
perror("malloc");
return -1;
}
memset(data, 0, size);
repl = (struct ipt_replace *) data;
repl->num_counters = 0x1;
repl->size = 0xffffffff;
repl->valid_hooks = 0x1;
repl->num_entries = 0x1;
ret = setsockopt(sock, SOL_IP, IPT_SO_SET_REPLACE, (void *) data, size);
printf("\ndone %d\n", ret);
perror("error: ");
return 0;
}这是输出:
sock:3
data:
size:92
done -1
error: : Protocol not available发布于 2016-07-01 23:49:01
简单地看一下内核代码,这似乎表明IP表模块不可用(即内核构建时没有配置它,或者无法找到或加载它)。
在我看来,对于您创建的那种套接字,代码流是:
raw_setsockopt:level != SOL_RAW so...ip_setsockopt:level == D9但option不是任何IP_xxx选项,因此...nf_setsockopt:在已加载的netfilter模块中搜索已注册netfilter的模块我想最后一个肯定失败了,所以你拿回了ENOPROTOOPT (==协议不可用)
https://stackoverflow.com/questions/38127129
复制相似问题