/* ** Copyright (c) 2018 D. Richard Hipp ** ** This program is free software; you can redistribute it and/or ** modify it under the terms of the Simplified BSD License (also ** known as the "2-Clause License" or "FreeBSD License".) ** ** This program is distributed in the hope that it will be useful, ** but without any warranty; without even the implied warranty of ** merchantability or fitness for a particular purpose. ** ** Author contact information: ** drh@hwaci.com ** http://www.hwaci.com/drh/ ** ******************************************************************************* ** ** Implementation of SMTP (Simple Mail Transport Protocol) according ** to RFC 5321. */ #include "config.h" #include "smtp.h" #include #if !defined(FOSSIL_OMIT_SMTP) #if defined(_WIN32) /* Don't yet know how to do this on windows */ #else # include # include # include # include #endif #endif /* !defined(FOSSIL_OMIT_SMTP) */ /* ** Find the hostname for receiving email for the domain given ** in zDomain. Return NULL if not found or not implemented. ** If multiple email receivers are advertized, pick the one with ** the lowest preference number. ** ** The returned string is obtained from fossil_malloc() ** and should be released using fossil_free(). */ char *smtp_mx_host(const char *zDomain){ #if !defined(_WIN32) && !defined(FOSSIL_OMIT_SMTP) int nDns; /* Length of the DNS reply */ int rc; /* Return code from various APIs */ int i; /* Loop counter */ int iBestPriority = 9999999; /* Best priority */ int nRec; /* Number of answers */ ns_msg h; /* DNS reply parser */ const unsigned char *pBest = 0; /* RDATA for the best answer */ unsigned char aDns[5000]; /* Raw DNS reply content */ char zHostname[5000]; /* Hostname for the MX */ nDns = res_query(zDomain, C_IN, T_MX, aDns, sizeof(aDns)); if( nDns<=0 ) return 0; res_init(); rc = ns_initparse(aDns,nDns,&h); if( rc ) return 0; nRec = ns_msg_count(h, ns_s_an); for(i=0; i2 ){ priority = p[0]*256 + p[1]; if( priority