使用socket获取dns的mx的有关问题

使用socket获取dns的mx的问题
怎样实现

------解决方案--------------------
/******************************************************************
*本文首发于bbs.bluegem.org的linux区
*本人email:chenfei@sohu.com
*如转载本文,请保留首发地和本人联络方式,以方便交流,谢谢!
******************************************************************/
/* 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 */