现在的位置: 首页 > 综合 > 正文

socket(AF_INET,SOCK_RAW,IPPROTO_IP)的意思

2018年09月15日 ⁄ 综合 ⁄ 共 1945字 ⁄ 字号 评论关闭

这是winsock2.h里的定义。

/*
* Protocols
*/
#define IPPROTO_IP               0                /* dummy for IP */
#define IPPROTO_HOPOPTS          0                /* IPv6 hop-by-hop options */
#define IPPROTO_ICMP 1 /* control message protocol */
#define IPPROTO_IGMP             2                /* internet group management protocol */
#define IPPROTO_GGP              3                /* gateway^2 (deprecated) */
#define IPPROTO_IPV4             4                /* IPv4 */
#define IPPROTO_TCP              6                /* tcp */
#define IPPROTO_PUP              12               /* pup */
#define IPPROTO_UDP              17               /* user datagram protocol */
#define IPPROTO_IDP              22               /* xns idp */
#define IPPROTO_IPV6             41               /* IPv6 */
#define IPPROTO_ROUTING          43               /* IPv6 routing header */
#define IPPROTO_FRAGMENT         44               /* IPv6 fragmentation header */
#define IPPROTO_ESP              50               /* IPsec ESP header */
#define IPPROTO_AH               51               /* IPsec AH */
#define IPPROTO_ICMPV6           58               /* ICMPv6 */
#define IPPROTO_NONE             59               /* IPv6 no next header */
#define IPPROTO_DSTOPTS          60               /* IPv6 destination options */
#define IPPROTO_ND               77               /* UNOFFICIAL net disk proto */
#define IPPROTO_ICLFXBM          78

#define IPPROTO_RAW              255              /* raw IP packet */
#define IPPROTO_MAX              256

--------------------------------------------------------------------------------------------------------

对于socket(AF_INET,SOCK_RAW,IPPROTO_IP),其原型为

SOCKET socket(
int
af
,
int
type
,
int
protocol,

);

  1   参数protocol用来指明所要接收的协议包,如果是象IPPROTO_TCP(6)这种非0、非255的协议,当操作系统内核碰到ip头中protocol域和创建socket所使用参数protocol相同的IP包,就会交给这个raw   socket来处理,因此,一般来说,要想接收什么样的数据包,就应该在参数protocol里来指定相应的协议。当内核向此raw   socket交付数据包的时候,是包括整个IP头的,并且已经是重组好的IP包。
 
2   如果protocol是IPPROTO_RAW(255),这时候,这个socket只能用来发送IP包,而不能接收任何的数据。发送的数据需要自己填充IP包头,并且自己计算校验和。  

3   对于protocol为0(IPPROTO_IP)的raw   socket。用于接收任何的IP数据包。其中的校验和和协议由自己完成。

 

抱歉!评论已关闭.