home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / ip / ppp / mac / macppp2.0.1-src.hqx / MacPPP 2.0.1 src / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-19  |  6.3 KB  |  289 lines

  1. /*
  2.  *  misc.c  - misc utility routines
  3.  *
  4.  * Copyright 1992-1993 Merit Network, Inc. and The Regents of the
  5.  *  University of Michigan.  Usage of this source code is restricted
  6.  *  to non-profit, non-commercial purposes.  The source is provided
  7.  *  "as-is", without warranty.
  8.  *
  9.  * Warning! Some of these routines assume a BIG-ENDIAN architecture
  10.  *     (i.e. the 68K) and are thus machine specific.
  11.  *
  12.  * Some of these routines were derived from similar functions in
  13.  *  KA9Q which are Copyright Phil Karn.  The code in these routines
  14.  *  have been substantially modified for the sake of efficiency.
  15.  */
  16.  
  17. #include "ppp.h"
  18.  
  19. /* define LOG in ppp.h to compine in logging code */
  20. #ifdef LOG    
  21. void log(LapInfo *lap, int rtn, int state, int code, int prot)
  22. {
  23.     *(lap->logp)++ = rtn;
  24.     *(lap->logp)++ = state;
  25.     *(lap->logp)++ = code;
  26.     *(lap->logp)++ = prot;
  27.     if (lap->logp > &(lap->logend))
  28.         lap->logp = &(lap->log[0]);
  29. }
  30. #endif
  31.  
  32. void AppendStr(unsigned char *dst, unsigned char *src)
  33. {
  34. unsigned char    dl, sl;
  35.  
  36.     dl = *dst;
  37.     sl = *src++;
  38.     *dst++ += sl;
  39.     BlockMove(src, dst + dl, sl);
  40. }
  41.  
  42. struct bufheader *
  43. getbuffer ()
  44. {
  45.     LapInfo        *lap;
  46.     short        savsr;
  47.     int            i;
  48.     struct bufheader    *bufptr;
  49.  
  50.     lap = GetLAPPtr();
  51.     savsr = set_sr(0x2100);        /* Disable timer ints */
  52.     bufptr = lap->buflist;
  53.     if (bufptr != nil) {
  54.         lap->buflist = (struct bufheader *) bufptr->dataptr;
  55.         bufptr->dataptr = ((unsigned char *) bufptr) + BUFOFFSET;
  56.         bufptr->length = 0;
  57.     } else {
  58.         PPP_DEBUG_CHECKS("\pOut of buffers!");
  59.         lap->OutofBuffers++;
  60.     }
  61.  
  62.     set_sr(savsr);            /* Re-enable ints */
  63.     return (bufptr);
  64. }
  65.  
  66. /*  routine to put a buffer back on the free list */
  67. void
  68. release( struct bufheader *bufptr )
  69. {
  70.     LapInfo        *lap;
  71.     short        savsr;
  72.  
  73.     if (bufptr == nil)
  74.         return;            /* already released */
  75.     lap = GetLAPPtr();
  76.     savsr = set_sr(0x2100);        /* Disable timer ints */
  77.     bufptr->dataptr = (unsigned char *) lap->buflist;
  78.     lap->buflist = bufptr;
  79.     set_sr(savsr);                /* restore ints */
  80. }
  81.  
  82. void
  83. makeroom(struct bufheader *bufptr, b_16 room)
  84. {
  85.     bufptr->length += room;
  86.     bufptr->dataptr -= room;
  87. }
  88.  
  89. /* yank off the desired number of byte from buffer
  90.    Return number of bytes yanked
  91.  */
  92. b_16
  93. yankbuf(register struct bufheader *bufptr, b_8 *yankbuf, b_16 count)
  94. {
  95.     register b_16 yanked;
  96.     
  97.     yanked = count > bufptr->length ? bufptr->length : count;
  98.     BlockMove(bufptr->dataptr, yankbuf, (long) yanked);
  99.     bufptr->dataptr += yanked;
  100.     bufptr->length -= yanked;
  101.     return (yanked);
  102. }
  103. /* Yank a 32-bit integer from a buffer.
  104.  * 0 is returned on error, but it cannot be distinguished from a good call.
  105.  */
  106. b_32
  107. yank32(struct bufheader *bufptr)
  108. {
  109.     b_32    rvalue;
  110.  
  111.     if (yankbuf(bufptr, (b_8 *) &rvalue, 4) < 4)
  112.         return 0;
  113.     return rvalue;
  114. }
  115. /* Yank a 16-bit integer in host order from buffer in network byte order.
  116.  * Return -1 on error
  117.  */
  118. long
  119. yank16(struct bufheader *bufptr)
  120. {
  121.     b_16    rvalue;
  122.  
  123.     if (yankbuf(bufptr, (b_8 *) &rvalue, 2) < 2)
  124.         return -1;
  125.     return (unsigned long) rvalue;
  126. }
  127.  
  128. /* yank a single byte from buffer, return -1 on error */
  129. short
  130. yankbyte(struct bufheader *bufptr)
  131. {
  132.     if  ( bufptr->length == 0)     /* no data */
  133.         return -1;
  134.     bufptr->length--;        /* decrement length */
  135.     return (*bufptr->dataptr++);
  136. }
  137.  
  138. /* 
  139.  *  getipheader() - pull IP header off a packet
  140.  */
  141. short
  142. getipheader(struct ipheader *header, struct bufheader *bufptr)
  143. {
  144.     short headlen, optlen;
  145.     
  146.     yankbuf(bufptr, (b_8 *) header, IPHLEN);
  147.     headlen = ( header->version & 0x0f ) << 2;
  148.     if ( (optlen = headlen - IPHLEN) > 0 )
  149.         yankbuf(bufptr, (b_8 *) header->options, optlen); /* copy options, if any */
  150.     return headlen;
  151. }
  152.  
  153. /*
  154.  * putipheader() - put an IP header on a packet
  155.  */
  156. void
  157. putipheader(struct ipheader *header, struct bufheader *bufptr)
  158. {
  159.     b_16 headlen;
  160.     
  161.     headlen = ( header->version & 0x0f) << 2;
  162.     bufptr->dataptr -= headlen;
  163.     bufptr->length += headlen;
  164.     BlockMove(header, bufptr->dataptr, headlen);
  165. }
  166.  
  167. /* 
  168.  *  gettcpheader() - pull TCP header off a packet
  169.  */
  170. short
  171. gettcpheader(struct tcpheader *header, struct bufheader *bufptr)
  172. {
  173.     short headlen, optlen;
  174.     
  175.     yankbuf(bufptr, (b_8 *) header, TCPHLEN);    /* copy the data */
  176.     headlen = ( header->offset & 0xf0 ) >> 2;
  177.     if ( (optlen = headlen - TCPHLEN) > 0 )
  178.         yankbuf(bufptr, header->options, optlen); /* copy options, if any */
  179.     return headlen;
  180. }
  181.  
  182. /*
  183.  * puttcpheader() - put an TCP header on a packet
  184.  */
  185. void
  186. puttcpheader(struct tcpheader *header, struct bufheader *bufptr)
  187. {
  188.     short headlen;
  189.     
  190.     headlen = ( header->offset & 0xf0 ) >> 2;
  191.     bufptr->dataptr -= headlen;
  192.     bufptr->length += headlen;
  193.     BlockMove(header, bufptr->dataptr, headlen);
  194. }
  195.  
  196. /*
  197.  *  tcp_window_fix() - window size changer to improve MacTCP performance
  198.  *
  199.  * Below we define the max window size to allow.  2920 corresponds to
  200.  * 2 full-size seqments and seems to be a good number to use
  201.  */
  202. #define MAXWIN 2920
  203. void
  204. tcp_window_fix(LapInfo *lap, struct bufheader *bufptr)
  205. {
  206. register b_16 windiff, winsize, checksum, winmax;
  207. b_8    *tcpptr;
  208.  
  209.     winmax = lap->prefdata.max_window;
  210.  
  211.     /* Check if TCP and not part of a fragment */
  212.     if (*(bufptr->dataptr + IP_PROTO_OFFSET) == TCP_PROTOCOL &&
  213.              (get16(bufptr->dataptr + IP_OFFSET_OFFSET) & 0x1fff) == 0){
  214.         /* get TCP header */
  215.         tcpptr = bufptr->dataptr + ((*bufptr->dataptr & 0x0f) << 2) + 14L;
  216.         winsize = get16(tcpptr);
  217.         if (winsize > winmax) {    /* check if window is too big */
  218.             windiff = winsize - winmax;
  219.             tcpptr = put16(tcpptr, winmax);
  220.             checksum = ~get16(tcpptr);    /* need to fix the checksum */
  221.             if ( windiff >= checksum )
  222.                 checksum--;
  223.             checksum -= windiff;
  224.             put16(tcpptr, ~checksum);
  225.         }
  226.     }
  227. }
  228.  
  229. /* simple/stupid byte compare routine */
  230. /* return 0 if equal, -1 if not */
  231. short
  232. bytecmp(b_8 *s1, b_8 *s2, short n)
  233. {
  234.     for ( ; n>0 ; n--) {
  235.         if (*s1++ != *s2++)
  236.             return -1;
  237.     }
  238.     return 0;
  239. }
  240.  
  241. /* Put a long in host order into a char array in network order */
  242. b_8 *
  243. put32(b_8 *cp, b_32 x)
  244. {
  245.     b_8 *ptr = (b_8 *) &x;
  246.     
  247.     *cp++ = *ptr++;
  248.     *cp++ = *ptr++;
  249.     *cp++ = *ptr++;
  250.     *cp++ = *ptr;
  251.     return cp;
  252. }
  253. b_32
  254. get32(b_8 *cp)
  255. {
  256.     b_32 rval;
  257.     b_8 *ptr = (b_8 *) &rval;
  258.  
  259.     *ptr++ = *cp++;
  260.     *ptr++ = *cp++;    
  261.     *ptr++ = *cp++;
  262.     *ptr = *cp;
  263.  
  264.     return rval;
  265. }
  266. #pragma options(honor_register) /* Make Think C honor registers */
  267. /* Put a short in host order into a char array in network order */
  268. b_8 *
  269. put16(register b_8 *cp, b_16 x)
  270. {
  271.     register b_8 *ptr =  (b_8 *) &x;
  272.  
  273.     *cp++ = *ptr++;
  274.     *cp++ = *ptr;
  275.  
  276.     return cp;
  277. }
  278. b_16
  279. get16(register b_8 *cp)
  280. {
  281.     b_16 rval;
  282.     register b_8 *ptr =  (b_8 *) &rval;
  283.  
  284.     *ptr++ = *cp++;
  285.     *ptr = *cp;
  286.  
  287.     return rval;
  288. }
  289.