udp.but 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. \# This file is so named for tradition's sake: it contains what we
  2. \# always used to refer to, before they were written down, as
  3. \# PuTTY's `unwritten design principles'. It has nothing to do with
  4. \# the User Datagram Protocol.
  5. \A{udp} PuTTY hacking guide
  6. This appendix lists a selection of the design principles applying to
  7. the PuTTY source code. If you are planning to send code
  8. contributions, you should read this first.
  9. \H{udp-portability} Cross-OS portability
  10. Despite Windows being its main area of fame, PuTTY is no longer a
  11. Windows-only application suite. It has a working Unix port; a Mac
  12. port is in progress; more ports may or may not happen at a later
  13. date.
  14. Therefore, embedding Windows-specific code in core modules such as
  15. \cw{ssh.c} is not acceptable. We went to great lengths to \e{remove}
  16. all the Windows-specific stuff from our core modules, and to shift
  17. it out into Windows-specific modules. Adding large amounts of
  18. Windows-specific stuff in parts of the code that should be portable
  19. is almost guaranteed to make us reject a contribution.
  20. The PuTTY source base is divided into platform-specific modules and
  21. platform-generic modules. The Unix-specific modules are all in the
  22. \c{unix} subdirectory; the Windows-specific modules are in the
  23. \c{windows} subdirectory.
  24. All the modules in the main source directory - notably \e{all} of
  25. the code for the various back ends - are platform-generic. We want
  26. to keep them that way.
  27. This also means you should stick to the C semantics guaranteed by the
  28. C standard: try not to make assumptions about the precise size of
  29. basic types such as \c{int} and \c{long int}; don't use pointer casts
  30. to do endianness-dependent operations, and so on.
  31. (Even \e{within} a platform front end you should still be careful of
  32. some of these portability issues. The Windows front end compiles on
  33. both 32- and 64-bit x86 and also Arm.)
  34. Our current choice of C standards version is \e{mostly} C99. With a
  35. couple of exceptions, you can assume that C99 features are available
  36. (in particular \cw{<stdint.h>}, \cw{<stdbool.h>} and \c{inline}), but
  37. you shouldn't use things that are new in C11 (such as \cw{<uchar.h>}
  38. or \cw{_Generic}).
  39. The exceptions to that rule are due to the need for Visual Studio
  40. compatibility:
  41. \b Don't use variable-length arrays. Visual Studio doesn't support
  42. them even now that it's adopted the rest of C99. We use \cw{-Wvla}
  43. when building with gcc and clang, to make it easier to avoid
  44. accidentally breaking that rule.
  45. \b For historical reasons, we still build with one older VS version
  46. which lacks \cw{<inttypes.h>}. So that file is included centrally in
  47. \c{defs.h}, and has a set of workaround definitions for the
  48. \cw{PRIx64}-type macros we use. If you need to use another one of
  49. those macros, you need to add a workaround definition in \c{defs.h},
  50. and don't casually re-include \cw{<inttypes.h>} anywhere else in the
  51. source file.
  52. Here are a few portability assumptions that we \e{do} currently allow
  53. (because we'd already have to thoroughly vet the existing code if they
  54. ever needed to change, and it doesn't seem worth doing that unless we
  55. really have to):
  56. \b You can assume \c{int} is \e{at least} 32 bits wide. (We've never
  57. tried to port PuTTY to a platform with 16-bit \cw{int}, and it doesn't
  58. look likely to be necessary in future.)
  59. \b Similarly, you can assume \c{char} is exactly 8 bits. (Exceptions
  60. to that are even less likely to be relevant to us than short
  61. \cw{int}.)
  62. \b You can assume that using \c{memset} to write zero bytes over a
  63. whole structure will have the effect of setting all its pointer fields
  64. to \cw{NULL}. (The standard itself guarantees this for \e{integer}
  65. fields, but not for pointers.)
  66. \b You can assume that \c{time_t} has POSIX semantics, i.e. that it
  67. represents an integer number of non-leap seconds since 1970-01-01
  68. 00:00:00 UTC. (Times in this format are used in X authorisation, but
  69. we could work around that by carefully distinguishing local \c{time_t}
  70. from time values used in the wire protocol; but these semantics of
  71. \c{time_t} are also baked into the shared library API used by the
  72. GSSAPI authentication code, which would be much harder to change.)
  73. \b You can assume that the execution character encoding is a superset
  74. of the printable characters of ASCII. (In particular, it's fine to do
  75. arithmetic on a \c{char} value representing a Latin alphabetic
  76. character, without bothering to allow for EBCDIC or other
  77. non-consecutive encodings of the alphabet.)
  78. On the other hand, here are some particular things \e{not} to assume:
  79. \b Don't assume anything about the \e{signedness} of \c{char}. In
  80. particular, you \e{must} cast \c{char} values to \c{unsigned char}
  81. before passing them to any \cw{<ctype.h>} function (because those
  82. expect a non-negative character value, or \cw{EOF}). If you need a
  83. particular signedness, explicitly specify \c{signed char} or
  84. \c{unsigned char}, or use C99 \cw{int8_t} or \cw{uint8_t}.
  85. \b From past experience with MacOS, we're still a bit nervous about
  86. \cw{'\\n'} and \cw{'\\r'} potentially having unusual meanings on a
  87. given platform. So it's fine to say \c{\\n} in a string you're passing
  88. to \c{printf}, but in any context where those characters appear in a
  89. standardised wire protocol or a binary file format, they should be
  90. spelled \cw{'\\012'} and \cw{'\\015'} respectively.
  91. \H{udp-multi-backend} Multiple backends treated equally
  92. PuTTY is not an SSH client with some other stuff tacked on the side.
  93. PuTTY is a generic, multiple-backend, remote VT-terminal client
  94. which happens to support one backend which is larger, more popular
  95. and more useful than the rest. Any extra feature which can possibly
  96. be general across all backends should be so: localising features
  97. unnecessarily into the SSH back end is a design error. (For example,
  98. we had several code submissions for proxy support which worked by
  99. hacking \cw{ssh.c}. Clearly this is completely wrong: the
  100. \cw{network.h} abstraction is the place to put it, so that it will
  101. apply to all back ends equally, and indeed we eventually put it
  102. there after another contributor sent a better patch.)
  103. The rest of PuTTY should try to avoid knowing anything about
  104. specific back ends if at all possible. To support a feature which is
  105. only available in one network protocol, for example, the back end
  106. interface should be extended in a general manner such that \e{any}
  107. back end which is able to provide that feature can do so. If it so
  108. happens that only one back end actually does, that's just the way it
  109. is, but it shouldn't be relied upon by any code.
  110. \H{udp-globals} Multiple sessions per process on some platforms
  111. Some ports of PuTTY - notably the in-progress Mac port - are
  112. constrained by the operating system to run as a single process
  113. potentially managing multiple sessions.
  114. Therefore, the platform-independent parts of PuTTY never use global
  115. variables to store per-session data. The global variables that do
  116. exist are tolerated because they are not specific to a particular
  117. login session: \c{flags} defines properties that are expected to
  118. apply equally to \e{all} the sessions run by a single PuTTY process,
  119. the random number state in \cw{sshrand.c} and the timer list in
  120. \cw{timing.c} serve all sessions equally, and so on. But most data
  121. is specific to a particular network session, and is therefore stored
  122. in dynamically allocated data structures, and pointers to these
  123. structures are passed around between functions.
  124. Platform-specific code can reverse this decision if it likes. The
  125. Windows code, for historical reasons, stores most of its data as
  126. global variables. That's OK, because \e{on Windows} we know there is
  127. only one session per PuTTY process, so it's safe to do that. But
  128. changes to the platform-independent code should avoid introducing
  129. global variables, unless they are genuinely cross-session.
  130. \H{udp-pure-c} C, not C++
  131. PuTTY is written entirely in C, not in C++.
  132. We have made \e{some} effort to make it easy to compile our code
  133. using a C++ compiler: notably, our \c{snew}, \c{snewn} and
  134. \c{sresize} macros explicitly cast the return values of \cw{malloc}
  135. and \cw{realloc} to the target type. (This has type checking
  136. advantages even in C: it means you never accidentally allocate the
  137. wrong size piece of memory for the pointer type you're assigning it
  138. to. C++ friendliness is really a side benefit.)
  139. We want PuTTY to continue being pure C, at least in the
  140. platform-independent parts and the currently existing ports. Patches
  141. which switch the Makefiles to compile it as C++ and start using
  142. classes will not be accepted. Also, in particular, we disapprove of
  143. \cw{//} comments, at least for the moment. (Perhaps once C99 becomes
  144. genuinely widespread we might be more lenient.)
  145. The one exception: a port to a new platform may use languages other
  146. than C if they are necessary to code on that platform. If your
  147. favourite PDA has a GUI with a C++ API, then there's no way you can
  148. do a port of PuTTY without using C++, so go ahead and use it. But
  149. keep the C++ restricted to that platform's subdirectory; if your
  150. changes force the Unix or Windows ports to be compiled as C++, they
  151. will be unacceptable to us.
  152. \H{udp-security} Security-conscious coding
  153. PuTTY is a network application and a security application. Assume
  154. your code will end up being fed deliberately malicious data by
  155. attackers, and try to code in a way that makes it unlikely to be a
  156. security risk.
  157. In particular, try not to use fixed-size buffers for variable-size
  158. data such as strings received from the network (or even the user).
  159. We provide functions such as \cw{dupcat} and \cw{dupprintf}, which
  160. dynamically allocate buffers of the right size for the string they
  161. construct. Use these wherever possible.
  162. \H{udp-multi-compiler} Independence of specific compiler
  163. Windows PuTTY can currently be compiled with any of three Windows
  164. compilers: MS Visual C, the Cygwin / \cw{mingw32} GNU tools, and
  165. \cw{clang} (in MS compatibility mode).
  166. This is a really useful property of PuTTY, because it means people
  167. who want to contribute to the coding don't depend on having a
  168. specific compiler; so they don't have to fork out money for MSVC if
  169. they don't already have it, but on the other hand if they \e{do}
  170. have it they also don't have to spend effort installing \cw{gcc}
  171. alongside it. They can use whichever compiler they happen to have
  172. available, or install whichever is cheapest and easiest if they
  173. don't have one.
  174. Therefore, we don't want PuTTY to start depending on which compiler
  175. you're using. Using GNU extensions to the C language, for example,
  176. would ruin this useful property (not that anyone's ever tried it!);
  177. and more realistically, depending on an MS-specific library function
  178. supplied by the MSVC C library (\cw{_snprintf}, for example) is a
  179. mistake, because that function won't be available under the other
  180. compilers. Any function supplied in an official Windows DLL as part
  181. of the Windows API is fine, and anything defined in the C library
  182. standard is also fine, because those should be available
  183. irrespective of compilation environment. But things in between,
  184. available as non-standard library and language extensions in only
  185. one compiler, are disallowed.
  186. (\cw{_snprintf} in particular should be unnecessary, since we
  187. provide \cw{dupprintf}; see \k{udp-security}.)
  188. Compiler independence should apply on all platforms, of course, not
  189. just on Windows.
  190. \H{udp-small} Small code size
  191. PuTTY is tiny, compared to many other Windows applications. And it's
  192. easy to install: it depends on no DLLs, no other applications, no
  193. service packs or system upgrades. It's just one executable. You
  194. install that executable wherever you want to, and run it.
  195. We want to keep both these properties - the small size, and the ease
  196. of installation - if at all possible. So code contributions that
  197. depend critically on external DLLs, or that add a huge amount to the
  198. code size for a feature which is only useful to a small minority of
  199. users, are likely to be thrown out immediately.
  200. We do vaguely intend to introduce a DLL plugin interface for PuTTY,
  201. whereby seriously large extra features can be implemented in plugin
  202. modules. The important thing, though, is that those DLLs will be
  203. \e{optional}; if PuTTY can't find them on startup, it should run
  204. perfectly happily and just won't provide those particular features.
  205. A full installation of PuTTY might one day contain ten or twenty
  206. little DLL plugins, which would cut down a little on the ease of
  207. installation - but if you really needed ease of installation you
  208. \e{could} still just install the one PuTTY binary, or just the DLLs
  209. you really needed, and it would still work fine.
  210. Depending on \e{external} DLLs is something we'd like to avoid if at
  211. all possible (though for some purposes, such as complex SSH
  212. authentication mechanisms, it may be unavoidable). If it can't be
  213. avoided, the important thing is to follow the same principle of
  214. graceful degradation: if a DLL can't be found, then PuTTY should run
  215. happily and just not supply the feature that depended on it.
  216. \H{udp-single-threaded} Single-threaded code
  217. PuTTY and its supporting tools, or at least the vast majority of
  218. them, run in only one OS thread.
  219. This means that if you're devising some piece of internal mechanism,
  220. there's no need to use locks to make sure it doesn't get called by
  221. two threads at once. The only way code can be called re-entrantly is
  222. by recursion.
  223. That said, most of Windows PuTTY's network handling is triggered off
  224. Windows messages requested by \cw{WSAAsyncSelect()}, so if you call
  225. \cw{MessageBox()} deep within some network event handling code you
  226. should be aware that you might be re-entered if a network event
  227. comes in and is passed on to our window procedure by the
  228. \cw{MessageBox()} message loop.
  229. Also, the front ends (in particular Windows Plink) can use multiple
  230. threads if they like. However, Windows Plink keeps \e{very} tight
  231. control of its auxiliary threads, and uses them pretty much
  232. exclusively as a form of \cw{select()}. Pretty much all the code
  233. outside \cw{windows/winplink.c} is \e{only} ever called from the one
  234. primary thread; the others just loop round blocking on file handles
  235. and send messages to the main thread when some real work needs
  236. doing. This is not considered a portability hazard because that bit
  237. of \cw{windows/winplink.c} will need rewriting on other platforms in
  238. any case.
  239. One important consequence of this: PuTTY has only one thread in
  240. which to do everything. That \q{everything} may include managing
  241. more than one login session (\k{udp-globals}), managing multiple
  242. data channels within an SSH session, responding to GUI events even
  243. when nothing is happening on the network, and responding to network
  244. requests from the server (such as repeat key exchange) even when the
  245. program is dealing with complex user interaction such as the
  246. re-configuration dialog box. This means that \e{almost none} of the
  247. PuTTY code can safely block.
  248. \H{udp-keystrokes} Keystrokes sent to the server wherever possible
  249. In almost all cases, PuTTY sends keystrokes to the server. Even
  250. weird keystrokes that you think should be hot keys controlling
  251. PuTTY. Even Alt-F4 or Alt-Space, for example. If a keystroke has a
  252. well-defined escape sequence that it could usefully be sending to
  253. the server, then it should do so, or at the very least it should be
  254. configurably able to do so.
  255. To unconditionally turn a key combination into a hot key to control
  256. PuTTY is almost always a design error. If a hot key is really truly
  257. required, then try to find a key combination for it which \e{isn't}
  258. already used in existing PuTTYs (either it sends nothing to the
  259. server, or it sends the same thing as some other combination). Even
  260. then, be prepared for the possibility that one day that key
  261. combination might end up being needed to send something to the
  262. server - so make sure that there's an alternative way to invoke
  263. whatever PuTTY feature it controls.
  264. \H{udp-640x480} 640\u00D7{x}480 friendliness in configuration panels
  265. There's a reason we have lots of tiny configuration panels instead
  266. of a few huge ones, and that reason is that not everyone has a
  267. 1600\u00D7{x}1200 desktop. 640\u00D7{x}480 is still a viable
  268. resolution for running Windows (and indeed it's still the default if
  269. you start up in safe mode), so it's still a resolution we care
  270. about.
  271. Accordingly, the PuTTY configuration box, and the PuTTYgen control
  272. window, are deliberately kept just small enough to fit comfortably
  273. on a 640\u00D7{x}480 display. If you're adding controls to either of
  274. these boxes and you find yourself wanting to increase the size of
  275. the whole box, \e{don't}. Split it into more panels instead.
  276. \H{udp-makefiles-auto} Automatically generated \cw{Makefile}s
  277. PuTTY is intended to compile on multiple platforms, and with
  278. multiple compilers. It would be horrifying to try to maintain a
  279. single \cw{Makefile} which handled all possible situations, and just
  280. as painful to try to directly maintain a set of matching
  281. \cw{Makefile}s for each different compilation environment.
  282. Therefore, we have moved the problem up by one level. In the PuTTY
  283. source archive is a file called \c{Recipe}, which lists which source
  284. files combine to produce which binaries; and there is also a script
  285. called \cw{mkfiles.pl}, which reads \c{Recipe} and writes out the
  286. real \cw{Makefile}s. (The script also reads all the source files and
  287. analyses their dependencies on header files, so we get an extra
  288. benefit from doing it this way, which is that we can supply correct
  289. dependency information even in environments where it's difficult to
  290. set up an automated \c{make depend} phase.)
  291. You should \e{never} edit any of the PuTTY \cw{Makefile}s directly.
  292. They are not stored in our source repository at all. They are
  293. automatically generated by \cw{mkfiles.pl} from the file \c{Recipe}.
  294. If you need to add a new object file to a particular binary, the
  295. right thing to do is to edit \c{Recipe} and re-run \cw{mkfiles.pl}.
  296. This will cause the new object file to be added in every tool that
  297. requires it, on every platform where it matters, in every
  298. \cw{Makefile} to which it is relevant, \e{and} to get all the
  299. dependency data right.
  300. If you send us a patch that modifies one of the \cw{Makefile}s, you
  301. just waste our time, because we will have to convert it into a
  302. change to \c{Recipe}. If you send us a patch that modifies \e{all}
  303. of the \cw{Makefile}s, you will have wasted a lot of \e{your} time
  304. as well!
  305. (There is a comment at the top of every \cw{Makefile} in the PuTTY
  306. source archive saying this, but many people don't seem to read it,
  307. so it's worth repeating here.)
  308. \H{udp-ssh-coroutines} Coroutines in the SSH code
  309. Large parts of the code in the various SSH modules (in fact most of
  310. the protocol layers) are structured using a set of macros that
  311. implement (something close to) Donald Knuth's \q{coroutines} concept
  312. in C.
  313. Essentially, the purpose of these macros are to arrange that a
  314. function can call \cw{crReturn()} to return to its caller, and the
  315. next time it is called control will resume from just after that
  316. \cw{crReturn} statement.
  317. This means that any local (automatic) variables declared in such a
  318. function will be corrupted every time you call \cw{crReturn}. If you
  319. need a variable to persist for longer than that, you \e{must} make it
  320. a field in some appropriate structure containing the persistent state
  321. of the coroutine \dash typically the main state structure for an SSH
  322. protocol layer.
  323. See
  324. \W{https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html}\c{https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html}
  325. for a more in-depth discussion of what these macros are for and how
  326. they work.
  327. Another caveat: most of these coroutines are not \e{guaranteed} to run
  328. to completion, because the SSH connection (or whatever) that they're
  329. part of might be interrupted at any time by an unexpected network
  330. event or user action. So whenever a coroutine-managed variable refers
  331. to a resource that needs releasing, you should also ensure that the
  332. cleanup function for its containing state structure can reliably
  333. release it even if the coroutine is aborted at an arbitrary point.
  334. For example, if an SSH packet protocol layer has to have a field that
  335. sometimes points to a piece of allocated memory, then you should
  336. ensure that when you free that memory you reset the pointer field to
  337. \cw{NULL}. Then, no matter when the protocol layer's cleanup function
  338. is called, it can reliably free the memory if there is any, and not
  339. crash if there isn't.
  340. \H{udp-compile-once} Single compilation of each source file
  341. The PuTTY build system for any given platform works on the following
  342. very simple model:
  343. \b Each source file is compiled precisely once, to produce a single
  344. object file.
  345. \b Each binary is created by linking together some combination of
  346. those object files.
  347. Therefore, if you need to introduce functionality to a particular
  348. module which is only available in some of the tool binaries (for
  349. example, a cryptographic proxy authentication mechanism which needs
  350. to be left out of PuTTYtel to maintain its usability in
  351. crypto-hostile jurisdictions), the \e{wrong} way to do it is by
  352. adding \cw{#ifdef}s in (say) \cw{proxy.c}. This would require
  353. separate compilation of \cw{proxy.c} for PuTTY and PuTTYtel, which
  354. means that the entire \cw{Makefile}-generation architecture (see
  355. \k{udp-makefiles-auto}) would have to be significantly redesigned.
  356. Unless you are prepared to do that redesign yourself, \e{and}
  357. guarantee that it will still port to any future platforms we might
  358. decide to run on, you should not attempt this!
  359. The \e{right} way to introduce a feature like this is to put the new
  360. code in a separate source file, and (if necessary) introduce a
  361. second new source file defining the same set of functions, but
  362. defining them as stubs which don't provide the feature. Then the
  363. module whose behaviour needs to vary (\cw{proxy.c} in this example)
  364. can call the functions defined in these two modules, and it will
  365. either provide the new feature or not provide it according to which
  366. of your new modules it is linked with.
  367. Of course, object files are never shared \e{between} platforms; so
  368. it is allowable to use \cw{#ifdef} to select between platforms. This
  369. happens in \cw{puttyps.h} (choosing which of the platform-specific
  370. include files to use), and also in \cw{misc.c} (the Windows-specific
  371. \q{Minefield} memory diagnostic system). It should be used
  372. sparingly, though, if at all.
  373. \H{udp-perfection} Do as we say, not as we do
  374. The current PuTTY code probably does not conform strictly to \e{all}
  375. of the principles listed above. There may be the occasional
  376. SSH-specific piece of code in what should be a backend-independent
  377. module, or the occasional dependence on a non-standard X library
  378. function under Unix.
  379. This should not be taken as a licence to go ahead and violate the
  380. rules. Where we violate them ourselves, we're not happy about it,
  381. and we would welcome patches that fix any existing problems. Please
  382. try to help us make our code better, not worse!