实现域名解析
来源:互联网 发布:macbook pro 软件下载 编辑:程序博客网 时间:2024/05/02 12:19
编写程序,使用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);}
0 0
- 实现域名解析
- 实现泛域名解析
- 实现泛域名解析
- 修改hosts实现域名解析
- 轻松实现域名解析本地化
- Linux文件系统域名解析实现
- c语言实现域名解析
- c语言实现域名解析
- Linux文件系统域名解析实现
- Linux文件系统域名解析实现
- java中实现域名解析
- C++实现DNS域名解析
- LWIP 实现DNS域名解析
- c++ DNS 域名解析实现
- C++实现DNS域名解析
- TCP通信+域名解析实现:
- c++实现域名解析
- linux c实现dns域名解析
- hover事件2,鼠标移入一个标签使其子级标签的css样式发生改变,显示可见
- xlwt模块使用
- puppet 配置 1. 服务器, 客户端配置说明
- ios tableView那些事 (十二) 给 tableview 加个长按快捷菜单
- View的基础知识
- 实现域名解析
- log4j:WARN No appenders could be found for logger
- 编程中的命名设计那点事
- MYSQL使用mysqldump导出某个表的部分数据
- 函数的格式
- linux、SecureCRT安装JDK
- Linux下mysql备份 恢复
- 自定布局 ☞ NSLayoutConstraint 布局
- 【貌似是改编题】Expanding Rods