- 论坛徽章:
- 0
|
原帖由 HJLin 于 2008-7-30 22:09 发表
下面的代码,能够使用,还是哪里我理解的有问题...
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "unistd.h"
#include "signal.h"
#include "sys/socket.h"
#include " ...
这就是AF_INET和AF_PACKET的区别了
使用AF_INET,就跟普通socekt一样,你能通过第三个参数来制定处理TCP,UDP或者ICMP等包
而使用AF_PACKET第三个参数指定为IPPROTO_TCP,IPPROTO_UDP, IPPROTO_ICMP等就收不到数据包,因为上文中已提到:
“as for AF_INET vs. AF_PACKET, well the former is the normal IP family, in
which you can have TCP, UDP, or raw sockets, while the latter is the Linux-specific
packet family, specifically designed for sniffing link-level packets off the wire”
AF_PACKET是专门用来处理raw socket的,符合我上面列举的规则
是否可以这样总结raw socket的用法:
(1):如果想单独处理TCP,UDP或者ICMP,可用AF_INET, 第三个参数用IPPROTO_TCP, IPPROTO_UDP等
(2):如果想对各种IP包进行处理,则用AF_PACKET/PF_PACKET,第三个参数用htons(ETH_P_IP)
(3):如果想处理一切链路层上的包(IP,RARP,ARP等),则用AF_PACKET/PF_PACKET,第三个参数用 htons(ETH_P_ALL)
[ 本帖最后由 duanjigang 于 2008-7-30 23:04 编辑 ] |
|