1、通過 socket函數創建原始套接字 int sock_raw_fd =socket(PF_PACKET, SOCK_RAW, ETH_P_ALL)
2、組裝數據包msg(按照協議格式進行組裝)
3、指定網卡 struct ifreq ethreq ; strncpy (ethreq.ifr_name , "eth0",IFNAMSIZ); if (-1 == ioctl(sock_raw_fd, SIOCGIFINDEX,ðreq)){ perror ( "ioctl") ;close ( sock_raw_fd) ;exit (-1) ; } struct sockaddr_ll sll; bzero (&sll,sizeof (sll)); sll.sll ifindex = ethreq.ifr ifindex;
4、發送數據 sendto(sock_raw_fd, msg, msg_len, 0,(struct sockaddr*)&sll, sizeof(sll));
5、關閉套接字 close ( sock_raw_fd) ;