home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / util / att-nameserver / itserver.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-17  |  13.5 KB  |  691 lines

  1. /* $XConsortium: itserver.c,v 1.3 91/02/17 15:20:18 rws Exp $ */
  2.  
  3. /*
  4.  * Copyright 1988, 1989 AT&T, Inc.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software and its
  7.  * documentation for any purpose and without fee is hereby granted, provided
  8.  * that the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of AT&T not be used in advertising
  11.  * or publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  AT&T makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * AT&T DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL AT&T
  18.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  *
  23. */
  24.  
  25. #define _USHORT_H    /* prevent conflicts between BSD sys/types.h and
  26.                            interlan/il_types.h */
  27. #ifdef LACHMAN
  28. #include <sys/types.h>
  29. #include <sys/socket.h>
  30. #include <netdb.h>
  31. #include <netinet/in.h>
  32. #else
  33. #include <interlan/il_types.h>
  34. #include <interlan/socket.h>
  35. #include <interlan/netdb.h>
  36. #include <interlan/in.h>
  37. #endif
  38. #include <tiuser.h>
  39. #include <sys/param.h>
  40. #include <sys/utsname.h>
  41. #include <sys/signal.h>
  42. #include <fcntl.h>
  43. #include <errno.h>
  44. #include <stdio.h>
  45. #include <ctype.h>
  46. #include <X11/Xproto.h>
  47. #include "Xstreams.h"                /* in Xlib sources */
  48. #include <X11/X.h>
  49.  
  50. #include "osdep.h"
  51.  
  52. #ifdef    LACHMAN
  53. #define    INADDRSIZE 16
  54. #else
  55. #define    INADDRSIZE 8
  56. #endif
  57.  
  58. extern    char    *calloc(), *realloc(), *alialloc();
  59. extern  char    *program;
  60. int    network;
  61. int    nextentry;
  62.  
  63. char    *xalloc();
  64. char    *xrealloc();
  65. char *makePacket();
  66.  
  67. char    *TheEnd;
  68. char    *inbuf;
  69. int     inlen;
  70. int     dispno;
  71. char    display[64];
  72. int     nhosts;
  73. int    nHosts;
  74. int     flags = 0;
  75. int    FamilyType;
  76.  
  77.  
  78. typedef struct {
  79.     int af, xf;
  80. } FamilyMap;
  81.  
  82. static FamilyMap familyMap[] = {
  83. #ifdef     AF_DECnet
  84.     {AF_DECnet, FamilyDECnet},
  85. #endif /* AF_DECnet */
  86. #ifdef     AF_CHAOS
  87.     {AF_CHAOS, FamilyChaos},
  88. #endif /* AF_CHAOS */
  89. #ifdef    AF_INET
  90.     {AF_INET, FamilyInternet},
  91. #endif
  92. #ifdef    FamilyUname
  93.     {AF_UNSPEC, FamilyUname}
  94. #endif
  95. };
  96.  
  97. #define FAMILIES ((sizeof familyMap)/(sizeof familyMap[0]))
  98.  
  99. static int XFamily(af)
  100.     int af;
  101. {
  102.     int i;
  103.     for (i = 0; i < FAMILIES; i++)
  104.     if (familyMap[i].af == af)
  105.             return familyMap[i].xf;
  106.     return FamilyUname;
  107. }
  108.  
  109. static int UnixFamily(xf)
  110.     int xf;
  111. {
  112.     int i;
  113.     for (i = 0; i < FAMILIES; i++)
  114.     if (familyMap[i].xf == xf)
  115.             return familyMap[i].af;
  116.     return AF_UNSPEC;
  117. }
  118.  
  119. IOBUFFER InputBuffer[1];
  120.  
  121. main()
  122. {
  123.     ServiceClient();
  124. /*
  125.     sleep(3);
  126. */
  127. }
  128.  
  129. SendNull()
  130. {
  131.     char    buf[32];
  132.     char    *ptr;
  133.  
  134.     ptr = buf;
  135.     *(int *) ptr = 0;
  136.     ptr += sizeof(int);
  137.     *(int *) ptr = 0;
  138.     write(1, buf, 2*sizeof(int));
  139. }
  140.  
  141. ServiceClient()
  142. {
  143.     register IOBUFFER *iop = &InputBuffer[0];
  144.     int    n,m;
  145.     char    *ptr, *net;
  146.  
  147.     if((iop->inputbuf = (char *) xalloc(BUFSIZE)) == NULL)
  148.     {
  149.         SendNull();
  150.         return;
  151.     }
  152.     iop->buflen    = BUFSIZE;
  153.  
  154.     if(!Read(0, iop->inputbuf, HEADERSIZE))
  155.     {
  156.         fprintf(stderr, "Cannot read HEADERSIZE\n");
  157.         SendNull();
  158.         return(-1);
  159.     }
  160.  
  161.     iop->bufptr = HEADERSIZE;
  162.     iop->msglen = *(int *) iop->inputbuf;
  163.  
  164.     if(iop->buflen < iop->msglen)
  165.     {
  166.        if((iop->inputbuf = (char *) xrealloc(iop->inputbuf, iop->msglen)) == NULL)
  167.        {
  168.              SendNull();
  169.         return;
  170.        }
  171.        iop->buflen    = iop->msglen;
  172.     }
  173.  
  174.     if(!Read(0, &iop->inputbuf[iop->bufptr], iop->msglen - iop->bufptr))
  175.     {
  176.         fprintf(stderr, "Cannot read The rest of the message\n");
  177.         SendNull();
  178.         return(-1);
  179.         }
  180.  
  181.     ptr = &iop->inputbuf[sizeof(int)];
  182.     m = *(int *) ptr;
  183.     ptr += sizeof(int);
  184.  
  185.     flags = *(int *) ptr;
  186.     ptr += sizeof(int);
  187.  
  188.     dispno = *(int *) ptr;
  189.  
  190.     sprintf(display, "%d", dispno);
  191.     ptr += sizeof(int);
  192.     n = *(int *) ptr;
  193.  
  194.     ptr += sizeof(int);
  195.     net  = ptr;
  196.  
  197.     if(strcmp(net, "it") != 0)
  198.     {
  199.     }
  200.     ptr = &iop->inputbuf[m];
  201.     inlen = *(int *) ptr;
  202.  
  203.     ptr += sizeof(int);
  204.     nhosts = *(int *) ptr;
  205.  
  206.     inbuf = ptr + sizeof(int);
  207.     TheEnd = &inbuf[inlen];
  208. #ifdef DEBUG
  209.     write(2, inbuf, inlen);
  210. #endif
  211.         nextentry = ((xHostEntry *) inbuf)->length;
  212.         if((ptr = (char *) makePacket()) != NULL)
  213.     {
  214. #ifdef DEBUG
  215.                     write(2, ptr, (*(int *) ptr) + 2*sizeof(int));
  216. #endif
  217.                     write(1, ptr, (*(int *) ptr) + 2*sizeof(int));
  218.         }
  219.     return(1);
  220. }
  221.  
  222.  
  223. char *
  224. xalloc(n)
  225. int    n;
  226. {
  227.     char    *ptr;
  228.  
  229.     if((ptr = (char *) malloc(n)) == NULL)
  230.     {
  231.         fprintf(stderr, "malloc failed\n");
  232.         return(NULL);
  233.     }
  234.     return(ptr);
  235. }
  236.  
  237.  
  238. char *
  239. xrealloc(buf, n)
  240. char    *buf;
  241. int    n;
  242. {
  243.      char    *ptr;
  244.  
  245.         if((ptr = (char *) realloc(buf, n)) == NULL)
  246.     {
  247.              fprintf(stderr, "realloc failed\n");
  248.         return(NULL);
  249.     }
  250.         return(ptr);
  251. }
  252.  
  253.  
  254. int    ConvertEthernetName();
  255. int    ConvertEthernetAddress();
  256. int    MakeEthernetCall();
  257.  
  258. int    bufsize = 512;
  259.  
  260. char    *getnextentry();
  261.  
  262.  
  263. int
  264. ConvertEthernetName(pktptr, n, entry, len)
  265. char    **pktptr, *entry;
  266. int    n, len;
  267. {
  268.      struct hostent *hp;
  269.     unsigned long    address;
  270.     int    port;
  271.         char    *ptr;
  272.     int    entlen = INADDRSIZE;
  273.     int    rndlen;
  274.  
  275. #ifdef DEBUG
  276. fprintf(stderr, "in ConvertEthernetName %s\n", entry);
  277. #endif
  278.  
  279.     rndlen = ((sizeof(xHostEntry) + entlen + 3) >> 2) << 2;
  280.  
  281.     if (isascii(entry[0]) && isdigit(entry[0]))
  282.         address = inet_addr (entry);
  283.     else
  284.         address = (unsigned long)~0L;
  285.     if (address == (unsigned long)~0L) {
  286.         hp = gethostbyname(entry);
  287.         if (hp)
  288.         address = *(unsigned long *)hp->h_addr;
  289.     }
  290.     if (address == (unsigned long)~0L) 
  291.     {
  292.         entlen = strlen(display) + len + 2;
  293.         rndlen = ((sizeof(xHostEntry) + entlen + 3) >> 2) << 2;
  294.         if((*pktptr = alialloc(*pktptr, n+rndlen)) == NULL)
  295.                     return(-1);
  296.         ptr = &(*pktptr)[n];
  297.         ((xHostEntry *)ptr)->family = XFamily(AF_UNSPEC);
  298.         ((xHostEntry *)ptr)->length = entlen;
  299.         ptr += sizeof(xHostEntry);
  300.  
  301.         sprintf(ptr, "%s", entry);
  302.         return(n+rndlen);
  303.     }
  304.     port = atoi(display);
  305.         port += X_TCP_PORT;
  306.  
  307.     if((*pktptr = alialloc(*pktptr, n+rndlen)) == NULL)
  308.         return(-1);
  309.  
  310.     ptr = &(*pktptr)[n];
  311.     ((xHostEntry *)ptr)->family = XFamily(AF_INET);
  312.     ((xHostEntry *)ptr)->length = entlen;
  313.     ptr += sizeof(xHostEntry);
  314. /*
  315.     *(short *) ptr = htons(AF_INET);
  316.     *(short *) (ptr + sizeof(short)) = htons(port);
  317.     *(int *) (ptr + sizeof(short) + sizeof(short)) = address;
  318. */
  319.     *(int *) ptr = address;
  320.  
  321. #ifdef DEBUG
  322. fprintf(stderr, "creating address for host %s address<0x%x>\n", entry, address);
  323. #endif
  324.  
  325.     return(n+rndlen);
  326. }
  327.  
  328. int
  329. BindEthernetName(pktptr, n, entry, len)
  330. char    **pktptr, *entry;
  331. int    n, len;
  332. {
  333.     struct hostent *hp;
  334.     unsigned long    address;
  335.     int    port;
  336.     char    *ptr;
  337.     int    entlen = INADDRSIZE;
  338.     int    rndlen;
  339.  
  340. #ifdef DEBUG
  341. fprintf(stderr, "in ConvertEthernetName %s\n", entry);
  342. #endif
  343.  
  344.     rndlen = ((sizeof(xHostEntry) + entlen + 3) >> 2) << 2;
  345.  
  346.     if (isascii(entry[0]) && isdigit(entry[0]))
  347.         address = inet_addr (entry);
  348.     else
  349.         address = (unsigned long)~0L;
  350.     if (address == (unsigned long)~0L) {
  351.         hp = gethostbyname(entry);
  352.         if (hp)
  353.         address = *(unsigned long *)hp->h_addr;
  354.     }
  355.     if (address == (unsigned long)~0L) 
  356.     {
  357.        return(-1);
  358.     }
  359.     port = atoi(display);
  360.     port += X_TCP_PORT;
  361.  
  362.     if((*pktptr = alialloc(*pktptr, n+rndlen)) == NULL)
  363.         return(-1);
  364.  
  365.     ptr = &(*pktptr)[n];
  366.     ((xHostEntry *)ptr)->family = AF_INET;
  367.     ((xHostEntry *)ptr)->length = entlen;
  368.     ptr += sizeof(xHostEntry);
  369. #ifdef LACHMAN
  370.     *(short *) ptr = AF_INET;
  371. #else
  372.     *(short *) ptr = htons(AF_INET);
  373. #endif
  374.     *(short *) (ptr + sizeof(short)) = htons(port);
  375.     *(int *) (ptr + sizeof(short) + sizeof(short)) = address;
  376.  
  377. #ifdef DEBUG
  378. fprintf(stderr, "creating address for host %s address<0x%x>\n", entry, address);
  379. #endif
  380.  
  381.     return(n+rndlen);
  382. }
  383.  
  384.  
  385.  
  386. MakeEthernetCall(pktptr, n, entry, len)
  387. char    **pktptr, *entry;
  388. int    n, len;
  389. {
  390.      struct hostent *hp;
  391.     unsigned long    address;
  392.     int    port;
  393.     char    *ptr;
  394.     int    rndlen;
  395.     int    ra, ro, ru;
  396.     int    a, o, u;
  397.     struct    utsname machine;
  398.  
  399. #ifdef DEBUG
  400. fprintf(stderr, "in MakeEthernetCall %s\n", entry);
  401. #endif
  402.  
  403.         a  = INADDRSIZE;
  404.     o  = 0;
  405.     u  = 0;
  406.  
  407.         ra = ((a + sizeof(xHostEntry) + 3) >> 2) << 2;
  408.     ro = ((o + sizeof(xHostEntry) + 3) >> 2) << 2;
  409.     ru = ((u + sizeof(xHostEntry) + 3) >> 2) << 2;
  410.  
  411.         rndlen = ra + ro + ru;
  412.  
  413.         if((*pktptr = alialloc(*pktptr, n+rndlen)) == NULL)
  414.         return(-1);
  415.  
  416.  
  417.     if (isascii(entry[0]) && isdigit(entry[0]))
  418.         address = inet_addr (entry);
  419.     else
  420.         address = (unsigned long)~0L;
  421.     if (address == (unsigned long)~0L) {
  422.         hp = gethostbyname(entry);
  423.         if (hp)
  424.         address = *(unsigned long *)hp->h_addr;
  425.     }
  426.     if (address == (unsigned long)~0L) 
  427.     {
  428.         return(-1);
  429.     }
  430.         port = atoi(display);
  431.     port += X_TCP_PORT;
  432.  
  433.         if((*pktptr = alialloc(*pktptr, n+rndlen)) == NULL)
  434.         return(-1);
  435.  
  436.         ptr = &(*pktptr)[n];
  437.     ((xHostEntry *)ptr)->length = a;
  438.  
  439.     ptr += sizeof(xHostEntry);
  440. #ifdef LACHMAN
  441.     *(short *) ptr = AF_INET;
  442. #else
  443.     *(short *) ptr = htons(AF_INET);
  444. #endif
  445.     *(short *) (ptr + sizeof(short)) = htons(port);
  446.     *(int *) (ptr + sizeof(short) + sizeof(short)) = address;
  447.  
  448.     ptr = &(*pktptr)[n+ra];
  449.     ((xHostEntry *)ptr)->length = o;
  450.  
  451.     ptr = &(*pktptr)[n+ra+ro];
  452.     ((xHostEntry *)ptr)->length = u;
  453.  
  454. #ifdef DEBUG
  455. fprintf(stderr, "creating address for host %s address<0x%x> and returning +%d\n",
  456.          entry, address, rndlen);
  457. #endif
  458.  
  459.     return(n+rndlen);
  460. }
  461.  
  462. int
  463. ConvertEtherCallToName(pktptr, n, entry, len)
  464. char    **pktptr, *entry;
  465. int    n, len;
  466. {
  467.      int    l, rl;
  468.     char    *ptr;
  469.     struct hostent *hp;
  470.     unsigned long    address;
  471.  
  472.         ptr = entry;
  473.  
  474. /*
  475.       nf = *(short *) ptr;
  476.     port = *(short *) (ptr + sizeof(short));
  477. */
  478.       address = *(int *) (ptr + sizeof(short) + sizeof(short));
  479.     if((hp = gethostbyaddr(&address, sizeof(int), AF_INET)) == NULL){
  480.         fprintf(stderr, "gethostbyaddr() failed \n");
  481.         return(n);
  482.         }
  483.  
  484.  
  485.         l = strlen(hp->h_name) + 1;
  486.  
  487.         rl = ((sizeof(xHostEntry) + l + 3) >> 2) << 2;
  488.  
  489.         if((*pktptr = alialloc(*pktptr, n+rl)) == NULL)
  490.         return(-1);
  491.  
  492.         ptr = &(*pktptr)[n];
  493.     ((xHostEntry *)ptr)->family = 1;
  494.     ((xHostEntry *)ptr)->length = l;
  495.  
  496.     ptr += sizeof(xHostEntry);
  497.  
  498.     sprintf(ptr, hp->h_name);
  499.  
  500. #ifdef DEBUG
  501. fprintf(stderr, "getting the name for host %s\n", hp->h_name);
  502. #endif
  503.  
  504.         return(rl+n);
  505. }
  506.  
  507. int
  508. ConvertEthernetAddress(pktptr, n, entry, len)
  509. char    **pktptr, *entry;
  510. int    n, len;
  511. {
  512.     register i;
  513.      char    *ptr;
  514.         int     entlen; 
  515.         int     rndlen;
  516.      struct hostent *hp;
  517.     int    address;
  518.     char    *name, buf[32];
  519.  
  520. /*
  521.     nf = *(short *) entry;
  522.     port = *(short *) (entry + sizeof(short));
  523.     address = *(int *) (entry + sizeof(short) + sizeof(short));
  524. */
  525.     address = *(int *) entry;
  526.     if((hp = gethostbyaddr(&address, sizeof(int), AF_INET)) == NULL)
  527.     {
  528. /*
  529.         fprintf(stderr, "gethostbyaddr() failed \n");
  530.         return(n);
  531. */
  532.         if(FamilyType == AF_UNSPEC){
  533.             name = entry;
  534.             entlen = len + 1;
  535.             }
  536.         else {
  537.             sprintf(buf, "%d", address);
  538.             fprintf(stderr, "%d: unknown address\n", address);
  539.             entlen = strlen(buf) +1;
  540.             name = buf;
  541.         }
  542.     }
  543.     else {
  544.         entlen = strlen(hp->h_name) + 1;
  545.         name = hp->h_name;
  546.     }
  547.  
  548.         rndlen = ((sizeof(xHostEntry) + entlen + 3) >> 2) << 2;
  549.  
  550.         if((*pktptr = alialloc(*pktptr, n+rndlen)) == NULL)
  551.                 return(-1);
  552.  
  553.         ptr = &(*pktptr)[n];
  554.         ((xHostEntry *)ptr)->family = FamilyType;
  555.         ((xHostEntry *)ptr)->length = entlen;
  556.         ptr += sizeof(xHostEntry);
  557.  
  558. #ifdef DEBUG
  559. fprintf(stderr, "getting the name for host %s\n", name);
  560. #endif
  561.       memcpy(ptr, name, entlen);    
  562.     
  563.     return(n+rndlen);
  564. }
  565.  
  566. char    *
  567. getnextentry(plen)
  568. int    *plen;
  569. {
  570.     char    *ptr;
  571.     int    n = nextentry;
  572.  
  573. #ifdef DEBUG
  574. fprintf(stderr,"In getnextentry()\n");
  575. #endif
  576.     if(inbuf >= TheEnd)
  577.     {
  578.         *plen = -1;
  579.         return(NULL);    
  580.     }
  581.  
  582.     *plen = nextentry;
  583.         ptr = inbuf + sizeof(xHostEntry);
  584.     FamilyType = UnixFamily(((xHostEntry *) inbuf)->family);
  585.  
  586.     inbuf += ((sizeof(xHostEntry) + *plen + 3) >> 2) << 2;
  587.     nextentry = ((xHostEntry *) inbuf)->length;
  588.     ptr[*plen] = '\0';
  589.         nhosts++;
  590.     return(ptr);
  591. }
  592.  
  593. char *
  594. makePacket()
  595. {
  596.     char *pktptr = NULL, *ptr;
  597.     int    len;
  598.     int    n, m;
  599.  
  600.     n = sizeof(int) * 2;
  601.     pktptr = (char *) malloc(bufsize);
  602.  
  603. #ifdef DEBUG
  604. fprintf(stderr,"In makePacket()\n");
  605. #endif
  606.  
  607.     if(pktptr == NULL)
  608.     return(NULL);
  609.     
  610.     for(nHosts = 0; nHosts < nhosts;)
  611.     {
  612.     ptr = getnextentry(&len);
  613.     if(len < 0)
  614.         break;
  615.     if(len == 0 || ptr == NULL)
  616.         continue;    
  617.     m = addentry(&pktptr, n, ptr, len);
  618.     if(m > n){
  619.         nHosts++;
  620.         n = m;
  621.         }
  622.     }
  623. #ifdef DEBUG
  624.     fprintf(stderr, "packet size is %d\n", n);
  625. #endif
  626.  
  627.     *(int *) pktptr = n - 2*sizeof(int);
  628.     *(int *) (pktptr+sizeof(int)) = nHosts;
  629.     return(pktptr);
  630. }
  631.  
  632. int
  633. addentry(pktptr, n, entry, len)
  634. char    **pktptr, *entry;
  635. int    n, len;
  636. {
  637.  
  638. #ifdef DEBUG
  639.     fprintf(stderr, "in addentry %s\n", entry);
  640. #endif
  641.     switch(flags)
  642.     {
  643.         case    ConvertNameToNetAddr:
  644.             return(ConvertEthernetName(pktptr, n, entry, len));
  645.         case    ConvertNetAddrToName:
  646.             return(ConvertEthernetAddress(pktptr, n, entry, len));
  647.         case    ConvertNameToTliCall:
  648.             return(MakeEthernetCall(pktptr, n, entry, len));
  649.         case    ConvertTliCallToName:
  650.             return(ConvertEtherCallToName(pktptr, n, entry, len));
  651.         case    ConvertNameToTliBind:
  652.             return(BindEthernetName(pktptr, n, entry, len));
  653.     }
  654.         return(-1);
  655. }
  656.  
  657. char *
  658. alialloc(ptr, size)
  659. char    *ptr;
  660. int    size;
  661. {
  662.     if(bufsize < size){
  663.         bufsize = size + 512;
  664.         ptr = realloc(ptr, bufsize);
  665.         }
  666.     return(ptr);
  667. }
  668.  
  669.  
  670. Read(fd, buf, count)
  671. int    fd, count;
  672. char    *buf;
  673. {
  674.      int    n;
  675.     int    m = 0;
  676.     int    t = count;
  677.     while((n = read(fd, buf, count)) > 0)
  678.     {
  679.              if(n == count)
  680.         {
  681.                      return(1);
  682.         }
  683.                 buf += n;
  684.         count -= n;
  685.         m += n;
  686.     }
  687.     fprintf(stderr, "Trying to read %d but only %d read\n", t, m);
  688.         return(0);
  689. }
  690.  
  691.