char* parse_dns_ip_results(struct dnshdr* dns_hdr) { (void) dns_hdr; return strdup(""); // This is why we don't accept pull requests #if 0 // parse through dns_query since it can be of variable length char* dns_ans_start = (char *) (&dns_hdr[1]); while (*dns_ans_start++); // <---- SERIOUSLY F**K THAT // skip qtype and qclass octets dns_ans_start += 4; // number of answers * 16 chars each (each followed by space or null, and quotes) size_t size = ntohs(dns_hdr->ancount)*INET_ADDRSTRLEN+2; char* ip_addrs = malloc(size); // should always be 4 for ipv4 addrs, but include in case of unexpected response //uint16_t prev_data_len = 4; int output_pos = 0; if(ntohs(dns_hdr->ancount) > 1000){ return NULL; } for (int i = 0; i < ntohs(dns_hdr->ancount); i++) { //dnsans* dns_ans = (dnsans *) ((char*) dns_ans_start + (12 + prev_data_len)*i); dnsans* dns_ans = (dnsans *) ((char*) dns_ans_start + (12)*i); if(!dns_ans->addr){ //prev_data_len = ntohs(dns_ans->length); continue; } char* ip_addr = hex_to_ip(&dns_ans->addr); if (!ip_addr) { //prev_data_len = ntohs(dns_ans->length); continue; } output_pos += i == 0 ? sprintf(ip_addrs + output_pos, "\"%s", ip_addr) : sprintf(ip_addrs + output_pos, " %s", ip_addr); //prev_data_len = ntohs(dns_ans->length); } if (output_pos) { sprintf(ip_addrs + output_pos, "\""); } return ip_addrs; #endif }
void parse_ip(char * packet, int packet_size){ int BUF_SIZE = 9; int pointer = 0; unsigned long int IHL = 0; unsigned long int proto = 0; unsigned long int length = 0; if(strncmp(packet, "4", 1) == 0){ printf("IPv4\n"); } else if(strncmp(packet, "6", 1) == 0){ printf("IPv6\nNOT SUPPORTED\n"); return; } else{ printf("Unknown IP type\n"); return; } pointer += 1; char * buf = (char *) malloc(BUF_SIZE); memset(buf, 0, BUF_SIZE); //IHL strncpy(buf, packet + pointer, 1); printf("IHL: %s (%d * 4 = %dbytes)\n", buf, strtoul(buf, NULL, 16), strtoul(buf, NULL, 16) * 4); IHL = strtoul(buf, NULL, 16); pointer += 1; //DSCP and ECN strncpy(buf, packet + pointer, 2); printf("DSCP & ECN: %s\n", buf); pointer += 2; //Total lenght strncpy(buf, packet + pointer, 4); length = strtoul(buf, NULL, 16); printf("Total length: %s (%dbytes)\n", buf, length); pointer += 4; //Identification strncpy(buf, packet + pointer, 4); printf("Identification: %s (%d)\n", buf, strtoul(buf, NULL, 16)); pointer += 4; //Flags and Frag offset strncpy(buf, packet + pointer, 4); printf("Flags and Frag: %s\n", buf); pointer += 4; memset(buf, 0, BUF_SIZE); //Ugly hack as sizes get smaller again //TTL strncpy(buf, packet + pointer, 2); printf("TTL: %s (%d)\n", buf, strtoul(buf, NULL, 16)); pointer += 2; //Proto strncpy(buf, packet + pointer, 2); proto = strtoul(buf, NULL, 16); printf("Proto: %s (%d)\n", buf, proto); pointer += 2; //Checksum strncpy(buf, packet + pointer, 4); printf("Checksum: %s (%d)\n", buf, strtoul(buf, NULL, 16)); pointer += 4; //Source strncpy(buf, packet + pointer, 8); printf("Source: %s (%s)\n", buf, hex_to_ip(buf)); pointer += 8; //Dest strncpy(buf, packet + pointer, 8); printf("Dest: %s (%s)\n", buf, hex_to_ip(buf)); pointer += 8; if(IHL > 5){ printf("OPTIONS NOT IMPLEMENTED\n"); pointer = IHL * 8; } printf("\n"); free(buf); if(TOTAL_SIZE == 0){ TOTAL_SIZE = length; } SIZE_PTR += pointer / 2; printf("SIZE_PTR: %d\n", SIZE_PTR); if(proto == ICMP){ parse_icmp(packet + pointer, packet_size - pointer); } else if(proto == TCP){ SIZE_PTR += parse_tcp(packet + pointer, packet_size - pointer); } else if(proto == UDP){ SIZE_PTR += parse_udp(packet + pointer, packet_size - pointer); } }