int send_arp_request(unsigned char *src_pr_add, unsigned char *dst_pr_add) { int i; struct rte_mbuf *new_mbuf = get_mbuf(); struct arp *arp_reply = (struct arp *)rte_pktmbuf_prepend (new_mbuf, sizeof(struct arp)); char mac[6]; // http://www.tcpipguide.com/free/t_ARPMessageFormat.htm arp_reply->hw_type = htons(HW_TYPE_ETHERNET); arp_reply->pr_type = htons(SW_TYPE_IPV4); arp_reply->hw_len = HW_LEN_ETHER; arp_reply->pr_len = PR_LEN_IPV4; arp_reply->opcode = htons(1); unsigned char dest_mac[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; uint32_t ip_add = GetIntAddFromChar(src_pr_add, 1); struct Interface *temp = NULL; temp = InterfaceList; while(temp && ip_add != GetIntAddFromChar(temp->IP, 1)) { temp = temp->Next; } if(temp == NULL) { logger(ARP, NORMAL, "Arp request failed, address not hosted\n"); return 0; } logger(ARP, NORMAL, "IP found in interface list\n"); int status = GetInterfaceMac(temp->InterfaceNumber, mac); memcpy(arp_reply->src_hw_add, mac, HW_LEN_ETHER); memcpy(arp_reply->dst_hw_add, dest_mac, HW_LEN_ETHER); memcpy(arp_reply->src_pr_add, src_pr_add, PR_LEN_IPV4); memcpy(arp_reply->dst_pr_add, dst_pr_add, PR_LEN_IPV4); send_arp(arp_reply); }
int arp_in (struct rte_mbuf *mbuf) { assert(mbuf->buf_len >= sizeof(struct arp)); struct arp *arp_pkt; struct ether_hdr *eth; uint32_t ip_add = 0; eth = rte_pktmbuf_mtod(mbuf, struct ether_hdr *); assert(rte_pktmbuf_data_len(mbuf) >= (sizeof(struct arp) + sizeof(struct ether_hdr))); arp_pkt = rte_pktmbuf_mtod(mbuf, char *) + sizeof(struct ether_hdr); switch(ntohs(arp_pkt->opcode)) { case ARP_REQ :// send_arp_reply(arp_pkt->dst_pr_add, arp_pkt->src_pr_add); break; /* uint32_t ip_add = GetIntAddFromChar(arp_pkt->src_pr_add, 0); add_mac((ip_add), arp_pkt->src_hw_add); logger(ARP, NORMAL, "seen arp packet\n"); break; */ case ARP_REPLY :// ip_add = GetIntAddFromChar(arp_pkt->src_pr_add, 0); add_mac((ip_add), arp_pkt->src_hw_add); break; // default : assert(0); } }
void AddInterface(struct Interface *Iface) { struct Interface *ptr = NULL; int i = 0; ptr = malloc(sizeof(struct Interface)); memcpy(ptr, Iface, sizeof(struct Interface)); ptr->Next = NULL; if(InterfaceList == NULL) { InterfaceList = ptr; } else { InterfaceList->Next = ptr; } uint32_t Ipv4Addr = GetIntAddFromChar(ptr->IP, 0); // ptr->IP[0] | ptr->IP[1] << 8 | ptr->IP[2] << 16 | ptr->IP[3] << 24 ; //printf("assembled mac address = %x\n", Ipv4Addr); add_mac(Ipv4Addr, ptr->HwAddress); }
void InitInterface(struct Interface *IfList[], UINT Count) { struct Interface *ptr = NULL; int i = 0; for(i=0; i<Count; i++) { ptr = malloc(sizeof(struct Interface)); memcpy(ptr, IfList[i], sizeof(struct Interface)); ptr->Next = NULL; if(i==0) { InterfaceList = ptr; } else { InterfaceList->Next = ptr; } uint32_t Ipv4Addr = GetIntAddFromChar(ptr->IP, 0); // ptr->IP[0] | ptr->IP[1] << 8 | ptr->IP[2] << 16 | ptr->IP[3] << 24 ; //printf("assembled mac address = %x\n", Ipv4Addr); add_mac(Ipv4Addr, ptr->HwAddress); } }