home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / xfe-dns.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  12.5 KB  |  498 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. /* 
  19.  * xfe_dns.c --- hooking X Mozilla into the portable nonblocking DNS code.
  20.  * Created: Jamie Zawinski <jwz@netscape.com>, 24-Dec-96.
  21.  */
  22.  
  23.  
  24. #include "unix-dns.h"
  25. #include "xfe-dns.h"
  26.  
  27. /* Comment this out to turn off asynchronous dns lookup. */
  28. /* #define UNIX_ASYNC_DNS */
  29.  
  30.  
  31. extern int MK_OUT_OF_MEMORY;
  32.  
  33. static int dns_socket = -1;
  34. static XtInputId dns_xt_id = 0;
  35.  
  36. #ifdef PROC1_DEBUG_PRINT
  37. # define LOG(PREFIX,SMPRINTF_ARGS) \
  38.     do { \
  39.       char *s; \
  40.       fprintf(stderr, "\tproc1 (%d): %s: ", getpid(), PREFIX); \
  41.       s = PR_smprintf SMPRINTF_ARGS; \
  42.       fprintf(stderr, "%s", s); \
  43.       free(s); \
  44.     } while(0)
  45. #else  /* !PROC1_DEBUG_PRINT */
  46. # define LOG(PREFIX,SMPRINTF_ARGS) do {} while(0)
  47. #endif /* !PROC1_DEBUG_PRINT */
  48.  
  49.  
  50. void
  51. XFE_InitDNS_Early(int argc, char **argv)
  52. {
  53. int sock;
  54. #ifdef UNIX_ASYNC_DNS
  55.   static XP_Bool done = FALSE;
  56.  
  57.   LOG("XFE_InitDNS_Early", ("calling DNS_SpawnProcess.\n"));
  58.   sock = DNS_SpawnProcess(argc, argv);
  59.  
  60.   XP_ASSERT(!done);
  61.   if (done) return;
  62.   done = TRUE;
  63.  
  64.   dns_socket = sock;
  65.   if (dns_socket <= 0)
  66.     {
  67.       fprintf(stderr,
  68.           "%s: dns helper process initialization failed:\n"
  69.           "\thost name lookups will be synchronous (blocking.)\n",
  70.           argv[0]);
  71.       /* #### better error message? */
  72.       dns_socket = -1;
  73.     }
  74. #else  /*! UNIX_ASYNC_DNS */
  75.   /* If the process never gets spawned, and sock never gets set, then
  76.      the rest of this code will be a no-op (and FE_StartAsyncDNSLookup()
  77.      will call gethostbyname() in the foreground.)
  78.    */
  79.   sock = -1;
  80. #endif /*! UNIX_ASYNC_DNS */
  81. }
  82.  
  83. static void
  84. dns_xt_callback (void *closure, int *source, XtInputId *id)
  85. {
  86.   int status = 0;
  87.   XP_ASSERT(*id == dns_xt_id);
  88.   XP_ASSERT(*source == dns_socket);
  89.  
  90.   LOG("dns_xt_callback", ("calling DNS_ServiceProcess(fd=%d)\n", *source));
  91.  
  92.   status = DNS_ServiceProcess(*source);
  93.  
  94.   if (status < 0)
  95.     {
  96.       LOG("dns_xt_callback",
  97.       ("DNS_ServiceProcess returned %d -- shutting down\n", status));
  98.       XtRemoveInput(dns_xt_id);
  99.       dns_xt_id = 0;
  100.       close(dns_socket);
  101.       dns_socket = -1;
  102.     }
  103. }
  104.  
  105.  
  106. void
  107. XFE_InitDNS_Late(XtAppContext app)
  108. {
  109.   static XP_Bool done = FALSE;
  110.   XP_ASSERT(!done);
  111.   if (done) return;
  112.   done = TRUE;
  113.  
  114.   if (dns_socket <= 0) return;
  115.  
  116.   dns_xt_id = XtAppAddInput (app, dns_socket,
  117.                 (XtPointer) (XtInputReadMask|XtInputExceptMask),
  118.                 dns_xt_callback, 0);
  119.   XP_ASSERT(dns_xt_id);
  120.   if (!dns_xt_id) return;
  121.  
  122.   LOG("XFE_InitDNS_Late", ("added input to Xt.\n"));
  123. }
  124.  
  125.  
  126. typedef struct x_pending_dns_lookup {
  127.   void *id;
  128.   char *name;
  129.   int socket;
  130.  
  131.   XP_Bool done;
  132.   int status;
  133.   unsigned char result[4];
  134.  
  135.   struct x_pending_dns_lookup *next, *prev;
  136. } x_pending_dns_lookup;
  137.  
  138. static x_pending_dns_lookup *queue = 0;
  139.  
  140.  
  141. #ifdef PROC1_DEBUG_PRINT
  142. static int
  143. x_pending_dns_queue_length(void)
  144. {
  145.   int i;
  146.   x_pending_dns_lookup *o;
  147.   for (i = 0, o = queue; o; o = o->next)
  148.     i++;
  149.   return i;
  150. }
  151. #endif /* PROC1_DEBUG_PRINT */
  152.  
  153.  
  154. static x_pending_dns_lookup *
  155. new_lookup (const char *name, int socket)
  156. {
  157.   x_pending_dns_lookup *obj;
  158.   XP_ASSERT(name);
  159.   if (!name) return 0;
  160.  
  161.   XP_ASSERT(socket > 0);
  162.  
  163.   obj = (x_pending_dns_lookup *) malloc(sizeof(*obj));
  164.   if (!obj) return 0;
  165.   memset(obj, 0, sizeof(*obj));
  166.   obj->name = strdup(name);
  167.   obj->done = FALSE;
  168.   obj->socket = socket;
  169.  
  170.   XP_ASSERT(!queue || queue->prev == 0);
  171.   obj->next = queue;
  172.   if (queue)
  173.     queue->prev = obj;
  174.   queue = obj;
  175.  
  176.   LOG("XFE new_lookup", ("linked in \"%s\", sock %d; ql=%d\n",
  177.              name, socket, x_pending_dns_queue_length()));
  178.  
  179.   return obj;
  180. }
  181.  
  182. static int
  183. free_lookup (x_pending_dns_lookup *obj)
  184. {
  185.   XP_ASSERT(obj);
  186.   if (!obj) return -1;
  187.  
  188.   if (obj->prev)
  189.     {
  190.       XP_ASSERT(obj->prev->next != obj->next);
  191.       obj->prev->next = obj->next;
  192.     }
  193.   if (obj->next)
  194.     {
  195.       XP_ASSERT(obj->next->prev != obj->prev);
  196.       obj->next->prev = obj->prev;
  197.     }
  198.   if (queue == obj)
  199.     queue = obj->next;
  200.  
  201.   LOG("XFE free_lookup", ("unlinked \"%s\", sock %d, id %d; ql=%d\n",
  202.               (obj->name ? obj->name : "(null)"),
  203.               obj->socket, (long) obj->id,
  204.               x_pending_dns_queue_length()));
  205.  
  206.   if (obj->name) free (obj->name);
  207.   free(obj);
  208.   return 0;
  209. }
  210.  
  211.  
  212. static int
  213. xfe_dns_done_cb (void *id, void *closure,
  214.          int status, const char *result)
  215. {
  216.   /* status > 0 means "success"
  217.      status == 0 means "aborted"
  218.      status < 0 means "error".
  219.    */
  220.  
  221.   x_pending_dns_lookup *obj = (x_pending_dns_lookup *) closure;
  222.   XP_ASSERT(obj->id == id);
  223.  
  224.   LOG("XFE dns_done_cb (Bug #87736)", ("status = %d, result = \"%s\"\n",
  225.                                        status, result));
  226.  
  227.   obj->done = TRUE;
  228.   obj->status = status;
  229.   if (status > 0)
  230.     {
  231.       unsigned char *ip = (unsigned char *) result;
  232.       obj->result[0] = ip[0];
  233.       obj->result[1] = ip[1];
  234.       obj->result[2] = ip[2];
  235.       obj->result[3] = ip[3];
  236.     }
  237.  
  238.   XP_ASSERT(obj->socket > 0);
  239.  
  240.  
  241.   LOG("XFE dns_done_cb",
  242.       ("done with \"%s\"\n\t\t\tsock %d; id %d; ip=%d.%d.%d.%d; ql=%d\n",
  243.        obj->name, obj->socket, (long) obj->id,
  244.        obj->result[0], obj->result[1],
  245.        obj->result[2], obj->result[3],
  246.        x_pending_dns_queue_length()));
  247.  
  248.  
  249.   /* This tells netlib that we're ready, and it should sink back down into
  250.      whatever state machine is on this socket, and cause it to re-call
  251.      FE_StartAsyncDNSLookup().  Since the object is now marked as `done',
  252.      that will return a hostent, and then unlink and free the object.
  253.  
  254.      (Don't call it if status == 0, because that means that we've been
  255.      called underneath FE_ClearDNSSelect/DNS_AbortHostLookup, and are
  256.      thus already in netlib.)
  257.    */
  258. #ifdef NSPR20_DISABLED
  259.   if (status != 0)
  260.     NET_ProcessNet (obj->socket, NET_SOCKET_FD);
  261. #endif 
  262.  
  263.   return 0;
  264. }
  265.  
  266.  
  267. static struct hostent *
  268. make_hostent(const char *name, const unsigned char ip[4])
  269. {
  270.   static struct hostent reused_hostent = { 0, };
  271.   static char *reused_addr_list[2] = { 0, };
  272.   static char reused_addr[5] = { 0, };
  273.  
  274.   reused_hostent.h_name = (char *) name;
  275.   reused_hostent.h_aliases = 0;
  276.   reused_hostent.h_addrtype = AF_INET;
  277.   reused_hostent.h_length = 4;
  278.   reused_hostent.h_addr_list = reused_addr_list;
  279.   reused_hostent.h_addr_list[0] = reused_addr;
  280.   reused_hostent.h_addr_list[0][0] = ip[0];
  281.   reused_hostent.h_addr_list[0][1] = ip[1];
  282.   reused_hostent.h_addr_list[0][2] = ip[2];
  283.   reused_hostent.h_addr_list[0][3] = ip[3];
  284.   reused_hostent.h_addr_list[1] = 0;
  285.  
  286.   return &reused_hostent;
  287. }
  288.  
  289. int
  290. FE_StartAsyncDNSLookup(MWContext *context,
  291.                char *host_and_port,
  292.                void **hoststruct_ptr_ret,
  293.                int socket)
  294. {
  295.   int status = 0;
  296.   x_pending_dns_lookup *obj;
  297.   char *host, *c;
  298.  
  299.   *hoststruct_ptr_ret = 0;
  300.   XP_ASSERT(host_and_port);
  301.   if (!host_and_port || !*host_and_port)
  302.     return -1;
  303.  
  304.   LOG("FE_StartAsyncDNSLookup (1)",
  305.       ("\"%s\", sock %d; ql=%d\n", host_and_port, socket,
  306.        x_pending_dns_queue_length()));
  307.  
  308.   XP_ASSERT(host_and_port);
  309.   if (!host_and_port) return MK_OUT_OF_MEMORY;
  310.  
  311.   c = strchr(host_and_port, '@');
  312.   if (c) host_and_port = c + 1;
  313.  
  314.   host = strdup(host_and_port);
  315.   if (!host) return MK_OUT_OF_MEMORY;
  316.  
  317.   c = strchr(host, ':');
  318.   if (c) *c = 0;
  319.  
  320.  
  321.   if (dns_socket < 0)    /* socket got hosed */
  322.     {
  323.       LOG("FE_StartAsyncDNSLookup (1.5)", ("calling gethostbyname()\n"));
  324.       *hoststruct_ptr_ret = gethostbyname(host);
  325.       if (*hoststruct_ptr_ret)
  326.     status = 0;
  327.       else
  328.     status = -1;
  329.       goto DONE;
  330.     }
  331.  
  332.  
  333.   /* First look through the queue and see if we have an answer for this one.
  334.    */
  335. RESTART_SEARCH:
  336.   for (obj = queue; obj; obj = obj->next)
  337.     {
  338.  
  339.       LOG("  FE_StartAsyncDNSLookup (2)",
  340.       ("comparing %s/%d to %s/%d/%d\n", host, socket,
  341.        (obj->name ? obj->name : "(null)"), obj->socket, (long) obj->id));
  342.  
  343.       XP_ASSERT(obj->name);
  344.       if (!obj->name) continue;
  345.       if (socket == obj->socket)
  346.     {
  347.  
  348.       /* Since we're on the same socket, it had better be the
  349.          same host!  If not, netlib has broken the contract...
  350.          (Unfortunately, this seems to happen...  Treat it as
  351.          an implied abort?)
  352.        */
  353.       if (!!strcasecomp(host, obj->name))
  354.         {
  355.           LOG("FE_StartAsyncDNSLookup (2.5)", ("netlib is crazy!!\n"));
  356.           /* XP_ASSERT(0); */
  357.           free_lookup(obj);
  358.           obj = 0;
  359.           goto RESTART_SEARCH; /* since free_ changed the list links */
  360.         }
  361.  
  362.       if (!obj->done)
  363.         {
  364.           /* A lookup has been started for this host already, and hasn't
  365.          come back.  (How do we get into this state?  (We do.)  It
  366.          means that FE_StartAsyncDNSLookup() was called twice with the
  367.          same host/sock, but before a response had come back on that
  368.          sock.)  So there will end up having been 3 calls.
  369.            */
  370.           LOG("FE_StartAsyncDNSLookup (3)",
  371.           ("\n\t\t\t already MK_WAITING_FOR_LOOKUP for %s\n", host));
  372.           status = MK_WAITING_FOR_LOOKUP;
  373.           goto DONE;
  374.         }
  375.       else if (obj->status <= 0)
  376.         {
  377.           /* Got an error looking up this host (that's bad!)
  378.          Remove this one from the list (is that safe?  I
  379.          *think* so...)
  380.            */
  381.  
  382.           LOG("FE_StartAsyncDNSLookup (4): negative status",
  383.           ("%s\n", host));
  384.  
  385.           status = obj->status;
  386.           free_lookup(obj);
  387.           obj = 0;
  388.           goto DONE;
  389.         }
  390.       else
  391.         {
  392.           /* Got an answer; construct a hostent and return it.
  393.          The caller expects this to be statically allocated
  394.          (same semantics as gethostbyname())
  395.            */
  396.           LOG("FE_StartAsyncDNSLookup (5): making hostent", ("%s\n",host));
  397.           *hoststruct_ptr_ret = make_hostent(obj->name, obj->result);
  398.           free_lookup(obj);
  399.           obj = 0;
  400.           status = 0;
  401.           goto DONE;
  402.         }
  403.     }
  404.     }
  405.   /* Didn't find an entry for this host, so make one... */
  406.  
  407.   LOG("FE_StartAsyncDNSLookup (6): launching lookup (1)", ("%s\n", host));
  408.  
  409.   obj = new_lookup (host, socket);
  410.   if (!obj)
  411.     {
  412.       status = MK_OUT_OF_MEMORY;
  413.       goto DONE;
  414.     }
  415.  
  416.   XP_ASSERT(!obj->done);
  417.   status = DNS_AsyncLookupHost(host, xfe_dns_done_cb, obj, &obj->id);
  418.   if (status >= 0)
  419.     {
  420.       status = MK_WAITING_FOR_LOOKUP;
  421.     }
  422.   else
  423.     {
  424.       free_lookup(obj);
  425.       obj = 0;
  426.       goto DONE;
  427.     }
  428.  
  429.  
  430.   LOG("FE_StartAsyncDNSLookup (7): launched lookup (2)",
  431.       ("%s; id=%d\n", host, (long) obj->id));
  432.  
  433.  
  434.   if (obj->done)
  435.     {
  436.       LOG("FE_StartAsyncDNSLookup (8): done already",
  437.       ("%s; sock=%d; id=%d\n", host, socket, (long) obj->id));
  438.  
  439.       status = obj->status;
  440.       if (status >= 0)
  441.     {
  442.       LOG("FE_StartAsyncDNSLookup (9): making hostent", ("%s\n", host));
  443.       *hoststruct_ptr_ret = make_hostent(obj->name, obj->result);
  444.     }
  445.       free_lookup(obj);
  446.       obj = 0;
  447.     }
  448.  
  449.  DONE:
  450.  
  451.   LOG("FE_StartAsyncDNSLookup (10)",
  452.       ("\n\t\t\treturning %s for %s; ql=%d\n",
  453.        (status == MK_WAITING_FOR_LOOKUP
  454.     ? "MK_WAITING_FOR_LOOKUP"
  455.     : (status == 0 ? "0" :
  456.        status < 0 ? "negative" : "positive")),
  457.        (host ? host : "(null)"),
  458.        x_pending_dns_queue_length()));
  459.  
  460.   if (host) free(host);
  461.  
  462.   return status;
  463. }
  464.  
  465.  
  466. void
  467. FE_ClearDNSSelect (MWContext *context, int socket)
  468. {
  469.   int status;
  470.   x_pending_dns_lookup *obj;
  471.  
  472.   LOG("FE_ClearDNSSelect (1)", ("sock %d; ql=%d\n", socket,
  473.                 x_pending_dns_queue_length()));
  474.  
  475.   for (obj = queue; obj; obj = obj->next)
  476.     {
  477.       LOG("  FE_ClearDNSSelect (2)",
  478.       ("comparing %d to %s/%d/%d\n", socket,
  479.        (obj->name ? obj->name : "(null)"), obj->socket, (long) obj->id));
  480.  
  481.       XP_ASSERT(obj->name);
  482.       if (!obj->name) continue;
  483.       if (socket == obj->socket && !obj->done)
  484.     {
  485.  
  486.       LOG("FE_ClearDNSSelect (3)", ("calling DNS_AbortHostLookup\n"));
  487.  
  488.       XP_ASSERT(!obj->done);
  489.       status = DNS_AbortHostLookup(obj->id);
  490.       return;
  491.     }
  492.     }
  493.  
  494.   LOG("FE_ClearDNSSelect (4)", ("no match for sock %d\n", socket));
  495.  
  496.   return;
  497. }
  498.