Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
7.3 KiB

  1. #ifndef _VJSLIP_
  2. #define _VJSLIP_
  3. /*
  4. * Copyright (c) 1989 Regents of the University of California.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms are
  8. * permitted provided that the above copyright notice and this
  9. * paragraph are duplicated in all such forms and that any
  10. * documentation, advertising materials, and other materials
  11. * related to such distribution and use acknowledge that the
  12. * software was developed by the University of California,
  13. * Berkeley. The name of the University may not be used to
  14. * endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS
  17. * OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE
  18. * IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A
  19. * PARTICULAR PURPOSE.
  20. */
  21. // A.1 Definitions and State Data
  22. #define MAX_VJ_STATES 16 /* must be >2 and <255 */
  23. #define MAX_HDR 128 /* max TCP+IP hdr length (by protocol def) */
  24. //
  25. // NT is little endian, so we follow these rules
  26. //
  27. #if (defined(_M_IX86) && (_MSC_FULL_VER > 13009037)) || ((defined(_M_AMD64) || defined(_M_IA64)) && (_MSC_FULL_VER > 13009175))
  28. #define ntohs(x) _byteswap_ushort((USHORT)(x))
  29. #define ntohl(x) _byteswap_ulong((ULONG)(x))
  30. #else
  31. #define ntohs(x) (USHORT)( ((x) >> 8) + (((x) & 0xFF) << 8) )
  32. #define ntohl(x) (ULONG) ( ((x) >> 24) + (((x) & 0xFF0000) >> 8) +\
  33. (((x) & 0xFF00) << 8) + (((x) & 0xFF) << 24) )
  34. #endif
  35. #define htons(x) ntohs(x)
  36. #define htonl(x) ntohl(x)
  37. /* packet types */
  38. #define TYPE_IP 0x40
  39. #define TYPE_UNCOMPRESSED_TCP 0x70
  40. #define TYPE_COMPRESSED_TCP 0x80
  41. #define TYPE_ERROR 0x00
  42. /* this is not a type that ever appears on
  43. * the wire. The receive framer uses it to
  44. * tell the decompressor there was a packet
  45. * transmission error. */
  46. /*
  47. * Bits in first octet of compressed packet
  48. */
  49. /* flag bits for what changed in a packet */
  50. #define NEW_C 0x40
  51. #define NEW_I 0x20
  52. #define TCP_PUSH_BIT 0x10
  53. #define NEW_S 0x08
  54. #define NEW_A 0x04
  55. #define NEW_W 0x02
  56. #define NEW_U 0x01
  57. /* reserved, special-case values of above */
  58. #define SPECIAL_I (NEW_S|NEW_W|NEW_U) /* echoed interactive traffic */
  59. #define SPECIAL_D (NEW_S|NEW_A|NEW_W|NEW_U) /* unidirectional data */
  60. #define SPECIALS_MASK (NEW_S|NEW_A|NEW_W|NEW_U)
  61. /*
  62. * "state" data for each active tcp conversation on the wire. This is
  63. * basically a copy of the entire IP/TCP header from the last packet together
  64. * with a small identifier the transmit & receive ends of the line use to
  65. * locate saved header.
  66. */
  67. struct cstate {
  68. struct cstate *cs_next; /* next most recently used cstate (xmit only) */
  69. USHORT cs_hlen; /* size of hdr (receive only) */
  70. UCHAR cs_id; /* connection # associated with this state */
  71. UCHAR cs_filler;
  72. union {
  73. UCHAR hdr[MAX_HDR];
  74. struct ip_v4 csu_ip; /* ip/tcp hdr from most recent packet */
  75. } slcs_u;
  76. };
  77. #define cs_ip slcs_u.csu_ip
  78. #define cs_hdr slcs_u.csu_hdr
  79. /*
  80. * all the state data for one serial line (we need one of these per line).
  81. */
  82. typedef struct slcompress slcompress;
  83. struct slcompress {
  84. struct cstate *last_cs; /* most recently used tstate */
  85. UCHAR last_recv; /* last rcvd conn. id */
  86. UCHAR last_xmit; /* last sent conn. id */
  87. USHORT flags;
  88. UCHAR MaxStates;
  89. //
  90. // Some Statistics
  91. //
  92. ULONG OutPackets;
  93. ULONG OutCompressed;
  94. ULONG OutSearches;
  95. ULONG OutMisses;
  96. ULONG InUncompressed;
  97. ULONG InCompressed;
  98. ULONG InErrors;
  99. ULONG InTossed;
  100. struct cstate tstate[MAX_VJ_STATES]; /* xmit connection states */
  101. struct cstate rstate[MAX_VJ_STATES]; /* receive connection states */
  102. };
  103. struct mbuf {
  104. PUCHAR m_off; // pointer to start of data
  105. UINT m_len; // length of data
  106. };
  107. #define mtod(m,t) ((t)(m->m_off))
  108. /* flag values */
  109. #define SLF_TOSS 1 /* tossing rcvd frames because of input err */
  110. /*
  111. * The following macros are used to encode and decode numbers. They all
  112. * assume that `cp' points to a buffer where the next byte encoded (decoded)
  113. * is to be stored (retrieved). Since the decode routines do arithmetic,
  114. * they have to convert from and to network byte order.
  115. */
  116. /*
  117. * ENCODE encodes a number that is known to be non-zero. ENCODEZ checks for
  118. * zero (zero has to be encoded in the long, 3 byte form).
  119. */
  120. #define ENCODE(n) { \
  121. if ((USHORT)(n) >= 256) { \
  122. *cp++ = 0; \
  123. cp[1] = (UCHAR)(n); \
  124. cp[0] = (UCHAR)((n) >> 8); \
  125. cp += 2; \
  126. } else { \
  127. *cp++ = (UCHAR)(n); \
  128. } \
  129. }
  130. #define ENCODEZ(n) { \
  131. if ((USHORT)(n) >= 256 || (USHORT)(n) == 0) { \
  132. *cp++ = 0; \
  133. cp[1] = (UCHAR)(n); \
  134. cp[0] = (UCHAR)((n) >> 8); \
  135. cp += 2; \
  136. } else { \
  137. *cp++ = (UCHAR)(n); \
  138. } \
  139. }
  140. /*
  141. * DECODEL takes the (compressed) change at byte cp and adds it to the
  142. * current value of packet field 'f' (which must be a 4-byte (long) integer
  143. * in network byte order). DECODES does the same for a 2-byte (short) field.
  144. * DECODEU takes the change at cp and stuffs it into the (short) field f.
  145. * 'cp' is updated to point to the next field in the compressed header.
  146. */
  147. #define DECODEL(f) { \
  148. ULONG _x_ = ntohl(f); \
  149. if (*cp == 0) {\
  150. _x_ += ((cp[1] << 8) + cp[2]); \
  151. (f) = htonl(_x_); \
  152. cp += 3; \
  153. } else { \
  154. _x_ += *cp; \
  155. (f) = htonl(_x_); \
  156. cp++; \
  157. } \
  158. }
  159. #define DECODES(f) { \
  160. USHORT _x_= ntohs(f); \
  161. if (*cp == 0) {\
  162. _x_ += ((cp[1] << 8) + cp[2]); \
  163. (f) = htons(_x_); \
  164. cp += 3; \
  165. } else { \
  166. _x_ += *cp; \
  167. (f) = htons(_x_); \
  168. cp++; \
  169. } \
  170. }
  171. #define DECODEU(f) { \
  172. USHORT _x_; \
  173. if (*cp == 0) {\
  174. _x_=(cp[1] << 8) + cp[2]; \
  175. (f) = htons(_x_); \
  176. cp += 3; \
  177. } else { \
  178. _x_=*cp; \
  179. (f) = htons(_x_); \
  180. cp++; \
  181. } \
  182. }
  183. typedef UCHAR UNALIGNED * PUUCHAR;
  184. UCHAR
  185. sl_compress_tcp(
  186. PUUCHAR UNALIGNED *m_off, // Frame start (points to IP header)
  187. PULONG m_len, // Length of entire frame
  188. PULONG precomph_len, // Length of tcp/ip header pre-comp
  189. PULONG postcomph_len, // Length of tcp/ip header post-comp
  190. struct slcompress *comp, // Compression struct for this link
  191. ULONG compress_cid); // Compress connection id boolean
  192. //LONG
  193. //sl_uncompress_tcp(
  194. // PUUCHAR UNALIGNED *bufp,
  195. // LONG len,
  196. // UCHAR type,
  197. // struct slcompress *comp);
  198. LONG
  199. sl_uncompress_tcp(
  200. PUUCHAR UNALIGNED *InBuffer,
  201. PLONG InLength,
  202. UCHAR UNALIGNED *OutBuffer,
  203. PLONG OutLength,
  204. UCHAR type,
  205. struct slcompress *comp
  206. );
  207. NDIS_STATUS
  208. sl_compress_init(
  209. struct slcompress **comp,
  210. UCHAR MaxStates);
  211. VOID
  212. sl_compress_terminate(
  213. struct slcompress **comp
  214. );
  215. #endif // _VJSLIP_