[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] DNS query på MX record.



On Sun, 24 Oct 1999, Carsten Svaneborg wrote:

> > SMTP VRFY i et forsøg på at checke om email adressen er korrekt, som jeg
> > lige kan tage med i morgen hvis du absolut insisterer :)
> 
> Hvis den forsøger at bruge MX så er jeg interesseret, men 
> min email code virker allerede, men kun hvis der kører en
> smtp server på den server der står i email addressen.
> f.x. virker sslug@sslug ikke fordi serveren risoe.dk netop
> ikke kører smtp, men et MX lookup vil vise at det er en anden
> server der tager sig af posten. (jeg kunne selvfølgeligt snage
> i sendmail's kode.. og se om jeg kunne lure den.) 

Her er den. Bemærk at koden er > 1 år gammel, og det er ikke mig der har
skrevet det :) Den er en del af et større program, så jeg har smidt alle
de headere som det program bruger ind, de er nok ikke alle nødvendige.

Selv bruger jeg det ikke længere til noget, jeg kalder bare sendmail ;)


#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <unistd.h>
#include <netinet/in.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <ctype.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <arpa/inet.h>

/* Mailhost lookup stuff

 * See RFCs 1034 and 1035 for details of these structures.
 */

/* blah.. we have to build our own structures here since nameser.h doesn't
 * have a decent RR structure
 */

struct rrecord {
    int16_t r_type;
    int16_t r_class;
    u_int32_t r_ttl;
    int16_t r_length;
};

char *find_mailhost(char *domain)
{
    static char out[1000];
    unsigned char query[1000];
    HEADER *h;
    unsigned char *p;
    int qcount, acount;
    char buf[1000];
    int best;
    struct rrecord *r;

    /* ask libc to do the query:
     * look up 'domain'
     * class C_IN (internet)
     * type  T_MX (mail exchanger record)
     * record results in query, max len 1000
     */
    if (res_search(domain, C_IN, T_MX, query, 1000) < 0)
	return domain;

    h = (HEADER *) query;

    /* skip over queries in packet */
    p = query + sizeof(HEADER);
    qcount = ntohs(h->qdcount);	/* # of queries present */
    acount = ntohs(h->ancount);	/* # of answers present */
    /* get past queries */
    while (qcount--) {
	p += dn_expand(query, query + 1000, p, buf, 1000);
	p += 4;			/* qclass, qtype */
    }

    /* now we are in the answers section. Just get the best exchanger,
     * we don't want to spend too much time messing around
     */
    best = 99999;
    out[0] = 0;
    while (acount--) {
	/* name that this answer refers to */
	p += dn_expand(query, query + 1000, p, buf, 1000);

	r = (struct rrecord *) p;
	if (htons(r->r_type) == T_MX) {		/* MX record */
	    int pref;
	    unsigned char *p1 = p + 10;

	    pref = ntohs(*(int16_t *) p1);	/* preference */
	    p1 += 2;
	    if (pref < best) {
		/* get exchanger name */
		dn_expand(query, query + 1000, p1, out, 1000);
		best = pref;
	    }
	}
	p = p + 10 + ntohs(r->r_length);	/* skip RR header and data */
    }

    if (out[0])
	return out;
    else
	return domain;
}


-- 
==============================================================================
Erwin Andreasen   Herlev, Denmark <sslug@sslug>          UNIX System Programmer
<URL:http://www.andreasen.org>              <*>              Goodbye, Richard!
==============================================================================



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:07 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *