void arp_handle(unsigned char *src_mac, unsigned char *data, unsigned short len) { struct arp_hdr *pkt = (struct arp_hdr *)data; switch (ntohs(pkt->oper)) { case ARP_OPER_REQUEST: #ifdef DEBUG_ARP printf("ARP Request for %d.%d.%d.%d\n", pkt->tpa[0], pkt->tpa[1], pkt->tpa[2], pkt->tpa[3]); #endif /* DEBUG_ARP */ arp_table_add(pkt->spa, pkt->sha); if (memcmp(pkt->tpa, ip_address, 4) == 0) arp_send_reply(src_mac, pkt->sha, pkt->spa, pkt->tpa); break; case ARP_OPER_REPLY: #ifdef DEBUG_ARP printf("ARP Reply\n"); #endif /* DEBUG_ARP */ arp_table_add(pkt->spa, pkt->sha); break; default: break; } }
void processArp(struct sr_instance *sr , uint8_t *packet, unsigned int len, char *interface) { struct sr_arp_hdr *arpHeader = (struct sr_arp_hdr *) (packet + sizeof(struct sr_ethernet_hdr)); /* Put ARP header into cache */ struct sr_arpreq *req = sr_arpcache_insert(&(sr->cache), arpHeader->ar_sha, ntohl(arpHeader->ar_sip)); if (req != NULL) { /* Found requests in queue waiting for this reply. Send all waiting packets */ struct sr_packet *waiting = req->packets; struct sr_rt *rt = findLongestMatchPrefix(sr->routing_table, htonl(req->ip)); while (waiting != NULL) { send_packet_to_dest(sr, waiting->buf, waiting->len, rt->interface, arpHeader->ar_sha, arpHeader->ar_sip); waiting = waiting->next; } /* Destroy arp request when complete */ sr_arpreq_destroy(&(sr->cache), req); } if (ntohs(arpHeader->ar_op) == arp_op_request) { /* Reply to sender with our information */ arp_send_reply(sr, packet, len, interface); } }
//***************************************************************************************** // // Function : server_process // Description : Run web server and listen on port 80 // //***************************************************************************************** void server_process ( void ) { MAC_ADDR client_mac; IP_ADDR client_ip; // you can change rx,tx buffer size in includes.h BYTE rxtx_buffer[MAX_RXTX_BUFFER]; WORD plen; if ( flag1.bits.syn_is_sent ) return; // get new packet plen = enc28j60_packet_receive( (BYTE*)&rxtx_buffer, MAX_RXTX_BUFFER ); //plen will ne unequal to zero if there is a valid packet (without crc error) if(plen==0) return; // copy client mac address from buffer to client mac variable memcpy ( (BYTE*)&client_mac, &rxtx_buffer[ ETH_SRC_MAC_P ], sizeof(MAC_ADDR) ); // check arp packet if match with avr ip let's send reply if ( arp_packet_is_arp( rxtx_buffer, (WORD_BYTES){ARP_OPCODE_REQUEST_V} ) ) { arp_send_reply ( (BYTE*)&rxtx_buffer, (BYTE*)&client_mac ); return; } // get client ip address memcpy ( (BYTE*)&client_ip, &rxtx_buffer[ IP_SRC_IP_P ], sizeof(IP_ADDR) ); // check ip packet send to avr or not? if ( ip_packet_is_ip ( (BYTE*)&rxtx_buffer ) == 0 ) { return; } // check packet if packet is icmp packet let's send icmp echo reply if ( icmp_send_reply ( (BYTE*)&rxtx_buffer, (BYTE*)&client_mac, (BYTE*)&client_ip ) ) { return; } // start web server at port 80, see http.c http_webserver_process ( (BYTE*)rxtx_buffer, (BYTE*)&client_mac, (BYTE*)&client_ip ); }