setsockopt IPT_SO_SET_REPLACE标志返回错误(linux)

setsockopt IPT_SO_SET_REPLACE标志返回错误(linux),linux,c,Linux,C,我尝试使用带有IPT_SO_SET_REPLACE标志的setsockopt,但我一直从errno协议获取有线错误不可用这是我的代码: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sched.h> #include <linux/sched.h> #include <errno.h> #include <sys/types.h

我尝试使用带有IPT_SO_SET_REPLACE标志的setsockopt,但我一直从errno协议获取有线错误不可用这是我的代码:

#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;
}

简单地看一下内核代码,这似乎表明IP表模块不可用(即,内核未配置,或者无法找到或加载)

在我看来,对于您创建的套接字类型,代码流是:

  • 输入
    raw_setsockopt
    level
    !=<代码>SOL_RAW因此
  • 调用
    ip_setsockopt
    level
    =
    SOL_ip
    ,但该选项不是任何
    ip_xxx
    选项,因此
  • 调用
    nf_setsockopt
    :搜索加载的netfilter模块,查找已注册的
    IPT_SO_SET_REPLACE
我想最后一个肯定失败了,所以您返回了
enprotoop
back(=协议不可用)

sock:3 
data: 
size:92 
done -1
error: : Protocol not available