查看手册man 7 socket
sockfd = socket(int socket_family, int socket_type, int protocol);
查看手册man 7 ip
tcp_socket = socket(AF_INET, SOCK_STREAM, 0);
udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
raw_socket = socket(AF_INET, SOCK_RAW, protocol);
查看手册man 7 raw
raw_socket = socket(AF_INET, SOCK_RAW, int protocol);
SOCK_RAW接收时包含IP头
若socket设置了IP_HDRINCL选项,发送时报文中需要填写IP头
socket(AF_INET, SOCK_RAW, IPPROTO_TCP);收取TCP报文
socket(AF_INET, SOCK_RAW, IPPROTO_UDP);收取UDP报文
socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);收取ICMP报文
查看手册man 7 packet
packet_socket = socket(AF_PACKET, int socket_type, int protocol);
SOCK_RAW接收包含MAC头
SOCK_DGRAM接收不包含MAC头
socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
socket(AF_PACKET, SOCK_RAW, htons(ETH_P_IP));
socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ARP));
socket(AF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL));
socket(AF_PACKET, SOCK_PACKET, htons(ETH_P_ALL));
AF_INET/SOCK_PACKET已不再使用
内核代码__sock_create遇到(PF_INET,SOCK_PACKET)会修改为(PF_PACKET,SOCK_PACKET)
SOCK_PACKET使用的结构体不一样
查看手册man 7 udplite
sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDPLITE);
查看手册man 7 rtnetlink
rtnetlink_socket = socket(AF_NETLINK, int socket_type, NETLINK_ROUTE);