int dns_init_perthread(void* buf, macaddr_t *src, macaddr_t *gw, UNUSED port_h_t dst_port, UNUSED void **arg_ptr) { memset(buf, 0, MAX_PACKET_SIZE); struct ether_header *eth_header = (struct ether_header *) buf; make_eth_header(eth_header, src, gw); struct ip *ip_header = (struct ip*)(ð_header[1]); uint16_t len = htons(sizeof(struct ip) + sizeof(struct udphdr) + dns_packet_len); make_ip_header(ip_header, IPPROTO_UDP, len); struct udphdr *udp_header = (struct udphdr*)(&ip_header[1]); len = sizeof(struct udphdr) + dns_packet_len; make_udp_header(udp_header, zconf.target_port, len); char* payload = (char*)(&udp_header[1]); module_dns.packet_length = sizeof(struct ether_header) + sizeof(struct ip) + sizeof(struct udphdr) + dns_packet_len; assert(module_dns.packet_length <= MAX_PACKET_SIZE); memcpy(payload, dns_packet, dns_packet_len); return EXIT_SUCCESS; }
void ip_handle_packet( sr_router* router, byte* ip_packet, interface_t* intf ) { printf(" ** ip_handle_packet(..) called \n"); // construct ip header struct ip* ip_header = make_ip_header(ip_packet); byte* ip_header_raw = (byte*) malloc_or_die(ip_header->ip_hl * 4); memcpy(ip_header_raw, ip_packet, ip_header->ip_hl * 4); // check if ip header is correct bool ret = check_ip_header(ip_header, ip_header_raw); if(ret == TRUE) { printf(" ** ip_handle_packet(..) header sanity checks passed\n"); // display ip header printf(" ** ip_handle_packet(..) packet contents \n"); display_ip_header(ip_header); // get payload uint16_t payload_len = ip_header->ip_len - (ip_header->ip_hl * 4); printf(" ** ip_handle_packet(..) Payload size %u bytes \n", payload_len); byte* payload = (byte*) malloc_or_die(payload_len); memcpy(payload, ip_packet + (ip_header->ip_hl * 4), payload_len); //free(ip_header_raw); // check destination printf(" ** ip_handle_packet(..) checking packet destination \n"); // check if spf broadcast packet char* all_spf_routers = ALL_SPF_ROUTERS_IP; if(check_packet_destination(ip_header->ip_dst, router) == TRUE || ip_header->ip_dst.s_addr == make_ip_addr(all_spf_routers)) { printf(" ** ip_handle_packet(..) yes I'm the destination \n"); // check if ttl expired /* if(ip_header->ip_ttl == 1) { printf(" ** ip_handle_packet(..) ttl expired, sending ICMP ttl error message \n"); uint8_t code = ICMP_TYPE_CODE_TTL_TRANSIT; icmp_type_ttl_send(&code, payload, (uint8_t*)&ip_header->ip_len, ip_header); } else*/ { // check protocol printf(" ** ip_handle_packet(..) checking protocol \n"); check_packet_protocol(router, ip_header, payload, payload_len, intf, ip_packet); } } else { printf(" ** ip_handle_packet(..) no I'm not the destination, send to ip lookup \n"); ip_header->ip_len = htons((ip_header->ip_len)); ip_header->ip_off = htons((ip_header->ip_off)); ip_header->ip_id = htons((ip_header->ip_id)); ip_header->ip_sum = htons((ip_header->ip_sum)); ip_look_up_reply(ip_packet, ip_header, intf); } } else printf(" ** ip_handle_packet(..) header sanity checks failed\n"); //free(ip_packet); //free(ip_header); //free(ip_header_raw); }
int synscan_init_perthread(void* buf, macaddr_t *src, macaddr_t *gw, port_h_t dst_port) { memset(buf, 0, MAX_PACKET_SIZE); struct ether_header *eth_header = (struct ether_header *) buf; make_eth_header(eth_header, src, gw); struct ip *ip_header = (struct ip*)(ð_header[1]); uint16_t len = htons(sizeof(struct ip) + sizeof(struct tcphdr)); make_ip_header(ip_header, IPPROTO_TCP, len); struct tcphdr *tcp_header = (struct tcphdr*)(&ip_header[1]); make_tcp_header(tcp_header, dst_port); return EXIT_SUCCESS; }