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);

    /* check length */
    if (len < sizeof(sr_ethernet_hdr_t)) {
        fprintf(stderr, "Failed to process ETHERNET header, insufficient length\n");
        return;
    }
    
    sr_ethernet_hdr_t *eth_hdr = (sr_ethernet_hdr_t *)packet;
    
    uint16_t ether_type = ntohs(eth_hdr->ether_type);
    struct sr_if *iface = sr_get_interface(sr, interface);
    
    if (ether_type == ethertype_ip) {
        sr_handle_ip(sr, packet, len, iface);
    } else if (ether_type == ethertype_arp) {
        sr_handle_arp(sr, packet, len, iface);
    }
}
Beispiel #2
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);

  /* Check packet type */
  uint16_t ethtype = ethertype(packet);
  unsigned int minlen = sizeof(sr_ethernet_hdr_t);

  if (len < minlen) {
	  fprintf(stderr, "Invalid Ethernet frame size");
	  return;
  }

  switch(ethtype) {
  	  case ethertype_arp:
  		 Debug("Receive an ARP packet sent to interface %s\n", interface);
  		 sr_handle_arp(sr, packet , len, interface);
  		 break;

  	  case ethertype_ip:
  		  Debug("Get an IP header sent to interface: %s\n", interface);
  		  sr_handle_ip(sr, packet, len, interface);
  		  break;

  	  default:
  		  fprintf(stderr, "Unrecognized Ethernet Type: %d\n", ethtype);
  		  break;
  }


}/* end sr_ForwardPacket */