Carpe diem, c'est la vie.
Posts: 6,433
Karma: 10773670
Join Date: Nov 2011
Location: Multiverse 6627A
Device: K1 to PW3
|
https://sourceforge.net/projects/sedhcp/
Quote:
A very simple DHCP server for use in embedded systems.
License: Public Domain
|
PHP Code:
//Minimal DHCP Implementation //
const int forever = 1;
#define DHCPDISCOVER 1 #define DHCPOFFER 2 #define DHCPREQUEST 3 #define DHCPACK 5
/* 32-bit structure containing 4-digit ip number */ struct id_struct { UINT8 is_ip_addrs[MAX_ADDRESS_SIZE]; /* IP address number */ };
struct addr_struct { INT16 family; /* family = INTERNET */ UINT16 port; /* machine's port number */ struct id_struct id; /* contains the 4-digit ip number for the host machine */ char *name; /* points to machine's name */ };
typedef struct { UINT8 dp_op; /* packet opcode type */ UINT8 dp_htype; /* hardware addr type */ UINT8 dp_hlen; /* hardware addr length */ UINT8 dp_hops; /* gateway hops */ UINT32 dp_xid; /* transaction ID */ UINT16 dp_secs; /* seconds since boot began */ UINT16 dp_flags; UINT8 dp_ciaddr[4]; /* client IP address */ UINT8 dp_yiaddr[4]; /* 'your' IP address */ UINT8 dp_siaddr[4]; /* server IP address */ UINT8 dp_giaddr[4]; /* gateway IP address */ UINT8 dp_chaddr[16]; /* client hardware address */ UINT8 dp_legacy[192]; UINT8 dp_magic[4]; UINT8 dp_options[275]; /* options area */ /* as of RFC2131 it is variable length */ }DHCP_TYPE;
static INT32 DHCPSocket = -1; char NewIP[4] = {192,168,0,16}; char New_subnet[] = {1,4,255,255,0,0}; char New_offer[] = {53,1,DHCPOFFER}; char New_ack[] = {53,1,DHCPACK}; char magic_cookie[] = {0x63,0x82,0x53,0x63};
VOID DHCP_Task(UNSIGNED argc, VOID *argv) { struct addr_struct myAddr; struct addr_struct sourceAddr; struct addr_struct destinationAddr; STATUS status; DHCP_TYPE DHCP_Buffer; struct sock_struct *sockptr; char * option_ptr;
myAddr.family = NU_FAMILY_IP; myAddr.port = IPPORT_DHCPS; memset (myAddr.id.is_ip_addrs,0,4);
DHCPSocket = NU_Socket(NU_FAMILY_IP, NU_TYPE_DGRAM, 0); status = NU_Bind(DHCPSocket, &myAddr, 0);
while ( forever ) { status = NU_Recv_From( DHCPSocket, (char *)&DHCP_Buffer, sizeof( DHCP_Buffer ), 0 , &sourceAddr, 0);
if ( status > 0 ) { switch ( DHCP_Buffer.dp_options[2] ) { case DHCPDISCOVER: DHCP_Buffer.dp_op = DHCPOFFER; memset( &DHCP_Buffer.dp_options, 0, sizeof( DHCP_Buffer.dp_options )); memcpy( &DHCP_Buffer.dp_yiaddr, NewIP, 4 ); memcpy( DHCP_Buffer.dp_magic, magic_cookie, 4 ); option_ptr = (char *)&DHCP_Buffer.dp_options; memcpy( option_ptr, New_offer, 3 ); option_ptr += 3; memcpy( option_ptr, New_subnet, 8 ); option_ptr += 8;
destinationAddr.port = IPPORT_DHCPC; destinationAddr.family = NU_FAMILY_IP; memset( destinationAddr.id.is_ip_addrs , 0xff, 4 ); NU_Send_To( DHCPSocket, (char *)&DHCP_Buffer, sizeof( DHCP_Buffer ), 0 , &destinationAddr, sizeof( destinationAddr )); break;
case DHCPREQUEST: DHCP_Buffer.dp_op = DHCPOFFER; memset( &DHCP_Buffer.dp_options, 0, sizeof( DHCP_Buffer.dp_options )); memcpy( &DHCP_Buffer.dp_yiaddr, NewIP, 4 ); memcpy( DHCP_Buffer.dp_magic, magic_cookie, 4 ); option_ptr = (char *)&DHCP_Buffer.dp_options; memcpy( option_ptr, New_ack, 3 ); option_ptr += 3; memcpy( option_ptr, New_subnet, 8 ); option_ptr += 8;
destinationAddr.port = IPPORT_DHCPC; destinationAddr.family = NU_FAMILY_IP; memset( destinationAddr.id.is_ip_addrs , 0xff, 4 ); NU_Send_To( DHCPSocket, (char *)&DHCP_Buffer, sizeof( DHCP_Buffer ), 0 , &destinationAddr, sizeof( destinationAddr )); break;
default: break; } } } }
Well, that's pretty simple. And it should be tiny too. But it has a lot of redundancy and could be simplified even more.
Last edited by geekmaster; 06-24-2016 at 01:05 AM.
|