Example #1
0
void sr_handlepacket(struct sr_instance* sr,
        uint8_t * packet/* lent */,
        unsigned int len,
        char* interface/* lent */)
{
  /* REQUIRES */
  assert(sr);
  assert(packet);
  assert(interface);

  printf("*** -> Received packet of length %d \n",len);

	if (ethertype(packet) == ethertype_arp) {			/* ARP packet */
		struct sr_arp_hdr *arpHeader = (struct sr_arp_hdr *) (packet + sizeof(struct sr_ethernet_hdr));
		if (is_broadcast_mac(packet) || we_are_dest(sr, arpHeader->ar_tip)) {
			/* Process only broadcasted packets or packets meant for me */
			processArp(sr, packet, len, interface);
		}

	} else if (ethertype(packet) == ethertype_ip) { 	/* IP packet */
		struct sr_ip_hdr *ipHeader = (struct sr_ip_hdr *) (packet + sizeof(struct sr_ethernet_hdr));
		
		/* Ignore invalid packets */
		if (!is_sane_ip_packet(packet, len)) {
			return;
		}

		/* If NAT is enabled, do an address translation */
		if (sr->natEnable) {
			int failed = sr_nat_translate_packet(sr, packet, len, interface);
			if (failed) {
				/* packet could not be translated. Drop it */
				return;
			}
		}

		if (we_are_dest(sr, ipHeader->ip_dst)) {
			/* We are destination */
			processIP(sr, packet, len, interface);
		} else {
			/* We are not destination. Forward it. */
			processForward(sr, packet, len, interface);
		}
	}
}
Example #2
0
uint8_t is_multi_broadcast_mac(const uint8_t *mac)
{
    return (is_broadcast_mac(mac) ||
            is_ipv4_multicast_mac(mac) ||
            is_ipv6_multicast_mac(mac));
}