void print_packet(const uint8_t *packet, unsigned int len) { assert(packet); printf("\n"); print_eth_hdr(packet, len); printf("\n"); eth_hdr *eth = (eth_hdr *)packet; if (ntohs(eth->eth_type) == ETH_TYPE_ARP) { print_arp_hdr(packet, len); printf("\n"); } else if (ntohs(eth->eth_type) == ETH_TYPE_IP) { ip_hdr *ip = get_ip_hdr(packet, len); indent(1); printf("IPv4 Packet (%d bytes)\n", len - sizeof(eth_hdr)); print_ip_hdr(packet, len); switch(ip->ip_p) { case 1: { print_icmp_load(packet, len); break; } case 6: { print_tcp_load(packet, len); break; } case 89: { print_pwospf_load(packet, len); break; } default: { printf("UNRECOGNIZABLE PROTOCOL\n"); break;} } } }
/**@brief This function sends out a fins frame that passes an arp request out to the network * @param sought_IP_addrs is the IP address whose associated MAC address is sought * @param fins_arp_out points to the fins frame which will be sent from the module * */ void arp_out_request(uint32_t sought_IP_addrs, struct finsFrame *fins_arp_out){ gen_requestARP(sought_IP_addrs, &arp_msg); arp_msg_to_hdr(&arp_msg, packet); host_to_net(packet); print_arp_hdr(packet); arp_to_fins(packet, fins_arp_out); /**arp request to be sent to network*/ }
/**@brief This function sends out a fins frame with a reply arp in response to a request * from the network * @param fins_arp_out points to the fins frame which will be sent from the module * */ void arp_out_reply(struct finsFrame *fins_arp_out){ struct ARP_message arp_msg_reply; gen_replyARP(&arp_msg, &arp_msg_reply); arp_msg_to_hdr(&arp_msg_reply, packet); host_to_net(packet); print_arp_hdr(packet); arp_to_fins(packet, fins_arp_out); /**arp reply to be sent to network */ }
/**@brief this function tests a set of functions which are used when * (1) a host receives an ARP request, and (2) keeps updating its cache based on * these ARP requests * @param fileName is the file from which the list of neighbors is drawn */ void send_receive_update(char *fileName) { extern struct node *ptr_cacheHeader; struct finsFrame request_fins, reply_fins, *request_fins_ptr, *reply_fins_ptr; struct ARP_message request_ARP1, request_ARP2, reply_ARP1, reply_ARP2; struct ARP_message *request_ARP_ptr1, *request_ARP_ptr2, *reply_ARP_ptr1, *reply_ARP_ptr2; struct arp_hdr hdr_ARP, *hdr_ARP_ptr; uint64_t MAC_addrs; uint32_t IP_addrs_read; int task; /**Following code generates a list of IP/MAC addresses of 'neighbors' and initializes cache*/ ptr_cacheHeader = init_intface(); gen_neighbor_list(fileName); init_recordsARP(fileName); print_cache(); print_neighbors(ptr_neighbor_list); hdr_ARP_ptr = &hdr_ARP; IP_addrs_read = 1; task = 1; /**Begin Initialize/Instantiate Pointers */ request_fins_ptr = &request_fins; reply_fins_ptr = &reply_fins; request_ARP_ptr1 = &request_ARP1; request_ARP_ptr2 = &request_ARP2; reply_ARP_ptr1 = &reply_ARP1; reply_ARP_ptr2 = &reply_ARP2; /**A host can send update its cache based on its own request or a request from another network host*/ while (IP_addrs_read!=0 && (task==0 || task == 1)) { PRINT_DEBUG("\nTest send request and update `0' or test receive a request `1' \n"); scanf("%d", &task); IP_addrs_read = read_IP_addrs(); /**The following functions test the internal operations of the module*/ if (task==0){ gen_requestARP(IP_addrs_read, request_ARP_ptr1); print_msgARP(request_ARP_ptr1); arp_to_fins(request_ARP_ptr1, request_fins_ptr); fins_to_arp(request_fins_ptr, request_ARP_ptr2); mimic_net_reply(request_ARP_ptr2, reply_ARP_ptr1); if (check_valid_arp(reply_ARP_ptr1)==1){ arp_to_fins(reply_ARP_ptr1, reply_fins_ptr); fins_to_arp(reply_fins_ptr, reply_ARP_ptr2); print_msgARP(reply_ARP_ptr2); update_cache(reply_ARP_ptr2);} print_cache(); } else if (task==1){ MAC_addrs = search_MAC_addrs(IP_addrs_read, ptr_neighbor_list); mimic_net_request(IP_addrs_read, MAC_addrs,request_ARP_ptr1); print_msgARP(request_ARP_ptr1); if (check_valid_arp(request_ARP_ptr1)==1){ arp_to_fins(request_ARP_ptr1, request_fins_ptr); fins_to_arp(request_fins_ptr, request_ARP_ptr2); print_msgARP(request_ARP_ptr2); update_cache(request_ARP_ptr2);} print_cache(); } /**The following functions test the external operation of the module*/ if (check_valid_arp(request_ARP_ptr2)==1){ hdr_ARP_ptr = &hdr_ARP; /**convert ARP message to htons format and generate MAC addresses as unsigned char ptr*/ net_fmt_conversion(request_ARP_ptr2, hdr_ARP_ptr); print_arp_hdr(hdr_ARP_ptr);/**print some fields of the ARP message in external format*/ host_fmt_conversion(hdr_ARP_ptr, request_ARP_ptr2);/**convert ARP message to ntohs format*/ print_msgARP(request_ARP_ptr2);/**print ARP message internal format*/ } } term_intface(); }