... int sockfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); struct sockaddr_ll sll; memset( &sll, 0, sizeof(sll) ); sll.sll_family = PF_PACKET; struct ifreq ifstruct; strcpy(ifstruct.ifr_name, "eth0"); ioctl(sockfd, SIOCGIFINDEX, &ifstruct); sll.sll_ifindex = ifstruct.ifr_ifindex; sll.sll_protocol = htons(ETH_P_ALL); if(bind(fd, (struct sockaddr *) &sll, sizeof(sll)) == -1 ) { perror("bind()"); ... |
int set_promisc(char *interface, int fd) { struct ifreq ifr; strcpy(ifr.ifr_name, interface); if(ioctl(fd, SIOCGIFFLAGS, &ifr) == -1) { perror("iotcl()"); return -1; } ifr.ifr_flags |= IFF_PROMISC; if(ioctl(fd, SIOCSIFFLAGS, &ifr) == -1) { perror("iotcl()"); return -1; } return 0; } int unset_promisc(char *interface, int fd) { struct ifreq ifr; strcpy(ifr.ifr_name, interface); if(ioctl(fd, SIOCGIFFLAGS, &ifr) == -1) { perror("iotcl()"); return -1; } ifr.ifr_flags &= ~IFF_PROMISC; if(ioctl(fd, SIOCSIFFLAGS, &ifr) == -1) { perror("iotcl()"); return -1; } return 0; } |
通信人家园 (https://www.txrjy.com/) | Powered by C114 |