c - 执行 iptables 代码时出错 “Error commit: Protocol wrong type for socket”

标签 c linux sockets iptables iptc

下面的 C 代码等价于下面的 iptables 命令:

ip6tables -A OUTPUT -t mangle -s 2001:db8:222:2::/64 -j MARK --set-mark 20

但是, iptables 命令在命令行中运行良好,但是当我执行代码时它给出了错误
Error commit: Protocol wrong type for socket   

虽然编译成功。我也尝试过设置 DSCP 值并且效果很好,所以我猜 MARK 模块缺少一些东西

Linux 内核 3.8.2
iptables 1.4.12 版(我也试过 1.4.21 但没用)

编码
struct ip6tc_handle *h;
const ip6t_chainlabel chain = "OUTPUT";
const char *tablename = "mangle";

struct ip6t_entry * e;
struct ip6t_entry_target * target;
struct xt_mark_tginfo2 *pmark;
unsigned int size_ip6t_entry, size_ip6t_entry_target,  size_pmark, total_length; 

size_ip6t_entry = XT_ALIGN(sizeof(struct ip6t_entry));
size_ip6t_entry_target = XT_ALIGN(sizeof(struct ip6t_entry_target));
size_pmark = XT_ALIGN(sizeof(struct xt_mark_tginfo2));

total_length =  size_ip6t_entry +  size_ip6t_entry_target + size_pmark ;


e = calloc(1, total_length);
if(e == NULL)
{
        printf("malloc failure");
        exit(1);
}

//offsets to the other bits:
//target struct begining
e->target_offset = size_ip6t_entry ;
//next "e" struct, end of the current one
e->next_offset = total_length;


char *temps = malloc(128);
temps = "2001:db8:222:2::";
inet_pton(AF_INET6, temps, &e->ipv6.src);
char *temps2 = malloc(128);
temps2 = "FFFF:FFFF:FFFF:FFFF::";
inet_pton(AF_INET6, temps2, &e->ipv6.smsk);
 //e->ipv6.proto = 58 ;
//strcpy(e->ipv6.iniface, "wlan1");


//target struct
target = (struct ip6t_entry_target *) e->elems; 
target->u.target_size = size_ip6t_entry_target;
strcpy(target->u.user.name, "MARK");

 pmark = (struct xt_mark_tginfo2 *) target->data;
 pmark->mark = 0x14;
 pmark->mask = 0xff;


h = ip6tc_init(tablename);
if ( !h )
{
      printf("Error initializing: %s\n", iptc_strerror(errno));
      exit(errno);
}

 int x = ip6tc_append_entry(chain, e, h);

if (!x)
{
        printf("Error append_entry: %s\n", iptc_strerror(errno));
        exit(errno);
}
printf("%s", target->data);
int y = ip6tc_commit(h);
if (!y)
{
        printf("Error commit: %s\n", iptc_strerror(errno));
        exit(errno);
}

exit(0);

有任何想法吗?
谢谢

最佳答案

迟到的回复,但想给潜在的求职者一个提示。
一些目标(也匹配)需要设置正确的版本,对于 xt_mark_tginfo2 我看到它必须是 1 或 2。
另一件事是条目(ip6t_entry)标志,例如
我一直在努力拒绝目标(ip6t_reject_info),后来我才发现必须将入口标志设置为 IP6T_F_PROTO。
我不完全确定,但也许可以从 xtables_target 结构中查看 .family 。
祝 iptc 图书馆战士好运;)

关于c - 执行 iptables 代码时出错 “Error commit: Protocol wrong type for socket”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23803128/

相关文章:

linux - Ubuntu 更新管理器的问题

c - flex 的链接器错误

c - Arduino C 不进入循环?

php - 将 HTML 转换为 RTF(HTML2RTF 转换器)

linux - 如何将两个单词添加到 bash 脚本变量

C++:使用选择()

reactjs - ReactJs + Electron-套接字连接仅在重新加载后才能工作

c - 用于通过有损串行链路重传的用户模式 ​​TCP 堆栈

c - 指针数组问题

c++ - 管理 C 套接字中客户端突然断开连接的好方法