Ejemplo n.º 1
0
void enc424j600_receivecallback( uint16_t packetlen )
{
	uint8_t is_the_packet_for_me = 1;
	unsigned char i;
	unsigned char ipproto;

	//First and foremost, make sure we have a big enough packet to work with.
	if( packetlen < 8 )
	{
#ifdef ETH_DEBUG
		sendstr( "Runt\n" );
#endif
		return;
	}

	//macto (ignore) our mac filter handles this.
	enc424j600_dumpbytes( 6 );

	POPB( macfrom, 6 );

	//Make sure it's ethernet!
	if( POP != 0x08 )
	{
#ifdef ETH_DEBUG
		sendstr( "Not ethernet.\n" );
#endif
		return;
	}

	//Is it ARP?
	if( POP == 0x06 )
	{
		HandleArp( );
		return;
	}

	//otherwise it's standard IP

	//So, we're expecting a '45

	if( POP != 0x45 )
	{
#ifdef ETH_DEBUG
		sendstr( "Not IP.\n" );
#endif
		return;
	}

	POP; //differentiated services field.

	iptotallen = POP16;
	enc424j600_dumpbytes( 5 ); //ID, Offset+FLAGS+TTL
	ipproto = POP;

	POP16; //header checksum

	POPB( ipsource, 4 );


	for( i = 0; i < 4; i++ )
	{
		unsigned char m = ~MyMask[i];
		unsigned char ch = POP;
		if( ch == MyIP[i] || (ch & m) == 0xff  ) continue;
		is_the_packet_for_me = 0;
	}

	//Tricky, for DHCP packets, we have to detect it even if it is not to us.
	if( ipproto == 17 )
	{
		remoteport = POP16;
		localport = POP16;
#ifdef ENABLE_DHCP_CLIENT
		if( localport == 68 && !dhcp_seconds_remain )
		{
			HandleDHCP( POP16 );
			return;
		}
#endif
	}

	if( !is_the_packet_for_me )
	{
#ifdef ETH_DEBUG
		sendstr( "not for me\n" );
#endif
		return;
	}

	//XXX TODO Handle IPL > 5  (IHL?)

	switch( ipproto )
	{
	case 1: //ICMP

		HandleICMP();
		break;
#ifdef INCLUDE_UDP
	case 17:
	{
		//err is this dangerous?
		HandleUDP( POP16 );
		break;	
	}
#endif
#ifdef INCLUDE_TCP
	case 6: // TCP
	{

		remoteport = POP16;
		localport = POP16;
		iptotallen-=20;
		HandleTCP( iptotallen );
		break;
	}
#endif // HAVE_TCP_SUPPORT
	default:
		break;
	}

//finishcb:
//	enc424j600_finish_callback_now();
}
Ejemplo n.º 2
0
Archivo: ip.c Proyecto: tcr/rise
int enc424j600_receivecallback( uint16_t packetlen )
{
	uint8_t is_the_packet_for_me = 1;
	unsigned char i;
	unsigned char ipproto;

	//First and foremost, make sure we have a big enough packet to work with.
	if (packetlen < 8) {
		// ERROR: Received runt packet
		return 1;
	}

	//macto (ignore) our mac filter handles this.
	enc424j600_dumpbytes( 6 );

	enc424j600_popblob( macfrom, 6 );

	//Make sure it's ethernet!
	if (enc424j600_pop8() != 0x08) {
		// ERROR: Not an ethernet packet
		return 1;
	}

	//Is it ARP?
	if (enc424j600_pop8() == 0x06) {
		HandleArp();
		return 0;
	}

	//otherwise it's standard IP

	//So, we're expecting a '45

	if (enc424j600_pop8() != 0x45) {
		// ERROR: Not an IP packet
		return 1;
	}

	enc424j600_pop8(); //differentiated services field.

	iptotallen = enc424j600_pop16();
	enc424j600_dumpbytes( 5 ); //ID, Offset+FLAGS+TTL
	ipproto = enc424j600_pop8();

	enc424j600_pop16(); //header checksum

	enc424j600_popblob( ipsource, 4 );

	for (i = 0; i < 4; i++) {
		unsigned char m = ~MyMask[i];
		unsigned char ch = enc424j600_pop8();
		if (ch == MyIP[i] || (ch & m) == 0xff) {
			continue;
		}
		is_the_packet_for_me = 0;
	}

	//Tricky, for DHCP packets, we have to detect it even if it is not to us.
	if (ipproto == 17) {
		remoteport = enc424j600_pop16();
		localport = enc424j600_pop16();
	}

	if (!is_the_packet_for_me) {
		// ERROR: Packet is not for us
		return 1;
	}

	//XXX TODO Handle IPL > 5  (IHL?)

	switch(ipproto) {
		// ICMP
		case 1: {
			HandleICMP();
			break;
		}

		// UDP
		case 17: {
			HandleUDP(enc424j600_pop16());
			break;
		}

		default: {
			break;
		}
	}

	return 0;

//finishcb:
//	enc424j600_finish_callback_now();
}