Пример #1
0
Файл: ip.c Проект: tcr/rise
void send_ip_header( unsigned short totallen, const unsigned char * to, unsigned char proto )
{
/*
	//This uses about 50 bytes less of flash, but 12 bytes more of ram.  You can choose that tradeoff.
	static unsigned char data[] = { 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 64, 0x00, 0x00, 0x00 };
	data[2] = totallen >> 8;
	data[3] = totallen & 0xFF;
	data[9] = proto;
	enc424j600_pushblob( data, 12 );
*/

	enc424j600_push16( 0x4500 );
	enc424j600_push16( totallen );

	enc424j600_push16( 0x0000 ); //ID
	enc424j600_push16( 0x4000 ); //Don't frgment, no fragment offset

	enc424j600_push8( 64 ); // TTL
	enc424j600_push8( proto ); // protocol

	enc424j600_push16( 0 ); //Checksum

	enc424j600_pushblob( MyIP, 4 );
	enc424j600_pushblob( to, 4 );
}
Пример #2
0
//Mode = 1 for discover, Mode = 3 for request - if discover, dhcp_server should be 0.
void RequestNewIP( uint8_t mode, uint8_t * negotiating_ip, uint8_t * dhcp_server )
{
	uint8_t oldip[4];
	SwitchToBroadcast();

	enc424j600_stopop();
	enc424j600_startsend( NetGetScratch() );
	send_etherlink_header( 0x0800 );

	//Tricky - backup our IP - we want to spoof it to 0.0.0.0
	memcpy( oldip, MyIP, 4 );
	MyIP[0] = 0; MyIP[1] = 0; MyIP[2] = 0; MyIP[3] = 0;
	send_ip_header( 0, "\xff\xff\xff\xff", 17 ); //UDP Packet to 255.255.255.255
	memcpy( MyIP, oldip, 4 );
	
/*
	enc424j600_push16( 68 );  //From bootpc
	enc424j600_push16( 67 );  //To bootps
	enc424j600_push16( 0 ); //length for later
	enc424j600_push16( 0 ); //csum for later

	//Payload of BOOTP packet.
	//	1, //Bootp request
	//	1, //Ethernet
	enc424j600_push16( 0x0101 );
	//	6, //MAC Length
	//	0, //Hops
	enc424j600_push16( 0x0600 );
*/
	enc424j600_pushpgmblob( PSTR("\x00\x44\x00\x43\x00\x00\x00\x00\x01\x01\x06"), 12 ); //NOTE: Last digit is 0 on wire, not included in string.

	enc424j600_pushblob( MyMAC, 4 );

	enc424j600_push16( dhcp_clocking ); //seconds
	enc424j600_pushzeroes( 10 ); //unicast, CLIADDR, YIADDR
	if( dhcp_server )
		enc424j600_pushblob( dhcp_server, 4 );
	else
		enc424j600_pushzeroes( 4 );
	enc424j600_pushzeroes( 4 ); //GIADDR IP
	enc424j600_pushblob( MyMAC, 6 ); //client mac
	enc424j600_pushzeroes( 10 + 0x40 + 0x80 ); //padding + Server Host Name
	enc424j600_push16( 0x6382 ); //DHCP Magic Cookie
	enc424j600_push16( 0x5363 );

	//Now we are on our DHCP portion
	enc424j600_push8( 0x35 );  //DHCP Message Type
	enc424j600_push16( 0x0100 | mode );

	{
		enc424j600_push16( 0x3204 ); //Requested IP address. (4 length)
		enc424j600_pushblob( negotiating_ip, 4 );
	}

	if( dhcp_server ) //request
	{
		enc424j600_push16( 0x3604 );
		enc424j600_pushblob( dhcp_server, 4 );
	}

	if( DHCPName )
	{
		uint8_t namelen = strlen( DHCPName );
		enc424j600_push8( 0x0c ); //Name
		enc424j600_push8( namelen );
		enc424j600_pushblob( DHCPName, namelen );
	}

	enc424j600_push16( 0x3702 ); //Parameter request list
	enc424j600_push16( 0x0103 ); //subnet mask, router
//	enc424j600_push16( 0x2a06 ); //NTP server, DNS server  (We don't use either NTP or DNS)
	enc424j600_push8( 0xff ); //End option

	enc424j600_pushzeroes( 32 ); //Padding

	util_finish_udp_packet();
}