home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / protocol / iso / 1057 < prev    next >
Encoding:
Text File  |  1992-08-26  |  25.7 KB  |  633 lines

  1. Newsgroups: comp.protocols.iso
  2. Path: sparky!uunet!gatech!darwin.sura.net!Sirius.dfn.de!fauern!fauna!mskuhn
  3. From: mskuhn@immd4.informatik.uni-erlangen.de (Markus Kuhn)
  4. Subject: Re: Info on Async HDLC wanted
  5. References: <1992Aug25.221533.27286@bnr.ca>
  6. Message-ID: <BtLJK6.5Ks@immd4.informatik.uni-erlangen.de>
  7. Organization: CSD., University of Erlangen, Germany
  8. Date: Wed, 26 Aug 1992 15:12:06 GMT
  9. Keywords: HDLC, Async, datalink, RS-232 
  10. Lines: 621
  11.  
  12. rbach@bnr.ca (Richard Bach) writes:
  13.  
  14. >I'm looking for a standard asychronous RS-232 data link protocol 
  15. >for use in a transaction processing application on a point
  16. >to point dedicated link.  Error detection and auto-recovery
  17. >mechanisms are important.
  18.  
  19. >I have heard that there is an asynchronous HDLC protocol as
  20. >defined in ISO 3309-1984/PDAD1 - "Addemdum 1: Start/Stop 
  21. >Transmission".  I have thus far been unable to obtain
  22. >a copy of the ISO document but an still trying.  
  23.  
  24. You might also be interessted in ISO 7776 which defines LAPB, a HDLC
  25. subset with error detection and auto-recovery. CCITT X.25 contains
  26. also an LAPB description, but I feel that the ISO doc is much easier
  27. to understand.
  28.  
  29. ISO 3309 only defines the framing. The following text contains some
  30. information about it (the 7-bit transparency option is still
  31. Draft Amendment 3, they simply make 8 septets out of 7 octets :-):
  32.  
  33. ----------------------------------------------------------------------
  34.  
  35. Guidelines for adding HDLC Framing to Serial Port Drivers
  36. ---------------------------------------------------------
  37.  
  38. Markus Kuhn, 1992-06-21             [DRAFT VERSION!]
  39.  
  40.  
  41. Summary
  42. -------
  43.  
  44. This  text  is intended to encourage developers  of  serial  line 
  45. drivers to include support for the asynchronous HDLC framing mode 
  46. in  their software. Distribution of [the final version!] of  this 
  47. text is unlimited.
  48.  
  49.  
  50. Introduction
  51. ------------
  52.  
  53. Most  of today's asynchronous port drivers are  character  orien-
  54. tated. They provide the application programmer an interface  that 
  55. allows to send and receive single bytes or sequences of bytes and 
  56. to  modify port parameters like transmission speed,  parity  bits 
  57. and flow control.
  58.  
  59. Many application programs (e.g. file transfer tools, packet  net-
  60. work access daemons) need an error corrected link over which data 
  61. packets  can  be sent. Today, every  application  protocol  (e.g. 
  62. Kermit, Zmodem, SLIP, PPP) defines it's own packet error  correc-
  63. tion method.
  64.  
  65. Last  year,  the International Organization  for  Standardization 
  66. (ISO)  published a new version of the International Standard  ISO 
  67. 3309.  This  document defines a well designed  frame  format  for 
  68. serial  lines. The old version of ISO 3309 only defined a  synch-
  69. ronous  frame format which is today used in nearly every  profes-
  70. sional  data communication product, especially  those  supporting 
  71. the  protocols  V.42, X.25/LAPB, ISDN/LAPD, Ethernet  LLC,  SDLC, 
  72. synchronous PPP and many others. Now ISO extended the standard by 
  73. an asynchronous mode (they call it start/stop transmission) which 
  74. is  likely to become as popular in the world of cheap RS-232C  PC
  75. interfaces  as the synchronous version has already been the  last 
  76. decade in the professional world.
  77.  
  78. ISO  3309  is one part of a series of standards that  define  the 
  79. HDLC  (High-level data link control procedures) family of  proto-
  80. cols.  We will only discuss the framing level of HDLC here,  i.e. 
  81. the  checksum  algorithm and the method used to  mark  the  frame 
  82. boundaries.  In  addition the asynchronous  HDLC  framing  method 
  83. allows to avoid some characters on the line, if they are used for 
  84. other purposes (e.g. XON/XOFF) or not transmitted (e.g. on not 8-
  85. bit  transparent connections), where this is necessary and  still
  86. provides  a completely 8-bit transparent link. It seems to  be  a 
  87. good  idea to perform these tasks within the serial  line  driver 
  88. that  controls  the interface chip, because framing may  be  time 
  89. consuming  and  should  be implemented  efficiently  with  direct 
  90. access  to  the input/output FIFOs. We will not  deal  here  with 
  91. higher layer functions like packet sequence control, error  reco-
  92. very  and  timeout  handling,  but if you  want  to  implement  a 
  93. complete  error correction protocol, you might be  interested  in 
  94. ISO 7776 which defines the LAPB protocol, a HDLC subset that uses 
  95. ISO 3309 framing.
  96.  
  97.  
  98. Frame structure
  99. ---------------
  100.  
  101. In  the HDLC asynchronous framing mode, all transmissions are  in 
  102. the  form of frames. Each frame is a sequence of bytes with  each
  103. byte  consisting of 1 start bit (logical 0), 8 data  bits  trans-
  104. mitted with low order bit first (the usual order), no  additional 
  105. parity  bit  and  one stop bit (logical 1). Each  frame  has  the 
  106. following structure:
  107.  
  108.               +------+--------------+-----+------+
  109.               | Flag |     Data     | FCS | Flag |
  110.               +------+--------------+-----+------+
  111.  
  112. The  flag is the byte 01111110 = 7Eh = '~'. All frames start  and 
  113. end with this flag byte. A single flag byte may at the same  time 
  114. be used as the closing flag of a frame and as the opening flag of 
  115. the  next frame. The data field may be any sequence of  at  least 
  116. two bytes followed by a 2- or 4-byte CRC frame checking  sequence 
  117. (FCS).
  118.  
  119.  
  120. Transparency
  121. ------------
  122.  
  123. For  proper operation, the flag byte must not appear between  the 
  124. frame boundaries. Otherwise it would not be possible to determine 
  125. the  end of the frame correctly and thus the location of the  FCS
  126. and the length of the data sequence would be unclear. The  trans-
  127. mitter  shall  examine the frame content consisting of  the  data 
  128. byte sequence and the FCS and shall replace every appearance of a 
  129. byte that is not allowed between the flags with the byte 01111101 
  130. =  7Dh = '}' followed by the replaced byte with bit 6 (which  has 
  131. the value 32 = 20h) being complemented.
  132.  
  133. The receiver discards each occurence of 7Dh and complements bit 6 
  134. in  the following byte. If the byte following 7Dh is the  closing 
  135. flag  7Eh, the complete frame is considered invalid and shall  be 
  136. discarded.
  137.  
  138. ISO 3309 defines three alternative levels of transparency:
  139.  
  140.   - Basic  transparency: Only the bytes 7Dh and 7Eh are  replaced 
  141.     by  7Dh 5Dh and 7Dh 5Eh. This is the minimal required  trans-
  142.     parency.  Represented  as ASCII characters,  the  replacement
  143.     rules are:
  144.                             }    ->    }]
  145.                             ~    ->    }^
  146.  
  147.   - Flow-control transparency: In some environments, the  control 
  148.     characters DC1/XON and DC3/XOFF with and without bit 8 set to 
  149.     1  must not be used in a frame as they may confuse  the  flow 
  150.     control between the modem and the serial line driver. In  the 
  151.     flow-control transparency mode, the characters 11h, 13h,  91h 
  152.     and 93h or in binary format the bytes x00100x1 (where each  x 
  153.     may  be  either  0 or 1) are replaced in  addition  to  those
  154.     replaced in basic transparency mode.
  155.  
  156.   - Control-character  octet transparency: This mode  avoids  all 
  157.     non-printable  characters  that might cause problems  on  the 
  158.     line. The replaced characters are 00h to 1Fh, 7Fh to 9Fh  and 
  159.     FFh  or  in  binary format the bytes  x00xxxxx  and  x1111111
  160.     (where  each  x may be either 0 or 1) in  addition  to  those 
  161.     replaced in basic transparency mode.
  162.  
  163. [do to: 7-bit transparency mode, PDAM not available for me, yet]
  164.  
  165.  
  166. Frame Check Sequence
  167. --------------------
  168.  
  169. A 16-bit or a 32-bit FCS are available as options. With a  16-bit 
  170. FCS, at least 4 bytes (without transparency replacements) between
  171. the  flags  are necessary to form a valid frame, with  32-bit  at 
  172. least  6  bytes are required. Shorter frames and  frames  without 
  173. correct  FCS  are considered to be transmission  errors  and  are 
  174. discarded.  The  16-bit  FCS shall be  implemented  in  all  HDLC 
  175. drivers, the 32-bit FCS is a possible extension.
  176.  
  177. The  mathematical  details of the FCS are defined  in  ISO  3309. 
  178. Appendix A describes an efficient way to implement the FCS.
  179.  
  180.  
  181. Example Frames
  182. --------------
  183.  
  184. [...]
  185.  
  186.  
  187. Implementation
  188. --------------
  189.  
  190. The  following chapter gives only some hints which  functionality 
  191. should be supported by the serial line driver and how it might be 
  192. provided to an application programmer.
  193.  
  194. A  serial  line driver can be operated in two  modes.  In  normal 
  195. mode,  single  bytes  and/or sequences of bytes can  be  sent  or 
  196. received to and from a FIFO. The only thing the driver is looking 
  197. for  in the byte stream are perhaps XON/XOFF characters  if  this 
  198. has  been selected. The normal mode may also be necessary  before  
  199. HDLC operation for modem control or perhaps a login procedure.
  200.  
  201. In HDLC framing mode, the application program uses two functions. 
  202. One  is used to get the data byte sequence and it's length  of  a 
  203. completely  received  frame  with correct  checksum.  The  second 
  204. function  will send a frame with a given data byte  sequence.  If
  205. sending a complete frame is not possible at the moment because of 
  206. lack  of FIFO space, an appropriate error value will be  returned 
  207. and no single byte will be sent.
  208.  
  209. Normaly,  an  opening flag is only needed for  the  first  frame. 
  210. After  this,  the closing flag of the last frame is  already  the
  211. opening flag of the next frame. The risk that comes with  leaving 
  212. away the opening flag is that if the time between the two  frames 
  213. is  bigger than a few seconds, the chances get higher that  noise 
  214. bytes  are  received  that will make the next  frame  longer  and 
  215. invalid.  The solution is to send a single frame flag  each  time 
  216. after a few seconds silence on the line. This silence time  could 
  217. be made configurable by the application program. One second might 
  218. be a good default value. An alternative solution would be to send 
  219. a new opening flag with the next frame if the last flag has  been 
  220. transmitted more than a few seconds ago.
  221.  
  222. The  application  program might want to set the  following  para-
  223. meters  (apart from standard things like baud rate  and  XON/XOFF 
  224. usage):
  225.  
  226.     send FCS:              16 or 32 bit
  227.     receive FCS:           same as send direction or both
  228.     send transparency:     basic, flow-control or control
  229.     receive transparency:  basic, flow-control or control
  230.     send 7-bit transp.:    yes or no
  231.     receive 7-bit transp.: same as send direction or both
  232.  
  233. There  are  2*3*2=12 different frame formats  available.  One  of 
  234. these  has  to be selected for outgoing frames.  The  application 
  235. program has to negotiate with the remote station which format  is 
  236. used (depending on line characteristics). In order to allow nego-
  237. tiation, it should be possible to set the driver in a mode  where 
  238. all alternatives are accepted. If the CRC for received frames  is
  239. not  fixed to the same used for outgoing frames, the driver  will 
  240. first  check  whether the frame has a correct 16-bit FCS  and  if 
  241. this fails (and there are more than 6 bytes between the flags) if 
  242. it  is a correct 32-bit FCS. The same principle might be  applied 
  243. with  the 7-bit transparency: try 8-bit mode at first and if  the 
  244. FCS is (or both are) incorrect, try the 7-bit mode and check  the
  245. FCS again.
  246.  
  247. The three levels of receive character transparency define,  which 
  248. received  characters  are completely ignored. In basic  mode,  no 
  249. byte is ignored, and in the other modes those bytes that have  to 
  250. be  replaced in outgoing frames are discarded. If  XON/XOFF  flow 
  251. control is selected between the modem and the computer, at  least 
  252. the  flow-control transparency should be selected in both  direc-
  253. tions.  In this way, no flow control characters will be sent  and 
  254. the received ones are only used for flow control and not as  part 
  255. of a frame.
  256.  
  257. The  most flexible mode for initial handshaking between  applica-
  258. tion protocols might be:
  259.  
  260.     send FCS:              16 bit
  261.     receive FCS:           both
  262.     send transparency:     control
  263.     receive transparency:  basic
  264.     send 7-bit transp.:    yes
  265.     receive 7-bit transp.: both
  266.  
  267. This mode is very unefficient and the FCS and 7-bit  transparency 
  268. for received frames should be defined as soon as possible by  the 
  269. application  program.  It  might be useful,  if  the  application 
  270. program could determine the frame format (FCS and 7-bit  transpa-
  271. rency) of the last received frame.
  272.  
  273. All  this  provides  a very powerful  and  flexible  transparency 
  274. mechanism that will be succesful on nearly every possible connec-
  275. tion. Under special circumstances, it might even be useful if the 
  276. application  program could define with an array which bytes  have 
  277. to be replaced in addition to the three defined sets, but this is 
  278. not required by ISO 3309. Protocols like PPP support this option.
  279.  
  280. Some application programs might want to send their frame as  late 
  281. as possible, because they want to include the latest packet  ack-
  282. nowledge information for received frames or recent keystrokes  in 
  283. the  next frame. The frame should not be sent too  late,  because 
  284. otherwise not every millisecond of the connection would be  used. 
  285. So  the  application needs a way to estimate the  time  that  the 
  286. transmission FIFO will need to get empty or the actual FIFO size. 
  287. It  seems  to be a good compromise, if  the  application  program 
  288. sends the packet if the actual FIFO size is less than the  length 
  289. of the next packet or a fixed limit.
  290.  
  291. It  might also be useful for the application program to  get  the 
  292. time that has elapsed since the last flag byte has been received. 
  293. In this way, it might detect a broken connection by timeout.
  294.  
  295. It is suggested, that drivers support frames containing data byte
  296. sequences of at least 1040 Bytes. This will allow 1024 byte  user 
  297. protocol  data units plus a reasonable amount of header  informa-
  298. tion from higher layer protocols.
  299.  
  300.  
  301. Appendix A: Fast Frame Check Sequence (FCS) Implementation
  302. ----------------------------------------------------------
  303.  
  304. The  following algorithm for the 16 bit FCS has been  copied  and 
  305. slightly  modified  from RFC 1171. The author also  adapted  this 
  306. code  to  get the 32 bit routines in section c) and d).  The  FCS
  307. bytes  that have to be appended at the end of the frame  are  the 
  308. complement of the return value beginning with the low order byte. 
  309. [The 32 bit version has not been tested for compatibility yet!]
  310.  
  311. a) 16 bit FCS Computation Method
  312.  
  313. The following code provides a table lookup computation for calcu-
  314. lating the Frame Check Sequence as data arrives at the interface.  
  315. The table is created by the code in section b).
  316.  
  317. /*
  318.  * u16 represents an unsigned 16-bit number.  Adjust the
  319.  * typedef for your hardware.
  320.  */
  321. typedef unsigned short u16;
  322.  
  323.  
  324. /*
  325.  * FCS lookup table as calculated by the table generator in section b).
  326.  */
  327. static u16 fcstab16[256] = {
  328.    0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
  329.    0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
  330.    0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
  331.    0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
  332.    0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
  333.    0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
  334.    0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
  335.    0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
  336.    0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
  337.    0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
  338.    0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
  339.    0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
  340.    0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
  341.    0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
  342.    0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
  343.    0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
  344.    0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
  345.    0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
  346.    0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
  347.    0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
  348.    0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
  349.    0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
  350.    0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
  351.    0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
  352.    0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
  353.    0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
  354.    0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
  355.    0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
  356.    0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
  357.    0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
  358.    0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
  359.    0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
  360. };
  361.  
  362. #define INITFCS16      0xffff  /* Initial FCS value */
  363. #define GOODFCS16      0xf0b8  /* Good final FCS value */
  364.  
  365. /*
  366.  * Calculate a new fcs given the current fcs and the new data.
  367.  */
  368. u16 crc16(fcs16, cp, len)
  369.     register u16 fcs16;
  370.     register unsigned char *cp;
  371.     register int len;
  372. {
  373.     assert(sizeof (u16) == 2);
  374.     assert(((u16) -1) > ((u16) 0));
  375.     while (len--)
  376.         fcs16 = (fcs16 >> 8) ^ fcstab16[(fcs16 ^ *cp++) & 0xff];
  377.  
  378.     return (fcs16);
  379. }
  380.  
  381.  
  382. b) Fast 16 bit FCS table generator
  383.  
  384. The following code creates the lookup table used to calculate the 
  385. FCS.
  386.  
  387. /*
  388.  * Generate a FCS table for the HDLC FCS.
  389.  *
  390.  * Drew D. Perkins at Carnegie Mellon University.
  391.  *
  392.  * Code liberally borrowed from Mohsen Banan and D. Hugh Redelmeier.
  393.  */
  394.  
  395. /*
  396.  * The HDLC polynomial: x**0 + x**5 + x**12 + x**16 (0x8408).
  397.  */
  398. #define P       0x8408
  399.  
  400.  
  401. main()
  402. {
  403.     register unsigned int b, v;
  404.     register int i;
  405.  
  406.     printf("typedef unsigned short u16;\n");
  407.     printf("static u16 fcstab16[256] = {");
  408.     for (b = 0; ; ) {
  409.         if (b % 8 == 0)
  410.             printf("\n");
  411.  
  412.         v = b;
  413.         for (i = 8; i--; )
  414.             v = v & 1 ? (v >> 1) ^ P : v >> 1;
  415.  
  416.         printf("0x%04x", v & 0xFFFF);
  417.         if (++b == 256)
  418.             break;
  419.         printf(",");
  420.     }
  421.     printf("\n};\n");
  422. }
  423.  
  424.  
  425. c) 32 bit FCS Computation Method
  426.  
  427. /*
  428.  * u32 represents an unsigned 32-bit number.  Adjust the
  429.  * typedef for your hardware.
  430.  */
  431. typedef unsigned long u32;
  432.  
  433.  
  434. /*
  435.  * FCS lookup table as calculated by the table generator in section d).
  436.  */
  437. static u32 fcstab32[256] = {
  438.    0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
  439.    0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
  440.    0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
  441.    0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
  442.    0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
  443.    0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
  444.    0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
  445.    0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
  446.    0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
  447.    0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
  448.    0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
  449.    0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
  450.    0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
  451.    0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
  452.    0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
  453.    0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
  454.    0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
  455.    0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
  456.    0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
  457.    0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
  458.    0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
  459.    0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
  460.    0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
  461.    0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
  462.    0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
  463.    0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
  464.    0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
  465.    0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
  466.    0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
  467.    0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
  468.    0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
  469.    0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
  470.    0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
  471.    0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
  472.    0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
  473.    0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
  474.    0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
  475.    0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
  476.    0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
  477.    0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
  478.    0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
  479.    0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
  480.    0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
  481. };
  482.  
  483. #define INITFCS32      0xffffffff  /* Initial FCS value */
  484. #define GOODFCS32      0xdebb20e3  /* Good final FCS value */
  485.  
  486. /*
  487.  * Calculate a new fcs given the current fcs and the new data.
  488.  */
  489. u32 crc32(fcs32, cp, len)
  490.     register u32 fcs32;
  491.     register unsigned char *cp;
  492.     register int len;
  493. {
  494.     assert(sizeof (u32) == 4);
  495.     assert(((u32) -1) > ((u32) 0));
  496.     while (len--)
  497.         fcs32 = (fcs32 >> 8) ^ fcstab32[(fcs32 ^ *cp++) & 0xff];
  498.  
  499.     return (fcs32);
  500. }
  501.  
  502.  
  503. d) Fast 32 bit FCS table generator
  504.  
  505. The following code creates the lookup table used to calculate the 
  506. FCS.
  507.  
  508. /*
  509.  * Generate a FCS table for the HDLC 32 bit FCS.
  510.  *
  511.  * Markus Kuhn, University of Erlangen
  512.  *
  513.  * Code liberally borrowed from Mohsen Banan, D. Hugh Redelmeier
  514.  * and Drew D. Perkins
  515.  *
  516.  */
  517.  
  518. /*
  519.  * The HDLC polynomial: x**0 + x**1 + x**2 + x**4 + x**5 + x**7 + x**8 +
  520.  *                      x**10 + x**11 + x**12 + x**16 + x**22 + x**23 +
  521.  *                      x**26 + x**32 (0xedb88320).
  522.  */
  523. #define P       0xedb88320
  524.  
  525.  
  526. main()
  527. {
  528.     register unsigned int b;
  529.     register unsigned long v;
  530.     register int i;
  531.  
  532.     printf("typedef unsigned long u32;\n");
  533.     printf("static u32 fcstab32[256] = {");
  534.     for (b = 0; ; ) {
  535.         if (b % 6 == 0)
  536.         printf("\n");
  537.  
  538.         v = b;
  539.         for (i = 8; i--; )
  540.             v = v & 1 ? (v >> 1) ^ P : v >> 1;
  541.  
  542.         printf("0x%08lx", v & 0xFFFFFFFF);
  543.         if (++b == 256)
  544.             break;
  545.         printf(",");
  546.     }
  547.     printf("\n};\n");
  548. }
  549.  
  550.  
  551. Appendix B: More details about HDLC
  552. -----------------------------------
  553.  
  554. This  appendix gives you some additional information that is  not 
  555. required  to  implement  the HDLC framing mode but  might  be  of 
  556. interest for you.
  557.  
  558. Many protocols separate the data byte sequence between the  open-
  559. ing flag and the FCS in an address field, a control field and  an 
  560. information field. Some control frames don't have an  information 
  561. field. Normaly each of the address and control fields consists of
  562. one  byte. The address field is intended to define for  which  of 
  563. several stations connected to a line the frame has been sent.  On 
  564. simple  point-to-point  connections, the address field  is  often 
  565. used  to distinguish between several protocols or  carries  other 
  566. information. E.g. the LAPB protocol uses the addresses 01h,  03h, 
  567. 07h and 0Fh and the Internet PPP uses only FFh. The control  byte
  568. is used by the error correction protocols in order to distinguish 
  569. the  different command and responses defined by the protocol  and 
  570. the  information field carries the data transfered by  the  error 
  571. correction protocol for higher layers.
  572.  
  573. HDLC  framing  is also defined for synchronous  lines.  The  only 
  574. difference  to the asynchronous mode is that the start  and  stop 
  575. bits  are left away and that the transparency algorithm is  simp-
  576. ler. The sender will insert a 0 bit after five contiguous 1 bits. 
  577. In  this  way, the flag 01111110 will never appear  in  the  data 
  578. stream. The receiver examines the incoming bits for 5  contiguous
  579. 1 bits. If the next bit is a 0, then it will be discarded. If the 
  580. next bits are 1 and 0 then a frame flag has been detected and  if 
  581. the  next  bits are 1 and 1, then the whole frame  is  considered 
  582. invalid and discarded.
  583.  
  584. All HDLC procedures are defined in the standards ISO 3309,  4335,
  585. 7478, 7776, 7809, 8471 and 8885.
  586.  
  587. References
  588. ----------
  589.  
  590. - Information  processing systems -- Data communication --  High-
  591.   level  data link control procedures -- Frame structure,  Inter-
  592.   national Organization for Standardization, ISO 3309-1984(E).
  593.  
  594. - Information  processing systems -- Data communication --  High-
  595.   level data link control procedures -- Frame structure, Addendum
  596.   1:  Start/stop  transmission,  International  Organization  for 
  597.   Standardization, ISO 3309-1984/AD1(E).
  598.  
  599. - Information  processing systems -- Data communication --  High-
  600.   level  data link control procedures -- Frame structure,  Amend-
  601.   ment 2: Extended transparency options for start/stop  transmis-
  602.   sion, International Organization for Standardization, ISO 3309-
  603.   1991/Am2(E).
  604.  
  605.  
  606. Author
  607. ------
  608.  
  609. Markus Kuhn
  610. Schlehenweg 9
  611. W-8525 Uttenreuth
  612. Germany
  613.  
  614. Phone: +49 9131 52226
  615.  
  616. FAX: +49 9131 85-8503
  617.  
  618. Internet: mskuhn@immd4.informatik.uni-erlangen.de
  619.  
  620. Don't hesitate to contact me if you want to include HDLC  framing
  621. in  a free serial line driver and have any questions or  suggest-
  622. ions.
  623.  
  624. ----------------------------------------------------------------------
  625.  
  626. Markus
  627.  
  628. -- 
  629. Markus Kuhn, Computer Science student -=-=- University of Erlangen, Germany
  630. Internet: mskuhn@immd4.informatik.uni-erlangen.de  |  X.500 entry available
  631. -A distributed system is one in which the failure of a computer you didn't-
  632. -even know existed can render your own computer unusable. (Leslie Lamport)-
  633.