1、通過 socket函數(shù)創(chuàng)建原始套接字 int sock_raw_fd =socket(PF_PACKET, SOCK_RAW, ETH_P_ALL)
2、組裝數(shù)據(jù)包msg(按照協(xié)議格式進(jìn)行組裝)
3、指定網(wǎng)卡 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、發(fā)送數(shù)據(jù) sendto(sock_raw_fd, msg, msg_len, 0,(struct sockaddr*)&sll, sizeof(sll));
5、關(guān)閉套接字 close ( sock_raw_fd) ;