问题描述
- sk_buff和ethhdr结构体中怎么获得源mac地址?并将类型转换
-
请教各位大神,写的这段代码功能没有实现,求指教!static size_t _format_mac_addr(char *buf, int buflen,const unsigned char *addr, int len)//mac地址转换函数,转为 aa:aa:aa:aa:aa:aa 这类
{
int i;
char *cp = buf;
for (i = 0; i < len; i++) {
cp += scnprintf(cp, buflen - (cp - buf), "%02x", addr[i]);
if (i == len - 1)
break;
cp += scnprintf(cp, buflen - (cp - buf), ":");
}
return cp - buf;
}unsigned int hook_func(unsigned int hooknum,struct sk_buff *skb,const struct net_device *in,const struct net_device *out,int (*okfn)(struct sk_buff *))//获取源mac地址并和本机mac地址对比
{
char buffer[20];
u8 haddr[20];
struct ethhdr *eth;
eth=(struct ethhdr *)skb_mac_header(skb);
memcpy(haddr,eth->h_source,6);_format_mac_addr(buffer, 18, haddr, 6); if(strcmp(buffer,"b8:88:e3:72:31:44")==0) return NF_ACCEPT; else return NF_DROP;
}
解决方案
设置断点单步调试跟踪一下,看具体代码跑到哪里有问题
时间: 2025-01-02 08:51:18