searchnet.c (2114B)
1#include <stdio.h> 2#include "ping.h" 3#include <sys/socket.h> 4#include <netinet/in.h> 5#include <arpa/inet.h> 6#include <stdint.h> 7#include <stdlib.h> 8#include "resolve.h" 9#include "rawdraw/os_generic.h" 10 11uint32_t my_random_key; 12uint8_t send_id[4]; 13 14extern float pingperiodseconds; 15 16void * PingListen( void * r ) 17{ 18 listener(); 19 printf( "Fault on listen.\n" ); 20 exit( -2 ); 21} 22 23void display(uint8_t *buf, int bytes) 24{ 25 uint32_t reqid = ((uint32_t)buf[0+1] << 24) | (buf[1+1]<<16) | (buf[2+1]<<8) | (buf[3+1]); 26 27 if( reqid != my_random_key ) return; 28 29 printf( "%d.%d.%d.%d\n", buf[4+1], buf[5+1], buf[6+1], buf[7+1] ); 30} 31 32int load_ping_packet( uint8_t * buffer, int bufflen ) 33{ 34 buffer[0+1] = my_random_key >> 24; 35 buffer[1+1] = my_random_key >> 16; 36 buffer[2+1] = my_random_key >> 8; 37 buffer[3+1] = my_random_key >> 0; 38 39 buffer[4+1] = send_id[0]; 40 buffer[5+1] = send_id[1]; 41 buffer[6+1] = send_id[2]; 42 buffer[7+1] = send_id[3]; 43 44 return 12; 45} 46 47 48int main( int argc, char ** argv ) 49{ 50 uint32_t offset; 51 int mask; 52 in_addr_t base; 53 char dispip[32]; 54 float speed; 55 56 ping_setup( 0, 0); 57 OGCreateThread( PingListen, 0 ); 58 srand( ((int)(OGGetAbsoluteTime()*10000)) ); 59 my_random_key = rand(); 60 61 if( argc != 4 ) 62 { 63 fprintf( stderr, "Usage: [searchnet IP net] [mask (single #, i.e. 24)] [speed (in seconds per attempt)]\n" ); 64 return -1; 65 } 66 67 base = ntohl(inet_addr( argv[1] )); 68 mask = 1<<(32-atoi(argv[2])); 69 speed = atof(argv[3]); 70 71 base &= ~(mask-1); 72 printf( "Base: %08x / Mask: %08x\n", base, mask ); 73 for( offset = 0; offset < mask; offset++ ) 74 { 75 uint32_t cur = base + offset; 76 sprintf( dispip, "%d.%d.%d.%d", (cur>>24)&0xff, (cur>>16)&0xff, (cur>>8)&0xff, (cur)&0xff ); 77 send_id[0] = (cur>>24)&0xff; 78 send_id[1] = (cur>>16)&0xff; 79 send_id[2] = (cur>>8)&0xff; 80 send_id[3] = (cur)&0xff; 81// printf( "Pinging: %s\n", dispip ); 82 pingperiodseconds = -1; 83 84 struct sockaddr_in6 psaddr; 85 socklen_t psaddr_len = sizeof(psaddr); 86 resolveName((struct sockaddr*) &psaddr, &psaddr_len, dispip, AF_UNSPEC); 87 singleping((struct sockaddr*) &psaddr, psaddr_len ); 88 89 OGUSleep( (int)(speed * 1000000) ); 90 } 91 92 return 0; 93} 94