/* * return the ip addresses for a type of server for system ip */ int lookupserver(char *attr, uchar ipaddrs[2][IPaddrlen], Ipinfo *iip) { Ndbtuple *t, *nt; Ndbs s; char ip[32]; char name[Ndbvlen]; char name1[Ndbvlen]; int i; name[0] = name1[0] = 0; snprint(ip, sizeof(ip), "%I", iip->ipaddr); t = ndbsearch(db, &s, "ip", ip); while(t){ for(nt = t; nt; nt = nt->entry){ if(strcmp(attr, nt->attr) == 0){ if(*name == 0) strcpy(name, nt->val); else { strcpy(name1, nt->val); break; } } } if(name[0]) break; t = ndbsnext(&s, "ip", ip); } if(name[0] == 0) recursesubnet(db, iip->ipaddr, classmask[CLASS(iip->ipaddr)], attr, name, name1); i = 0; if(name[0] && getipaddr(db, name, *ipaddrs, iip) == 1){ ipaddrs++; i++; } if(name1[0] && getipaddr(db, name1, *ipaddrs, iip) == 1) i++; return i; }
int init_cache_ipinfo(char *path) { int i = 0; unsigned int tmp, endip; char ip[2][16]; off_t filesize = 0; openshare(path, &filesize); //char * pos = p_share + getlong4(p_share); char * pos = p_begin; endip = getlong4(p_end); //printf("1111%u\n", getlong4(pos)); IpInfo * buf; buf = (IpInfo *)malloc(sizeof(IpInfo)*ipsum); if (buf == NULL){ printf("%s %s %d\n", strerror(errno), __func__, __LINE__); exit(1); } memset(buf, 0, sizeof(IpInfo)*ipsum); while (i < ipsum){ //memset(ip, 0, sizeof(ip)); pos = p_begin + i*RLEN; buf[i].ip[0] = getlong4(pos); buf[i].ip[1] = getlong4(p_share+getlong3(pos + 4)); getipaddr(pos+4, &buf[i].addr.countrybuf, &buf[i].addr.areabuf); #if 0 tmp = htonl(buf[i].ip[0]); inet_ntop(AF_INET, &tmp, ip[0], sizeof(ip[0])); tmp = htonl(buf[i].ip[1]); inet_ntop(AF_INET, &tmp, ip[1], sizeof(ip[1])); printf("i:%d %s %s %s %s\n", i, ip[0], ip[1], buf[i].addr.countrybuf, buf[i].addr.areabuf); #endif i++; } closeshare(filesize); p_ip_info = buf; return 0; }
int get_all_atkip(atklist_st *atklist) { int index; struct sockaddr_in myaddr, gateway; getipaddr(DEV_NAME, (struct sockaddr *)&myaddr); printf("myip: %s\n", inet_ntoa(myaddr.sin_addr)); for (index = 1; index < 255; index ++) { 3[(char *)&myaddr.sin_addr.s_addr] = index; atklist->atk_list[index] = myaddr.sin_addr; } atklist->atk_num = 255; delete_ip(atklist, myaddr.sin_addr); gateway.sin_addr.s_addr = inet_addr(GATEWAY); delete_ip(atklist, gateway.sin_addr); return 0; }
int main(int argc, char** argv) { pcap_dumper_t *dumper; pcap_t *p; struct pcap_pkthdr header; u_char *packet; int i; struct sockaddr *sin_src; struct sockaddr *sin_net; struct sockaddr *sin_dst; struct sockaddr *sin_mask; int sendsize; struct options_args opts; FILE* fp_domain; char * domain; int q_class; int q_type; /** * * headers */ struct ether_header *eth; struct iphdr *ip; struct ip6_hdr *ip6; struct udphdr *udp; u_char *dns; setoptions(argc, argv, &opts); header.ts.tv_sec = 0; header.ts.tv_usec = 0; packet = calloc(sizeof (u_char) * 4096, 1); sin_src = calloc(sizeof (struct sockaddr_storage), 1); sin_net = calloc(sizeof (struct sockaddr_storage), 1); sin_dst = calloc(sizeof (struct sockaddr_storage), 1); sin_mask = calloc(sizeof (struct sockaddr_storage), 1); domain = calloc(sizeof (char) * 1024, 1); if(packet == NULL || sin_src == NULL || sin_net == NULL || sin_dst == NULL || sin_mask == NULL || domain == NULL) { return -1; } /** * * ETHERNET static */ eth = (struct ether_header *) packet; if (ether_setaddr(opts.smac, &(eth->ether_shost)) < 0) { fprintf(stderr, "smac error\n"); return 1; } if (ether_setaddr(opts.dmac, &(eth->ether_dhost)) < 0) { fprintf(stderr, "dmac error\n"); return 1; } /** * * IP static */ if (getipaddr(opts.dip, sin_dst, opts.family) < 0) { fprintf(stderr, "dip error\n"); return 2; } if (getipaddr(opts.snet, sin_net, opts.family) < 0) { fprintf(stderr, "sip error\n"); return 2; } if (sin_net->sa_family != sin_dst->sa_family) { fprintf(stderr, "IP family doesn't match\n"); return 2; } set_mask(sin_mask, sin_net->sa_family, opts.smask); if (sin_net->sa_family == AF_INET) { eth->ether_type = htons(ETHERTYPE_IP); ip = (struct iphdr *) (packet + ETHER_HDR_LEN); ip->daddr = ((struct sockaddr_in *) sin_dst)->sin_addr.s_addr; ip->version = 4; ip->ihl = 5; // no opts ip->frag_off = 0; ip->id = 0; ip->protocol = IPPROTO_UDP; ip->tos = 0; ip->ttl = IPDEFTTL; udp = (struct udphdr *) (((void *) ip) + IP_HDR_LEN); } else if (sin_net->sa_family == AF_INET6) { eth->ether_type = htons(ETHERTYPE_IPV6); ip6 = (struct ip6_hdr *) (packet + ETHER_HDR_LEN); ip6->ip6_vfc = 6 << 4; ip6->ip6_hlim = IPDEFTTL; ip6->ip6_nxt = IPPROTO_UDP; memcpy(&(ip6->ip6_dst), &(((struct sockaddr_in6 *) sin_dst)->sin6_addr), 16); udp = (struct udphdr *) (((void *) ip6) + IP6_HDR_LEN); } else { fprintf(stderr, "Family unknown\n"); return 2; } /** * * UDP static */ udp->check = 0; udp->dest = htons(53); /** * * DNS static */ dns = (u_char *) (((void *) udp) + UDP_HDR_LEN); p = pcap_open_dead(DLT_EN10MB, PCAP_SNAPLEN); dumper = pcap_dump_open(p, opts.out_file_name); if(dumper == NULL) { fprintf(stderr, "Can't open output file\n"); pcap_close(p); return 3; } init_domain_file(&fp_domain, opts.in_file_name); if (fp_domain == NULL) { fprintf(stderr, "Can't open queries file\n"); pcap_dump_close(dumper); pcap_close(p); return 4; } for (i = 0; i < opts.count; i++) { /** * * DNS dynamic */ nextdomain(fp_domain, &domain, &q_type, &q_class); sendsize = res_mkquery(QUERY, domain, q_class, q_type, NULL, 0, NULL, dns, PACKETSZ); /** * * UDP dynamic */ udp->source = htons(rand()); udp->len = htons(sendsize + UDP_HDR_LEN); /** * * IP dynamic */ get_rand_addr(sin_net, sin_mask, sin_src); if (sin_net->sa_family == AF_INET) { ip->saddr = ((struct sockaddr_in *) sin_src)->sin_addr.s_addr; ip->tot_len = htons(sendsize + UDP_HDR_LEN + (ip->ihl * 4)); header.len = sendsize + UDP_HDR_LEN + (ip->ihl * 4) + ETHER_HDR_LEN; ip->check = 0; ip->check = inet_cksum(ip, (ip->ihl * 4)); } else if (sin_net->sa_family == AF_INET6) { memcpy(&(ip6->ip6_src), &(((struct sockaddr_in6 *) sin_src)->sin6_addr), 16); ip6->ip6_plen = htons(sendsize + UDP_HDR_LEN); header.len = sendsize + UDP_HDR_LEN + sizeof (struct ip6_hdr) +ETHER_HDR_LEN; } header.caplen = header.len; if (header.len > opts.mtu) { fprintf(stderr, "too long: %s needs %d MTU is %d\n", domain,header.len, opts.mtu); } else { pcap_dump(dumper, &header, packet); } } pcap_dump_close(dumper); pcap_close(p); fclose(fp_domain); return (EXIT_SUCCESS); }
int main(int argc, char** argv) { u_char *packet; int i; uint32_t ck; struct sockaddr *sin_src; struct sockaddr *sin_net; struct sockaddr *sin_dst; struct sockaddr *sin_mask; struct ip6_pseudo_hdr ps_hdr; int sendsize; struct options opts; FILE* fp_domain; char * domain; struct writer_info wi; int pkt_len; double speed_limit; double pkt_sent; int q_class; int q_type; pthread_t thread; pthread_create(&thread,NULL,readliner,&speed_limit); sleeper = 50; pkt_sent = 0; speed_limit = 50000; /** * * headers */ struct ether_header *eth; struct iphdr *ip; struct ip6_hdr *ip6; struct udphdr *udp; u_char *dns; u_char dns_opt[] = {00, 00, 0x29, 0x08, 00, 00, 00, 00, 00, 00, 00}; setoptions(argc, argv, &opts); packet = calloc(sizeof (u_char) * 4096, 1); sin_src = calloc(sizeof (struct sockaddr_storage), 1); sin_net = calloc(sizeof (struct sockaddr_storage), 1); sin_dst = calloc(sizeof (struct sockaddr_storage), 1); sin_mask = calloc(sizeof (struct sockaddr_storage), 1); domain = calloc(sizeof (char) * 1024, 1); //domain = NULL; if (packet == NULL || sin_src == NULL || sin_net == NULL || sin_dst == NULL || sin_mask == NULL || domain == NULL) { return -1; } /** * * ETHERNET static */ eth = (struct ether_header *) packet; if (ether_setaddr(opts.smac, &(eth->ether_shost)) < 0) { fprintf(stderr, "smac error:%s\n"); return 1; } if (ether_setaddr(opts.dmac, &(eth->ether_dhost)) < 0) { fprintf(stderr, "dmac error\n"); return 1; } /** * * IP static */ if (getipaddr(opts.dip, sin_dst, opts.family) < 0) { fprintf(stderr, "dip error\n"); return 2; } if (getipaddr(opts.snet, sin_net, opts.family) < 0) { fprintf(stderr, "sip error\n"); return 2; } if (sin_net->sa_family != sin_dst->sa_family) { fprintf(stderr, "IP family doesn't match\n"); return 2; } set_mask(sin_mask, sin_net->sa_family, opts.smask); if (sin_net->sa_family == AF_INET) { eth->ether_type = htons(ETHERTYPE_IP); ip = (struct iphdr *) (packet + ETHER_HDR_LEN); ip->daddr = ((struct sockaddr_in *) sin_dst)->sin_addr.s_addr; ip->version = 4; ip->ihl = 5; // no opts ip->frag_off = 0; ip->id = 0; ip->protocol = IPPROTO_UDP; ip->tos = 0; ip->ttl = IPDEFTTL; udp = (struct udphdr *) (((void *) ip) + IP_HDR_LEN); } else if (sin_net->sa_family == AF_INET6) { eth->ether_type = htons(ETHERTYPE_IPV6); ip6 = (struct ip6_hdr *) (packet + ETHER_HDR_LEN); ip6->ip6_vfc = 6 << 4; ip6->ip6_hlim = IPDEFTTL; ip6->ip6_nxt = IPPROTO_UDP; memcpy(&(ip6->ip6_dst), &(((struct sockaddr_in6 *) sin_dst)->sin6_addr), 16); udp = (struct udphdr *) (((void *) ip6) + IP6_HDR_LEN); } else { fprintf(stderr, "Family unknown\n"); return 2; } /** * * UDP static */ //udp->check = 0; udp->dest = htons(53); /** * * DNS static */ dns = (u_char *) (((void *) udp) + UDP_HDR_LEN); wopen(&wi,&opts); init_domain_file(&fp_domain, opts.in_file_name); if (fp_domain == NULL) { fprintf(stderr, "Can't open queries file\n"); wclose(&wi); return 4; } for (i = 0; i < opts.count; i++) { if(pkt_sent >= sleeper) { speed_calc(pkt_sent,speed_limit, &sleeper); pkt_sent = 0; usleep(1); } /** * * DNS dynamic */ if(nextdomain(fp_domain, &domain, &q_type, &q_class)) { printf("Can't read next domain\n"); exit(1); } sendsize = res_mkquery(QUERY, domain, q_class, q_type, NULL, 0, NULL, dns, PACKETSZ); dns[11] = 1; memcpy(dns + sendsize, dns_opt, 11); sendsize += 11; /** * * UDP dynamic */ udp->source = htons(rand()); udp->len = htons(sendsize + UDP_HDR_LEN); udp->check = 0; /** * * IP dynamic */ get_rand_addr(sin_net, sin_mask, sin_src); if (sin_net->sa_family == AF_INET) { ip->saddr = ((struct sockaddr_in *) sin_src)->sin_addr.s_addr; ip->tot_len = htons(sendsize + UDP_HDR_LEN + (ip->ihl * 4)); pkt_len = sendsize + UDP_HDR_LEN + (ip->ihl * 4) + ETHER_HDR_LEN; ip->check = 0; ip->check = inet_cksum(ip, (ip->ihl * 4), 0); } else if (sin_net->sa_family == AF_INET6) { memcpy(&(ip6->ip6_src), &(((struct sockaddr_in6 *) sin_src)->sin6_addr), 16); ip6->ip6_plen = htons(sendsize + UDP_HDR_LEN); pkt_len = sendsize + UDP_HDR_LEN + sizeof (struct ip6_hdr) +ETHER_HDR_LEN; ps_hdr.src = ip6->ip6_src; ps_hdr.dst = ip6->ip6_dst; ps_hdr.len = udp->len; ps_hdr.nh = ntohs(17); ck = inet_cksum(&ps_hdr, sizeof (ps_hdr), 0); udp->check = inet_cksum(udp, sendsize + UDP_HDR_LEN, (~ck)&0xffff); } if (pkt_len > opts.mtu) { fprintf(stderr, "too long: %s needs %d MTU is %d\n", domain, pkt_len, opts.mtu); } else { wwrite(&wi,packet,pkt_len); } pkt_sent++; } wclose(&wi); fclose(fp_domain); return (EXIT_SUCCESS); }