Telnet.pm untested

filter: Statementstmt 0.0% 0 / 1556 Branchbran 0.0% 0 / 844 Conditioncond 0.0% 0 / 280 MC/DCmcdc 0.0% 0 / 167 Subroutinesub 0.0% 0 / 182 Podpod 0.0% 0 / 170 totaltotal 0.0% 0 / 3199 CC 422 SCAR 55.8
CC
422
Cov
0%
new
1260.0
open
1122.0
waitfor
600.0
CRAP 265.1
1 package Net::Telnet;
2
3 ## Copyright 1997, 2000, 2002, 2013 Jay Rogers. All rights reserved.
4 ## This program is free software; you can redistribute it and/or
5 ## modify it under the same terms as Perl itself.
6
7 ## See user documentation at the end of this file. Search for =head
8
9 use strict;
10 require 5.002;
11
12 ## Module export.
13 use vars qw(@EXPORT_OK);
14 @EXPORT_OK = qw(TELNET_IAC TELNET_DONT TELNET_DO TELNET_WONT TELNET_WILL
15 TELNET_SB TELNET_GA TELNET_EL TELNET_EC TELNET_AYT TELNET_AO
16 TELNET_IP TELNET_BREAK TELNET_DM TELNET_NOP TELNET_SE
17 TELNET_EOR TELNET_ABORT TELNET_SUSP TELNET_EOF TELNET_SYNCH
18 TELOPT_BINARY TELOPT_ECHO TELOPT_RCP TELOPT_SGA TELOPT_NAMS
19 TELOPT_STATUS TELOPT_TM TELOPT_RCTE TELOPT_NAOL TELOPT_NAOP
20 TELOPT_NAOCRD TELOPT_NAOHTS TELOPT_NAOHTD TELOPT_NAOFFD
21 TELOPT_NAOVTS TELOPT_NAOVTD TELOPT_NAOLFD TELOPT_XASCII
22 TELOPT_LOGOUT TELOPT_BM TELOPT_DET TELOPT_SUPDUP
23 TELOPT_SUPDUPOUTPUT TELOPT_SNDLOC TELOPT_TTYPE TELOPT_EOR
24 TELOPT_TUID TELOPT_OUTMRK TELOPT_TTYLOC TELOPT_3270REGIME
25 TELOPT_X3PAD TELOPT_NAWS TELOPT_TSPEED TELOPT_LFLOW
26 TELOPT_LINEMODE TELOPT_XDISPLOC TELOPT_OLD_ENVIRON
27 TELOPT_AUTHENTICATION TELOPT_ENCRYPT TELOPT_NEW_ENVIRON
28 TELOPT_TN3270E TELOPT_CHARSET TELOPT_COMPORT TELOPT_KERMIT
29 TELOPT_EXOPL);
30
31 ## Module import.
32 use Exporter ();
33 use Socket qw(AF_INET SOCK_STREAM inet_aton sockaddr_in);
34 use Symbol qw(qualify);
35
36 ## Base classes.
37 use vars qw(@ISA);
38 @ISA = qw(Exporter);
39 if (&_io_socket_include) { # successfully required module IO::Socket
40 push @ISA, "IO::Socket::INET";
41 }
42 else { # perl version < 5.004
43 require FileHandle;
44 push @ISA, "FileHandle";
45 }
46 my $AF_INET6 = &_import_af_inet6();
47 my $AF_UNSPEC = &_import_af_unspec() || 0;
48 my $AI_ADDRCONFIG = &_import_ai_addrconfig() || 0;
49 my $EAI_BADFLAGS = &_import_eai_badflags() || -1;
50 my $EINTR = &_import_eintr();
51
52 ## Global variables.
53 use vars qw($VERSION @Telopts);
54 $VERSION = "3.05";
55 @Telopts = ("BINARY", "ECHO", "RCP", "SUPPRESS GO AHEAD", "NAMS", "STATUS",
56 "TIMING MARK", "RCTE", "NAOL", "NAOP", "NAOCRD", "NAOHTS",
57 "NAOHTD", "NAOFFD", "NAOVTS", "NAOVTD", "NAOLFD", "EXTEND ASCII",
58 "LOGOUT", "BYTE MACRO", "DATA ENTRY TERMINAL", "SUPDUP",
59 "SUPDUP OUTPUT", "SEND LOCATION", "TERMINAL TYPE", "END OF RECORD",
60 "TACACS UID", "OUTPUT MARKING", "TTYLOC", "3270 REGIME", "X.3 PAD",
61 "NAWS", "TSPEED", "LFLOW", "LINEMODE", "XDISPLOC", "OLD-ENVIRON",
62 "AUTHENTICATION", "ENCRYPT", "NEW-ENVIRON", "TN3270E", "XAUTH",
63 "CHARSET", "RSP", "COMPORT", "SUPPRESS LOCAL ECHO", "START TLS",
64 "KERMIT");
65
66
67 ########################### Public Methods ###########################
68
69
70 sub new {
71 my ($class) = @_;
72 my (
73 $dump_log,
74 $errmode,
75 $family,
76 $fh_open,
77 $host,
78 $input_log,
79 $localfamily,
80 $option_log,
81 $output_log,
82 $port,
83 $prompt,
84 $self,
85 %args,
86 );
87 local $_;
88
89 ## Create a new object with defaults.
90 $self = $class->SUPER::new;
91 *$self->{net_telnet} = {
92 bin_mode => 0,
93 blksize => &_optimal_blksize(),
94 buf => "",
95 cmd_prompt => '/[\$%#>] $/',
96 cmd_rm_mode => "auto",
97 dumplog => '',
98 eofile => 1,
99 errormode => "die",
100 errormsg => "",
101 fdmask => '',
102 host => "localhost",
103 inputlog => '',
104 last_line => "",
105 last_prompt => "",
106 local_family => "ipv4",
107 local_host => "",
108 maxbufsize => 1_048_576,
109 num_wrote => 0,
110 ofs => "",
111 opened => '',
112 opt_cback => '',
113 opt_log => '',
114 opts => {},
115 ors => "\n",
116 outputlog => '',
117 peer_family => "ipv4",
118 pending_errormsg => "",
119 port => 23,
120 pushback_buf => "",
121 rs => "\n",
122 select_supported => 1,
123 sock_family => 0,
124 subopt_cback => '',
125 telnet_mode => 1,
126 time_out => 10,
127 timedout => '',
128 unsent_opts => "",
129 };
130
131 ## Indicate that we'll accept an offer from remote side for it to echo
132 ## and suppress go aheads.
133 &_opt_accept($self,
134 { option => &TELOPT_ECHO,
135 is_remote => 1,
136 is_enable => 1 },
137 { option => &TELOPT_SGA,
138 is_remote => 1,
139 is_enable => 1 },
140 );
141
142 ## Parse the args.
143 if (@_ == 2) { # one positional arg given
144 $host = $_[1];
145 }
146 elsif (@_ > 2) { # named args given
147 ## Get the named args.
148 (undef, %args) = @_;
149
150 ## Parse all other named args.
151 foreach (keys %args) {
152 if (/^-?binmode$/i) {
153 $self->binmode($args{$_});
154 }
155 elsif (/^-?cmd_remove_mode$/i) {
156 $self->cmd_remove_mode($args{$_});
157 }
158 elsif (/^-?dump_log$/i) {
159 $dump_log = $args{$_};
160 }
161 elsif (/^-?errmode$/i) {
162 $errmode = $args{$_};
163 }
164 elsif (/^-?family$/i) {
165 $family = $args{$_};
166 }
167 elsif (/^-?fhopen$/i) {
168 $fh_open = $args{$_};
169 }
170 elsif (/^-?host$/i) {
171 $host = $args{$_};
172 }
173 elsif (/^-?input_log$/i) {
174 $input_log = $args{$_};
175 }
176 elsif (/^-?input_record_separator$/i or /^-?rs$/i) {
177 $self->input_record_separator($args{$_});
178 }
179 elsif (/^-?localfamily$/i) {
180 $localfamily = $args{$_};
181 }
182 elsif (/^-?localhost$/i) {
183 $self->localhost($args{$_});
184 }
185 elsif (/^-?max_buffer_length$/i) {
186 $self->max_buffer_length($args{$_});
187 }
188 elsif (/^-?option_log$/i) {
189 $option_log = $args{$_};
190 }
191 elsif (/^-?output_field_separator$/i or /^-?ofs$/i) {
192 $self->output_field_separator($args{$_});
193 }
194 elsif (/^-?output_log$/i) {
195 $output_log = $args{$_};
196 }
197 elsif (/^-?output_record_separator$/i or /^-?ors$/i) {
198 $self->output_record_separator($args{$_});
199 }
200 elsif (/^-?port$/i) {
201 $port = $args{$_};
202 }
203 elsif (/^-?prompt$/i) {
204 $prompt = $args{$_};
205 }
206 elsif (/^-?telnetmode$/i) {
207 $self->telnetmode($args{$_});
208 }
209 elsif (/^-?timeout$/i) {
210 $self->timeout($args{$_});
211 }
212 else {
213 &_croak($self, "bad named parameter \"$_\" given " .
214 "to " . ref($self) . "::new()");
215 }
216 }
217 }
218
219 if (defined $errmode) { # user wants to set errmode
220 $self->errmode($errmode);
221 }
222
223 if (defined $host) { # user wants to set host
224 $self->host($host);
225 }
226
227 if (defined $port) { # user wants to set port
228 $self->port($port)
229 or return;
230 }
231
232 if (defined $family) { # user wants to set family
233 $self->family($family)
234 or return;
235 }
236
237 if (defined $localfamily) { # user wants to set localfamily
238 $self->localfamily($localfamily)
239 or return;
240 }
241
242 if (defined $prompt) { # user wants to set prompt
243 $self->prompt($prompt)
244 or return;
245 }
246
247 if (defined $dump_log) { # user wants to set dump_log
248 $self->dump_log($dump_log)
249 or return;
250 }
251
252 if (defined $input_log) { # user wants to set input_log
253 $self->input_log($input_log)
254 or return;
255 }
256
257 if (defined $option_log) { # user wants to set option_log
258 $self->option_log($option_log)
259 or return;
260 }
261
262 if (defined $output_log) { # user wants to set output_log
263 $self->output_log($output_log)
264 or return;
265 }
266
267 if (defined $fh_open) { # user wants us to attach to existing filehandle
268 $self->fhopen($fh_open)
269 or return;
270 }
271 elsif (defined $host) { # user wants us to open a connection to host
272 $self->open
273 or return;
274 }
275
276 $self;
277 } # end sub new
278
279
280 sub DESTROY {
281 } # end sub DESTROY
282
283
284 sub binmode {
285 my ($self, $mode) = @_;
286 my (
287 $prev,
288 $s,
289 );
290
291 $s = *$self->{net_telnet};
292 $prev = $s->{bin_mode};
293
294 if (@_ >= 2) {
295 unless (defined $mode) {
296 $mode = 0;
297 }
298
299 $s->{bin_mode} = $mode;
300 }
301
302 $prev;
303 } # end sub binmode
304
305
306 sub break {
307 my ($self) = @_;
308 my $s = *$self->{net_telnet};
309 my $break_cmd = "\xff\xf3";
310
311 $s->{timedout} = '';
312
313 &_put($self, \$break_cmd, "break");
314 } # end sub break
315
316
317 sub buffer {
318 my ($self) = @_;
319 my $s = *$self->{net_telnet};
320
321 \$s->{buf};
322 } # end sub buffer
323
324
325 sub buffer_empty {
326 my ($self) = @_;
327 my (
328 $buffer,
329 );
330
331 $buffer = $self->buffer;
332 $$buffer = "";
333 } # end sub buffer_empty
334
335
336 sub close {
337 my ($self) = @_;
338 my $s = *$self->{net_telnet};
339
340 $s->{eofile} = 1;
341 $s->{opened} = '';
342 $s->{sock_family} = 0;
343 close $self
344 if defined fileno($self);
345
346 1;
347 } # end sub close
348
349
350 sub cmd {
351 my ($self, @args) = @_;
352 my (
353 $arg_errmode,
354 $cmd_remove_mode,
355 $firstpos,
356 $last_prompt,
357 $lastpos,
358 $lines,
359 $ors,
360 $output,
361 $output_ref,
362 $prompt,
363 $remove_echo,
364 $rs,
365 $rs_len,
366 $s,
367 $telopt_echo,
368 $timeout,
369 %args,
370 );
371 my $cmd = "";
372 local $_;
373
374 ## Init.
375 $self->timed_out('');
376 $self->last_prompt("");
377 $s = *$self->{net_telnet};
378 $output = [];
379 $cmd_remove_mode = $self->cmd_remove_mode;
380 $ors = $self->output_record_separator;
381 $prompt = $self->prompt;
382 $rs = $self->input_record_separator;
383 $timeout = $self->timeout;
384
385 ## Override errmode first, if specified.
386 $arg_errmode = &_extract_arg_errmode($self, \@args);
387 local $s->{errormode} = $arg_errmode
388 if $arg_errmode;
389
390 ## Parse args.
391 if (@args == 1) { # one positional arg given
392 $cmd = $args[0];
393 }
394 elsif (@args >= 2) { # named args given
395 ## Get the named args.
396 %args = @args;
397
398 ## Parse the named args.
399 foreach (keys %args) {
400 if (/^-?cmd_remove/i) {
401 $cmd_remove_mode = &_parse_cmd_remove_mode($self, $args{$_});
402 }
403 elsif (/^-?input_record_separator$/i or /^-?rs$/i) {
404 $rs = &_parse_input_record_separator($self, $args{$_});
405 }
406 elsif (/^-?output$/i) {
407 $output_ref = $args{$_};
408 if (defined($output_ref) and ref($output_ref) eq "ARRAY") {
409 $output = $output_ref;
410 }
411 }
412 elsif (/^-?output_record_separator$/i or /^-?ors$/i) {
413 $ors = $args{$_};
414 }
415 elsif (/^-?prompt$/i) {
416 $prompt = &_parse_prompt($self, $args{$_})
417 or return;
418 }
419 elsif (/^-?string$/i) {
420 $cmd = $args{$_};
421 }
422 elsif (/^-?timeout$/i) {
423 $timeout = &_parse_timeout($self, $args{$_});
424 }
425 else {
426 &_croak($self, "bad named parameter \"$_\" given " .
427 "to " . ref($self) . "::cmd()");
428 }
429 }
430 }
431
432 ## Override some user settings.
433 local $s->{time_out} = &_endtime($timeout);
434 $self->errmsg("");
435
436 ## Send command and wait for the prompt.
437 {
438 local $s->{errormode} = "return";
439
440 $self->put($cmd . $ors)
441 and ($lines, $last_prompt) = $self->waitfor($prompt);
442 }
443
444 ## Check for failure.
445 return $self->error("command timed-out") if $self->timed_out;
446 return $self->error($self->errmsg) if $self->errmsg ne "";
447
448 ## Save the most recently matched prompt.
449 $self->last_prompt($last_prompt);
450
451 ## Split lines into an array, keeping record separator at end of line.
452 $firstpos = 0;
453 $rs_len = length $rs;
454 while (($lastpos = index($lines, $rs, $firstpos)) > -1) {
455 push(@$output,
456 substr($lines, $firstpos, $lastpos - $firstpos + $rs_len));
457 $firstpos = $lastpos + $rs_len;
458 }
459
460 if ($firstpos < length $lines) {
461 push @$output, substr($lines, $firstpos);
462 }
463
464 ## Determine if we should remove the first line of output based
465 ## on the assumption that it's an echoed back command.
466 if ($cmd_remove_mode eq "auto") {
467 ## See if remote side told us they'd echo.
468 $telopt_echo = $self->option_state(&TELOPT_ECHO);
469 $remove_echo = $telopt_echo->{remote_enabled};
470 }
471 else { # user explicitly told us how many lines to remove.
472 $remove_echo = $cmd_remove_mode;
473 }
474
475 ## Get rid of possible echo back command.
476 while ($remove_echo--) {
477 shift @$output;
478 }
479
480 ## Ensure at least a null string when there's no command output - so
481 ## "true" is returned in a list context.
482 unless (@$output) {
483 @$output = ("");
484 }
485
486 ## Return command output via named arg, if requested.
487 if (defined $output_ref) {
488 if (ref($output_ref) eq "SCALAR") {
489 $$output_ref = join "", @$output;
490 }
491 elsif (ref($output_ref) eq "HASH") {
492 %$output_ref = @$output;
493 }
494 }
495
496 wantarray ? @$output : 1;
497 } # end sub cmd
498
499
500 sub cmd_remove_mode {
501 my ($self, $mode) = @_;
502 my (
503 $prev,
504 $s,
505 );
506
507 $s = *$self->{net_telnet};
508 $prev = $s->{cmd_rm_mode};
509
510 if (@_ >= 2) {
511 $s->{cmd_rm_mode} = &_parse_cmd_remove_mode($self, $mode);
512 }
513
514 $prev;
515 } # end sub cmd_remove_mode
516
517
518 sub dump_log {
519 my ($self, $name) = @_;
520 my (
521 $fh,
522 $s,
523 );
524
525 $s = *$self->{net_telnet};
526 $fh = $s->{dumplog};
527
528 if (@_ >= 2) {
529 if (!defined($name) or $name eq "") { # input arg is ""
530 ## Turn off logging.
531 $fh = "";
532 }
533 elsif (&_is_open_fh($name)) { # input arg is an open fh
534 ## Use the open fh for logging.
535 $fh = $name;
536 select((select($fh), $|=1)[$[]); # don't buffer writes
537 }
538 elsif (!ref $name) { # input arg is filename
539 ## Open the file for logging.
540 $fh = &_fname_to_handle($self, $name)
541 or return;
542 select((select($fh), $|=1)[$[]); # don't buffer writes
543 }
544 else {
545 return $self->error("bad Dump_log argument ",
546 "\"$name\": not filename or open fh");
547 }
548
549 $s->{dumplog} = $fh;
550 }
551
552 $fh;
553 } # end sub dump_log
554
555
556 sub eof {
557 my ($self) = @_;
558
559 *$self->{net_telnet}{eofile};
560 } # end sub eof
561
562
563 sub errmode {
564 my ($self, $mode) = @_;
565 my (
566 $prev,
567 $s,
568 );
569
570 $s = *$self->{net_telnet};
571 $prev = $s->{errormode};
572
573 if (@_ >= 2) {
574 $s->{errormode} = &_parse_errmode($self, $mode);
575 }
576
577 $prev;
578 } # end sub errmode
579
580
581 sub errmsg {
582 my ($self, @errmsgs) = @_;
583 my (
584 $prev,
585 $s,
586 );
587
588 $s = *$self->{net_telnet};
589 $prev = $s->{errormsg};
590
591 if (@_ >= 2) {
592 $s->{errormsg} = join "", @errmsgs;
593 }
594
595 $prev;
596 } # end sub errmsg
597
598
599 sub error {
600 my ($self, @errmsg) = @_;
601 my (
602 $errmsg,
603 $func,
604 $mode,
605 $s,
606 @args,
607 );
608 local $_;
609
610 $s = *$self->{net_telnet};
611
612 if (@_ >= 2) {
613 ## Put error message in the object.
614 $errmsg = join "", @errmsg;
615 $s->{errormsg} = $errmsg;
616
617 ## Do the error action as described by error mode.
618 $mode = $s->{errormode};
619 if (ref($mode) eq "CODE") {
620 &$mode($errmsg);
621 return;
622 }
623 elsif (ref($mode) eq "ARRAY") {
624 ($func, @args) = @$mode;
625 &$func(@args);
626 return;
627 }
628 elsif ($mode =~ /^return$/i) {
629 return;
630 }
631 else { # die
632 if ($errmsg =~ /\n$/) {
633 die $errmsg;
634 }
635 else {
636 ## Die and append caller's line number to message.
637 &_croak($self, $errmsg);
638 }
639 }
640 }
641 else {
642 return $s->{errormsg} ne "";
643 }
644 } # end sub error
645
646
647 sub family {
648 my ($self, $family) = @_;
649 my (
650 $prev,
651 $s,
652 );
653
654 $s = *$self->{net_telnet};
655 $prev = $s->{peer_family};
656
657 if (@_ >= 2) {
658 $family = &_parse_family($self, $family)
659 or return;
660
661 $s->{peer_family} = $family;
662 }
663
664 $prev;
665 } # end sub family
666
667
668 sub fhopen {
669 my ($self, $fh) = @_;
670 my (
671 $globref,
672 $s,
673 );
674
675 ## Convert given filehandle to a typeglob reference, if necessary.
676 $globref = &_qualify_fh($self, $fh);
677
678 ## Ensure filehandle is already open.
679 return $self->error("fhopen filehandle isn't already open")
680 unless defined($globref) and defined(fileno $globref);
681
682 ## Ensure we're closed.
683 $self->close;
684
685 ## Save our private data.
686 $s = *$self->{net_telnet};
687
688 ## Switch ourself with the given filehandle.
689 *$self = *$globref;
690
691 ## Restore our private data.
692 *$self->{net_telnet} = $s;
693
694 ## Re-initialize ourself.
695 select((select($self), $|=1)[$[]); # don't buffer writes
696 $s = *$self->{net_telnet};
697 $s->{blksize} = &_optimal_blksize((stat $self)[11]);
698 $s->{buf} = "";
699 $s->{eofile} = '';
700 $s->{errormsg} = "";
701 vec($s->{fdmask}='', fileno($self), 1) = 1;
702 $s->{host} = "";
703 $s->{last_line} = "";
704 $s->{last_prompt} = "";
705 $s->{num_wrote} = 0;
706 $s->{opened} = 1;
707 $s->{pending_errormsg} = "";
708 $s->{port} = '';
709 $s->{pushback_buf} = "";
710 $s->{select_supported} = $^O ne "MSWin32" || -S $self;
711 $s->{timedout} = '';
712 $s->{unsent_opts} = "";
713 &_reset_options($s->{opts});
714
715 1;
716 } # end sub fhopen
717
718
719 sub get {
720 my ($self, %args) = @_;
721 my (
722 $binmode,
723 $endtime,
724 $errmode,
725 $line,
726 $s,
727 $telnetmode,
728 $timeout,
729 );
730 local $_;
731
732 ## Init.
733 $s = *$self->{net_telnet};
734 $timeout = $s->{time_out};
735 $s->{timedout} = '';
736 return if $s->{eofile};
737
738 ## Parse the named args.
739 foreach (keys %args) {
740 if (/^-?binmode$/i) {
741 $binmode = $args{$_};
742 unless (defined $binmode) {
743 $binmode = 0;
744 }
745 }
746 elsif (/^-?errmode$/i) {
747 $errmode = &_parse_errmode($self, $args{$_});
748 }
749 elsif (/^-?telnetmode$/i) {
750 $telnetmode = $args{$_};
751 unless (defined $telnetmode) {
752 $telnetmode = 0;
753 }
754 }
755 elsif (/^-?timeout$/i) {
756 $timeout = &_parse_timeout($self, $args{$_});
757 }
758 else {
759 &_croak($self, "bad named parameter \"$_\" given " .
760 "to " . ref($self) . "::get()");
761 }
762 }
763
764 ## If any args given, override corresponding instance data.
765 local $s->{errormode} = $errmode
766 if defined $errmode;
767 local $s->{bin_mode} = $binmode
768 if defined $binmode;
769 local $s->{telnet_mode} = $telnetmode
770 if defined $telnetmode;
771
772 ## Set wall time when we time out.
773 $endtime = &_endtime($timeout);
774
775 ## Try to send any waiting option negotiation.
776 if (length $s->{unsent_opts}) {
777 &_flush_opts($self);
778 }
779
780 ## Try to read just the waiting data using return error mode.
781 {
782 local $s->{errormode} = "return";
783 $s->{errormsg} = "";
784 &_fillbuf($self, $s, 0);
785 }
786
787 ## We're done if we timed-out and timeout value is set to "poll".
788 return $self->error($s->{errormsg})
789 if ($s->{timedout} and defined($timeout) and $timeout == 0
790 and !length $s->{buf});
791
792 ## We're done if we hit an error other than timing out.
793 if ($s->{errormsg} and !$s->{timedout}) {
794 if (!length $s->{buf}) {
795 return $self->error($s->{errormsg});
796 }
797 else { # error encountered but there's some data in buffer
798 $s->{pending_errormsg} = $s->{errormsg};
799 }
800 }
801
802 ## Clear time-out error from first read.
803 $s->{timedout} = '';
804 $s->{errormsg} = "";
805
806 ## If buffer is still empty, try to read according to user's timeout.
807 if (!length $s->{buf}) {
808 &_fillbuf($self, $s, $endtime)
809 or do {
810 return if $s->{timedout};
811
812 ## We've reached end-of-file.
813 $self->close;
814 return;
815 };
816 }
817
818 ## Extract chars from buffer.
819 $line = $s->{buf};
820 $s->{buf} = "";
821
822 $line;
823 } # end sub get
824
825
826 sub getline {
827 my ($self, %args) = @_;
828 my (
829 $binmode,
830 $endtime,
831 $errmode,
832 $len,
833 $line,
834 $offset,
835 $pos,
836 $rs,
837 $s,
838 $telnetmode,
839 $timeout,
840 );
841 local $_;
842
843 ## Init.
844 $s = *$self->{net_telnet};
845 $s->{timedout} = '';
846 return if $s->{eofile};
847 $rs = $s->{"rs"};
848 $timeout = $s->{time_out};
849
850 ## Parse the named args.
851 foreach (keys %args) {
852 if (/^-?binmode$/i) {
853 $binmode = $args{$_};
854 unless (defined $binmode) {
855 $binmode = 0;
856 }
857 }
858 elsif (/^-?errmode$/i) {
859 $errmode = &_parse_errmode($self, $args{$_});
860 }
861 elsif (/^-?input_record_separator$/i or /^-?rs$/i) {
862 $rs = &_parse_input_record_separator($self, $args{$_});
863 }
864 elsif (/^-?telnetmode$/i) {
865 $telnetmode = $args{$_};
866 unless (defined $telnetmode) {
867 $telnetmode = 0;
868 }
869 }
870 elsif (/^-?timeout$/i) {
871 $timeout = &_parse_timeout($self, $args{$_});
872 }
873 else {
874 &_croak($self, "bad named parameter \"$_\" given " .
875 "to " . ref($self) . "::getline()");
876 }
877 }
878
879 ## If any args given, override corresponding instance data.
880 local $s->{bin_mode} = $binmode
881 if defined $binmode;
882 local $s->{errormode} = $errmode
883 if defined $errmode;
884 local $s->{telnet_mode} = $telnetmode
885 if defined $telnetmode;
886
887 ## Set wall time when we time out.
888 $endtime = &_endtime($timeout);
889
890 ## Try to send any waiting option negotiation.
891 if (length $s->{unsent_opts}) {
892 &_flush_opts($self);
893 }
894
895 ## Keep reading into buffer until end-of-line is read.
896 $offset = 0;
897 while (($pos = index($s->{buf}, $rs, $offset)) == -1) {
898 $offset = length $s->{buf};
899 &_fillbuf($self, $s, $endtime)
900 or do {
901 return if $s->{timedout};
902
903 ## We've reached end-of-file.
904 $self->close;
905 if (length $s->{buf}) {
906 return $s->{buf};
907 }
908 else {
909 return;
910 }
911 };
912 }
913
914 ## Extract line from buffer.
915 $len = $pos + length $rs;
916 $line = substr($s->{buf}, 0, $len);
917 substr($s->{buf}, 0, $len) = "";
918
919 $line;
920 } # end sub getline
921
922
923 sub getlines {
924 my ($self, %args) = @_;
925 my (
926 $binmode,
927 $errmode,
928 $line,
929 $rs,
930 $s,
931 $telnetmode,
932 $timeout,
933 );
934 my $all = 1;
935 my @lines = ();
936 local $_;
937
938 ## Init.
939 $s = *$self->{net_telnet};
940 $s->{timedout} = '';
941 return if $s->{eofile};
942 $timeout = $s->{time_out};
943
944 ## Parse the named args.
945 foreach (keys %args) {
946 if (/^-?all$/i) {
947 $all = $args{$_};
948 unless (defined $all) {
949 $all = '';
950 }
951 }
952 elsif (/^-?binmode$/i) {
953 $binmode = $args{$_};
954 unless (defined $binmode) {
955 $binmode = 0;
956 }
957 }
958 elsif (/^-?errmode$/i) {
959 $errmode = &_parse_errmode($self, $args{$_});
960 }
961 elsif (/^-?input_record_separator$/i or /^-?rs$/i) {
962 $rs = &_parse_input_record_separator($self, $args{$_});
963 }
964 elsif (/^-?telnetmode$/i) {
965 $telnetmode = $args{$_};
966 unless (defined $telnetmode) {
967 $telnetmode = 0;
968 }
969 }
970 elsif (/^-?timeout$/i) {
971 $timeout = &_parse_timeout($self, $args{$_});
972 }
973 else {
974 &_croak($self, "bad named parameter \"$_\" given " .
975 "to " . ref($self) . "::getlines()");
976 }
977 }
978
979 ## If any args given, override corresponding instance data.
980 local $s->{bin_mode} = $binmode
981 if defined $binmode;
982 local $s->{errormode} = $errmode
983 if defined $errmode;
984 local $s->{"rs"} = $rs
985 if defined $rs;
986 local $s->{telnet_mode} = $telnetmode
987 if defined $telnetmode;
988 local $s->{time_out} = &_endtime($timeout);
989
990 ## User requested only the currently available lines.
991 if (! $all) {
992 return &_next_getlines($self, $s);
993 }
994
995 ## Read lines until eof or error.
996 while (1) {
997 $line = $self->getline
998 or last;
999 push @lines, $line;
1000 }
1001
1002 ## Check for error.
1003 return if ! $self->eof;
1004
1005 @lines;
1006 } # end sub getlines
1007
1008
1009 sub host {
1010 my ($self, $host) = @_;
1011 my (
1012 $prev,
1013 $s,
1014 );
1015
1016 $s = *$self->{net_telnet};
1017 $prev = $s->{host};
1018
1019 if (@_ >= 2) {
1020 unless (defined $host) {
1021 $host = "";
1022 }
1023
1024 $s->{host} = $host;
1025 }
1026
1027 $prev;
1028 } # end sub host
1029
1030
1031 sub input_log {
1032 my ($self, $name) = @_;
1033 my (
1034 $fh,
1035 $s,
1036 );
1037
1038 $s = *$self->{net_telnet};
1039 $fh = $s->{inputlog};
1040
1041 if (@_ >= 2) {
1042 if (!defined($name) or $name eq "") { # input arg is ""
1043 ## Turn off logging.
1044 $fh = "";
1045 }
1046 elsif (&_is_open_fh($name)) { # input arg is an open fh
1047 ## Use the open fh for logging.
1048 $fh = $name;
1049 select((select($fh), $|=1)[$[]); # don't buffer writes
1050 }
1051 elsif (!ref $name) { # input arg is filename
1052 ## Open the file for logging.
1053 $fh = &_fname_to_handle($self, $name)
1054 or return;
1055 select((select($fh), $|=1)[$[]); # don't buffer writes
1056 }
1057 else {
1058 return $self->error("bad Input_log argument ",
1059 "\"$name\": not filename or open fh");
1060 }
1061
1062 $s->{inputlog} = $fh;
1063 }
1064
1065 $fh;
1066 } # end sub input_log
1067
1068
1069 sub input_record_separator {
1070 my ($self, $rs) = @_;
1071 my (
1072 $prev,
1073 $s,
1074 );
1075
1076 $s = *$self->{net_telnet};
1077 $prev = $s->{"rs"};
1078
1079 if (@_ >= 2) {
1080 $s->{"rs"} = &_parse_input_record_separator($self, $rs);
1081 }
1082
1083 $prev;
1084 } # end sub input_record_separator
1085
1086
1087 sub last_prompt {
1088 my ($self, $string) = @_;
1089 my (
1090 $prev,
1091 $s,
1092 );
1093
1094 $s = *$self->{net_telnet};
1095 $prev = $s->{last_prompt};
1096
1097 if (@_ >= 2) {
1098 unless (defined $string) {
1099 $string = "";
1100 }
1101
1102 $s->{last_prompt} = $string;
1103 }
1104
1105 $prev;
1106 } # end sub last_prompt
1107
1108
1109 sub lastline {
1110 my ($self, $line) = @_;
1111 my (
1112 $prev,
1113 $s,
1114 );
1115
1116 $s = *$self->{net_telnet};
1117 $prev = $s->{last_line};
1118
1119 if (@_ >= 2) {
1120 unless (defined $line) {
1121 $line = "";
1122 }
1123
1124 $s->{last_line} = $line;
1125 }
1126
1127 $prev;
1128 } # end sub lastline
1129
1130
1131 sub localfamily {
1132 my ($self, $family) = @_;
1133 my (
1134 $prev,
1135 $s,
1136 );
1137
1138 $s = *$self->{net_telnet};
1139 $prev = $s->{local_family};
1140
1141 if (@_ >= 2) {
1142 unless (defined $family) {
1143 $family = "";
1144 }
1145
1146 if ($family =~ /^\s*ipv4\s*$/i) { # family arg is "ipv4"
1147 $s->{local_family} = "ipv4";
1148 }
1149 elsif ($family =~ /^\s*any\s*$/i) { # family arg is "any"
1150 if ($Socket::VERSION >= 1.94 and defined $AF_INET6) { # has IPv6
1151 $s->{local_family} = "any";
1152 }
1153 else { # IPv6 not supported on this machine
1154 $s->{local_family} = "ipv4";
1155 }
1156 }
1157 elsif ($family =~ /^\s*ipv6\s*$/i) { # family arg is "ipv6"
1158 return $self->error("Localfamily arg ipv6 not supported when " .
1159 "Socket.pm version < 1.94")
1160 unless $Socket::VERSION >= 1.94;
1161 return $self->error("Localfamily arg ipv6 not supported by " .
1162 "this OS: AF_INET6 not in Socket.pm")
1163 unless defined $AF_INET6;
1164
1165 $s->{local_family} = "ipv6";
1166 }
1167 else {
1168 return $self->error("bad Localfamily argument \"$family\": " .
1169 "must be \"ipv4\", \"ipv6\", or \"any\"");
1170 }
1171 }
1172
1173 $prev;
1174 } # end sub localfamily
1175
1176
1177 sub localhost {
1178 my ($self, $localhost) = @_;
1179 my (
1180 $prev,
1181 $s,
1182 );
1183
1184 $s = *$self->{net_telnet};
1185 $prev = $s->{local_host};
1186
1187 if (@_ >= 2) {
1188 unless (defined $localhost) {
1189 $localhost = "";
1190 }
1191
1192 $s->{local_host} = $localhost;
1193 }
1194
1195 $prev;
1196 } # end sub localhost
1197
1198
1199 sub login {
1200 my ($self, @args) = @_;
1201 my (
1202 $arg_errmode,
1203 $error,
1204 $is_passwd_arg,
1205 $is_username_arg,
1206 $lastline,
1207 $match,
1208 $ors,
1209 $passwd,
1210 $prematch,
1211 $prompt,
1212 $s,
1213 $timeout,
1214 $username,
1215 %args,
1216 );
1217 local $_;
1218
1219 ## Init.
1220 $self->timed_out('');
1221 $self->last_prompt("");
1222 $s = *$self->{net_telnet};
1223 $timeout = $self->timeout;
1224 $ors = $self->output_record_separator;
1225 $prompt = $self->prompt;
1226
1227 ## Parse positional args.
1228 if (@args == 2) { # just username and passwd given
1229 $username = $args[0];
1230 $passwd = $args[1];
1231
1232 $is_username_arg = 1;
1233 $is_passwd_arg = 1;
1234 }
1235
1236 ## Override errmode first, if specified.
1237 $arg_errmode = &_extract_arg_errmode($self, \@args);
1238 local $s->{errormode} = $arg_errmode
1239 if $arg_errmode;
1240
1241 ## Parse named args.
1242 if (@args > 2) {
1243 ## Get the named args.
1244 %args = @args;
1245
1246 ## Parse the named args.
1247 foreach (keys %args) {
1248 if (/^-?name$/i) {
1249 $username = $args{$_};
1250 unless (defined $username) {
1251 $username = "";
1252 }
1253
1254 $is_username_arg = 1;
1255 }
1256 elsif (/^-?pass/i) {
1257 $passwd = $args{$_};
1258 unless (defined $passwd) {
1259 $passwd = "";
1260 }
1261
1262 $is_passwd_arg = 1;
1263 }
1264 elsif (/^-?prompt$/i) {
1265 $prompt = &_parse_prompt($self, $args{$_})
1266 or return;
1267 }
1268 elsif (/^-?timeout$/i) {
1269 $timeout = &_parse_timeout($self, $args{$_});
1270 }
1271 else {
1272 &_croak($self, "bad named parameter \"$_\" given ",
1273 "to " . ref($self) . "::login()");
1274 }
1275 }
1276 }
1277
1278 ## Ensure both username and password argument given.
1279 &_croak($self,"Name argument not given to " . ref($self) . "::login()")
1280 unless $is_username_arg;
1281 &_croak($self,"Password argument not given to " . ref($self) . "::login()")
1282 unless $is_passwd_arg;
1283
1284 ## Set timeout for this invocation.
1285 local $s->{time_out} = &_endtime($timeout);
1286
1287 ## Create a subroutine to generate an error.
1288 $error
1289 = sub {
1290 my ($errmsg) = @_;
1291
1292 if ($self->timed_out) {
1293 return $self->error($errmsg);
1294 }
1295 elsif ($self->eof) {
1296 ($lastline = $self->lastline) =~ s/\n+//;
1297 return $self->error($errmsg, ": ", $lastline);
1298 }
1299 else {
1300 return $self->error($self->errmsg);
1301 }
1302 };
1303
1304
1305 return $self->error("login failed: filehandle isn't open")
1306 if $self->eof;
1307
1308 ## Wait for login prompt.
1309 $self->waitfor(Match => '/login[: ]*$/i',
1310 Match => '/username[: ]*$/i',
1311 Errmode => "return")
1312 or do {
1313 return &$error("eof read waiting for login prompt")
1314 if $self->eof;
1315 return &$error("timed-out waiting for login prompt");
1316 };
1317
1318 ## Delay sending response because of bug in Linux login program.
1319 &_sleep(0.01);
1320
1321 ## Send login name.
1322 $self->put(String => $username . $ors,
1323 Errmode => "return")
1324 or return &$error("login disconnected");
1325
1326 ## Wait for password prompt.
1327 $self->waitfor(Match => '/password[: ]*$/i',
1328 Errmode => "return")
1329 or do {
1330 return &$error("eof read waiting for password prompt")
1331 if $self->eof;
1332 return &$error("timed-out waiting for password prompt");
1333 };
1334
1335 ## Delay sending response because of bug in Linux login program.
1336 &_sleep(0.01);
1337
1338 ## Send password.
1339 $self->put(String => $passwd . $ors,
1340 Errmode => "return")
1341 or return &$error("login disconnected");
1342
1343 ## Wait for command prompt or another login prompt.
1344 ($prematch, $match) = $self->waitfor(Match => '/login[: ]*$/i',
1345 Match => '/username[: ]*$/i',
1346 Match => $prompt,
1347 Errmode => "return")
1348 or do {
1349 return &$error("eof read waiting for command prompt")
1350 if $self->eof;
1351 return &$error("timed-out waiting for command prompt");
1352 };
1353
1354 ## It's a bad login if we got another login prompt.
1355 return $self->error("login failed: bad name or password")
1356 if $match =~ /login[: ]*$/i or $match =~ /username[: ]*$/i;
1357
1358 ## Save the most recently matched command prompt.
1359 $self->last_prompt($match);
1360
1361 1;
1362 } # end sub login
1363
1364
1365 sub max_buffer_length {
1366 my ($self, $maxbufsize) = @_;
1367 my (
1368 $prev,
1369 $s,
1370 );
1371 my $minbufsize = 512;
1372
1373 $s = *$self->{net_telnet};
1374 $prev = $s->{maxbufsize};
1375
1376 if (@_ >= 2) {
1377 ## Ensure a positive integer value.
1378 unless (defined $maxbufsize
1379 and $maxbufsize =~ /^\d+$/
1380 and $maxbufsize)
1381 {
1382 &_carp($self, "ignoring bad Max_buffer_length " .
1383 "argument \"$maxbufsize\": it's not a positive integer");
1384 $maxbufsize = $prev;
1385 }
1386
1387 ## Adjust up values that are too small.
1388 if ($maxbufsize < $minbufsize) {
1389 $maxbufsize = $minbufsize;
1390 }
1391
1392 $s->{maxbufsize} = $maxbufsize;
1393 }
1394
1395 $prev;
1396 } # end sub max_buffer_length
1397
1398
1399 ## Make ofs() synonymous with output_field_separator().
1400 sub ofs { &output_field_separator; }
1401
1402
1403 sub open {
1404 my ($self, @args) = @_;
1405 my (
1406 $af,
1407 $arg_errmode,
1408 $err,
1409 $errno,
1410 $family,
1411 $flags_hint,
1412 $host,
1413 $ip_addr,
1414 $lfamily,
1415 $localhost,
1416 $port,
1417 $s,
1418 $timeout,
1419 %args,
1420 @ai,
1421 );
1422 local $@;
1423 local $_;
1424 my $local_addr = '';
1425 my $remote_addr = '';
1426 my %af = (
1427 ipv4 => AF_INET,
1428 ipv6 => defined($AF_INET6) ? $AF_INET6 : undef,
1429 any => $AF_UNSPEC,
1430 );
1431
1432 ## Init.
1433 $s = *$self->{net_telnet};
1434 $s->{timedout} = '';
1435 $s->{sock_family} = 0;
1436 $port = $self->port;
1437 $family = $self->family;
1438 $localhost = $self->localhost;
1439 $lfamily = $self->localfamily;
1440 $timeout = $self->timeout;
1441
1442 ## Override errmode first, if specified.
1443 $arg_errmode = &_extract_arg_errmode($self, \@args);
1444 local $s->{errormode} = $arg_errmode
1445 if $arg_errmode;
1446
1447 if (@args == 1) { # one positional arg given
1448 $self->host($args[0]);
1449 }
1450 elsif (@args >= 2) { # named args given
1451 ## Get the named args.
1452 %args = @args;
1453
1454 ## Parse the named args.
1455 foreach (keys %args) {
1456 if (/^-?family$/i) {
1457 $family = &_parse_family($self, $args{$_});
1458 }
1459 elsif (/^-?host$/i) {
1460 $self->host($args{$_});
1461 }
1462 elsif (/^-?localfamily$/i) {
1463 $lfamily = &_parse_localfamily($self, $args{$_});
1464 }
1465 elsif (/^-?localhost$/i) {
1466 $args{$_} = "" unless defined $args{$_};
1467 $localhost = $args{$_};
1468 }
1469 elsif (/^-?port$/i) {
1470 $port = &_parse_port($self, $args{$_});
1471 }
1472 elsif (/^-?timeout$/i) {
1473 $timeout = &_parse_timeout($self, $args{$_});
1474 }
1475 else {
1476 &_croak($self, "bad named parameter \"$_\" given ",
1477 "to " . ref($self) . "::open()");
1478 }
1479 }
1480 }
1481
1482 ## Get hostname/ip address.
1483 $host = $self->host;
1484
1485 ## Ensure we're already closed.
1486 $self->close;
1487
1488 ## Connect with or without a timeout.
1489 if (defined($timeout) and &_have_alarm) { # use a timeout
1490 ## Convert possible absolute timeout to relative timeout.
1491 if ($timeout >= $^T) { # it's an absolute time
1492 $timeout = $timeout - time;
1493 }
1494
1495 ## Ensure a valid timeout value for alarm.
1496 if ($timeout < 1) {
1497 $timeout = 1;
1498 }
1499 $timeout = int($timeout + 0.5);
1500
1501 ## Connect to server, timing out if it takes too long.
1502 eval {
1503 ## Turn on timer.
1504 local $SIG{"__DIE__"} = "DEFAULT";
1505 local $SIG{ALRM} = sub { die "timed-out\n" };
1506 alarm $timeout;
1507
1508 if ($family eq "ipv4") {
1509 ## Lookup server's IP address.
1510 $ip_addr = inet_aton $host
1511 or die "unknown remote host: $host\n";
1512 $af = AF_INET;
1513 $remote_addr = sockaddr_in($port, $ip_addr);
1514 }
1515 else { # family is "ipv6" or "any"
1516 ## Lookup server's IP address.
1517 $flags_hint = $family eq "any" ? $AI_ADDRCONFIG : 0;
1518 ($err, @ai) = Socket::getaddrinfo($host, $port,
1519 { socktype => SOCK_STREAM,
1520 "family" => $af{$family},
1521 "flags" => $flags_hint });
1522 if ($err == $EAI_BADFLAGS) {
1523 ## Try again with no flags.
1524 ($err, @ai) = Socket::getaddrinfo($host, $port,
1525 {socktype => SOCK_STREAM,
1526 "family"=> $af{$family},
1527 "flags" => 0 });
1528 }
1529 die "unknown remote host: $host: $err\n"
1530 if $err or !@ai;
1531 $af = $ai[0]{"family"};
1532 $remote_addr = $ai[0]{addr};
1533 }
1534
1535 ## Create a socket and attach the filehandle to it.
1536 socket $self, $af, SOCK_STREAM, 0
1537 or die "problem creating socket: $!\n";
1538
1539 ## Bind to a local network interface.
1540 if (length $localhost) {
1541 if ($lfamily eq "ipv4") {
1542 ## Lookup server's IP address.
1543 $ip_addr = inet_aton $localhost
1544 or die "unknown local host: $localhost\n";
1545 $local_addr = sockaddr_in(0, $ip_addr);
1546 }
1547 else { # local family is "ipv6" or "any"
1548 ## Lookup local IP address.
1549 ($err, @ai) = Socket::getaddrinfo($localhost, 0,
1550 {socktype => SOCK_STREAM,
1551 "family"=>$af{$lfamily},
1552 "flags" => 0 });
1553 die "unknown local host: $localhost: $err\n"
1554 if $err or !@ai;
1555 $local_addr = $ai[0]{addr};
1556 }
1557
1558 bind $self, $local_addr
1559 or die "problem binding to \"$localhost\": $!\n";
1560 }
1561
1562 ## Open connection to server.
1563 connect $self, $remote_addr
1564 or die "problem connecting to \"$host\", port $port: $!\n";
1565 };
1566 alarm 0;
1567
1568 ## Check for error.
1569 if ($@ =~ /^timed-out$/) { # time out failure
1570 $s->{timedout} = 1;
1571 $self->close;
1572 if (!$remote_addr) {
1573 return $self->error("unknown remote host: $host: ",
1574 "name lookup timed-out");
1575 }
1576 elsif (length($localhost) and !$local_addr) {
1577 return $self->error("unknown local host: $localhost: ",
1578 "name lookup timed-out");
1579 }
1580 else {
1581 return $self->error("problem connecting to \"$host\", ",
1582 "port $port: connect timed-out");
1583 }
1584 }
1585 elsif ($@) { # hostname lookup or connect failure
1586 $self->close;
1587 chomp $@;
1588 return $self->error($@);
1589 }
1590 }
1591 else { # don't use a timeout
1592 $timeout = undef;
1593
1594 if ($family eq "ipv4") {
1595 ## Lookup server's IP address.
1596 $ip_addr = inet_aton $host
1597 or return $self->error("unknown remote host: $host");
1598 $af = AF_INET;
1599 $remote_addr = sockaddr_in($port, $ip_addr);
1600 }
1601 else { # family is "ipv6" or "any"
1602 ## Lookup server's IP address.
1603 $flags_hint = $family eq "any" ? $AI_ADDRCONFIG : 0;
1604 ($err, @ai) = Socket::getaddrinfo($host, $port,
1605 { socktype => SOCK_STREAM,
1606 "family" => $af{$family},
1607 "flags" => $flags_hint });
1608 if ($err == $EAI_BADFLAGS) {
1609 ## Try again with no flags.
1610 ($err, @ai) = Socket::getaddrinfo($host, $port,
1611 { socktype => SOCK_STREAM,
1612 "family"=> $af{$family},
1613 "flags" => 0 });
1614 }
1615 return $self->error("unknown remote host: $host")
1616 if $err or !@ai;
1617 $af = $ai[0]{"family"};
1618 $remote_addr = $ai[0]{addr};
1619 }
1620
1621 ## Create a socket and attach the filehandle to it.
1622 socket $self, $af, SOCK_STREAM, 0
1623 or return $self->error("problem creating socket: $!");
1624
1625 ## Bind to a local network interface.
1626 if (length $localhost) {
1627 if ($lfamily eq "ipv4") {
1628 ## Lookup server's IP address.
1629 $ip_addr = inet_aton $localhost
1630 or return $self->error("unknown local host: $localhost");
1631 $local_addr = sockaddr_in(0, $ip_addr);
1632 }
1633 else { # local family is "ipv6" or "any"
1634 ## Lookup local IP address.
1635 ($err, @ai) = Socket::getaddrinfo($localhost, 0,
1636 { socktype => SOCK_STREAM,
1637 "family"=>$af{$lfamily},
1638 "flags" => 0 });
1639 return $self->error("unknown local host: $localhost: $err")
1640 if $err or !@ai;
1641 $local_addr = $ai[0]{addr};
1642 }
1643
1644 bind $self, $local_addr
1645 or return $self->error("problem binding ",
1646 "to \"$localhost\": $!");
1647 }
1648
1649 ## Open connection to server.
1650 connect $self, $remote_addr
1651 or do {
1652 $errno = "$!";
1653 $self->close;
1654 return $self->error("problem connecting to \"$host\", ",
1655 "port $port: $errno");
1656 };
1657 }
1658
1659 select((select($self), $|=1)[$[]); # don't buffer writes
1660 $s->{blksize} = &_optimal_blksize((stat $self)[11]);
1661 $s->{buf} = "";
1662 $s->{eofile} = '';
1663 $s->{errormsg} = "";
1664 vec($s->{fdmask}='', fileno($self), 1) = 1;
1665 $s->{last_line} = "";
1666 $s->{sock_family} = $af;
1667 $s->{num_wrote} = 0;
1668 $s->{opened} = 1;
1669 $s->{pending_errormsg} = "";
1670 $s->{pushback_buf} = "";
1671 $s->{select_supported} = 1;
1672 $s->{timedout} = '';
1673 $s->{unsent_opts} = "";
1674 &_reset_options($s->{opts});
1675
1676 1;
1677 } # end sub open
1678
1679
1680 sub option_accept {
1681 my ($self, @args) = @_;
1682 my (
1683 $arg,
1684 $option,
1685 $s,
1686 @opt_args,
1687 );
1688 local $_;
1689
1690 ## Init.
1691 $s = *$self->{net_telnet};
1692
1693 ## Parse the named args.
1694 while (($_, $arg) = splice @args, 0, 2) {
1695 ## Verify and save arguments.
1696 if (/^-?do$/i) {
1697 ## Make sure a callback is defined.
1698 return $self->error("usage: an option callback must already ",
1699 "be defined when enabling with $_")
1700 unless $s->{opt_cback};
1701
1702 $option = &_verify_telopt_arg($self, $arg, $_);
1703 return unless defined $option;
1704 push @opt_args, { option => $option,
1705 is_remote => '',
1706 is_enable => 1,
1707 };
1708 }
1709 elsif (/^-?dont$/i) {
1710 $option = &_verify_telopt_arg($self, $arg, $_);
1711 return unless defined $option;
1712 push @opt_args, { option => $option,
1713 is_remote => '',
1714 is_enable => '',
1715 };
1716 }
1717 elsif (/^-?will$/i) {
1718 ## Make sure a callback is defined.
1719 return $self->error("usage: an option callback must already ",
1720 "be defined when enabling with $_")
1721 unless $s->{opt_cback};
1722
1723 $option = &_verify_telopt_arg($self, $arg, $_);
1724 return unless defined $option;
1725 push @opt_args, { option => $option,
1726 is_remote => 1,
1727 is_enable => 1,
1728 };
1729 }
1730 elsif (/^-?wont$/i) {
1731 $option = &_verify_telopt_arg($self, $arg, $_);
1732 return unless defined $option;
1733 push @opt_args, { option => $option,
1734 is_remote => 1,
1735 is_enable => '',
1736 };
1737 }
1738 else {
1739 return $self->error('usage: $obj->option_accept(' .
1740 '[Do => $telopt,] ',
1741 '[Dont => $telopt,] ',
1742 '[Will => $telopt,] ',
1743 '[Wont => $telopt,]');
1744 }
1745 }
1746
1747 ## Set "receive ok" for options specified.
1748 &_opt_accept($self, @opt_args);
1749 } # end sub option_accept
1750
1751
1752 sub option_callback {
1753 my ($self, $callback) = @_;
1754 my (
1755 $prev,
1756 $s,
1757 );
1758
1759 $s = *$self->{net_telnet};
1760 $prev = $s->{opt_cback};
1761
1762 if (@_ >= 2) {
1763 unless (defined $callback and ref($callback) eq "CODE") {
1764 &_carp($self, "ignoring Option_callback argument because it's " .
1765 "not a code ref");
1766 $callback = $prev;
1767 }
1768
1769 $s->{opt_cback} = $callback;
1770 }
1771
1772 $prev;
1773 } # end sub option_callback
1774
1775
1776 sub option_log {
1777 my ($self, $name) = @_;
1778 my (
1779 $fh,
1780 $s,
1781 );
1782
1783 $s = *$self->{net_telnet};
1784 $fh = $s->{opt_log};
1785
1786 if (@_ >= 2) {
1787 if (!defined($name) or $name eq "") { # input arg is ""
1788 ## Turn off logging.
1789 $fh = "";
1790 }
1791 elsif (&_is_open_fh($name)) { # input arg is an open fh
1792 ## Use the open fh for logging.
1793 $fh = $name;
1794 select((select($fh), $|=1)[$[]); # don't buffer writes
1795 }
1796 elsif (!ref $name) { # input arg is filename
1797 ## Open the file for logging.
1798 $fh = &_fname_to_handle($self, $name)
1799 or return;
1800 select((select($fh), $|=1)[$[]); # don't buffer writes
1801 }
1802 else {
1803 return $self->error("bad Option_log argument ",
1804 "\"$name\": not filename or open fh");
1805 }
1806
1807 $s->{opt_log} = $fh;
1808 }
1809
1810 $fh;
1811 } # end sub option_log
1812
1813
1814 sub option_state {
1815 my ($self, $option) = @_;
1816 my (
1817 $opt_state,
1818 $s,
1819 %opt_state,
1820 );
1821
1822 ## Ensure telnet option is non-negative integer.
1823 $option = &_verify_telopt_arg($self, $option);
1824 return unless defined $option;
1825
1826 ## Init.
1827 $s = *$self->{net_telnet};
1828 unless (defined $s->{opts}{$option}) {
1829 &_set_default_option($s, $option);
1830 }
1831
1832 ## Return hashref to a copy of the values.
1833 $opt_state = $s->{opts}{$option};
1834 %opt_state = %$opt_state;
1835 \%opt_state;
1836 } # end sub option_state
1837
1838
1839 ## Make ors() synonymous with output_record_separator().
1840 sub ors { &output_record_separator; }
1841
1842
1843 sub output_field_separator {
1844 my ($self, $ofs) = @_;
1845 my (
1846 $prev,
1847 $s,
1848 );
1849
1850 $s = *$self->{net_telnet};
1851 $prev = $s->{"ofs"};
1852
1853 if (@_ >= 2) {
1854 unless (defined $ofs) {
1855 $ofs = "";
1856 }
1857
1858 $s->{"ofs"} = $ofs;
1859 }
1860
1861 $prev;
1862 } # end sub output_field_separator
1863
1864
1865 sub output_log {
1866 my ($self, $name) = @_;
1867 my (
1868 $fh,
1869 $s,
1870 );
1871
1872 $s = *$self->{net_telnet};
1873 $fh = $s->{outputlog};
1874
1875 if (@_ >= 2) {
1876 if (!defined($name) or $name eq "") { # input arg is ""
1877 ## Turn off logging.
1878 $fh = "";
1879 }
1880 elsif (&_is_open_fh($name)) { # input arg is an open fh
1881 ## Use the open fh for logging.
1882 $fh = $name;
1883 select((select($fh), $|=1)[$[]); # don't buffer writes
1884 }
1885 elsif (!ref $name) { # input arg is filename
1886 ## Open the file for logging.
1887 $fh = &_fname_to_handle($self, $name)
1888 or return;
1889 select((select($fh), $|=1)[$[]); # don't buffer writes
1890 }
1891 else {
1892 return $self->error("bad Output_log argument ",
1893 "\"$name\": not filename or open fh");
1894 }
1895
1896 $s->{outputlog} = $fh;
1897 }
1898
1899 $fh;
1900 } # end sub output_log
1901
1902
1903 sub output_record_separator {
1904 my ($self, $ors) = @_;
1905 my (
1906 $prev,
1907 $s,
1908 );
1909
1910 $s = *$self->{net_telnet};
1911 $prev = $s->{"ors"};
1912
1913 if (@_ >= 2) {
1914 unless (defined $ors) {
1915 $ors = "";
1916 }
1917
1918 $s->{"ors"} = $ors;
1919 }
1920
1921 $prev;
1922 } # end sub output_record_separator
1923
1924
1925 sub peerhost {
1926 my ($self) = @_;
1927 my (
1928 $host,
1929 $sockaddr,
1930 );
1931 local $^W = ''; # avoid closed socket warning from getpeername()
1932
1933 ## Get packed sockaddr struct of remote side and then unpack it.
1934 $sockaddr = getpeername $self
1935 or return "";
1936 (undef, $host) = $self->_unpack_sockaddr($sockaddr);
1937
1938 $host;
1939 } # end sub peerhost
1940
1941
1942 sub peerport {
1943 my ($self) = @_;
1944 my (
1945 $port,
1946 $sockaddr,
1947 );
1948 local $^W = ''; # avoid closed socket warning from getpeername()
1949
1950 ## Get packed sockaddr struct of remote side and then unpack it.
1951 $sockaddr = getpeername $self
1952 or return "";
1953 ($port) = $self->_unpack_sockaddr($sockaddr);
1954
1955 $port;
1956 } # end sub peerport
1957
1958
1959 sub port {
1960 my ($self, $port) = @_;
1961 my (
1962 $prev,
1963 $s,
1964 $service,
1965 );
1966
1967 $s = *$self->{net_telnet};
1968 $prev = $s->{port};
1969
1970 if (@_ >= 2) {
1971 $port = &_parse_port($self, $port)
1972 or return;
1973
1974 $s->{port} = $port;
1975 }
1976
1977 $prev;
1978 } # end sub port
1979
1980
1981 sub print {
1982 my ($self) = shift;
1983 my (
1984 $buf,
1985 $fh,
1986 $s,
1987 );
1988
1989 $s = *$self->{net_telnet};
1990 $s->{timedout} = '';
1991 return $self->error("write error: filehandle isn't open")
1992 unless $s->{opened};
1993
1994 ## Add field and record separators.
1995 $buf = join($s->{"ofs"}, @_) . $s->{"ors"};
1996
1997 ## Log the output if requested.
1998 if ($s->{outputlog}) {
1999 &_log_print($s->{outputlog}, $buf);
2000 }
2001
2002 ## Convert native newlines to CR LF.
2003 if (!$s->{bin_mode}) {
2004 $buf =~ s(\n)(\015\012)g;
2005 }
2006
2007 ## Escape TELNET IAC and also CR not followed by LF.
2008 if ($s->{telnet_mode}) {
2009 $buf =~ s(\377)(\377\377)g;
2010 &_escape_cr(\$buf);
2011 }
2012
2013 &_put($self, \$buf, "print");
2014 } # end sub print
2015
2016
2017 sub print_length {
2018 my ($self) = @_;
2019
2020 *$self->{net_telnet}{num_wrote};
2021 } # end sub print_length
2022
2023
2024 sub prompt {
2025 my ($self, $prompt) = @_;
2026 my (
2027 $prev,
2028 $s,
2029 );
2030
2031 $s = *$self->{net_telnet};
2032 $prev = $s->{cmd_prompt};
2033
2034 ## Parse args.
2035 if (@_ == 2) {
2036 $prompt = &_parse_prompt($self, $prompt)
2037 or return;
2038
2039 $s->{cmd_prompt} = $prompt;
2040 }
2041
2042 $prev;
2043 } # end sub prompt
2044
2045
2046 sub put {
2047 my ($self) = @_;
2048 my (
2049 $binmode,
2050 $buf,
2051 $errmode,
2052 $is_timeout_arg,
2053 $s,
2054 $telnetmode,
2055 $timeout,
2056 %args,
2057 );
2058 local $_;
2059
2060 ## Init.
2061 $s = *$self->{net_telnet};
2062 $s->{timedout} = '';
2063
2064 ## Parse args.
2065 if (@_ == 2) { # one positional arg given
2066 $buf = $_[1];
2067 }
2068 elsif (@_ > 2) { # named args given
2069 ## Get the named args.
2070 (undef, %args) = @_;
2071
2072 ## Parse the named args.
2073 foreach (keys %args) {
2074 if (/^-?binmode$/i) {
2075 $binmode = $args{$_};
2076 unless (defined $binmode) {
2077 $binmode = 0;
2078 }
2079 }
2080 elsif (/^-?errmode$/i) {
2081 $errmode = &_parse_errmode($self, $args{$_});
2082 }
2083 elsif (/^-?string$/i) {
2084 $buf = $args{$_};
2085 }
2086 elsif (/^-?telnetmode$/i) {
2087 $telnetmode = $args{$_};
2088 unless (defined $telnetmode) {
2089 $telnetmode = 0;
2090 }
2091 }
2092 elsif (/^-?timeout$/i) {
2093 $timeout = &_parse_timeout($self, $args{$_});
2094 $is_timeout_arg = 1;
2095 }
2096 else {
2097 &_croak($self, "bad named parameter \"$_\" given ",
2098 "to " . ref($self) . "::put()");
2099 }
2100 }
2101 }
2102
2103 ## If any args given, override corresponding instance data.
2104 local $s->{bin_mode} = $binmode
2105 if defined $binmode;
2106 local $s->{errormode} = $errmode
2107 if defined $errmode;
2108 local $s->{telnet_mode} = $telnetmode
2109 if defined $telnetmode;
2110 local $s->{time_out} = $timeout
2111 if defined $is_timeout_arg;
2112
2113 ## Check for errors.
2114 return $self->error("write error: filehandle isn't open")
2115 unless $s->{opened};
2116
2117 ## Log the output if requested.
2118 if ($s->{outputlog}) {
2119 &_log_print($s->{outputlog}, $buf);
2120 }
2121
2122 ## Convert native newlines to CR LF.
2123 if (!$s->{bin_mode}) {
2124 $buf =~ s(\n)(\015\012)g;
2125 }
2126
2127 ## Escape TELNET IAC and also CR not followed by LF.
2128 if ($s->{telnet_mode}) {
2129 $buf =~ s(\377)(\377\377)g;
2130 &_escape_cr(\$buf);
2131 }
2132
2133 &_put($self, \$buf, "put");
2134 } # end sub put
2135
2136
2137 ## Make rs() synonymous input_record_separator().
2138 sub rs { &input_record_separator; }
2139
2140
2141 sub sockfamily {
2142 my ($self) = @_;
2143 my $s = *$self->{net_telnet};
2144 my $sockfamily = "";
2145
2146 if ($s->{sock_family} == AF_INET) {
2147 $sockfamily = "ipv4";
2148 }
2149 elsif (defined($AF_INET6) and $s->{sock_family} == $AF_INET6) {
2150 $sockfamily = "ipv6";
2151 }
2152
2153 $sockfamily;
2154 } # end sub sockfamily
2155
2156
2157 sub sockhost {
2158 my ($self) = @_;
2159 my (
2160 $host,
2161 $sockaddr,
2162 );
2163 local $^W = ''; # avoid closed socket warning from getsockname()
2164
2165 ## Get packed sockaddr struct of local side and then unpack it.
2166 $sockaddr = getsockname $self
2167 or return "";
2168 (undef, $host) = $self->_unpack_sockaddr($sockaddr);
2169
2170 $host;
2171 } # end sub sockhost
2172
2173
2174 sub sockport {
2175 my ($self) = @_;
2176 my (
2177 $port,
2178 $sockaddr,
2179 );
2180 local $^W = ''; # avoid closed socket warning from getsockname()
2181
2182 ## Get packed sockaddr struct of local side and then unpack it.
2183 $sockaddr = getsockname $self
2184 or return "";
2185 ($port) = $self->_unpack_sockaddr($sockaddr);
2186
2187 $port;
2188 } # end sub sockport
2189
2190
2191 sub suboption_callback {
2192 my ($self, $callback) = @_;
2193 my (
2194 $prev,
2195 $s,
2196 );
2197
2198 $s = *$self->{net_telnet};
2199 $prev = $s->{subopt_cback};
2200
2201 if (@_ >= 2) {
2202 unless (defined $callback and ref($callback) eq "CODE") {
2203 &_carp($self,"ignoring Suboption_callback argument because it's " .
2204 "not a code ref");
2205 $callback = $prev;
2206 }
2207
2208 $s->{subopt_cback} = $callback;
2209 }
2210
2211 $prev;
2212 } # end sub suboption_callback
2213
2214
2215 sub telnetmode {
2216 my ($self, $mode) = @_;
2217 my (
2218 $prev,
2219 $s,
2220 );
2221
2222 $s = *$self->{net_telnet};
2223 $prev = $s->{telnet_mode};
2224
2225 if (@_ >= 2) {
2226 unless (defined $mode) {
2227 $mode = 0;
2228 }
2229
2230 $s->{telnet_mode} = $mode;
2231 }
2232
2233 $prev;
2234 } # end sub telnetmode
2235
2236
2237 sub timed_out {
2238 my ($self, $value) = @_;
2239 my (
2240 $prev,
2241 $s,
2242 );
2243
2244 $s = *$self->{net_telnet};
2245 $prev = $s->{timedout};
2246
2247 if (@_ >= 2) {
2248 unless (defined $value) {
2249 $value = "";
2250 }
2251
2252 $s->{timedout} = $value;
2253 }
2254
2255 $prev;
2256 } # end sub timed_out
2257
2258
2259 sub timeout {
2260 my ($self, $timeout) = @_;
2261 my (
2262 $prev,
2263 $s,
2264 );
2265
2266 $s = *$self->{net_telnet};
2267 $prev = $s->{time_out};
2268
2269 if (@_ >= 2) {
2270 $s->{time_out} = &_parse_timeout($self, $timeout);
2271 }
2272
2273 $prev;
2274 } # end sub timeout
2275
2276
2277 sub waitfor {
2278 my ($self, @args) = @_;
2279 my (
2280 $arg,
2281 $binmode,
2282 $endtime,
2283 $errmode,
2284 $len,
2285 $match,
2286 $match_op,
2287 $pos,
2288 $prematch,
2289 $s,
2290 $search,
2291 $search_cond,
2292 $telnetmode,
2293 $timeout,
2294 @match_cond,
2295 @match_ops,
2296 @search_cond,
2297 @string_cond,
2298 @warns,
2299 );
2300 local $@;
2301 local $_;
2302
2303 ## Init.
2304 $s = *$self->{net_telnet};
2305 $s->{timedout} = '';
2306 return if $s->{eofile};
2307 return unless @args;
2308 $timeout = $s->{time_out};
2309
2310 ## Code template used to build string match conditional.
2311 ## Values between array elements must be supplied later.
2312 @string_cond =
2313 ('if (($pos = index $s->{buf}, ', ') > -1) {
2314 $len = ', ';
2315 $prematch = substr $s->{buf}, 0, $pos;
2316 $match = substr $s->{buf}, $pos, $len;
2317 substr($s->{buf}, 0, $pos + $len) = "";
2318 last;
2319 }');
2320
2321 ## Code template used to build pattern match conditional.
2322 ## Values between array elements must be supplied later.
2323 @match_cond =
2324 ('if ($s->{buf} =~ ', ') {
2325 $prematch = $`;
2326 $match = $&;
2327 substr($s->{buf}, 0, length($`) + length($&)) = "";
2328 last;
2329 }');
2330
2331 ## Parse args.
2332 if (@_ == 2) { # one positional arg given
2333 $arg = $_[1];
2334
2335 ## Fill in the blanks in the code template.
2336 push @match_ops, $arg;
2337 push @search_cond, join("", $match_cond[0], $arg, $match_cond[1]);
2338 }
2339 elsif (@_ > 2) { # named args given
2340 ## Parse the named args.
2341 while (($_, $arg) = splice @args, 0, 2) {
2342 if (/^-?binmode$/i) {
2343 $binmode = $arg;
2344 unless (defined $binmode) {
2345 $binmode = 0;
2346 }
2347 }
2348 elsif (/^-?errmode$/i) {
2349 $errmode = &_parse_errmode($self, $arg);
2350 }
2351 elsif (/^-?match$/i) {
2352 ## Fill in the blanks in the code template.
2353 push @match_ops, $arg;
2354 push @search_cond, join("",
2355 $match_cond[0], $arg, $match_cond[1]);
2356 }
2357 elsif (/^-?string$/i) {
2358 ## Fill in the blanks in the code template.
2359 $arg =~ s/'/\\'/g; # quote ticks
2360 push @search_cond, join("",
2361 $string_cond[0], "'$arg'",
2362 $string_cond[1], length($arg),
2363 $string_cond[2]);
2364 }
2365 elsif (/^-?telnetmode$/i) {
2366 $telnetmode = $arg;
2367 unless (defined $telnetmode) {
2368 $telnetmode = 0;
2369 }
2370 }
2371 elsif (/^-?timeout$/i) {
2372 $timeout = &_parse_timeout($self, $arg);
2373 }
2374 else {
2375 &_croak($self, "bad named parameter \"$_\" given " .
2376 "to " . ref($self) . "::waitfor()");
2377 }
2378 }
2379 }
2380
2381 ## If any args given, override corresponding instance data.
2382 local $s->{errormode} = $errmode
2383 if defined $errmode;
2384 local $s->{bin_mode} = $binmode
2385 if defined $binmode;
2386 local $s->{telnet_mode} = $telnetmode
2387 if defined $telnetmode;
2388
2389 ## Check for bad match operator argument.
2390 foreach $match_op (@match_ops) {
2391 return $self->error("missing opening delimiter of match operator ",
2392 "in argument \"$match_op\" given to ",
2393 ref($self) . "::waitfor()")
2394 unless $match_op =~ m(^\s*/) or $match_op =~ m(^\s*m\s*\W);
2395 }
2396
2397 ## Construct conditional to check for requested string and pattern matches.
2398 ## Turn subsequent "if"s into "elsif".
2399 $search_cond = join "\n\tels", @search_cond;
2400
2401 ## Construct loop to fill buffer until string/pattern, timeout, or eof.
2402 $search = join "", "
2403 while (1) {\n\t",
2404 $search_cond, '
2405 &_fillbuf($self, $s, $endtime)
2406 or do {
2407 last if $s->{timedout};
2408 $self->close;
2409 last;
2410 };
2411 }';
2412
2413 ## Set wall time when we timeout.
2414 $endtime = &_endtime($timeout);
2415
2416 ## Run the loop.
2417 {
2418 local $^W = 1;
2419 local $SIG{"__WARN__"} = sub { push @warns, @_ };
2420 local $s->{errormode} = "return";
2421 $s->{errormsg} = "";
2422 eval $search;
2423 }
2424
2425 ## Check for failure.
2426 return $self->error("pattern match timed-out") if $s->{timedout};
2427 return $self->error($s->{errormsg}) if $s->{errormsg} ne "";
2428 return $self->error("pattern match read eof") if $s->{eofile};
2429
2430 ## Check for Perl syntax errors or warnings.
2431 if ($@ or @warns) {
2432 foreach $match_op (@match_ops) {
2433 &_match_check($self, $match_op)
2434 or return;
2435 }
2436 return $self->error($@) if $@;
2437 return $self->error(@warns) if @warns;
2438 }
2439
2440 wantarray ? ($prematch, $match) : 1;
2441 } # end sub waitfor
2442
2443
2444 ######################## Private Subroutines #########################
2445
2446
2447 sub _append_lineno {
2448 my ($obj, @msgs) = @_;
2449 my (
2450 $file,
2451 $line,
2452 $pkg,
2453 );
2454
2455 ## Find the caller that's not in object's class or one of its base classes.
2456 ($pkg, $file , $line) = &_user_caller($obj);
2457 join("", @msgs, " at ", $file, " line ", $line, "\n");
2458 } # end sub _append_lineno
2459
2460
2461 sub _carp {
2462 my ($self) = @_;
2463 my $s = *$self->{net_telnet};
2464
2465 $s->{errormsg} = &_append_lineno(@_);
2466 warn $s->{errormsg}, "\n";
2467 } # end sub _carp
2468
2469
2470 sub _croak {
2471 my ($self) = @_;
2472 my $s = *$self->{net_telnet};
2473
2474 $s->{errormsg} = &_append_lineno(@_);
2475 die $s->{errormsg}, "\n";
2476 } # end sub _croak
2477
2478
2479 sub _endtime {
2480 my ($interval) = @_;
2481
2482 ## Compute wall time when timeout occurs.
2483 if (defined $interval) {
2484 if ($interval >= $^T) { # it's already an absolute time
2485 return $interval;
2486 }
2487 elsif ($interval > 0) { # it's relative to the current time
2488 return int($interval + time + 0.5);
2489 }
2490 else { # it's a one time poll
2491 return 0;
2492 }
2493 }
2494 else { # there's no timeout
2495 return undef;
2496 }
2497 } # end sub _endtime
2498
2499
2500 sub _errno_include {
2501 local $@;
2502 local $SIG{"__DIE__"} = "DEFAULT";
2503
2504 eval "require Errno";
2505 } # end sub errno_include
2506
2507
2508 sub _escape_cr {
2509 my ($string) = @_;
2510 my (
2511 $nextchar,
2512 );
2513 my $pos = 0;
2514
2515 ## Convert all CR (not followed by LF) to CR NULL.
2516 while (($pos = index($$string, "\015", $pos)) > -1) {
2517 $nextchar = substr $$string, $pos + 1, 1;
2518
2519 substr($$string, $pos, 1) = "\015\000"
2520 unless $nextchar eq "\012";
2521
2522 $pos++;
2523 }
2524
2525 1;
2526 } # end sub _escape_cr
2527
2528
2529 sub _extract_arg_errmode {
2530 my ($self, $args) = @_;
2531 my (
2532 %args,
2533 );
2534 local $_;
2535 my $errmode = '';
2536
2537 ## Check for named parameters.
2538 return '' unless @$args >= 2;
2539
2540 ## Rebuild args without errmode parameter.
2541 %args = @$args;
2542 @$args = ();
2543
2544 ## Extract errmode arg.
2545 foreach (keys %args) {
2546 if (/^-?errmode$/i) {
2547 $errmode = &_parse_errmode($self, $args{$_});
2548 }
2549 else {
2550 push @$args, $_, $args{$_};
2551 }
2552 }
2553
2554 $errmode;
2555 } # end sub _extract_arg_errmode
2556
2557
2558 sub _fillbuf {
2559 my ($self, $s, $endtime) = @_;
2560 my (
2561 $msg,
2562 $nfound,
2563 $nread,
2564 $pushback_len,
2565 $read_pos,
2566 $ready,
2567 $timed_out,
2568 $timeout,
2569 $unparsed_pos,
2570 );
2571
2572 ## If error from last read not yet reported then do it now.
2573 if ($s->{pending_errormsg}) {
2574 $msg = $s->{pending_errormsg};
2575 $s->{pending_errormsg} = "";
2576 return $self->error($msg);
2577 }
2578
2579 return unless $s->{opened};
2580
2581 while (1) {
2582 ## Maximum buffer size exceeded?
2583 return $self->error("maximum input buffer length exceeded: ",
2584 $s->{maxbufsize}, " bytes")
2585 unless length($s->{buf}) <= $s->{maxbufsize};
2586
2587 ## Determine how long to wait for input ready.
2588 ($timed_out, $timeout) = &_timeout_interval($endtime);
2589 if ($timed_out) {
2590 $s->{timedout} = 1;
2591 return $self->error("read timed-out");
2592 }
2593
2594 ## Wait for input ready.
2595 $nfound = select $ready=$s->{fdmask}, "", "", $timeout;
2596
2597 ## Handle any errors while waiting.
2598 if ((!defined $nfound or $nfound <= 0) and $s->{select_supported}) {
2599 if (defined $nfound and $nfound == 0) { # timed-out
2600 $s->{timedout} = 1;
2601 return $self->error("read timed-out");
2602 }
2603 else { # error waiting for input ready
2604 if (defined $EINTR) {
2605 next if $! == $EINTR; # restart select()
2606 }
2607 else {
2608 next if $! =~ /^interrupted/i; # restart select()
2609 }
2610
2611 $s->{opened} = '';
2612 return $self->error("read error: $!");
2613 }
2614 }
2615
2616 ## Append to buffer any partially processed telnet or CR sequence.
2617 $pushback_len = length $s->{pushback_buf};
2618 if ($pushback_len) {
2619 $s->{buf} .= $s->{pushback_buf};
2620 $s->{pushback_buf} = "";
2621 }
2622
2623 ## Read the waiting data.
2624 $read_pos = length $s->{buf};
2625 $unparsed_pos = $read_pos - $pushback_len;
2626 $nread = sysread $self, $s->{buf}, $s->{blksize}, $read_pos;
2627
2628 ## Handle any read errors.
2629 if (!defined $nread) { # read failed
2630 if (defined $EINTR) {
2631 next if $! == $EINTR; # restart sysread()
2632 }
2633 else {
2634 next if $! =~ /^interrupted/i; # restart sysread()
2635 }
2636
2637 $s->{opened} = '';
2638 return $self->error("read error: $!");
2639 }
2640
2641 ## Handle eof.
2642 if ($nread == 0) { # eof read
2643 $s->{opened} = '';
2644 return;
2645 }
2646
2647 ## Display network traffic if requested.
2648 if ($s->{dumplog}) {
2649 &_log_dump('<', $s->{dumplog}, \$s->{buf}, $read_pos);
2650 }
2651
2652 ## Process any telnet commands in the data stream.
2653 if ($s->{telnet_mode} and index($s->{buf},"\377",$unparsed_pos) > -1) {
2654 &_interpret_tcmd($self, $s, $unparsed_pos);
2655 }
2656
2657 ## Process any carriage-return sequences in the data stream.
2658 &_interpret_cr($s, $unparsed_pos);
2659
2660 ## Read again if all chars read were consumed as telnet cmds.
2661 next if $unparsed_pos >= length $s->{buf};
2662
2663 ## Log the input if requested.
2664 if ($s->{inputlog}) {
2665 &_log_print($s->{inputlog}, substr($s->{buf}, $unparsed_pos));
2666 }
2667
2668 ## Save the last line read.
2669 &_save_lastline($s);
2670
2671 ## We've successfully read some data into the buffer.
2672 last;
2673 } # end while(1)
2674
2675 1;
2676 } # end sub _fillbuf
2677
2678
2679 sub _flush_opts {
2680 my ($self) = @_;
2681 my (
2682 $option_chars,
2683 );
2684 my $s = *$self->{net_telnet};
2685
2686 ## Get option and clear the output buf.
2687 $option_chars = $s->{unsent_opts};
2688 $s->{unsent_opts} = "";
2689
2690 ## Try to send options without waiting.
2691 {
2692 local $s->{errormode} = "return";
2693 local $s->{time_out} = 0;
2694 &_put($self, \$option_chars, "telnet option negotiation")
2695 or do {
2696 ## Save chars not printed for later.
2697 substr($option_chars, 0, $self->print_length) = "";
2698 $s->{unsent_opts} .= $option_chars;
2699 };
2700 }
2701
2702 1;
2703 } # end sub _flush_opts
2704
2705
2706 sub _fname_to_handle {
2707 my ($self, $filename) = @_;
2708 my (
2709 $fh,
2710 );
2711 no strict "refs";
2712
2713 $fh = &_new_handle();
2714 CORE::open $fh, "> $filename"
2715 or return $self->error("problem creating $filename: $!");
2716
2717 $fh;
2718 } # end sub _fname_to_handle
2719
2720
2721 sub _have_alarm {
2722 local $@;
2723
2724 eval {
2725 local $SIG{"__DIE__"} = "DEFAULT";
2726 local $SIG{ALRM} = sub { die };
2727 alarm 0;
2728 };
2729
2730 ! $@;
2731 } # end sub _have_alarm
2732
2733
2734 sub _import_af_inet6 {
2735 local $@;
2736
2737 eval {
2738 local $SIG{"__DIE__"} = "DEFAULT";
2739
2740 Socket::AF_INET6();
2741 };
2742 } # end sub _import_af_inet6
2743
2744
2745 sub _import_af_unspec {
2746 local $@;
2747
2748 eval {
2749 local $SIG{"__DIE__"} = "DEFAULT";
2750
2751 Socket::AF_UNSPEC();
2752 };
2753 } # end sub _import_af_unspec
2754
2755
2756 sub _import_ai_addrconfig {
2757 local $@;
2758
2759 eval {
2760 local $SIG{"__DIE__"} = "DEFAULT";
2761
2762 Socket::AI_ADDRCONFIG();
2763 };
2764 } # end sub _import_ai_addrconfig
2765
2766
2767 sub _import_eai_badflags {
2768 local $@;
2769
2770 eval {
2771 local $SIG{"__DIE__"} = "DEFAULT";
2772
2773 Socket::EAI_BADFLAGS();
2774 };
2775 } # end sub _import_eai_badflags
2776
2777
2778 sub _import_eintr {
2779 local $@;
2780 local $SIG{"__DIE__"} = "DEFAULT";
2781
2782 eval "require Errno; Errno::EINTR();";
2783 } # end sub _import_eintr
2784
2785
2786 sub _interpret_cr {
2787 my ($s, $pos) = @_;
2788 my (
2789 $nextchar,
2790 );
2791
2792 while (($pos = index($s->{buf}, "\015", $pos)) > -1) {
2793 $nextchar = substr($s->{buf}, $pos + 1, 1);
2794 if ($nextchar eq "\0") {
2795 ## Convert CR NULL to CR when in telnet mode.
2796 if ($s->{telnet_mode}) {
2797 substr($s->{buf}, $pos + 1, 1) = "";
2798 }
2799 }
2800 elsif ($nextchar eq "\012") {
2801 ## Convert CR LF to newline when not in binary mode.
2802 if (!$s->{bin_mode}) {
2803 substr($s->{buf}, $pos, 2) = "\n";
2804 }
2805 }
2806 elsif (!length($nextchar) and ($s->{telnet_mode} or !$s->{bin_mode})) {
2807 ## Save CR in alt buffer for possible CR LF or CR NULL conversion.
2808 $s->{pushback_buf} .= "\015";
2809 chop $s->{buf};
2810 }
2811
2812 $pos++;
2813 }
2814
2815 1;
2816 } # end sub _interpret_cr
2817
2818
2819 sub _interpret_tcmd {
2820 my ($self, $s, $offset) = @_;
2821 my (
2822 $callback,
2823 $endpos,
2824 $nextchar,
2825 $option,
2826 $parameters,
2827 $pos,
2828 $subcmd,
2829 );
2830 local $_;
2831
2832 ## Parse telnet commands in the data stream.
2833 $pos = $offset;
2834 while (($pos = index $s->{buf}, "\377", $pos) > -1) { # unprocessed IAC
2835 $nextchar = substr $s->{buf}, $pos + 1, 1;
2836
2837 ## Save command if it's only partially read.
2838 if (!length $nextchar) {
2839 $s->{pushback_buf} .= "\377";
2840 chop $s->{buf};
2841 last;
2842 }
2843
2844 if ($nextchar eq "\377") { # IAC is escaping "\377" char
2845 ## Remove escape char from data stream.
2846 substr($s->{buf}, $pos, 1) = "";
2847 $pos++;
2848 }
2849 elsif ($nextchar eq "\375" or $nextchar eq "\373" or
2850 $nextchar eq "\374" or $nextchar eq "\376") { # opt negotiation
2851 $option = substr $s->{buf}, $pos + 2, 1;
2852
2853 ## Save command if it's only partially read.
2854 if (!length $option) {
2855 $s->{pushback_buf} .= "\377" . $nextchar;
2856 chop $s->{buf};
2857 chop $s->{buf};
2858 last;
2859 }
2860
2861 ## Remove command from data stream.
2862 substr($s->{buf}, $pos, 3) = "";
2863
2864 ## Handle option negotiation.
2865 &_negotiate_recv($self, $s, $nextchar, ord($option), $pos);
2866 }
2867 elsif ($nextchar eq "\372") { # start of subnegotiation parameters
2868 ## Save command if it's only partially read.
2869 $endpos = index $s->{buf}, "\360", $pos;
2870 if ($endpos == -1) {
2871 $s->{pushback_buf} .= substr $s->{buf}, $pos;
2872 substr($s->{buf}, $pos) = "";
2873 last;
2874 }
2875
2876 ## Remove subnegotiation cmd from buffer.
2877 $subcmd = substr($s->{buf}, $pos, $endpos - $pos + 1);
2878 substr($s->{buf}, $pos, $endpos - $pos + 1) = "";
2879
2880 ## Invoke subnegotiation callback.
2881 if ($s->{subopt_cback} and length($subcmd) >= 5) {
2882 $option = unpack "C", substr($subcmd, 2, 1);
2883 if (length($subcmd) >= 6) {
2884 $parameters = substr $subcmd, 3, length($subcmd) - 5;
2885 }
2886 else {
2887 $parameters = "";
2888 }
2889
2890 $callback = $s->{subopt_cback};
2891 &$callback($self, $option, $parameters);
2892 }
2893 }
2894 else { # various two char telnet commands
2895 ## Ignore and remove command from data stream.
2896 substr($s->{buf}, $pos, 2) = "";
2897 }
2898 }
2899
2900 ## Try to send any waiting option negotiation.
2901 if (length $s->{unsent_opts}) {
2902 &_flush_opts($self);
2903 }
2904
2905 1;
2906 } # end sub _interpret_tcmd
2907
2908
2909 sub _io_socket_include {
2910 local $@;
2911 local $SIG{"__DIE__"} = "DEFAULT";
2912
2913 eval "require IO::Socket";
2914 } # end sub io_socket_include
2915
2916 sub _is_open_fh {
2917 my ($fh) = @_;
2918 my $is_open = '';
2919 local $@;
2920
2921 eval {
2922 local $SIG{"__DIE__"} = "DEFAULT";
2923 $is_open = defined(fileno $fh);
2924 };
2925
2926 if (!$is_open and $@) { # fileno() failed
2927 ## Check if filehandle is tied and printable.
2928 eval {
2929 local $SIG{"__DIE__"} = "DEFAULT";
2930 $is_open = tied(*$fh) && $fh->can("print");
2931 };
2932 }
2933
2934 $is_open;
2935 } # end sub _is_open_fh
2936
2937 sub _log_dump {
2938 my ($direction, $fh, $data, $offset, $len) = @_;
2939 my (
2940 $addr,
2941 $hexvals,
2942 $line,
2943 );
2944
2945 $addr = 0;
2946 $len = length($$data) - $offset
2947 if !defined $len;
2948 return 1 if $len <= 0;
2949
2950 ## Print data in dump format.
2951 while ($len > 0) {
2952 ## Convert up to the next 16 chars to hex, padding w/ spaces.
2953 if ($len >= 16) {
2954 $line = substr $$data, $offset, 16;
2955 }
2956 else {
2957 $line = substr $$data, $offset, $len;
2958 }
2959 $hexvals = unpack("H*", $line);
2960 $hexvals .= ' ' x (32 - length $hexvals);
2961
2962 ## Place in 16 columns, each containing two hex digits.
2963 $hexvals = sprintf("%s %s %s %s " x 4,
2964 unpack("a2" x 16, $hexvals));
2965
2966 ## For the ASCII column, change unprintable chars to a period.
2967 $line =~ s/[\000-\037,\177-\237]/./g;
2968
2969 ## Print the line in dump format.
2970 &_log_print($fh, sprintf("%s 0x%5.5lx: %s%s\n",
2971 $direction, $addr, $hexvals, $line));
2972
2973 $addr += 16;
2974 $offset += 16;
2975 $len -= 16;
2976 }
2977
2978 &_log_print($fh, "\n");
2979
2980 1;
2981 } # end sub _log_dump
2982
2983
2984 sub _log_option {
2985 my ($fh, $direction, $request, $option) = @_;
2986 my (
2987 $name,
2988 );
2989
2990 if ($option >= 0 and $option <= $#Telopts) {
2991 $name = $Telopts[$option];
2992 }
2993 else {
2994 $name = $option;
2995 }
2996
2997 &_log_print($fh, "$direction $request $name\n");
2998 } # end sub _log_option
2999
3000
3001 sub _log_print {
3002 my ($fh, $buf) = @_;
3003 local $\ = '';
3004
3005 if (ref($fh) eq "GLOB") { # fh is GLOB ref
3006 print $fh $buf;
3007 }
3008 else { # fh isn't GLOB ref
3009 $fh->print($buf);
3010 }
3011 } # end sub _log_print
3012
3013
3014 sub _match_check {
3015 my ($self, $code) = @_;
3016 my $error;
3017 my @warns = ();
3018 local $@;
3019
3020 ## Use eval to check for syntax errors or warnings.
3021 {
3022 local $SIG{"__DIE__"} = "DEFAULT";
3023 local $SIG{"__WARN__"} = sub { push @warns, @_ };
3024 local $^W = 1;
3025 local $_ = '';
3026 eval "\$_ =~ $code;";
3027 }
3028 if ($@) {
3029 ## Remove useless lines numbers from message.
3030 ($error = $@) =~ s/ at \(eval \d+\) line \d+.?//;
3031 chomp $error;
3032 return $self->error("bad match operator: $error");
3033 }
3034 elsif (@warns) {
3035 ## Remove useless lines numbers from message.
3036 ($error = shift @warns) =~ s/ at \(eval \d+\) line \d+.?//;
3037 $error =~ s/ while "strict subs" in use//;
3038 chomp $error;
3039 return $self->error("bad match operator: $error");
3040 }
3041
3042 1;
3043 } # end sub _match_check
3044
3045
3046 sub _negotiate_callback {
3047 my ($self, $opt, $is_remote, $is_enabled, $was_enabled, $opt_bufpos) = @_;
3048 my (
3049 $callback,
3050 $s,
3051 );
3052 local $_;
3053
3054 ## Keep track of remote echo.
3055 if ($is_remote and $opt == &TELOPT_ECHO) { # received WILL or WONT ECHO
3056 $s = *$self->{net_telnet};
3057
3058 if ($is_enabled and !$was_enabled) { # received WILL ECHO
3059 $s->{remote_echo} = 1;
3060 }
3061 elsif (!$is_enabled and $was_enabled) { # received WONT ECHO
3062 $s->{remote_echo} = '';
3063 }
3064 }
3065
3066 ## Invoke callback, if there is one.
3067 $callback = $self->option_callback;
3068 if ($callback) {
3069 &$callback($self, $opt, $is_remote,
3070 $is_enabled, $was_enabled, $opt_bufpos);
3071 }
3072
3073 1;
3074 } # end sub _negotiate_callback
3075
3076
3077 sub _negotiate_recv {
3078 my ($self, $s, $opt_request, $opt, $opt_bufpos) = @_;
3079
3080 ## Ensure data structure exists for this option.
3081 unless (defined $s->{opts}{$opt}) {
3082 &_set_default_option($s, $opt);
3083 }
3084
3085 ## Process the option.
3086 if ($opt_request eq "\376") { # DONT
3087 &_negotiate_recv_disable($self, $s, $opt, "dont", $opt_bufpos,
3088 $s->{opts}{$opt}{local_enable_ok},
3089 \$s->{opts}{$opt}{local_enabled},
3090 \$s->{opts}{$opt}{local_state});
3091 }
3092 elsif ($opt_request eq "\375") { # DO
3093 &_negotiate_recv_enable($self, $s, $opt, "do", $opt_bufpos,
3094 $s->{opts}{$opt}{local_enable_ok},
3095 \$s->{opts}{$opt}{local_enabled},
3096 \$s->{opts}{$opt}{local_state});
3097 }
3098 elsif ($opt_request eq "\374") { # WONT
3099 &_negotiate_recv_disable($self, $s, $opt, "wont", $opt_bufpos,
3100 $s->{opts}{$opt}{remote_enable_ok},
3101 \$s->{opts}{$opt}{remote_enabled},
3102 \$s->{opts}{$opt}{remote_state});
3103 }
3104 elsif ($opt_request eq "\373") { # WILL
3105 &_negotiate_recv_enable($self, $s, $opt, "will", $opt_bufpos,
3106 $s->{opts}{$opt}{remote_enable_ok},
3107 \$s->{opts}{$opt}{remote_enabled},
3108 \$s->{opts}{$opt}{remote_state});
3109 }
3110 else { # internal error
3111 die;
3112 }
3113
3114 1;
3115 } # end sub _negotiate_recv
3116
3117
3118 sub _negotiate_recv_disable {
3119 my ($self, $s, $opt, $opt_request,
3120 $opt_bufpos, $enable_ok, $is_enabled, $state) = @_;
3121 my (
3122 $ack,
3123 $disable_cmd,
3124 $enable_cmd,
3125 $is_remote,
3126 $nak,
3127 $was_enabled,
3128 );
3129
3130 ## What do we use to request enable/disable or respond with ack/nak.
3131 if ($opt_request eq "wont") {
3132 $enable_cmd = "\377\375" . pack("C", $opt); # do command
3133 $disable_cmd = "\377\376" . pack("C", $opt); # dont command
3134 $is_remote = 1;
3135 $ack = "DO";
3136 $nak = "DONT";
3137
3138 &_log_option($s->{opt_log}, "RCVD", "WONT", $opt)
3139 if $s->{opt_log};
3140 }
3141 elsif ($opt_request eq "dont") {
3142 $enable_cmd = "\377\373" . pack("C", $opt); # will command
3143 $disable_cmd = "\377\374" . pack("C", $opt); # wont command
3144 $is_remote = '';
3145 $ack = "WILL";
3146 $nak = "WONT";
3147
3148 &_log_option($s->{opt_log}, "RCVD", "DONT", $opt)
3149 if $s->{opt_log};
3150 }
3151 else { # internal error
3152 die;
3153 }
3154
3155 ## Respond to WONT or DONT based on the current negotiation state.
3156 if ($$state eq "no") { # state is already disabled
3157 }
3158 elsif ($$state eq "yes") { # they're initiating disable
3159 $$is_enabled = '';
3160 $$state = "no";
3161
3162 ## Send positive acknowledgment.
3163 $s->{unsent_opts} .= $disable_cmd;
3164 &_log_option($s->{opt_log}, "SENT", $nak, $opt)
3165 if $s->{opt_log};
3166
3167 ## Invoke callbacks.
3168 &_negotiate_callback($self, $opt, $is_remote,
3169 $$is_enabled, $was_enabled, $opt_bufpos);
3170 }
3171 elsif ($$state eq "wantno") { # they sent positive ack
3172 $$is_enabled = '';
3173 $$state = "no";
3174
3175 ## Invoke callback.
3176 &_negotiate_callback($self, $opt, $is_remote,
3177 $$is_enabled, $was_enabled, $opt_bufpos);
3178 }
3179 elsif ($$state eq "wantno opposite") { # pos ack but we changed our mind
3180 ## Indicate disabled but now we want to enable.
3181 $$is_enabled = '';
3182 $$state = "wantyes";
3183
3184 ## Send queued request.
3185 $s->{unsent_opts} .= $enable_cmd;
3186 &_log_option($s->{opt_log}, "SENT", $ack, $opt)
3187 if $s->{opt_log};
3188
3189 ## Invoke callback.
3190 &_negotiate_callback($self, $opt, $is_remote,
3191 $$is_enabled, $was_enabled, $opt_bufpos);
3192 }
3193 elsif ($$state eq "wantyes") { # they sent negative ack
3194 $$is_enabled = '';
3195 $$state = "no";
3196
3197 ## Invoke callback.
3198 &_negotiate_callback($self, $opt, $is_remote,
3199 $$is_enabled, $was_enabled, $opt_bufpos);
3200 }
3201 elsif ($$state eq "wantyes opposite") { # nak but we changed our mind
3202 $$is_enabled = '';
3203 $$state = "no";
3204
3205 ## Invoke callback.
3206 &_negotiate_callback($self, $opt, $is_remote,
3207 $$is_enabled, $was_enabled, $opt_bufpos);
3208 }
3209 } # end sub _negotiate_recv_disable
3210
3211
3212 sub _negotiate_recv_enable {
3213 my ($self, $s, $opt, $opt_request,
3214 $opt_bufpos, $enable_ok, $is_enabled, $state) = @_;
3215 my (
3216 $ack,
3217 $disable_cmd,
3218 $enable_cmd,
3219 $is_remote,
3220 $nak,
3221 $was_enabled,
3222 );
3223
3224 ## What we use to send enable/disable request or send ack/nak response.
3225 if ($opt_request eq "will") {
3226 $enable_cmd = "\377\375" . pack("C", $opt); # do command
3227 $disable_cmd = "\377\376" . pack("C", $opt); # dont command
3228 $is_remote = 1;
3229 $ack = "DO";
3230 $nak = "DONT";
3231
3232 &_log_option($s->{opt_log}, "RCVD", "WILL", $opt)
3233 if $s->{opt_log};
3234 }
3235 elsif ($opt_request eq "do") {
3236 $enable_cmd = "\377\373" . pack("C", $opt); # will command
3237 $disable_cmd = "\377\374" . pack("C", $opt); # wont command
3238 $is_remote = '';
3239 $ack = "WILL";
3240 $nak = "WONT";
3241
3242 &_log_option($s->{opt_log}, "RCVD", "DO", $opt)
3243 if $s->{opt_log};
3244 }
3245 else { # internal error
3246 die;
3247 }
3248
3249 ## Save current enabled state.
3250 $was_enabled = $$is_enabled;
3251
3252 ## Respond to WILL or DO based on the current negotiation state.
3253 if ($$state eq "no") { # they're initiating enable
3254 if ($enable_ok) { # we agree they/us should enable
3255 $$is_enabled = 1;
3256 $$state = "yes";
3257
3258 ## Send positive acknowledgment.
3259 $s->{unsent_opts} .= $enable_cmd;
3260 &_log_option($s->{opt_log}, "SENT", $ack, $opt)
3261 if $s->{opt_log};
3262
3263 ## Invoke callbacks.
3264 &_negotiate_callback($self, $opt, $is_remote,
3265 $$is_enabled, $was_enabled, $opt_bufpos);
3266 }
3267 else { # we disagree they/us should enable
3268 ## Send negative acknowledgment.
3269 $s->{unsent_opts} .= $disable_cmd;
3270 &_log_option($s->{opt_log}, "SENT", $nak, $opt)
3271 if $s->{opt_log};
3272 }
3273 }
3274 elsif ($$state eq "yes") { # state is already enabled
3275 }
3276 elsif ($$state eq "wantno") { # error: our disable req answered by enable
3277 $$is_enabled = '';
3278 $$state = "no";
3279
3280 ## Invoke callbacks.
3281 &_negotiate_callback($self, $opt, $is_remote,
3282 $$is_enabled, $was_enabled, $opt_bufpos);
3283 }
3284 elsif ($$state eq "wantno opposite") { # err: disable req answerd by enable
3285 $$is_enabled = 1;
3286 $$state = "yes";
3287
3288 ## Invoke callbacks.
3289 &_negotiate_callback($self, $opt, $is_remote,
3290 $$is_enabled, $was_enabled, $opt_bufpos);
3291 }
3292 elsif ($$state eq "wantyes") { # they sent pos ack
3293 $$is_enabled = 1;
3294 $$state = "yes";
3295
3296 ## Invoke callback.
3297 &_negotiate_callback($self, $opt, $is_remote,
3298 $$is_enabled, $was_enabled, $opt_bufpos);
3299 }
3300 elsif ($$state eq "wantyes opposite") { # pos ack but we changed our mind
3301 ## Indicate enabled but now we want to disable.
3302 $$is_enabled = 1;
3303 $$state = "wantno";
3304
3305 ## Inform other side we changed our mind.
3306 $s->{unsent_opts} .= $disable_cmd;
3307 &_log_option($s->{opt_log}, "SENT", $nak, $opt)
3308 if $s->{opt_log};
3309
3310 ## Invoke callback.
3311 &_negotiate_callback($self, $opt, $is_remote,
3312 $$is_enabled, $was_enabled, $opt_bufpos);
3313 }
3314
3315 1;
3316 } # end sub _negotiate_recv_enable
3317
3318
3319 sub _new_handle {
3320 if ($INC{"IO/Handle.pm"}) {
3321 return IO::Handle->new;
3322 }
3323 else {
3324 require FileHandle;
3325 return FileHandle->new;
3326 }
3327 } # end sub _new_handle
3328
3329
3330 sub _next_getlines {
3331 my ($self, $s) = @_;
3332 my (
3333 $len,
3334 $line,
3335 $pos,
3336 @lines,
3337 );
3338
3339 ## Fill buffer and get first line.
3340 $line = $self->getline
3341 or return;
3342 push @lines, $line;
3343
3344 ## Extract subsequent lines from buffer.
3345 while (($pos = index($s->{buf}, $s->{"rs"})) != -1) {
3346 $len = $pos + length $s->{"rs"};
3347 push @lines, substr($s->{buf}, 0, $len);
3348 substr($s->{buf}, 0, $len) = "";
3349 }
3350
3351 @lines;
3352 } # end sub _next_getlines
3353
3354
3355 sub _opt_accept {
3356 my ($self, @args) = @_;
3357 my (
3358 $arg,
3359 $option,
3360 $s,
3361 );
3362
3363 ## Init.
3364 $s = *$self->{net_telnet};
3365
3366 foreach $arg (@args) {
3367 ## Ensure data structure defined for this option.
3368 $option = $arg->{option};
3369 if (!defined $s->{opts}{$option}) {
3370 &_set_default_option($s, $option);
3371 }
3372
3373 ## Save whether we'll accept or reject this option.
3374 if ($arg->{is_remote}) {
3375 $s->{opts}{$option}{remote_enable_ok} = $arg->{is_enable};
3376 }
3377 else {
3378 $s->{opts}{$option}{local_enable_ok} = $arg->{is_enable};
3379 }
3380 }
3381
3382 1;
3383 } # end sub _opt_accept
3384
3385
3386 sub _optimal_blksize {
3387 my ($blksize) = @_;
3388 local $^W = ''; # avoid non-numeric warning for ms-windows blksize of ""
3389
3390 ## Use default when block size is invalid.
3391 if (!defined $blksize or $blksize < 512 or $blksize > 1_048_576) {
3392 $blksize = 4096;
3393 }
3394
3395 $blksize;
3396 } # end sub _optimal_blksize
3397
3398
3399 sub _parse_cmd_remove_mode {
3400 my ($self, $mode) = @_;
3401
3402 if (!defined $mode) {
3403 $mode = 0;
3404 }
3405 elsif ($mode =~ /^\s*auto\s*$/i) {
3406 $mode = "auto";
3407 }
3408 elsif ($mode !~ /^\d+$/) {
3409 &_carp($self, "ignoring bad Cmd_remove_mode " .
3410 "argument \"$mode\": it's not \"auto\" or a " .
3411 "non-negative integer");
3412 $mode = *$self->{net_telnet}{cmd_rm_mode};
3413 }
3414
3415 $mode;
3416 } # end sub _parse_cmd_remove_mode
3417
3418
3419 sub _parse_errmode {
3420 my ($self, $errmode) = @_;
3421
3422 ## Set the error mode.
3423 if (!defined $errmode) {
3424 &_carp($self, "ignoring undefined Errmode argument");
3425 $errmode = *$self->{net_telnet}{errormode};
3426 }
3427 elsif ($errmode =~ /^\s*return\s*$/i) {
3428 $errmode = "return";
3429 }
3430 elsif ($errmode =~ /^\s*die\s*$/i) {
3431 $errmode = "die";
3432 }
3433 elsif (ref($errmode) eq "CODE") {
3434 }
3435 elsif (ref($errmode) eq "ARRAY") {
3436 unless (ref($errmode->[0]) eq "CODE") {
3437 &_carp($self, "ignoring bad Errmode argument: " .
3438 "first list item isn't a code ref");
3439 $errmode = *$self->{net_telnet}{errormode};
3440 }
3441 }
3442 else {
3443 &_carp($self, "ignoring bad Errmode argument \"$errmode\"");
3444 $errmode = *$self->{net_telnet}{errormode};
3445 }
3446
3447 $errmode;
3448 } # end sub _parse_errmode
3449
3450
3451 sub _parse_family {
3452 my ($self, $family) = @_;
3453 my (
3454 $parsed_family,
3455 );
3456
3457 unless (defined $family) {
3458 $family = "";
3459 }
3460
3461 if ($family =~ /^\s*ipv4\s*$/i) { # family arg is "ipv4"
3462 $parsed_family = "ipv4";
3463 }
3464 elsif ($family =~ /^\s*any\s*$/i) { # family arg is "any"
3465 if ($Socket::VERSION >= 1.94 and defined $AF_INET6) { # has IPv6
3466 $parsed_family = "any";
3467 }
3468 else { # IPv6 not supported on this machine
3469 $parsed_family = "ipv4";
3470 }
3471 }
3472 elsif ($family =~ /^\s*ipv6\s*$/i) { # family arg is "ipv6"
3473 return $self->error("Family arg ipv6 not supported when " .
3474 "Socket.pm version < 1.94")
3475 unless $Socket::VERSION >= 1.94;
3476 return $self->error("Family arg ipv6 not supported by " .
3477 "this OS: AF_INET6 not in Socket.pm")
3478 unless defined $AF_INET6;
3479
3480 $parsed_family = "ipv6";
3481 }
3482 else {
3483 return $self->error("bad Family argument \"$family\": " .
3484 "must be \"ipv4\", \"ipv6\", or \"any\"");
3485 }
3486
3487 $parsed_family;
3488 } # end sub _parse_family
3489
3490
3491 sub _parse_input_record_separator {
3492 my ($self, $rs) = @_;
3493
3494 unless (defined $rs and length $rs) {
3495 &_carp($self, "ignoring null Input_record_separator argument");
3496 $rs = *$self->{net_telnet}{"rs"};
3497 }
3498
3499 $rs;
3500 } # end sub _parse_input_record_separator
3501
3502
3503 sub _parse_localfamily {
3504 my ($self, $family) = @_;
3505
3506 unless (defined $family) {
3507 $family = "";
3508 }
3509
3510 if ($family =~ /^\s*ipv4\s*$/i) { # family arg is "ipv4"
3511 $family = "ipv4";
3512 }
3513 elsif ($family =~ /^\s*any\s*$/i) { # family arg is "any"
3514 if ($Socket::VERSION >= 1.94 and defined $AF_INET6) { # has IPv6
3515 $family = "any";
3516 }
3517 else { # IPv6 not supported on this machine
3518 $family = "ipv4";
3519 }
3520 }
3521 elsif ($family =~ /^\s*ipv6\s*$/i) { # family arg is "ipv6"
3522 return $self->error("Localfamily arg ipv6 not supported when " .
3523 "Socket.pm version < 1.94")
3524 unless $Socket::VERSION >= 1.94;
3525 return $self->error("Localfamily arg ipv6 not supported by " .
3526 "this OS: AF_INET6 not in Socket.pm")
3527 unless defined $AF_INET6;
3528
3529 $family = "ipv6";
3530 }
3531 else {
3532 return $self->error("bad Localfamily argument \"$family\": " .
3533 "must be \"ipv4\", \"ipv6\", or \"any\"");
3534 }
3535
3536 $family;
3537 } # end sub _parse_localfamily
3538
3539
3540 sub _parse_port {
3541 my ($self, $port) = @_;
3542 my (
3543 $service,
3544 );
3545
3546 unless (defined $port) {
3547 $port = "";
3548 }
3549
3550 return $self->error("bad Port argument \"$port\"")
3551 unless $port;
3552
3553 if ($port !~ /^\d+$/) { # port isn't all digits
3554 $service = $port;
3555 $port = getservbyname($service, "tcp");
3556
3557 return $self->error("bad Port argument \"$service\": " .
3558 "it's an unknown TCP service")
3559 unless $port;
3560 }
3561
3562 $port;
3563 } # end sub _parse_port
3564
3565
3566 sub _parse_prompt {
3567 my ($self, $prompt) = @_;
3568
3569 unless (defined $prompt) {
3570 $prompt = "";
3571 }
3572
3573 return $self->error("bad Prompt argument \"$prompt\": " .
3574 "missing opening delimiter of match operator")
3575 unless $prompt =~ m(^\s*/) or $prompt =~ m(^\s*m\s*\W);
3576
3577 $prompt;
3578 } # end sub _parse_prompt
3579
3580
3581 sub _parse_timeout {
3582 my ($self, $timeout) = @_;
3583 local $@;
3584
3585 ## Ensure valid timeout.
3586 if (defined $timeout) {
3587 ## Test for non-numeric or negative values.
3588 eval {
3589 local $SIG{"__DIE__"} = "DEFAULT";
3590 local $SIG{"__WARN__"} = sub { die "non-numeric\n" };
3591 local $^W = 1;
3592 $timeout *= 1;
3593 };
3594 if ($@) { # timeout arg is non-numeric
3595 &_carp($self,
3596 "ignoring non-numeric Timeout argument \"$timeout\"");
3597 $timeout = *$self->{net_telnet}{time_out};
3598 }
3599 elsif ($timeout < 0) { # timeout arg is negative
3600 &_carp($self, "ignoring negative Timeout argument \"$timeout\"");
3601 $timeout = *$self->{net_telnet}{time_out};
3602 }
3603 }
3604
3605 $timeout;
3606 } # end sub _parse_timeout
3607
3608
3609 sub _put {
3610 my ($self, $buf, $subname) = @_;
3611 my (
3612 $endtime,
3613 $len,
3614 $nfound,
3615 $nwrote,
3616 $offset,
3617 $ready,
3618 $s,
3619 $timed_out,
3620 $timeout,
3621 $zero_wrote_count,
3622 );
3623
3624 ## Init.
3625 $s = *$self->{net_telnet};
3626 $s->{num_wrote} = 0;
3627 $zero_wrote_count = 0;
3628 $offset = 0;
3629 $len = length $$buf;
3630 $endtime = &_endtime($s->{time_out});
3631
3632 return $self->error("write error: filehandle isn't open")
3633 unless $s->{opened};
3634
3635 ## Try to send any waiting option negotiation.
3636 if (length $s->{unsent_opts}) {
3637 &_flush_opts($self);
3638 }
3639
3640 ## Write until all data blocks written.
3641 while ($len) {
3642 ## Determine how long to wait for output ready.
3643 ($timed_out, $timeout) = &_timeout_interval($endtime);
3644 if ($timed_out) {
3645 $s->{timedout} = 1;
3646 return $self->error("$subname timed-out");
3647 }
3648
3649 ## Wait for output ready.
3650 $nfound = select "", $ready=$s->{fdmask}, "", $timeout;
3651
3652 ## Handle any errors while waiting.
3653 if ((!defined $nfound or $nfound <= 0) and $s->{select_supported}) {
3654 if (defined $nfound and $nfound == 0) { # timed-out
3655 $s->{timedout} = 1;
3656 return $self->error("$subname timed-out");
3657 }
3658 else { # error waiting for output ready
3659 if (defined $EINTR) {
3660 next if $! == $EINTR; # restart select()
3661 }
3662 else {
3663 next if $! =~ /^interrupted/i; # restart select()
3664 }
3665
3666 $s->{opened} = '';
3667 return $self->error("write error: $!");
3668 }
3669 }
3670
3671 ## Write the data.
3672 $nwrote = syswrite $self, $$buf, $s->{blksize}, $offset;
3673
3674 ## Handle any write errors.
3675 if (!defined $nwrote) { # write failed
3676 if (defined $EINTR) {
3677 next if $! == $EINTR; # restart syswrite()
3678 }
3679 else {
3680 next if $! =~ /^interrupted/i; # restart syswrite()
3681 }
3682
3683 $s->{opened} = '';
3684 return $self->error("write error: $!");
3685 }
3686 elsif ($nwrote == 0) { # zero chars written
3687 ## Try ten more times to write the data.
3688 if ($zero_wrote_count++ <= 10) {
3689 &_sleep(0.01);
3690 next;
3691 }
3692
3693 $s->{opened} = '';
3694 return $self->error("write error: zero length write: $!");
3695 }
3696
3697 ## Display network traffic if requested.
3698 if ($s->{dumplog}) {
3699 &_log_dump('>', $s->{dumplog}, $buf, $offset, $nwrote);
3700 }
3701
3702 ## Increment.
3703 $s->{num_wrote} += $nwrote;
3704 $offset += $nwrote;
3705 $len -= $nwrote;
3706 }
3707
3708 1;
3709 } # end sub _put
3710
3711
3712 sub _qualify_fh {
3713 my ($obj, $name) = @_;
3714 my (
3715 $user_class,
3716 );
3717 local $@;
3718 local $_;
3719
3720 ## Get user's package name.
3721 ($user_class) = &_user_caller($obj);
3722
3723 ## Ensure name is qualified with a package name.
3724 $name = qualify($name, $user_class);
3725
3726 ## If it's not already, make it a typeglob ref.
3727 if (!ref $name) {
3728 no strict;
3729 local $SIG{"__DIE__"} = "DEFAULT";
3730 local $^W = '';
3731
3732 $name =~ s/^\*+//;
3733 $name = eval "\\*$name";
3734 return unless ref $name;
3735 }
3736
3737 $name;
3738 } # end sub _qualify_fh
3739
3740
3741 sub _reset_options {
3742 my ($opts) = @_;
3743 my (
3744 $opt,
3745 );
3746
3747 foreach $opt (keys %$opts) {
3748 $opts->{$opt}{remote_enabled} = '';
3749 $opts->{$opt}{remote_state} = "no";
3750 $opts->{$opt}{local_enabled} = '';
3751 $opts->{$opt}{local_state} = "no";
3752 }
3753
3754 1;
3755 } # end sub _reset_options
3756
3757
3758 sub _save_lastline {
3759 my ($s) = @_;
3760 my (
3761 $firstpos,
3762 $lastpos,
3763 $len_w_sep,
3764 $len_wo_sep,
3765 $offset,
3766 );
3767 my $rs = "\n";
3768
3769 if (($lastpos = rindex $s->{buf}, $rs) > -1) { # eol found
3770 while (1) {
3771 ## Find beginning of line.
3772 $firstpos = rindex $s->{buf}, $rs, $lastpos - 1;
3773 if ($firstpos == -1) {
3774 $offset = 0;
3775 }
3776 else {
3777 $offset = $firstpos + length $rs;
3778 }
3779
3780 ## Determine length of line with and without separator.
3781 $len_wo_sep = $lastpos - $offset;
3782 $len_w_sep = $len_wo_sep + length $rs;
3783
3784 ## Save line if it's not blank.
3785 if (substr($s->{buf}, $offset, $len_wo_sep)
3786 !~ /^\s*$/)
3787 {
3788 $s->{last_line} = substr($s->{buf},
3789 $offset,
3790 $len_w_sep);
3791 last;
3792 }
3793
3794 last if $firstpos == -1;
3795
3796 $lastpos = $firstpos;
3797 }
3798 }
3799
3800 1;
3801 } # end sub _save_lastline
3802
3803
3804 sub _set_default_option {
3805 my ($s, $option) = @_;
3806
3807 $s->{opts}{$option} = {
3808 remote_enabled => '',
3809 remote_state => "no",
3810 remote_enable_ok => '',
3811 local_enabled => '',
3812 local_state => "no",
3813 local_enable_ok => '',
3814 };
3815 } # end sub _set_default_option
3816
3817
3818 sub _sleep {
3819 my ($secs) = @_;
3820 my $bitmask = "";
3821 local *SOCK;
3822
3823 socket SOCK, AF_INET, SOCK_STREAM, 0;
3824 vec($bitmask, fileno(SOCK), 1) = 1;
3825 select $bitmask, "", "", $secs;
3826 CORE::close SOCK;
3827
3828 1;
3829 } # end sub _sleep
3830
3831
3832 sub _timeout_interval {
3833 my ($endtime) = @_;
3834 my (
3835 $timeout,
3836 );
3837
3838 ## Return timed-out boolean and timeout interval.
3839 if (defined $endtime) {
3840 ## Is it a one-time poll.
3841 return ('', 0) if $endtime == 0;
3842
3843 ## Calculate the timeout interval.
3844 $timeout = $endtime - time;
3845
3846 ## Did we already timeout.
3847 return (1, 0) unless $timeout > 0;
3848
3849 return ('', $timeout);
3850 }
3851 else { # there is no timeout
3852 return ('', undef);
3853 }
3854 } # end sub _timeout_interval
3855
3856
3857 sub _unpack_sockaddr {
3858 my ($self, $sockaddr) = @_;
3859 my (
3860 $packed_addr,
3861 $sockfamily,
3862 );
3863 my $addr = "";
3864 my $port = "";
3865
3866 $sockfamily = $self->sockfamily;
3867
3868 ## Parse sockaddr struct.
3869 if ($sockfamily eq "ipv4") {
3870 ($port, $packed_addr) = sockaddr_in($sockaddr);
3871 $addr = Socket::inet_ntoa($packed_addr);
3872 }
3873 elsif ($sockfamily eq "ipv6") {
3874 ($port, $packed_addr) = Socket::sockaddr_in6($sockaddr);
3875 $addr = Socket::inet_ntop($AF_INET6, $packed_addr);
3876 }
3877
3878 ($port, $addr);
3879 } # end sub _unpack_sockaddr
3880
3881
3882 sub _user_caller {
3883 my ($obj) = @_;
3884 my (
3885 $class,
3886 $curr_pkg,
3887 $file,
3888 $i,
3889 $line,
3890 $pkg,
3891 %isa,
3892 @isa,
3893 );
3894 local $@;
3895 local $_;
3896
3897 ## Create a boolean hash to test for isa. Make sure current
3898 ## package and the object's class are members.
3899 $class = ref $obj;
3900 @isa = eval "\@${class}::ISA";
3901 push @isa, $class;
3902 ($curr_pkg) = caller 1;
3903 push @isa, $curr_pkg;
3904 %isa = map { $_ => 1 } @isa;
3905
3906 ## Search back in call frames for a package that's not in isa.
3907 $i = 1;
3908 while (($pkg, $file, $line) = caller ++$i) {
3909 next if $isa{$pkg};
3910
3911 return ($pkg, $file, $line);
3912 }
3913
3914 ## If not found, choose outer most call frame.
3915 ($pkg, $file, $line) = caller --$i;
3916 return ($pkg, $file, $line);
3917 } # end sub _user_caller
3918
3919
3920 sub _verify_telopt_arg {
3921 my ($self, $option, $argname) = @_;
3922 local $@;
3923
3924 ## If provided, use argument name in error message.
3925 if (defined $argname) {
3926 $argname = "for arg $argname";
3927 }
3928 else {
3929 $argname = "";
3930 }
3931
3932 ## Ensure telnet option is a non-negative integer.
3933 eval {
3934 local $SIG{"__DIE__"} = "DEFAULT";
3935 local $SIG{"__WARN__"} = sub { die "non-numeric\n" };
3936 local $^W = 1;
3937 $option = abs(int $option);
3938 };
3939 return $self->error("bad telnet option $argname: non-numeric")
3940 if $@;
3941
3942 return $self->error("bad telnet option $argname: option > 255")
3943 unless $option <= 255;
3944
3945 $option;
3946 } # end sub _verify_telopt_arg
3947
3948
3949 ######################## Exported Constants ##########################
3950
3951
3952 sub TELNET_IAC () {255}; # interpret as command:
3953 sub TELNET_DONT () {254}; # you are not to use option
3954 sub TELNET_DO () {253}; # please, you use option
3955 sub TELNET_WONT () {252}; # I won't use option
3956 sub TELNET_WILL () {251}; # I will use option
3957 sub TELNET_SB () {250}; # interpret as subnegotiation
3958 sub TELNET_GA () {249}; # you may reverse the line
3959 sub TELNET_EL () {248}; # erase the current line
3960 sub TELNET_EC () {247}; # erase the current character
3961 sub TELNET_AYT () {246}; # are you there
3962 sub TELNET_AO () {245}; # abort output--but let prog finish
3963 sub TELNET_IP () {244}; # interrupt process--permanently
3964 sub TELNET_BREAK () {243}; # break
3965 sub TELNET_DM () {242}; # data mark--for connect. cleaning
3966 sub TELNET_NOP () {241}; # nop
3967 sub TELNET_SE () {240}; # end sub negotiation
3968 sub TELNET_EOR () {239}; # end of record (transparent mode)
3969 sub TELNET_ABORT () {238}; # Abort process
3970 sub TELNET_SUSP () {237}; # Suspend process
3971 sub TELNET_EOF () {236}; # End of file
3972 sub TELNET_SYNCH () {242}; # for telfunc calls
3973
3974 sub TELOPT_BINARY () {0}; # Binary Transmission
3975 sub TELOPT_ECHO () {1}; # Echo
3976 sub TELOPT_RCP () {2}; # Reconnection
3977 sub TELOPT_SGA () {3}; # Suppress Go Ahead
3978 sub TELOPT_NAMS () {4}; # Approx Message Size Negotiation
3979 sub TELOPT_STATUS () {5}; # Status
3980 sub TELOPT_TM () {6}; # Timing Mark
3981 sub TELOPT_RCTE () {7}; # Remote Controlled Trans and Echo
3982 sub TELOPT_NAOL () {8}; # Output Line Width
3983 sub TELOPT_NAOP () {9}; # Output Page Size
3984 sub TELOPT_NAOCRD () {10}; # Output Carriage-Return Disposition
3985 sub TELOPT_NAOHTS () {11}; # Output Horizontal Tab Stops
3986 sub TELOPT_NAOHTD () {12}; # Output Horizontal Tab Disposition
3987 sub TELOPT_NAOFFD () {13}; # Output Formfeed Disposition
3988 sub TELOPT_NAOVTS () {14}; # Output Vertical Tabstops
3989 sub TELOPT_NAOVTD () {15}; # Output Vertical Tab Disposition
3990 sub TELOPT_NAOLFD () {16}; # Output Linefeed Disposition
3991 sub TELOPT_XASCII () {17}; # Extended ASCII
3992 sub TELOPT_LOGOUT () {18}; # Logout
3993 sub TELOPT_BM () {19}; # Byte Macro
3994 sub TELOPT_DET () {20}; # Data Entry Terminal
3995 sub TELOPT_SUPDUP () {21}; # SUPDUP
3996 sub TELOPT_SUPDUPOUTPUT () {22}; # SUPDUP Output
3997 sub TELOPT_SNDLOC () {23}; # Send Location
3998 sub TELOPT_TTYPE () {24}; # Terminal Type
3999 sub TELOPT_EOR () {25}; # End of Record
4000 sub TELOPT_TUID () {26}; # TACACS User Identification
4001 sub TELOPT_OUTMRK () {27}; # Output Marking
4002 sub TELOPT_TTYLOC () {28}; # Terminal Location Number
4003 sub TELOPT_3270REGIME () {29}; # Telnet 3270 Regime
4004 sub TELOPT_X3PAD () {30}; # X.3 PAD
4005 sub TELOPT_NAWS () {31}; # Negotiate About Window Size
4006 sub TELOPT_TSPEED () {32}; # Terminal Speed
4007 sub TELOPT_LFLOW () {33}; # Remote Flow Control
4008 sub TELOPT_LINEMODE () {34}; # Linemode
4009 sub TELOPT_XDISPLOC () {35}; # X Display Location
4010 sub TELOPT_OLD_ENVIRON () {36}; # Environment Option
4011 sub TELOPT_AUTHENTICATION () {37}; # Authentication Option
4012 sub TELOPT_ENCRYPT () {38}; # Encryption Option
4013 sub TELOPT_NEW_ENVIRON () {39}; # New Environment Option
4014 sub TELOPT_TN3270E () {40}; # TN3270 Enhancements
4015 sub TELOPT_CHARSET () {42}; # CHARSET Option
4016 sub TELOPT_COMPORT () {44}; # Com Port Control Option
4017 sub TELOPT_KERMIT () {47}; # Kermit Option
4018 sub TELOPT_EXOPL () {255}; # Extended-Options-List
4019
4020
4021 1;
4022 __END__;