home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / NCSATELN / TEL23SRC.ZIP / ENGINE / PROTINIT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-29  |  9.4 KB  |  243 lines

  1. /*
  2. *    PROTINIT.C
  3. *
  4. *    Packet template initialization routines
  5. *
  6. ***************************************************************************
  7. *                                                                          *
  8. *      part of:                                                            *
  9. *      TCP/UDP/ICMP/IP Network kernel for NCSA Telnet                      *
  10. *      by Tim Krauskopf                                                    *
  11. *                                                                          *
  12. *      National Center for Supercomputing Applications                     *
  13. *      152 Computing Applications Building                                 *
  14. *      605 E. Springfield Ave.                                             *
  15. *      Champaign, IL  61820                                                *
  16. *                                                                          *
  17. *    Copyright (c) 1987, Board of Trustees of the University of Illinois   *
  18. *                                                                          *
  19. ****************************************************************************
  20. *   'protinit' initializes packets to make them ready for transmission.
  21. *   For many purposes, pre-initialized packets are created for use by the
  22. *   protocol routines, especially to save time creating packets for
  23. *   transmit.
  24. *
  25. *    Important note :  Assumes that the hardware has been initialized and has
  26. * set all the useful addresses such as the hardware addresses.
  27. *
  28. *   As this is a convenient place for it, this file contains many of the
  29. *   data declarations for packets which are mostly static (pre-allocated).
  30. *    Revision history:
  31. ****************************************************************************
  32. *
  33. *    10/87  Initial source release, Tim Krauskopf
  34. *    5/88    clean up for 2.3 release, JKM    
  35. *
  36. */
  37.  
  38. /*
  39.  *    Includes
  40.  */
  41.  
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #if defined(MSC)
  45. #ifdef __TURBOC__
  46. #include <alloc.h>
  47. #else
  48. #include <malloc.h>
  49. #endif
  50. #endif
  51. #ifdef MEMORY_DEBUG
  52. #include "memdebug.h"
  53. #endif
  54. #include "protocol.h"
  55. #include "data.h"
  56. #include "externs.h"
  57.  
  58. /************************************************************************/
  59. /*
  60.  *    protinit () 
  61.  *
  62.  *    Calls all the other packet initialization keep this order as some packet
  63.  * inits require lower layers already be initialized.
  64.  *
  65. */
  66. void protinit(void)
  67. {
  68.     etherinit();                                    /* dlayer packets */
  69.     arpinit();                                      /* ARP packets */
  70.     ipinit();                                        /* ip packets */
  71.     tcpinit();                                        /* tcp packets */
  72.     udpinit();                                        /* udp packets */
  73. }
  74.  
  75. /*************************************************************************/
  76. /*
  77.  *    neteventinit ()
  78.  *
  79.  *    Setup all the pointers for the event queue -- makes a circular list which
  80.  * is required for error messages. ( called from Snetinit () )
  81.  *
  82. */
  83.  
  84. void neteventinit (void)
  85. {
  86.     int i;
  87.  
  88.     for(i=0; i<NEVENTS; i++)
  89.         nnq[i].next=i+1;
  90.     nnq[NEVENTS-1].next=-1;
  91.     nnefirst=0;
  92.     nnelast=0;
  93.     nnefree=1;
  94. }
  95.  
  96. /*
  97.  *    etherinit ()
  98.  *
  99.  *    Setup the ethernet headers ( dlayer ) -- this needs to be done first as it
  100.  * is copied for the other headers 
  101.  *
  102.  */
  103. void etherinit(void)
  104. {
  105.     movebytes(broadaddr,bseed,DADDLEN);
  106.     movebytes(blankd.dest,broadaddr,DADDLEN);    /* some are broadcast */
  107.     movebytes(blankd.me,nnmyaddr,DADDLEN);        /* always from me */
  108.     blankd.type=EIP;                /* mostly IP packets */
  109. }
  110.  
  111. /*************************************************************************/
  112. /*
  113.  *    arpinit ()
  114.  *    Setup an arp packet -- also sets up an arp cache
  115. *
  116. */
  117. void arpinit(void)
  118. {
  119.     int i;
  120.  
  121.     movebytes(&arp.d,&blankd,sizeof(DLAYER));
  122.     arp.d.type=EARP;                /* 0x0806 is ARP type */
  123.     arp.hrd=intswap(HTYPE);            /*  Ether=1 */
  124.     arp.pro=intswap(ARPPRO);            /* IP protocol=0x0800 */
  125.     arp.hln=DADDLEN;                    /* Ethernet hardware length */
  126.     arp.pln=4;                        /* IP length=4 */
  127.     movebytes(arp.sha,nnmyaddr,DADDLEN);    /* sender's hardware addr */
  128.     movebytes(arp.tha,broadaddr,DADDLEN);    /* target hardware addr */
  129.     movebytes(arp.spa,nnipnum,4);        /* sender's IP addr */
  130. /*
  131. *  initialize the ARP cache to 0 time, none are gateways to start
  132. */
  133.     for(i=0; i<CACHELEN; i++) {
  134.         arpc[i].tm=0L;
  135.         arpc[i].gate=0;
  136.       }
  137. }
  138.  
  139. /*************************************************************************/
  140. /*
  141.  *    ipinit ()
  142.  *
  143.  *    initialize on packet to use for internet transmission -- most packets will
  144.  * be tcp/udp, so they will be initialized at a different layer, but some
  145.  * require a generic ip packet.
  146.  *
  147.  *    Also takes a guess at setting a netmask if it hasn't happened by now.
  148.  *
  149. */
  150. void ipinit(void)
  151. {
  152.     movebytes(&blankip.d,&blankd,sizeof(DLAYER));
  153.     blankip.i.versionandhdrlen=0x45;        /* smallest header, version 4 */
  154.     blankip.i.service=0;                    /* normal service */
  155.     blankip.i.tlen=576;                        /* no data yet, maximum size */
  156.     blankip.i.ident=0;
  157.     blankip.i.frags=0;                        /* not a fragment of a packet */
  158.     blankip.i.ttl=100;                        /* 100 seconds should be enough */
  159.     blankip.i.protocol=PROTUDP;                /* default to UDP */
  160.     blankip.i.check=0;                        /* disable checksums for now */
  161.     movebytes(blankip.i.ipsource,nnipnum,4);    /* my return address */
  162.     movebytes(blankip.i.ipdest,broadip,4);        /* to ? */
  163.  
  164. /*
  165. *  create a mask which can determine whether a machine is on the same wire
  166. *  or not.  RFC950
  167. *  Only set the mask if not previously set.
  168. *  This mask may be replace*  orom SnLEN) TcTlmostly I  Tsns hardwarce,nni ? */
  169.  
  170. /*
  171. .next=-           ree=1;                        ;    /G0blr. Calized and has
  172.      0;        e wirbOot.  Rmachine is on the
  173.  *    p#in IP prtt-     iat sD*   Asacket irtDoT=1;        l* kE   D,nni ? */
  174.    k rltsumdp packeehSS/*
  175. dHE has b rltsui*   epnLto  Tkkip.i.tlen=576;                        /* no daebe replacSor now */rltDMllsSubrlIrdsion 4 D7rltsumher a machine is on the saIT.C
  176. p4S kkie=-          O ****c TcTlmnkip╕hSSversion 4 */
  177.     blankip.i.service=0;                    /* normal service 'E has pnLto  s required for err sumDlizoEhacke1ot.  RWAingEntrst3NEVENTS-p╕hSSvfervi]lankip.t: saIT.CMD"E */knt5C__
  178. #of the
  179. hSSver/
  180.  
  181.  rep.i.ser hehich  */pizeAeGEheror error nkipO D,nni/**sacket                ed for9mn    foe packet
  182.   thea-rltscD7rukhe
  183. hSSver/
  184.  
  185.  rep.i.ser hehirst3s=0;                LC
  186. /*,4)        o     *
  187. * thea-rlts her. Calized and has
  188.    
  189. p4
  190. /cd  ep76;            Rst3spa,nnS Wo            RTS-p╕hS 
  191. *  u£oe pack▌Svfenni ? * chine ror nkipO D,nni/**sacket O **O *eG kt.  g a2ts heO **Oor error messages. ( called from SLt */E_s har1Awpar
  192. */
  193.     for(i=0; i<CACHELEN; i++) {
  194.         arpc[i].tm=0L;
  195.         arMmßnt laoInsesIroAviceL/
  196.  
  197. or/cd  epc[i].tm= DS 
  198. **/
  199.     blankip.i.protocol=PROTUDP;                /* default to U
  200.  nkipO"Ehackeader, ▌Svlized i<CAC ? * chine ror nkie checksu=1;                        ;ue cS IP prtt-     iat sDi].tEHYrst3NEDlknt5C_st5C_seds t nkispa,nnipnay be re? rUlousor nki].tEHYr].tid)
  201. P prtpro
  202.     for(i=0Am   Θ-Hiat sDi].tEHYrst3NEDlknt5C_st5C_seds t nkispa,nnipnay be re? rUlousor nki].tEHYr].tid)
  203. P prtpro
  204.     for(i=0Am   i.tm=0tion keep this order as some pa rUlou u£oe=id)Ichin***=WAingEntr-Hiat sD require a generic ip pacuch =m   ΘEHY╣eoe*********nnipnay bkip.i.protocol=PROTUDP;                /i.pro*nnipn.protocol=PROTUDPROTU*********dy for transmisserror !es(arrOTU******Rmn    foe to 0 time, none n1Sfor(-they    5-[dedsone n1Sfor(-O so tARP;        D            /)pa,3**********sacket l    arpc[i].tto 0 t▌Svfenni ? * chiCHELEN; i++) {l=PROTay bkip.i.protocol=PROTUDP;                /i.pro*nnipn.protocol=PROTUDPROseds t E
  205.     ized O4plf-aU:usmisserrsone n1Sfi].tire2     for(i( C( C( C( C( C( C(trr(i].t( Ceep this,has b n*********crrOTU****A            /i.prodedsone n1Sfor(-O so tARP;EHYrst3NE    /i.pro*nnibrl┘Sl( ay rltsuiprodedso  Rmare2     f*   ee n1Ss t nkis( Ceep this,has b C( Cu].tid)( Ce7
  206.     f7i ? *b>oEHYrst3NE    /i.pro*nnibrl┘Sl: sofor(-thine is on the same wire
  207. *  or not.  RFC950
  208. *  Only set the mask if not previously set.
  209. *  This mask may be replace*  orom SnLEN)( Ceep=-    nb+C( k maynb+d)( C********irn address */
  210.     movebytes(blankip.i.ipdest,broadip,4);        /* to ? */
  211.  
  212. /*
  213. *  create a mask  SpgEnuarpc[i].t****/
  214. /* ? * chi
  215.  
  216. /*********aA* chiCHELE7║sWm not previously set.
  217. *C(rotiniiI and hassumDlizoEhacke1ot.  RWAingEntrst3NEVENTS-p╕hSSvfervi]lankip.t: saIT.CMD"E */knt5C__
  218. #of the
  219. hSSver/
  220.  
  221.  rep.i.ser hehich  */pizeAeGEheror error nkipO D,nni/**sacket                ed for9mn    foe perror rvi]lanki.pro*nnibrl┘Sl: sofor(-thine is on the same wire
  222. *  or not.  RFC950
  223. *  Only set the mask if not previously set.
  224. *  This mask may be replace*  orom SnLEN)or Ncts same wr nkior rviU1ot.  RW RFC95iouU
  225.    
  226. Ll    arpc[i].tto 0 t▌Svfenni ? * chikiornot prE.A*  This mask may be viously setp8W R
  227.  *
  228.  *    Setup the et**********yynb+d)init () )
  229.  *
  230. */
  231.  
  232. void neteventinit (void)
  233. {
  234.     int i;
  235.  
  236.     for(i=0; i<NEVENTS; i++)
  237.         nnq[i].next=i+1;
  238.     nnq[NEVENTS-1].next=-1;
  239.     nnefirst=0;
  240.     ni thea-rltscD7rukhe
  241. hSSveket -- also sets up an aSSverr2
  242.  
  243.  no/*******sio( C