#include #include #include #include #include #include ipheader ip; ipstats ipstat; WORD rxpos=0; WORD txpos=0; WORD datalen; extern BYTE rxbuff[]; extern BYTE txbuffer[]; WORD ipid=1; /* ************************************************************************ * void ip_receive() * process the IP header, storing any required values in global * memory before passing control over to the next layer ************************************************************************/ void ip_receive() { WORD ch; BYTE tempb; BYTE flags; BYTE proto; WORD offset; BYTE version; BYTE headerlen; BYTE n; /* increment receive statistics counter */ ipstat.rxpackets++; /* place circular buffer pointer at start of buffer */ rxpos=0; /* copy byte from receive buffer */ copy_rx_byte(&tempb); /* retrieve version number */ version = tempb >> 4; /* and discard the datagram if it's not v4 */ if (version != 4) { ipstat.dropped++; return; } /* mask off upper four bits and store * the ip header length for later use */ headerlen = tempb & 15; /* discard the TOS */ discard_byte(); /* store the total length of the IP datagram * to be read later by other protocols */ copy_rx_word(&ip.totallen); /* discard the IPID */ discard_word(); /* read the 3 bit flags and 15 bit offset */ copy_rx_byte(&tempb); flags = (tempb>>5); tempb &= 0x1F; copy_rx_byte(&offset); offset = ((WORD)tempb<<8)|offset; /* discard the datagram if it is fragmented * (sometimes have unexpected problems with this) */ if (offset || (flags & 0x01)) { ipstat.dropped++; return; } /* discard TTL field */ discard_byte(); /* store protocol field for demultiplexing later */ copy_rx_byte(&proto); /* and just skip past the checksum */ discard_word(); /* copy the source address, to be used * as the destination address of the next outgoing packet */ copy_rx_byte(&ip.destaddr[0]); copy_rx_byte(&ip.destaddr[1]); copy_rx_byte(&ip.destaddr[2]); copy_rx_byte(&ip.destaddr[3]); /* ensure the destination address of incoming datagram * matches the preconfigured address of the device * otherwise, drop the datagram */ if (rx_byte() != IP1 || rx_byte() != IP2 || rx_byte() != IP3 || rx_byte() != IP4) { ipstat.dropped++; return; } /* options will come next, but we just want to discard them */ for (n=5;n