home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / pfkerm.zip / PFKERM.DOC < prev    next >
Text File  |  1997-07-04  |  12KB  |  264 lines

  1.  
  2.                            Kermit in Pygmy
  3.  
  4.      A Simple Implementation of the Kermit Protocol in Pygmy Forth
  5.  
  6.                            by Frank Sergeant
  7.  
  8. Introduction
  9.  
  10.      Kermit is not only the name of Henson Associates Inc's
  11. famous frog but also the name of an extensive and complex
  12. file transfer/remote computer access application written by
  13. Columbia University and/or volunteers (as I understand it)
  14. and sold and distributed by Columbia University [1].  The
  15. name Kermit is also used to refer to the family of file
  16. transfer protocols used in Columbia University's Kermit and
  17. by many modem programs.  As I use the term Kermit throughout
  18. the rest of this article, it refers to the Kermit protocols,
  19. particularly the simplest form implemented here in Pygmy
  20. Forth.
  21.  
  22.      My Forth applications sometime need to transfer files
  23. with other systems via modem.  I felt I needed to implement
  24. one or more of the following protocols: XMODEM, YMODEM,
  25. Kermit, ZMODEM.  The basic XMODEM protocol doesn't have much
  26. respect these days, as it does not transmit exact file
  27. lengths, but rounds up to the nearest block size (128
  28. bytes).  Enhanced versions of XMODEM, such as YMODEM
  29. overcome this.  Kermit is well thought of and widely
  30. available.  ZMODEM is usually considered to be the best
  31. although there seem to be arguments between the kermit and
  32. ZMODEM camps as to which is the better, faster protocol
  33. under various circumstances.  I, personally, found the
  34. ZMODEM arguments more persuasive, but I don't really care
  35. about minor differences.  I wanted something that worked
  36. reasonably well and was fairly easy to implement.  Looking
  37. over the Kermit and ZMODEM protocols, I decided that a
  38. simple form of Kermit would be slightly easier to program.
  39. Later, I hope to implement ZMODEM as well.
  40.  
  41. Why Write My Own
  42.  
  43.      You may well ask why we insist on writing our own
  44. version instead of using an existing product.  Indeed, we
  45. have experimented some with other products and have been
  46. severely disappointed.  For example, we have had a lot of
  47. trouble with PCAnywhere trying to get it configured
  48. correctly under DOS.  On some client systems, we were unable
  49. to configure it by changing the configuration file in the
  50. directory PCAnywhere would be run from.  Instead, we had to
  51. resort to a Rube Goldberg (an extremely cumbersome) approach
  52. to get the configuration to "take."  We had to modify the
  53. batch file temporarily so it would not call the PCAnywhere
  54. script, run the application and let it shell out to
  55. PCAnywere, change and save the settings, and again modify
  56. the batch file so it would run the script.  We never found
  57. just why we could configure it in some offices without going
  58. through this tedious process but not in most, but it was
  59. enough to make us hate PCAnywhere.
  60.  
  61.      Further, while the modem scripts would run under DOS,
  62. PCAnywhere was too slow.  Also, we could not make the DOS
  63. versions of the scripts run under the Windows 95 version of
  64. PCAnywhere.  Also, it was not free and we needed to fool with
  65. getting each client to purchase PCAnywhere.  Similarly, the
  66. use of Columbia University's Kermit product would have
  67. required each customer to purchase a license for it (yes, it
  68. is NOT free).  Ditto for the Oman Technologies ZMODEM
  69. product.  So, to hell with the third-party products.  We
  70. will just write our own, have full control of it, simpler
  71. installation, and a faster user interface.  Were this
  72. running under Unix/Linux, we would have used the built-in,
  73. freely distributable zmodem.
  74.  
  75. The Kermit Protocol
  76.  
  77.      My main guide for the Kermit protocol was _C
  78. Programmer's Guide to Serial Communications_ by Joe Campbell
  79. [2].  Note, there are some errors in Campbell's examples.
  80.  
  81.      Files are transferred in Kermit by exchanging "frames"
  82. between the sender and the receiver.  Every frame begins
  83. with an SOH (start of header) character and ends with a
  84. carriage return.  Between them are the following five
  85. fields: length, sequence, type, data, checksum.  Each
  86. field except for the data field is a single character in
  87. printable form.  The process of converting a number, say
  88. for the length field, into a character, is called
  89. "character-ization" and is done by adding the value of
  90. the space character to the number.  Thus, the number zero
  91. becomes $20 and prints as a space.  The number thirty-seven
  92. (i.e. $25) becomes $45 and prints as the letter "E" and so
  93. forth.  I use the words CHAR and UNCHAR to convert between
  94. the two formats.  Note this requirement of fitting numbers
  95. into a single byte as a printable character limits the
  96. numbers to the range zero to 94 and limits the size of
  97. frames that can be transmitted.  Various extensions to the
  98. basic Kermit protocol allow longer frames, but not the
  99. simple form described here.
  100.  
  101.      The frame types used are S to initiate a file transfer
  102. session, F to send a file name, D to send a data frame,
  103. Z to indicate end of file, B to indicate end of
  104. transmission, E to indicate a fatal error, A to send file
  105. attributes (we do not use this one), Y to ack a frame, and
  106. N to nak a frame.  No single bytes are exchanged between the
  107. sender and receiver, only whole frames.  The length field
  108. indicates the number of bytes to follow the length field up
  109. to and including the checksum field, but not the carriage
  110. return.
  111.  
  112.      To transfer a file, the sender waits for an N-frame (a
  113. negative acknowledgement), then sends an S-frame to tell the
  114. receiver what values it wants to use for various protocol
  115. parameters, such as maximum frame length, the repeat
  116. character, the escape character, etc.  The receiver then
  117. sends a Y-frame (an acknowledgement) with its preferred
  118. values for these parameters.  The word COMPROMISE takes the
  119. more conservative value for each of the parameters.  This
  120. single exchange of frames sets the protocol parameters for
  121. the session.  In the S-frame and its Y-frame, the values of
  122. the parameters are sent in a fixed-order in the frames' data
  123. fields.
  124.  
  125.      Kermit has two major ways of converting bytes before
  126. transmitting them.  One is the "character-ization" already
  127. mentioned, for converting numbers.  The other is
  128. "controlification" to flip a bit in a control character to
  129. turn it into a printable character.  Numbers, as such, are
  130. expected in the length and sequence and checksum fields, but
  131. never in the data field (except during the initial S and Y
  132. frames that establish the protocol parameters).  Kermit
  133. would allow for transmitting 7-bit data bytes by escaping,
  134. with an ampersand, each byte with a high bit set and then
  135. clearing that high bit, but we do not do this.  We assume
  136. the availability of an 8-bit channel.  Kermit does insist,
  137. though, on escaping control characters (with "#") and at
  138. least some implementations insist on compressing data by
  139. run-length encoding repeated bytes.  "#" and "~" characters,
  140. when appearing in the data as themselves, must be escaped.
  141. Thus a single "#" would appear in the data field as "##".
  142.  
  143.      To keep things simple, we never compress the data when
  144. we are sending a file.  Unfortunately, not all
  145. implementations of Kermit respect the request not to use
  146. repeat counts.  Therefore, we must be prepared to handle
  147. compressed data when receiving a file.  (Since our main
  148. purpose in using Kermit is to transmit zip files, it doesn't
  149. look like we would gain much speed by compressing the data
  150. we send.)
  151.  
  152.      Kermit allows various types of checksums, ranging from
  153. a single byte to three bytes.  Our main use will be to
  154. transmit zip'd files, with the zip file itself providing an
  155. additional level of data integrity verification with its
  156. 32-bit CRC, therefore, I am content to use a 1-byte
  157. checksum, the simplest form Kermit allows.
  158.  
  159. The V-frame
  160.  
  161.      We invent a special input frame and assign it type V.
  162. A V-frame is never actually sent.  Instead, whenever a
  163. timeout occurs, KSER-IN terminates (_and_ terminates its
  164. caller GETFRAME) and returns a V-frame.  This way, a timeout
  165. is not a special case, but just an ordinary "frame."
  166.  
  167. What Else Can Go Wrong
  168.  
  169.      In addition to a timeout, a frame with a bad checksum
  170. might be received.  In this case, we send an N-frame to
  171. alert the sender so it will try again.  If we are receiving
  172. and a frame is lost, the V-frame alerts us to the missing
  173. frame and we again send an N-frame to request a repeat.
  174. When sending, if a frame is lost or its Y-frame is lost,
  175. either a timeout or receipt of an N-frame alerts us to the
  176. need to resend that frame.  When we receive a duplicate
  177. frame (perhaps because our Y-frame never reached the
  178. sender), we basically ignore it.  However, we do acknowledge
  179. it so the sender will be free to continue.  The progress of
  180. a file transfer is indicated on-screen by the printing of a
  181. dot for each frame transferred.  Our code will wait forever
  182. attempting a transfer unless it is cancelled by the user or
  183. unless it receives an E-frame indicating a fatal error.
  184.  
  185. The Environment
  186.  
  187.      This implementation expects the SER-IN, SER-OUT and
  188. related words that allow transmitting and receiving to and
  189. from the serial port connected to a modem.  "The PC Serial
  190. Port in Forth" [3] describes one approach to handling the
  191. serial port on a PC using interrupts.
  192.  
  193.      My current approach does not use interrupts.  Instead,
  194. it uses the new multi-tasking ability of the upcoming Pygmy
  195. version 1.5.  The serial input is serviced by a separate
  196. task, thus greatly simplifying the serial port code.
  197.  
  198.      The Kermit code presented here should work regardless
  199. of which form of serial handling is used.
  200.  
  201. How to Transfer Files
  202.  
  203.      Assuming the modem connection has been made and the
  204. other end is expecting to send or receive the file using
  205. Kermit, type " filename.zip" SEND to send a file, or type
  206. RECEIVE to receive a file.  The RECEIVE routine is capable
  207. of receiving multiple files.  The SEND routine transmits a
  208. single file.  Since Pygmy 1.5 accepts instructions from
  209. the command line, you could type
  210.  
  211.            PYGMY " filename.zip" SEND BYE
  212.  
  213. on the DOS command line or in a batch file, etc.  In our
  214. application, we have a menu and a terminal mode with PgUp
  215. and PgDn keys invoking the SEND and RECEIVE routines.  You
  216. can see a clue as to how we use this in the definition of
  217. KSER-IN on block 12007 where a user abort of the file
  218. transfer executes the DEFER'd word MYMENU to put the user
  219. back at the application menu.
  220.  
  221. Conclusion
  222.  
  223.      This code has been used successfully at a number of
  224. client sites, but, remember, it uses only a very basic
  225. version of the Kermit protocol.  It very well could have
  226. problems transferring between certain sites.  If you try it
  227. out, please let me know your results.  For your downloading
  228. convenience, I have zip'd the Kermit source and shadow
  229. blocks, along with a copy of this article, and a preliminary
  230. version of PYGMY.COM that contains the multi-tasking version
  231. of the serial port words, and placed this on my web site as
  232. http://www.eskimo.com/~pygmy/forth/kermit.zip.
  233.  
  234. References
  235.  
  236.     [1] "Kermit: A File-transfer Protocol for Universities,"
  237. parts 1 and 2, Frank da Cruz and Bill Catchings, June and
  238. July, 1984, Byte magazine.
  239.  
  240.     [2] _C Programmer's Guide to Serial Communications_, Joe
  241. Campbell, Howard W. Sams & Company, 1987, ISBN
  242. 0-672-22584-0, pp 98-113.  Note, there are some errors in
  243. Campbell's examples.
  244.  
  245.     [3] "XT Corner: The PC Serial Port in Forth," Frank
  246. Sergeant, _The Computer Journal_, issue 79, Fall 1996,
  247. pp 5-10.  My web page, http://www.eskimo.com/~pygmy contains
  248. a link to TCJ's web page.
  249.  
  250.  
  251. Author's Bio
  252.  
  253.      Frank Sergeant, on the "thirty year plan," received his
  254. Master of Science in Computer Science degree from Southwest
  255. Texas State University.  The degree and his 4.0 GPA were
  256. hard won.  His thesis was entitled "Calculating the Crust of
  257. a Set of Points in C++" and has nothing to do with Forth
  258. except to help illustrate that computational geometry is
  259. easier in Forth than in C++.  He can be reached at
  260. pygmy@pobox.com.
  261.  
  262.  
  263.                            END
  264.