DNS is recursive process. It works something like this:
  • You go to your webbrowser and type in http://www.everything2.org/
  • Your webbrower queries the name server listed in your /etc/resolv.conf (if you're using a Unix-alike) or equivalent in accordance with you OS.
  • (Note: the above is if you're using a webbrowser to access a host, vary for whichever protocol you're using)
  • The name server will look in it's cache to see if it already knows the IP address of the host you wish to contact. If it does know the address, it will return that to the client (your webbrowser) immediately.
  • If it does not have the address cached, it will contact the "." nameserver (root nameserver). It will then ask the root nameserver where it can find out about .org. addresses. Root nameserver then says, "here's the IP of the .org. server, go look there".
  • Our nameserver then goes off to the IP that root told it of and asks "where can i find out about everything2.org. addresses?". .org. then says, "here's the IP of their nameserver, they're the one's with your info, ask there".
  • So the nameserver goes off and asks everything2.org's nameserver, "you seem to be authoritative for everything2.org, what's the IP address of www.everything2.org?"
  • At this point, all being well, that nameserver will return the IP address to www.everything2.org to our local nameserver, which will then return that IP to our webbrowser.
  • At which point our browser will go and connect to port 80 (the standard port for http transfers) and ask for whatever info you need... At this point E2 will hopefully return to use with the wonderful "Welcome to Everything" node!

That's about it. Of course, it could be the case that at any point along there, they already have the IP address of one of those servers cached and it will ask them directly (skipping straight over to ".org." instead of asking "." directly)

Thank you class, your lesson for the day is over. If you have any queries, or feel I have made any mistakes, please /msg me...

DNS is an acronym for Domain Name Server. This server maps IP addresses to host names. This allows computers to be accessed remotely by name instead of number.

When you type in "www.everything2.com" into your web browser, your computer first checks to see if it knows the IP Address (usually in memory, or on the computers DNS server). If it doesn't (usually), then it makes a request to the local DNS on your network. This server then replies with the IP Address of the computer you are looking for.

If your local DNS doesn't have the IP Address for "www" at "everything2.com", it will make a request to the DNS at "everything2.com", which will give it the IP Address of "www", which will then be forwarded to you. Happy!

The process of resolving a hostname to an IP address is as follows:

  • Depending on system and configuration, when a hostname is entered the system will check local files to see if it has a name to IP address mapping for that host name. If found, this is treated as authoritative even though it may be incorrect, out of date or otherwise.

  • If this fails, and DNS resolution is enabled the system consults the first nameserver it is configured to use and sends a query to that nameserver.

  • If the server being queried has authoritative data for that hostname, it returns it. In other words, if that system holds local files listing information about the domain and is configured to believe that it holds the definitive result, it will return it. Again this does not imply the correct result.

  • If the server being queried does not hold the definitive result and it is configured to forward queries to other nameservers it will consult the list of root nameservers

    .

  • Starting from the rightmost atom, the query will be broken down by finding the definitive system that holds data for that atom. So for example to resolve www.microsoft.com the root nameservers are asked "which system holds authoritative data for ".com"? The query for "which system holds authoritative data for "MICROSOFT.com"? is then forwarded to that system, and so on until system holding the definitive information is reached.

The process of hostname resolution can seem quite stupid. IF for example you configure your system to "search" mycompany.com and you want to visit www.microsoft.com, it will first search for authoritative data for www.microsoft.com.mycompany.com before it looks for www.microsoft.com. This is designed to accomodate lazy people who like to type things like www into their browser window and have the company's webpage pop up immediately.

Tip: To see which nameserver is authoritative for a certain domain from a UNIX system, you can type the command:

dig soa somedomain.com

(Of course assuming that the 'dig' command, internet access, visibility through firewalls are all enabled)

Otherwise you consult the website for your favorite internet registry for the information.

Taken from the original at http://www.ecst.csuchico.edu/~beej/guide/net/ ... see end of writeup for Copyright statement.

DNS--You say "whitehouse.gov", I say "198.137.240.100"

In case you don't know what DNS is, it stands for "Domain Name Service". In a nutshell, you tell it what the human-readable address is for a site, and it'll give you the IP address (so you can use it with bind(), connect(), sendto(), or whatever you need it for.) This way, when someone enters:

$ telnet whitehouse.gov

telnet can find out that it needs to connect() to "198.137.240.100".

But how does it work? You'll be using the function gethostbyname():

#include <netdb.h>
    
struct hostent *gethostbyname(const char *name);

As you see, it returns a pointer to a struct hostent, the layout of which is as follows:

struct hostent {
    char    *h_name;
    char    **h_aliases;
    int     h_addrtype;
    int     h_length;
    char    **h_addr_list;
};
#define h_addr h_addr_list[0]

And here are the descriptions of the fields in the struct hostent:

  • h_name - Official name of the host.
  • h_aliases - A NULL-terminated array of alternate names for the host.
  • h_addrtype - The type of address being returned; usually AF_INET.
  • h_length - The length of the address in bytes.
  • h_addr_list - A zero-terminated array of network addresses for the host. Host addresses are in Network Byte Order.
  • h_addr - The first address in h_addr_list.

gethostbyname() returns a pointer to the filled struct hostent, or NULL on error. (But errno is not set -- h_errno is set instead. See herror(), below.)

But how is it used? Sometimes (as we find from reading computer manuals), just spewing the information at the reader is not enough. This function is certainly easier to use than it looks.

Here's an example program:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>

int main(int argc, char *argv[])
{
  struct hostent *h;

  if (argc != 2) {  /* error check the command line */
    fprintf(stderr, "usage: getip address\n");
    exit(1);
  }

  if ((h=gethostbyname(argv[1])) == NULL) {  /* get the host info */
    herror("gethostbyname");
    exit(1);
  }

  printf("Host name  : %s\n", h->h_name);
  printf("IP Address : %s\n",inet_ntoa(*((struct in_addr *)h->h_addr)));

  return 0;
}

With gethostbyname(), you can't use perror() to print error message (since errno is not used). Instead, call herror().

It's pretty straightforward. You simply pass the string that contains the machine name ("whitehouse.gov") to gethostbyname(), and then grab the information out of the returned struct hostent.

The only possible weirdness might be in the printing of the IP address, above. h->h_addr is a char *, but inet_ntoa() wants a struct in_addr passed to it. So I cast h->h_addr to a struct in_addr *, then dereference it to get at the data.


Prev | Up | Next


Copyright © 1995, 1996 by Brian "Beej" Hall. This guide may be reprinted in any medium provided that its content is not altered, it is presented in its entirety, and this copyright notice remains intact. Contact beej@ecst.csuchico.edu for more information.

Log in or register to write something here or to contact authors.