void sr_print_if_list(struct sr_router* sr) { struct sr_if* if_walker = 0; if(sr->if_list == 0) { printf(" Interface list empty \n"); return; } if_walker = sr->if_list; sr_print_if(if_walker); while(if_walker->next) { if_walker = if_walker->next; sr_print_if(if_walker); } } /* -- sr_print_if_list -- */
void sr_print_if_list(struct sr_instance* sr) { struct sr_vns_if* if_walker = 0; struct sr_router* sub = (struct sr_router*)sr_get_subsystem(sr); if(sub->if_list == 0) { printf(" Interface list empty \n"); return; } if_walker = sub->if_list; sr_print_if(if_walker); while(if_walker->next) { if_walker = if_walker->next; sr_print_if(if_walker); } } /* -- sr_print_if_list -- */
/*--------------------------------------------------------------------- * Method: process_arp_packet(struct sr_instance* sr, uint8_t *packet_buffer, unsigned int len, char* interface) * Scope: Global * * function to process the arp packet * *---------------------------------------------------------------------*/ int process_arp_packet( struct sr_instance* sr, uint8_t *packet_buffer, unsigned int len, char* interface) { assert(sr); assert(packet_buffer); assert(interface); int etherLen = sizeof(sr_ethernet_hdr_t); //ethernet header size int ipLen = sizeof(sr_ip_hdr_t); //ip header length int arpLen = sizeof(sr_arp_hdr_t); //arp header length if (len < etherLen + arpLen) { fprintf(stderr, "Error: ARP header - insufficient length\n"); return -1; } printf("ARP Packet Processing Initiated\n"); // print_hdr_arp(packet + sizeof(sr_ethernet_hdr_t)); // Create ARP Header and find interface sr_arp_hdr_t* arp_header = (sr_arp_hdr_t*)(packet_buffer + etherLen); struct sr_if* my_interface = sr_find_interface(sr, arp_header->ar_tip); if (my_interface) { printf("Found Interface: "); sr_print_if(my_interface); if (strcmp(my_interface->name, interface) == 0) { printf("Interface name's match up\n"); unsigned short op_code = ntohs(arp_header->ar_op); if (op_code == arp_op_reply) { // Process ARP Reply printf("Processing ARP Reply\n"); // See if there's an ARP request in the queue. struct sr_arpreq* req = sr_arpcache_insert( &(sr->cache), arp_header->ar_sha, arp_header->ar_sip); // Forward all packets waiting on req if req exists. struct sr_packet* pckt = req ? req->packets : NULL; for (; pckt != NULL; pckt = pckt->next) { eth_frame_send_with_mac( sr, pckt->buf, pckt->len, arp_header->ar_sha, pckt->iface); } } else if (op_code == arp_op_request) { // Process ARP Request printf("Processing ARP Request\n"); // Set the target to the incoming ARP source. memcpy(arp_header->ar_tha, arp_header->ar_sha, ETHER_ADDR_LEN); arp_header->ar_tip = arp_header->ar_sip; // Set the source to this interface. memcpy(arp_header->ar_sha, my_interface->addr, ETHER_ADDR_LEN); arp_header->ar_sip = my_interface->ip; // Set ethernet frame MAC information sr_ethernet_hdr_t* ethernet_hdr = (sr_ethernet_hdr_t*)(packet_buffer); memcpy(ethernet_hdr->ether_dhost, arp_header->ar_tha, ETHER_ADDR_LEN); memcpy(ethernet_hdr->ether_shost, arp_header->ar_sha, ETHER_ADDR_LEN); // Send the packet back on it's way. arp_header->ar_op = htons(arp_op_reply); printf("Sending out ARP Reply\n"); sr_send_packet(sr, packet_buffer, len, interface); } else { fprintf(stderr, "ARP Op Code Unknown: (%d)\n", arp_header->ar_op); return -1; } } else { fprintf(stderr, "ARP interface names didn't match: %s, %s\n", my_interface->name, interface); return -1; } } else { printf("ARP interface not found\n"); } return 0; }