home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Telnet 2.7b5 / source / network / mydnr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-28  |  5.0 KB  |  202 lines  |  [TEXT/CWIE]

  1. /*
  2. *   Util.c
  3. *   utility library for use with the Network kernel
  4. *
  5. *   version 2, full session layer, TK started 6/17/87
  6. *****************************************************************
  7. *    NCSA Telnet for the Macintosh                                *
  8. *                                                                *
  9. *    National Center for Supercomputing Applications                *
  10. *    Software Development Group                                    *
  11. *    152 Computing Applications Building                            *
  12. *    605 E. Springfield Ave.                                        *
  13. *    Champaign, IL  61820                                        *
  14. *                                                                *
  15. *    Copyright (c) 1986-1992,                                    *
  16. *    Board of Trustees of the University of Illinois                *
  17. *****************************************************************
  18. */
  19.  
  20. #ifdef MPW
  21. #pragma segment DNR
  22. #endif
  23.  
  24.  
  25. #include "InternalEvents.h"
  26. #include "wind.h"
  27. #include "bkgr.proto.h"
  28. #include "netevent.proto.h"
  29. #include "mydnr.proto.h"
  30. #include "Connections.proto.h"
  31. #include "AddressXlation.h"
  32. extern long MyA5;
  33.  
  34. #ifdef __MWERKS__
  35. #pragma profile off
  36. #endif
  37.  
  38. typedef    struct {
  39.     short
  40.         screen;
  41.     long
  42.         MyA5;
  43.     OSErr
  44.         theError;
  45.     Str63
  46.         hostname;
  47.     struct hostInfo
  48.         *hinfo;
  49. }    DNRDelayStruct;
  50.  
  51. pascal void DNRDone(struct hostInfo *hostInfoPtr, DNRDelayStruct *info);
  52. pascal void DNRDoneInit(struct hostInfo *hostInfoPtr, DNRDelayStruct *info);
  53. PROTO_UPP(DNRDoneInit, Result);
  54.  
  55. /**********************************************************************
  56.  * DotToNum - turn an address in dotted decimal into an internet address
  57.  * returns True if the conversion was successful.  This routine is
  58.  * somewhat limited, in that it will accept only four octets, and does
  59.  * not permit the abbreviated forms for class A and B networks. <- Steve Dorner
  60.  **********************************************************************/
  61. Boolean DotToNum(BytePtr string,ip_addr *nPtr)
  62. {
  63.   unsigned long    address=0;
  64.   short            b=0;
  65.   BytePtr         cp;
  66.   short            dotcount=0;
  67.   
  68.   /*
  69.    * allow leading spaces
  70.    */
  71.   for (cp=string+1;cp<=string+*string;cp++) if (*cp!=' ') break;
  72.   
  73.   /*
  74.    * the address
  75.    */
  76.   for (;cp<=string+*string;cp++)
  77.   {
  78.     if (*cp=='.')
  79.     {
  80.       if (++dotcount > 3) return (FALSE); /* only 4 octets allowed */
  81.       address <<= 8;
  82.       address |= b;
  83.       b=0;
  84.     }
  85.     else if (isdigit(*cp))
  86.     {
  87.       b *= 10;
  88.       b += (*cp - '0');
  89.       if (b>255) return (FALSE);          /* keep it under 256 */
  90.     }
  91.     else if (*cp==' ')                    /* allow trailing spaces */
  92.       break;
  93.     else
  94.       return (FALSE);                     /* periods or digits ONLY */
  95.   }
  96.   
  97.   /*
  98.    * final checks, assignment
  99.    */
  100.   if (dotcount!=3) return (FALSE);
  101.   address <<= 8;
  102.   address |= b;
  103.   *nPtr = (ip_addr) address;
  104.   return(TRUE);
  105. }
  106.  
  107. /**************************************************************************/
  108. /*    For FTP to look up the transfer options to use when running */
  109. Boolean    TranslateIPtoDNSname(ip_addr ipnum, StringPtr machineName)
  110. {
  111.     UNUSED_ARG(ipnum)
  112.     UNUSED_ARG(machineName)
  113.     // NEED TO IMPLEMENT THIS BUGG
  114.     return(FALSE);
  115.  
  116. }
  117.  
  118. /*********************************************************************/
  119. pascal void DNRDone(struct hostInfo *hostInfoPtr, DNRDelayStruct *info)
  120. {
  121.     UNUSED_ARG(hostInfoPtr)
  122.     netputevent(USERCLASS,DOMAIN_DONE,info->screen, (long)info);
  123. }
  124.  
  125. SIMPLE_UPP(DNRDoneInit, Result);
  126. pascal void DNRDoneInit(struct hostInfo *hostInfoPtr, DNRDelayStruct *info)
  127. {
  128.     long saveA5;
  129.     DNRDelayStruct *mptr;
  130.     struct hostInfo *hptr;
  131.     
  132.     mptr = info;
  133.     hptr = hostInfoPtr;
  134.  
  135. #ifndef    __powerpc__
  136.     saveA5 = SetA5(mptr->MyA5);
  137. #endif
  138.  
  139.     DNRDone(hptr, mptr);
  140.     
  141. #ifndef    __powerpc__
  142.     SetA5(saveA5);
  143. #endif
  144. }
  145.  
  146. OSErr    DoTheDNR(StringPtr hostname, short window)
  147. {
  148.     DNRDelayStruct        *Info;
  149.     struct hostInfo        *HInfo;
  150.     ip_addr                ip;
  151.     
  152.     Info = (DNRDelayStruct *)myNewPtr(sizeof(DNRDelayStruct));
  153.     HInfo = (struct hostInfo *)myNewPtr(sizeof(struct hostInfo));
  154.     
  155.     if ((Info == NULL) || (HInfo == NULL)) return(memFullErr);
  156.     
  157.     Info->screen = window;
  158. #ifndef __powerpc__
  159.     Info->MyA5 = MyA5;
  160. #endif
  161.     Info->hinfo = HInfo;
  162.         
  163.     BlockMove(hostname, Info->hostname, Length(hostname)+1);
  164.     
  165.     if (DotToNum(hostname, &ip)) {
  166.         Info->hinfo->rtnCode = noErr;
  167.         Info->hinfo->addr[0] = ip;
  168.         DNRDone(HInfo, Info);
  169.         return(noErr);
  170.         }
  171.         
  172.     PtoCstr(Info->hostname);
  173.     Info->theError = StrToAddr((char *)Info->hostname, HInfo,
  174.                                 DNRDoneInitUPP, (Ptr)Info);
  175.     
  176.     if ((Info->theError != cacheFault) && (Info->theError != inProgress))
  177.         DNRDone(HInfo, Info);
  178.     
  179.     return(noErr);
  180. }
  181.  
  182. //    The event queue routines send us the screen number that DNRDone sent.  We demangle
  183. //    this mess of data and call CompleteConnectionOpening to do all of the port and screen
  184. //    stuff that we shouldn't know about at this level.  We are merely a non-interrupt level
  185. //    flow control point. (i.e. I would do this from DNRDone, but that's interrupt time)
  186. void    HandleDomainDoneMessage(short screen, long data2)
  187. {
  188.     DNRDelayStruct    *MyData = (DNRDelayStruct *)data2;
  189.     ip_addr            the_IP;
  190.     OSErr            theErr;        // The error, if any
  191.     
  192.     the_IP = MyData->hinfo->addr[0];
  193.     theErr = MyData->hinfo->rtnCode;
  194.     
  195.     CompleteConnectionOpening(screen, the_IP, theErr, MyData->hinfo->cname);
  196.  
  197.     //    We also dispose of the DNR memory allocations
  198.         
  199.     DisposePtr((Ptr)MyData->hinfo);
  200.     DisposePtr((Ptr)MyData);
  201. }
  202.