RTCS DHCP server Implementation example for the MCF52259

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

RTCS DHCP server Implementation example for the MCF52259

跳至解决方案
9,363 次查看
RBORB
NXP Employee
NXP Employee

Hello,

 

Does anyone have an example of a DHCP server for MQX/RTCS?

 

Thanks,

RB

标签 (1)
标记 (1)
0 项奖励
回复
1 解答
2,909 次查看
RBORB
NXP Employee
NXP Employee
Here is what I did to get DHCP server working:
1.- In ../rtcs/sources/apps/dhcpsrv.c  in the function DHCPSRV_write_header
modify the definition of the variable    optlen    to   volatile uint_32           optlen;  See below.
/*FUNCTION*-------------------------------------------------------------
*
* Function Name   : DHCPSRV_write_header
* Returned Value  : void
* Comments        : This function fills the send buffer with the
*                   initial values needed in a DHCP response.
*
*END*-----------------------------------------------------------------*/
static void DHCPSRV_write_header
   (
      DHCPSRV_STATE_STRUCT_PTR   state,
      DHCPSRV_ADDR_STRUCT_PTR    lease_ptr,
      uint_32                    lease_time,
      uchar                      msgtype
   )
{ /* Body */
   DHCP_HEADER_PTR   outp;
   DHCP_HEADER_PTR   inp;
   uchar_ptr         optptr;
   volatile uint_32           optlen;
   uint_32           temp_long;
   uint_16           temp_short;
   uchar             temp_char;
In RTCS.c  add the following:

/* Start DHCP Server: */
 error = DHCPSRV_init("DHCP server", 7, 2000);
 if (error != RTCS_OK) {
 printf("\nFailed to initialize DHCP Server, error %x", error);
 return;
 }
 printf("\nDHCP Server running");
 /* Fill in the required parameters: */
 /* 192.168.0.1: */
 dhcpsrv_data.SERVERID = 0xC0A80001;
 /* Infinite leases: */
 dhcpsrv_data.LEASE = 0xFFFFFFFF;
 /* 255.255.255.0: */
 dhcpsrv_data.MASK = 0xFFFFFF00;
 /* TFTP server address: */
 dhcpsrv_data.SADDR = 0xC0A80001;
 memset(dhcpsrv_data.SNAME, 0, sizeof(dhcpsrv_data.SNAME));
 memset(dhcpsrv_data.FILE, 0, sizeof(dhcpsrv_data.FILE));
 /* Fill in the options: */
 optptr = dhcpsrv_options;
 optlen = sizeof(dhcpsrv_options);
 /* Default IP TTL: */
 DHCPSRV_option_int8(&optptr, &optlen, 23, 64);
 /* MTU: */
 DHCPSRV_option_int16(&optptr, &optlen, 26, 1500);
 /* Renewal time: */
 DHCPSRV_option_int32(&optptr, &optlen, 58, 3600);
 /* Rebinding time: */
 DHCPSRV_option_int32(&optptr, &optlen, 59, 5400);
 /* Domain name: */
 DHCPSRV_option_string(&optptr, &optlen, 15, "arc.com");
 /* Broadcast address: */
 DHCPSRV_option_addr(&optptr, &optlen, 28, 0xC0A800FF);
 /* Router list: */
 routers[0] = 0xC0A80001;
// routers[1] = 0xC0A80005;
// routers[2] = 0xC0A80006;
 DHCPSRV_option_addrlist( &optptr, &optlen, 3, routers, 1);
 /* Serve addresses 192.168.0.129 to 192.168.0.135 inclusive: */
 DHCPSRV_ippool_add(0xC0A80081, 7, &dhcpsrv_data, dhcpsrv_options,optptr - dhcpsrv_options);

 

 

 

 modified the following function in dhcpsrv.c

 
Added the following line.  state->SND_BUFFER_LEN++;   //RB
 Looks like that works.
See below:
 
 
 
/*FUNCTION*-------------------------------------------------------------
*
* Function Name   : DHCPSRV_send
* Returned Value  : void
* Comments        : This function sends a prebuilt DHCP reply.
*
*END*-----------------------------------------------------------------*/
static void DHCPSRV_send
   (
      DHCPSRV_STATE_STRUCT_PTR   state
   )
{ /* Body */
   _ip_address    giaddr;
   sockaddr_in    raddr;
   giaddr = ntohl(((DHCP_HEADER_PTR)state->RCV_BUFFER)->GIADDR);
   if (giaddr) {
      raddr.sin_family      = AF_INET;
      raddr.sin_port        = IPPORT_BOOTPS;
      raddr.sin_addr.s_addr = giaddr;
   } else {
      raddr.sin_family      = AF_INET;
      raddr.sin_port        = IPPORT_BOOTPC;
      raddr.sin_addr.s_addr = INADDR_BROADCAST;
   } /* Endif */
   /* Start CR 1131 */
   state->SND_BUFFER_LEN++;   //RB
   sendto(state->SOCKET, state->SND_BUFFER, state->SND_BUFFER_LEN,
      RTCS_MSG_NOLOOP, &raddr, sizeof(raddr));
   /* End CR 1131 */
} /* Endbody */

在原帖中查看解决方案

0 项奖励
回复
9 回复数
2,909 次查看
anthony_huereca
NXP Employee
NXP Employee
There are DHCP examples for the TWR-MCF51CN-KIT, in the newly released MQX 3.2.1. Look in the SEC_InitializeNetworking() function in the security_webserver or security_telnet demo folders. You should be able to take that code and copy it into your MCF52259DEMO project without many modifications needed.
0 项奖励
回复
2,909 次查看
Orzor
Contributor I

Hi, I'm a new in this forum. I'm a french student and I would like know if you had a program or an example for connect the M52259DEMO in DHCP.

 

I would like connect the module any where.

 

Thanks.

 

Best Regard.

 

Orzor :smileywink:

0 项奖励
回复
2,910 次查看
RBORB
NXP Employee
NXP Employee
Here is what I did to get DHCP server working:
1.- In ../rtcs/sources/apps/dhcpsrv.c  in the function DHCPSRV_write_header
modify the definition of the variable    optlen    to   volatile uint_32           optlen;  See below.
/*FUNCTION*-------------------------------------------------------------
*
* Function Name   : DHCPSRV_write_header
* Returned Value  : void
* Comments        : This function fills the send buffer with the
*                   initial values needed in a DHCP response.
*
*END*-----------------------------------------------------------------*/
static void DHCPSRV_write_header
   (
      DHCPSRV_STATE_STRUCT_PTR   state,
      DHCPSRV_ADDR_STRUCT_PTR    lease_ptr,
      uint_32                    lease_time,
      uchar                      msgtype
   )
{ /* Body */
   DHCP_HEADER_PTR   outp;
   DHCP_HEADER_PTR   inp;
   uchar_ptr         optptr;
   volatile uint_32           optlen;
   uint_32           temp_long;
   uint_16           temp_short;
   uchar             temp_char;
In RTCS.c  add the following:

/* Start DHCP Server: */
 error = DHCPSRV_init("DHCP server", 7, 2000);
 if (error != RTCS_OK) {
 printf("\nFailed to initialize DHCP Server, error %x", error);
 return;
 }
 printf("\nDHCP Server running");
 /* Fill in the required parameters: */
 /* 192.168.0.1: */
 dhcpsrv_data.SERVERID = 0xC0A80001;
 /* Infinite leases: */
 dhcpsrv_data.LEASE = 0xFFFFFFFF;
 /* 255.255.255.0: */
 dhcpsrv_data.MASK = 0xFFFFFF00;
 /* TFTP server address: */
 dhcpsrv_data.SADDR = 0xC0A80001;
 memset(dhcpsrv_data.SNAME, 0, sizeof(dhcpsrv_data.SNAME));
 memset(dhcpsrv_data.FILE, 0, sizeof(dhcpsrv_data.FILE));
 /* Fill in the options: */
 optptr = dhcpsrv_options;
 optlen = sizeof(dhcpsrv_options);
 /* Default IP TTL: */
 DHCPSRV_option_int8(&optptr, &optlen, 23, 64);
 /* MTU: */
 DHCPSRV_option_int16(&optptr, &optlen, 26, 1500);
 /* Renewal time: */
 DHCPSRV_option_int32(&optptr, &optlen, 58, 3600);
 /* Rebinding time: */
 DHCPSRV_option_int32(&optptr, &optlen, 59, 5400);
 /* Domain name: */
 DHCPSRV_option_string(&optptr, &optlen, 15, "arc.com");
 /* Broadcast address: */
 DHCPSRV_option_addr(&optptr, &optlen, 28, 0xC0A800FF);
 /* Router list: */
 routers[0] = 0xC0A80001;
// routers[1] = 0xC0A80005;
// routers[2] = 0xC0A80006;
 DHCPSRV_option_addrlist( &optptr, &optlen, 3, routers, 1);
 /* Serve addresses 192.168.0.129 to 192.168.0.135 inclusive: */
 DHCPSRV_ippool_add(0xC0A80081, 7, &dhcpsrv_data, dhcpsrv_options,optptr - dhcpsrv_options);

 

 

 

 modified the following function in dhcpsrv.c

 
Added the following line.  state->SND_BUFFER_LEN++;   //RB
 Looks like that works.
See below:
 
 
 
/*FUNCTION*-------------------------------------------------------------
*
* Function Name   : DHCPSRV_send
* Returned Value  : void
* Comments        : This function sends a prebuilt DHCP reply.
*
*END*-----------------------------------------------------------------*/
static void DHCPSRV_send
   (
      DHCPSRV_STATE_STRUCT_PTR   state
   )
{ /* Body */
   _ip_address    giaddr;
   sockaddr_in    raddr;
   giaddr = ntohl(((DHCP_HEADER_PTR)state->RCV_BUFFER)->GIADDR);
   if (giaddr) {
      raddr.sin_family      = AF_INET;
      raddr.sin_port        = IPPORT_BOOTPS;
      raddr.sin_addr.s_addr = giaddr;
   } else {
      raddr.sin_family      = AF_INET;
      raddr.sin_port        = IPPORT_BOOTPC;
      raddr.sin_addr.s_addr = INADDR_BROADCAST;
   } /* Endif */
   /* Start CR 1131 */
   state->SND_BUFFER_LEN++;   //RB
   sendto(state->SOCKET, state->SND_BUFFER, state->SND_BUFFER_LEN,
      RTCS_MSG_NOLOOP, &raddr, sizeof(raddr));
   /* End CR 1131 */
} /* Endbody */
0 项奖励
回复
2,909 次查看
Vijayaragavan
Contributor I

Hi RBORB,

 

                    can you please explain me step-by-step how to implemet DHCP server on demo MCF52259 board.

I am new to MQXRTOS.

 

Thanks Vijay

 

 

 

 

0 项奖励
回复
2,909 次查看
mitsaltair
Contributor I

Hi,

 

For hardware that implements two Ethernet ports (configured to be on different subnets), is there any way to configure the dhcp server to respond to discover requests on only one port?

 

I got the dhcp server working, however it would respond to dhcp discover requests received from either Ethernet port (which is something I do not want).

 

  Thanks,

           mitsaltair

0 项奖励
回复
2,909 次查看
PetrM
Senior Contributor I

Hello,

 

DHCP server example is planned to be included (among others) in further releases of MQX.

 

PetrM

0 项奖励
回复
2,909 次查看
Vijayaragavan
Contributor I


Hi

 

This is not coming to this line

 

 printf("\nDHCP Server running...\n");

 

it stays this line only

 

DHCPSRV_init();

 

help me its urgent

 

0 项奖励
回复
2,909 次查看
PetrM
Senior Contributor I

There's a missing initialization of ethernet device and RTCS itself. DHCP server depends on those two. Please look at some ethernet example (e.g. telnet_to_serial demo) for correct initialization procedure.

 

PetrM

 

0 项奖励
回复
2,909 次查看
Vijayaragavan
Contributor I

Hi

 

 

This is my code.but this is not working. what i am doing wrong.

 

void dhcpserver_task(uint_32);

TASK_TEMPLATE_STRUCT  MQX_template_list[] =
{
    {HELLO_TASK, dhcpserver_task, 1500, 9, "hello", MQX_AUTO_START_TASK, 0, 0},
    {0,          0,          0,   0, 0,       0,                   0, 0}
};

void dhcpserver_task(uint_32 initial_data)
{
 
   DHCPSRV_DATA_STRUCT dhcpserver_data;
   uchar    dhcpserver_options[200];
   _ip_address      routers[3];
   uchar_ptr   optptr;
   uint_32    error;
   uint_32    optlen;  
  
   error = DHCPSRV_init("mydhcp",7,2000);
  
   if(error != RTCS_OK)
   {
    printf("\nDHCP server initialize returned : %08x\n",error);
    printf("\nFailed to initialize the DHCP server\n");
    _mqx_exit(0);
   }  
   printf("\nDHCP Server running...\n");
   dhcpserver_data.SERVERID = 0xC0A80101;
   dhcpserver_data.LEASE    = 0xFFFFFFFF;
   dhcpserver_data.MASK  = 0xFFFFFF00;
   dhcpserver_data.SADDR = 0xC0A80102;  
   _mem_zero(dhcpserver_data.SNAME,sizeof(dhcpserver_data.SNAME));
   _mem_zero(dhcpserver_data.FILE,sizeof(dhcpserver_data.FILE));
   optptr = dhcpserver_options;
   optlen = sizeof(dhcpserver_options);  
   DHCPSRV_option_int8(&optptr,&optlen,23,64);
   DHCPSRV_option_int16(&optptr,&optlen,26,1500);
   DHCPSRV_option_int32(&optptr,&optlen,58,3600);
   DHCPSRV_option_int32(&optptr,&optlen,59,5400);
   DHCPSRV_option_string(&optptr,&optlen,15,"arc.com");
   DHCPSRV_option_addr(&optptr,&optlen,28,0xC0A801FF);
   routers[0] = 0xC0A80104;
   routers[1] = 0xC0A80105;
   routers[2] = 0xC0A80106;
   DHCPSRV_option_addrlist(&optptr,&optlen,3,routers,3);
   DHCPSRV_ippool_add(0xC0A80181,7,&dhcpserver_data,dhcpserver_options,optptr - dhcpserver_options);
   _task_block();
  
}

 

Thanks

Vijay

 

0 项奖励
回复