博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

专注于linux技术 心无旁骛

潜心学习linux,每天进步一点点
  diymyunix.cublog.cn

关于作者
姓名:QQCC
职业:IITT
年龄:8822
位置:WuHan
联系方式:
Email: justforfan528@gmail.com
个性签名:免费劳动力
|| << >> ||
我的分类


linux下c语言编程获得mx记录
 
原文如下:
 

/******************************************************************

*本文首发于bbs.bluegem.orglinux

*本人emailchenfei@sohu.com

*如转载本文,请保留首发地和本人联络方式,以方便交流,谢谢!

******************************************************************/  

一.     需求分析:

Linux下用c语言编程实现获得某个域的mx记录。

二.     编程所需键函数:

res_search等,请参考man page

三.     实现:

http://user.pa.net/~hallerp/thesis/msmtpd-1.0.0/src/dns.c

这段代码也是一个项目的实现,功能单一。以字符串形式按响应顺序返回所有mx记录的主机,以“:”分割。Postfixsendmailnslookup的源码可读性均没有这个好。

四.     参考资料:

linux man page

linux DNS Server管理指南》电子工业出版社。

postfix源码。

nslookup命令源码。nslookup命令属于bind-utils-9.1.3-4

Sendmail源码。

http://user.pa.net/~hallerp/thesis/msmtpd-1.0.0/src/dns.c


/* dns.c
 *
 * char *getmxbyname(char *domain) - gets the DNS MX records for host/domain char *domain
 * it returns a colon delimited list of valid MXs.
 */



#include <msmtpd.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <signal.h>
#include <setjmp.h>

#include <sys/types.h> /* not always automatically included */
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include <netdb.h>
#undef NOERROR /* in <sys/streams.h> on solaris 2.x */
#include <arpa/nameser.h>
#include <resolv.h>

 
/*
 * Copyright (c) 1983 Eric P. Allman
 * Copyright (c) 1988 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted provided
 * that: (1) source distributions retain this entire copyright notice and
 * comment, and (2) distributions including binaries display the following
 * acknowledgement: ``This product includes software developed by the
 * University of California, Berkeley and its contributors'' in the
 * documentation or other materials provided with the distribution and in
 * all advertising materials mentioning features or use of this software.
 * Neither the name of the University nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */


/*silly hackery */

#if defined(BIND_493)
typedef u_char qbuf_t;
#else
typedef char qbuf_t;
#endif

#if defined(BIND_493)
typedef char nbuf_t;
#else
typedef u_char nbuf_t;
#endif
           

 


/**/
/*
** GETMXBYNAME -- Fetch mx hosts for a domain
** ------------------------------------------
**
** Returns:
** Number of mx hosts found.
**
** Outputs:
** The contains the mx names.
*/


char *getmxbyname(char *domain) /*find mxs for this domain*/
{
 //int verbose = 0;


 //int debug = 0;



 /* #define HFIXEDSZ sizeof(HEADER) actually 12 */
 #define MAXPACKET 8192 /* max size of packet */
 //#define MAXMXHOSTS 20 /* max num of mx records we want to see */


 enum { MAXMXHOSTS = 20 };
 //char _arr[MAXBUF][MAXBUF];


 enum { MAXMXBUFSIZ = (MAXMXHOSTS * (MAXBUF+1)) };

typedef union {
 HEADER hdr;
 u_char buf[MAXPACKET];
} querybuf;

 static char hostbuf[MAXMXBUFSIZ];

 char *MxHosts[MAXMXHOSTS];
 querybuf answer; /* answer buffer from nameserver */
 HEADER *hp; /* answer buffer header */
 int ancount, qdcount; /* answer count and query count */
 u_char *msg, *eom, *cp; /* answer buffer positions */
 int type, class, dlen; /* record type, class and length */
 u_short pref; /* mx preference value */
 u_short prefer[MAXMXHOSTS]; /* saved preferences of mx records */
 char *bp; /* hostbuf pointer */
 int nmx; /* number of mx hosts found */
 register int i;
 register int j;
 register int n;

 char *str; /* final answer string buffer. */
 str = xmalloc(MAXBUF);

/*
 * Query the nameserver to retrieve mx records for the given domain.
 */

 errno = 0; /* reset before querying nameserver */
 h_errno = 0;

 n = res_search(domain, C_IN, T_MX, (u_char *)&answer, sizeof(answer));
 if (n < 0)
 {
  if (_res.options & RES_DEBUG)
   debug("sres_search failed\n");
  return(0);
 }

 errno = 0; /* reset after we got an answer */

 if (n < HFIXEDSZ)
 {
  h_errno = NO_RECOVERY;
  return(0);
 }

 /* avoid problems after truncation in tcp packets */
 if (n > sizeof(answer))
  n = sizeof(answer);

/*
 * Valid answer received. Skip the query record.
 */

 hp = (HEADER *)&answer;
 qdcount = ntohs((u_short)hp->qdcount);
 ancount = ntohs((u_short)hp->ancount);

 msg = (u_char *)&answer;
 eom = (u_char *)&answer + n;
 cp = (u_char *)&answer + HFIXEDSZ;

 while (qdcount-- > 0 && cp < eom)
 {
  n = dn_skipname(cp, eom);
  if (n < 0)
   return(0);
  cp += n;
  cp += QFIXEDSZ;
 }

/*
 * Loop through the answer buffer and extract mx records.
 */

 nmx = 0;
 bp = hostbuf;

 while (ancount-- > 0 && cp < eom && nmx < MAXMXHOSTS)
 {
  //if (verbose >= 4 || debug)


  // (void) p_rr((qbuf_t *)cp, (qbuf_t *)msg, stdout);



  n = dn_expand(msg, eom, cp, (nbuf_t *)bp, MAXBUF);
  if (n < 0)
   break;
  cp += n;

  type = _getshort(cp);
   cp += INT16SZ;

  class = _getshort(cp);
   cp += INT16SZ;

  /* ttl = _getlong(cp); */
   cp += INT32SZ;

  dlen = _getshort(cp);
  cp += INT16SZ;

  if (type != T_MX || class != C_IN)
  {
   cp += dlen;
   continue;
  }

  pref = _getshort(cp);
  cp += INT16SZ;

  n = dn_expand(msg, eom, cp, (nbuf_t *)bp, MAXBUF);
  if (n < 0)
   break;
  cp += n;

  prefer[nmx] = pref;
  MxHosts[nmx] = bp;
  nmx++;

  //n = strlength(bp) + 1;


  n = strlen(bp) + 1;
  bp += n;
 }

/*
 * Sort all records by preference.
 */

 for (i = 0; i < nmx; i++)
 {
  for (j = i + 1; j < nmx; j++)
  {
   if (prefer[i] > prefer[j])
   {
    register u_short tmppref;
    register char *tmphost;

    tmppref = prefer[i];
    prefer[i] = prefer[j];
    prefer[j] = tmppref;

    tmphost = MxHosts[i];
    MxHosts[i] = MxHosts[j];
    MxHosts[j] = tmphost;
   }
  }
 }

 for (i = 0; i< nmx; i++){
                strcat(str, MxHosts[i]);
                strcat(str, ":");
 }
      
 return(str);

}

 TAG linux mx纪录 dns
发表于: 2007-10-15,修改于: 2007-10-15 15:06,已浏览279次,有评论0条 推荐 投诉


网友评论
 发表评论