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

222 lines
8.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. This file should help you to add new address types and address options to
  2. socat.
  3. NOTE:
  4. socat will in future releases be split into a library "libxio" containing all
  5. the address stuff, useful also for many other purposes, and the socat main()
  6. and data shuffler. If you intend to perform major changes to the xio part and
  7. to publish them, please contact me before!
  8. ADDING A NEW ADDRESS TYPE:
  9. * Create new files xio-newaddr.c and xio-newaddr.h
  10. * Create a new record of struct addrdesc in xio-newaddr.c, with declaration in xio-newaddr.h.
  11. * Make a new entry to addressnames[] in xioopen.c with the addresses main name
  12. and maybe with alias names. Keep this array ASCII sorted, without uppercase
  13. chars.
  14. * config.h.in: #undef WITH_NEWADDR
  15. * configure.in: Copy the disable part of, e.g., WITH_SOCKS4 and adapt it to
  16. NEWADDR
  17. * In socat.c, add to socat_version
  18. * Write a function xioopen_newaddr() in xio-newaddr.c, declaration in
  19. xio-newaddr.h
  20. Do not forget the following option processing calls:
  21. All groups: _xio_openlate()
  22. Group FD: applyopts_cloexec()
  23. Group NAMED: applyopts_file() for phases PREOPEN, OPEN, and FD
  24. * Describe a tested example in file EXAMPLES, and maybe in the socat manpage
  25. source.
  26. * Try to define a test for this address type in test.sh
  27. * Update file CHANGES
  28. ADDING A NEW ADDRESS OPTION:
  29. xioopen.c:
  30. * If this option depends on a #define that is probably not available on all
  31. platforms, make all new code for this option dependent on the existence of this
  32. C header define:
  33. #ifdef PREFIX_NEWOPTION
  34. ...
  35. #endif
  36. * Add an OPT_NEWOPTION to enum e_optcode in xioopts.h, preferably keeping
  37. alphabetic order
  38. * Add a struct optdesc opt_newoption record in xio-newaddr.c and its
  39. declaration in xio-newaddr.h. The complete structure definition must be in one
  40. line without breaks for automatic docu extraction.
  41. Build the record from the following components:
  42. . A canonical default name (e.g. "newoption")
  43. . A short, preferable name (e.g. "newopt") or NULL
  44. . OPT_NEWOPTION (from enum e_optcode, see above)
  45. . A group membership that restricts appliance of the new option to matching
  46. address types (e.g., one of GROUP_ANY, GROUP_IP_TCP, GROUP_EXEC)
  47. . A phase specification that positions this option within address processing.
  48. Note that the function code can override this value.
  49. . A representation type for option arguments (e.g., TYPE_INT, TYPE_STRING etc.;
  50. use TYPE_BOOL if this option just triggers an action)
  51. . A function or action definition for applying this option. If it does not use
  52. one of the standard functions (open(), ioctl(), setsockopt()...), then use
  53. OFUNC_SPEC (specific).
  54. * For the canonical name and all its aliases and abbreviations, add entries to
  55. the array optionnames in xioopts.c. KEEP STRICT ALPHABETIC (ASCII) ORDER!
  56. The entries must be embedded in an IF_... macro of their group for conditional
  57. compiling.
  58. * For options using some predefined action (see OFUNC above), this might be
  59. enough - test the option and document it in xio.help!
  60. For OFUNC_SPEC, it might suffice to add another "case" to the OFUNC_SPEC branch
  61. in applyopts() in xioopts.c. If you need more special handling, you should try
  62. to understand the address specific functions and add your code there.
  63. * If you use system or low level C library calls or library calls that might
  64. hang or induce problems, please invoke them with capitalized name; if no such
  65. name is defined, add an appropriate debug function to sycls.c, and a header
  66. entry and a wrapper "define" to sycls.h
  67. * Update file CHANGES
  68. INFO ABOUT ADDRESS PHASES:
  69. Each option entry has a field specifying a default phase for its application.
  70. Of course, the code that analyses and applies an address may override this
  71. default phase.
  72. Depending on the type of address there are several major phase sequences:
  73. OPEN addresses:
  74. PH_INIT retrieving info from original state
  75. PH_EARLY before any other processing
  76. PH_PREOPEN before file creation/opening (not UNIX sockets)
  77. PH_OPEN during file creation/opening (not UNIX sockets)
  78. PH_PASTOPEN past file creation/opening (not UNIX sockets)
  79. PH_FD soon after FD creation or identification
  80. PH_LATE FD is ready, before start of data loop
  81. PH_LATE2 FD is ready, dropping privileges
  82. SOCKET addresses:
  83. PH_INIT retrieving info from original state
  84. PH_EARLY before any other processing
  85. PH_PRESOCKET before socket call
  86. PH_SOCKET for socket call
  87. PH_PASTSOCKET after socket call
  88. PH_FD soon after FD creation or identification
  89. PH_PREBIND before socket bind()
  90. PH_BIND during socket bind()
  91. PH_PASTBIND past socket bind()
  92. PH_PRECONNECT before connect()
  93. PH_CONNECT during connect()
  94. PH_PASTCONNECT after connect()
  95. PH_CONNECTED phase common with listen
  96. PH_LATE FD is ready, before start of data loop
  97. PH_LATE2 FD is ready, dropping privileges
  98. SOCKET with LISTEN and ACCEPT:
  99. PH_INIT retrieving info from original state
  100. PH_EARLY before any other processing
  101. PH_PRESOCKET before socket call
  102. PH_SOCKET for socket call
  103. PH_PREBIND before socket bind()
  104. PH_BIND during socket bind()
  105. PH_PASTBIND past socket bind()
  106. PH_PRELISTEN before listen()
  107. PH_LISTEN during listen()
  108. PH_PASTLISTEN after listen()
  109. PH_PREACCEPT before accept()
  110. PH_ACCEPT during accept()
  111. PH_PASTACCEPT after accept()
  112. # and the following on the new FD:
  113. PH_FD soon after FD creation or identification
  114. PH_PASTSOCKET after socket call
  115. PH_CONNECTED phase common with connect
  116. PH_PREFORK before forking
  117. PH_FORK during fork()
  118. PH_PASTFORK after fork()
  119. PH_LATE FD is ready, before start of data loop
  120. PH_LATE2 FD is ready, dropping privileges
  121. FD addresses:
  122. PH_INIT retrieving info from original state
  123. PH_EARLY before any other processing
  124. PH_FD soon after FD identification
  125. PH_LATE FD is ready, before start of data loop
  126. PH_LATE2 FD is ready, dropping privileges
  127. EXEC addresses:
  128. PH_INIT retrieving info from original state
  129. PH_EARLY before any other processing
  130. PH_PREBIGEN before socketpair() pipe() openpty()
  131. PH_BIGEN during socketpair() pipe() openpty()
  132. PH_PASTBIGEN past socketpair() pipe() openpty()
  133. PH_PASTSOCKET for socketpair()
  134. PH_FD soon after FD creation or identification
  135. PH_PREFORK before forking
  136. PH_FORK during fork()
  137. PH_PASTFORK after fork()
  138. PH_LATE FD is ready, before start of data loop
  139. PH_LATE2 FD is ready, dropping privileges
  140. PH_PREEXEC before exec() or system()
  141. PH_EXEC during exec() or system()
  142. There are lots of semantic relations between group, phase, and func fields of
  143. an option.
  144. There exists something like an overall phase sequence:
  145. PH_INIT # su-d.1
  146. PH_EARLY # chroot-early
  147. PH_PREOPEN, PH_OPEN, PH_PASTOPEN # (chroot before/after?)
  148. PH_PRESOCKET, PH_SOCKET, PH_PASTSOCKET # (su after (root for raw)?)
  149. PH_PREBIGEN, PH_BIGEN, PH_PASTBIGEN # (chroot before/after (/dev..)?)
  150. PH_FD
  151. PH_PREBIND, PH_BIND, PH_PASTBIND # (su after(before?))
  152. PH_PRELISTEN, PH_LISTEN, PH_PASTLISTEN
  153. PH_PRECONNECT, PH_CONNECT, PH_PASTCONNECT # (chroot before/after (AF_UNIX)?)
  154. PH_PREACCEPT, PH_ACCEPT, PH_PASTACCEPT
  155. PH_CONNECTED
  156. PH_PREFORK, PH_FORK, PH_PASTFORK # (all before/after?)
  157. PH_LATE # chroot
  158. PH_LATE2 # su, su-d.2
  159. PH_PREEXEC, PH_EXEC # (all before)
  160. ===============================================================================
  161. // Up to 1.7.2.4 socat used non async signal safe system and library calls in signal handlers, mostly for logging purposes. This problem was fixed in release 1.7.3.0 with the following concepts:
  162. Signal handlers set on entry and unset on return the diag_in_handler global variable. The logging system, when this variable is set, queues the text message together with errno and exit info in a UNIX datagram socket. When invoked with unset diag_in_handler it first checks if there are messages in that queue and prints them first.
  163. A async signal safe but minimal version of vsnprintf, named vsnprintf_r, was written so no value arguments need to be queued.
  164. Because strerror is not async signal safe a new function snprinterr was written that replaces the (glibc compatible) %m format with strerror output. The original errno is passed in the message queue, snprinterr is called when dequeuing messages outside of signal handler.
  165. // List of signal handlers in socat
  166. socat.c:socat_signal (generic, just logs and maybe exits)
  167. xioshutdown.c:signal_kill_pid (SIGALRM, kill child process)
  168. xiosigchld.c:childdied (SIGCHLD: get info, log; possibly close channel)
  169. xiosignal.c:socatsignalpass: cascades signal to channel child processes; w/ options sighup,sigint,sigquit
  170. xio-socket.c:xiosigaction_hasread: SIGUSR1,SIGCHLD, tells parent that datagram has been consumed