Advertisment

Doing an MX Lookup 

author-image
PCQ Bureau
New Update

The coming of the Internet has made e-mail an integral part of our lives. In this article, we’ll see how MX (mail exchanger) servers help in delivering the e-mail to the recipient’s e-mail server. 

Advertisment

How is an e-mail sent?

There are two ways to send an e-mail–logging on to a Web-based e-mail service or using an e-mail client software like Microsoft Outlook.

The diagram below gives a general idea as to how an e-mail is sent:

The entire procedure starts off with the user composing the e-mail, either on a Web-based interface or using his e-mail client software, and then pressing the send button. The Web e-mail service or the e-mail client software then requests the OS’s socket environment to resolve the name of the configured SMTP (outgoing mail) server to an IP address. The sockets environment, in turn, queries the configured DNS server to get the address, which is then returned to the Web e-mail service or the e-mail client software.

Advertisment

Next, a connection is made by the e-mail client at the TCP port 25 of the SMTP server, which is always “listening” for incoming client requests. Upon establishing connection, the mail is transferred to the SMTP server via the SMTP protocol. Once the e-mail is transferred, the client closes the connection. A thread on the SMTP server, called the MTA (Mail Transfer Agent), comes into action that has the job of delivering the e-mail to their respective e-mail server. Now, an e-mail address (for example, gaurav@wintools.f2s.com) is divided into two portions: the username gaurav and the domain name wintools.f2s.com. The MTA checks if the domain name of the recipient’s e-mail address is registered with its own SMTP server. If so, the e-mail is considered local, and after it ensures that a user with the specified username holds an e-mail account in that domain on the SMTP server, the mail is delivered to the user’s Inbox. If, however, no such user holds an account on the server, the MTA bounces the mail back to the sender.

If the domain name of the e-mail address is not registered with the MTA’s SMTP server, the mail is then considered to be remote. The MTA then proceeds to query the DNS server(s) for MX records of the remote e-mail domain. Actually, when a domain is to be registered as an e-mail domain within the DNS database, a special record called the MX record is also added to the DNS database. This record holds the name or the IP address of the server responsible for accepting e-mail for the concerned domain. There multiple MX records for a given e-mail domain in the DNS database, and each record, along with the MX server name or IP address, also contain the preference value for the record. The lower the preference value, the higher the priority of the MX server. The idea is to deliver the e-mail to the MX servers of a remote e-mail domain in the decreasing order of their priority.

Advertisment

The MX servers speak SMTP so that MTA can transfer the mail to them, and may be a system separate from the actual e-mail server of the remote e-mail domain. Once the mail reaches the MX server, it is transferred to the remote e-mail domain’s SMTP server, where it is checked if the specified user holds an account or not. If he does, the mail is saved to his inbox, but if he doesn’t, the mail is bounced back to the sender. 

Thus, it’s the MX server that is responsible for accepting e-mail for their respective e-mail domains, and without them, one cannot think of sending an e-mail outside their own domain like yahoo.com or
hotmail.com.

Introducing CMXLookup

We’ll now write a C++ class, CMXLookup that serves the job of enumerating MX servers for a given e-mail domain, along with their reference values, and also sort them out in the decreasing order of their priority. The class declaration of CMXLookup is as follows: 

Advertisment

// class declaration

class CMXLookup

{

// private data

char QueryBuf<512>;

char DNSHostname<512>;

char MXDomain<512>;

SOCKET soc;

bool DidLookup;

char MXServer<100>;

int MXCount, MXPref;



// private methods

u_long GetHostAddress(LPSTR hostname);

bool SetMXServerDetails(struct MXInfo& rInfo, int MXIndex); 

public:

CMXLookup(char *DNSServer);

bool Lookup(char *Domain);

bool ParseReplyMX(void);

int GetMXCount(void) const;

bool GetMXServerDetails(struct MXInfo& rInfo, int MXIndex) const;

void Sort(void);

};

MAX defines the MAX number of MX entries to be enumerated, and has been set to 50. The structure, MXInfo, is defined as shown below:

struct MXInfo

{

char MXServer<100>;

int MXPref;

};

Advertisment

It stores the name/IP address of the MX server, along with its preference value. The best way to understand the working of the class is to start right from the scratch, that is, instantiating an object of CMXLookup. 

CMXLookup has only one constructor, that is parameterized and takes the name or IP address of the DNS server to which it will query for MX records. The constructor does the following jobs:

  1. Initializes the internal buffer for DNS query
  2. Copies the passed in DNS server name/IP address to an internal variable
  3. Initializes the MX e-mail domain name to be queried for
  4. Initializes the number of MX servers found to zero
  5. Initializes the flag which indicates whether or not, a DNS query has taken place.
Advertisment

The next logical step is to call the ‘Lookup’ method of the class, which does the main job of querying the DNS server for the MX records, by passing it the name of the e-mail domain whose MX records are to be enumerated. The method also prepares the query for the DNS server, understanding the structure of which is important.

Next month, we’ll see the implementation (working) of the methods declared in the class CMXLookup, which accomplish the task.

Kumar Gaurav Khanna runs www.wintoolzone.com

Advertisment

Stay connected with us through our social media channels for the latest updates and news!

Follow us: