DNSClient.cpp
资源名称:FindMail.zip [点击查看]
上传用户:feituo2008
上传日期:2013-02-02
资源大小:493k
文件大小:5k
源码类别:
Email客户端
开发平台:
Visual C++
- #include "StdAfx.h"
- #include "iphlpapi.h"
- #include "DNSClient.h"
- #include "udp.h"
- #include "util.h"
- CDNSClient::CDNSClient(void)
- : m_mx_server_count(0)
- , m_mx_servers(NULL)
- , m_dns_count(0)
- , m_dns_servers(NULL)
- {
- GetDNS();
- }
- CDNSClient::~CDNSClient(void)
- {
- FreeMxServers();
- FreeDnsServers();
- }
- int CDNSClient::GetMxServers(char * domain_name)
- {
- // 使用UDP查询结果
- int sd =udp_connect(m_dns_ip, 53);
- if(sd <0)
- return -1;
- FreeMxServers();
- char buf[512];
- memset(buf, 0, sizeof(buf));
- buf[0] =0;
- buf[1] =1; // 序号
- buf[2] =1; // 查询
- buf[3] =buf[4] =0;
- buf[5] =1; // 1个问题
- char dm[100];
- strcpy(dm, domain_name);
- char *p =dm;
- char *p1 =strchr(p, '.');
- int pos =12;
- while(p)
- {
- if(p1) *p1 =0;
- buf[pos] =(char)strlen(p);
- strcpy(&buf[pos+1], p);
- pos +=(char)(strlen(p)+1);
- if(p1)
- {
- p =p1+1;
- p1 =strchr(p, '.');
- }
- else p =NULL;
- }
- buf[pos++] =0;
- buf[pos++] =0;
- buf[pos++] =15;
- buf[pos++] =0;
- buf[pos++] =1;
- udp_send(sd, buf, sizeof(buf));
- int len =udp_recv(sd, buf, sizeof(buf), 20);
- udp_close(sd);
- if(len <0)
- return -1;
- int qcount =(buf[4] & 0x00FF << 8) | (buf[5] & 0x00FF); // 问题数
- if(qcount <0)
- return 0;
- int acount =(buf[6] & 0xFF << 8) | (buf[7] & 0x00FF);
- if(acount <0)
- return 0;
- pos =12;
- int i;
- //WriteBinData(buf, len);
- for(i =0; i<qcount; i++)
- {
- dm[0] =0;
- pos =SkipString(buf, pos, dm)+4;
- //WriteLog("dm:%s", dm);
- }
- for(i =0; i<acount; i++)
- {
- dm[0] =0;
- pos =SkipString(buf, pos, dm)+10+2;
- //WriteLog("dm:%s", dm);
- dm[0] =0;
- pos =SkipString(buf, pos, dm);
- //WriteLog("dm:%s", dm);
- AddMxServer(dm);
- WriteStat("mx server %d:%s", i, dm);
- }
- return m_mx_server_count;
- }
- int CDNSClient::GetDNS(void)
- {
- //strcpy(m_dns_ip, "202.103.96.112");
- IP_ADDR_STRING *pIPAddr;
- ULONG len;
- DWORD ret;
- FIXED_INFO *pfixed_info =(FIXED_INFO *)malloc(sizeof(FIXED_INFO));
- len =sizeof(FIXED_INFO);
- FreeDnsServers();
- if((ret =GetNetworkParams(pfixed_info, &len)) !=ERROR_BUFFER_OVERFLOW && ret !=0)
- {
- WriteLog("CDNSClient::GetDNS GetNetworkParams failed!");
- free(pfixed_info);
- return -1;
- }
- if(ret ==ERROR_BUFFER_OVERFLOW)
- {
- pfixed_info =(FIXED_INFO *)realloc(pfixed_info, len);
- if((ret =GetNetworkParams(pfixed_info, &len)) <0)
- {
- WriteLog("CDNSClient::GetDNS GetNetworkParams failed!");
- free(pfixed_info);
- return -1;
- }
- }
- pIPAddr =&pfixed_info->DnsServerList;
- while(pIPAddr)
- {
- AddDnsServer(pIPAddr->IpAddress.String);
- pIPAddr =pIPAddr->Next;
- }
- if(pfixed_info->CurrentDnsServer)
- strcpy(m_dns_ip, pfixed_info->CurrentDnsServer->IpAddress.String);
- else strcpy(m_dns_ip, m_dns_servers[0]);
- free(pfixed_info);
- return 0;
- }
- int CDNSClient::FreeMxServers(void)
- {
- if(m_mx_servers ==NULL || m_mx_server_count <=0) return 0;
- for(int i =0; i<m_mx_server_count; i++)
- free(m_mx_servers[i]);
- free(m_mx_servers);
- m_mx_servers =NULL;
- m_mx_server_count =0;
- return 0;
- }
- // add a mx server to m_mx_servers
- int CDNSClient::AddMxServer(char * server_name)
- {
- if(m_mx_servers ==NULL)
- m_mx_servers =(char **)malloc(sizeof(char *));
- else m_mx_servers =(char **)realloc(m_mx_servers, sizeof(char *)*(m_mx_server_count+1));
- if(m_mx_servers ==NULL) return -1;
- m_mx_servers[m_mx_server_count] =(char *)malloc(64); // 域名最长64
- strcpy(m_mx_servers[m_mx_server_count], server_name);
- m_mx_server_count ++;
- return 0;
- }
- int CDNSClient::SkipString(char *buf, int pos, char *domain_name)
- {
- byte len;
- len =(char)buf[pos] & 0xFF;
- while(len !=0)
- {
- if(pos >512) return -1;
- pos ++;
- if((len & 0xc0) ==0xc0) // 压缩格式
- {
- int offset =((len & 0x3f) << 8) | (buf[pos++] & 0xff);
- SkipString(buf, offset, domain_name);
- return pos;
- }
- else
- {
- int len2 =(int)strlen(domain_name);
- strncat(domain_name, &buf[pos], len);
- domain_name[len2+len] =0;
- pos +=len;
- }
- if(buf[pos]) strcat(domain_name, ".");
- len =buf[pos];
- }
- return pos+1;
- }
- int CDNSClient::FreeDnsServers(void)
- {
- if(m_dns_servers ==NULL || m_dns_count <=0) return 0;
- for(int i =0; i<m_dns_count; i++)
- free(m_dns_servers[i]);
- free(m_dns_servers);
- m_dns_servers =NULL;
- m_dns_count =0;
- return 0;
- }
- int CDNSClient::AddDnsServer(char *dns_server)
- {
- if(m_dns_servers ==NULL)
- m_dns_servers =(char **)malloc(sizeof(char *));
- else m_dns_servers =(char **)realloc(m_dns_servers, sizeof(char *)*(m_dns_count+1));
- if(m_dns_servers ==NULL) return -1;
- m_dns_servers[m_dns_count] =(char *)malloc(20); // 域名最长64
- strcpy(m_dns_servers[m_dns_count], dns_server);
- m_dns_count ++;
- return 0;
- }