当前位置: 技术问答>linux和unix
多网卡的网络DHCP应用
来源: 互联网 发布时间:2016-07-04
本文导语: 我现在有两个网卡,分别在不同的网络里,IP地址的分别是通过DHCP服务器自动分配,设计了一套DHCP客户端申请程序,单个网口可以实现IP地址的动态分配,串行方式两个网口都可动态获得IP地址,能不能实现两个网口...
我现在有两个网卡,分别在不同的网络里,IP地址的分别是通过DHCP服务器自动分配,设计了一套DHCP客户端申请程序,单个网口可以实现IP地址的动态分配,串行方式两个网口都可动态获得IP地址,能不能实现两个网口同时动态申请IP地址哪?这样可以节约时间,因为一个DHCP申请需要大约8s左右,望高手不吝赐教。谢谢!!!!
STATUS DhcpClient(char * ipName)
{
unsigned long duration;
struct ifnet *pIf;
STATUS result;
void *pLeaseCookie;
extern struct ifnet *ifunit ();
struct dhcp_param paramList;
pIf = ifunit (ipName); /* Access network device. */
/* Initialize lease variables for automatic configuration. */
pLeaseCookie = dhcpcInit (pIf, TRUE);
if (pLeaseCookie == NULL)
{
printf("dhcpcInit errorn");
return (ERROR);
}
/* Set any lease options here. */
duration = htonl(DHCP_PORT);
dhcpcOptionAdd (pLeaseCookie, _DHCP_LEASE_TIME_TAG, 4, (UCHAR*)&duration);
dhcpcOptionSet (pLeaseCookie, _DHCP_ROUTER_TAG);
dhcpcOptionSet (pLeaseCookie, _DHCP_DNS_SERVER_TAG);
dhcpcOptionSet (pLeaseCookie, _DHCP_DNS_DOMAIN_TAG);
dhcpcOptionSet (pLeaseCookie, _DHCP_ROUTER_DISCOVER_TAG);
dhcpcOptionSet (pLeaseCookie, _DHCP_STATIC_ROUTE_TAG);
dhcpcOptionSet (pLeaseCookie, _DHCP_VENDOR_SPEC_TAG);
dhcpcOptionSet (pLeaseCookie, _DHCP_NBN_SERVER_TAG);
dhcpcOptionSet (pLeaseCookie, _DHCP_NB_NODETYPE_TAG);
dhcpcOptionSet (pLeaseCookie, _DHCP_NB_SCOPE_TAG);
result = dhcpcBind (pLeaseCookie, TRUE); // Synchronous execution.
if (result != OK)
{
printf("dhcpcBind errorn");
return (ERROR);
}
/****************************************************************/
/**************** Using dhcpcParamsGet() ***********************/
/* prepare to use dhcpcParamsGet() */
memset((char *)¶mList, 0x00, sizeof(paramList));
paramList.subnet_mask = (struct in_addr *)malloc(sizeof(struct
in_addr));
memset((char *)paramList.subnet_mask, 0x00,
sizeof(paramList.subnet_mask));
paramList.sname = (char *)malloc(256);
memset((char *)paramList.sname, 0x00, sizeof(paramList.sname));
paramList.dns_server = (struct in_addrs *)malloc(sizeof(struct in_addrs));
memset((char *)paramList.dns_server, 0x00,
sizeof(paramList.dns_server));
paramList.dns_domain = (char *)malloc(256);
if (dhcpcParamsGet(pLeaseCookie, ¶mList) == ERROR)
logMsg("Can not get parameter list. % x n", errno,0,0,0,0,0);
else
{
logMsg("DHCP server host name is: %s. n", paramList.sname,0,0,0,0,0);
logMsg("DHCP dns domain name is: %s. n",
paramList.dns_domain,0,0,0,0,0);
logMsg("DHCP server's IP address is: %s.n",
inet_ntoa(paramList.server_id),0,0,0,0,0);
logMsg("This client's pre-dhcp assigned address is: %s.n",
inet_ntoa(paramList.ciaddr),0,0,0,0,0);
logMsg("This client's assigned address is: %s.n",
inet_ntoa(paramList.yiaddr),0,0,0,0,0);
logMsg("This client's subnet mask is:%s.n",
inet_ntoa(*(paramList.subnet_mask)),0,0,0,0,0);
/* next log msg was commented out by sjk since it doesn't work */
//logMsg("DNS server IP address is: %s. n",
//inet_ntoa(*(paramList.dns_server)),0,0,0,0,0);
/* dhcpcParamsShow() gives the dns IP addresses */
}
//dhcpcServerShow(pLeaseCookie);
return OK;
}
STATUS DhcpClient(char * ipName)
{
unsigned long duration;
struct ifnet *pIf;
STATUS result;
void *pLeaseCookie;
extern struct ifnet *ifunit ();
struct dhcp_param paramList;
pIf = ifunit (ipName); /* Access network device. */
/* Initialize lease variables for automatic configuration. */
pLeaseCookie = dhcpcInit (pIf, TRUE);
if (pLeaseCookie == NULL)
{
printf("dhcpcInit errorn");
return (ERROR);
}
/* Set any lease options here. */
duration = htonl(DHCP_PORT);
dhcpcOptionAdd (pLeaseCookie, _DHCP_LEASE_TIME_TAG, 4, (UCHAR*)&duration);
dhcpcOptionSet (pLeaseCookie, _DHCP_ROUTER_TAG);
dhcpcOptionSet (pLeaseCookie, _DHCP_DNS_SERVER_TAG);
dhcpcOptionSet (pLeaseCookie, _DHCP_DNS_DOMAIN_TAG);
dhcpcOptionSet (pLeaseCookie, _DHCP_ROUTER_DISCOVER_TAG);
dhcpcOptionSet (pLeaseCookie, _DHCP_STATIC_ROUTE_TAG);
dhcpcOptionSet (pLeaseCookie, _DHCP_VENDOR_SPEC_TAG);
dhcpcOptionSet (pLeaseCookie, _DHCP_NBN_SERVER_TAG);
dhcpcOptionSet (pLeaseCookie, _DHCP_NB_NODETYPE_TAG);
dhcpcOptionSet (pLeaseCookie, _DHCP_NB_SCOPE_TAG);
result = dhcpcBind (pLeaseCookie, TRUE); // Synchronous execution.
if (result != OK)
{
printf("dhcpcBind errorn");
return (ERROR);
}
/****************************************************************/
/**************** Using dhcpcParamsGet() ***********************/
/* prepare to use dhcpcParamsGet() */
memset((char *)¶mList, 0x00, sizeof(paramList));
paramList.subnet_mask = (struct in_addr *)malloc(sizeof(struct
in_addr));
memset((char *)paramList.subnet_mask, 0x00,
sizeof(paramList.subnet_mask));
paramList.sname = (char *)malloc(256);
memset((char *)paramList.sname, 0x00, sizeof(paramList.sname));
paramList.dns_server = (struct in_addrs *)malloc(sizeof(struct in_addrs));
memset((char *)paramList.dns_server, 0x00,
sizeof(paramList.dns_server));
paramList.dns_domain = (char *)malloc(256);
if (dhcpcParamsGet(pLeaseCookie, ¶mList) == ERROR)
logMsg("Can not get parameter list. % x n", errno,0,0,0,0,0);
else
{
logMsg("DHCP server host name is: %s. n", paramList.sname,0,0,0,0,0);
logMsg("DHCP dns domain name is: %s. n",
paramList.dns_domain,0,0,0,0,0);
logMsg("DHCP server's IP address is: %s.n",
inet_ntoa(paramList.server_id),0,0,0,0,0);
logMsg("This client's pre-dhcp assigned address is: %s.n",
inet_ntoa(paramList.ciaddr),0,0,0,0,0);
logMsg("This client's assigned address is: %s.n",
inet_ntoa(paramList.yiaddr),0,0,0,0,0);
logMsg("This client's subnet mask is:%s.n",
inet_ntoa(*(paramList.subnet_mask)),0,0,0,0,0);
/* next log msg was commented out by sjk since it doesn't work */
//logMsg("DNS server IP address is: %s. n",
//inet_ntoa(*(paramList.dns_server)),0,0,0,0,0);
/* dhcpcParamsShow() gives the dns IP addresses */
}
//dhcpcServerShow(pLeaseCookie);
return OK;
}
|
不是很明白,
两个网卡分别处于不同的网络里,各自的网络里有一个DHCP Server。
两个网卡分别于各自的网络进行IP地址申请。
不明白你说的串行,并行是什么意思?
两个网卡分别处于不同的网络里,各自的网络里有一个DHCP Server。
两个网卡分别于各自的网络进行IP地址申请。
不明白你说的串行,并行是什么意思?