static void arp_get_mac_func(void** state) { //timer setting timer_init("IntelCore(TM) i5-4670 CPU @ 3.40GHz"); // Nic initialization void* malloc_pool = malloc(POOL_SIZE); init_memory_pool(POOL_SIZE, malloc_pool, 0); __nics[0] = malloc(sizeof(NIC)); __nic_count++; __nics[0]->mac = 0x74d4358f66cb; __nics[0]->pool_size = POOL_SIZE; __nics[0]->pool = malloc_pool; __nics[0]->output_buffer = fifo_create(8, malloc_pool); __nics[0]->config = map_create(8, NULL, NULL, __nics[0]->pool); Packet* packet = nic_alloc(__nics[0], sizeof(arp_reply_packet)); memcpy(packet->buffer + packet->start, arp_reply_packet, sizeof(arp_reply_packet)); Ether* ether = (Ether*)(packet->buffer + packet->start); ARP* arp = (ARP*)ether->payload; uint32_t addr = endian32(arp->tpa); nic_ip_add(__nics[0], addr); if(!arp_process(packet)) return; uint32_t spa = 0xc0a80a6f; uint32_t dpa = 0xc0a80a90; uint64_t comp_tha = 0x74d4358f66cb; uint64_t d_mac = arp_get_mac(__nics[0], dpa, spa); assert_memory_equal((uint8_t*)&d_mac, (uint8_t*)&comp_tha, 6); destroy_memory_pool(malloc_pool); free(malloc_pool); malloc_pool = NULL; }
void process(NetworkInterface* ni) { Packet* packet = ni_input(ni); if(!packet) return; if(arp_process(packet)) return; Ether* ether = (Ether*)(packet->buffer + packet->start); if(endian16(ether->type) == ETHER_TYPE_IPv4) { IP* ip = (IP*)ether->payload; if(ip->protocol == IP_PROTOCOL_UDP) { UDP* udp = (UDP*)ip->body; if(endian16(udp->destination) == SETKEY_PORT_NUM) { int orig_len = endian16(ip->length); Parameter* parameter = (Parameter*)udp->body; // ARP Request if(arp_request(ni, parameter->dst_ip) == true); //printf("ARP Request for destination IP\n"); int result = parse(parameter); memcpy(&(udp->body), &result, sizeof(result)); #ifdef _DEBUG_ for(int i = 0; i < 64 /* Packet Minimum Size */ - ETHER_LEN /* 14 */ - IP_LEN /* 20 */ - UDP_LEN /* 8 */ - sizeof(result); i++) udp->body[i + 4] = i; #endif uint16_t t = udp->destination; udp->destination = udp->source; udp->source = t; udp->checksum = 0; udp->length = endian16(64 - ETHER_LEN - IP_LEN); uint32_t t2 = ip->destination; ip->destination = ip->source; ip->source = t2; ip->ttl = 0x40; ip->length = endian16(64 - ETHER_LEN); ip->checksum = 0; ip->checksum = endian16(checksum(ip, ip->ihl * 4)); uint64_t t3 = ether->dmac; ether->dmac = ether->smac; ether->smac = t3; packet->end += (endian16(ip->length) - orig_len); ni_output(ni, packet); packet = NULL; } } if(ip->protocol == IP_PROTOCOL_ESP){ int orig_len = endian16(ip->length); #ifdef _DEBUG_ printf("Before Decryption - Packet : \n"); for(int i = 1; i < 1 + endian16(ip->length); i++) { printf("%02x ", ether->payload[i - 1]); // Packet - IP Header if( i % 16 == 0 ) printf("\n"); } printf("\n"); #endif if(decrypt(ip) >= 0) { packet->end += (endian16(ip->length) - orig_len); // ether->dmac = endian48(arp_get_mac(ni, endian32(ip->destination))); // ether->smac = endian48(ni->mac); #ifdef _DEBUG_ printf("- Ethernet Header After Decryption-\n"); printf("Dst Mac : %012lx Src Mac : %012lx\n Ether Type : %x\n", endian48(ether->dmac), endian48(ether->smac), ether->type); #endif if(ip->protocol == IP_PROTOCOL_ICMP){ // Echo reply ICMP* icmp = (ICMP*)ip->body; icmp->type = 0; icmp->checksum = 0; icmp->checksum = endian16(checksum(icmp, packet->end - packet->start - ETHER_LEN - IP_LEN)); uint32_t temp = ip->destination; ip->destination = ip->source; ip->source = temp; ip->ttl = endian8(64); ip->checksum = 0; ip->checksum = endian16(checksum(ip, ip->ihl * 4)); // ether->dmac = ether->smac; // ether->smac = endian48(ni->mac); // ni_output(ni, packet); // packet = NULL; } // ni_output(ni1, packet); // packet = NULL; } orig_len = endian16(ip->length); #ifdef _DEBUG_ printf("Before Encryption - Packet : \n"); for(int i = 1; i < 1 + endian16(ip->length); i++) { printf("%02x ", ether->payload[i - 1]); // Packet - IP Header if( i % 16 == 0 ) printf("\n"); } printf("\n"); #endif if(encrypt(ip) >= 0) { packet->end += (endian16(ip->length) - orig_len); ether->dmac = endian48(arp_get_mac(ni, endian32(ip->destination))); ether->smac = endian48(ni->mac); #ifdef _DEBUG_ printf("- Ethernet Header After Encryption-\n"); printf("Dst Mac : %012lx Src Mac : %012lx\n Ether Type : %x\n", endian48(ether->dmac), endian48(ether->smac), ether->type); #endif ni_output(ni, packet); packet = NULL; } } } if(packet) ni_free(packet); }