[Javanetsim-cvs] IceScan/icesockets sock_utils.cc, NONE, 1.1 sock_utils.h, NONE, 1.1 cethwrapper.h,
Status: Beta
Brought to you by:
darkkey
From: Alexander B. <da...@us...> - 2006-12-21 16:29:40
|
Update of /cvsroot/javanetsim/IceScan/icesockets In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv4099/icesockets Modified Files: crawsocket.h sock_pcap.h sock_name.h sock_name.cc csocket.h Added Files: sock_utils.cc sock_utils.h cethwrapper.h Removed Files: sock_pcap.cc sock_arp.h ice_rand.h sock_files.h sock_time.h sock_eth.cc sock_eth.h sock_time.cc sock_files.cc ice_rand.cc sock_arp.cc Log Message: no message --- ice_rand.cc DELETED --- --- sock_pcap.cc DELETED --- --- sock_arp.h DELETED --- --- sock_eth.h DELETED --- --- sock_eth.cc DELETED --- --- sock_time.cc DELETED --- Index: sock_name.h =================================================================== RCS file: /cvsroot/javanetsim/IceScan/icesockets/sock_name.h,v retrieving revision 1.16 retrieving revision 1.17 diff -C2 -d -r1.16 -r1.17 *** sock_name.h 21 Dec 2006 15:37:23 -0000 1.16 --- sock_name.h 21 Dec 2006 16:29:36 -0000 1.17 *************** *** 12,16 **** #include "sock_types.h" ! #include "sock_eth.h" #include "iceregex.h" --- 12,16 ---- #include "sock_types.h" ! #include "cethwrapper.h" #include "iceregex.h" *************** *** 31,33 **** --- 31,37 ---- short int get_host_byip(icestring &nisname, icestring &retname); + icestring get_source_ip(const icestring &destip, icestring &sourceint); + + icestring get_arp_from_cache(icestring &ip); + #endif --- sock_time.h DELETED --- --- sock_files.h DELETED --- --- sock_arp.cc DELETED --- Index: crawsocket.h =================================================================== RCS file: /cvsroot/javanetsim/IceScan/icesockets/crawsocket.h,v retrieving revision 1.19 retrieving revision 1.20 diff -C2 -d -r1.19 -r1.20 *** crawsocket.h 17 Dec 2006 18:49:55 -0000 1.19 --- crawsocket.h 21 Dec 2006 16:29:36 -0000 1.20 *************** *** 3,7 **** #include "csocket.h" ! #include "sock_eth.h" #include "sock_types.h" --- 3,7 ---- #include "csocket.h" ! #include "cethwrapper.h" #include "sock_types.h" *************** *** 198,206 **** if(ethernet){ ! res = send_ip_packet(packet, ntohs(ip->tot_len)); }else if ((res = sendto(destination.c_str(), packet, ntohs(ip->tot_len), 0)) == -1) { ! perror("sendto in send_tcp_raw"); ! #ifdef WIN32 free(packet); #endif --- 198,209 ---- if(ethernet){ ! #ifdef HAVE_LIBDNET ! cethwrapper ew; ! res = ew.send_ip_packet(packet, ntohs(ip->tot_len)); ! #endif }else if ((res = sendto(destination.c_str(), packet, ntohs(ip->tot_len), 0)) == -1) { ! perror("sendto in send_tcp_raw"); ! #ifdef WIN32 free(packet); #endif *************** *** 208,212 **** } ! #ifdef WIN32 free(packet); #endif --- 211,215 ---- } ! #ifdef WIN32 free(packet); #endif *************** *** 240,294 **** return sendto(hostname, buftosend, len, 0); } - - // from Fyodor's "The Art of Port Scanning" - /*static int print_tcppacket(char *packet, int readdata) { - struct iphdr *ip = (struct iphdr *) packet; - struct tcphdr *tcp = (struct tcphdr *) (packet + sizeof(struct iphdr)); - char *data = packet + sizeof(struct iphdr) + sizeof(struct tcphdr); - int tot_len; - struct in_addr bullshit, bullshit2; - char sourcehost[16]; - int i; - - if (!packet) { - std::cerr << "readtcppacket: packet is NULL!\n"; - return -1; - } - - bullshit.s_addr = ip->saddr; bullshit2.s_addr = ip->daddr; - tot_len = ntohs(ip->tot_len); - strncpy(sourcehost, inet_ntoa(bullshit), 16); - i = 4 * (ntohs(ip->ihl) + ntohs(tcp->th_off)); - if (ip->protocol == IPPROTO_TCP){ - if (ip->frag_off){ - printf("Packet is fragmented, offset field: %u", ip->frag_off); - } else { - printf("TCP packet: %s:%d -> %s:%d (total: %d bytes)\n", sourcehost, - ntohs(tcp->th_sport), inet_ntoa(bullshit2), - ntohs(tcp->th_dport), tot_len); - printf("Flags: "); - if (!tcp->th_flags) printf("(none)"); - if (tcp->th_flags & TH_RST) printf("RST "); - if (tcp->th_flags & TH_SYN) printf("SYN "); - if (tcp->th_flags & TH_ACK) printf("ACK "); - if (tcp->th_flags & TH_PUSH) printf("PSH "); - if (tcp->th_flags & TH_FIN) printf("FIN "); - if (tcp->th_flags & TH_URG) printf("URG "); - printf("\n"); - printf("ttl: %hi ", ip->ttl); - if (tcp->th_flags & (TH_SYN | TH_ACK)) printf("Seq: %lu\tAck: %lu\n", - tcp->th_seq, tcp->th_ack); - else if (tcp->th_flags & TH_SYN) printf("Seq: %lu\n", ntohl(tcp->th_seq)); - else if (tcp->th_flags & TH_ACK) printf("Ack: %lu\n", ntohl(tcp->th_ack)); - } - } - if (readdata && i < tot_len) { - printf("Data portion:\n"); - while(i < tot_len) printf("%2X%c", data[i], (++i%16)? ' ' : '\n'); - printf("\n"); - } - - return 0; - } */ }; --- 243,246 ---- Index: csocket.h =================================================================== RCS file: /cvsroot/javanetsim/IceScan/icesockets/csocket.h,v retrieving revision 1.24 retrieving revision 1.25 diff -C2 -d -r1.24 -r1.25 *** csocket.h 17 Dec 2006 15:06:37 -0000 1.24 --- csocket.h 21 Dec 2006 16:29:36 -0000 1.25 *************** *** 7,11 **** #include "sock_name.h" #include "sock_time.h" - #include "sock_arp.h" #ifdef WIN32 --- 7,10 ---- *************** *** 200,204 **** if (remoteport) *remoteport = ntohs(sraddress.sin_port); return csocket(domain, type, resultid); ! }else return NULL; } --- 199,203 ---- if (remoteport) *remoteport = ntohs(sraddress.sin_port); return csocket(domain, type, resultid); ! }else csocket(domain, type, -1); } --- NEW FILE: sock_utils.h --- #ifndef ICESOCK_SOCK_TIME_H #define ICESOCK_SOCK_TIME_H #include "sock_types.h" #ifdef HAVE_SYS_TIME_H #include <sys/time.h> #endif #define SUB_TIMEVALS(a,b) (((a).tv_sec - (b).tv_sec) * 1000000 + (a).tv_usec - (b).tv_usec) #ifdef WIN32 #define usleep(t) Sleep((t) / 1000) #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) || defined(__WATCOMC__) #define DELTA_EPOCH_IN_USEC 11644473600000000Ui64 #else #define DELTA_EPOCH_IN_USEC 11644473600000000ULL #endif static u_int64_t filetime_to_unix_epoch (const FILETIME *ft); int gettimeofday (struct timeval *tv, void *tz_U_); #endif void iceusleep(unsigned long); void icesleep(int, int); float timeb_diff(timeb &, timeb &); icestring make_uptime(long); bool file_exists(icestring &fname); icestring find_icefile_path(char *fname); // from Fyodor's "The Art of Port Scanning" int get_random_bytes(void *buf, int numbytes); void init_rand(); #endif Index: sock_name.cc =================================================================== RCS file: /cvsroot/javanetsim/IceScan/icesockets/sock_name.cc,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** sock_name.cc 21 Dec 2006 15:37:23 -0000 1.1 --- sock_name.cc 21 Dec 2006 16:29:36 -0000 1.2 *************** *** 99,101 **** --- 99,227 ---- } + bool islocal(icestring &ip){ + + } + + icestring get_source_ip(const icestring &destip, icestring &sourceint){ + #ifndef HAVE_LIBDNET + char local_hostname[80]; + if(gethostname(local_hostname, 80)) exit(-1); + + sourceint.assign("null"); //unfortunately, no pcap in cygwin + + icestring ret(local_hostname); + + return ret; + #else + + cethwrapper ew; + + return ew.get_source_ip(destip, sourceint); + + #endif + } + + icestring get_arp_from_cache(icestring &ip) + { + char arp[24]; + + + #ifdef __CYGWIN__ + return ""; + #elif HAVE_LIBDNET + struct arp_entry ae; + + addr_pton(ip.c_str(), &ae.arp_pa); + + arp_t *p = arp_open(); + + arp_get(p, &ae); + + arp_close(p); + + struct sockaddr sa; + + addr_ntos(&(ae.arp_ha), &sa); + + sprintf(arp, "%02x:%02x:%02x:%02x:%02x:%02x", + sa.sa_data[0] & 0xFF, + sa.sa_data[1] & 0xFF, + sa.sa_data[2] & 0xFF, + sa.sa_data[3] & 0xFF, + sa.sa_data[4] & 0xFF, + sa.sa_data[5] & 0xFF); + + icestring ret(arp); + + return ret; + #elif WIN32 && !__WITH_DNET__ + PMIB_IPNETTABLE pIPNetTable = NULL; + PMIB_IPNETROW pIPNetRow = NULL; + ULONG ulSize = 0; + IN_ADDR ia; + + GetIpNetTable(pIPNetTable, &ulSize, TRUE); + pIPNetTable = new MIB_IPNETTABLE[ulSize]; + if (NULL != pIPNetTable) + { + GetIpNetTable(pIPNetTable, &ulSize, TRUE); + + for (int x = 0; x < pIPNetTable->dwNumEntries; x++) + { + pIPNetRow = &(pIPNetTable->table[x]); + ia.S_un.S_addr = pIPNetRow->dwAddr; + + if(!strcmp(inet_ntoa(ia), ip.c_str())){ + sprintf(arp, "%02x:%02x:%02x:%02x:%02x:%02x", + pIPNetRow->bPhysAddr[0] & 0xFF, + pIPNetRow->bPhysAddr[1] & 0xFF, + pIPNetRow->bPhysAddr[2] & 0xFF, + pIPNetRow->bPhysAddr[3] & 0xFF, + pIPNetRow->bPhysAddr[4] & 0xFF, + pIPNetRow->bPhysAddr[5] & 0xFF); + + icestring ret(arp); + + return ret; + } + } + + delete [] pIPNetTable; + + return ""; + } + #elif __linux__ && !__WITH_DNET__ + struct sockaddr_in sin = { 0 }; + struct arpreq myarp = { { 0 } }; + int sockfd; + + sin.sin_family = AF_INET; + inet_aton(ip.c_str(), &sin.sin_addr); + + memcpy(&myarp.arp_pa, &sin, sizeof myarp.arp_pa); + strcpy(myarp.arp_dev, "any"); //UGLY + + if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { + perror("socket"); + return ""; + } + + if (ioctl(sockfd, SIOCGARP, &myarp) == -1) { + return ""; + } + + sprintf(arp, "%02X:%02X:%02X:%02X:%02X:%02X", myarp.arp_ha.sa_data[0] & 0xFF, + myarp.arp_ha.sa_data[1] & 0xFF, + myarp.arp_ha.sa_data[2] & 0xFF, + myarp.arp_ha.sa_data[3] & 0xFF, + myarp.arp_ha.sa_data[4] & 0xFF, + myarp.arp_ha.sa_data[5] & 0xFF); + icestring ret(arp); + + return ret; + #else + return ""; + #endif + } + --- NEW FILE: sock_utils.cc --- #include "sock_time.h" #ifdef WIN32 static u_int64_t filetime_to_unix_epoch (const FILETIME *ft) { u_int64_t res = (u_int64_t) ft->dwHighDateTime << 32; res |= ft->dwLowDateTime; res /= 10; res -= DELTA_EPOCH_IN_USEC; return (res); } int gettimeofday (struct timeval *tv, void *tz_U_) { FILETIME ft; u_int64_t tim; if (!tv) { errno = EINVAL; return (-1); } GetSystemTimeAsFileTime (&ft); tim = filetime_to_unix_epoch (&ft); tv->tv_sec = (long) (tim / 1000000L); tv->tv_usec = (long) (tim % 1000000L); return (0); } #endif void iceusleep(unsigned long usec){ usleep(usec); } void icesleep(int secs, int nsecs = 0){ #ifndef WIN32 struct timespec ts; ts.tv_sec = secs; ts.tv_nsec = nsecs; nanosleep (&ts, NULL); #else usleep(secs + nsecs * 1000); #endif } float timeb_diff(timeb &tp1, timeb &tp2){ float dt; int secs = tp2.time - tp1.time; int msecs = tp2.millitm - tp1.millitm; if(msecs < 0){ secs--; msecs = 1000 + msecs; } dt = secs + 0.001*msecs; return dt; } icestring make_uptime(long t){ char buf[1024]; int msecs = t % (1000); int secs = t/1000 % (60); int mins = t/1000/60 % (60); int hours = t/1000/60/60; double fsecs = secs + (double) msecs/1000; sprintf(buf, "%d hours %d minutes %.3f seconds", hours, mins, fsecs); icestring ret(buf); return ret; } bool file_exists(icestring &fname){ std::ifstream in; in.open(fname.c_str()); bool result = in.is_open(); in.close(); return result; } icestring find_icefile_path(char *fname){ icestring ret; // current dir ret.assign(fname); if(file_exists(ret)) return ret; // datadir ret.assign(""); ret.append(ICESCAN_DATADIR); ret.push_back(TRAILING_SLASH); ret.append(fname); if(file_exists(ret)) return ret; // --data_dir // add some code here // default ret.assign(""); return ret; } // from Fyodor's "The Art of Port Scanning" int get_random_bytes(void *buf, int numbytes) { static char bytebuf[2048]; static char badrandomwarning = 0; static int bytesleft = 0; int tmp; int res; struct timeval tv; FILE *fp = NULL; unsigned int i; short *iptr; if (numbytes < 0 || numbytes > 0xFFFF) return -1; if (bytesleft == 0) { fp = fopen("/dev/arandom", "r"); if (!fp) fp = fopen("/dev/urandom", "r"); if (!fp) fp = fopen("/dev/random", "r"); if (fp) { res = (int) fread(bytebuf, 1, sizeof(bytebuf), fp); if (res != sizeof(bytebuf)) { printf("Failed to read from /dev/urandom or /dev/random\n"); fclose(fp); fp = NULL; } bytesleft = sizeof(bytebuf); } if (!fp) { if (badrandomwarning == 0) { badrandomwarning++; gettimeofday(&tv, NULL); srand((tv.tv_sec ^ tv.tv_usec) ^ getpid()); } for(i=0; i < sizeof(bytebuf) / sizeof(short); i++) { iptr = (short *) ((char *)bytebuf + i * sizeof(short)); *iptr = rand(); } bytesleft = (sizeof(bytebuf) / sizeof(short)) * sizeof(short); } else fclose(fp); } if (numbytes <= bytesleft) { /* we can cover it */ memcpy(buf, bytebuf + (sizeof(bytebuf) - bytesleft), numbytes); bytesleft -= numbytes; return 0; } memcpy(buf, bytebuf + (sizeof(bytebuf) - bytesleft), bytesleft); tmp = bytesleft; bytesleft = 0; return get_random_bytes((char *)buf + tmp, numbytes - tmp); } void init_rand(){ unsigned i; get_random_bytes(&i, sizeof(i)); srand(i); } --- ice_rand.h DELETED --- Index: sock_pcap.h =================================================================== RCS file: /cvsroot/javanetsim/IceScan/icesockets/sock_pcap.h,v retrieving revision 1.11 retrieving revision 1.12 diff -C2 -d -r1.11 -r1.12 *** sock_pcap.h 21 Dec 2006 15:37:23 -0000 1.11 --- sock_pcap.h 21 Dec 2006 16:29:36 -0000 1.12 *************** *** 16,35 **** #endif ! pcap_t* init_pcap(int); ! pcap_t* start_pcap(int, bool, char); ! void listdev_pcap(); ! char* readip_pcap(pcap_t*, unsigned int *, struct timeval *); ! bool pcap_filter(pcap_t *, char *); ! int getdev_pcap(icestring &); ! int pcap_block(pcap_t *, bool); ! void close_pcap(pcap_t *); #endif /* _sock_pcap_H */ --- 16,301 ---- #endif ! class cpcapreader{ ! pcap_t* p; ! ! public: ! ! cpcapreader(int iface, bool block, char *filter_exp = NULL){ ! p = init_pcap(iface); ! pcap_block(p, block); ! if(filter_exp) pcap_filter(p, filter_exp); ! } ! ! void block(bool block){ ! pcap_block(p, block); ! } ! ! bool opened(){ ! return p ? true : false; ! } ! ! char* read_packet(unsigned int *len, struct timeval *rcvd_time){ ! return readip_pcap(p, len, rcvd_time); ! } ! ! static void listdev_pcap(){ ! #ifdef HAVE_LIBPCAP ! pcap_if_t *alldevs; ! pcap_if_t *d; ! int i=0; ! char errbuf[PCAP_ERRBUF_SIZE]; ! ! if(pcap_findalldevs(&alldevs, errbuf) == -1) ! { ! printf("Error in pcap_findalldevs: %s\n", errbuf); ! exit(1); ! } ! ! for(d=alldevs; d; d=d->next) ! { ! printf("%d. %s", ++i, d->name); ! if (d->description) ! printf(" (%s)\n", d->description); ! else ! printf(" (No description available)\n"); ! } ! ! pcap_freealldevs(alldevs); ! if(i==0) ! { ! printf("\nNo interfaces found! Make sure WinPcap is installed.\n"); ! return; ! } ! #endif ! } ! static int getdev_pcap(icestring &dev){ ! #ifdef HAVE_LIBPCAP ! pcap_if_t *alldevs; ! pcap_if_t *d; ! int i=1; ! char errbuf[PCAP_ERRBUF_SIZE]; ! if(pcap_findalldevs(&alldevs, errbuf) == -1) ! { ! printf("Error in pcap_findalldevs: %s\n", errbuf); ! exit(1); ! } ! for(d=alldevs; d; d=d->next, i++) ! { ! if(!strcmp(d->name, dev.c_str())) break; ! } ! if(!d) (i = 0); ! pcap_freealldevs(alldevs); ! ! if(i==0) ! printf("\nNo interfaces found! Make sure WinPcap is installed.\n"); ! ! return i; ! #else ! return 0; ! #endif ! } ! ! ~cpcapreader(){ ! close_pcap(p); ! } ! ! private: ! ! #ifndef HAVE_LIBPCAP ! ! pcap_t* init_pcap(int index = 0){ ! return NULL; ! } ! ! char* readip_pcap(pcap_t* pd, unsigned int *len, struct timeval *rcvd_time){ ! return NULL; ! } ! ! bool pcap_filter(pcap_t *p, char *filter_exp){ ! return true; ! } ! ! int pcap_block(pcap_t *p, bool block){ ! } ! ! void close_pcap(pcap_t *p){ ! } ! ! #else ! ! pcap_t* init_pcap(int index = 0){ ! char *dev; ! char errbuf[PCAP_ERRBUF_SIZE]; ! pcap_t* descr; ! pcap_if_t *alldevs; ! pcap_if_t *d; ! int i=0; ! ! if(index){ ! ! if(pcap_findalldevs(&alldevs, errbuf) == -1) ! { ! printf("Error in pcap_findalldevs: %s\n", errbuf); ! exit(1); ! } ! ! for(d=alldevs; d; d=d->next) ! i++; ! ! if(i==0) ! { ! printf("\nNo interfaces found! Make sure WinPcap/LibPcap is installed.\n"); ! pcap_freealldevs(alldevs); ! return NULL; ! } + for(d=alldevs, i=0; i< index-1 ;d=d->next, i++); + + pcap_t *dev = pcap_open_live(d->name,BUFSIZ,0,-1,errbuf); + + pcap_freealldevs(alldevs); + + return dev; + }else{ + dev = pcap_lookupdev(errbuf); + + if(dev == NULL) + { + return NULL; + } + + return pcap_open_live(dev,BUFSIZ,0,-1,errbuf); + } + + return NULL; + } + + + char* readip_pcap(pcap_t* pd, unsigned int *len, struct timeval *rcvd_time){ + unsigned int offset = 0; + struct pcap_pkthdr head; + char *p; + static char *alignedbuf = NULL; + static unsigned int alignedbufsz = 0; + int link; + + link = pcap_datalink(pd); + + switch(link){ + case DLT_EN10MB: offset = 14; break; + + case DLT_IEEE802: offset = 22; break; + + #ifdef DLT_LOOP + case DLT_LOOP: + #endif + case DLT_NULL: offset = 4; break; + + case DLT_SLIP: + #if (FREEBSD || OPENBSD || NETBSD || BSDI || MACOSX) + offset = 16; + #else + offset = 24; + #endif + break; + case DLT_PPP: + #ifdef DLT_PPP_BSDOS + case DLT_PPP_BSDOS: + #endif + #ifdef DLT_PPP_SERIAL + case DLT_PPP_SERIAL: + #endif + #ifdef DLT_PPP_ETHER + case DLT_PPP_ETHER: + #endif + #if (FREEBSD || OPENBSD || NETBSD || BSDI || MACOSX) + offset = 4; + #else + #ifdef SOLARIS + offset = 8; + #else + offset = 24; + #endif /* solaris */ + #endif /* freebsd || openbsd || netbsd || bsdi */ + break; + case DLT_RAW: offset = 0; break; + case DLT_FDDI: offset = 21; break; + #ifdef DLT_ENC + case DLT_ENC: offset = 12; break; + #endif /* DLT_ENC */ + #ifdef DLT_LINUX_SLL + case DLT_LINUX_SLL: offset = 16; break; + #endif + default: + err_die("FATAL: Unknown datalink type!", 0); + exit(1); + } + + p = (char *) pcap_next(pd, &head); + + if(p){ + if(head.caplen <= offset){ + *len = 0; + return NULL; + } + p += offset; + }else return NULL; + + *len = head.caplen - offset; + + if (*len > alignedbufsz) { + alignedbuf = (char *) realloc(alignedbuf, *len); + if (!alignedbuf) { + err_die("Unable to realloc mem!", 0); + } + alignedbufsz = *len; + } + memcpy(alignedbuf, p, *len); + + if (rcvd_time) { + #if defined(WIN32) + struct timeval tv_end; + + gettimeofday(&tv_end, NULL); + *rcvd_time = tv_end; + #else + rcvd_time->tv_sec = head.ts.tv_sec; + rcvd_time->tv_usec = head.ts.tv_usec; + #endif + } + + return alignedbuf; + } + + + bool pcap_filter(pcap_t *p, char *filter_exp){ + struct bpf_program fp; /* The compiled filter expression */ + + + if (pcap_compile(p, &fp, filter_exp, 0, 0) == -1) { + return false; + } + if (pcap_setfilter(p, &fp) == -1) { + return false; + } + return true; + } + + int pcap_block(pcap_t *p, bool block){ + return pcap_setnonblock(p, (int)(!block), NULL); + } + + void close_pcap(pcap_t *p){ + pcap_close(p); + } + #endif + }; #endif /* _sock_pcap_H */ --- NEW FILE: cethwrapper.h --- (This appears to be a binary file; contents omitted.) --- sock_files.cc DELETED --- |