免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1478 | 回复: 3
打印 上一主题 下一主题

[函数] 发表一个获取MX记录的函数支不支持呀 ^_^ [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-06-25 11:11 |只看该作者 |倒序浏览
/***************************************************************************
                          smtp.h  -  description
                             -------------------
    begin                : 2003.06.25
    copyright            : (C) 2003 by 小东(chinlone@163.com) GPL版权
    email                : chinlone@163.com
***************************************************************************/

/***************************************************************************
GPL软件授权.
函数返回值: -1(出错),否则为MX记录的条数
函数使用方法:将此头文件包含后
char *hosts[MAXMXHOSTS];
char *host; /* host name */
host = "163.com";
int n = getmxrr(host, hosts); /* 开始调用函数 */
if(n<0) printf("Can't get MX!" ;
/* 打印MX列表 */
   else
   {
     int i = 0;  
     while(i<n)
    {
      printf("%s\n",hosts);
      i++;
    }
  }                                                                                                
***************************************************************************/
#include <resolv.h>;
#define MAXMXHOSTS 20

int getmxrr(char *hostname, char *mxhosts[])
{
  static char *commonBufferPoint;
  char *index ();
  int n;                                /* utility index */
  HEADER *dhp;                        /* DNS reply header and fields*/
  int ancount;
  int qdcount;
  union                                 /* DNS reply and pointers */
   {
             HEADER hdr;
      u_char bytes[PACKETSZ]; /* hostnames buffer PACKETSZ = 512 */
    } answer;
   u_char *cp;
   u_char *eom;
   char hostbuf[PACKETSZ];                /* hostnames buffer PACKETSZ = 512 */
   int buflen;
   u_short pref;
   u_short type;
   u_short prefs[MAXMXHOSTS];
   u_long bestpref;
   int i;
  
  if (res_init() < 0)  return -1;
  n = res_search(hostname, C_IN, T_MX, answer.bytes, sizeof (answer));
  if (n < 0) return -1;
   /* find first answer */
    dhp = &answer.hdr;
    cp = answer.bytes + sizeof (HEADER);
    eom = answer.bytes + n;
    for (qdcount = ntohs (dhp->;qdcount); qdcount--; cp += n + QFIXEDSZ)
  /* Skip domain name, get class, and type. */
        if ((n = dn_skipname (cp, eom)) < 0) return -1;
  /* copy MX hosts and preferences into arrays */
  buflen = sizeof (hostbuf);
  commonBufferPoint = hostbuf;
  ancount = ntohs (dhp->;ancount);
  i = 0;
  while (--ancount >;= 0 && cp < eom && i < MAXMXHOSTS)
   {
    if ((n = dn_expand (&answer.hdr, eom, cp, commonBufferPoint, buflen)) < 0) return -1;
    /*  printf("dn_expand error:%s\n", hstrerror(errno)); */
      cp += n;
      GETSHORT (type, cp);
      cp += sizeof (u_short) + sizeof (u_long);
      GETSHORT (n, cp);
            if (type != T_MX)
            {
              cp += n;
              continue;
            }
    GETSHORT (pref, cp);
          if ((n = dn_expand (&answer.hdr, eom, cp, commonBufferPoint, buflen)) < 0) return -1;
          cp += n;
          prefs = pref;
    mxhosts = commonBufferPoint;
          i++;
    n = strlen (commonBufferPoint) + 1;
          commonBufferPoint += n;
          buflen -= n;
   }
  return i;
};

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2003-06-25 11:40 |只看该作者

发表一个获取MX记录的函数支不支持呀 ^_^

恩,我支持你一下。

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
3 [报告]
发表于 2003-06-25 11:40 |只看该作者

发表一个获取MX记录的函数支不支持呀 ^_^

好东西呀。最好用code方式存放代码。

论坛徽章:
0
4 [报告]
发表于 2003-06-25 12:37 |只看该作者

发表一个获取MX记录的函数支不支持呀 ^_^

对了,忘了说,因为用到了resolv库,所以编译的时候要加 -lresolv 参数编译,要不然可以通过编译但链接(link)的时候会出错
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP