编写程序,使用gethostbyname,getaddrinfo。
//不过你必须保证自己设备板子的DNS配置部分是正常的。
//不过你必须保证自己设备板子的DNS配置部分是正常的。
代码不一定完全正确,我简单测试了一下,可以用 。 主要是:bool TE_GetRealIP(char *HostName,char *strRealIP),输入的HostName可以是IP,也可以是域名,strRealIP得到真正的IP
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <time.h>
#include <ifaddrs.h>
#include <string.h>
#include <errno.h>
#include <string>
#include <iostream>
#include <poll.h>
#include <pthread.h>
#include <signal.h>
#include <fcntl.h>
#include <libgen.h>
#include <dirent.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netinet/tcp.h>
#include <netinet/if_ether.h>
#include <asm/types.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/neighbour.h>
#include <linux/if_addr.h>
#include <sys/sysinfo.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <net/route.h>
#include <error.h>
#include <math.h>
#include <sys/socket.h>
#include <sys/signal.h>
#include <sys/time.h>
#include <sys/vfs.h>
#include <sys/statvfs.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <arpa/inet.h>
using namespace std;
#include <netdb.h>
bool TE_CheckIfRealIp(char *IP)
{
if(!IP)
return false;
int nlen=strlen(IP);
//长度不能小于7不能大于15
if(nlen<7||nlen>15)
return false;
//不能有非0-9以外的字母
for(int i=0;i<nlen;i++){
if((char)IP[i]=='.')
continue;
if((char)IP[i]<'0'||(char)IP[i]>'9')
return false;
}
return true;
}
DWORD platform_domain2ipEx(const char *szDomain, char *szDotNumIP)
{
char szAddr[32] = {0};
struct hostent *pHost;
if((pHost = gethostbyname(szDomain)) == NULL)
return -1;
DWORD dwIP = inet_addr( inet_ntoa(*(struct in_addr *)*pHost->h_addr_list) );
strcpy(szAddr, inet_ntoa(*((struct in_addr *)pHost->h_addr)));
if(TE_CheckIfRealIp(szAddr)){
strcpy(szDotNumIP, szAddr);
return dwIP;
}
return 0;
}
bool TE_GetRealIP(char *HostName,char *strRealIP)
{
if(!HostName||!strRealIP)
return false;
struct addrinfo hints;
struct addrinfo *result, *result_pointer;
int ret;
bool bGetIP=false;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_CANONNAME;
hints.ai_protocol = 0;
ret = getaddrinfo(HostName, NULL, &hints, &result);
if (ret != 0)
return false;
DebugInf("getnameinfo:%s int \n",HostName);
for (result_pointer = result; result_pointer != NULL; result_pointer = result_pointer->ai_next)
{
char hostname[1025] = "";
ret = getnameinfo(result_pointer->ai_addr, result_pointer->ai_addrlen, hostname, sizeof(hostname), NULL, 0, NI_NUMERICHOST);
if (ret != 0)
continue;
else
{
DebugInf("getnameinfo:%s IP: %s \n",HostName,hostname);
if(strlen(hostname)>0&&TE_CheckIfRealIp(hostname)){
printf("HostName:%s IP: %s \n",HostName,hostname);
sprintf(strRealIP,"%s",hostname);
bGetIP=true;
break;
}
}
}
freeaddrinfo(result);
DebugInf("getnameinfo:%s in 2 \n",HostName);
//没有获取到
if(!bGetIP){
DebugInf("platform_domain2ipEx:%s int \n",HostName);
if(platform_domain2ipEx(HostName,strRealIP)!=-1)
bGetIP=true;
}
if(bGetIP)
return true;
else
return false;
}
void WriteNewDNS(char *_strNewDNS)
{
char strCMD[128];
memset(strCMD,0,sizeof strCMD);
删除系统的DNS文件
printf("WriteNewDNS delete resolv.conf \n");
sprintf(strCMD,"/bin/rm /etc/resolv.conf");
system(strCMD);
printf("WriteNewDNS recreate resolv.conf \n");
sprintf(strCMD,"echo \"nameserver %s \">> /etc/resolv.conf",_strNewDNS);
system(strCMD);
}