当前位置: 技术问答>linux和unix
1.请问如何编程获取网关ip地址 2.如何在一块网卡中绑定多个ip地址?
来源: 互联网 发布时间:2015-10-13
本文导语: 1.请问如何编程获取网关ip地址 2.如何在一块网卡中绑定多个ip地址? 我知道怎样编程设置网卡的ip地址,但如何添加多个ip地址给一个网卡? 请大家帮忙,多谢! | 我这有设...
1.请问如何编程获取网关ip地址
2.如何在一块网卡中绑定多个ip地址?
我知道怎样编程设置网卡的ip地址,但如何添加多个ip地址给一个网卡?
请大家帮忙,多谢!
2.如何在一块网卡中绑定多个ip地址?
我知道怎样编程设置网卡的ip地址,但如何添加多个ip地址给一个网卡?
请大家帮忙,多谢!
|
我这有设置网关的代码,不知有用否?
bool SetDefaultRoute(char *szGateWay)
{
int sockfd;
struct rtentry rm;
struct sockaddr_in ic_gateway ; // Gateway IP address
int err;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == -1)
{
return FALSE;
}
memset(&rm, 0, sizeof(rm));
ic_gateway.sin_family = AF_INET;
ic_gateway.sin_addr.s_addr = inet_addr(szGateWay);
ic_gateway.sin_port = 0;
/* if ((ic_gateway ^ sin.sin_addr) & mask.sin_addr) {
printf("IP-Config: Gateway not on directly connected network.n");
return FALSE;
} */
(( struct sockaddr_in*)&rm.rt_dst)->sin_family = AF_INET;
(( struct sockaddr_in*)&rm.rt_dst)->sin_addr.s_addr = 0;
(( struct sockaddr_in*)&rm.rt_dst)->sin_port = 0;
(( struct sockaddr_in*)&rm.rt_genmask)->sin_family = AF_INET;
(( struct sockaddr_in*)&rm.rt_genmask)->sin_addr.s_addr = 0;
(( struct sockaddr_in*)&rm.rt_genmask)->sin_port = 0;
memcpy((void *) &rm.rt_gateway, &ic_gateway, sizeof(ic_gateway));
rm.rt_flags = RTF_UP | RTF_GATEWAY;
if ((err = ioctl(sockfd, SIOCADDRT, &rm))
bool SetDefaultRoute(char *szGateWay)
{
int sockfd;
struct rtentry rm;
struct sockaddr_in ic_gateway ; // Gateway IP address
int err;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == -1)
{
return FALSE;
}
memset(&rm, 0, sizeof(rm));
ic_gateway.sin_family = AF_INET;
ic_gateway.sin_addr.s_addr = inet_addr(szGateWay);
ic_gateway.sin_port = 0;
/* if ((ic_gateway ^ sin.sin_addr) & mask.sin_addr) {
printf("IP-Config: Gateway not on directly connected network.n");
return FALSE;
} */
(( struct sockaddr_in*)&rm.rt_dst)->sin_family = AF_INET;
(( struct sockaddr_in*)&rm.rt_dst)->sin_addr.s_addr = 0;
(( struct sockaddr_in*)&rm.rt_dst)->sin_port = 0;
(( struct sockaddr_in*)&rm.rt_genmask)->sin_family = AF_INET;
(( struct sockaddr_in*)&rm.rt_genmask)->sin_addr.s_addr = 0;
(( struct sockaddr_in*)&rm.rt_genmask)->sin_port = 0;
memcpy((void *) &rm.rt_gateway, &ic_gateway, sizeof(ic_gateway));
rm.rt_flags = RTF_UP | RTF_GATEWAY;
if ((err = ioctl(sockfd, SIOCADDRT, &rm))