//******************************************************************************************* // // Function : arp_who_is // Description : send arp request to destination ip, and save destination mac to dest_mac. // call this function to find the destination mac address before send other packet. // //******************************************************************************************* BYTE arp_who_is ( BYTE *rxtx_buffer, BYTE *dest_mac, BYTE *dest_ip ) { BYTE i; WORD dlength; // send arp request packet to network arp_send_request ( rxtx_buffer, dest_ip ); for ( i=0; i<10; i++ ) { // Time out 10x10ms = 100ms _delay_ms ( 10 ); dlength = enc28j60_packet_receive( rxtx_buffer, MAX_RXTX_BUFFER ); // destination ip address was found on network if ( dlength ) { if ( arp_packet_is_arp ( rxtx_buffer, (WORD_BYTES){ARP_OPCODE_REPLY_V} ) ) { // copy destination mac address from arp reply packet to destination mac address memcpy ( dest_mac, &rxtx_buffer[ ETH_SRC_MAC_P ], sizeof(MAC_ADDR) ); return 1; } } } // destination ip was not found on network return 0; }
//***************************************************************************************** // // 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 ); }