home *** CD-ROM | disk | FTP | other *** search
/ GRIPS 2: Government Rast…rocessing Software & Data / GRIPS_2.cdr / dos / ncsa_tel / contribu / byu_tel2.hqx / tcpip / mactools.c < prev    next >
Text File  |  1989-07-10  |  10KB  |  409 lines

  1. /*
  2. *  mactools.c                by Gaige B. Paulsen
  3. *
  4. *  adapted from pctools.c      by Tim Krauskopf
  5. ****************************************************************************
  6. *                                                                          *
  7. *      part of:                                                            *
  8. *      TCP/UDP/ICMP/IP Network kernel for NCSA Telnet                      *
  9. *      by Tim Krauskopf                                                    *
  10. *                                                                          *
  11. *      National Center for Supercomputing Applications                     *
  12. *      152 Computing Applications Building                                 *
  13. *      605 E. Springfield Ave.                                             *
  14. *      Champaign, IL  61820                                                *
  15. *                                                                          *
  16. *                                                                          *
  17. ****************************************************************************
  18. *
  19. *  those generic tool-type things that only work on Macs.
  20. *  includes all hardware-level calls to Ethernet that are unique to the Mac
  21. *
  22. */
  23.  
  24. #include "stdio.h"
  25. #include "protocol.h"
  26. #include "data.h"
  27. #include <Files.h>
  28. #include <AppleTalk.h>
  29. #include <Events.h>
  30.  
  31. #include "ethertalk.h"
  32. #include "atalk.h"                /* Dlayer to atalk conversion */
  33.  
  34. #ifdef MPW
  35. #define    movmem(y,x,z)    memcpy(x,y,z)
  36. #endif
  37.  
  38. /*
  39.  * THE ALL-POWERFUL VARIABLE !!!!!!!!!!
  40.  */
  41.  
  42. int EtherNet=0;        /* Default to AppleTalk */
  43.  
  44.  
  45.  
  46. /**********************************************************************/
  47. /*  netarpme
  48. *   send an arp to my address.  arpinterpret will notice any response.
  49. *   Checks for adapters which receive their own broadcast packets.
  50. */
  51. netarpme(s)
  52.     char *s;
  53.     {
  54.     if (EtherNet) {    
  55.         reqarp(s);
  56.     }
  57.     return(0);
  58. }
  59.  
  60.  
  61. /************************************************************************/
  62. /*  dlayersend
  63. *
  64. *  usage:   err = dlayersend(ptr,size)
  65. *      err = 0 for successful, non-zero error code otherwise
  66. *      ptr is to a dlayer packet header
  67. *      size is the number of bytes total
  68. *
  69. *  This particular dlayer routine is for Ethernet.  It will have to be
  70. *  replaced for any other dlayer.
  71. *
  72. *  Assumes that dlayer-dependent initializations were made in protinit.c
  73. *  to help efficiency.
  74. *
  75. *  Ethernet addresses are resolved at higher levels because they will only
  76. *  need to be resolved once per logical connection, instead of once per
  77. *  packet.  Not too layer-like, but hopefully modular.
  78. *
  79. */
  80.  
  81. dlayersend(ptr, size)
  82. DLAYER *ptr;
  83. int size;
  84. {
  85.     if (EtherNet== 0)
  86.         return( ATdlayersend(ptr,size));
  87.     if (EtherNet==-2)
  88.         return( FNdlayersend(ptr,size));
  89.     else
  90.         return( ETdlayersend(ptr,size));
  91. }
  92.  
  93. /*
  94.  * ATdlayersend (ptr, size)
  95.  *  send size from ptr out the AT port
  96.  *    All outgoing conversions done here ! 
  97.  */
  98. ATdlayersend(ptr,size)            /*MAC: replaced */
  99. ARPKT *ptr;
  100. unsigned size;
  101. {
  102.     char *todata;
  103.     int *destptr, ptype, tsize;
  104.     char wds[14], header[18];
  105.     MPPParamBlock pb;
  106.     static ATARPKT arpbuffer;
  107.  
  108.     int err,lowadd,highadd;
  109.  
  110.     ptype = AIP;                                    /* Default to sending an IP Packet */
  111.  
  112.     pb.DDPsocket=IPSock;                            /* Sorry.... */
  113.     pb.DDPchecksumFlag=FALSE;                        /* who knows why, but we don't */
  114.     pb.DDPwdsPointer=wds;                            /* Here we go..... */
  115.     pb.MPPcsCode=246;                                /* We want to send a packet */
  116.  
  117.     todata=(char *)ptr + sizeof(DLAYER);            /* Point at the real data... */
  118.     destptr=&ptr->d.dest[0];                        /* Where is the destination ? */
  119.     tsize = size- sizeof(DLAYER);                    /* Get transmission size */
  120.  
  121.     if (!memcmp( destptr, bseed, 6))                    /* Watch for Broadcasts */
  122.         movmem(ATbseed, destptr, 4);
  123.  
  124.     if (ptr->d.type == EARP) {
  125.         ptype=AARP;                                        /* Arp packet */
  126.         arpbuffer.hrd = 3;                                /* ATalk */
  127.         arpbuffer.hln = 4;
  128.         arpbuffer.pln = 4;
  129.         arpbuffer.pro = ptr->pro;
  130.         arpbuffer.op  = ptr->op;
  131.         movmem( &ptr->sha[0], &arpbuffer.sha[0], 4);    /* Move addresses */
  132.         movmem( &ptr->spa[0], &arpbuffer.spa[0], 4);
  133.         movmem( &ptr->tha[0], &arpbuffer.tha[0], 4);
  134.         movmem( &ptr->tpa[0], &arpbuffer.tpa[0], 4);
  135.         todata= &arpbuffer;
  136.         todata+= sizeof(ATdlayer);                        /* New address to go from */
  137.         tsize = sizeof(ATARPKT)- sizeof(ATdlayer);
  138.         }
  139.         
  140. #ifdef MPW
  141.     BuildDDPwds( wds, header, todata, *((AddrBlock *)destptr), ptype, tsize );
  142. #else
  143.     highadd= *destptr++;                                /* Get the address */
  144.     lowadd= *destptr++;
  145.  
  146.     BuildDDPwds( wds, todata, tempptr, lowadd, highadd, ptype , tsize );
  147. #endif
  148.  
  149.     err=PWriteDDP( &pb, FALSE);
  150.     if (err!=0) putln("DDPW Error = %d");
  151. #ifdef SENDNOTICE    
  152.     else putln("send");
  153. #endif
  154.     return(err);
  155.     
  156. }
  157.  
  158. /**********************************************************************/
  159. /*   demux
  160. *      find the packets in the buffer, determine their lowest level
  161. *  packet type and call the correct interpretation routines
  162. *
  163. *  the 'all' parameter tells demux whether it should attempt to empty
  164. *  the input packet buffer or return after the first packet is dealt with.
  165. *
  166. *  returns the number of packets demuxed
  167. */
  168.  
  169. demux(all)
  170. int all;
  171. {
  172.     if (EtherNet==0)
  173.         return( ATdemux(all));
  174.     if (EtherNet==-2)
  175.         return( FNdemux(all));
  176.     else
  177.         return( ETdemux(all));
  178.  
  179. }
  180.  
  181.  
  182. ATdemux(all)
  183. int all;
  184. {
  185.     unsigned getcode;
  186.     int nmuxed,sizered, count;
  187.     ATARPKT *firstlook;
  188.     static ARPKT arpbuffer;
  189.     nmuxed = 0;
  190.  
  191.     do {                                    /* while all flag is on */
  192.  
  193.         if (bufinfo.bufbig > 0) {
  194.  
  195.             nmuxed++;
  196.             firstlook = (ATARPKT *)(bufinfo.bufread);     /*MAC where packet is */
  197.  
  198.             getcode = firstlook->d.type;            /* where does it belong? */
  199.             count = firstlook->d.count;
  200.             movmem( firstlook->d.dest, arpbuffer.d.dest,4);
  201.             movmem( firstlook->d.me,   arpbuffer.d.me  ,4);
  202.             
  203.             switch (getcode) {                    /* what to do with it? */
  204.                 case AARP:
  205.                     arpbuffer.d.type=EARP;                            /* Arp packet */
  206.                     arpbuffer.hrd = 1;                                /* Enet */
  207.                     arpbuffer.hln = 6;
  208.                     arpbuffer.pln = 4;
  209.                     arpbuffer.pro = firstlook->pro;
  210.                     arpbuffer.op  = firstlook->op;
  211.                     movmem( &firstlook->sha[0], &arpbuffer.sha[0], 4);        /* Move addresses */
  212.                     movmem( &firstlook->spa[0], &arpbuffer.spa[0], 4);
  213.                     movmem( &firstlook->tha[0], &arpbuffer.tha[0], 4);
  214.                     movmem( &firstlook->tpa[0], &arpbuffer.tpa[0], 4);
  215. #ifdef OLDM
  216.                     putln("AARP");
  217. #endif
  218.                     arpinterpret(&arpbuffer);    /* handle ARP packet */
  219.                     break;
  220.                 case AIP:
  221.                     arpbuffer.d.type=EIP;
  222.                     movmem( &arpbuffer, firstlook, sizeof(DLAYER) );
  223.                     ipinterpret(firstlook);
  224.                     break;
  225.                 default:
  226.                     break;
  227.             }
  228.  
  229.             sizered = count + sizeof(DLAYER);
  230.             bufinfo.bufbig-=sizered;
  231.             bufinfo.bufread+=(sizered+1) & 0xFFFE;
  232.  
  233.             if (bufinfo.bufbig<0) bufinfo.bufbig=0;
  234.             if (bufinfo.bufread>bufinfo.bufend) bufinfo.bufread=bufinfo.buforg;
  235.  
  236.         }
  237.         else 
  238.             all = 0;
  239.  
  240.  
  241.     } while (all);            /* should we look for more to deal with? */
  242.  
  243.  
  244.     return(nmuxed);          /* no packets anymore */
  245.  
  246. }
  247.  
  248. /*****************************************************************
  249.  *     E  T  H  E  R  N  E  T   /   E  T  H  E  R  T  A  L  K    *
  250.  *****************************************************************/
  251.  
  252. struct ETHERWDSstruct {
  253.     short len1;
  254.     DLAYER *ptr1;
  255.     short len2;
  256.     DLAYER *ptr2;
  257.     short term;
  258.     } EWDS;
  259.  
  260. ETdlayersend(ptr,size)            /*MAC: replaced */
  261. DLAYER *ptr;
  262. unsigned size;
  263. {
  264.     EtParam ET;
  265.     int err;
  266.  
  267.     EWDS.len1=size;                    /* Could it would it in a WDS? */
  268.     EWDS.ptr1=ptr;
  269.     EWDS.len2=0;
  270.  
  271.     ET.address = &EWDS;
  272.  
  273.     err=ETcall( &ET, EWrite);
  274.     if (err!=0) putln("ETW Error = %d");
  275. #ifdef SENDNOTICE    
  276.     else putln("send");
  277. #endif
  278.     return(err);
  279.     
  280. }
  281.  
  282. /**********************************************************************/
  283. /*   ETdemux    - Ethernet Version of DEMUX routine
  284. *
  285. *  returns the number of packets demuxed
  286. */
  287.  
  288. ETdemux(all)
  289.     int all;
  290.     {
  291.     unsigned getcode;
  292.     int nmuxed,sizered;
  293.     typedef struct peek {
  294.         unsigned short count;
  295.         unsigned char dest[DADDLEN],
  296.             me[DADDLEN];
  297.         unsigned short type;
  298.     } Dpeek;
  299.  
  300.     Dpeek *firstlook;
  301.  
  302.     nmuxed = 0;
  303.  
  304.     do {                                    /* while all flag is on */
  305.  
  306.         if (bufinfo.bufbig > 0) { /*MAC*/
  307.  
  308.             nmuxed++;
  309.             firstlook = (Dpeek *)(bufinfo.bufread);     /*MAC where packet is */
  310.  
  311.             getcode = firstlook->type;            /* where does it belong? */
  312.  
  313.             switch (getcode) {                    /* what to do with it? */
  314.                 case EARP:
  315.                 case ERARP:
  316.                     arpinterpret(firstlook->dest);    /* handle [R]ARP packet */
  317.                     break;
  318.                 case EIP: 
  319.                     ipinterpret(firstlook->dest);    /* handle IP packet */
  320.                     break;
  321.                 default:
  322.                     break;
  323.             }
  324.  
  325.             sizered=(firstlook->count+sizeof(Dpeek));
  326.             bufinfo.bufbig-=sizered;
  327.             
  328. #ifdef DEBUGONLY
  329.             {
  330.             char s[60];
  331.             sprintf(s,"buf: %d, pkt: %d",bufinfo.bufbig,sizered);
  332.             putln(s);
  333.             }
  334. #endif
  335.             bufinfo.bufread+=(sizered+1) & 0xFFFE;
  336.  
  337.             if (bufinfo.bufbig<0) bufinfo.bufbig=0;
  338.             if (bufinfo.bufread>bufinfo.bufend) bufinfo.bufread=bufinfo.buforg;
  339.             
  340.         }
  341.         else {
  342.  
  343.             if (ETdeafreset()) {
  344.                 /* clear deaf flag, buffer overrun */
  345.                 putln("warning: buffer deaf, restarting");
  346.                 }
  347.  
  348.             all = 0;
  349.             }
  350.  
  351.  
  352.     } while (all);            /* should we look for more to deal with? */
  353.  
  354.  
  355.     return(nmuxed);          /* no packets anymore */
  356.  
  357. }
  358.  
  359.  
  360.  
  361. initbuffer()
  362.     {
  363.     int i;
  364.  
  365.     raw=malloc(17000);        /* Added by GBP */
  366.     if (raw==NULL) return(-1);
  367.     bufinfo.bufpt = bufinfo.bufread = bufinfo.buforg
  368.                                          = raw;    /*  start at the beginning */
  369.  
  370.     bufinfo.bufend = raw+14500;            /* leave 2K breathing room, required */
  371.     bufinfo.buflim = 12000;             /* another 2K breathing room */
  372.     bufinfo.bufbig = 0;                    /* Added by GBP */
  373.  
  374.     if (EtherNet==0)
  375.         InitBufPtr(&bufinfo);
  376.     else if (EtherNet != -2) 
  377.         ETInitBufPtr( &bufinfo);
  378.     return(0);
  379. }
  380.  
  381. /***************************************************************************/
  382. /* dlayerinit
  383. *  Do machine dependent initializations of whatever hardware we have
  384. *  (happens to be 3com ethernet board here ) 
  385. */
  386. dlayerinit()
  387. {
  388.  
  389.     if (initbuffer())
  390.         return(-1);
  391.     if (EtherNet==0) 
  392.         return(atopen());
  393.     if (EtherNet==-2)
  394.         return(FNopen());
  395.     else
  396.         return(ETopen(EtherNet));    /* Pass the Type along */
  397. }
  398.  
  399. dlayershut()
  400. {
  401.     if (EtherNet==0)
  402.         return(atclose());
  403.     if (EtherNet==-2)
  404.         return(FNclose());
  405.     else
  406.         return(ETclose());
  407. }
  408.  
  409.