home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / unix / volume26 / faucet / part01 < prev    next >
Encoding:
Text File  |  1993-01-29  |  55.8 KB  |  1,699 lines

  1. Newsgroups: comp.sources.unix
  2. From: thoth@raybans.cis.ufl.edu (Robert Forsman)
  3. Subject: v26i086: faucet - pipe-like functionality over TCP sockets, Part01/01
  4. Sender: unix-sources-moderator@pa.dec.com
  5. Approved: vixie@pa.dec.com
  6.  
  7. Submitted-By: thoth@raybans.cis.ufl.edu (Robert Forsman)
  8. Posting-Number: Volume 26, Issue 86
  9. Archive-Name: faucet/part01
  10.  
  11. This shar has the source for two utilities that give you the power of the
  12. "|" (pipe) over the network.  I'd like to see these two programs become
  13. standard features on all machines with Berkeley stream sockets.
  14.  
  15. You may be amused to know that the source code for these programs is smaller
  16. than the copyright terms.  Pretty disgusting that it's so hard to give away
  17. free software :)
  18.  
  19. And yes, I *DO* have man pages this time, but they could lose a few rough
  20. edges.  I welcome comments on the documentation and the source code.  If it
  21. doesn't make instantly on your machine, tell me why and I'll try to make it
  22. easier next time.
  23.  
  24.     thoth@raybans.cis.ufl.edu (Robert Forsman)
  25.  
  26. #! /bin/sh
  27. # This is a shell archive.  Remove anything before this line, then unpack
  28. # it by saving it into a file and typing "sh file".  To overwrite existing
  29. # files, type "sh file -c".  You can also feed this as standard input via
  30. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  31. # will see the following message at the end:
  32. #        "End of archive 1 (of 1)."
  33. # Contents:  COPYING MANIFEST Makefile README faucet.1 faucet.c hose.1
  34. #   hose.c portname.c
  35. # Wrapped by vixie@cognition.pa.dec.com on Sat Jan 30 16:56:52 1993
  36. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  37. if test -f 'COPYING' -a "${1}" != "-c" ; then 
  38.   echo shar: Will not clobber existing file \"'COPYING'\"
  39. else
  40. echo shar: Extracting \"'COPYING'\" \(17981 characters\)
  41. sed "s/^X//" >'COPYING' <<'END_OF_FILE'
  42. X            GNU GENERAL PUBLIC LICENSE
  43. X               Version 2, June 1991
  44. X
  45. X Copyright (C) 1989, 1991 Free Software Foundation, Inc.
  46. X                          675 Mass Ave, Cambridge, MA 02139, USA
  47. X Everyone is permitted to copy and distribute verbatim copies
  48. X of this license document, but changing it is not allowed.
  49. X
  50. X                Preamble
  51. X
  52. X  The licenses for most software are designed to take away your
  53. freedom to share and change it.  By contrast, the GNU General Public
  54. License is intended to guarantee your freedom to share and change free
  55. software--to make sure the software is free for all its users.  This
  56. General Public License applies to most of the Free Software
  57. XFoundation's software and to any other program whose authors commit to
  58. using it.  (Some other Free Software Foundation software is covered by
  59. he GNU Library General Public License instead.)  You can apply it to
  60. your programs, too.
  61. X
  62. X  When we speak of free software, we are referring to freedom, not
  63. price.  Our General Public Licenses are designed to make sure that you
  64. have the freedom to distribute copies of free software (and charge for
  65. this service if you wish), that you receive source code or can get it
  66. if you want it, that you can change the software or use pieces of it
  67. in new free programs; and that you know you can do these things.
  68. X
  69. X  To protect your rights, we need to make restrictions that forbid
  70. anyone to deny you these rights or to ask you to surrender the rights.
  71. These restrictions translate to certain responsibilities for you if you
  72. distribute copies of the software, or if you modify it.
  73. X
  74. X  For example, if you distribute copies of such a program, whether
  75. gratis or for a fee, you must give the recipients all the rights that
  76. you have.  You must make sure that they, too, receive or can get the
  77. source code.  And you must show them these terms so they know their
  78. rights.
  79. X
  80. X  We protect your rights with two steps: (1) copyright the software, and
  81. X(2) offer you this license which gives you legal permission to copy,
  82. distribute and/or modify the software.
  83. X
  84. X  Also, for each author's protection and ours, we want to make certain
  85. that everyone understands that there is no warranty for this free
  86. software.  If the software is modified by someone else and passed on, we
  87. want its recipients to know that what they have is not the original, so
  88. that any problems introduced by others will not reflect on the original
  89. authors' reputations.
  90. X
  91. X  Finally, any free program is threatened constantly by software
  92. patents.  We wish to avoid the danger that redistributors of a free
  93. program will individually obtain patent licenses, in effect making the
  94. program proprietary.  To prevent this, we have made it clear that any
  95. patent must be licensed for everyone's free use or not licensed at all.
  96. X
  97. X  The precise terms and conditions for copying, distribution and
  98. modification follow.
  99. X
  100. X            GNU GENERAL PUBLIC LICENSE
  101. X   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  102. X
  103. X  0. This License applies to any program or other work which contains
  104. a notice placed by the copyright holder saying it may be distributed
  105. under the terms of this General Public License.  The "Program", below,
  106. refers to any such program or work, and a "work based on the Program"
  107. means either the Program or any derivative work under copyright law:
  108. that is to say, a work containing the Program or a portion of it,
  109. either verbatim or with modifications and/or translated into another
  110. language.  (Hereinafter, translation is included without limitation in
  111. the term "modification".)  Each licensee is addressed as "you".
  112. X
  113. Activities other than copying, distribution and modification are not
  114. covered by this License; they are outside its scope.  The act of
  115. running the Program is not restricted, and the output from the Program
  116. is covered only if its contents constitute a work based on the
  117. Program (independent of having been made by running the Program).
  118. Whether that is true depends on what the Program does.
  119. X
  120. X  1. You may copy and distribute verbatim copies of the Program's
  121. source code as you receive it, in any medium, provided that you
  122. conspicuously and appropriately publish on each copy an appropriate
  123. copyright notice and disclaimer of warranty; keep intact all the
  124. notices that refer to this License and to the absence of any warranty;
  125. and give any other recipients of the Program a copy of this License
  126. along with the Program.
  127. X
  128. You may charge a fee for the physical act of transferring a copy, and
  129. you may at your option offer warranty protection in exchange for a fee.
  130. X
  131. X  2. You may modify your copy or copies of the Program or any portion
  132. of it, thus forming a work based on the Program, and copy and
  133. distribute such modifications or work under the terms of Section 1
  134. above, provided that you also meet all of these conditions:
  135. X
  136. X    a) You must cause the modified files to carry prominent notices
  137. X    stating that you changed the files and the date of any change.
  138. X
  139. X    b) You must cause any work that you distribute or publish, that in
  140. X    whole or in part contains or is derived from the Program or any
  141. X    part thereof, to be licensed as a whole at no charge to all third
  142. X    parties under the terms of this License.
  143. X
  144. X    c) If the modified program normally reads commands interactively
  145. X    when run, you must cause it, when started running for such
  146. X    interactive use in the most ordinary way, to print or display an
  147. X    announcement including an appropriate copyright notice and a
  148. X    notice that there is no warranty (or else, saying that you provide
  149. X    a warranty) and that users may redistribute the program under
  150. X    these conditions, and telling the user how to view a copy of this
  151. X    License.  (Exception: if the Program itself is interactive but
  152. X    does not normally print such an announcement, your work based on
  153. X    the Program is not required to print an announcement.)
  154. X
  155. These requirements apply to the modified work as a whole.  If
  156. identifiable sections of that work are not derived from the Program,
  157. and can be reasonably considered independent and separate works in
  158. themselves, then this License, and its terms, do not apply to those
  159. sections when you distribute them as separate works.  But when you
  160. distribute the same sections as part of a whole which is a work based
  161. on the Program, the distribution of the whole must be on the terms of
  162. this License, whose permissions for other licensees extend to the
  163. entire whole, and thus to each and every part regardless of who wrote it.
  164. X
  165. Thus, it is not the intent of this section to claim rights or contest
  166. your rights to work written entirely by you; rather, the intent is to
  167. exercise the right to control the distribution of derivative or
  168. collective works based on the Program.
  169. X
  170. In addition, mere aggregation of another work not based on the Program
  171. with the Program (or with a work based on the Program) on a volume of
  172. a storage or distribution medium does not bring the other work under
  173. the scope of this License.
  174. X
  175. X  3. You may copy and distribute the Program (or a work based on it,
  176. under Section 2) in object code or executable form under the terms of
  177. Sections 1 and 2 above provided that you also do one of the following:
  178. X
  179. X    a) Accompany it with the complete corresponding machine-readable
  180. X    source code, which must be distributed under the terms of Sections
  181. X    1 and 2 above on a medium customarily used for software interchange; or,
  182. X
  183. X    b) Accompany it with a written offer, valid for at least three
  184. X    years, to give any third party, for a charge no more than your
  185. X    cost of physically performing source distribution, a complete
  186. X    machine-readable copy of the corresponding source code, to be
  187. X    distributed under the terms of Sections 1 and 2 above on a medium
  188. X    customarily used for software interchange; or,
  189. X
  190. X    c) Accompany it with the information you received as to the offer
  191. X    to distribute corresponding source code.  (This alternative is
  192. X    allowed only for noncommercial distribution and only if you
  193. X    received the program in object code or executable form with such
  194. X    an offer, in accord with Subsection b above.)
  195. X
  196. The source code for a work means the preferred form of the work for
  197. making modifications to it.  For an executable work, complete source
  198. code means all the source code for all modules it contains, plus any
  199. associated interface definition files, plus the scripts used to
  200. control compilation and installation of the executable.  However, as a
  201. special exception, the source code distributed need not include
  202. anything that is normally distributed (in either source or binary
  203. form) with the major components (compiler, kernel, and so on) of the
  204. operating system on which the executable runs, unless that component
  205. itself accompanies the executable.
  206. X
  207. If distribution of executable or object code is made by offering
  208. access to copy from a designated place, then offering equivalent
  209. access to copy the source code from the same place counts as
  210. distribution of the source code, even though third parties are not
  211. compelled to copy the source along with the object code.
  212. X
  213. X  4. You may not copy, modify, sublicense, or distribute the Program
  214. except as expressly provided under this License.  Any attempt
  215. otherwise to copy, modify, sublicense or distribute the Program is
  216. void, and will automatically terminate your rights under this License.
  217. However, parties who have received copies, or rights, from you under
  218. this License will not have their licenses terminated so long as such
  219. parties remain in full compliance.
  220. X
  221. X  5. You are not required to accept this License, since you have not
  222. signed it.  However, nothing else grants you permission to modify or
  223. distribute the Program or its derivative works.  These actions are
  224. prohibited by law if you do not accept this License.  Therefore, by
  225. modifying or distributing the Program (or any work based on the
  226. Program), you indicate your acceptance of this License to do so, and
  227. all its terms and conditions for copying, distributing or modifying
  228. the Program or works based on it.
  229. X
  230. X  6. Each time you redistribute the Program (or any work based on the
  231. Program), the recipient automatically receives a license from the
  232. original licensor to copy, distribute or modify the Program subject to
  233. these terms and conditions.  You may not impose any further
  234. restrictions on the recipients' exercise of the rights granted herein.
  235. You are not responsible for enforcing compliance by third parties to
  236. this License.
  237. X
  238. X  7. If, as a consequence of a court judgment or allegation of patent
  239. infringement or for any other reason (not limited to patent issues),
  240. conditions are imposed on you (whether by court order, agreement or
  241. otherwise) that contradict the conditions of this License, they do not
  242. excuse you from the conditions of this License.  If you cannot
  243. distribute so as to satisfy simultaneously your obligations under this
  244. License and any other pertinent obligations, then as a consequence you
  245. may not distribute the Program at all.  For example, if a patent
  246. license would not permit royalty-free redistribution of the Program by
  247. all those who receive copies directly or indirectly through you, then
  248. the only way you could satisfy both it and this License would be to
  249. refrain entirely from distribution of the Program.
  250. X
  251. If any portion of this section is held invalid or unenforceable under
  252. any particular circumstance, the balance of the section is intended to
  253. apply and the section as a whole is intended to apply in other
  254. circumstances.
  255. X
  256. It is not the purpose of this section to induce you to infringe any
  257. patents or other property right claims or to contest validity of any
  258. such claims; this section has the sole purpose of protecting the
  259. integrity of the free software distribution system, which is
  260. implemented by public license practices.  Many people have made
  261. generous contributions to the wide range of software distributed
  262. through that system in reliance on consistent application of that
  263. system; it is up to the author/donor to decide if he or she is willing
  264. to distribute software through any other system and a licensee cannot
  265. impose that choice.
  266. X
  267. This section is intended to make thoroughly clear what is believed to
  268. be a consequence of the rest of this License.
  269. X
  270. X  8. If the distribution and/or use of the Program is restricted in
  271. certain countries either by patents or by copyrighted interfaces, the
  272. original copyright holder who places the Program under this License
  273. may add an explicit geographical distribution limitation excluding
  274. those countries, so that distribution is permitted only in or among
  275. countries not thus excluded.  In such case, this License incorporates
  276. the limitation as if written in the body of this License.
  277. X
  278. X  9. The Free Software Foundation may publish revised and/or new versions
  279. of the General Public License from time to time.  Such new versions will
  280. be similar in spirit to the present version, but may differ in detail to
  281. address new problems or concerns.
  282. X
  283. XEach version is given a distinguishing version number.  If the Program
  284. specifies a version number of this License which applies to it and "any
  285. later version", you have the option of following the terms and conditions
  286. either of that version or of any later version published by the Free
  287. Software Foundation.  If the Program does not specify a version number of
  288. this License, you may choose any version ever published by the Free Software
  289. XFoundation.
  290. X
  291. X  10. If you wish to incorporate parts of the Program into other free
  292. programs whose distribution conditions are different, write to the author
  293. to ask for permission.  For software which is copyrighted by the Free
  294. Software Foundation, write to the Free Software Foundation; we sometimes
  295. make exceptions for this.  Our decision will be guided by the two goals
  296. of preserving the free status of all derivatives of our free software and
  297. of promoting the sharing and reuse of software generally.
  298. X
  299. X                NO WARRANTY
  300. X
  301. X  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
  302. XFOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
  303. OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
  304. PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
  305. OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  306. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
  307. TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
  308. PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
  309. REPAIR OR CORRECTION.
  310. X
  311. X  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
  312. WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
  313. REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
  314. INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
  315. OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
  316. TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
  317. YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
  318. PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
  319. POSSIBILITY OF SUCH DAMAGES.
  320. X
  321. X             END OF TERMS AND CONDITIONS
  322. X
  323. X    Appendix: How to Apply These Terms to Your New Programs
  324. X
  325. X  If you develop a new program, and you want it to be of the greatest
  326. possible use to the public, the best way to achieve this is to make it
  327. free software which everyone can redistribute and change under these terms.
  328. X
  329. X  To do so, attach the following notices to the program.  It is safest
  330. to attach them to the start of each source file to most effectively
  331. convey the exclusion of warranty; and each file should have at least
  332. the "copyright" line and a pointer to where the full notice is found.
  333. X
  334. X    <one line to give the program's name and a brief idea of what it does.>
  335. X    Copyright (C) 19yy  <name of author>
  336. X
  337. X    This program is free software; you can redistribute it and/or modify
  338. X    it under the terms of the GNU General Public License as published by
  339. X    the Free Software Foundation; either version 2 of the License, or
  340. X    (at your option) any later version.
  341. X
  342. X    This program is distributed in the hope that it will be useful,
  343. X    but WITHOUT ANY WARRANTY; without even the implied warranty of
  344. X    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  345. X    GNU General Public License for more details.
  346. X
  347. X    You should have received a copy of the GNU General Public License
  348. X    along with this program; if not, write to the Free Software
  349. X    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  350. X
  351. Also add information on how to contact you by electronic and paper mail.
  352. X
  353. If the program is interactive, make it output a short notice like this
  354. when it starts in an interactive mode:
  355. X
  356. X    Gnomovision version 69, Copyright (C) 19yy name of author
  357. X    Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
  358. X    This is free software, and you are welcome to redistribute it
  359. X    under certain conditions; type `show c' for details.
  360. X
  361. The hypothetical commands `show w' and `show c' should show the appropriate
  362. parts of the General Public License.  Of course, the commands you use may
  363. be called something other than `show w' and `show c'; they could even be
  364. mouse-clicks or menu items--whatever suits your program.
  365. X
  366. You should also get your employer (if you work as a programmer) or your
  367. school, if any, to sign a "copyright disclaimer" for the program, if
  368. necessary.  Here is a sample; alter the names:
  369. X
  370. X  Yoyodyne, Inc., hereby disclaims all copyright interest in the program
  371. X  `Gnomovision' (which makes passes at compilers) written by James Hacker.
  372. X
  373. X  <signature of Ty Coon>, 1 April 1989
  374. X  Ty Coon, President of Vice
  375. X
  376. This General Public License does not permit incorporating your program into
  377. proprietary programs.  If your program is a subroutine library, you may
  378. consider it more useful to permit linking proprietary applications with the
  379. library.  If this is what you want to do, use the GNU Library General
  380. Public License instead of this License.
  381. END_OF_FILE
  382. if test 17981 -ne `wc -c <'COPYING'`; then
  383.     echo shar: \"'COPYING'\" unpacked with wrong size!
  384. fi
  385. # end of 'COPYING'
  386. fi
  387. if test -f 'MANIFEST' -a "${1}" != "-c" ; then 
  388.   echo shar: Will not clobber existing file \"'MANIFEST'\"
  389. else
  390. echo shar: Extracting \"'MANIFEST'\" \(393 characters\)
  391. sed "s/^X//" >'MANIFEST' <<'END_OF_FILE'
  392. X   File Name        Archive #    Description
  393. X-----------------------------------------------------------
  394. X COPYING                    1    
  395. X MANIFEST                   1    This shipping list
  396. X Makefile                   1    
  397. X README                     1    
  398. X faucet.1                   1    
  399. X faucet.c                   1    
  400. X hose.1                     1    
  401. X hose.c                     1    
  402. X portname.c                 1    
  403. END_OF_FILE
  404. if test 393 -ne `wc -c <'MANIFEST'`; then
  405.     echo shar: \"'MANIFEST'\" unpacked with wrong size!
  406. fi
  407. # end of 'MANIFEST'
  408. fi
  409. if test -f 'Makefile' -a "${1}" != "-c" ; then 
  410.   echo shar: Will not clobber existing file \"'Makefile'\"
  411. else
  412. echo shar: Extracting \"'Makefile'\" \(1264 characters\)
  413. sed "s/^X//" >'Makefile' <<'END_OF_FILE'
  414. X#    faucet and hose: network pipe utilities
  415. X#    Copyright (C) 1992 Robert Forsman
  416. X#
  417. X#    This program is free software; you can redistribute it and/or modify
  418. X#    it under the terms of the GNU General Public License as published by
  419. X#    the Free Software Foundation; either version 2 of the License, or
  420. X#    (at your option) any later version.
  421. X#
  422. X#    This program is distributed in the hope that it will be useful,
  423. X#    but WITHOUT ANY WARRANTY; without even the implied warranty of
  424. X#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  425. X#    GNU General Public License for more details.
  426. X#
  427. X#    You should have received a copy of the GNU General Public License
  428. X#    along with this program; if not, write to the Free Software
  429. X#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  430. X
  431. X#
  432. X# under HPUX you will probably have to make using LDLIBS=-lBSD
  433. X#
  434. X
  435. CFLAGS = 
  436. X
  437. XFOBJS = faucet.o portname.o
  438. HOBJS = hose.o portname.o
  439. ALL =    faucet hose
  440. X
  441. all    : ${ALL}
  442. X
  443. faucet    : ${FOBJS}
  444. X    ${CC} ${CFLAGS} -o $@ ${FOBJS} ${LDLIBS}
  445. X
  446. hose    : ${HOBJS}
  447. X    ${CC} ${CFLAGS} -o $@ ${HOBJS} ${LDLIBS}
  448. X
  449. install : all
  450. X    cp faucet hose ${HOME}/bin/`mach`
  451. X
  452. clean    :
  453. X    rm -f ${FOBJS}  ${HOBJS}  ${ALL}
  454. X    rm -f *.o *~ *.CKP *.BAK core a.out
  455. X
  456. spotless: clean
  457. X    rm -f *~ core  hose faucet
  458. END_OF_FILE
  459. if test 1264 -ne `wc -c <'Makefile'`; then
  460.     echo shar: \"'Makefile'\" unpacked with wrong size!
  461. fi
  462. # end of 'Makefile'
  463. fi
  464. if test -f 'README' -a "${1}" != "-c" ; then 
  465.   echo shar: Will not clobber existing file \"'README'\"
  466. else
  467. echo shar: Extracting \"'README'\" \(6764 characters\)
  468. sed "s/^X//" >'README' <<'END_OF_FILE'
  469. X
  470. X"Mutant" Bob Forsman and his amazing 43 burner stove present
  471. X------------------------------------------------------------
  472. X            "stuff"
  473. X------------------------------------------------------------
  474. X
  475. X  This directory contains faucet and hose.
  476. X
  477. X
  478. X
  479. faucet and hose:
  480. X
  481. X  These two utilities are useful for connecting arbitrary programs
  482. over sockets.  The power of the '|' characeter in the shell can now be
  483. used over the network.
  484. X  As an example of what they can do, compile them and on one machine
  485. type
  486. X
  487. lightning:21 % ./faucet 3000 "tar cf - ." out
  488. X
  489. X and on another type
  490. X
  491. springs:3 % ./hose lightning 3000 "tar tvf -" in
  492. X
  493. X  You will get output (from machine springs) that looks like this.
  494. X
  495. rwxr-xr-x771/50      0 Jul  7 11:53 1990 ./
  496. rw-r-----771/50   9266 Jun 30 21:41 1990 ./faucet.c~
  497. rw-r--r--771/50   6086 Mar  6 21:40 1990 ./server.cc
  498. rw-r--r--771/50   1203 Jul  7 11:49 1990 ./Makefile
  499. rw-r-----771/50   9757 Jul  7 11:49 1990 ./faucet.c
  500. rw-r--r--771/50   4241 Jun 23 04:09 1990 ./hose.c~
  501. rwxr-xr-x771/50  24576 Jul  7 11:50 1990 ./hose
  502. rw-r--r--771/50   4041 Jul  7 11:50 1990 ./hose.c
  503. rw-r-----771/50    592 Jul  7 11:48 1990 ./portname.c
  504. rw-r--r--771/50    317 Jul  7 11:48 1990 ./portname.o
  505. rwxr-xr-x771/50  24576 Jul  7 11:50 1990 ./faucet
  506. rw-r--r--771/50   3378 Jul  7 11:50 1990 ./hose.o
  507. rw-r--r--771/50   1069 Jun 23 02:42 1990 ./Makefile~
  508. rw-r--r--771/50   4662 Jul  7 11:50 1990 ./faucet.o
  509. rw-r-----771/50    377 Jul  7 11:53 1990 ./#README#
  510. rw-r-----771/50    371 Jul  7 11:53 1990 ./README
  511. rw-r--r--771/50   2917 Mar  6 22:13 1990 ./client.cc
  512. X
  513. X  The 3000 is the port number to connect to.  Normal users can use any
  514. number over 1023.  You can also specify a service name in place of a
  515. number since the program can look it up from the services database.
  516. X  The argument in ""s (argv[2] for faucet, argv[3] for hose) is the
  517. command to be connected to the socket.  The flags in, out and err
  518. govorn which file descriptors get connected to the socket after the
  519. connection is successful.
  520. X  hose is a one-shot program.  It connects to the foreign server
  521. socket and execs the command.  faucet is an undying server.  Every
  522. time it recieves a connection on its socket it forks and execs a "csh
  523. X-c".  faucet can be limited to one-shot by the "once" flag.
  524. X
  525. SYNTAX
  526. X
  527. X   faucet <port> <command> (in|out|err)+ [once] [verb(|ose)] [quiet]
  528. X        [unix] [foreignport <port>] [foreignhost <host>]
  529. X
  530. X   hose <hostname> <port> <command> (in|out|err)+ [unix] [localport <port>]}
  531. X
  532. X  Host names can be be the name of the machine or (if no name is
  533. found) the internet number.  The individual numbers are sscanfed with
  534. X%i so 0x or 0 prefixes mean hex or octal instead of decimal for that
  535. one byte.
  536. X  You must chose at least one of in, out, and err.  The other flags
  537. are optional and listed below
  538. X   unix.........causes the program to go into unix-domain socket mode.
  539. X        hose can also be forced into unix-domain operation by
  540. X        specifying -unix- as the hostname (those dashes are
  541. X        part of the arg) or by being run with argv[0] as the
  542. X        string "uhose" (possible by hardlinking the binary).
  543. X        Port names are then treated as unix domain socket
  544. X        addresses (filenames) as opposed to internet port
  545. X        numbers.
  546. X   localport....(only on hose) asks hose to bind to a specific local
  547. X        port.
  548. X   once.........(only on faucet) the faucet command will not fork, but
  549. X        will exec the command, and when the command exits
  550. X        you're done.
  551. X   verbose|quiet (only on faucet) enables|disables the printing of
  552. X        extra information such as what host and port faucet is
  553. X        getting connections from.
  554. X   foreignport..(only on faucet) causes faucet to refuse (close
  555. X        immediately) any connection from a machine that hasn't
  556. X        bound its socket to the port specified immediately
  557. X        after the foreignport flag.  This can be used with the
  558. X        localport option of hose to perform crude
  559. X        authentication.  If the foreignport is <1024 then only
  560. X        a foreign root user will be able to connect to your
  561. X        faucet because only root can bind to a port number
  562. X        below 1024.
  563. X   foreignhost..(only on faucet) causes faucet to refuse (close
  564. X        immediately) any connection from any machine other
  565. X        than the host specified immediately after the
  566. X        foreignhost flag.
  567. X
  568. X  Be aware that addresses and ports can be spoofed if your network
  569. isn't secure (yours probably isn't).  If you have PCs or other
  570. non-UNIX boxes connected to the network, then it's DISGUSTINGLY easy
  571. for ANYONE to spoof the network.
  572. X
  573. X
  574. X
  575. XEXAMPLES
  576. X
  577. X    FAUCET                    HOSE
  578. X
  579. reef:100 % ./faucet chat "echo send
  580. X$14M and 2 airline stewardesses or
  581. you'll never see Hoffa alive"
  582. foreignhost azalea verbose out
  583. X
  584. X                reef:40 % ./hose reef chat cat in
  585. X
  586. refusing connection from host 128.
  587. X227.224.61(reef.cis.ufl.edu).
  588. X
  589. X                azalea:20 % ./hose reef chat cat in
  590. X
  591. X./faucet: Got connection from 128.
  592. X227.224.55(azalea.cis.ufl.edu) port
  593. X1687
  594. Unmatched '.
  595. X                /* darn, screwed up the shell quoting */
  596. X^C
  597. X
  598. reef:107 % ./faucet /tmp/blah 'echo
  599. send '\'\$14M\'' and 2 airline
  600. stewardesses or you'\\\''ll never
  601. see Hoffa alive' foreignhost azalea
  602. verbose out foreignport /tmp/auth unix
  603. X./faucet: foreignhost parameter makes no
  604. sense with UNIX domain sockets, ignoring.
  605. X                /* %#@)*, bite me */
  606. X
  607. X                reef:41 % ./hose -unix- /tmp/blah cat in
  608. X
  609. X./faucet: refusing connection from port
  610. X
  611. X                reef:43 % ./hose -unix- /tmp/blah cat in
  612. X                localport /tmp/auth
  613. X
  614. X./faucet: Got connection from /tmp/auth
  615. X
  616. X                send $14M and 2 airline stewardesses or
  617. X                you'll never see Hoffa alive
  618. X
  619. X
  620. X  Notice the elaborate quoting on that one.  faucet+hose fork a "csh
  621. X-c " to interpret the command.  The echo command had to be quoted so
  622. that faucet saw:
  623. X
  624. echo send '$14M' and 2 airline stewardesses or you\'ll never see Hoffa alive
  625. X
  626. X  faucet then performed this system call
  627. X
  628. execl("/bin/csh","csh","-c",argv[2],NULL);
  629. X
  630. X  and the "$14M" and "you'll" were properly quoted to survive THAT csh
  631. as well.  Commands to hose must be similarly protected.
  632. X
  633. X  Why use csh?
  634. X   1) This allows you to have pipelines and other fancy stuff inside
  635. X    the command, and
  636. X   2) I don't have to write code to parse the command!
  637. X
  638. X
  639. X
  640. BUGS
  641. X
  642. X  Hoohoohoo, god knows what sort of bugs are waiting to rip your
  643. ankles off.  I'm the only one who's extensively used these.  They were
  644. developed on Sun3s and Sun4s.  They have been compiled (but not
  645. extensively tested) on Sonys, DECstations and HPs.  If your machine
  646. needs different include files, start grepping and mail me the results.
  647. X
  648. X  If anything is not crystal clear, check the source and mail me.
  649. I'll try to put it in the README.
  650. X
  651. X  I need to dragoon someone into writing a man page for this.
  652. X
  653. X
  654. AUTHOR
  655. X
  656. X  Robert H. Forsman Jr.        <thoth@lightning.cis.ufl.edu>
  657. X  former underpaid Systems Programmer
  658. X  now piddly underpaid Research Assistant
  659. X  University of Florida
  660. X  Department of Computer
  661. X  and Information Science
  662. END_OF_FILE
  663. if test 6764 -ne `wc -c <'README'`; then
  664.     echo shar: \"'README'\" unpacked with wrong size!
  665. fi
  666. # end of 'README'
  667. fi
  668. if test -f 'faucet.1' -a "${1}" != "-c" ; then 
  669.   echo shar: Will not clobber existing file \"'faucet.1'\"
  670. else
  671. echo shar: Extracting \"'faucet.1'\" \(4160 characters\)
  672. sed "s/^X//" >'faucet.1' <<'END_OF_FILE'
  673. X.\" faucet.1 Copyright 3/30/1992 by Robert Forsman
  674. X.TH FAUCET 1 "Mar 30 1992"
  675. X.SH NAME
  676. faucet - a fixture for a BSD network pipe
  677. X.SH SYNOPSIS
  678. X\fBfaucet\fP \fIport\fP \fIcommand\fP (\fBin\fP|\fBout\fP|\fBerr\fP)
  679. X[\fBonce\fP] [\fBverbose\fP] [\fBquiet\fP] [\fBunix\fP]
  680. X[\fBforeignhost\fP \fIhost\fP] [\fBforeignport\fP \fIport\fP]
  681. X
  682. X.SH DESCRIPTION
  683. X.LP
  684. X.B faucet
  685. attempts to provide the functionality of pipes over the network.
  686. It behaves as the server end of a server\-client connection.
  687. When used with
  688. X.B hose(1)
  689. it can function as a replacement for
  690. X.IP
  691. tar \-cf \- . | rsh other "cd destdir; tar -xf -"
  692. X.LP
  693. X.B faucet
  694. and
  695. X.B hose
  696. are especially useful when you don't have easy access to the
  697. destination machine.
  698. X
  699. X.LP
  700. X.B faucet
  701. creates a BSD socket, binds it to the
  702. X.I port
  703. specified on the command line, and listens for connections.
  704. XEvery time
  705. X.B faucet
  706. gets a connection it forks the
  707. X.I command
  708. with stdin, stdout, and/or stderr redirected according to the 
  709. X.B in out err
  710. flags.  If the
  711. X.B once
  712. flag is specified,
  713. X.B faucet
  714. will exec(2) the
  715. X.I command
  716. instead of fork(2)ing and exec(2)ing.  
  717. X.B once
  718. means that the network pipe
  719. is only good for one shot.
  720. The
  721. X.B verbose
  722. flag specifies that
  723. X.B faucet
  724. should print information about connecting hosts .  This information
  725. includes the numeric host address, host names, and foreign port numbers.
  726. The
  727. X.B quiet
  728. flag specifies that
  729. X.B faucet
  730. should NOT print such info.
  731. X.B quiet
  732. is the default. The
  733. X.B unix
  734. flag specifies that the
  735. X.I port
  736. is not an internet port number or service name, but instead it is a
  737. filename for a UNIX domain socket.  The
  738. X.B foreignhost
  739. option specifies that faucet should reject all connections that do not
  740. come from the
  741. X.I host
  742. machine.  Similarly
  743. X.B foreignport
  744. specifies that faucet should reject all connections that are not bound
  745. on their local machine to the
  746. X.I port
  747. argument.  The above two options allow a crude form of authentication.
  748. Note that on most systems only root can bind a socket to a port number
  749. below 1024.
  750. X
  751. X.SH "EXAMPLES"
  752. X.LP
  753. This creates a TCP\-IP socket on the local machine bound to port 3000.
  754. X.IP
  755. example% faucet 3000 "tar -cf - ." out verbose
  756. X.LP
  757. XEvery time some process (from any machine) attempts to connect to
  758. port 3000 on this machine the
  759. X.B faucet
  760. program will fork(2) a process and the child will exec(2) a
  761. X.IP
  762. X/bin/csh -c "tar -cf - ."
  763. X.LP
  764. The
  765. X.B out
  766. option means that the output of the child process will have been
  767. redirected into the new socket retrieved by the accept(2) call.
  768. X.B verbose
  769. means that faucet will print information about each new connection.
  770. X
  771. X.LP
  772. This creates a UNIX domain socket in the current directory
  773. X.IP
  774. example% faucet u-socket "dd if=angio.pgm" out err once
  775. X.LP
  776. The
  777. X.B out err
  778. option means that stdout and stderr will be redirected in the child
  779. process.  The
  780. X.B once
  781. option means that the faucet will not fork(2), but exec(2) the process
  782. so that only the first process can connect to the u-socket before the
  783. faucet becomes unavailable.
  784. X
  785. X.SH "SEE ALSO"
  786. X.BR hose (1),
  787. X.BR socket (2),
  788. X.BR bind (2),
  789. X.BR listen (2),
  790. X.BR accept (2),
  791. X.BR services (5),
  792. X.BR gethostbyaddr (3)
  793. X
  794. X.SH "NOTES"
  795. X.LP
  796. Doubtless there are bugs in this program, especially in the unix domain
  797. socket portions.  I welcome problem reports and would like to make
  798. these programs as "clean" (no leftover files, sockets) as possible.
  799. X
  800. X.SH "COPYRIGHT"
  801. Copyright (C) 1992 Robert Forsman
  802. X
  803. This program is free software; you can redistribute it and/or modify
  804. it under the terms of the GNU General Public License as published by
  805. the Free Software Foundation; either version 2 of the License, or
  806. X(at your option) any later version.
  807. X
  808. This program is distributed in the hope that it will be useful,
  809. but WITHOUT ANY WARRANTY; without even the implied warranty of
  810. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  811. GNU General Public License for more details.
  812. X
  813. You should have received a copy of the GNU General Public License
  814. along with this program; if not, write to the Free Software
  815. XFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  816. X
  817. X.SH "AUTHOR"
  818. X Robert Forsman
  819. X thoth@lightning.cis.ufl.edu
  820. X University of Florida
  821. X Department of Computer and Information Science
  822. END_OF_FILE
  823. if test 4160 -ne `wc -c <'faucet.1'`; then
  824.     echo shar: \"'faucet.1'\" unpacked with wrong size!
  825. fi
  826. # end of 'faucet.1'
  827. fi
  828. if test -f 'faucet.c' -a "${1}" != "-c" ; then 
  829.   echo shar: Will not clobber existing file \"'faucet.c'\"
  830. else
  831. echo shar: Extracting \"'faucet.c'\" \(9476 characters\)
  832. sed "s/^X//" >'faucet.c' <<'END_OF_FILE'
  833. X/*
  834. X
  835. X    faucet.c, part of
  836. X    faucet and hose: network pipe utilities
  837. X    Copyright (C) 1992 Robert Forsman
  838. X
  839. X    This program is free software; you can redistribute it and/or modify
  840. X    it under the terms of the GNU General Public License as published by
  841. X    the Free Software Foundation; either version 2 of the License, or
  842. X    (at your option) any later version.
  843. X
  844. X    This program is distributed in the hope that it will be useful,
  845. X    but WITHOUT ANY WARRANTY; without even the implied warranty of
  846. X    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  847. X    GNU General Public License for more details.
  848. X
  849. X    You should have received a copy of the GNU General Public License
  850. X    along with this program; if not, write to the Free Software
  851. X    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  852. X
  853. X    */
  854. X
  855. static char info[] = "faucet: a network utility for sockets\nWritten 1992 by Robert Forsman <thoth@ufl.edu>\n";
  856. X#include    <stdio.h>
  857. X#include    <fcntl.h>
  858. X#include    <errno.h>
  859. X#ifdef hpux
  860. X#include    <signal.h>
  861. X#include    <sgtty.h>
  862. X#endif
  863. X#include    <sys/wait.h>
  864. X#include    <sys/param.h>
  865. X#include    <sys/file.h>
  866. X#include    <sys/ioctl.h>
  867. X#include    <sys/socket.h>
  868. X#include    <sys/un.h>
  869. X#include    <netinet/in.h>
  870. X#include    <netdb.h>
  871. X
  872. int    mastersocket;
  873. X#define    DOSTDOUT    (1<<0)
  874. X#define    DOSTDIN        (1<<1)
  875. X#define    DOSTDERR    (1<<2)
  876. X#define    DOONCE        (1<<3)
  877. X#define    DOVERBOSE    (1<<4)
  878. X#define    DOUNIX        (1<<5)
  879. long    doflags=0;
  880. int    running=1;
  881. X
  882. char    *foreignhost=NULL,*foreignport=NULL;
  883. int    foreignPORT;
  884. struct in_addr    foreignHOST;
  885. X
  886. char    *programname;
  887. extern int errno;
  888. extern char *sys_errlist[];
  889. X
  890. X
  891. int name_to_inet_port();
  892. X
  893. void nice_shutdown()
  894. X/* This procedure gets called when we are killed with one of the reasonable
  895. X   signals (TERM, HUP, that kind of thing).  The main while loop then
  896. X   terminates and we get a chance to clean up. */
  897. X{
  898. X  running = 0;
  899. X}
  900. X
  901. X
  902. X/* print an internet host address prettily */
  903. printhost(addr)
  904. X     struct in_addr    *addr;
  905. X{
  906. X  struct hostent    *h;
  907. X  char    *s,**p;
  908. X  int    i;
  909. X
  910. X  h = gethostbyaddr(addr, sizeof(*addr),AF_INET);
  911. X  s = (h==NULL) ? NULL : h->h_name;
  912. X
  913. X  printf("%d", ((u_char*)addr)[0]);
  914. X  for (i=1; i<sizeof(*addr); i++)
  915. X    printf(".%d",((u_char*)addr)[i]);
  916. X
  917. X  printf("(%s",s?s:"name unknown");
  918. X  if (s)
  919. X    for (p=h->h_aliases; *p; p++)
  920. X      printf(",%s",*p);
  921. X  printf(")");
  922. X}
  923. X
  924. int setup_socket(name)
  925. char *name;
  926. X/* This procedure creates a socket and handles retries on the inet domain.
  927. X   Sockets seem to "stick" on my system (SunOS [43].x) */
  928. X{
  929. X  int    sock;
  930. X
  931. X  sock = socket((doflags&DOUNIX)?AF_UNIX:AF_INET, SOCK_STREAM, 0);
  932. X  /* I need a real value for the protocol eventually.  IPPROTO_TCP sounds
  933. X     like a good value, but what about AF_UNIX sockets?  It seems to have
  934. X     worked so far... */
  935. X
  936. X  if (sock <0) {
  937. X      perror("opening stream socket");
  938. X      exit(1);
  939. X    }
  940. X
  941. X  if (!bindlocal(sock, name, (doflags&DOUNIX)?AF_UNIX:AF_INET)) {
  942. X      fprintf(stderr,"%s: error binding stream socket %s (%s)",
  943. X          programname,name,sys_errlist[errno]);
  944. X      exit(1);
  945. X    }
  946. X
  947. X  listen(sock,NOFILE);
  948. X
  949. X  return(sock);
  950. X}
  951. X
  952. X
  953. void waitonchild()
  954. X
  955. X{
  956. X  union wait status;
  957. X#if 0
  958. X  unsigned char    reason,signal,rval;
  959. X  char    buf[32];
  960. X#endif
  961. X  int    childpid;
  962. X  
  963. X  childpid = wait3(&status,WNOHANG,NULL);
  964. X  /* What a pity I can't easily print out child statuses */
  965. X#if 0
  966. X  if (childpid==-1) {
  967. X    fputs(stderr,programname);
  968. X    fputs(stderr,": error in wait3 while handling SIGCHLD (");
  969. X    fputs(stderr,sys_errlist[errno]);
  970. X    fputs(stderr,")\n");
  971. X    return;
  972. X  }
  973. X  reason = status.w_status & 0xff;
  974. X  if (reason==0) {
  975. X    rval = reason >> 8;
  976. X    if (rval!=0) {
  977. X      fputs(stderr,programname);
  978. X      fputs(stderr,": Child ");
  979. X      sprintf(buf,"%d",childpid);    fputs(stderr,buf);
  980. X      fputs(stderr," gave abnormal exit code ");
  981. X      sprintf(buf,"%d",rval);    fputs(stderr,buf);
  982. X      fputs(stderr,"\n");
  983. X    }
  984. X  } else if (reason!=0177) {
  985. X    signal = reason & 0x7f;
  986. X    fputs(stderr,programname);
  987. X    fputs(stderr,": Child ");
  988. X    sprintf(buf,"%d",childpid);    fputs(stderr,buf);
  989. X    fputs(stderr," killed by signal ");
  990. X    sprintf(buf,"%d",signal);        fputs(stderr,buf);
  991. X    fputs(stderr," (");
  992. X    fputs(stderr,(signal<=SIGUSR2)?signames[signal]:"bogus signal number");
  993. X    fputs(stderr,")\n");
  994. X  }
  995. X#endif
  996. X}
  997. X
  998. X
  999. int
  1000. authorize_address(sin)
  1001. X     struct sockaddr    *sin;
  1002. X{
  1003. X  if (doflags&DOUNIX) {
  1004. X    struct sockaddr_un     *srv = (struct sockaddr_un*)sin;
  1005. X    
  1006. X    if (foreignport != NULL && 0!=strcmp(foreignport, srv->sun_path)) {
  1007. X      if (doflags&DOVERBOSE) {
  1008. X    printf("%s: refusing connection from port %s\n",
  1009. X           programname, srv->sun_path);
  1010. X      }
  1011. X      return 0;
  1012. X    }
  1013. X  } else {
  1014. X    struct sockaddr_in    *srv = (struct sockaddr_in*)sin;
  1015. X    
  1016. X    if (foreignhost!=NULL &&
  1017. X    0!=bcmp(&srv->sin_addr,
  1018. X        &foreignHOST, sizeof(foreignHOST))) {
  1019. X      if (doflags&DOVERBOSE) {
  1020. X    printf("refusing connection from host ");
  1021. X    printhost(&srv->sin_addr);
  1022. X    printf(".\n");
  1023. X      }
  1024. X      return 0;
  1025. X    }
  1026. X    
  1027. X    if (foreignport!=NULL && foreignPORT != srv->sin_port) {
  1028. X      if (doflags&DOVERBOSE) {
  1029. X    printf("refusing connection from port %d.\n",
  1030. X           ntohs(srv->sin_port));
  1031. X      }
  1032. X      return 0;
  1033. X    }
  1034. X  }
  1035. X  
  1036. X  return 1;
  1037. X}
  1038. X
  1039. X
  1040. main (argc,argv)
  1041. int argc;
  1042. char ** argv;
  1043. X
  1044. X{
  1045. X  int    rval,length;
  1046. X  struct sockaddr    saddr;
  1047. X  struct sockaddr_in    *sinp = (struct sockaddr_in*)&saddr;
  1048. X  struct sockaddr_un    *sunp = (struct sockaddr_un*)&saddr;
  1049. X  
  1050. X  programname = argv[0];
  1051. X  
  1052. X  if (argc<3) {
  1053. X    fprintf(stderr,"Usage : %s <port> <command> (in|out|err)+ [once] [verb(|ose)] [quiet] [unix] [foreignport <port>] [foreignhost <host>]\n", programname);
  1054. X    exit(1);
  1055. X  }
  1056. X  
  1057. X  /* parse trailing args */
  1058. X  for (length=3; length<argc; length++) {
  1059. X    if (strcmp(argv[length],"in")==0)
  1060. X      doflags |= DOSTDIN;
  1061. X    else if (strcmp(argv[length],"out")==0)
  1062. X      doflags |= DOSTDOUT;
  1063. X    else if (strcmp(argv[length],"err")==0)
  1064. X      doflags |= DOSTDERR;
  1065. X    else if (strcmp(argv[length],"once")==0)
  1066. X      doflags |= DOONCE;
  1067. X    else if (strcmp(argv[length],"verbose")==0
  1068. X         || strcmp(argv[length],"verb")==0)
  1069. X      doflags |= DOVERBOSE;
  1070. X    else if (strcmp(argv[length],"quiet")==0)
  1071. X      doflags &= ~DOVERBOSE;
  1072. X    else if (strcmp(argv[length],"unix")==0)
  1073. X      doflags |= DOUNIX;
  1074. X    else if (strcmp(argv[length],"foreignport")==0) {
  1075. X      if (length+1<argc)
  1076. X    foreignport=argv[++length];
  1077. X      else
  1078. X    fprintf(stderr,"%s: foreignport requires port name or number.\n",
  1079. X        programname);
  1080. X    } else if (strcmp(argv[length],"foreignhost")==0) {
  1081. X      if (length+1<argc)
  1082. X    foreignhost=argv[++length];
  1083. X      else
  1084. X    fprintf(stderr,"%s: foreignhost requires host name or number.\n",
  1085. X        programname);
  1086. X    } else
  1087. X      fprintf(stderr,"%s: Bogus extra command line flag \"%s\".\n",
  1088. X          programname,argv[length]);
  1089. X  }
  1090. X  
  1091. X  if ( ! (doflags&(DOSTDIN|DOSTDERR|DOSTDOUT)) ) {
  1092. X    fprintf(stderr,"%s: Need at least one {in|out|err}.\n",programname);
  1093. X    exit(1);
  1094. X  }
  1095. X  
  1096. X  if ( (doflags&DOUNIX) && foreignhost!=NULL ) {
  1097. X    fprintf(stderr, "%s: foreignhost parameter makes no sense with UNIX domain sockets, ignoring.\n", programname);
  1098. X    foreignhost = NULL;
  1099. X  }
  1100. X  
  1101. X  signal(SIGCHLD,waitonchild);
  1102. X  
  1103. X  mastersocket = setup_socket(argv[1]);
  1104. X  
  1105. X  signal(SIGHUP, nice_shutdown);
  1106. X  signal(SIGINT, nice_shutdown);
  1107. X  signal(SIGPIPE, nice_shutdown);
  1108. X  signal(SIGALRM, nice_shutdown);
  1109. X  signal(SIGTERM, nice_shutdown);
  1110. X  
  1111. X  if (foreignhost != NULL && !convert_hostname(foreignhost, &foreignHOST)) {
  1112. X    fprintf(stderr, "%s: could not translate %s to a host address\n",
  1113. X        programname, foreignhost);
  1114. X    exit(1);
  1115. X  }
  1116. X  
  1117. X  if (foreignport!=NULL && !(doflags&DOUNIX) &&
  1118. X      0 == (foreignPORT = name_to_inet_port(foreignport)) ) {
  1119. X    fprintf(stderr,"%s: port %s unknown.\n",programname,foreignport);
  1120. X    exit(1);
  1121. X  }
  1122. X  
  1123. X  while (running) {
  1124. X    
  1125. X    length = sizeof(saddr);
  1126. X    
  1127. X    rval = accept(mastersocket,&saddr,&length);
  1128. X    
  1129. X    if (rval<0) {
  1130. X      if (errno==EWOULDBLOCK) {
  1131. X    printf("%s: No more connections to talk to.\n",programname);
  1132. X      } else if (errno!=EINTR) {
  1133. X    fprintf(stderr,"%s: error in accept (%s).",
  1134. X        programname,sys_errlist[errno]);
  1135. X    exit(1);
  1136. X      }
  1137. X      continue;
  1138. X    }
  1139. X    
  1140. X    if (!authorize_address(&saddr)) {
  1141. X      close(rval);
  1142. X      continue;
  1143. X    }
  1144. X    
  1145. X    if ( doflags&DOVERBOSE ) {
  1146. X      printf("%s: Got connection from ",programname);
  1147. X      if ( doflags&DOUNIX ) {
  1148. X    printf("%s\n", sunp->sun_path);
  1149. X      } else {
  1150. X    printhost(&sinp->sin_addr);
  1151. X    printf(" port %d\n",ntohs(sinp->sin_port));
  1152. X      }
  1153. X    }
  1154. X    
  1155. X    fflush(stdout);
  1156. X    
  1157. X    if ( doflags&DOONCE || fork()==0 ) {
  1158. X      /* child process: frob descriptors and exec */
  1159. X      char    *s;
  1160. X      
  1161. X      if ( (doflags&(DOONCE|DOUNIX)) == (DOONCE|DOUNIX) )
  1162. X    unlink(argv[1]);
  1163. X      /* We don't want the unix domain socket anymore */
  1164. X      
  1165. X      dup2(fileno(stderr),mastersocket);
  1166. X      ioctl(mastersocket,FIOCLEX,NULL);
  1167. X      /* We don't need old stderr hanging around after an exec.
  1168. X     The mastersocket has been closed by the dup2 */
  1169. X      
  1170. X      if (doflags & DOSTDIN)
  1171. X    dup2(rval,fileno(stdin));
  1172. X      if (doflags & DOSTDOUT)
  1173. X    dup2(rval,fileno(stdout));
  1174. X      if (doflags & DOSTDERR)
  1175. X    dup2(rval,fileno(stderr));
  1176. X      
  1177. X      close(rval); /* rval has been properly duplicated */
  1178. X      
  1179. X      execl("/bin/csh","csh","-c",argv[2],NULL);
  1180. X      s ="exec failed\n";
  1181. X      write(mastersocket,s,strlen(s));
  1182. X      exit(0);
  1183. X    } else {
  1184. X      /* parent: close socket.
  1185. X     Signal will arrive upon death of child. */
  1186. X      close(rval);
  1187. X    }
  1188. X  }
  1189. X  
  1190. X  /* clean up the socket when we're done */
  1191. X  if (doflags&DOUNIX)
  1192. X    unlink(argv[1]);
  1193. X  close(mastersocket);
  1194. X  
  1195. X}
  1196. END_OF_FILE
  1197. if test 9476 -ne `wc -c <'faucet.c'`; then
  1198.     echo shar: \"'faucet.c'\" unpacked with wrong size!
  1199. fi
  1200. # end of 'faucet.c'
  1201. fi
  1202. if test -f 'hose.1' -a "${1}" != "-c" ; then 
  1203.   echo shar: Will not clobber existing file \"'hose.1'\"
  1204. else
  1205. echo shar: Extracting \"'hose.1'\" \(3029 characters\)
  1206. sed "s/^X//" >'hose.1' <<'END_OF_FILE'
  1207. X.\" hose.1 Copyright 3/30/1992 by Robert Forsman
  1208. X.TH HOSE 1 "Mar 30 1992"
  1209. X.SH NAME
  1210. hose - the client end of a BSD network pipe
  1211. X.SH SYNOPSIS
  1212. X\fBhose\fP \fIhostname\fP \fIport\fP \fIcommand\fP
  1213. X(\fBin\fP|\fBout\fP|\fBerr\fP) [\fBunix\fP] [\fBlocalport\fP \fIport\fP]
  1214. X
  1215. X.SH DESCRIPTION
  1216. X.LP
  1217. X.B hose
  1218. attempts to provide the functionality of pipes over the network.
  1219. It behaves as the client end of a server\-client connection.
  1220. When used with
  1221. X.B faucet(1)
  1222. it can function as a replacement for
  1223. X.IP
  1224. tar \-cf \- . | rsh other "cd destdir; tar -xf -"
  1225. X.LP
  1226. X.B faucet
  1227. and
  1228. X.B hose
  1229. are especially useful when you don't have easy access to the
  1230. destination machine.
  1231. X
  1232. X.LP
  1233. X.B hose
  1234. creates a BSD socket and, if the
  1235. X.B localport
  1236. option is used, binds it to the port number (or service name)
  1237. specified immediately afterwards.
  1238. X.B hose
  1239. then tries to connect to the foreign machine
  1240. X.I hostname
  1241. with foreign port
  1242. X.I port
  1243. X .  If successful
  1244. X.B hose
  1245. redirects stdin, stdout, and/or stderr
  1246. according to the
  1247. X.B in out err
  1248. flags.
  1249. X.B hose
  1250. then exec(2)s a csh -f "\fIcommand\fP".
  1251. The
  1252. X.B unix
  1253. flag specifies that the
  1254. X.I port
  1255. is not an internet port number or service name, but instead it is a
  1256. filename for a UNIX domain socket.  This option may be simulated by
  1257. using 
  1258. X.B -unix-
  1259. as the host name to connect to, or by renaming the
  1260. X.B hose
  1261. program to \fBuhose\fP.
  1262. X
  1263. X.SH "EXAMPLES"
  1264. X.LP
  1265. This will connect to port 3000 on the machine reef and connect the socket
  1266. to the stdin of a tar command.
  1267. X.IP
  1268. example% hose reef 3000 "tar -xf - ." in
  1269. X.LP
  1270. The command actually exec(2)ed by the
  1271. X.B hose
  1272. program is
  1273. X.IP
  1274. X/bin/csh -c "tar -xf - ."
  1275. X.LP
  1276. The
  1277. X.B in
  1278. option means that the input of the child process will have been
  1279. redirected into the socket connected to reef.
  1280. X
  1281. X.LP
  1282. This connects to a UNIX domain socket in the current directory
  1283. X.IP
  1284. example% hose u-socket "dd of=sample.pgm" in
  1285. X.LP
  1286. X
  1287. X.SH "SEE ALSO"
  1288. X.BR faucet (1),
  1289. X.BR socket (2),
  1290. X.BR bind (2),
  1291. X.BR listen (2),
  1292. X.BR accept (2),
  1293. X.BR services (5),
  1294. X.BR gethostbyaddr (3)
  1295. X
  1296. X.SH "NOTES"
  1297. X.LP
  1298. Doubtless there are bugs in this program, especially in the unix domain
  1299. socket portions.  I welcome problem reports and would like to make
  1300. these programs as "clean" (no leftover files, sockets) as possible.
  1301. X
  1302. X.SH "COPYRIGHT"
  1303. Copyright (C) 1992 Robert Forsman
  1304. X
  1305. This program is free software; you can redistribute it and/or modify
  1306. it under the terms of the GNU General Public License as published by
  1307. the Free Software Foundation; either version 2 of the License, or
  1308. X(at your option) any later version.
  1309. X
  1310. This program is distributed in the hope that it will be useful,
  1311. but WITHOUT ANY WARRANTY; without even the implied warranty of
  1312. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  1313. GNU General Public License for more details.
  1314. X
  1315. You should have received a copy of the GNU General Public License
  1316. along with this program; if not, write to the Free Software
  1317. XFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  1318. X
  1319. X.SH "AUTHOR"
  1320. X Robert Forsman
  1321. X thoth@lightning.cis.ufl.edu
  1322. X University of Florida
  1323. X Department of Computer and Information Science
  1324. END_OF_FILE
  1325. if test 3029 -ne `wc -c <'hose.1'`; then
  1326.     echo shar: \"'hose.1'\" unpacked with wrong size!
  1327. fi
  1328. # end of 'hose.1'
  1329. fi
  1330. if test -f 'hose.c' -a "${1}" != "-c" ; then 
  1331.   echo shar: Will not clobber existing file \"'hose.c'\"
  1332. else
  1333. echo shar: Extracting \"'hose.c'\" \(4891 characters\)
  1334. sed "s/^X//" >'hose.c' <<'END_OF_FILE'
  1335. X/*
  1336. X
  1337. X    hose.c, part of
  1338. X    faucet and hose: network pipe utilities
  1339. X    Copyright (C) 1992 Robert Forsman
  1340. X
  1341. X    This program is free software; you can redistribute it and/or modify
  1342. X    it under the terms of the GNU General Public License as published by
  1343. X    the Free Software Foundation; either version 2 of the License, or
  1344. X    (at your option) any later version.
  1345. X
  1346. X    This program is distributed in the hope that it will be useful,
  1347. X    but WITHOUT ANY WARRANTY; without even the implied warranty of
  1348. X    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  1349. X    GNU General Public License for more details.
  1350. X
  1351. X    You should have received a copy of the GNU General Public License
  1352. X    along with this program; if not, write to the Free Software
  1353. X    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  1354. X
  1355. X    */
  1356. X
  1357. static char info[] = "hose: a network utility for sockets\nWritten 1992 by Robert Forsman <thoth@ufl.edu>\n";
  1358. X#include    <stdio.h>
  1359. X#include    <fcntl.h>
  1360. X#ifdef hpux
  1361. X#include    <sgtty.h>
  1362. X#endif
  1363. X#include    <sys/errno.h>
  1364. X#include    <sys/param.h>
  1365. X#include    <sys/file.h>
  1366. X#include    <sys/ioctl.h>
  1367. X#include    <sys/socket.h>
  1368. X#include    <sys/un.h>
  1369. X#include    <netinet/in.h>
  1370. X#include    <netdb.h>
  1371. X
  1372. X#define    DOSTDOUT    (1<<0)
  1373. X#define    DOSTDIN        (1<<1)
  1374. X#define    DOSTDERR    (1<<2)
  1375. X#define    DOUNIX        (1<<3)
  1376. int    doflags=0;
  1377. char    *localport=NULL;
  1378. char    *programname;
  1379. extern int    errno;
  1380. extern char *sys_errlist[];
  1381. X
  1382. X
  1383. int name_to_inet_port();
  1384. X
  1385. X
  1386. int setup_socket(hostname,portname)
  1387. char    *hostname;
  1388. char    *portname;
  1389. X
  1390. X{
  1391. X  int    sock;
  1392. X  struct sockaddr    server;
  1393. X  int length;
  1394. X  
  1395. X  sock = socket((doflags&DOUNIX)?AF_UNIX:AF_INET, SOCK_STREAM, 0);
  1396. X  if (sock <0) {
  1397. X    perror("opening stream socket");
  1398. X    exit(1);
  1399. X  }
  1400. X  
  1401. X  length = sizeof(server);
  1402. X  
  1403. X  if (localport != NULL &&
  1404. X      !bindlocal(sock, localport, (doflags&DOUNIX)?AF_UNIX:AF_INET) ) {
  1405. X    fprintf(stderr,"%s: error binding stream socket %s (%s)",
  1406. X        programname,localport,sys_errlist[errno]);
  1407. X    exit(1);
  1408. X  }
  1409. X  
  1410. X  if (doflags&DOUNIX) {
  1411. X    /* ignore the hostname parameter */
  1412. X    ((struct sockaddr_un*)&server)->sun_family = AF_UNIX;
  1413. X    strcpy( ((struct sockaddr_un*)&server)->sun_path, portname);
  1414. X  } else {
  1415. X    struct sockaddr_in *svr=(struct sockaddr_in *)&server;
  1416. X    
  1417. X    ((struct sockaddr_in*)&server)->sin_family = AF_INET;
  1418. X    
  1419. X    if (!convert_hostname(hostname, &svr->sin_addr)) {
  1420. X      fprintf(stderr, "%s: could not translate %s to a host address\n",
  1421. X          programname, hostname);
  1422. X      exit(1);
  1423. X    }
  1424. X    
  1425. X    svr->sin_port = name_to_inet_port(portname);
  1426. X    if (svr->sin_port==0) {
  1427. X      fprintf(stderr,"%s: bogus port number %s\n",programname,portname);
  1428. X      exit(1);
  1429. X    }
  1430. X  }
  1431. X  
  1432. X  if (connect(sock,(struct sockaddr*)&server,sizeof(server)) < 0) {
  1433. X    perror("connecting");
  1434. X    exit(1);
  1435. X  }
  1436. X  
  1437. X  return(sock);
  1438. X}
  1439. X
  1440. X
  1441. X
  1442. X
  1443. main (argc,argv)
  1444. X     int argc;
  1445. X     char ** argv;
  1446. X     
  1447. X{
  1448. X  int    rval,length;
  1449. X  
  1450. X  programname=argv[0];
  1451. X  
  1452. X  if (argc<4) {
  1453. X    fprintf(stderr,"Usage : %s <hostname> <port> <command> (in|out|err)+ [unix] [localport <port>]}\n",programname);
  1454. X    exit(1);
  1455. X  }
  1456. X  if (strcmp(argv[1],"-unix-")==0 || strcmp(programname,"uhose")==0 )
  1457. X    doflags |= DOUNIX;
  1458. X  for (length=4; length<argc; length++) {
  1459. X    if (strcmp(argv[length],"in")==0)
  1460. X      doflags |= DOSTDIN;
  1461. X    else if (strcmp(argv[length],"out")==0)
  1462. X      doflags |= DOSTDOUT;
  1463. X    else if (strcmp(argv[length],"err")==0)
  1464. X      doflags |= DOSTDERR;
  1465. X    else if (strcmp(argv[length],"unix")==0)
  1466. X      doflags |= DOUNIX;
  1467. X    else if (strcmp(argv[length],"localport")==0) {
  1468. X      if (length+1<argc)
  1469. X    localport=argv[++length];
  1470. X      else
  1471. X    fprintf(stderr,"%s: localport requires port name or number after.\n",
  1472. X        programname);
  1473. X    } else
  1474. X      fprintf(stderr,"%s: Bogus extra command line flag \"%s\".\n",
  1475. X          programname,argv[length]);
  1476. X  }
  1477. X  
  1478. X  if ( ! (doflags&(DOSTDIN|DOSTDERR|DOSTDOUT)) ) {
  1479. X    fprintf(stderr,"%s: Need at least one {in|out|err}.\n",programname);
  1480. X    exit(1);
  1481. X  }
  1482. X  
  1483. X  rval = setup_socket(argv[1],argv[2]);
  1484. X  
  1485. X  if (doflags&DOUNIX && localport!=NULL)
  1486. X    unlink(localport);
  1487. X  
  1488. X#if 0
  1489. X  if (!fork()) {
  1490. X    int sparefd;
  1491. X    char *s;
  1492. X    
  1493. X    sparefd = dup(fileno(stderr));
  1494. X    ioctl(sparefd,FIOCLEX,NULL);
  1495. X    
  1496. X    if (!(doflags & DOSTDIN))
  1497. X      dup2(rval,fileno(stdin));
  1498. X    if (!(doflags & DOSTDOUT))
  1499. X      dup2(rval,fileno(stdin));
  1500. X    if (!(doflags & DOSTDERR))
  1501. X      dup2(rval,fileno(stderr));
  1502. X    close(rval);
  1503. X    
  1504. X    execl("/bin/cat",NULL);
  1505. X    s ="exec failed\n";
  1506. X    write(sparefd,s,strlen(s));
  1507. X    exit(1);
  1508. X  }
  1509. X#endif
  1510. X  {
  1511. X    int sparefd;
  1512. X    char *s;
  1513. X    
  1514. X    sparefd = dup(fileno(stderr));
  1515. X    ioctl(sparefd,FIOCLEX,NULL);
  1516. X    
  1517. X    if (doflags & DOSTDIN)
  1518. X      dup2(rval,fileno(stdin));
  1519. X    if (doflags & DOSTDOUT)
  1520. X      dup2(rval,fileno(stdout));
  1521. X    if (doflags & DOSTDERR)
  1522. X      dup2(rval,fileno(stderr));
  1523. X    close(rval);
  1524. X    
  1525. X    execl("/bin/csh","csh","-c",argv[3],NULL);
  1526. X    s ="exec failed\n";
  1527. X    write(sparefd,s,strlen(s));
  1528. X    exit(1);
  1529. X  }
  1530. X}
  1531. END_OF_FILE
  1532. if test 4891 -ne `wc -c <'hose.c'`; then
  1533.     echo shar: \"'hose.c'\" unpacked with wrong size!
  1534. fi
  1535. # end of 'hose.c'
  1536. fi
  1537. if test -f 'portname.c' -a "${1}" != "-c" ; then 
  1538.   echo shar: Will not clobber existing file \"'portname.c'\"
  1539. else
  1540. echo shar: Extracting \"'portname.c'\" \(2973 characters\)
  1541. sed "s/^X//" >'portname.c' <<'END_OF_FILE'
  1542. X/*
  1543. X
  1544. X    portname.c, part of
  1545. X    faucet and hose: network pipe utilities
  1546. X    Copyright (C) 1992 Robert Forsman
  1547. X
  1548. X    This program is free software; you can redistribute it and/or modify
  1549. X    it under the terms of the GNU General Public License as published by
  1550. X    the Free Software Foundation; either version 2 of the License, or
  1551. X    (at your option) any later version.
  1552. X
  1553. X    This program is distributed in the hope that it will be useful,
  1554. X    but WITHOUT ANY WARRANTY; without even the implied warranty of
  1555. X    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  1556. X    GNU General Public License for more details.
  1557. X
  1558. X    You should have received a copy of the GNU General Public License
  1559. X    along with this program; if not, write to the Free Software
  1560. X    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  1561. X
  1562. X    */
  1563. X
  1564. X#include    <stdio.h>
  1565. X#include    <fcntl.h>
  1566. X#include    <errno.h>
  1567. X#include    <sys/socket.h>
  1568. X#include    <sys/un.h>
  1569. X#include    <netdb.h>
  1570. X#include    <netinet/in.h>
  1571. X
  1572. int name_to_inet_port(portname)
  1573. char *portname;
  1574. X/* This procedure converts a character string to a port number.  It looks
  1575. X   up the service by name and if there is none, then it converts the string
  1576. X   to a number with sscanf */
  1577. X{
  1578. X  struct servent    *p;
  1579. X
  1580. X  if (portname==NULL)
  1581. X    return 0;
  1582. X
  1583. X  p = getservbyname(portname,"tcp");
  1584. X  if (p!=NULL)
  1585. X    {
  1586. X      return p->s_port;
  1587. X    }
  1588. X  else
  1589. X    {
  1590. X      int    port;
  1591. X      if (sscanf(portname,"%i",&port)!=1)
  1592. X    {
  1593. X      return 0;
  1594. X    }
  1595. X      else
  1596. X    return htons(port);
  1597. X    }
  1598. X}
  1599. X
  1600. int
  1601. convert_hostname(name, addr)
  1602. X     char    *name;
  1603. X     struct in_addr    *addr;
  1604. X{
  1605. X  struct hostent    *hp;
  1606. X  int        len;
  1607. X
  1608. X  hp = gethostbyname(name);
  1609. X  if (hp != NULL)
  1610. X    bcopy(hp->h_addr,addr,hp->h_length);
  1611. X  else
  1612. X    {
  1613. X      int    count;
  1614. X      unsigned int    a1,a2,a3,a4;
  1615. X      count = sscanf(name,"%i.%i.%i.%i%n", &a1, &a2, &a3, &a4, &len);
  1616. X      if (4!=count || 0!=name[len] )
  1617. X    return 0;
  1618. X      addr->S_un.S_un_b.s_b1 = a1;
  1619. X      addr->S_un.S_un_b.s_b2 = a2;
  1620. X      addr->S_un.S_un_b.s_b3 = a3;
  1621. X      addr->S_un.S_un_b.s_b4 = a4;
  1622. X    }
  1623. X  return 1;
  1624. X}
  1625. X
  1626. X
  1627. int
  1628. bindlocal(fd, name, domain)
  1629. X     int    fd, domain;
  1630. X     char    *name;
  1631. X{
  1632. X  struct sockaddr    laddr;
  1633. X  int    countdown;
  1634. X  int    rval;
  1635. X  
  1636. X  if (domain==AF_INET)
  1637. X    {
  1638. X      struct sockaddr_in    *srv = (struct sockaddr_in*)&laddr;
  1639. X      
  1640. X      srv->sin_family = AF_INET;
  1641. X      srv->sin_addr.s_addr = INADDR_ANY;
  1642. X      
  1643. X      srv->sin_port = name_to_inet_port(name);
  1644. X      
  1645. X      if (srv->sin_port==0)
  1646. X    {
  1647. X      fprintf(stderr, "port %s unknown\n", name);
  1648. X      return 0;
  1649. X    }
  1650. X    }
  1651. X  else
  1652. X    {
  1653. X      struct sockaddr_un    *srv = (struct sockaddr_un *)&laddr;
  1654. X      
  1655. X      srv->sun_family = AF_UNIX;
  1656. X      strcpy(srv->sun_path, name);
  1657. X    }
  1658. X  
  1659. X  countdown= (domain==AF_UNIX)?1:10;
  1660. X  do {
  1661. X    rval = bind(fd, &laddr, sizeof(laddr));
  1662. X    if (rval)
  1663. X      if (errno==EADDRINUSE && --countdown>0)
  1664. X    {
  1665. X      fprintf(stderr,"Address %s in use, sleeping 10.\n",
  1666. X          name);
  1667. X      sleep (10);
  1668. X      fprintf(stderr,"Trying again . . .\n");
  1669. X    }
  1670. X      else
  1671. X    return 0;
  1672. X  } while (rval);
  1673. X
  1674. X  return 1;
  1675. X}
  1676. END_OF_FILE
  1677. if test 2973 -ne `wc -c <'portname.c'`; then
  1678.     echo shar: \"'portname.c'\" unpacked with wrong size!
  1679. fi
  1680. # end of 'portname.c'
  1681. fi
  1682. echo shar: End of archive 1 \(of 1\).
  1683. cp /dev/null ark1isdone
  1684. MISSING=""
  1685. for I in 1 ; do
  1686.     if test ! -f ark${I}isdone ; then
  1687.     MISSING="${MISSING} ${I}"
  1688.     fi
  1689. done
  1690. if test "${MISSING}" = "" ; then
  1691.     echo You have the archive.
  1692.     rm -f ark[1-9]isdone
  1693. else
  1694.     echo You still need to unpack the following archives:
  1695.     echo "        " ${MISSING}
  1696. fi
  1697. ##  End of shell archive.
  1698. exit 0
  1699.