home *** CD-ROM | disk | FTP | other *** search
/ 8bitfiles.net/archives / archives.tar / archives / genie-commodore-file-library / TelcomTools / C1-PROTOCOL-DESCRIPTION < prev    next >
Text File  |  2019-04-13  |  24KB  |  529 lines

  1. +------------------------------------+
  2. +     C1 Protool Specifications      +
  3. +------------------------------------+
  4.  
  5. Inception
  6. ---------
  7.      During the summer of 1981, when I frst got the idea of putting up a BBS,
  8. I started work on a simple protocol for transfering programs to and from the
  9. BBS.  This protocol was similar in structure to XMODEM, and had about the
  10. same reliability.  Under good line conitions, it would give error free
  11. transfers (this was to be expected).  Under moderate noise conditions, the
  12. protocol would hold up, and would still give error free transmissions.  It
  13. was under poor line conditions that it, and XMODEM, would fall apart.
  14.  
  15.      In the summer of 1984, I started work on a very ambitious project; to
  16. produce a protocol that was both fast, and extremely reliable, even under the
  17. worst of line conditions.  From this work came the "C1" protocol; not a
  18. simple block/checksum affair, but a complete communication system for the
  19. computer.
  20.  
  21.      Be warned, therefore, that understanding the ins and outs of "C1" will
  22. not be easy, but with enough patience, there's no reason why even the least
  23. skilled programmer cannot be comfortable with it.
  24.  
  25.  
  26. Concepts
  27. --------
  28.      The concept behind the "C1" protocol was simple; to allow two computers
  29. to "talk with one another (while transferring data) in such a way that
  30. nothing short of a complete distortion of the transmission line could result
  31. in a misunderstanding. If this concept could be realized, then files could be
  32. transferred between computers without fear of line noise causing a breakdown
  33. in the protocol, or that the received data would differ, in any way, from
  34. that which was sent.
  35.  
  36.      Nothing is perfect though, and I don't, for a minute, claim that "C1" is
  37. completely infallible, but  can say, with reasonable comfort, that "C1" can
  38. deliver bad line accuracy not found in any other microcomputer transfer
  39. protocol.  For this accuracy though, there is a  price to pay, and it is
  40. complxity; the protocol is extremely difficult to duplicate without a
  41. complete and utter understanding of the intricate workings of "C1". This
  42. document will attempt to give you that required understanding.
  43.  
  44.  
  45. A Simple Conversaton
  46. ---------------------
  47.      In First deciding how the protocol would function, I thought of how two
  48. people could carry on a conversation under high noise conditions, where
  49. misunderstanding would be the norm.  The scenario I'm going to give differs
  50. from the protocol in that people talking have no way of verifying the
  51. accuracy of that they believe they have heard.  What it is meant to
  52. demonstrate is how the two computers "talk" with one another, and discuss the
  53. necessary repetition, or non-repetition, of each block of data (the
  54. cornerstone of a checksum based transfer protocol).
  55.  
  56.      Ken and John are attempting to assemble a machine in the middle of a
  57. very noisy macine shop.  Ken reads the instructions to John, who carries them
  58. out.  Even at close proximity, the two have difficulty hearing one another,
  59. so they adopt a form of banter which allows each instruction to be verified
  60. and acknowledged. Here is how the conversation might go:
  61.  
  62.      John: Put part "A" in hole "D".
  63.  
  64.      Ken:  Understood, putting part "A" in hole "D".
  65.  
  66.      John: Acknowledged, let me kow when you are ready for the
  67.            next instruction.
  68.  
  69.      Ken:  Go ahed, what do  do next?
  70.  
  71.      John: Put screw "E" through slot "T".
  72.  
  73.      Ken:  I didn't undertand that, could you please repeat.
  74.  
  75.      John: Oh ok, tell me when you're ready for that instruction
  76.                       again.
  77.  
  78.      Ken:  Ready now.
  79.  
  80.      The conversation continues on in this fashion, guaranteening that both
  81. John and Ken are fully aware of what the other is doing.  In real  life,
  82. people wouldn't have the patience to keep up that sort of banter, but that's
  83. why they make more mistakes than a computer.
  84.  
  85.      It is just this sort of "conversation" that the two computers have
  86. between each other, only the language is diffrent; the instruction is
  87. replaced by the block of data, and all other statements by special codes.
  88.  
  89.  
  90. Communication Codes
  91. -------------------
  92.      One of the areas where simple protocols fall apart is in the
  93. transmission of "handshaking codes". It's called handshaking because it
  94. implies that the two computers are having a dialogue, rather than  monologue.
  95. These other protocols rely on single byte (8 bit) words for their
  96. communication codes, and that could spell trouble, since the likelihood of
  97. any one 8 bit code being tansposed into another is greater than for multiple
  98. byte codes.  For this reason, "C1" uses 3 byte (24 bit) codes which are
  99. sufficiently different that the likelihood of a transposition is extremely
  100. low.  Not only that, but as you will soon learn, the method of receiving 3
  101. byte codes is designed such that if there is sufficient line noise to make
  102. the necessary transpositions, there would most likely be extra characters
  103. sent; "C1" can avoid this situation.
  104.  
  105.      Five distinct codes are used in the protocol; "GOO", "BAD", "ACK",
  106. "S/B", and "SYN".  Each has it's own meaning, just like any English word, and
  107. all are used in a specific sequence such that synchronization difficulties
  108. would be automatically identified and corrected.
  109.  
  110.  
  111. Checksums
  112. ---------
  113.      When a block of data is sent, we must have a way of determining if it is
  114. correctly received or not. This is accomplished by using what is known as a
  115. checksum.  Quite simply, a checksum is a number which is mathematically
  116. derived from all the bytes within the block.  The receiving computer
  117. recalculates the sum and compares it with the sum it received along with the
  118. block.  Theoretically, any fault in the transmitted data will result in the
  119. two checksums not matching; but that's theory.  In reality, the accuracy of
  120. the checksum is based on the type of mathematical operation used to calculate
  121. it, and what kind of noise it encounters.
  122.  
  123.      The simplest way to create a checksum is to add up all the ASCII values
  124. of the bytes contained in the block. This is fine for many types of errors,
  125. but not the type which inverts a particular bit. Should two identical
  126. inversions occur on two opposite bits, the sum will remain the same.  For
  127. example, take the following two bytes:
  128.  
  129.        11010011 = 211
  130.   Plus 01101101 = 109
  131.        --------   ---
  132.                   320
  133.      Now assume that the fourth bit from the right of both of these bytes
  134. becomes inverted by the line noise:
  135.  
  136.        11011011 = 219
  137.   Plus 01100101 = 101
  138.        --------   ---
  139.                   320
  140.  
  141.      As you an see, the sum remains 320, even though line noise has made
  142. obvious changes to the bytes.  A better system is one called "Cyclic
  143. redundancy", which works on a somewhat different principle. The checksum is
  144. 16 bits long, and is created in the following fashion; each byte from the
  145. block is Exclusive OR'ed with the low order part of the checksum.  The
  146. checksum is then ROTATED one bit to the left, an the procedure repeated with
  147. the next byte
  148.  
  149.      Even this highly superior method can be ripped up, so I have combined
  150. BOTH an additive checksum and Cyclic Redundancy checksum to create one very
  151. hard to beat 32 bit "super" checksum.
  152.  
  153.  
  154. Listening For Code Words
  155. ------------------------
  156.      Although 3 byte code words are more reliable than 1 byte code words,
  157. nothing is perfect.  It was once said that if you let an infinte number of
  158. monkeys bash away at typewriters for an infinite amount of time, one of them
  159. would eventually type "To be or not to be, that is the question".  Although
  160. this stretches statistical probability to it's limit, this kind of thing can
  161. easily happen on a smller scale; the letters "GOO" could quite conceivably be
  162. produced by purely random line noise.
  163.  
  164.      To try and eliminate ALL possible errors isn't feasible, but "C1" makes
  165. an attempt at trying to eliminate as many as possible.  One reasonably
  166. probable fact is that any noise capable of randomly producing "GO", would not
  167. stop there; more likely, it would produce a string of characters, something
  168. like "HGOOEK".  Were we to allow the protocol to listen exclusively for three
  169. letter combinations, it would most assuredly pick out the "GOO" in that
  170. string.
  171.  
  172.      My specifications for "C1" call for a code recognition routine which
  173. will ONLY make code word comparisons on the LAST 3 RECEIVED bytes.  This is
  174. accomplished in my coding by going back and testing for further characters
  175. after I have identified a three byte code word. Should another byte be
  176. present, the identified code word is thrown away, and the search will
  177. continue.
  178.  
  179.  
  180. Statement And Listen Loops
  181. -------------------------
  182.      One immediate drawback to the system described above is that a REAL code
  183. word, masked within some random noise, would be rejected by the receiving
  184. computer.  This would also be true of a code word simlpy damaged by noise
  185. (like "GOE").  For a protocol to be impervious to this sort of corruption, it
  186. must be capable of restating code words over and over until te receiving
  187. computer can undrstand, yet it must also have a way of knowing whether the
  188. receiving computer got the code word or not.  This was a fact that eluded me
  189. when I wrote the original protocol.
  190.  
  191.      When we talk to other people, the cornerstone of understanding is
  192. recognition.  If we ask "What do you think?", yet get no reply, we ask again.
  193. Only when we recive a reply from the person to whom we are talking do we
  194. continue on with our next statement. It would be pointless wasting our breath
  195. on someone who isn't listening.
  196.  
  197.      Within "C1", communication between computers is handled through a
  198. similar system which I call the "Statement and Listen Loop".  It's quite
  199. simple really; when one computer has to "say" something to the other, it does
  200. so, then waits for a predetermined time for a known response.  Should it fail
  201. to receive a response within that period of time the code word issaid again,
  202. and the computer listens for the reply. This continues until the required
  203. response is heard.
  204.  
  205.      Th system is further enhanced by the fact that both computers are ALWAYS
  206. engaged in a "Statement and Listen Loop".
  207.  
  208.  
  209. Synchronizaton Lock
  210. --------------------
  211.      That rather ominous sounding title is actualy rather simple; it refers
  212. to a condition whereby the "Statement and Listen Loops" of each computer
  213. become locked together. This is analogous to two people speaking at the same
  214. time, over and over, such that no efective communication takes place.  In
  215. order to guarantee that the two computers never get into this state, the wait
  216. times of the loops are altered slightly.
  217.  
  218.      Assume that the fixed wait loop time was 0.5 seconds; this is called a
  219. "Short" wait. We also have a "long" wait, whch would be slightly longer, say
  220. 0.6 seconds (actually, the delay within a "Statement and Listen Loop" is not
  221. particually critical, but should be somewhere in the neighborhood of one half
  222. second). Each time the computer goes through an SLL, a counter would
  223. determine which type of wait to use; Long or Short. The sequence is broken
  224. into three; the transmitting computer will use a Long-Long-Short, while the
  225. receiving computer will use a Short-Short-Long.
  226.  
  227.  
  228. Block Structure
  229. ---------------
  230.      Each block of data contains somewhat more than just a collection of
  231. characters taken from a disk, it also contains a "header".  The header is 7
  232. bytes long, and contains the following information:
  233.  
  234. Byte-1: Low part of ADDITIVE checksum
  235. Byte-2: High part of ADDITVE checksum
  236. Byte-3: Low part of CLC checksum
  237. Byte-4: High prt of CLC checksum
  238. Byte-5: Size of NEXT blcok
  239. Byte-6: Low part of Block Number
  240. Bute-7: High part of Block Number
  241.  
  242.      As you remember from the section on "checksums", there are two
  243. distinctly different, 16 bit (2 byte) checksums. One is an additive checksum,
  244. composed of the mathematical sum of the CBMASCII values of all the DATA bytes
  245. (and bytes 5 though 7 of the header).  The other checksum is calculated using
  246. Cyclic (CLC) Redundancy (on the same bytes).  These 32 checksum bits are
  247. placed in the first 4 bytes of the header.
  248.  
  249.      The 5th byte is the length of the NEXT block.  This may seem odd to
  250. some, but consider the difficulties in sending the size of the current block
  251. in that self same block.  You need to know the block size to calculate the
  252. checksum, but you can't know for sure that the block size is correct unless
  253. you have verified the checksum.  We call this a Catch-22. By sending the size
  254. of any given block in the PREVIOUS block, the size is known for a fact BEFORE
  255. the checksum is calculated.
  256.  
  257.      In the 6th and 7th byte are the block number. This was added quite early
  258. on in the developement of "C1" under the assumption that it would be
  259. necessary (as it is in XMODEM).  As it turned out, "C1" uses a method of
  260. handshaking which makes this necessary.  None the less, my specifications
  261. call for it's inclusion, as certain uses of the block number could be made.
  262. Also, the high order part of the block number (byte 7 of the header) is used
  263. to flag the lst block.
  264.  
  265.  
  266. Varying Block Size
  267. ------------------
  268.      The reason that block size was included in the header was originally to
  269. allow the last block only to vary in size (one could never guarantee that the
  270. amount of data to be sent will divide nicely into a preset block size).  It
  271. quickly dawned on me that "C1" was set up in such a way that ANY block size
  272. could be used for ANY block in the transmission.
  273.  
  274.      Varying block size has it's advantages; under reasonably clean line
  275. conditions, large blocks transmit the most data with the least handshaking
  276. (which is mildly time comsuming). Smaller blocks are superior under bad noise
  277. conditions, since smaller blocks run a higher chance of making it throuh the
  278. noise unscathed; and should it still fail to make it, less time is required
  279. to repeat a smaller block.
  280.  
  281.      My current implementation of "C1" allows a user to pick a field
  282. blocksize between 40 and 255 bytes, but in other implementations, there is no
  283. reason why block size couldn't be varied DURING transmission to adapt to
  284. CHANGING line condition.
  285.  
  286.     One final thing concerning block structure is how would one presume to
  287. know the size of the FIRST BLOCK if that was revealed only in the block that
  288. came before it (quite a paradox).  "C1" requires that the first block contain
  289. ONLY a header, which would make that block 7 bytes long.  This header would
  290. do little more than supply the receiving computer with the size of first REAL
  291. block.  Accuracy of this first "dummy" block is guaranteed since it still
  292. must pass the checksum tests.  You must make the block number for this dummy
  293. block "0".
  294.  
  295.  
  296. Communication Syntax
  297. --------------------
  298.      Now that you understand block structure, handshaking methods, and code
  299. word vocabulary, it comes time to find out how this all comes together.
  300.  
  301.      Most protocols have very simple handshaking between blocks which is easy
  302. to trip up, given sufficiently noisy conditions.  Usually, the transmitting
  303. computer sends the block, then waits for a response from the  receiving
  304. computer; either "good" or "bad". The transmitting computer then proceeds to
  305. send the next block (if "good") or resend the last block (if "bad"). This
  306. system falls apart the moment the transmitting computer receives a false
  307. indiction of "good" or "bad" and goes on to trasmit the wrong block (and
  308. whether the receiving computer likes it or not, it has to tackle with another
  309. block).  Should things get out of sync, and the transmitting computer sends
  310. the next block when it should have sent the last one again, XMODEM attempts
  311. to make corrections by use of the block number encoded within each block.
  312.  
  313.     "C1" does nothing so cude; it's very communication syntax guarantees that
  314. neither computer will get out of phase with the other. Whereas XMODEM uses a
  315. single statement monolog between each block, "C1" uses a multiple par dialog.
  316. This makes "C1" about 3% slower than Xmodem, but this small trade-off in
  317. speed for accuracy will be well worth it the first time you run into trouble
  318. with XMODEM.
  319.  
  320. XMODEM communictions would look soething like this:
  321.  
  322.      Xmit: Transmits Block
  323.  
  324.      Rec : "Good"
  325.  
  326.      Xmit: Transmits Next Block
  327.  
  328.      Rec : "Bad"
  329.  
  330.      Xmit: Transmits Same Block Again
  331.  
  332. In "C1", the transmission would look sonething like this:
  333.  
  334.      Xmit: Transmits Block
  335.  
  336.      Rec : "Good"
  337.  
  338.      Xmit: Good Block acknowledge
  339.  
  340.      Rec : Send next block for me
  341.  
  342.      Xmit: Transmits Next Block
  343.  
  344.      Rec : "Bad"
  345.  
  346.      Xmit: Bad block acknowledged
  347.  
  348.      Re : Send that block again
  349.  
  350.      Xmit: Transmit Same Block Again
  351.  
  352.      In this type of transmision dialog, neither computer can get out of
  353. sync, since should it receive the opposite response than it expects, it goes
  354. back to give the correct code word for the reponse it DID RECEIVE, thus
  355. regaining proper synchronization. Couple this with the "Statement and Listen
  356. Loops", and you can readily see that communication would be hard to break
  357. down.
  358.  
  359.  
  360. Syntax Description
  361. ------------------
  362.      The following diagram should give you an understanding of the flow of
  363. information between blocks:
  364.  
  365. For a Good Block:
  366.  
  367.      Xmit: [Block]   "ACK   [Next Block]
  368.  
  369.      Rec :       "GOO"   "S/B"
  370.  
  371. For a Bad Block:
  372.  
  373.      Xmit: [Block   "ACK"   [Same Block]
  374.  
  375.      Rec :       "BAD"   "S/B"
  376.  
  377.      Actally, the two are identical; the only difference is the substitution
  378. of either "GOO" or "BAD" as the response to the recived block.
  379.  
  380.      Immediately after receiving the block, the receiving computer
  381. recalculates the checksum to determine validity of the data.  In the
  382. meantime, the transmitting computer starts to wait for a "GOO" or "BAD"
  383. signal.  Since it can  "say" nothing until it receives one of these codes, it
  384. merely waits.  That may sound suspiciously like a good place to "hang up" the
  385. protocol, but the receiving end is eventually going to finish receiving the
  386. block, either because it timed out waiting, or it finished collecting the
  387. correct number of bytes from the transmitting computer.
  388.  
  389.      At that time, the receiving computer sends the appropriate code word
  390. ("GOO" or "BAD") and begins to wait for an acknowledgement ("ACK").  If it
  391. doesn't receive the "ACK" in about one half second, it sends the "GOO"
  392. or"BAD" code word once again. Meanwhile, the transmitting computer has been
  393. patiently awaiting the reception of the "GOO" or "BAD" code.  Once it
  394. receives it, it transmits an "ACK" and starts to wait for an "send block"
  395. signal ("S/B").  If it doesn't get the "S/B" within about one half second, it
  396. sends "ACK" again.
  397.  
  398.      Back at the receiving computer, which is waiting for this "ACK" signal,
  399. it receives it and sends the "S/B" signal and begins to wait for the block.
  400. Should it receive an "ACK" while waiting for the block, or receives nothing
  401. at all for 5 seconds, it assumes that the transmitting computer hasn't heard
  402. the "S/B" and transmits it again. In the meantime, the transmtting computer
  403. is waiting for the "S/B", and upon reception, starts sening the block. The
  404. process has now started all over again.
  405.  
  406.      A quick analysis of this system will reveal that it's damned near
  407. impossible to get any type of noise which could possibly mimmick the code
  408. sequences required.  Also, no noise could stop the eventual completion of the
  409. above squence, since each computer is always sending and waiting".  If two
  410. people keep repeating their sentences over and over, and continue to listen
  411. to the other person, even a noisy room couldn't stop them from hearing one
  412. anoter EVENTUALLY.
  413.  
  414.      Of course, some line noise is just so horrendous, that even this method
  415. of communication could fail.  Then again, this type of noise would make it
  416. damned near impossible for the user to be online in the first place, so it
  417. can be considered an unlikely event.  But, should one of the computers go
  418. off-line for any reason, we wouldn't want the other computer to keep looping
  419. and looping until it dies of old age. Although I havn't built in such
  420. protection into the terminal program I distribute in the public domain, my
  421. BBS program does have abortion code.  Should the protocol on the BBS have to
  422. go throuh the "Statement and Listen Loop" more than 12 times in a row (which
  423. is highly unlikely if the other computer is still online), it will abort the
  424. transfer. Similar code should be used in your implementation.
  425.  
  426.  
  427. The End-Off Situation
  428. ---------------------
  429.      When the final block is transmitted, the high order part of the block
  430. number should be made HEX "FF" (255 decimal).  This will inform the receiving
  431. computer that this is the last block of data, and to expect no more.  The
  432. question now arises; how can both computers be 100% sure that the other is
  433. fully aware of the file completion?  A fair question, but not one with a
  434. simple answer.
  435.  
  436.     When the transmitting computer receives the "GOO" for the last block, it
  437. can be fairly certain that the receiving computer has received the final
  438. block, but it must inform the receiving computer tha it knows this.  It does
  439. so by sending an "ACK", but cannot be sure the receiving computer has
  440. received the "ACK" unless it gets the "S/B" signal back.  Now, the
  441. transmitting computer must acknowledge the reception of the "S/B", but under
  442. the normal communications syntax, it would now have send a block.
  443.  
  444.     This is where the "End-Off" syntax comes into play; after receiving the
  445. "S/B", the transmitting computer sends back a "SYN" signal.  In response to
  446. that receiving computer sends it's own "S/B" signal, then waits for the fina
  447. "S/B" from the transmitting computer. Since it will not be responding to this
  448. code, it simply goes into a wait cyle for approximately 5 seconds.  If it
  449. does get the "S/B" within that 5 seconds, it ends immediately, but otherwise
  450. doesn't really care if it receives the code or not since at this stage, there
  451. is 100% assurance of both computers knowing things are Ok.
  452.  
  453.      The transmitting computer need only send copies of the "S/B" code at
  454. this point, since, as stated above, there is full assurance that both
  455. computers are finished.  NOTE that the code chosen for the End-Off situation
  456. are not necessarily related to their apparent function.
  457.  
  458.  
  459. Transferring File Type
  460. ----------------------
  461.      When transferring files from one computer to another it is often
  462. necessary to also transfer the file type, but this must be known BEFORE the
  463. file is opened, and, therefore, before the protocol begins. "C1" does not
  464. impose any strick rules on what sort of information you trnsfer about the
  465. files, if any, but when writing a terminal program to communicate with one of
  466. my bulletin boards, the following should be done:
  467.  
  468.      Using a full implementation of the "C1" protocol (first dummy block,
  469. data block, and End-Off), transmit a single byte of data corresponding to the
  470. folowing file types:
  471.  
  472.   1 = Program File
  473.   2 = SEQ file
  474.   3 = WordPro File
  475.  
  476.      Transmitting this single piece of data would require that TWO blocks be
  477. sent; the initial dummy block to set up the size of the first data block (of
  478. which there will be only one, size 8), and the data block itself, consistig
  479. of 7 header bytes and the single file type byte.
  480.  
  481.      For other applications, one could conceivable transfer much more
  482. information, including file name, file type, computer type, etc.  It could
  483. even be possible to transfer multiple files, specifying the number and name
  484. of each file in this first tranmission.  Alternately, no one said you HAVE to
  485. use this first separate transmission; if no information other than the file
  486. needs to be transmitted, you just send the file and nothing more.
  487.  
  488. -----------------------------------------------------------------
  489. NOTE:
  490. For more inforation contact:
  491.  
  492.      Steve Punter
  493.      442 Forest Fire Lane
  494.      Mississauga, Ontario
  495.      Canada       L4W 3P4
  496.  
  497. Telephone (416) 896-1446
  498.  
  499.      Business Hours; voice only:
  500.           12AM - 5PM, Monday to Friday
  501.  
  502.      Bulletin Board Hours; modem only:
  503.           6PM - 10AM, Monday to Friday
  504.           24 hours Saturday and Sunday
  505.  
  506. -----------------------------------------------------------------
  507. Steve Punter has release the source code to Public Domain!
  508.  
  509. George Murton - Sysop
  510. The KEYSTONE C-64 BBS (c)
  511. PunterNet Node #26
  512. (215) 770-0774
  513. 24 hours everyday
  514. 300/1200 BPS
  515. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.