| iMatix home page
| << | < | > | >>
SFL Logo SFL
Version 2.11

 

get_name_server

#include "sflsock.h"
int
get_name_server (struct sockaddr_in *ns_address, int ns_max)

Synopsis

gets the addresses of the DNS servers defined in the TCP/IP configuration. The addresses are returned in a user-provided struct sockaddr_in array. The maximum number of addresses in this array is supplied as the ns_max argument. Return the number of address found.

Source Code - (sflsock.c)

{
    int
        ns_count = 0;                /*  Number of servers that we found  */

#if (defined (WIN32))
    static OSVERSIONINFO
        version_info;
    HKEY
        hkey;                           /*  Handle to returned reg. key      */
    static char
        registry_value [LINE_MAX + 1];  /*  DNS server info from registry    */
    long
        size = LINE_MAX;                /*  Max. size of returned value      */
    DWORD
        type;
    char
        *key,
        **address_list = NULL;
    int
        address_nbr;

    /*  Look in registry; this sometimes works, but not always               */
    version_info.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
    if (GetVersionEx (&version_info)
    &&  version_info.dwPlatformId == VER_PLATFORM_WIN32_NT)
        key = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters";
    else
        key = "SYSTEM\\CurrentControlSet\\Services\\Vxd\\Mstcp\\Parameters";

    if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, key, 0,
        KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS
    &&  RegQueryValueEx (hkey, "NameServer", NULL, (LPDWORD) &type,
        (LPBYTE) registry_value, (LPDWORD) &size) == ERROR_SUCCESS)
      {
        address_list = tok split (registry_value);
        for (address_nbr = 0; address_list [address_nbr]; address_nbr++)
          {
            if (ns_count >= ns_max)
                break;

            ns_address [ns_count].sin_family      = AF_INET;
            ns_address [ns_count].sin_port        = htons (DNS_PORT);
            ns_address [ns_count].sin_addr.s_addr =
                inet_addr (address_list [address_nbr]);
            ns_count++;
          }
        tok free (address_list);
        RegCloseKey (hkey);
      }

#elif (defined (__UNIX__))
    static char
        buffer  [LINE_MAX + 1],
        address [16];
    FILE
        *resolver;
    int
        rc;

    resolver = file open ("/etc/resolv.conf", 'r');
    if (resolver)
      {
        while (file read (resolver, buffer))
          {
            rc = sscanf (buffer, "nameserver %s", address);
            if (rc > 0 && rc != EOF)
              {
                if (ns_count >= ns_max)
                    break;

                ns_address [ns_count].sin_family      = AF_INET;
                ns_address [ns_count].sin_port        = htons (DNS_PORT);
                ns_address [ns_count].sin_addr.s_addr = inet_addr (address);
                ns_count++;
              }
          }
        file close (resolver);
      }

#elif (defined (__OS2__))
    static char
        buffer  [LINE_MAX + 1],
        address [16];
    char
        *etcenv   = NULL,
        *filename = NULL;
    FILE
        *resolver = NULL;
    int
        rc;

    /*  Under OS/2 the file controlling the resolver is stored in the        */
    /*  directory pointed at by the ETC environment variable.  It is called  */
    /*  resolv2 or resolv (I *think* that is the order of preference), so we */
    /*  try those two file names in that order.                              */

    /*  If the ETC environment variable is not set we try the /mptn/etc      */
    /*  directory since that is a likely default location for it.            */

    etcenv = getenv ("ETC");
    if (etcenv)
      {
        filename = mem_alloc (strlen(etcenv) + 10);
        if (!filename)
          return 0;                  /*  Cannot allocate memory for filename */

        strcpy (filename, etcenv);
        strcat (filename, "/resolv2");

        resolver = file open (filename, 'r');

        if (! resolver)
          { /*  Not available under that filename, let's try the other one   */
            strcpy (filename, etcenv);
            strcat (filename, "/resolv");

            resolver = file open (filename, 'r');
          }
        mem_free (filename);
      }
    else
      { /*  No environment variable around, try using the defaults           */
        resolver = file open ("/mptn/etc/resolv2", 'r');
        if (! resolver)
            resolver = file open ("/mptn/etc/resolv", 'r');
      }
    if (resolver)
      {
        while (file read (resolver, buffer))
          {
            rc = sscanf (buffer, "nameserver %s", address);
            if (rc > 0 && rc != EOF)
              {
                if (ns_count >= ns_max)
                    break;

                ns_address [ns_count].sin_family      = AF_INET;
                ns_address [ns_count].sin_port        = htons (DNS_PORT);
                ns_address [ns_count].sin_addr.s_addr = inet_addr (address);
                ns_count++;
              }
          }
        file close (resolver);
      }
#endif

    return (ns_count);
}

| << | < | > | >> iMatix Copyright © 1996-2000 iMatix Corporation