Есть код:
int main (int argc, char **argv, char **envp) {
int sock; // socket descriptor;
int i,rc; // return code
int promisc = 0; // promiscious flag
int lo = 0; // localhost flag
struct ifreq interface; // interface structure
signal(SIGINT,quit);
signal(SIGQUIT,quit);
signal(SIGTERM,quit);
printf("Tiny demo sniffer v0.0 by [Privacy], 2002\n");
if(argc<2) {
printf("Usage: %s <-i interface> [-p]\n\t-i\tphysical interface name or \"localhost\" for loopback\n\t-p\tuse this option to put a device into promiscious mode\n",argv[0]);
printf("Example:\t%s -i eth0 -p\n\t\t%s -i localhost\n",argv[0],argv[0]);
return 1;
}
device[0] = 0;
for( i=1; i<argc; i++ ) {
if( !memcmp( argv[i], "-i", 2 ) ) {
if( argv[++i] ) {
strcpy( device, argv[i] );
} else {
printf("Exiting: -i parameter is omitted!\n");
return 1;
}
}
if( !memcmp( argv[i], "-p", 2 ) ) {
promisc = 1;
}
}
if( !device[0] ) {
printf("Exiting: no device entered!\n");
return 1;
}
if( !strcmp( device, "localhost" ) ) {
lo = 1;
}
if( lo && promisc ) {
printf("Cannot put loopback device into promiscious mode!\n");
return 1;
}
// allocating a socket
sock = socket( PF_PACKET, /*SOCK_RAW*/SOCK_PACKET, htons( ETH_P_ALL ) );
if(sock < 0) {
printf("Exiting: failed allocating a socket!\n");
return 1;
}
// putting a device into non-blocking mode, if required
if( promisc ) {
strcpy(interface.ifr_name,device);
if(ioctl(sock,SIOCGIFFLAGS,&interface) < 0) {
printf("failed reading data for %s!\n",device);
return 1;
}
interface.ifr_flags |= IFF_PROMISC;
// setting promiscious flag here
if(ioctl(sock,SIOCSIFFLAGS,&interface) < 0) {
printf("failed setting data for %s!\n",device);
return 1;
}
}
// binding a socket to device
if(!lo) {
rc = setsockopt(sock,SOL_SOCKET,SO_BINDTODEVICE,device,strlen(device)+1);
if(rc == -1) {
printf("Exiting: failed binding socket to %s\n",device);
return 1;
}
}
printf("Listening on %s\n",device);
memset( buf_send, 0, sizeof( buf_send ) );
buf_send[0] = 0x08;
buf_send[1] = 0x00;
buf_send[2] = 0x27;
buf_send[3] = 0xA3;
buf_send[4] = 0x12;
buf_send[5] = 0x55;
buf_send[6] = 0x08;
buf_send[7] = 0x00;
buf_send[8] = 0x27;
buf_send[9] = 0xA3;
buf_send[10] = 0x12;
buf_send[11] = 0x55;
buf_send[12] = 0x01;
if( lo ) {
while( !enough ) {
rc = recvfrom( sock, buf, sizeof( buf ), 0, 0, 0 );
if( !memcmp( buf, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 12 ) ) {
printf( "\npacket #%u\n", packets++ );
decode( buf, rc, out );
printf( "%s\n", out );
hexout( buf, rc, out );
printf( "%s\n", out );
}
}
} else {
while( !enough ) {
rc = sendto( sock, buf_send, sizeof( buf_send ), 0, 0, 0 );
printf( "send=%d : %s\n", rc, strerror( errno ) );
}
}
if( promisc ) {
interface.ifr_flags &= ~IFF_PROMISC;
if( ioctl( sock, SIOCSIFFLAGS, &interface ) < 0 ) {
printf("failed wiping IFF_PROMISC flag!\n");
return 1;
}
}
return 0;
}
почему sendto() не работает? пишет, что нет конечно точки для транспортировки пакетов.