![]() |
| Home |
|
|
Function xn_dhcp_server_daemonThis function starts the background DHCP server task. int xn_dhcp_server_daemon(dhcps_config * cfg); ParameterscfgPointer to the structure whose fields determine the server configuration. The individual fields are described below. return valueOn failure, the function returns SOCKET_ERROR and xn_getlasterror and xn_geterror_string return the error value. Error Codes further describes each error. If the function succeeds, it never returns. This routine implements a DHCP server over UDP and may be called after the interface has been initialized. The routine takes as parameter a pointer to a dhcps_config structure, whose fields are used to configure the server. The dhcps_config structure consists of a simple address pool, a few configuration parameters (DNS server, default gateway, etc.), and a field which allows the server to be extended to give the client additional configuration parameters. The fields in the dhcps_config structure determine the server configuration. This structure is declared in Dhcpsapi.h as:
struct dhcps_address_binding {
BYTE start_ip[4];
BYTE end_ip[4];
DWORD lease_time;
};
struct dhcps_option {
int number;
int length;
PFVOID value;
};
struct dhcps_params {
BYTE subnet_mask[4];
BYTE default_gateway[4];
BYTE dns_servers[2][4];
struct dhcps_option * extra_options;
int num_extra_options;
};
struct dhcps_client_profile {
BYTE hw_addr[16];
BYTE ip_addr[4];
DWORD lease_time;
struct dhcps_params * params;
BYTE status;
DWORD cur_lease_time;
};
struct dhcps_config {
struct dhcps_address_binding address_pool[8];
struct dhcps_params pub_params;
struct dhcps_client_profile client_profiles;
int num_client_profiles;
};
See below for instructions on how each field should be initialized. Fields not mentioned here should be initalized to zero and are for the DHCP server's internal use. dhcps_config.address_poolThe address_pool is a list of IP address ranges and associated lease times. For example, initializing this field to:
{ {205,161,8,200}, {205,161,8,205}, 10000 },
{ {205,161,8,220}, {205,161,8,225}, 20000 },
{ {205,161,8,230}, {205,161,8,230}, 30000 }
configures the server to lease out IP addresses 205.161.8.200 through 205.161.8.205 with a lease time of 10,000 seconds, addresses 205.161.8.220 through 205.161.8.225 with a lease time of 20,000 seconds, and the address 205.161.8.230 with a lease time of 30,000 seconds. Up to 8 ranges can be specified. Set unused ranges to zero.
dhcps_config.pub_params.subnet_maskThis field specifies the subnet mask that the server will configure the clients with. For example, initializing this field to:
{255,255,255,0}
will set up clients with a subnet mask of 255.255.255.0.
dhcps_config.pub_params.default_gatewayThis field specifies the default gateway for the subnet. For example, initializing this field to:
{205,161,8,1}
will configure clients with a default gateway of 205.161.8.1.
dhcps_config.pub_params.dns_serversThis field specifies a list of DNS servers used to configure DHCP clients. Initializing this field to:
{ {205,161,8,5}, {205,161,8,1} }
sets up two DNS servers: 205.161.8.5 and 205.161.8.1. If only one DNS server is available, set the second IP address to zero.
dhcps_config.pub_params.extra_options and num_extra_optionsThese fields are used to make the server pass additional configuration parameters to the clients. Assuming the following global variable has been declared:
char my_domain_name[]="on-time.com";
struct dhcps_option my_extra_options[]= {
{ DOMAIN_NAME, sizeof(my_domain_name), my_domain_name }
};
then setting extra_options to my_extra_options and setting num_extra_options to 1 will configure clients with the domain name "on-time.com." Note that all available option numbers are declared in header file Dhcpcapi.h.
Example (Simple):
struct dhcps_params dhcps_parms =
{
{ 255, 255, 255, 0}, // subnet mask
{ 192, 168, 1, 1}, // default gateway
{{192, 168, 1, 1}}, // 1st DNS server
NULL, // extra_options
0 // num_extra_options
};
struct dhcps_config dhcps_cfg =
{
{{{192, 168, 1, 128}, {192, 168, 1, 254}, 3*24*60*60}}, // 3 days
&dhcps_parms
};
/*-----------------------------------*/
int main(void)
{
// initialize the stack
NetInitialize();
// run the DHCP server
if (xn_dhcp_server_daemon(&dhcps_cfg) == SOCKET_ERROR)
Error("xn_dhcp_server_daemon failed");
// the server never returns
return 0;
}
Example (with Options):
char my_dom_name[] = "on-time.com";
dword dhcps_mdrs = 4000; // max frag size
dword dhcps_ip_ttl = 60;
struct dhcps_option my_extra_options[] =
{
{ DOMAIN_NAME, sizeof(my_dom_name), my_dom_name },
{ MDRS, sizeof(dhcps_mdrs), &dhcps_mdrs },
{ DEFAULT_IP_TTL, sizeof(dhcps_ip_ttl), &dhcps_ip_ttl},
};
struct dhcps_params dhcps_parms =
{
{ 255, 255, 255, 0}, // subnet mask
{ 192, 168, 1, 1}, // default gateway
{{192, 168, 1, 1}, // 1st DNS server
{192, 168, 1, 2}}, // 2st DNS server
&my_extra_options,
sizeof(my_extra_options) / sizeof(my_extra_options[0])
};
struct dhcps_config dhcps_cfg =
{
{{{192, 168, 1, 64}, {192, 168, 1, 127}, 1*24*60*60}, // 1 day
{{192, 168, 1, 128}, {192, 168, 1, 254}, 3*24*60*60}}, // 3 days
&dhcps_parms
};
/*-----------------------------------*/
int main(void)
{
// initialize the stack
NetInitialize();
// run the DHCP server
if (xn_dhcp_server_daemon(&dhcps_cfg) == SOCKET_ERROR)
Error("xn_dhcp_server_daemon failed");
// the server never returns
return 0;
}
|