Beispiel #1
0
/*
Expects everything but len in network order
 */
void send_time_exceeded(struct sr_instance *sr, uint8_t *pkt) {

  /* Need to buffer enough space to include the unused 32 bits, the IP
  header length, and 64 bits first bits of original datagrams data  */
  unsigned int packet_size = sizeof(sr_ethernet_hdr_t) +
			      sizeof(sr_ip_hdr_t) + sizeof(sr_icmp_hdr_t)
			      + UNUSED_BYTE_LENGTH_TIME_EXCEEDED_ICMP + ICMP_DATA_SIZE; 

  uint8_t *outgoing_pkt = malloc(packet_size);
  
  sr_ethernet_hdr_t *outgoing_eth_hdr = get_ethernet_hdr(outgoing_pkt);
  sr_ip_hdr_t *outgoing_ip_hdr = get_ip_hdr(outgoing_pkt);
  sr_icmp_hdr_t *outgoing_icmp_hdr = get_icmp_hdr(outgoing_pkt);

  uint32_t ip_dst = get_ip_hdr(pkt)->ip_src;
  struct sr_rt *outgoing_routing_entry = find_lpm_in_routing_table(sr->routing_table, ntohl(ip_dst));
  if (!outgoing_routing_entry) {
    free(pkt);
    free(outgoing_pkt);
    return;
  }
  struct sr_if *outgoing_inf = sr_get_interface(sr, outgoing_routing_entry->interface);

  /*Ethernet*/
  memcpy(outgoing_eth_hdr->ether_shost, outgoing_inf->addr, ETHER_ADDR_LEN);
  outgoing_eth_hdr->ether_type = htons(ethertype_ip);

  /*IP*/
  populate_ip_hdr_normal(outgoing_ip_hdr);
  outgoing_ip_hdr->ip_len = htons(packet_size - sizeof(sr_ethernet_hdr_t));
  outgoing_ip_hdr->ip_p = ip_protocol_icmp;
  outgoing_ip_hdr->ip_sum = 0;
  outgoing_ip_hdr->ip_src = outgoing_inf->ip;
  outgoing_ip_hdr->ip_dst = ip_dst;
  outgoing_ip_hdr->ip_sum = cksum(outgoing_ip_hdr, sizeof(sr_ip_hdr_t));

  /*ICMP*/
  outgoing_icmp_hdr->icmp_type = ICMP_TYPE_TIME_EXCEEDED;
  outgoing_icmp_hdr->icmp_code = ICMP_CODE_TIME_EXCEEDED;
  outgoing_icmp_hdr->icmp_sum = 0;
  memset((char *)outgoing_icmp_hdr + sizeof(sr_icmp_hdr_t), 0, UNUSED_BYTE_LENGTH_TIME_EXCEEDED_ICMP); 
  memcpy((char *)outgoing_icmp_hdr + sizeof(sr_icmp_hdr_t) + UNUSED_BYTE_LENGTH_TIME_EXCEEDED_ICMP, 
	 get_ip_hdr(pkt), ICMP_DATA_SIZE);
  outgoing_icmp_hdr->icmp_sum = cksum(outgoing_icmp_hdr,
				      sizeof(sr_icmp_hdr_t) + UNUSED_BYTE_LENGTH_TIME_EXCEEDED_ICMP + ICMP_DATA_SIZE);

  send_or_queue_packet_based_on_arp_entry_existence(sr, outgoing_routing_entry,
						    outgoing_pkt, packet_size);
}
Beispiel #2
0
/*
Expects entire ethernet frame, still in network order.
 */
void handle_ip_packet_destined_to_router(struct sr_instance *sr, uint8_t *pkt, sr_ip_hdr_t *ip_hdr, unsigned int len) {  
  if (ip_hdr->ip_p == ip_protocol_icmp) {
    sr_icmp_hdr_t *icmp_hdr = get_icmp_hdr(pkt);

    if (icmp_packet_echo_request_sanity_check(icmp_hdr, len) && icmp_hdr->icmp_type == ICMP_TYPE_ECHO_REQUEST && icmp_hdr->icmp_code == ICMP_CODE_ECHO_REQUEST) {
      send_echo_reply(sr, pkt, len);
    }

  } else if (ip_hdr->ip_p == ip_protocol_tcp || 
	     ip_hdr->ip_p == ip_protocol_udp) {
    send_icmp_port_unreachable(sr, pkt, len);
  } else {
    printf("unexpected protocol in ip hdr.\n");
  }
}
Beispiel #3
0
/*
Expects everything but len in network order
 */
void send_echo_reply(struct sr_instance *sr, uint8_t *pkt, unsigned int len) {
  uint8_t *outgoing_pkt = malloc(len);
  
  sr_ethernet_hdr_t *outgoing_eth_hdr = get_ethernet_hdr(outgoing_pkt);
  sr_ip_hdr_t *outgoing_ip_hdr = get_ip_hdr(outgoing_pkt);
  sr_icmp_hdr_t *outgoing_icmp_hdr = get_icmp_hdr(outgoing_pkt);

  uint32_t ip_dst = get_ip_hdr(pkt)->ip_src;
  struct sr_rt *outgoing_routing_entry = find_lpm_in_routing_table(sr->routing_table, ntohl(ip_dst));
  if (!outgoing_routing_entry) {
    free(pkt);
    free(outgoing_pkt);
    return;
  }
  struct sr_if *outgoing_inf = sr_get_interface(sr, outgoing_routing_entry->interface);

  /*Ethernet*/
  memcpy(outgoing_eth_hdr->ether_shost, outgoing_inf->addr, ETHER_ADDR_LEN);
  outgoing_eth_hdr->ether_type = htons(ethertype_ip);

  /*IP*/
  populate_ip_hdr_normal(outgoing_ip_hdr);
  outgoing_ip_hdr->ip_len = htons(len - sizeof(sr_ethernet_hdr_t));
  outgoing_ip_hdr->ip_p = ip_protocol_icmp;
  outgoing_ip_hdr->ip_sum = 0;
  outgoing_ip_hdr->ip_src = get_ip_hdr(pkt)->ip_dst;
  outgoing_ip_hdr->ip_dst = ip_dst;
  outgoing_ip_hdr->ip_sum = cksum(outgoing_ip_hdr, sizeof(sr_ip_hdr_t));

  /*ICMP*/
  outgoing_icmp_hdr->icmp_type = ICMP_TYPE_ECHO_REPLY;
  outgoing_icmp_hdr->icmp_code = ICMP_CODE_ECHO_REPLY;
  outgoing_icmp_hdr->icmp_sum = 0;
  memcpy((char *)outgoing_icmp_hdr + sizeof(sr_icmp_hdr_t), pkt +
	 sizeof(sr_ethernet_hdr_t) + sizeof(sr_ip_hdr_t) +
	 sizeof(sr_icmp_hdr_t), len - sizeof(sr_ethernet_hdr_t) - sizeof(sr_ip_hdr_t) -
	 sizeof(sr_icmp_hdr_t));
  outgoing_icmp_hdr->icmp_sum = cksum(outgoing_icmp_hdr,
				      ntohs(outgoing_ip_hdr->ip_len) - sizeof(sr_ip_hdr_t));


  send_or_queue_packet_based_on_arp_entry_existence(sr, outgoing_routing_entry,
						    outgoing_pkt, len);
}
Beispiel #4
0
void print_sping_queue(struct sr_instance *sr)
{
	assert(sr);

	printf("\n\n*******\nSPING QUEUE CONTENTS\n");
	router_state *rs = get_router_state(sr);
	node *sping_walker = 0;
	sping_queue_entry *ae = 0;

	sping_walker = rs->sping_queue;
	while(sping_walker)
	{
		ae = (sping_queue_entry *)sping_walker->data;
		uint8_t *data =(uint8_t*) get_icmp_hdr(ae->packet, ae->len);
		data = data+sizeof(icmp_hdr);
		unsigned short *id = (unsigned short *)data;
		printf("%X\n", *id);

		sping_walker = sping_walker->next;
	}
}
Beispiel #5
0
/* PRETTY PRINT ICMP PACKET HEADER */
void print_icmp_load(const uint8_t *packet, unsigned int len)
{
	assert(packet);

	icmp_hdr *icmp = get_icmp_hdr(packet, len);

	indent(1);
	printf("ICMP Packet Header (%d bytes)\n", sizeof(icmp_hdr));
	indent(2);
	switch(icmp->icmp_type) {
		case ICMP_TYPE_ECHO_REPLY:
		{
			printf("Type = Echo reply\n");
			indent(2);
			printf("Code = %d\n", icmp->icmp_code);
			break;
		}
		case ICMP_TYPE_ECHO_REQUEST:
		{
			printf("Type = Echo request\n");
			indent(2);
			printf("Code = %d\n", icmp->icmp_code);
			break;
		}
		case ICMP_TYPE_TIME_EXCEEDED:
		{
			printf("Type = Time exceeded\n");
			indent(2);
			printf("Code = %d\n", icmp->icmp_code);
			break;
		}
		case ICMP_TYPE_DESTINATION_UNREACHABLE:
		{
			printf("Type = Destination unreachable\n");
			indent(2);
			switch(icmp->icmp_code) {
				case 0:
				{
					printf("Code = Network unreachable\n error\n");
					break;
				}
				case 1:
				{
					printf("Code = Host unreachable error\n");
					break;
				}
				case 2:
				{
					printf("Code = Protocol unreachable error\n");
					break;
				}
				case 3:
				{
					printf("Code = Port unreachable error\n");
					break;
				}
			} /* end of switch(icmp->icmp_code)) */
			break;
		}
		default:
		{
			printf("Type = %d\n", icmp->icmp_type);
			indent(2);
       			printf("Code = %d\n", icmp->icmp_code);
			break;
		}
	}/* end of switch(icmp->icmp_type)) */
	indent(2);
	printf("Checksum = 0x%X\n", htons(icmp->icmp_sum));

	indent(2);
 	uint8_t* data = (uint8_t *) icmp;
	data += sizeof(icmp_hdr);
	int data_len = len - (sizeof(eth_hdr)+sizeof(ip_hdr)+sizeof(icmp_hdr));
	printf("\nPayload 1(%d bytes)\n", data_len);
	int i; for(i=0; i<data_len; i++)
	{ printf("%X ", data[i]); }
	printf("\n\n");
}
Beispiel #6
0
/*
 * HELPER function called from arp_thread
 */
void process_arp_queue(struct sr_instance* sr) {
	router_state* rs = get_router_state(sr);
	node* n = get_router_state(sr)->arp_queue;
	node* next = NULL;
	time_t now;
	double diff;

	while (n) {
		next = n->next;

		arp_queue_entry* aqe = (arp_queue_entry*)n->data;

		/* has it been over a second since the last arp request was sent? */
		time(&now);
		diff = difftime(now, aqe->last_req_time);
		if (diff > 1) {
			/* have we sent less than 5 arp requests? */
			if (aqe->requests < 5) {
				/* send another */
				time(&(aqe->last_req_time));
				++(aqe->requests);
				send_arp_request(sr, aqe->next_hop.s_addr, aqe->out_iface_name);
			} else {
				/* we have exceeded the max arp requests, return packets to sender */
				node* cur_packet_node = aqe->head;
				node* next_packet_node = NULL;

				while (cur_packet_node) {
					/* send icmp for the packet, free it, and its encasing entry */
					arp_queue_packet_entry* aqpe = (arp_queue_packet_entry*)cur_packet_node->data;

					/* only send an icmp error if the packet is not icmp, or if it is, its an echo request or reply
					 * also ensure we don't send an icmp error back to one of our interfaces
					 */
					if ((get_ip_hdr(aqpe->packet, aqpe->len)->ip_p != IP_PROTO_ICMP) ||
							(get_icmp_hdr(aqpe->packet, aqpe->len)->icmp_type == ICMP_TYPE_ECHO_REPLY) ||
							(get_icmp_hdr(aqpe->packet, aqpe->len)->icmp_type == ICMP_TYPE_ECHO_REQUEST)) {

					 	/* also ensure we don't send an icmp error back to one of our interfaces */
						if (!iface_match_ip(rs, get_ip_hdr(aqpe->packet, aqpe->len)->ip_src.s_addr)) {
							/* Total hack here to increment the TTL since we already decremented it earlier in the pipeline
							 * and the ICMP error should return the original packet.
							 * TODO: Don't decrement the TTL until the packet is ready to be put on the wire
							 * and we have the next hop ARP address, although checking should be done
							 * where it is currently being decremented to minimize effort on a doomed packet */
							ip_hdr *ip = get_ip_hdr(aqpe->packet, aqpe->len);
							if (ip->ip_ttl < 255) {
								ip->ip_ttl++;

								/* recalculate checksum */
								bzero(&ip->ip_sum, sizeof(uint16_t));
								uint16_t checksum = htons(compute_ip_checksum(ip));
								ip->ip_sum = checksum;
							}

							send_icmp_packet(sr, aqpe->packet, aqpe->len, ICMP_TYPE_DESTINATION_UNREACHABLE, ICMP_CODE_HOST_UNREACHABLE);
						}
					}

					free(aqpe->packet);
					next_packet_node = cur_packet_node->next;
					//free(cur_packet_node);   /* IS THIS CORRECT TO FREE IT ? */
					node_remove(&(aqe->head), cur_packet_node);
					cur_packet_node = next_packet_node;
				}

				/* free the arp queue entry for this destination ip, and patch the list */
				node_remove(&(get_router_state(sr)->arp_queue), n);
			}
		}

		n = next;
	}
}
Beispiel #7
0
/* Handle IP packet */
void sr_handle_ippacket(struct sr_instance* sr,
        uint8_t * packet/* lent */,
        unsigned int len,
        char* interface/* lent */) 
{
    assert(sr);
    assert(packet);
    assert(interface);

    /* Get ethernet header */
    sr_ethernet_hdr_t *eth_hdr = get_eth_hdr(packet);
	if (eth_hdr == NULL) {
		printf("ethernet header NULL!!!\n");
		return;
	}

    /* Get ip header */
    sr_ip_hdr_t *ip_hdr = get_ip_hdr(packet);
	if (ip_hdr == NULL) {
		printf("ip header NULL!!!\n");
		return;
	}

	/* Before doing ttl decrement, check checksum */
	uint16_t old_ip_sum = ip_hdr->ip_sum;
	ip_hdr->ip_sum = 0;

	if (!verify_checksum(ip_hdr, sizeof(sr_ip_hdr_t), old_ip_sum)) {
		fprintf(stderr, "CHECKSUM FAILED!!\n");
		return;
	}
	ip_hdr->ip_sum = old_ip_sum;

    /* Get the arp cache */
    struct sr_arpcache *sr_arp_cache = &sr->cache;

    /* Get the destination interface on the router */
	struct sr_if *sr_iface = sr_get_router_if(sr, ip_hdr->ip_dst);
	/* Get the connected interface on the router */
	struct sr_if *sr_con_if = sr_get_interface(sr, interface);

    /* Check the time exceeded condition, if ttl==0, we need to form icmp 11 and send back */
    if (ip_hdr->ip_ttl <= 1) {
        /* time exceeded message and icmp type 11 */
        printf("TTL time exceeded\n");
        int packet_len = ICMP_T3_PACKET_LEN;
        uint8_t *icmp_t3_hdr = (uint8_t *)malloc(packet_len);

        create_ethernet_hdr(eth_hdr, (sr_ethernet_hdr_t *)icmp_t3_hdr, sr_con_if);
        /* Create ip header */
        create_echo_ip_hdr(ip_hdr, (sr_ip_hdr_t *)((char *)icmp_t3_hdr+ETHER_PACKET_LEN), sr_con_if);

        /* Send icmp type 11 time exceeded */
        /* icmp_t3 type=11, code=0 */
        create_icmp_t3_hdr(ip_hdr, (sr_icmp_t3_hdr_t *)((char *)icmp_t3_hdr+IP_PACKET_LEN), 11, 0);

        /* Send icmp type 11 packet */
		struct sr_arpentry *arp_entry = sr_arpcache_lookup(sr_arp_cache, ip_hdr->ip_src);
		if (arp_entry != NULL) {
			sr_send_packet(sr, icmp_t3_hdr, packet_len, sr_con_if->name);
			free(icmp_t3_hdr);
		} else {
			struct sr_arpreq *arp_req = sr_arpcache_queuereq(sr_arp_cache, ip_hdr->ip_src, icmp_t3_hdr, packet_len, sr_con_if->name);
			handle_arpreq(arp_req, sr);
		}
        return;
    }

    /* Get the protocol from IP */
    uint8_t ip_p = ip_hdr->ip_p;

    /* If the packet is sent to self, meaning the ip is sent to the router */
    if (sr_iface) {
        /* Check the protocol if it is icmp */
        if (ip_p == ip_protocol_icmp) {
            /* Get the icmp header */
            sr_icmp_hdr_t *icmp_hdr = get_icmp_hdr(packet);

            /* Check if it is ICMP echo request */
            /* icmp_echo_req = 8 */
            if (icmp_hdr->icmp_type == 8) {
				/* Do LPM on the routing table */
        		/* Check the routing table and see if the incoming ip matches the routing table ip, and find LPM router entry */
        		struct sr_rt *longest_pref_match = sr_lpm(sr, ip_hdr->ip_src);

				if (longest_pref_match) {
					/* check ARP cache */
					struct sr_arpentry *arp_entry = sr_arpcache_lookup(&sr->cache, longest_pref_match->gw.s_addr);
					struct sr_if *out_iface = sr_get_interface(sr, longest_pref_match->interface);

					/* If hit, meaning the arp mapping has been cached */
					if (arp_entry != NULL) {
						/* We need to send the icmp echo reply */
				        /* Modify ethernet header */
						memcpy(eth_hdr->ether_dhost, eth_hdr->ether_shost, ETHER_ADDR_LEN);
						memcpy(eth_hdr->ether_shost, out_iface->addr, ETHER_ADDR_LEN);

				        /* Modify ip header */
						ip_hdr->ip_off = htons(0b0100000000000000);        /* fragment offset field */
						ip_hdr->ip_ttl = 100;                    			/* time to live */
						uint32_t temp = ip_hdr->ip_src;
						ip_hdr->ip_src = ip_hdr->ip_dst;        /* source address */
						ip_hdr->ip_dst = temp;        			/* dest address */
						ip_hdr->ip_sum = 0;
						ip_hdr->ip_sum = cksum(ip_hdr, sizeof(sr_ip_hdr_t));			/* checksum */

				        /* Modify icmp header */
						unsigned int icmp_whole_size = len - IP_PACKET_LEN;
						icmp_hdr->icmp_type = 0;
						icmp_hdr->icmp_code = 0;
						icmp_hdr->icmp_sum = 0;
						icmp_hdr->icmp_sum = cksum(icmp_hdr, icmp_whole_size);

				        /* Send icmp echo reply */
				        sr_send_packet(sr, packet, len, out_iface->name);
				        return;
					}
					/* Else no hit, we cache it to the queue and send arp request */ 
					else {
						/* Add reply to the ARP queue */
						/* We need to send the icmp echo reply */
				        /* Modify ethernet header */
						memcpy(eth_hdr->ether_dhost, eth_hdr->ether_shost, ETHER_ADDR_LEN);
						memcpy(eth_hdr->ether_shost, sr_con_if->addr, ETHER_ADDR_LEN);

				        /* Modify ip header */
						ip_hdr->ip_off = htons(0b0100000000000000);        /* fragment offset field */
						ip_hdr->ip_ttl = 100;                    			/* time to live */
						uint32_t temp = ip_hdr->ip_src;
						ip_hdr->ip_src = ip_hdr->ip_dst;        /* source address */
						ip_hdr->ip_dst = temp;        			/* dest address */
						ip_hdr->ip_sum = 0;
						ip_hdr->ip_sum = cksum(ip_hdr, sizeof(sr_ip_hdr_t));			/* checksum */

				        /* Modify icmp header */
						unsigned int icmp_whole_size = len - IP_PACKET_LEN;
						icmp_hdr->icmp_type = 0;
						icmp_hdr->icmp_code = 0;
						icmp_hdr->icmp_sum = 0;
						icmp_hdr->icmp_sum = cksum(icmp_hdr, icmp_whole_size);
						struct sr_arpreq *arp_req = sr_arpcache_queuereq(sr_arp_cache, ip_hdr->ip_dst, packet, len, out_iface->name);
						/* Send ARP request, which is a broadcast */
						handle_arpreq(arp_req, sr);
						return;
					}
				} else {
					fprintf(stderr, "Longest prefix doesnt match!!\n");
                	return;
				}

            } else {
                fprintf(stderr, "Not an ICMP request!\n");
                return;
            }
        }
        /* Else it is TCP/UDP request */
        else {
            fprintf(stderr, "*** -> Received TCP/UDP!\n");

			/* Do LPM on the routing table */
    		/* Check the routing table and see if the incoming ip matches the routing table ip, and find LPM router entry */
    		struct sr_rt *longest_pref_match = sr_lpm(sr, ip_hdr->ip_src);

			if (longest_pref_match) {
				/* check ARP cache */
				struct sr_arpentry *arp_entry = sr_arpcache_lookup(&sr->cache, longest_pref_match->gw.s_addr);
				struct sr_if *out_iface = sr_get_interface(sr, longest_pref_match->interface);
				
				/* Send ICMP port unreachable */
				if (arp_entry != NULL) {

					int packet_len = ICMP_T3_PACKET_LEN;
				    uint8_t *icmp_t3_hdr = (uint8_t *)malloc(packet_len);

				    /* Create ethernet header */
				    create_ethernet_hdr(eth_hdr, (sr_ethernet_hdr_t *)icmp_t3_hdr, sr_iface);
					/*memcpy(((sr_ethernet_hdr_t *)icmp_t3_hdr)->ether_dhost, eth_hdr->ether_shost, ETHER_ADDR_LEN);
					memcpy(((sr_ethernet_hdr_t *)icmp_t3_hdr)->ether_shost, eth_hdr->ether_dhost, ETHER_ADDR_LEN);*/

				    /* Create ip header */
				    create_echo_ip_hdr(ip_hdr, (sr_ip_hdr_t *)((char *)icmp_t3_hdr+ETHER_PACKET_LEN), sr_iface);
					/*sr_ip_hdr_t *icmp_t3_hdr_ip = (sr_ip_hdr_t *)((char *)icmp_t3_hdr+ETHER_PACKET_LEN);
					icmp_t3_hdr_ip->ip_src = ip_hdr->ip_dst;
					icmp_t3_hdr_ip->ip_sum = 0;
					icmp_t3_hdr_ip->ip_sum = cksum(icmp_t3_hdr_ip, sizeof(sr_ip_hdr_t));*/
					

					/* Should update source address to be interface address */

				    /* Send icmp type 3 port unreachable */
				    /* Create icmp port unreachable packet */
				    /* icmp_t3 type=3, code=3 */
				    create_icmp_t3_hdr(ip_hdr, (sr_icmp_t3_hdr_t *)((char *)icmp_t3_hdr+IP_PACKET_LEN), 3, 3);

				    /* Send icmp type 3 packet */
				    sr_send_packet(sr, icmp_t3_hdr, packet_len, out_iface->name);

				    free(icmp_t3_hdr);
				    return;
				} else {
				
					int packet_len = ICMP_T3_PACKET_LEN;
				    uint8_t *icmp_t3_hdr = (uint8_t *)malloc(packet_len);

				    /* Create ethernet header */
				    create_ethernet_hdr(eth_hdr, (sr_ethernet_hdr_t *)icmp_t3_hdr, sr_iface);
					/*memcpy(((sr_ethernet_hdr_t *)icmp_t3_hdr)->ether_dhost, eth_hdr->ether_shost, ETHER_ADDR_LEN);
					memcpy(((sr_ethernet_hdr_t *)icmp_t3_hdr)->ether_shost, eth_hdr->ether_dhost, ETHER_ADDR_LEN);*/

				    /* Create ip header */
				    create_echo_ip_hdr(ip_hdr, (sr_ip_hdr_t *)((char *)icmp_t3_hdr+ETHER_PACKET_LEN), sr_iface);
					/*sr_ip_hdr_t *icmp_t3_hdr_ip = (sr_ip_hdr_t *)((char *)icmp_t3_hdr+ETHER_PACKET_LEN);
					icmp_t3_hdr_ip->ip_src = ip_hdr->ip_dst;
					icmp_t3_hdr_ip->ip_sum = 0;
					icmp_t3_hdr_ip->ip_sum = cksum(icmp_t3_hdr_ip, sizeof(sr_ip_hdr_t));*/

				    /* Send icmp type 3 port unreachable */
				    /* Create icmp port unreachable packet */
				    /* icmp_t3 type=3, code=3 */
				    create_icmp_t3_hdr(ip_hdr, (sr_icmp_t3_hdr_t *)((char *)icmp_t3_hdr+IP_PACKET_LEN), 3, 3);

					struct sr_arpreq *arp_req = sr_arpcache_queuereq(sr_arp_cache, ip_hdr->ip_src, icmp_t3_hdr, packet_len, out_iface->name);
					/* Send ARP request, which is a broadcast */
					handle_arpreq(arp_req, sr);
					return;
				}
			} else {
				fprintf(stderr, "Longest prefix doesnt match!!\n");
                return;
			}            
            
        }
    }
    /* Else Check the routing table, perfomr LPM */
    else {
        /* Sanity-check the packet */
        /* minimum length */
        if (!check_min_length(len, IP_PACKET_LEN)) {
            fprintf(stderr, "The packet length is not enough:(\n");
            return;
        }
		
        /* Do LPM on the routing table */
        /* Check the routing table and see if the incoming ip matches the routing table ip, and find LPM router entry */
        struct sr_rt *longest_pref_match = sr_lpm(sr, ip_hdr->ip_dst);
        if (longest_pref_match) {
            /* check ARP cache */
			struct sr_if *out_iface = sr_get_interface(sr, longest_pref_match->interface);

            struct sr_arpentry *arp_entry = sr_arpcache_lookup(&sr->cache, longest_pref_match->gw.s_addr); /* ip_hdr->ip_dst */
         
			/* If hit, meaning the arp_entry is found */
            if (arp_entry) {

				/*fprintf(stderr, "************ found the lpm router entry ***********\n");*/
                /* Send frame to next hop */
                /* update the eth_hdr source and destination ethernet address */
                /* use next_hop_ip->mac mapping in the entry to send the packet */

                ip_hdr->ip_ttl--;

                /* recompute the packet checksum over the modified header */
                ip_hdr->ip_sum = 0;
                uint16_t new_ip_sum = cksum(ip_hdr, sizeof(sr_ip_hdr_t));
                ip_hdr->ip_sum = new_ip_sum;

                memcpy(eth_hdr->ether_shost, out_iface->addr, ETHER_ADDR_LEN);
                memcpy(eth_hdr->ether_dhost, arp_entry->mac, ETHER_ADDR_LEN);
                sr_send_packet(sr, packet, len, out_iface->name);
				print_hdr_ip((uint8_t*)ip_hdr);
                /* free the entry */
                free(arp_entry);
                return;
            } else/* No Hit */ {
                /* send an ARP request for the next-hop IP */
                /* add the packet to the queue of packets waiting on this ARP request */
                /* Add request to ARP queue*/

                ip_hdr->ip_ttl--;

                /* recompute the packet checksum over the modified header */
                ip_hdr->ip_sum = 0;
                uint16_t new_ip_sum = cksum(ip_hdr, sizeof(sr_ip_hdr_t));
                ip_hdr->ip_sum = new_ip_sum;

                struct sr_arpreq *arp_req = sr_arpcache_queuereq(sr_arp_cache, ip_hdr->ip_dst, packet, len, out_iface->name);
                /* send ARP request, this is a broadcast */
                handle_arpreq(arp_req, sr);
                return;
            }
        } else /* if not matched */ {
            /* Send ICMP net unreachable */
			printf("--------------- Net Unreachable ---------------\n");

			/* Do LPM on the routing table */
    		/* Check the routing table and see if the incoming ip matches the routing table ip, and find LPM router entry */
    		struct sr_rt *longest_pref_match = sr_lpm(sr, ip_hdr->ip_src);

			if (longest_pref_match) {
				/* check ARP cache */
				struct sr_arpentry *arp_entry = sr_arpcache_lookup(&sr->cache, longest_pref_match->gw.s_addr);
				struct sr_if *out_iface = sr_get_interface(sr, longest_pref_match->interface);

				if (arp_entry) {
					int packet_len = ICMP_T3_PACKET_LEN;
				    uint8_t *icmp_t3_hdr = (uint8_t *)malloc(packet_len);

				    /* Create ethernet header */
				    create_ethernet_hdr(eth_hdr, (sr_ethernet_hdr_t *)icmp_t3_hdr, out_iface);

				    /* Create ip header */
				    create_echo_ip_hdr(ip_hdr, (sr_ip_hdr_t *)((char *)icmp_t3_hdr+ETHER_PACKET_LEN), out_iface);

				    /* Create icmp net unreachable */
				    /* icmp_t3 type=3, code=0 */
				    create_icmp_t3_hdr(ip_hdr, (sr_icmp_t3_hdr_t *)((char *)icmp_t3_hdr+IP_PACKET_LEN), 3, 0);

				    /* Send icmp type 3 packet */
				    sr_send_packet(sr, icmp_t3_hdr, packet_len, out_iface->name);

				    free(icmp_t3_hdr);
				    return;
				} else {

					int packet_len = ICMP_T3_PACKET_LEN;
				    uint8_t *icmp_t3_hdr = (uint8_t *)malloc(packet_len);

				    /* Create ethernet header */
				    create_ethernet_hdr(eth_hdr, (sr_ethernet_hdr_t *)icmp_t3_hdr, out_iface);

				    /* Create ip header */
				    create_echo_ip_hdr(ip_hdr, (sr_ip_hdr_t *)((char *)icmp_t3_hdr+ETHER_PACKET_LEN), out_iface);
					/*	((sr_ip_hdr_t *)((char *)icmp_t3_hdr+ETHER_PACKET_LEN))->ip_ttl += 1; */

				    /* Send icmp type 3 net unreachable */
				    /* Create icmp net unreachable packet */
				    /* icmp_t3 type=3, code=0 */
				    create_icmp_t3_hdr(ip_hdr, (sr_icmp_t3_hdr_t *)((char *)icmp_t3_hdr+IP_PACKET_LEN), 3, 0);

					struct sr_arpreq *arp_req = sr_arpcache_queuereq(sr_arp_cache, ip_hdr->ip_src, icmp_t3_hdr, packet_len, out_iface->name);
					/* Send ARP request, which is a broadcast */
					handle_arpreq(arp_req, sr);
					return;
				}
			} else {
				fprintf(stderr, "Longest prefix doesnt match!!\n");
                return;
			}
        }
    }

    return;
}