Name a valid email – The Daily WTF

Properly validating an email address, according to the specification, is actually surprisingly difficult. This is partly because there are many features in an email address that no one ever uses. Even so, you can easily get close enough with a basic regular expression, which is what most people do.

Niels Hthe predecessor wanted to go a little further. They wanted to ensure that the submitted domain name had a valid DNS entry. Excessively? Probably. But luckily for them, the .NET framework offers practical help System.Net.Dns.GetHostEntry method that resolves the domain name. At least that makes the problem easy to solve.

This makes troubleshooting easier if you use it. But why use a built-in method when you can do it yourself?

private bool IsValidDomain(string EmailAddress)

  List<byte> ByteList = new List<byte>();
  string DNS = GetDnsAddress();
  if (DNS == "") 
    throw new Exception("No DNS is found.");
  
  if (EmailAddress.Contains("@")) 
    try 
      var HostName = EmailAddress.Split('@')[1];
      ByteList.AddRange(new byte[]  88, 89, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0 );
      var UDPC = new UdpClient(DNS, 53);
      foreach (string S in HostName.Split('.')) 
        ByteList.Add(Convert.ToByte(S.Length));
        char[] chars = S.ToCharArray();
        foreach (char c in chars)
          ByteList.Add(Convert.ToByte(Convert.ToInt32(c)));
      

      ByteList.AddRange(new byte[]  0, 0, Convert.ToByte("15"), 0, 1 );
      byte[] Req = new byte[ByteList.Count];
      for (int i = 0; i < ByteList.Count; i++)
        Req[i] = ByteList[i];
      UDPC.Send(Req, Req.Length);
      IPEndPoint ep = null;
      byte[] recv = UDPC.Receive(ref ep);
      UDPC.Close();
      var Resp = new int[recv.Length];
      for (int i = 0; i < Resp.Length; i++)
        Resp[i] = Convert.ToInt32(recv[i]);
      int status = Resp[3];
      if (status != 128) 
        return false;
      
      int answers = Resp[7];
      if (answers == 0) 
        return false;
      
     catch 
      return false;
    
   else 
    return false;
  
  return true;

immediately, IsValidDomain is a poorly named and poorly designed function. Takes a string containing the email address and returns true or false if email address contains a valid domain. I’d argue that a cleaner design would be to take part of the domain name as input, and have some other function take care of the email address separation.

But that’s missing the forest for the trees, isn’t it. This function has it all. It starts by manually setting the byte structure of the DNS request packet. Then repeating the string to convert each character to a byte. And since we do it at List structure, we have to convert it to a string, but we can’t use it ToArray function, so we manually copy each byte into the field.

We then send and receive a UDP packet for our DNS request. Note that we are creating a variable to hold IPEndPointreturn value of GetHostEntrywhich points to that could be at some point someone thought about using a built-in function.

But after getting our bytes back from the UDP packet, we copy the bytes into an array of 32-bit integers, simply incrementing each byte to 32-bits. Why? I have no idea. We still treat the array as bytes, moving at certain offsets to detect whether our request succeeded or not.

As a final note, I’m not one to dwell on “coming back from just one post!” type rules, I think multiple returns are fine and can be cleaner to read in many cases. But this one seems annoyingly complex to keep track of, since we’re not just returning from multiple locations, but from multiple levels of blocks.

Finally, today I learned something about the structure of DNS packets.

[Advertisement]

Continuously monitor your servers for configuration changes and report if configuration drift occurs. Get started with Otter today!

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *