void tprint_nic(char* desc, CnetNICaddr addr) { char str[17]; CNET_format_nicaddr(str, addr); //printf("%s: %s\n", desc, str); }
/* * Handle arp packet */ int handle_arp_packet(ARP_PACKET *arp_packet) { bool addtoarptable = false; char sourmac[18]; //source hardware address const char *sourip = network_display_address(arp_packet->sour_ip); CNET_format_nicaddr(sourmac, arp_packet->sour_mac); printf("ARP packet source MAC: %s \n", sourmac); //handle arp ethernet if (arp_packet->htype == ETH_CODE){ if (arp_packet->hlen == LEN_NICADDR){ if (arp_packet->ptype == IP_CODE){ if (arp_packet->plen == IP_ADDRLEN){ int pos = 0; pos = query_arp_table(arp_packet->sour_ip); if (pos != -1){ printf("Update ARP table - source IP: %s \n", sourip); update_arp_table(arp_packet->sour_mac, pos); addtoarptable = true; } else { printf("Add source IP to ARP table: %s \n", sourip); IPAddr sour_ip = arp_packet->sour_ip; add_arp_table(sour_ip, arp_packet->sour_mac); CHECK(network_unpause_destination(sour_ip)); } if (arp_packet->dest_ip == network_local_address()){ if (!addtoarptable){ printf("Add source IP to ARP table: %s \n", sourip); IPAddr sour_ip = arp_packet->sour_ip; add_arp_table(sour_ip, arp_packet->sour_mac); } if (arp_packet->opcode == ARP_REQUEST_CODE){ CnetNICaddr nicAddress; IPAddr ip1 = 0, ip2 = 0; printf("ARP response IP: %s \n", sourip); arp_packet->opcode = ARP_REPLY_CODE; memcpy(nicAddress, arp_packet->sour_mac, sizeof(CnetNICaddr)); memcpy(arp_packet->sour_mac, linkinfo[1].nicaddr, sizeof(CnetNICaddr)); memcpy(arp_packet->dest_mac, nicAddress, sizeof(CnetNICaddr)); ip1 = arp_packet->sour_ip; ip2 = network_local_address(); arp_packet->sour_ip = ip2; arp_packet->dest_ip = ip1; arp_request(arp_packet); } } } else { printf("IP address length error! (should be equal to 4) \n"); return -1; } } else { printf("Ethernet protocol error! (should be IPv4) \n"); return -1; } } else { printf("MAC address length error! (should be equal to 6) \n"); return -1; } } else { printf("Hardware protocol error! (should be Ethernet) \n"); return -1; } return 0; }