- /*************************************************************************
- > File Name: gg.cpp
- ************************************************************************/
- #include <string.h>
- #include <iostream>
- #include <map>
- #include <sys/time.h>
- #include <stdint.h>
- #include <stdio.h>
- inline char* getDomByHost(const char* szHost, int hostLen)
- {
- if ( 0 == *szHost )
- return NULL;
- char *strHost = hostLen + (char*)szHost;
- int index = 0;
- while( strHost != szHost)
- {
- if (*strHost == '.' || *strHost == ' ')
- {
- index++;
- // Host: www.google.com.hk 当倒数第二个‘.’号后面的字符串等于com,
- //net,org,gov时 默认取倒数第三个'.'后的字符串为域名
- if (index == 2 && ( (0 == memcmp(strHost + 1, "com", sizeof("com")-1 )) \\
- || (0 == memcmp(strHost + 1, "net", sizeof("net")-1 )) \\
- || (0 == memcmp(strHost + 1, "org", sizeof("org")-1 )) \\
- || (0 == memcmp(strHost + 1, "gov", sizeof("gov")-1 )) ))
- {
- index = 1;
- }
- if ( 2 == index )
- return strHost + 1;
- }
- --strHost;
- }
- return NULL;
- }
- #include <netdb.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- inline char* getHostByIP(const char* szIPHost)
- {
- struct sockaddr_in addr;
- struct hostent *host;
- if(inet_aton(szIPHost, &addr.sin_addr)!=0)
- {
- host = gethostbyaddr((char*)&addr.sin_addr,4,AF_INET);
- }
- else
- {
- host = gethostbyname(szIPHost);
- }
- if(host == NULL)
- {
- return NULL;
- }
- return host->h_name;
- #if 0
- printf("Official host name: %s\\n",host->h_name);
- printf("Name aliases: ");
- for(alias = host->h_aliases; *alias!=NULL; alias++)
- printf("%s ,", *alias);
- printf("\\nIp address: ");
- for(alias = host->h_addr_list; *alias!=NULL; alias++)
- printf("%s ,", inet_ntoa(*(struct in_addr *)(*alias)));
- return NULL;
- #endif
- }
- inline void getDomain(const char* szHost, char* szOut, int outLen)
- {
- //参数合法检测
- if ( *szHost == 0 || NULL == szOut)
- return ;
- //ASSERT(host == NULL);
- char host_copy[64];
- memcpy(host_copy, szHost, 64);
- char *out = NULL;
- char *flag = strchr(host_copy, ':');
- if (flag != NULL ){
- *flag = '\\0';
- //Host: 74.125.71.103:8080
- if ( '0' <= *host_copy && '9' >= *host_copy){
- out = getHostByIP(host_copy);
- if (NULL != out){
- char *tmp;
- tmp = getDomByHost(out, strlen(out));
- if ( NULL != tmp)
- strncpy(szOut, tmp , outLen);
- }
- }
- else {
- out = getDomByHost(host_copy, strlen(host_copy));
- if (NULL != out)
- strncpy(szOut, out , outLen);
- }
- }
- //Host: 74.125.71.103
- else if ( '0' <= *host_copy && '9' >= *host_copy){
- out = getHostByIP(host_copy);
- if (NULL != out){
- char *tmp;
- tmp = getDomByHost(out, strlen(out));
- if ( NULL != tmp)
- strncpy(szOut, tmp , outLen);
- }
- }
- else {
- out = getDomByHost(host_copy, strlen(host_copy));
- if (NULL != out)
- strncpy(szOut, out , outLen);
- }
- }
- int main()
- {
- char hh[20][64]={"t.qq.com",
- "www.google.org.sg",
- "www.bbs.sina.com.cn",
- "www.baidu.xxx.msn.cn",
- "mm.t.qq.com",
- "cc.t.sohu.com",
- "sfafsaf.qq.com",
- "news.sohu.com",
- "dict.youdao.com",
- "ynote.youdao.com",
- "Host: note.sdo.com:8080",
- "74.125.71.105",
- "74.125.71.103:80",
- "119.75.217.56:80"};
- std::cout <<"----------------------------------------------------------------" <<std::endl;
- char out[64];
- memset(out,0 , 64);
- for (int i = 0 ; i < 14 ; i++)
- {
- getDomain(hh[i], out, 64);
- std::cout << out <<std::endl;
- memset(out,0 , 64);
- }
- std::cout << getHostByIP("www.google.com") <<std::endl;
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/040720134420.html
来源: http://www.codesnippet.cn/detail/040720134420.html