|
On Wed, 24 Oct 2001, Murphy, Guy wrote: > > I've been tasked with checking the validity of email addresses we've > collected in our database. I need to programmically query the MX records on > a domain name server. Does anyone know how to do this from the iSeries.? > Could you point me to documentation on how to do this? Thanks. > Hmmm... the documentation isn't very clear on how to use the resolver functions, but they work just like the BSD ones, as far as I can tell... Warning: Don't make the mistake of thinking that you need an 'MX' record to deliver mail. If no 'MX' record exists, you should fall back to the 'A' record, instead!! Having said that -- here's some sample code (in RPG IV) that looks up the MX records for a host: H DFTACTGRP(*NO) ACTGRP(*NEW) BNDDIR('QC2LE') D GetMX PR 10I 0 D domain 256A const D max_entries 10I 0 value D host_array * value D pty_array * value D MAX_ENTRIES_ALLOWED... D C CONST(50) D C_ANY C CONST(255) D C_IN C CONST(1) D T_A C CONST(1) D T_NS C CONST(2) D T_CNAME C CONST(5) D T_SOA C CONST(6) D T_WKS C CONST(11) D T_PTR C CONST(12) D T_HINFO C CONST(13) D T_MX C CONST(15) D T_TXT C CONST(16) D T_ANY C CONST(255) D HFIXEDSZ C CONST(12) D QFIXEDSZ C CONST(4) D p_dns_HEADER S * D dns_HEADER DS based(p_dns_HEADER) D dns_id 5U 0 D dns_bitflds 2A D dns_qdcount 5U 0 D dns_ancount 5U 0 D dns_nscount 5U 0 D dns_arcount 5U 0 D res_search PR 10I 0 extproc('res_search') D domain_name * value options(*string) D class 10I 0 value D type 10I 0 value D answer_buf * value D ans_buf_len 10I 0 value D dn_skipname PR 10I 0 extproc('dn_skipname') D comp_dn * value D end_of_msg * value D dn_expand PR 10I 0 extproc('dn_expand') D start_dns_pkt * value D end_dns_pkt * value D comp_dn * value D expanded_dn * value D bufsize 10I 0 value D GetInt16 PR 5U 0 D ptr * D GetInt32 PR 10U 0 D ptr * D hosts S 256A dim(15) D pris S 5U 0 dim(15) D count S 10I 0 D x S 10I 0 d msg S 52A c *entry plist c parm peHost 32 c eval count = GetMX(peHost: 15: c %addr(hosts): %addr(pris)) c if count < 0 c eval msg = 'GetMX() returned failure.' c msg dsply c else c do count x c eval msg = 'host=' + %trim(hosts(x)) + c ', pri=' + %trim(%editc(pris(x):'P')) c msg dsply c enddo c endif c dsply pause 1 c eval *inlr = *on c return *++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * This uses the system's resolver routines to look up the MX * (mail exchanger) records for a host. * * domain = host to look up records for. * max_entries = max number of entries you can fit in your array * (should not exceed the amount defined by MAX_ENTRIES_ALLOWED) * host_array = pointer to an array of 256A variables that * will be used to return the MX hosts * pty_array = pointer to an array of 5U 0 variables that * contain the corresponding priority levels of the mail * exchangers. * * returns -1 upon failure, otherwise the number of MX hosts found. *++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ P GetMX B D GetMX PI 10I 0 D domain 256A const D max_entries 10I 0 value D host_array * value D pty_array * value D rc S 10I 0 D answer S 2048A D cp S * D end S * D namebuf S 256A D mxhost S 256A dim(50) d based(host_array) D mxpty S 5U 0 dim(50) d based(pty_array) D n S 10I 0 D entries S 10I 0 D type S 5U 0 D class S 5U 0 D ttl S 10U 0 D size S 5U 0 D pri S 5U 0 c if max_entries > 50 c return -1 c endif C************************************************************** c* Perform a DNS search for domain by MX record C* C* FIXME: lookup failure could be for a variety of reasons. C* we should be checking h_errno here! C************************************************************** c eval rc = res_search(%trim(domain): C_IN:T_MX: c %addr(answer): %size(answer)) c if rc < 0 c return -1 c endif C************************************************************** C* Parse the returned DNS record, and place entries into the C* mxpty and mxhost arrays C************************************************************** C* just to make sure we don't overflow our buffer if C* and invalid packet length is given to us: c if rc > %size(answer) c eval rc = %size(answer) c endif c* intialize pointers & misc c eval p_dns_HEADER = %addr(answer) c eval cp = %addr(answer) + HFIXEDSZ c eval end = %addr(answer) + rc c eval entries = 0 c* skip over the question records: c* c do dns_qdcount c if cp >= end c leave c endif c eval n = dn_skipname(cp: end) c if n < 0 c return -1 c endif c eval cp = cp + n + QFIXEDSZ c enddo C* each answer record in the buffer consists of (for MX): c do dns_ancount c if cp >= end c leave c endif c eval n = dn_expand(p_dns_HEADER: end: cp: c %addr(namebuf): %size(namebuf)) c eval cp = cp + n c eval type = GetInt16(cp) c eval class = GetInt16(cp) c eval ttl = GetInt32(cp) c eval size = GetInt16(cp) c if type <> T_MX c eval cp = cp + size c iter c endif c eval pri = GetInt16(cp) c eval n = dn_expand(p_dns_HEADER: end: cp: c %addr(namebuf): %size(namebuf)) c eval cp = cp + n c if entries < MAX_ENTRIES c eval entries = entries + 1 c eval mxpty(entries) = pri c eval mxhost(entries) = %str(%addr(namebuf)) c endif c enddo c return entries P E *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * This gets a short int from a DNS record and advances the * pointer. *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ P GetInt16 B D GetInt16 PI 5U 0 D ptr * D p_short S * D value S 5U 0 based(p_short) c eval p_short = ptr c eval ptr = ptr + %size(value) c return value P E *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * This gets an int from a DNS record and advances the * pointer. *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ P GetInt32 B D GetInt32 PI 10U 0 D ptr * D p_int S * D value S 10U 0 based(p_int) c eval p_int = ptr c eval ptr = ptr + %size(value) c return value P E when you need to look up the 'A' record for the host, I suggest using gethostbyname() since it's a lot easier to work with than the resolver functions. You can find info about gethostbyname in my sockets tutorial: http://klement.dstorm.net/rpg/socktut/ Hope this helps!
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.