Source code of Windows XP (NT5)
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.

370 lines
9.5 KiB

  1. # chat.pl: chat with a server
  2. # Based on: V2.01.alpha.7 91/06/16
  3. # Randal L. Schwartz (was <[email protected]>)
  4. # multihome additions by [email protected]
  5. # allow for /dev/pts based systems by Joe Doupnik <[email protected]>
  6. package chat;
  7. require 'sys/socket.ph';
  8. if( defined( &main'PF_INET ) ){
  9. $pf_inet = &main'PF_INET;
  10. $sock_stream = &main'SOCK_STREAM;
  11. local($name, $aliases, $proto) = getprotobyname( 'tcp' );
  12. $tcp_proto = $proto;
  13. }
  14. else {
  15. # XXX hardwired $PF_INET, $SOCK_STREAM, 'tcp'
  16. # but who the heck would change these anyway? (:-)
  17. $pf_inet = 2;
  18. $sock_stream = 1;
  19. $tcp_proto = 6;
  20. }
  21. $sockaddr = 'S n a4 x8';
  22. chop($thishost = `hostname`);
  23. # *S = symbol for current I/O, gets assigned *chatsymbol....
  24. $next = "chatsymbol000000"; # next one
  25. $nextpat = "^chatsymbol"; # patterns that match next++, ++, ++, ++
  26. ## $handle = &chat'open_port("server.address",$port_number);
  27. ## opens a named or numbered TCP server
  28. sub open_port { ## public
  29. local($server, $port) = @_;
  30. local($serveraddr,$serverproc);
  31. # We may be multi-homed, start with 0, fixup once connexion is made
  32. $thisaddr = "\0\0\0\0" ;
  33. $thisproc = pack($sockaddr, 2, 0, $thisaddr);
  34. *S = ++$next;
  35. if ($server =~ /^(\d+)+\.(\d+)\.(\d+)\.(\d+)$/) {
  36. $serveraddr = pack('C4', $1, $2, $3, $4);
  37. } else {
  38. local(@x) = gethostbyname($server);
  39. return undef unless @x;
  40. $serveraddr = $x[4];
  41. }
  42. $serverproc = pack($sockaddr, 2, $port, $serveraddr);
  43. unless (socket(S, $pf_inet, $sock_stream, $tcp_proto)) {
  44. ($!) = ($!, close(S)); # close S while saving $!
  45. return undef;
  46. }
  47. unless (bind(S, $thisproc)) {
  48. ($!) = ($!, close(S)); # close S while saving $!
  49. return undef;
  50. }
  51. unless (connect(S, $serverproc)) {
  52. ($!) = ($!, close(S)); # close S while saving $!
  53. return undef;
  54. }
  55. # We opened with the local address set to ANY, at this stage we know
  56. # which interface we are using. This is critical if our machine is
  57. # multi-homed, with IP forwarding off, so fix-up.
  58. local($fam,$lport);
  59. ($fam,$lport,$thisaddr) = unpack($sockaddr, getsockname(S));
  60. $thisproc = pack($sockaddr, 2, 0, $thisaddr);
  61. # end of post-connect fixup
  62. select((select(S), $| = 1)[0]);
  63. $next; # return symbol for switcharound
  64. }
  65. ## ($host, $port, $handle) = &chat'open_listen([$port_number]);
  66. ## opens a TCP port on the current machine, ready to be listened to
  67. ## if $port_number is absent or zero, pick a default port number
  68. ## process must be uid 0 to listen to a low port number
  69. sub open_listen { ## public
  70. *S = ++$next;
  71. local($thisport) = shift || 0;
  72. local($thisproc_local) = pack($sockaddr, 2, $thisport, $thisaddr);
  73. local(*NS) = "__" . time;
  74. unless (socket(NS, $pf_inet, $sock_stream, $tcp_proto)) {
  75. ($!) = ($!, close(NS));
  76. return undef;
  77. }
  78. unless (bind(NS, $thisproc_local)) {
  79. ($!) = ($!, close(NS));
  80. return undef;
  81. }
  82. unless (listen(NS, 1)) {
  83. ($!) = ($!, close(NS));
  84. return undef;
  85. }
  86. select((select(NS), $| = 1)[0]);
  87. local($family, $port, @myaddr) =
  88. unpack("S n C C C C x8", getsockname(NS));
  89. $S{"needs_accept"} = *NS; # so expect will open it
  90. (@myaddr, $port, $next); # returning this
  91. }
  92. ## $handle = &chat'open_proc("command","arg1","arg2",...);
  93. ## opens a /bin/sh on a pseudo-tty
  94. sub open_proc { ## public
  95. local(@cmd) = @_;
  96. *S = ++$next;
  97. local(*TTY) = "__TTY" . time;
  98. local($pty,$tty) = &_getpty(S,TTY);
  99. die "Cannot find a new pty" unless defined $pty;
  100. $pid = fork;
  101. die "Cannot fork: $!" unless defined $pid;
  102. unless ($pid) {
  103. close STDIN; close STDOUT; close STDERR;
  104. setpgrp(0,$$);
  105. if (open(DEVTTY, "/dev/tty")) {
  106. ioctl(DEVTTY,0x20007471,0); # XXX s/b &TIOCNOTTY
  107. close DEVTTY;
  108. }
  109. open(STDIN,"<&TTY");
  110. open(STDOUT,">&TTY");
  111. open(STDERR,">&STDOUT");
  112. die "Oops" unless fileno(STDERR) == 2; # sanity
  113. close(S);
  114. exec @cmd;
  115. die "Cannot exec @cmd: $!";
  116. }
  117. close(TTY);
  118. $next; # return symbol for switcharound
  119. }
  120. # $S is the read-ahead buffer
  121. ## $return = &chat'expect([$handle,] $timeout_time,
  122. ## $pat1, $body1, $pat2, $body2, ... )
  123. ## $handle is from previous &chat'open_*().
  124. ## $timeout_time is the time (either relative to the current time, or
  125. ## absolute, ala time(2)) at which a timeout event occurs.
  126. ## $pat1, $pat2, and so on are regexs which are matched against the input
  127. ## stream. If a match is found, the entire matched string is consumed,
  128. ## and the corresponding body eval string is evaled.
  129. ##
  130. ## Each pat is a regular-expression (probably enclosed in single-quotes
  131. ## in the invocation). ^ and $ will work, respecting the current value of $*.
  132. ## If pat is 'TIMEOUT', the body is executed if the timeout is exceeded.
  133. ## If pat is 'EOF', the body is executed if the process exits before
  134. ## the other patterns are seen.
  135. ##
  136. ## Pats are scanned in the order given, so later pats can contain
  137. ## general defaults that won't be examined unless the earlier pats
  138. ## have failed.
  139. ##
  140. ## The result of eval'ing body is returned as the result of
  141. ## the invocation. Recursive invocations are not thought
  142. ## through, and may work only accidentally. :-)
  143. ##
  144. ## undef is returned if either a timeout or an eof occurs and no
  145. ## corresponding body has been defined.
  146. ## I/O errors of any sort are treated as eof.
  147. $nextsubname = "expectloop000000"; # used for subroutines
  148. sub expect { ## public
  149. if ($_[0] =~ /$nextpat/) {
  150. *S = shift;
  151. }
  152. local($endtime) = shift;
  153. local($timeout,$eof) = (1,1);
  154. local($caller) = caller;
  155. local($rmask, $nfound, $timeleft, $thisbuf);
  156. local($cases, $pattern, $action, $subname);
  157. $endtime += time if $endtime < 600_000_000;
  158. if (defined $S{"needs_accept"}) { # is it a listen socket?
  159. local(*NS) = $S{"needs_accept"};
  160. delete $S{"needs_accept"};
  161. $S{"needs_close"} = *NS;
  162. unless(accept(S,NS)) {
  163. ($!) = ($!, close(S), close(NS));
  164. return undef;
  165. }
  166. select((select(S), $| = 1)[0]);
  167. }
  168. # now see whether we need to create a new sub:
  169. unless ($subname = $expect_subname{$caller,@_}) {
  170. # nope. make a new one:
  171. $expect_subname{$caller,@_} = $subname = $nextsubname++;
  172. $cases .= <<"EDQ"; # header is funny to make everything elsif's
  173. sub $subname {
  174. LOOP: {
  175. if (0) { ; }
  176. EDQ
  177. while (@_) {
  178. ($pattern,$action) = splice(@_,0,2);
  179. if ($pattern =~ /^eof$/i) {
  180. $cases .= <<"EDQ";
  181. elsif (\$eof) {
  182. package $caller;
  183. $action;
  184. }
  185. EDQ
  186. $eof = 0;
  187. } elsif ($pattern =~ /^timeout$/i) {
  188. $cases .= <<"EDQ";
  189. elsif (\$timeout) {
  190. package $caller;
  191. $action;
  192. }
  193. EDQ
  194. $timeout = 0;
  195. } else {
  196. $pattern =~ s#/#\\/#g;
  197. $cases .= <<"EDQ";
  198. elsif (\$S =~ /$pattern/) {
  199. \$S = \$';
  200. package $caller;
  201. $action;
  202. }
  203. EDQ
  204. }
  205. }
  206. $cases .= <<"EDQ" if $eof;
  207. elsif (\$eof) {
  208. undef;
  209. }
  210. EDQ
  211. $cases .= <<"EDQ" if $timeout;
  212. elsif (\$timeout) {
  213. undef;
  214. }
  215. EDQ
  216. $cases .= <<'ESQ';
  217. else {
  218. $rmask = "";
  219. vec($rmask,fileno(S),1) = 1;
  220. ($nfound, $rmask) =
  221. select($rmask, undef, undef, $endtime - time);
  222. if ($nfound) {
  223. $nread = sysread(S, $thisbuf, 1024);
  224. if ($nread > 0) {
  225. $S .= $thisbuf;
  226. } else {
  227. $eof++, redo LOOP; # any error is also eof
  228. }
  229. } else {
  230. $timeout++, redo LOOP; # timeout
  231. }
  232. redo LOOP;
  233. }
  234. }
  235. }
  236. ESQ
  237. eval $cases; die "$cases:\n$@" if $@;
  238. }
  239. $eof = $timeout = 0;
  240. do $subname();
  241. }
  242. ## &chat'print([$handle,] @data)
  243. ## $handle is from previous &chat'open().
  244. ## like print $handle @data
  245. sub print { ## public
  246. if ($_[0] =~ /$nextpat/) {
  247. *S = shift;
  248. }
  249. local $out = join $, , @_;
  250. syswrite(S, $out, length $out);
  251. if( $chat'debug ){
  252. print STDERR "printed:";
  253. print STDERR @_;
  254. }
  255. }
  256. ## &chat'close([$handle,])
  257. ## $handle is from previous &chat'open().
  258. ## like close $handle
  259. sub close { ## public
  260. if ($_[0] =~ /$nextpat/) {
  261. *S = shift;
  262. }
  263. close(S);
  264. if (defined $S{"needs_close"}) { # is it a listen socket?
  265. local(*NS) = $S{"needs_close"};
  266. delete $S{"needs_close"};
  267. close(NS);
  268. }
  269. }
  270. ## @ready_handles = &chat'select($timeout, @handles)
  271. ## select()'s the handles with a timeout value of $timeout seconds.
  272. ## Returns an array of handles that are ready for I/O.
  273. ## Both user handles and chat handles are supported (but beware of
  274. ## stdio's buffering for user handles).
  275. sub select { ## public
  276. local($timeout) = shift;
  277. local(@handles) = @_;
  278. local(%handlename) = ();
  279. local(%ready) = ();
  280. local($caller) = caller;
  281. local($rmask) = "";
  282. for (@handles) {
  283. if (/$nextpat/o) { # one of ours... see if ready
  284. local(*SYM) = $_;
  285. if (length($SYM)) {
  286. $timeout = 0; # we have a winner
  287. $ready{$_}++;
  288. }
  289. $handlename{fileno($_)} = $_;
  290. } else {
  291. $handlename{fileno(/'/ ? $_ : "$caller\'$_")} = $_;
  292. }
  293. }
  294. for (sort keys %handlename) {
  295. vec($rmask, $_, 1) = 1;
  296. }
  297. select($rmask, undef, undef, $timeout);
  298. for (sort keys %handlename) {
  299. $ready{$handlename{$_}}++ if vec($rmask,$_,1);
  300. }
  301. sort keys %ready;
  302. }
  303. # ($pty,$tty) = $chat'_getpty(PTY,TTY):
  304. # internal procedure to get the next available pty.
  305. # opens pty on handle PTY, and matching tty on handle TTY.
  306. # returns undef if can't find a pty.
  307. # Modify "/dev/pty" to "/dev/pts" for Dell Unix v2.2 (aka SVR4.04). Joe Doupnik.
  308. sub _getpty { ## private
  309. local($_PTY,$_TTY) = @_;
  310. $_PTY =~ s/^([^']+)$/(caller)[$[]."'".$1/e;
  311. $_TTY =~ s/^([^']+)$/(caller)[$[]."'".$1/e;
  312. local($pty, $tty, $kind);
  313. if( -e "/dev/pts000" ){ ## mods by Joe Doupnik Dec 1992
  314. $kind = "pts"; ## SVR4 Streams
  315. } else {
  316. $kind = "pty"; ## BSD Clist stuff
  317. }
  318. for $bank (112..127) {
  319. next unless -e sprintf("/dev/$kind%c0", $bank);
  320. for $unit (48..57) {
  321. $pty = sprintf("/dev/$kind%c%c", $bank, $unit);
  322. open($_PTY,"+>$pty") || next;
  323. select((select($_PTY), $| = 1)[0]);
  324. ($tty = $pty) =~ s/pty/tty/;
  325. open($_TTY,"+>$tty") || next;
  326. select((select($_TTY), $| = 1)[0]);
  327. system "stty nl>$tty";
  328. return ($pty,$tty);
  329. }
  330. }
  331. undef;
  332. }
  333. 1;