opening raw udp socket in promiscuous mode c -
i trying open raw socket deal udp traffic. need promiscuous. have looked @ lots of resources totally confused. here code:
char interface_name[12]; strcpy(interface_name, "eth1"); sd = socket(af_inet, sock_raw, ipproto_udp); if ( sd == -1 ) { perror("error in opening socket."); return; } if ( setsockopt(sd, sol_socket, so_bindtodevice, interface_name, strlen(interface_name)) == -1 ) { perror("error in binding sd."); return; } int 1 = 1; if ( setsockopt (sd, ipproto_ip, ip_hdrincl, &one, sizeof(one)) < 0) { perror("error in setting hdrincl."); return; } struct ifreq ifr; memset(&ifr, 0, sizeof(ifr)); strcpy(ifr.ifr_name, interface_name); if (ioctl(sd, siocgifindex, &ifr) < 0) { perror("ioctl(siocgifindex) failed"); return; } int interface_index = ifr.ifr_ifindex; ifr.ifr_flags |= iff_promisc; if( ioctl(sd, siocsifflags, &ifr) != 0 ) { perror("ioctl iff_promisc failed."); return; } struct packet_mreq mr; memset(&mr, 0, sizeof(mr)); mr.mr_ifindex = interface_index; mr.mr_type = packet_mr_promisc; if (setsockopt(sd, sol_packet, packet_add_membership, &mr, sizeof(mr)) < 0) { perror("setsockopt(packet_mr_promisc) failed"); return 1; }
the final setsockopt fails error message "protocol not available". reason? suggestion?
it's because use sol_packet
used packet(7)
sockets. raw inet
sockets, don't need option.
Comments
Post a Comment