[Javanetsim-cvs] IceScan/icesockets sock_pcap.cc, NONE, 1.1 sock_win.cc, NONE, 1.1 sock_eth.cc, NON
Status: Beta
Brought to you by:
darkkey
From: Alexander B. <da...@us...> - 2006-12-21 15:37:27
|
Update of /cvsroot/javanetsim/IceScan/icesockets In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv15392/icesockets Modified Files: sock_win.h sock_arp.h sock_pcap.h sock_name.h ice_rand.h sock_files.h sock_time.h sock_eth.h Added Files: sock_pcap.cc sock_win.cc sock_eth.cc sock_name.cc sock_time.cc sock_files.cc ice_rand.cc sock_arp.cc Log Message: no message --- NEW FILE: ice_rand.cc --- #include "ice_rand.h" // 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)) { fprintf(stderr, "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); } --- NEW FILE: sock_pcap.cc --- #include "sock_pcap.h" #ifndef HAVE_LIBPCAP pcap_t* init_pcap(int index = 0){ return NULL; } pcap_t* start_pcap(int iface, bool block, char *filter_exp){ return NULL; } void listdev_pcap(){ } 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 getdev_pcap(icestring &dev){ return 0; } 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; } void listdev_pcap(){ 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; } } int getdev_pcap(icestring &dev){ 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; } 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); } pcap_t* open_pcap(int iface, bool block, char *filter_exp){ pcap_t* p = init_pcap(iface); if(!p) return NULL; pcap_block(p, block); pcap_filter(p, filter_exp); return p; } void close_pcap(pcap_t *p){ pcap_close(p); } #endif Index: sock_arp.h =================================================================== RCS file: /cvsroot/javanetsim/IceScan/icesockets/sock_arp.h,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** sock_arp.h 21 Dec 2006 10:13:53 -0000 1.7 --- sock_arp.h 21 Dec 2006 15:37:23 -0000 1.8 *************** *** 11,117 **** #include "sock_types.h" ! ! icestring get_arp_from_cache(icestring &ip) ! { ! char arp[24]; ! ! ! #ifdef __CYGWIN__ ! return ""; ! #elif __WITH_DNET__ ! 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 ! } #endif /* _icearp_H */ --- 11,15 ---- #include "sock_types.h" ! icestring get_arp_from_cache(icestring &ip); #endif /* _icearp_H */ Index: sock_eth.h =================================================================== RCS file: /cvsroot/javanetsim/IceScan/icesockets/sock_eth.h,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** sock_eth.h 21 Dec 2006 13:11:46 -0000 1.7 --- sock_eth.h 21 Dec 2006 15:37:23 -0000 1.8 *************** *** 2,12 **** #define _iceeth_H ! #ifndef HAVE_LIBDNET ! int send_ip_packet(const void *buf, size_t len){ ! return 1; ! } ! #else /* Portions of code below are from libdnet package: --- 2,12 ---- #define _iceeth_H ! #include "sock_types.h" ! int send_ip_packet(const void *buf, size_t len); ! icestring get_source_ip(const icestring &destip, icestring &sourceint); ! ! #ifdef HAVE_LIBDNET /* Portions of code below are from libdnet package: *************** *** 43,46 **** --- 43,48 ---- #include "queue.h" + #define SOCK_ETH + struct ip_intf { eth_t *eth; *************** *** 62,290 **** }; - static int - i_add_ip_intf(const struct intf_entry *entry, void *arg) - { - ip_t *ip = (ip_t *)arg; - struct ip_intf *ipi; - - if (entry->intf_type == INTF_TYPE_ETH && - (entry->intf_flags & INTF_FLAG_UP) != 0 && - entry->intf_mtu >= ETH_LEN_MIN && - entry->intf_addr.addr_type == ADDR_TYPE_IP && - entry->intf_link_addr.addr_type == ADDR_TYPE_ETH) { - - if ((ipi = (ip_intf*) calloc(1, sizeof(*ipi))) == NULL) - return (-1); - - strncpy(ipi->name, entry->intf_name, sizeof(ipi->name)); - memcpy(&ipi->ha, &entry->intf_link_addr, sizeof(ipi->ha)); - memcpy(&ipi->pa, &entry->intf_addr, sizeof(ipi->pa)); - ipi->mtu = entry->intf_mtu; - - LIST_INSERT_HEAD(&ip->ip_intf_list, ipi, next); - } - return (0); - } - - ip_t * - i_ip_open(void) - { - ip_t *ip; - - if ((ip = (ip_t*) calloc(1, sizeof(*ip))) != NULL) { - ip->fd = -1; - - if ((ip->arp = arp_open()) == NULL || - (ip->intf = intf_open()) == NULL || - (ip->route = route_open()) == NULL) - return (ip_close(ip)); - - if ((ip->fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) - return (ip_close(ip)); - - memset(&ip->sin, 0, sizeof(ip->sin)); - ip->sin.sin_family = AF_INET; - ip->sin.sin_port = htons(666); - - LIST_INIT(&ip->ip_intf_list); - - if (intf_loop(ip->intf, i_add_ip_intf, ip) != 0) - return (ip_close(ip)); - } - return (ip); - } - - static struct ip_intf * - i_lookup_ip_intf(ip_t *ip, ip_addr_t dst) - { - struct ip_intf *ipi; - int n; - - ip->sin.sin_addr.s_addr = dst; - n = sizeof(ip->sin); - - if (connect(ip->fd, (struct sockaddr *)&ip->sin, n) < 0) - return (NULL); - - if (getsockname(ip->fd, (struct sockaddr *)&ip->sin, (socklen_t *) &n) < 0) - return (NULL); - - LIST_FOREACH(ipi, &ip->ip_intf_list, next) { - if (ipi->pa.addr_ip == ip->sin.sin_addr.s_addr) { - if (ipi->eth == NULL) { - if ((ipi->eth = eth_open(ipi->name)) == NULL) - return (NULL); - } - if (ipi != LIST_FIRST(&ip->ip_intf_list)) { - LIST_REMOVE(ipi, next); - LIST_INSERT_HEAD(&ip->ip_intf_list, ipi, next); - } - return (ipi); - } - } - return (NULL); - } - - static void - i_request_arp(struct ip_intf *ipi, struct addr *dst) - { - u_char frame[ETH_HDR_LEN + ARP_HDR_LEN + ARP_ETHIP_LEN]; - - eth_pack_hdr(frame, ETH_ADDR_BROADCAST, ipi->ha.addr_eth, - ETH_TYPE_ARP); - arp_pack_hdr_ethip(frame + ETH_HDR_LEN, ARP_OP_REQUEST, - ipi->ha.addr_eth, ipi->pa.addr_ip, ETH_ADDR_BROADCAST, - dst->addr_ip); - - eth_send(ipi->eth, frame, sizeof(frame)); - } - - ssize_t - i_ip_send(ip_t *ip, const void *buf, size_t len) - { - struct ip_hdr *iph; - struct ip_intf *ipi; - struct arp_entry arpent; - struct route_entry rtent; - u_char frame[ETH_LEN_MAX]; - int i, usec; - - iph = (struct ip_hdr *)buf; - - if ((ipi = i_lookup_ip_intf(ip, iph->ip_dst)) == NULL) { - errno = EHOSTUNREACH; - return (-1); - } - arpent.arp_pa.addr_type = ADDR_TYPE_IP; - arpent.arp_pa.addr_bits = IP_ADDR_BITS; - arpent.arp_pa.addr_ip = iph->ip_dst; - memcpy(&rtent.route_dst, &arpent.arp_pa, sizeof(rtent.route_dst)); - - for (i = 0, usec = 10; i < 3; i++, usec *= 100) { - if (arp_get(ip->arp, &arpent) == 0) - break; - - if (route_get(ip->route, &rtent) == 0 && - rtent.route_gw.addr_ip != ipi->pa.addr_ip) { - memcpy(&arpent.arp_pa, &rtent.route_gw, - sizeof(arpent.arp_pa)); - if (arp_get(ip->arp, &arpent) == 0) - break; - } - i_request_arp(ipi, &arpent.arp_pa); - - usleep(usec); - } - if (i == 3) - memset(&arpent.arp_ha.addr_eth, 0xff, ETH_ADDR_LEN); - - eth_pack_hdr(frame, arpent.arp_ha.addr_eth, - ipi->ha.addr_eth, ETH_TYPE_IP); - - if (len > ipi->mtu) { - u_char *p, *start, *end, *ip_data; - #undef ip_hl - int ip_hl, fraglen; - ip_hl = iph->ip_hl << 2; - fraglen = ipi->mtu - ip_hl; ! iph = (struct ip_hdr *)(frame + ETH_HDR_LEN); ! memcpy(iph, buf, ip_hl); ! ip_data = (u_char *)iph + ip_hl; ! start = (u_char *)buf + ip_hl; ! end = (u_char *)buf + len; ! ! for (p = start; p < end; ) { ! memcpy(ip_data, p, fraglen); ! ! iph->ip_len = htons(ip_hl + fraglen); ! iph->ip_off = htons(((p + fraglen < end) ? IP_MF : 0) | ! ((p - start) >> 3)); ! ip_checksum(iph, ip_hl + fraglen); ! i = ETH_HDR_LEN + ip_hl + fraglen; ! if (eth_send(ipi->eth, frame, i) != i) ! return (-1); ! p += fraglen; ! if (end - p < fraglen) ! fraglen = end - p; ! } ! return (len); ! #define ip_hl ip_hv&0x0f ! } ! memcpy(frame + ETH_HDR_LEN, buf, len); ! i = ETH_HDR_LEN + len; ! if (eth_send(ipi->eth, frame, i) != i) ! return (-1); ! ! return (len); ! } ip_t * ! i_ip_close(ip_t *ip) ! { ! struct ip_intf *ipi, *nxt; ! ! if (ip != NULL) { ! for (ipi = LIST_FIRST(&ip->ip_intf_list); ! ipi != LIST_END(&ip->ip_intf_list); ipi = nxt) { ! nxt = LIST_NEXT(ipi, next); ! if (ipi->eth != NULL) ! eth_close(ipi->eth); ! free(ipi); ! } ! if (ip->fd >= 0) ! #ifdef WIN32 ! closesocket(ip->fd); ! #else ! close(ip->fd); ! #endif ! if (ip->route != NULL) ! route_close(ip->route); ! if (ip->intf != NULL) ! intf_close(ip->intf); ! if (ip->arp != NULL) ! arp_close(ip->arp); ! free(ip); ! } ! return (NULL); ! } /* end of modified code from libdnet */ - int send_ip_packet(const void *buf, size_t len){ - ip_t *i = i_ip_open(); - - i_ip_send(i, buf, len); - - i_ip_close(i); - - return 0; - } - #endif --- 64,84 ---- }; ! static int i_add_ip_intf(const struct intf_entry *entry, void *arg); ! ip_t* i_ip_open(void); ! static struct ip_intf* i_lookup_ip_intf(ip_t*, ip_addr_t); ! static void i_request_arp(struct ip_intf *ipi, struct addr *dst); ! ssize_t i_ip_send(ip_t *ip, const void *buf, size_t len); ip_t * ! i_ip_close(ip_t *ip); /* end of modified code from libdnet */ #endif --- NEW FILE: sock_eth.cc --- #include "sock_eth.h" #ifndef HAVE_LIBDNET int send_ip_packet(const void *buf, size_t len){ return 1; } #else /* Portions of code below are from libdnet package: * Copyright (c) 2000-2004 Dug Song <du...@mo...> * All rights reserved, all wrongs reversed. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The names of the authors and copyright holders may not be used to * endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* start of modified code from libdnet */ static int i_add_ip_intf(const struct intf_entry *entry, void *arg) { ip_t *ip = (ip_t *)arg; struct ip_intf *ipi; if (entry->intf_type == INTF_TYPE_ETH && (entry->intf_flags & INTF_FLAG_UP) != 0 && entry->intf_mtu >= ETH_LEN_MIN && entry->intf_addr.addr_type == ADDR_TYPE_IP && entry->intf_link_addr.addr_type == ADDR_TYPE_ETH) { if ((ipi = (ip_intf*) calloc(1, sizeof(*ipi))) == NULL) return (-1); strncpy(ipi->name, entry->intf_name, sizeof(ipi->name)); memcpy(&ipi->ha, &entry->intf_link_addr, sizeof(ipi->ha)); memcpy(&ipi->pa, &entry->intf_addr, sizeof(ipi->pa)); ipi->mtu = entry->intf_mtu; LIST_INSERT_HEAD(&ip->ip_intf_list, ipi, next); } return (0); } ip_t * i_ip_open(void) { ip_t *ip; if ((ip = (ip_t*) calloc(1, sizeof(*ip))) != NULL) { ip->fd = -1; if ((ip->arp = arp_open()) == NULL || (ip->intf = intf_open()) == NULL || (ip->route = route_open()) == NULL) return (ip_close(ip)); if ((ip->fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) return (ip_close(ip)); memset(&ip->sin, 0, sizeof(ip->sin)); ip->sin.sin_family = AF_INET; ip->sin.sin_port = htons(666); LIST_INIT(&ip->ip_intf_list); if (intf_loop(ip->intf, i_add_ip_intf, ip) != 0) return (ip_close(ip)); } return (ip); } static struct ip_intf * i_lookup_ip_intf(ip_t *ip, ip_addr_t dst) { struct ip_intf *ipi; int n; ip->sin.sin_addr.s_addr = dst; n = sizeof(ip->sin); if (connect(ip->fd, (struct sockaddr *)&ip->sin, n) < 0) return (NULL); if (getsockname(ip->fd, (struct sockaddr *)&ip->sin, (socklen_t *) &n) < 0) return (NULL); LIST_FOREACH(ipi, &ip->ip_intf_list, next) { if (ipi->pa.addr_ip == ip->sin.sin_addr.s_addr) { if (ipi->eth == NULL) { if ((ipi->eth = eth_open(ipi->name)) == NULL) return (NULL); } if (ipi != LIST_FIRST(&ip->ip_intf_list)) { LIST_REMOVE(ipi, next); LIST_INSERT_HEAD(&ip->ip_intf_list, ipi, next); } return (ipi); } } return (NULL); } static void i_request_arp(struct ip_intf *ipi, struct addr *dst) { u_char frame[ETH_HDR_LEN + ARP_HDR_LEN + ARP_ETHIP_LEN]; eth_pack_hdr(frame, ETH_ADDR_BROADCAST, ipi->ha.addr_eth, ETH_TYPE_ARP); arp_pack_hdr_ethip(frame + ETH_HDR_LEN, ARP_OP_REQUEST, ipi->ha.addr_eth, ipi->pa.addr_ip, ETH_ADDR_BROADCAST, dst->addr_ip); eth_send(ipi->eth, frame, sizeof(frame)); } ssize_t i_ip_send(ip_t *ip, const void *buf, size_t len) { struct ip_hdr *iph; struct ip_intf *ipi; struct arp_entry arpent; struct route_entry rtent; u_char frame[ETH_LEN_MAX]; int i, usec; iph = (struct ip_hdr *)buf; if ((ipi = i_lookup_ip_intf(ip, iph->ip_dst)) == NULL) { errno = EHOSTUNREACH; return (-1); } arpent.arp_pa.addr_type = ADDR_TYPE_IP; arpent.arp_pa.addr_bits = IP_ADDR_BITS; arpent.arp_pa.addr_ip = iph->ip_dst; memcpy(&rtent.route_dst, &arpent.arp_pa, sizeof(rtent.route_dst)); for (i = 0, usec = 10; i < 3; i++, usec *= 100) { if (arp_get(ip->arp, &arpent) == 0) break; if (route_get(ip->route, &rtent) == 0 && rtent.route_gw.addr_ip != ipi->pa.addr_ip) { memcpy(&arpent.arp_pa, &rtent.route_gw, sizeof(arpent.arp_pa)); if (arp_get(ip->arp, &arpent) == 0) break; } i_request_arp(ipi, &arpent.arp_pa); usleep(usec); } if (i == 3) memset(&arpent.arp_ha.addr_eth, 0xff, ETH_ADDR_LEN); eth_pack_hdr(frame, arpent.arp_ha.addr_eth, ipi->ha.addr_eth, ETH_TYPE_IP); if (len > ipi->mtu) { u_char *p, *start, *end, *ip_data; #undef ip_hl int ip_hl, fraglen; ip_hl = iph->ip_hl << 2; fraglen = ipi->mtu - ip_hl; iph = (struct ip_hdr *)(frame + ETH_HDR_LEN); memcpy(iph, buf, ip_hl); ip_data = (u_char *)iph + ip_hl; start = (u_char *)buf + ip_hl; end = (u_char *)buf + len; for (p = start; p < end; ) { memcpy(ip_data, p, fraglen); iph->ip_len = htons(ip_hl + fraglen); iph->ip_off = htons(((p + fraglen < end) ? IP_MF : 0) | ((p - start) >> 3)); ip_checksum(iph, ip_hl + fraglen); i = ETH_HDR_LEN + ip_hl + fraglen; if (eth_send(ipi->eth, frame, i) != i) return (-1); p += fraglen; if (end - p < fraglen) fraglen = end - p; } return (len); #define ip_hl ip_hv&0x0f } memcpy(frame + ETH_HDR_LEN, buf, len); i = ETH_HDR_LEN + len; if (eth_send(ipi->eth, frame, i) != i) return (-1); return (len); } ip_t * i_ip_close(ip_t *ip) { struct ip_intf *ipi, *nxt; if (ip != NULL) { for (ipi = LIST_FIRST(&ip->ip_intf_list); ipi != LIST_END(&ip->ip_intf_list); ipi = nxt) { nxt = LIST_NEXT(ipi, next); if (ipi->eth != NULL) eth_close(ipi->eth); free(ipi); } if (ip->fd >= 0) #ifdef WIN32 closesocket(ip->fd); #else close(ip->fd); #endif if (ip->route != NULL) route_close(ip->route); if (ip->intf != NULL) intf_close(ip->intf); if (ip->arp != NULL) arp_close(ip->arp); free(ip); } return (NULL); } /* end of modified code from libdnet */ int send_ip_packet(const void *buf, size_t len){ ip_t *i = i_ip_open(); i_ip_send(i, buf, len); i_ip_close(i); return 0; } #endif 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 #ifdef __linux__ if(!strcmp(destip.c_str(), "127.0.0.1")){ sourceint.assign("lo"); return "127.0.0.1"; } #elif //Need to add some code here... #endif struct ip_intf *ipi; ip_t *ip = i_ip_open(); if ((ipi = i_lookup_ip_intf(ip, inet_addr(destip.c_str()))) == NULL) { errno = EHOSTUNREACH; return ""; } struct sockaddr sa; addr_ntos(&(ipi->pa), &sa); struct sockaddr_in *sai = (sockaddr_in *) &sa; sourceint.assign(ipi->name); icestring addr(inet_ntoa(sai->sin_addr)); #ifdef __linux__ //add more platforms here if(! strcmp(addr.c_str(), destip.c_str())) sourceint.assign("lo"); #endif return addr; #endif } --- NEW FILE: sock_time.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; } --- NEW FILE: sock_win.cc --- #include "sock_win.h" #ifdef WIN32 WSADATA start_sockets(){ WSADATA wsa; if(WSAStartup(MAKEWORD(2,0), &wsa) != 0x0){ printf("WSAStartup() error!\n"); exit(-1); } return wsa; } void stop_sockets(){ WSACleanup(); } int getpid(){ return _getpid(); } int getuid(){ return (int)!(IsCurrentUserLocalAdministrator()); //UGLY, FIXIT! } //from MS Q118626: BOOL IsCurrentUserLocalAdministrator(void) { BOOL fReturn = FALSE; DWORD dwStatus; DWORD dwAccessMask; DWORD dwAccessDesired; DWORD dwACLSize; DWORD dwStructureSize = sizeof(PRIVILEGE_SET); PACL pACL = NULL; PSID psidAdmin = NULL; HANDLE hToken = NULL; HANDLE hImpersonationToken = NULL; PRIVILEGE_SET ps; GENERIC_MAPPING GenericMapping; PSECURITY_DESCRIPTOR psdAdmin = NULL; SID_IDENTIFIER_AUTHORITY SystemSidAuthority = SECURITY_NT_AUTHORITY; const DWORD ACCESS_READ = 1; const DWORD ACCESS_WRITE = 2; __try { if (!OpenThreadToken(GetCurrentThread(), TOKEN_DUPLICATE|TOKEN_QUERY, TRUE, &hToken)) { if (GetLastError() != ERROR_NO_TOKEN) __leave; if (!OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE|TOKEN_QUERY, &hToken)) __leave; } if (!DuplicateToken (hToken, SecurityImpersonation, &hImpersonationToken)) __leave; if (!AllocateAndInitializeSid(&SystemSidAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &psidAdmin)) __leave; psdAdmin = LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH); if (psdAdmin == NULL) __leave; if (!InitializeSecurityDescriptor(psdAdmin, SECURITY_DESCRIPTOR_REVISION)) __leave; dwACLSize = sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(psidAdmin) - sizeof(DWORD); pACL = (PACL)LocalAlloc(LPTR, dwACLSize); if (pACL == NULL) __leave; if (!InitializeAcl(pACL, dwACLSize, ACL_REVISION2)) __leave; dwAccessMask= ACCESS_READ | ACCESS_WRITE; if (!AddAccessAllowedAce(pACL, ACL_REVISION2, dwAccessMask, psidAdmin)) __leave; if (!SetSecurityDescriptorDacl(psdAdmin, TRUE, pACL, FALSE)) __leave; SetSecurityDescriptorGroup(psdAdmin, psidAdmin, FALSE); SetSecurityDescriptorOwner(psdAdmin, psidAdmin, FALSE); if (!IsValidSecurityDescriptor(psdAdmin)) __leave; dwAccessDesired = ACCESS_READ; /* Initialize GenericMapping structure even though you do not use generic rights. */ GenericMapping.GenericRead = ACCESS_READ; GenericMapping.GenericWrite = ACCESS_WRITE; GenericMapping.GenericExecute = 0; GenericMapping.GenericAll = ACCESS_READ | ACCESS_WRITE; if (!AccessCheck(psdAdmin, hImpersonationToken, dwAccessDesired, &GenericMapping, &ps, &dwStructureSize, &dwStatus, &fReturn)) { fReturn = FALSE; __leave; } } __finally { // Clean up. if (pACL) LocalFree(pACL); if (psdAdmin) LocalFree(psdAdmin); if (psidAdmin) FreeSid(psidAdmin); if (hImpersonationToken) CloseHandle (hImpersonationToken); if (hToken) CloseHandle (hToken); } return fReturn; } #else int start_sockets(){} void stop_sockets(){} #endif Index: sock_name.h =================================================================== RCS file: /cvsroot/javanetsim/IceScan/icesockets/sock_name.h,v retrieving revision 1.15 retrieving revision 1.16 diff -C2 -d -r1.15 -r1.16 *** sock_name.h 21 Dec 2006 13:59:51 -0000 1.15 --- sock_name.h 21 Dec 2006 15:37:23 -0000 1.16 *************** *** 19,166 **** #endif ! int make_sockname(struct sockaddr_in& saddr, const char *hostname, int port, int domain) { ! saddr.sin_family = domain; ! if (hostname){ ! struct hostent *hostpointer = gethostbyname(hostname); ! memcpy((char *) &saddr.sin_addr, (char *) hostpointer->h_addr, hostpointer->h_length); ! } else ! saddr.sin_addr.s_addr = INADDR_ANY; ! ! ! saddr.sin_port = htons(port); ! saddr.sin_family = domain; ! memset(&(saddr.sin_zero), '\0', 8); ! ! return sizeof(saddr); ! } ! ! ! bool is_domainname(char * nisname){ ! IceRegex re("\\d+\\.\\d+\\.\\d+\\.\\d+"); ! return ! (re.match(nisname)); ! } ! ! bool is_domainname(icestring &nisname){ ! IceRegex re("\\d+\\.\\d+\\.\\d+\\.\\d+"); ! return ! (re.match(nisname.c_str())); ! } ! ! ! short int get_ip_byhost(const char * nisname, char * retname, int retlen){ ! struct hostent *h; ! char hname[1024]; ! char *rname; ! ! int len = (strlen(nisname) < 1024) ? strlen(nisname) : 1024; ! ! strncpy(hname, nisname, len); ! ! if( ( h = gethostbyname(nisname) ) == NULL) { ! return -1; ! } ! ! rname = inet_ntoa(*((struct in_addr *)h->h_addr)); ! ! retlen = (retlen >= strlen(rname)) ? strlen(rname) : retlen; ! ! strncpy(retname, rname, retlen); ! ! if(h->h_addr_list != NULL) return 1; ! else return 0; ! } ! ! short int get_ips_byhost(const char * nisname, std::vector <icestring> &v){ ! struct addrinfo hints, *res; ! char hname[1024]; ! int cnt = 0; ! ! int len = (strlen(nisname) < 1024) ? strlen(nisname) : 1024; ! ! strncpy(hname, nisname, len); ! ! Bzero(&hints, sizeof(hints)); ! hints.ai_flags = AI_CANONNAME; hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; ! ! if( getaddrinfo(nisname, "domain", &hints, &res) ) { ! return -1; ! } ! ! while(res){ ! icestring is(inet_ntoa( *( &( ((sockaddr_in *)res->ai_addr)->sin_addr ) ) )); ! ! v.push_back(is); ! res = res->ai_next; ! cnt++; ! } ! ! freeaddrinfo(res); ! ! return cnt; ! } ! short int get_host_byip(icestring &nisname, icestring &retname){ ! int rc; ! char hbuf[ICEMAXHOSTNAME]; ! struct sockaddr_in saddr; ! ! int len = make_sockname(saddr, nisname.c_str(), 53, AF_INET); ! ! if( (rc = getnameinfo((sockaddr *) &saddr, len, hbuf, sizeof(hbuf), NULL, 0, NI_NAMEREQD)) != 0) { //CHECK IN LINUX/BSD!!! ! //perror("getnameinfo"); ! return -1; ! } ! retname.assign(hbuf); ! ! return 0; ! } ! icestring get_source_ip(const icestring &destip, icestring &sourceint){ ! #ifndef DNET_H ! 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 ! #ifdef __linux__ ! if(!strcmp(destip.c_str(), "127.0.0.1")){ ! sourceint.assign("lo"); ! return "127.0.0.1"; ! } ! #elif ! //Need to add some code here... ! #endif ! ! struct ip_intf *ipi; ! ! ip_t *ip = i_ip_open(); ! ! if ((ipi = i_lookup_ip_intf(ip, inet_addr(destip.c_str()))) == NULL) { ! errno = EHOSTUNREACH; ! return ""; ! } ! ! ! struct sockaddr sa; ! ! addr_ntos(&(ipi->pa), &sa); ! ! struct sockaddr_in *sai = (sockaddr_in *) &sa; ! ! sourceint.assign(ipi->name); ! icestring addr(inet_ntoa(sai->sin_addr)); ! ! #ifdef __linux__ //add more platforms here ! if(! strcmp(addr.c_str(), destip.c_str())) sourceint.assign("lo"); ! #endif ! ! return addr; ! #endif ! } #endif --- 19,33 ---- #endif ! int make_sockname(struct sockaddr_in& saddr, const char *hostname, int port, int domain); ! bool is_domainname(char * nisname); ! bool is_domainname(icestring &nisname); ! short int get_ip_byhost(const char * nisname, char * retname, int retlen); ! short int get_ips_byhost(const char * nisname, std::vector <icestring> &v); ! short int get_host_byip(icestring &nisname, icestring &retname); #endif Index: sock_time.h =================================================================== RCS file: /cvsroot/javanetsim/IceScan/icesockets/sock_time.h,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** sock_time.h 21 Dec 2006 13:11:46 -0000 1.10 --- sock_time.h 21 Dec 2006 15:37:23 -0000 1.11 *************** *** 15,96 **** #endif ! 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; ! } #endif --- 15,30 ---- #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); #endif Index: sock_files.h =================================================================== RCS file: /cvsroot/javanetsim/IceScan/icesockets/sock_files.h,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** sock_files.h 21 Dec 2006 13:59:51 -0000 1.1 --- sock_files.h 21 Dec 2006 15:37:23 -0000 1.2 *************** *** 11,44 **** #include "sock_types.h" ! 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; ! } #endif /* _sock_files_H */ --- 11,17 ---- #include "sock_types.h" ! bool file_exists(icestring &fname); ! icestring find_icefile_path(char *fname); #endif /* _sock_files_H */ --- NEW FILE: sock_arp.cc --- #include "sock_arp.h" icestring get_arp_from_cache(icestring &ip) { char arp[24]; #ifdef __CYGWIN__ return ""; #elif __WITH_DNET__ 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_name.cc --- #include "sock_name.h" int make_sockname(struct sockaddr_in& saddr, const char *hostname, int port, int domain) { saddr.sin_family = domain; if (hostname){ struct hostent *hostpointer = gethostbyname(hostname); memcpy((char *) &saddr.sin_addr, (char *) hostpointer->h_addr, hostpointer->h_length); } else saddr.sin_addr.s_addr = INADDR_ANY; saddr.sin_port = htons(port); saddr.sin_family = domain; memset(&(saddr.sin_zero), '\0', 8); return sizeof(saddr); } bool is_domainname(char * nisname){ IceRegex re("\\d+\\.\\d+\\.\\d+\\.\\d+"); return ! (re.match(nisname)); } bool is_domainname(icestring &nisname){ IceRegex re("\\d+\\.\\d+\\.\\d+\\.\\d+"); return ! (re.match(nisname.c_str())); } short int get_ip_byhost(const char * nisname, char * retname, int retlen){ struct hostent *h; char hname[1024]; char *rname; int len = (strlen(nisname) < 1024) ? strlen(nisname) : 1024; strncpy(hname, nisname, len); if( ( h = gethostbyname(nisname) ) == NULL) { return -1; } rname = inet_ntoa(*((struct in_addr *)h->h_addr)); retlen = (retlen >= strlen(rname)) ? strlen(rname) : retlen; strncpy(retname, rname, retlen); if(h->h_addr_list != NULL) return 1; else return 0; } short int get_ips_byhost(const char * nisname, std::vector <icestring> &v){ struct addrinfo hints, *res; char hname[1024]; int cnt = 0; int len = (strlen(nisname) < 1024) ? strlen(nisname) : 1024; strncpy(hname, nisname, len); Bzero(&hints, sizeof(hints)); hints.ai_flags = AI_CANONNAME; hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; if( getaddrinfo(nisname, "domain", &hints, &res) ) { return -1; } while(res){ icestring is(inet_ntoa( *( &( ((sockaddr_in *)res->ai_addr)->sin_addr ) ) )); v.push_back(is); res = res->ai_next; cnt++; } freeaddrinfo(res); return cnt; } short int get_host_byip(icestring &nisname, icestring &retname){ int rc; char hbuf[ICEMAXHOSTNAME]; struct sockaddr_in saddr; int len = make_sockname(saddr, nisname.c_str(), 53, AF_INET); if( (rc = getnameinfo((sockaddr *) &saddr, len, hbuf, sizeof(hbuf), NULL, 0, NI_NAMEREQD)) != 0) { //CHECK IN LINUX/BSD!!! //perror("getnameinfo"); return -1; } retname.assign(hbuf); return 0; } Index: sock_win.h =================================================================== RCS file: /cvsroot/javanetsim/IceScan/icesockets/sock_win.h,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** sock_win.h 17 Dec 2006 19:52:23 -0000 1.9 --- sock_win.h 21 Dec 2006 15:37:23 -0000 1.10 *************** *** 32,177 **** #include <lmcons.h> ! WSADATA start_sockets(){ ! WSADATA wsa; ! ! if(WSAStartup(MAKEWORD(2,0), &wsa) != 0x0){ ! printf("WSAStartup() error!\n"); ! exit(-1); ! } ! ! return wsa; ! } ! void stop_sockets(){ ! WSACleanup(); ! } ! ! int getpid(){ ! return _getpid(); ! } ! ! BOOL IsCurrentUserLocalAdministrator(void); ! ! int getuid(){ ! return (int)!(IsCurrentUserLocalAdministrator()); //UGLY, FIXIT! ! } ! ! //from MS Q118626: ! BOOL IsCurrentUserLocalAdministrator(void) ! { ! BOOL fReturn = FALSE; ! DWORD dwStatus; ! DWORD dwAccessMask; ! DWORD dwAccessDesired; ! DWORD dwACLSize; ! DWORD dwStructureSize = sizeof(PRIVILEGE_SET); ! PACL pACL = NULL; ! PSID psidAdmin = NULL; ! ! HANDLE hToken = NULL; ! HANDLE hImpersonationToken = NULL; ! ! PRIVILEGE_SET ps; ! GENERIC_MAPPING GenericMapping; ! ! PSECURITY_DESCRIPTOR psdAdmin = NULL; ! SID_IDENTIFIER_AUTHORITY SystemSidAuthority = SECURITY_NT_AUTHORITY; ! ! const DWORD ACCESS_READ = 1; ! const DWORD ACCESS_WRITE = 2; ! ! ! __try ! { ! ! if (!OpenThreadToken(GetCurrentThread(), TOKEN_DUPLICATE|TOKEN_QUERY, TRUE, &hToken)) ! { ! if (GetLastError() != ERROR_NO_TOKEN) ! __leave; ! ! if (!OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE|TOKEN_QUERY, &hToken)) ! __leave; ! } ! ! if (!DuplicateToken (hToken, SecurityImpersonation, &hImpersonationToken)) ! __leave; ! ! ! if (!AllocateAndInitializeSid(&SystemSidAuthority, 2, ! SECURITY_BUILTIN_DOMAIN_RID, ! DOMAIN_ALIAS_RID_ADMINS, ! 0, 0, 0, 0, 0, 0, &psidAdmin)) ! __leave; ! ! psdAdmin = LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH); ! if (psdAdmin == NULL) ! __leave; ! ! if (!InitializeSecurityDescriptor(psdAdmin, SECURITY_DESCRIPTOR_REVISION)) ! __leave; ! ! dwACLSize = sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) + ! GetLengthSid(psidAdmin) - sizeof(DWORD); ! ! pACL = (PACL)LocalAlloc(LPTR, dwACLSize); ! if (pACL == NULL) ! __leave; ! ! if (!InitializeAcl(pACL, dwACLSize, ACL_REVISION2)) ! __leave; ! ! dwAccessMask= ACCESS_READ | ACCESS_WRITE; ! ! if (!AddAccessAllowedAce(pACL, ACL_REVISION2, dwAccessMask, psidAdmin)) ! __leave; ! ! if (!SetSecurityDescriptorDacl(psdAdmin, TRUE, pACL, FALSE)) ! __leave; ! ! SetSecurityDescriptorGroup(psdAdmin, psidAdmin, FALSE); ! SetSecurityDescriptorOwner(psdAdmin, psidAdmin, FALSE); ! ! if (!IsValidSecurityDescriptor(psdAdmin)) ! __leave; ! ! dwAccessDesired = ACCESS_READ; ! /* ! Initialize GenericMapping structure even though you ! do not use generic rights. ! */ ! GenericMapping.GenericRead = ACCESS_READ; ! GenericMapping.GenericWrite = ACCESS_WRITE; ! GenericMapping.GenericExecute = 0; ! GenericMapping.GenericAll = ACCESS_READ | ACCESS_WRITE; ! if (!AccessCheck(psdAdmin, hImpersonationToken, dwAccessDesired, ! &GenericMapping, &ps, &dwStructureSize, &dwStatus, ! &fReturn)) ! { ! fReturn = FALSE; ! __leave; ! } ! } ! __finally ! { ! // Clean up. ! if (pACL) LocalFree(pACL); ! if (psdAdmin) LocalFree(psdAdmin); ! if (psidAdmin) FreeSid(psidAdmin); ! if (hImpersonationToken) CloseHandle (hImpersonationToken); ! if (hToken) CloseHandle (hToken); ! } ! return fReturn; ! } #else ! typedef int WSADATA; ! ! int start_sockets(){ } ! ! void stop_sockets(){ } #endif --- 32,51 ---- #include <lmcons.h> ! WSADATA start_sockets(); ! void stop_sockets(); ! int getpid(); ! BOOL IsCurrentUserLocalAdministrator(void); ! int getuid(); #else + typedef int WSADATA; + + int start_sockets(); ! void stop_sockets(); #endif Index: ice_rand.h =================================================================== RCS file: /cvsroot/javanetsim/IceScan/icesockets/ice_rand.h,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** ice_rand.h 16 Dec 2006 16:06:06 -0000 1.3 --- ice_rand.h 21 Dec 2006 15:37:23 -0000 1.4 *************** *** 9,78 **** #define _ice_rand_H ! #include <string.h> ! #include <stdio.h> ! #include <stdlib.h> ! #if HAVE_SYS_TIME_H ! #include <sys/time.h> #endif // 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)) { ! fprintf(stderr, "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); ! } #endif /* _ice_rand_H */ --- 9,22 ---- #define _ice_rand_H ! #include "sock_types.h" ! ! #ifdef HAVE_SYS_TIME_H ! #include <sys/time.h> #endif // from Fyodor's "The Art of Port Scanning" ! int get_random_bytes(void *buf, int numbytes); ! void init_rand(); #endif /* _ice_rand_H */ Index: sock_pcap.h =================================================================== RCS file: /cvsroot/javanetsim/IceScan/icesockets/sock_pcap.h,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** sock_pcap.h 21 Dec 2006 13:11:46 -0000 1.10 --- sock_pcap.h 21 Dec 2006 15:37:23 -0000 1.11 *************** *** 9,282 **** #define _sock_pcap_H #include "sock_types.h" #ifndef HAVE_LIBPCAP ! typedef int pcap_t; ! ! pcap_t* init_pcap(int index = 0){ ! return NULL; ! } ! ! pcap_t* start_pcap(int iface, bool block, char *filter_exp){ ! return NULL; ! } ! ! void listdev_pcap(){ ! } ! ! 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 getdev_pcap(icestring &dev){ ! return 0; ! } ! ! 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; ! } ! ! void listdev_pcap(){ ! 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; ! } ! } ! ! int getdev_pcap(icestring &dev){ ! 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; ! } ! ! 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); ! } ! pcap_t* open_pcap(int iface, bool block, char *filter_exp){ ! pcap_t* p = init_pcap(iface); ! ! if(!p) return NULL; ! ! pcap_block(p, block); ! ! pcap_filter(p, filter_exp); ! ! return p; ! } - void close_pcap(pcap_t *p){ - pcap_close(p); - } - #endif #endif /* _sock_pcap_H */ --- 9,35 ---- #define _sock_pcap_H + #include "sock_err.h" #include "sock_types.h" #ifndef HAVE_LIBPCAP ! typedef int pcap_t; #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 */ --- NEW FILE: sock_files.cc --- #include "sock_files.h" 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; } |