Has anyone written or ported a DHCP server for the NB (5234)?
I am looking at porting opendhcp but I don't want to reinvent the wheel!
Any suggestions welcome. Thanks.
DHCP Server
Re: DHCP Server
I am also needing to invent a DHCP server (for NB700EX).
Were you able to do a "port" or find existing code for NB?
Were you able to do a "port" or find existing code for NB?
Re: DHCP Server
// Minimal DHCP Implementation IPv4 static allocation of a single address
// Based on "Simple Embedded DHCP" project at SourceForge.net
// Ported to use NetBurner instead of Nucleus network calls
// Used with crossover-cable connection to single laptop or PC
// Called by main when no other DHCP server is available on the network
// Test to make sure that there is not another DHPC server present before starting this task.
// Recommend using Multi-Home capability to "reserve" a static IP address for HTML interface
const int forever = 1;
#define DHCPDISCOVER 1
#define DHCPOFFER 2
#define DHCPREQUEST 3
#define DHCPACK 5
#include "predef.h"
/**********************************************************************/
#include <stdio.h>
#include <ctype.h>
#include <startnet.h>
#include <ucos.h>
#include <udp.h>
#include <autoupdate.h>
#include <dhcpclient.h>
#include <string.h>
#define IPPORT_DHCPS 67
#define IPPORT_DHCPC 68
void DHCP_serverTask(void *data)
{
/* 32-bit structure containing 4-digit IP number */
struct id_struct
{
UINT8 is_ip_addrs[4]; /* IP address number */
};
#if 0
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 */
};
#endif
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;
char NewIP[4] = {10,254,168,1}; // Your New IP Address
char New_ack[] = {53,1,DHCPACK};
char magic_cookie[] = {0x63,0x82,0x53,0x63};
char New_offer[] = {53,1,DHCPOFFER};
char New_subnet[] = {1,4,255,255,255,0};
char Lease_time[] = {51,4, 0, 0x76, 0xa7, 0x00};
char Server_identification[] = {54, 4, 10, 254, 168, 4}; // My IP Address (Same as for Multi-Home)
char MyName[] = {0x0C, 0x06,'D', 'C', 'L', 'W', 'e', 'b'};
char TheEnd[] = {0xff};
DHCP_TYPE DHCP_Buffer;
WORD ServerPort = IPPORT_DHCPS; // I am the Server
WORD ClientPort = IPPORT_DHCPC;
IPADDR to_addr = 0xffffffff; // Broadcast
IPADDR ClientIP;
char * option_ptr;
//struct addr_struct destinationAddr;
//memcpy( &to_addr, MyIP, 4 );
int DHCPSocket = CreateRxTxUdpSocket( to_addr, ClientPort, ServerPort );
if ( DHCPSocket <= 0 )
{
iprintf("Error Creating UDP Socket: %d\r\n", DHCPSocket);
while (1) { OSTimeDly(TICKS_PER_SECOND); }
} else
{
iprintf( "DHCP Server started, IP Address:");
ShowIP( to_addr );
iprintf( "\r\nServer Port: %d, Client Port: %d\r\n", ServerPort, ClientPort );
}
/*-------------------------------------------------------------------
* This task will wait for incoming UDP packets and process them.
------------------------------------------------------------------*/
while (1)
{
recvfrom( DHCPSocket, (BYTE *)&DHCP_Buffer,
sizeof(DHCP_Buffer), &ClientIP, &ServerPort, &ClientPort );
switch ( DHCP_Buffer.dp_options[2] )
{
case DHCPDISCOVER:
iprintf( "\r\nReceived DHCP DISCOVER message from: ");
ShowIP( ClientIP ); // ClientIP should be 0.0.0.0
iprintf( "\r\n\n");
ShowData((BYTE *)&DHCP_Buffer, sizeof(DHCP_Buffer));
DHCP_Buffer.dp_op = DHCPOFFER;
DHCP_Buffer.dp_flags = 0; // clear flags
DHCP_Buffer.dp_secs = 0; // clear seconds
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, 6 );
option_ptr += 6;
memcpy( option_ptr, Lease_time, 6 );
option_ptr += 6;
memcpy( option_ptr, Server_identification, 6 );
option_ptr += 6;
memcpy( option_ptr, MyName, 8 );
option_ptr += 8;
memcpy( option_ptr, TheEnd, 1 );
option_ptr += 1;
//OSTimeDly(0.5 * TICKS_PER_SECOND);
iprintf("\r\nTransmited DHCP OFFER message to: ");
ShowIP( to_addr );
iprintf(" from: ");
ShowIP( EthernetIP );
iprintf( "\r\n\n");
ShowData((BYTE *)&DHCP_Buffer, sizeof(DHCP_Buffer));
sendto( DHCPSocket, (BYTE *)&DHCP_Buffer,
sizeof( DHCP_Buffer ), to_addr, ClientPort );
break;
case DHCPREQUEST:
iprintf( "\r\nReceived DHCP REQUEST message from: ");
ShowIP( ClientIP );
iprintf( "\r\n\n");
ShowData((BYTE *)&DHCP_Buffer, sizeof(DHCP_Buffer));
DHCP_Buffer.dp_op = DHCPOFFER;
DHCP_Buffer.dp_flags = 0; // clear flags
DHCP_Buffer.dp_secs = 0; // clear seconds
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, 6 );
option_ptr += 6;
memcpy( option_ptr, Lease_time, 6 );
option_ptr += 6;
memcpy( option_ptr, Server_identification, 6 );
option_ptr += 6;
memcpy( option_ptr, MyName, 8 );
option_ptr += 8;
memcpy( option_ptr, TheEnd, 1 );
option_ptr += 1;
iprintf("\r\nTransmited DHCP ACK message to: ");
ShowIP( to_addr );
iprintf(" from: ");
ShowIP( EthernetIP );
iprintf( "\r\n\n");
ShowData((BYTE *)&DHCP_Buffer, sizeof(DHCP_Buffer));
//OSTimeDly(0.5 * TICKS_PER_SECOND);
sendto( DHCPSocket, (BYTE *)&DHCP_Buffer,
sizeof( DHCP_Buffer ), to_addr, ClientPort );
break;
default:
iprintf( "\r\nReceived broadcast message from: ");
ShowIP( ClientIP ); // ClientIP should be 0.0.0.0
iprintf( "\r\n\n");
ShowData((BYTE *)&DHCP_Buffer, sizeof(DHCP_Buffer));
break;
}
}
}
// Based on "Simple Embedded DHCP" project at SourceForge.net
// Ported to use NetBurner instead of Nucleus network calls
// Used with crossover-cable connection to single laptop or PC
// Called by main when no other DHCP server is available on the network
// Test to make sure that there is not another DHPC server present before starting this task.
// Recommend using Multi-Home capability to "reserve" a static IP address for HTML interface
const int forever = 1;
#define DHCPDISCOVER 1
#define DHCPOFFER 2
#define DHCPREQUEST 3
#define DHCPACK 5
#include "predef.h"
/**********************************************************************/
#include <stdio.h>
#include <ctype.h>
#include <startnet.h>
#include <ucos.h>
#include <udp.h>
#include <autoupdate.h>
#include <dhcpclient.h>
#include <string.h>
#define IPPORT_DHCPS 67
#define IPPORT_DHCPC 68
void DHCP_serverTask(void *data)
{
/* 32-bit structure containing 4-digit IP number */
struct id_struct
{
UINT8 is_ip_addrs[4]; /* IP address number */
};
#if 0
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 */
};
#endif
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;
char NewIP[4] = {10,254,168,1}; // Your New IP Address
char New_ack[] = {53,1,DHCPACK};
char magic_cookie[] = {0x63,0x82,0x53,0x63};
char New_offer[] = {53,1,DHCPOFFER};
char New_subnet[] = {1,4,255,255,255,0};
char Lease_time[] = {51,4, 0, 0x76, 0xa7, 0x00};
char Server_identification[] = {54, 4, 10, 254, 168, 4}; // My IP Address (Same as for Multi-Home)
char MyName[] = {0x0C, 0x06,'D', 'C', 'L', 'W', 'e', 'b'};
char TheEnd[] = {0xff};
DHCP_TYPE DHCP_Buffer;
WORD ServerPort = IPPORT_DHCPS; // I am the Server
WORD ClientPort = IPPORT_DHCPC;
IPADDR to_addr = 0xffffffff; // Broadcast
IPADDR ClientIP;
char * option_ptr;
//struct addr_struct destinationAddr;
//memcpy( &to_addr, MyIP, 4 );
int DHCPSocket = CreateRxTxUdpSocket( to_addr, ClientPort, ServerPort );
if ( DHCPSocket <= 0 )
{
iprintf("Error Creating UDP Socket: %d\r\n", DHCPSocket);
while (1) { OSTimeDly(TICKS_PER_SECOND); }
} else
{
iprintf( "DHCP Server started, IP Address:");
ShowIP( to_addr );
iprintf( "\r\nServer Port: %d, Client Port: %d\r\n", ServerPort, ClientPort );
}
/*-------------------------------------------------------------------
* This task will wait for incoming UDP packets and process them.
------------------------------------------------------------------*/
while (1)
{
recvfrom( DHCPSocket, (BYTE *)&DHCP_Buffer,
sizeof(DHCP_Buffer), &ClientIP, &ServerPort, &ClientPort );
switch ( DHCP_Buffer.dp_options[2] )
{
case DHCPDISCOVER:
iprintf( "\r\nReceived DHCP DISCOVER message from: ");
ShowIP( ClientIP ); // ClientIP should be 0.0.0.0
iprintf( "\r\n\n");
ShowData((BYTE *)&DHCP_Buffer, sizeof(DHCP_Buffer));
DHCP_Buffer.dp_op = DHCPOFFER;
DHCP_Buffer.dp_flags = 0; // clear flags
DHCP_Buffer.dp_secs = 0; // clear seconds
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, 6 );
option_ptr += 6;
memcpy( option_ptr, Lease_time, 6 );
option_ptr += 6;
memcpy( option_ptr, Server_identification, 6 );
option_ptr += 6;
memcpy( option_ptr, MyName, 8 );
option_ptr += 8;
memcpy( option_ptr, TheEnd, 1 );
option_ptr += 1;
//OSTimeDly(0.5 * TICKS_PER_SECOND);
iprintf("\r\nTransmited DHCP OFFER message to: ");
ShowIP( to_addr );
iprintf(" from: ");
ShowIP( EthernetIP );
iprintf( "\r\n\n");
ShowData((BYTE *)&DHCP_Buffer, sizeof(DHCP_Buffer));
sendto( DHCPSocket, (BYTE *)&DHCP_Buffer,
sizeof( DHCP_Buffer ), to_addr, ClientPort );
break;
case DHCPREQUEST:
iprintf( "\r\nReceived DHCP REQUEST message from: ");
ShowIP( ClientIP );
iprintf( "\r\n\n");
ShowData((BYTE *)&DHCP_Buffer, sizeof(DHCP_Buffer));
DHCP_Buffer.dp_op = DHCPOFFER;
DHCP_Buffer.dp_flags = 0; // clear flags
DHCP_Buffer.dp_secs = 0; // clear seconds
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, 6 );
option_ptr += 6;
memcpy( option_ptr, Lease_time, 6 );
option_ptr += 6;
memcpy( option_ptr, Server_identification, 6 );
option_ptr += 6;
memcpy( option_ptr, MyName, 8 );
option_ptr += 8;
memcpy( option_ptr, TheEnd, 1 );
option_ptr += 1;
iprintf("\r\nTransmited DHCP ACK message to: ");
ShowIP( to_addr );
iprintf(" from: ");
ShowIP( EthernetIP );
iprintf( "\r\n\n");
ShowData((BYTE *)&DHCP_Buffer, sizeof(DHCP_Buffer));
//OSTimeDly(0.5 * TICKS_PER_SECOND);
sendto( DHCPSocket, (BYTE *)&DHCP_Buffer,
sizeof( DHCP_Buffer ), to_addr, ClientPort );
break;
default:
iprintf( "\r\nReceived broadcast message from: ");
ShowIP( ClientIP ); // ClientIP should be 0.0.0.0
iprintf( "\r\n\n");
ShowData((BYTE *)&DHCP_Buffer, sizeof(DHCP_Buffer));
break;
}
}
}
Re: DHCP Server
Very nice find.
Looking at the original it seems its public domain.
I think we will add this to the library.
I'll probably make it so it does not add another task and integrates with our DHCP system,
IE if our DHCP fails we switch to server mode.
Looking at the original it seems its public domain.
I think we will add this to the library.
I'll probably make it so it does not add another task and integrates with our DHCP system,
IE if our DHCP fails we switch to server mode.