Go Back   UNIX Socket FAQ > Languages > C

Reply
 
Thread Tools Display Modes
  #1  
Old 04-14-2004, 02:42 AM
aukahi aukahi is offline
 
Join Date: Apr 2004
Location: Honolulu
Posts: 14
Default performing mx lookup with resolver

does any one know how to perform mx lookups with the resolver library?

i was reading the man pages on resolver, it doesn't contain any examples.
basically, this is what i want to do, a function to get the mx record of any given host name:

char *mx = getmxbyname("yahoo.com");
now, mx should be "mta01.yahoo.com".

knowing the mail preference would also be good.
Reply With Quote
  #2  
Old 04-14-2004, 02:03 PM
RobSeace RobSeace is offline
Administrator
 
Join Date: Jun 2002
Location: Boston, MA
Posts: 3,380
Default

Well, here's a very basic program that just prints the MX info for
given domains:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <resolv.h>

int main (int argc, char *argv[])
{
    u_char nsbuf[4096];
    char dispbuf[4096];
    ns_msg msg;
    ns_rr rr;
    int i, j, l;

    if (argc < 2) {
        printf ("Usage: %s <domain>[...]\n", argv[0]);
        exit (1);
    }

    for (i = 1; i < argc; i++) {
        l = res_query (argv[i], ns_c_any, ns_t_mx, nsbuf, sizeof (nsbuf));
        if (l < 0) {
            perror (argv[i]);
        } else {
#ifdef USE_PQUERY
/* this will give lots of detailed info on the request and reply */
            res_pquery (&_res, nsbuf, l, stdout);
#else
/* just grab the MX answer info */
            ns_initparse (nsbuf, l, &msg);
            printf ("%s :\n", argv[i]);
            l = ns_msg_count (msg, ns_s_an);
            for (j = 0; j < l; j++) {
                ns_parserr (&msg, ns_s_an, j, &rr);
                ns_sprintrr (&msg, &rr, NULL, NULL, dispbuf, sizeof (dispbuf));
                printf ("%s\n", dispbuf);
            }
#endif
        }
    }

    exit (0);
}
Now, if you just want to get at the bits of the data in a more
usable format, rather than a displayable format, you'll have to
basically do similar work that ns_sprintrr() does in parsing up the
RR message... I don't know right off the top of my head exactly
how to do that, but you could always take a look at the resolver
library code which is available from many places online... Or,
you could just parse up the displayable string to get at the data,
instead... *shrug*
Reply With Quote
  #3  
Old 04-15-2004, 05:13 AM
aukahi aukahi is offline
 
Join Date: Apr 2004
Location: Honolulu
Posts: 14
Default

thanks, works like a charm.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 11:47 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.