home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / util / att-nameserver / starserver.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-10  |  9.5 KB  |  504 lines

  1. #ifndef NOIDENT
  2. #ident    "@(#)nameserver:starserver.c    1.4"
  3. #endif
  4.  
  5. /*
  6.  * Copyright 1988, 1989 AT&T, Inc.
  7.  *
  8.  * Permission to use, copy, modify, and distribute this software and its
  9.  * documentation for any purpose and without fee is hereby granted, provided
  10.  * that the above copyright notice appear in all copies and that both that
  11.  * copyright notice and this permission notice appear in supporting
  12.  * documentation, and that the name of AT&T not be used in advertising
  13.  * or publicity pertaining to distribution of the software without specific,
  14.  * written prior permission.  AT&T makes no representations about the
  15.  * suitability of this software for any purpose.  It is provided "as is"
  16.  * without express or implied warranty.
  17.  *
  18.  * AT&T DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  19.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL AT&T
  20.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  21.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  22.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
  23.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  *
  25. */
  26.  
  27. #include <X11/Xos.h>
  28. #include <tiuser.h>
  29. #include <sys/param.h>
  30. #include <sys/utsname.h>
  31. #include <sys/signal.h>
  32. #include <errno.h>
  33. #include <stdio.h>
  34. #include <X11/Xproto.h>
  35. #include "Xstreams.h"                /* in Xlib sources */
  36.  
  37. #include "osdep.h"
  38.  
  39.  
  40. extern    char    *calloc(), *realloc(), *alialloc();
  41. extern  char    *program;
  42. int    network;
  43. int    nextentry;
  44.  
  45. char    *xalloc();
  46. char    *xrealloc();
  47. char *makePacket();
  48.  
  49. char    *TheEnd;
  50. char    *inbuf;
  51. int     inlen;
  52. int     dispno;
  53. char    display[64];
  54. int     nhosts;
  55. int    nHosts;
  56. int     flags = 0;
  57.  
  58. IOBUFFER InputBuffer[1];
  59.  
  60. main()
  61. {
  62.     ServiceClient();
  63. /*
  64.     sleep(3);
  65. */
  66. }
  67.  
  68. SendNull()
  69. {
  70.     char    buf[32];
  71.     char    *ptr;
  72.  
  73.     ptr = buf;
  74.     *(int *) ptr = 0;
  75.     ptr += sizeof(int);
  76.     *(int *) ptr = 0;
  77.     write(1, buf, 2*sizeof(int));
  78. }
  79.  
  80. ServiceClient()
  81. {
  82.     register IOBUFFER *iop = &InputBuffer[0];
  83.     int    n,m;
  84.     char    *ptr, *net;
  85.  
  86.     if((iop->inputbuf = (char *) xalloc(BUFSIZE)) == NULL)
  87.     {
  88.         SendNull();
  89.         return;
  90.     }
  91.     iop->buflen    = BUFSIZE;
  92.  
  93.     if(!Read(0, iop->inputbuf, HEADERSIZE))
  94.     {
  95.         fprintf(stderr, "Cannot read the HEADERSIZE\n");
  96.         SendNull();
  97.         return(-1);
  98.     }
  99.  
  100.     iop->bufptr = HEADERSIZE;
  101.     iop->msglen = *(int *) iop->inputbuf;
  102.  
  103.     if(iop->buflen < iop->msglen)
  104.     {
  105.        iop->inputbuf = (char *) xrealloc(iop->inputbuf, iop->msglen);
  106.        if( iop->inputbuf == NULL)
  107.            {
  108.                 SendNull();
  109.                 return;
  110.            }
  111.        iop->buflen    = iop->msglen;
  112.     }
  113.  
  114.     if(!Read(0, &iop->inputbuf[iop->bufptr], iop->msglen - iop->bufptr))
  115.     {
  116.         fprintf(stderr, "Cannot read the rest of the message\n");
  117.         SendNull();
  118.         return(-1);
  119.         }
  120.  
  121.     ptr = &iop->inputbuf[sizeof(int)];
  122.     m = *(int *) ptr;
  123.     ptr += sizeof(int);
  124.  
  125.     flags = *(int *) ptr;
  126.     ptr += sizeof(int);
  127.  
  128.     dispno = *(int *) ptr;
  129.  
  130.     ptr += sizeof(int);
  131.     n = *(int *) ptr;
  132.  
  133.     ptr += sizeof(int);
  134.     net  = ptr;
  135.  
  136.     ptr = &iop->inputbuf[m];
  137.     inlen = *(int *) ptr;
  138.  
  139.     ptr += sizeof(int);
  140.     nhosts = *(int *) ptr;
  141.  
  142.     inbuf = ptr + sizeof(int);
  143.     TheEnd = &inbuf[inlen];
  144. #ifdef DEBUG
  145.     write(2, inbuf, inlen);
  146. #endif
  147.         nextentry = ((xHostEntry *) inbuf)->length;
  148.         if((ptr = (char *) makePacket()) != NULL)
  149.     {
  150. #ifdef DEBUG1
  151.                     write(2, ptr, (*(int *) ptr) + 2*sizeof(int));
  152. #endif
  153.                     write(1, ptr, (*(int *) ptr) + 2*sizeof(int));
  154.         }
  155.     return(1);
  156. }
  157.  
  158.  
  159. char *
  160. xalloc(n)
  161. int    n;
  162. {
  163.     char    *ptr;
  164.  
  165.     if((ptr = (char *) malloc(n)) == NULL)
  166.     {
  167.         fprintf(stderr, "malloc failed\n");
  168.         return(NULL);
  169.     }
  170.     return(ptr);
  171. }
  172.  
  173.  
  174. char *
  175. xrealloc(buf, n)
  176. char    *buf;
  177. int    n;
  178. {
  179.      char    *ptr;
  180.  
  181.         if((ptr = (char *) realloc(buf, n)) == NULL)
  182.     {
  183.              fprintf(stderr, "realloc failed\n");
  184.         return(NULL);
  185.     }
  186.         return(ptr);
  187. }
  188.  
  189.  
  190. int    ConvertStarlanAddress();
  191. int    ConvertStarlanName();
  192. int    MakeStarlanCall();
  193.  
  194. int    bufsize = 512;
  195.  
  196. char    *getnextentry();
  197.  
  198. int
  199. BindStarlanName(pktptr, n, entry, len)
  200. char    **pktptr, *entry;
  201. int    n, len;
  202. {
  203.      int    entlen;
  204.     int    rndlen;
  205.     char    *ptr;
  206.  
  207. #ifdef DEBUG
  208. fprintf(stderr, "in ConvertStarlanName %s\n", entry);
  209. #endif
  210.     sprintf(display, "%s%d", entry, dispno);
  211.     entlen = strlen(display) + 1;
  212.         rndlen = ((sizeof(xHostEntry) + entlen + 3) >> 2) << 2;
  213.     if((*pktptr = alialloc(*pktptr, n+rndlen)) == NULL)
  214.                                 return(-1);
  215.  
  216.         ptr = &(*pktptr)[n];
  217.     ((xHostEntry *)ptr)->family = FamilyUname;
  218.     ((xHostEntry *)ptr)->length = entlen - 1;
  219.     ptr += sizeof(xHostEntry);
  220.  
  221.         sprintf(ptr, "%s", display);
  222. #ifdef DEBUG
  223. fprintf(stderr, "creating address for host %s, address<%s>\n", entry, ptr);
  224. #endif
  225.  
  226.         return(n+rndlen);
  227. }
  228.  
  229. int
  230. ConvertStarlanName(pktptr, n, entry, len)
  231. char    **pktptr, *entry;
  232. int    n, len;
  233. {
  234.     int    entlen = 1 + len ;
  235.     int    rndlen;
  236.     char    *ptr;
  237.  
  238. #ifdef DEBUG
  239. fprintf(stderr, "in ConvertStarlanName %s\n", entry);
  240. #endif
  241.  
  242.     rndlen = ((sizeof(xHostEntry) + entlen + 3) >> 2) << 2;
  243.     if((*pktptr = alialloc(*pktptr, n+rndlen)) == NULL)
  244.                 return(-1);
  245.  
  246.     ptr = &(*pktptr)[n];
  247.         ((xHostEntry *)ptr)->family = FamilyUname;
  248.         ((xHostEntry *)ptr)->length = entlen;
  249.         ptr += sizeof(xHostEntry);
  250.  
  251.     sprintf(ptr, "%s", entry);
  252. #ifdef DEBUG
  253. fprintf(stderr, "creating address for host %s, address<%s>\n", entry, ptr);
  254. #endif
  255.         
  256.     return(n+rndlen);
  257. }
  258.  
  259. int
  260. ConvertStarCallToName(pktptr, n, entry, len)
  261. char    **pktptr, *entry;
  262. int    n, len;
  263. {
  264.     int    l, rl;
  265.     char    *src, *ptr;
  266.     int    a, o, u;
  267.  
  268. #ifdef DEBUG
  269. fprintf(stderr, "In ConvertStarCallToName()\n");
  270. #endif
  271.  
  272.     if(
  273.         getnextentry(&l) == NULL ||
  274.         (src = getnextentry(&l)) == NULL
  275.       ){
  276.         fprintf(stderr,
  277.         "ConvertStarCallToName didn't receive a correct TLI message\n");
  278.         return(-1);
  279.     }
  280.  
  281.         rl = ((l + sizeof(xHostEntry) + 3) >> 2) << 2;
  282.  
  283.         if((*pktptr = alialloc(*pktptr, n+rl)) == NULL)
  284.         return(-1);
  285.  
  286.         ptr = &(*pktptr)[n];
  287.     ((xHostEntry *)ptr)->length = l;
  288.  
  289.     ptr += sizeof(xHostEntry);
  290.     
  291.     bcopy(src, ptr, l);
  292. #ifdef DEBUG
  293.     fprintf(stderr, "ConvertStarCallToName returns %s\n", ptr);
  294. #endif
  295.     return(rl+n);
  296. }
  297.  
  298.  
  299. int
  300. MakeStarlanCall(pktptr, n, entry, len)
  301. char    **pktptr, *entry;
  302. int    n, len;
  303. {
  304.     char    *ptr;
  305.     int    rndlen;
  306.     int    ra, ro, ru;
  307.     int    a, o, u;
  308.     struct    utsname machine;
  309.  
  310. #ifdef DEBUG
  311. fprintf(stderr, "in MakeStarlanCall %s\n", entry);
  312. #endif
  313.  
  314.     if(uname(&machine) < 0)
  315.         return(-1);
  316.  
  317.     sprintf(display, "%s%d", entry, dispno);
  318.     a  = strlen(display) + 1;
  319.     o  = 0;
  320.     u  = strlen(machine.nodename) + 1;
  321.  
  322.     ra = ((a + sizeof(xHostEntry) + 3) >> 2) << 2;
  323.     ro = ((o + sizeof(xHostEntry) + 3) >> 2) << 2;
  324.         ru = ((u + sizeof(xHostEntry) + 3) >> 2) << 2;
  325.  
  326.         rndlen = ra + ro + ru;
  327.  
  328.     if((*pktptr = alialloc(*pktptr, n+rndlen)) == NULL)
  329.         return(-1);
  330.  
  331.  
  332.     ptr = &(*pktptr)[n];
  333.     ((xHostEntry *)ptr)->length = a - 1;
  334.     ptr += sizeof(xHostEntry);
  335.     sprintf(ptr, "%s", display);
  336.  
  337. #ifdef DEBUG
  338. fprintf(stderr, "creating address for host %s address<%s>\n", entry, ptr);
  339. #endif
  340.  
  341.     ptr = &(*pktptr)[n+ra];
  342.     ((xHostEntry *)ptr)->length = o;
  343.  
  344.  
  345.     ptr = &(*pktptr)[n+ra+ro];
  346.     ((xHostEntry *)ptr)->length = u;
  347.     ptr += sizeof(xHostEntry);
  348.     sprintf(ptr, "%s", machine.nodename);
  349.  
  350.     return(n+rndlen);
  351. }
  352.  
  353.  
  354. int
  355. ConvertStarlanAddress(pktptr, n, entry, len)
  356. char    **pktptr, *entry;
  357. int    n, len;
  358. {
  359.     char    *ptr;
  360.     int    entlen = len;
  361.     int    rndlen;
  362.  
  363.     rndlen = ((sizeof(xHostEntry) + entlen + 3) >> 2) << 2;
  364.  
  365.     if((*pktptr = alialloc(*pktptr, n+rndlen)) == NULL)
  366.         return(-1);
  367.  
  368.     ptr = &(*pktptr)[n];
  369.     ((xHostEntry *)ptr)->family = FamilyUname;
  370.     ((xHostEntry *)ptr)->length = entlen;
  371.     ptr += sizeof(xHostEntry);
  372.  
  373. #ifdef DEBUG
  374. fprintf(stderr, "getting the name for host %s\n", entry);
  375. #endif
  376.     bcopy(entry, ptr, len);
  377.  
  378.     return(n+rndlen);
  379. }
  380.  
  381. char    *
  382. getnextentry(plen)
  383. int    *plen;
  384. {
  385.     char    *ptr;
  386.     int    n = nextentry;
  387.  
  388. #ifdef DEBUG
  389. fprintf(stderr,"In getnextentry()\n");
  390. #endif
  391.     if(inbuf >= TheEnd)
  392.     {
  393.         *plen = -1;
  394.         return(NULL);    
  395.     }
  396.  
  397.     *plen = nextentry;
  398.         ptr = inbuf + sizeof(xHostEntry);
  399.     inbuf += ((sizeof(xHostEntry) + *plen + 3) >> 2) << 2;
  400.     nextentry = ((xHostEntry *) inbuf)->length;
  401.     ptr[*plen] = '\0';
  402.         nhosts++;
  403.     return(ptr);
  404. }
  405.  
  406. char *
  407. makePacket()
  408. {
  409.     char *pktptr = NULL, *ptr;
  410.     int    len;
  411.     int    n, m;
  412.  
  413.     n = sizeof(int) * 2;
  414.     pktptr = (char *) malloc(bufsize);
  415.  
  416. #ifdef DEBUG
  417. fprintf(stderr,"In makePacket()\n");
  418. #endif
  419.  
  420.     if(pktptr == NULL)
  421.     return(NULL);
  422.     
  423.     for(nHosts = 0; nHosts < nhosts;)
  424.     {
  425.     ptr = getnextentry(&len);
  426.     if(len < 0)
  427.         break;
  428.     if(len == 0 || ptr == NULL)
  429.         continue;    
  430.     m = addentry(&pktptr, n, ptr, len);
  431.     if(m > n){
  432.         nHosts++;
  433.         n = m;
  434.         }
  435.     }
  436. #ifdef DEBUG
  437.     fprintf(stderr, "packet size is %d\n", n);
  438. #endif
  439.  
  440.     *(int *) pktptr = n - 2*sizeof(int);
  441.     *(int *) (pktptr+sizeof(int)) = nHosts;
  442.     return(pktptr);
  443. }
  444.  
  445. int
  446. addentry(pktptr, n, entry, len)
  447. char    **pktptr, *entry;
  448. int    n, len;
  449. {
  450.  
  451. #ifdef DEBUG
  452.     fprintf(stderr, "in addStarlanEntry %s\n", entry);
  453. #endif
  454.  
  455.     switch(flags)
  456.     {
  457.         case    ConvertNameToNetAddr:
  458.             return(ConvertStarlanName(pktptr, n, entry, len));
  459.         case    ConvertNetAddrToName:
  460.             return(ConvertStarlanAddress(pktptr, n, entry, len));
  461.         case    ConvertNameToTliCall:
  462.             return(MakeStarlanCall(pktptr, n, entry, len));
  463.         case    ConvertTliCallToName:
  464.             return(ConvertStarCallToName(pktptr, n, entry, len));
  465.         case    ConvertNameToTliBind:
  466.             return(BindStarlanName(pktptr, n, entry, len));
  467.     }
  468.     return(-1);
  469. }
  470.  
  471. char *
  472. alialloc(ptr, size)
  473. char    *ptr;
  474. int    size;
  475. {
  476.     if(bufsize < size){
  477.         bufsize = size + 512;
  478.         ptr = realloc(ptr, bufsize);
  479.         }
  480.     return(ptr);
  481. }
  482.  
  483.  
  484. Read(fd, buf, count)
  485. int    fd, count;
  486. char    *buf;
  487. {
  488.      int    n, m = 0, t = count;
  489.  
  490.     while((n = read(fd, buf, count)) > 0)
  491.     {
  492.              if(n == count)
  493.         {
  494.                      return(1);
  495.         }
  496.                 buf += n;
  497.         count -= n;
  498.         m += n;
  499.     }
  500.     fprintf(stderr, "Trying to read %d but only %d read\n", t, m);
  501.         return(0);
  502. }
  503.  
  504.