2 * Routines for Telnet packet dissection; see RFC 854 and RFC 855
3 * Copyright 1999, Richard Sharpe <rsharpe@ns.aus.com>
5 * $Id: packet-telnet.c,v 1.41 2003/06/12 08:33:30 guy Exp $
7 * Ethereal - Network traffic analyzer
8 * By Gerald Combs <gerald@ethereal.com>
9 * Copyright 1998 Gerald Combs
11 * Copied from packet-pop.c
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 #include <epan/packet.h>
37 #include <epan/strutil.h>
39 static int proto_telnet = -1;
41 static gint ett_telnet = -1;
42 static gint ett_telnet_subopt = -1;
43 static gint ett_status_subopt = -1;
44 static gint ett_rcte_subopt = -1;
45 static gint ett_olw_subopt = -1;
46 static gint ett_ops_subopt = -1;
47 static gint ett_crdisp_subopt = -1;
48 static gint ett_htstops_subopt = -1;
49 static gint ett_htdisp_subopt = -1;
50 static gint ett_ffdisp_subopt = -1;
51 static gint ett_vtstops_subopt = -1;
52 static gint ett_vtdisp_subopt = -1;
53 static gint ett_lfdisp_subopt = -1;
54 static gint ett_extasc_subopt = -1;
55 static gint ett_bytemacro_subopt = -1;
56 static gint ett_det_subopt = -1;
57 static gint ett_supdupout_subopt = -1;
58 static gint ett_sendloc_subopt = -1;
59 static gint ett_termtype_subopt = -1;
60 static gint ett_tacacsui_subopt = -1;
61 static gint ett_outmark_subopt = -1;
62 static gint ett_tlocnum_subopt = -1;
63 static gint ett_tn3270reg_subopt = -1;
64 static gint ett_x3pad_subopt = -1;
65 static gint ett_naws_subopt = -1;
66 static gint ett_tspeed_subopt = -1;
67 static gint ett_rfc_subopt = -1;
68 static gint ett_linemode_subopt = -1;
69 static gint ett_xdpyloc_subopt = -1;
70 static gint ett_env_subopt = -1;
71 static gint ett_auth_subopt = -1;
72 static gint ett_enc_subopt = -1;
73 static gint ett_newenv_subopt = -1;
74 static gint ett_tn3270e_subopt = -1;
75 static gint ett_xauth_subopt = -1;
76 static gint ett_charset_subopt = -1;
77 static gint ett_rsp_subopt = -1;
78 static gint ett_comport_subopt = -1;
81 /* Some defines for Telnet */
83 #define TCP_PORT_TELNET 23
108 NO_LENGTH, /* option has no data, hence no length */
109 FIXED_LENGTH, /* option always has the same length */
110 VARIABLE_LENGTH /* option is variable-length - optlen is minimum */
113 /* Member of table of IP or TCP options. */
114 typedef struct tn_opt {
115 char *name; /* name of option */
116 gint *subtree_index; /* pointer to subtree index for option */
117 tn_opt_len_type len_type; /* type of option length field */
118 int optlen; /* value length should be (minimum if VARIABLE) */
119 void (*dissect)(const char *, tvbuff_t *, int, int, proto_tree *);
120 /* routine to dissect option */
124 dissect_string_subopt(const char *optname, tvbuff_t *tvb, int offset, int len,
129 cmd = tvb_get_guint8(tvb, offset);
133 proto_tree_add_text(tree, tvb, offset, 1, "Here's my %s", optname);
137 proto_tree_add_text(tree, tvb, offset, len, "Value: %s",
138 tvb_format_text(tvb, offset, len));
143 proto_tree_add_text(tree, tvb, offset, 1, "Send your %s", optname);
147 proto_tree_add_text(tree, tvb, offset, len, "Extra data");
151 proto_tree_add_text(tree, tvb, offset, 1, "Invalid %s subcommand %u",
156 proto_tree_add_text(tree, tvb, offset, len, "Subcommand data");
162 dissect_outmark_subopt(const char *optname _U_, tvbuff_t *tvb, int offset,
163 int len, proto_tree *tree)
166 int gs_offset, datalen;
169 cmd = tvb_get_guint8(tvb, offset);
173 proto_tree_add_text(tree, tvb, offset, 1, "ACK");
177 proto_tree_add_text(tree, tvb, offset, 1, "NAK");
181 proto_tree_add_text(tree, tvb, offset, 1, "Default");
185 proto_tree_add_text(tree, tvb, offset, 1, "Top");
189 proto_tree_add_text(tree, tvb, offset, 1, "Bottom");
193 proto_tree_add_text(tree, tvb, offset, 1, "Left");
197 proto_tree_add_text(tree, tvb, offset, 1, "Right");
201 proto_tree_add_text(tree, tvb, offset, 1, "Bogus value: %u", cmd);
208 gs_offset = tvb_find_guint8(tvb, offset, len, 29);
209 if (gs_offset == -1) {
210 /* None found - run to the end of the packet. */
211 gs_offset = offset + len;
213 datalen = gs_offset - offset;
215 proto_tree_add_text(tree, tvb, offset, datalen, "Banner: %s",
216 tvb_format_text(tvb, offset, datalen));
224 dissect_htstops_subopt(const char *optname, tvbuff_t *tvb, int offset, int len,
230 cmd = tvb_get_guint8(tvb, offset);
234 proto_tree_add_text(tree, tvb, offset, 1, "Here's my %s", optname);
240 proto_tree_add_text(tree, tvb, offset, 1, "Send your %s", optname);
246 proto_tree_add_text(tree, tvb, offset, 1, "Invalid %s subcommand %u",
251 proto_tree_add_text(tree, tvb, offset, len, "Subcommand data");
256 tabval = tvb_get_guint8(tvb, offset);
260 proto_tree_add_text(tree, tvb, offset, 1,
261 "Sender wants to handle tab stops");
265 proto_tree_add_text(tree, tvb, offset, 1,
266 "Sender wants receiver to handle tab stop at %u",
274 proto_tree_add_text(tree, tvb, offset, 1,
275 "Invalid value: %u", tabval);
279 proto_tree_add_text(tree, tvb, offset, 1,
280 "Sender wants receiver to handle tab stops");
289 dissect_naws_subopt(const char *optname _U_, tvbuff_t *tvb, int offset,
290 int len _U_, proto_tree *tree)
292 proto_tree_add_text(tree, tvb, offset, 2, "Width: %u",
293 tvb_get_ntohs(tvb, offset));
295 proto_tree_add_text(tree, tvb, offset, 2, "Height: %u",
296 tvb_get_ntohs(tvb, offset));
299 /* BEGIN RFC-2217 (COM Port Control) Definitions */
301 #define TNCOMPORT_SIGNATURE 0
302 #define TNCOMPORT_SETBAUDRATE 1
303 #define TNCOMPORT_SETDATASIZE 2
304 #define TNCOMPORT_SETPARITY 3
305 #define TNCOMPORT_SETSTOPSIZE 4
306 #define TNCOMPORT_SETCONTROL 5
307 #define TNCOMPORT_NOTIFYLINESTATE 6
308 #define TNCOMPORT_NOTIFYMODEMSTATE 7
309 #define TNCOMPORT_FLOWCONTROLSUSPEND 8
310 #define TNCOMPORT_FLOWCONTROLRESUME 9
311 #define TNCOMPORT_SETLINESTATEMASK 10
312 #define TNCOMPORT_SETMODEMSTATEMASK 11
313 #define TNCOMPORT_PURGEDATA 12
315 /* END RFC-2217 (COM Port Control) Definitions */
318 dissect_comport_subopt(const char *optname, tvbuff_t *tvb, int offset, int len,
320 {static const char *datasizes[] = {
331 static const char *parities[] = {
339 static const char *stops[] = {
345 static const char *control[] = {
346 "Output Flow Control Request",
348 "Output Flow: XON/XOFF",
349 "Output Flow: CTS/RTS",
359 "Input Flow Control Request",
361 "Input Flow: XON/XOFF",
362 "Input Flow: CTS/RTS",
367 static const char *linestate_bits[] = {
373 "Transfer Holding Register Empty",
374 "Transfer Shift Register Empty",
377 static const char *modemstate_bits[] = {
387 static const char *purges[] = {
398 cmd = tvb_get_guint8(tvb, offset);
399 isservercmd = cmd > 99;
400 cmd = (isservercmd) ? (cmd - 100) : cmd;
401 source = (isservercmd) ? "Server" : "Client";
404 case TNCOMPORT_SIGNATURE:
407 proto_tree_add_text(tree, tvb, offset, 1, "%s Requests Signature",source);
409 guint8 *sig = tvb_get_string(tvb, offset + 1, len);
410 proto_tree_add_text(tree, tvb, offset, 1 + len, "%s Signature: %s",source, sig);
415 case TNCOMPORT_SETBAUDRATE:
418 guint32 baud = tvb_get_ntohl(tvb, offset+1);
420 proto_tree_add_text(tree, tvb, offset, 5, "%s Requests Baud Rate",source);
422 proto_tree_add_text(tree, tvb, offset, 5, "%s Baud Rate: %d",source,baud);
425 proto_tree_add_text(tree, tvb, offset, 1 + len, "%s <Invalid Baud Rate Packet>",source);
429 case TNCOMPORT_SETDATASIZE:
432 guint8 datasize = tvb_get_guint8(tvb, offset+1);
433 const char *ds = (datasize > 8) ? "<invalid>" : datasizes[datasize];
434 proto_tree_add_text(tree, tvb, offset, 2, "%s Data Size: %s",source,ds);
436 proto_tree_add_text(tree, tvb, offset, 1 + len, "%s <Invalid Data Size Packet>",source);
440 case TNCOMPORT_SETPARITY:
443 guint8 parity = tvb_get_guint8(tvb, offset+1);
444 const char *pr = (parity > 5) ? "<invalid>" : parities[parity];
445 proto_tree_add_text(tree, tvb, offset, 2, "%s Parity: %s",source,pr);
447 proto_tree_add_text(tree, tvb, offset, 1 + len, "%s <Invalid Parity Packet>",source);
451 case TNCOMPORT_SETSTOPSIZE:
454 guint8 stop = tvb_get_guint8(tvb, offset+1);
455 const char *st = (stop > 3) ? "<invalid>" : stops[stop];
456 proto_tree_add_text(tree, tvb, offset, 2, "%s Stop: %s",source,st);
458 proto_tree_add_text(tree, tvb, offset, 1 + len, "%s <Invalid Stop Packet>",source);
462 case TNCOMPORT_SETCONTROL:
465 guint8 crt = tvb_get_guint8(tvb, offset+1);
466 const char *c = (crt > 19) ? "Control: <invalid>" : control[crt];
467 proto_tree_add_text(tree, tvb, offset, 2, "%s %s",source,c);
469 proto_tree_add_text(tree, tvb, offset, 1 + len, "%s <Invalid Control Packet>",source);
473 case TNCOMPORT_SETLINESTATEMASK:
474 case TNCOMPORT_NOTIFYLINESTATE:
477 const char *print_pattern = (cmd == TNCOMPORT_SETLINESTATEMASK) ?
478 "%s Set Linestate Mask: %s" : "%s Linestate: %s";
480 guint8 ls = tvb_get_guint8(tvb, offset+1);
484 for (idx = 0; idx < 8; idx++) {
487 if (print_count != 0) {
488 strcat(ls_buffer,", ");
490 strcat(ls_buffer,linestate_bits[idx]);
495 proto_tree_add_text(tree, tvb, offset, 2, print_pattern, source, ls_buffer);
497 const char *print_pattern = (cmd == TNCOMPORT_SETLINESTATEMASK) ?
498 "%s <Invalid Linestate Mask>" : "%s <Invalid Linestate Packet>";
499 proto_tree_add_text(tree, tvb, offset, 1 + len, print_pattern, source);
503 case TNCOMPORT_SETMODEMSTATEMASK:
504 case TNCOMPORT_NOTIFYMODEMSTATE:
507 const char *print_pattern = (cmd == TNCOMPORT_SETMODEMSTATEMASK) ?
508 "%s Set Modemstate Mask: %s" : "%s Modemstate: %s";
510 guint8 ms = tvb_get_guint8(tvb, offset+1);
514 for (idx = 0; idx < 8; idx++) {
517 if (print_count != 0) {
518 strcat(ms_buffer,", ");
520 strcat(ms_buffer,modemstate_bits[idx]);
525 proto_tree_add_text(tree, tvb, offset, 2, print_pattern, source, ms_buffer);
527 const char *print_pattern = (cmd == TNCOMPORT_SETMODEMSTATEMASK) ?
528 "%s <Invalid Modemstate Mask>" : "%s <Invalid Modemstate Packet>";
529 proto_tree_add_text(tree, tvb, offset, 1 + len, print_pattern, source);
533 case TNCOMPORT_FLOWCONTROLSUSPEND:
535 proto_tree_add_text(tree, tvb, offset, 1, "%s Flow Control Suspend",source);
538 case TNCOMPORT_FLOWCONTROLRESUME:
540 proto_tree_add_text(tree, tvb, offset, 1, "%s Flow Control Resume",source);
543 case TNCOMPORT_PURGEDATA:
546 guint8 purge = tvb_get_guint8(tvb, offset+1);
547 const char *p = (purge > 3) ? "<Purge invalid>" : purges[purge];
548 proto_tree_add_text(tree, tvb, offset, 2, "%s %s",source,p);
550 proto_tree_add_text(tree, tvb, offset, 1 + len, "%s <Invalid Purge Packet>",source);
555 proto_tree_add_text(tree, tvb, offset, 1, "Invalid %s subcommand %u",
560 proto_tree_add_text(tree, tvb, offset, len, "Subcommand data");
566 static const value_string rfc_opt_vals[] = {
569 { 2, "RESTART-ANY" },
570 { 3, "RESTART-XON" },
575 dissect_rfc_subopt(const char *optname _U_, tvbuff_t *tvb, int offset,
576 int len _U_, proto_tree *tree)
580 cmd = tvb_get_guint8(tvb, offset);
581 proto_tree_add_text(tree, tvb, offset, 2, "%s",
582 val_to_str(cmd, rfc_opt_vals, "Unknown (%u)"));
585 static tn_opt options[] = {
587 "Binary Transmission", /* RFC 856 */
588 NULL, /* no suboption negotiation */
594 "Echo", /* RFC 857 */
595 NULL, /* no suboption negotiation */
601 "Reconnection", /* DOD Protocol Handbook */
608 "Suppress Go Ahead", /* RFC 858 */
609 NULL, /* no suboption negotiation */
615 "Approx Message Size Negotiation", /* Ethernet spec(!) */
622 "Status", /* RFC 859 */
626 NULL /* XXX - fill me in */
629 "Timing Mark", /* RFC 860 */
630 NULL, /* no suboption negotiation */
636 "Remote Controlled Trans and Echo", /* RFC 726 */
640 NULL /* XXX - fill me in */
643 "Output Line Width", /* DOD Protocol Handbook */
645 VARIABLE_LENGTH, /* XXX - fill me in */
646 0, /* XXX - fill me in */
647 NULL /* XXX - fill me in */
650 "Output Page Size", /* DOD Protocol Handbook */
652 VARIABLE_LENGTH, /* XXX - fill me in */
653 0, /* XXX - fill me in */
654 NULL /* XXX - fill me in */
657 "Output Carriage-Return Disposition", /* RFC 652 */
661 NULL /* XXX - fill me in */
664 "Output Horizontal Tab Stops", /* RFC 653 */
668 dissect_htstops_subopt
671 "Output Horizontal Tab Disposition", /* RFC 654 */
675 NULL /* XXX - fill me in */
678 "Output Formfeed Disposition", /* RFC 655 */
682 NULL /* XXX - fill me in */
685 "Output Vertical Tabstops", /* RFC 656 */
689 NULL /* XXX - fill me in */
692 "Output Vertical Tab Disposition", /* RFC 657 */
696 NULL /* XXX - fill me in */
699 "Output Linefeed Disposition", /* RFC 658 */
703 NULL /* XXX - fill me in */
706 "Extended ASCII", /* RFC 698 */
710 NULL /* XXX - fill me in */
713 "Logout", /* RFC 727 */
714 NULL, /* no suboption negotiation */
720 "Byte Macro", /* RFC 735 */
721 &ett_bytemacro_subopt,
724 NULL /* XXX - fill me in */
727 "Data Entry Terminal", /* RFC 732, RFC 1043 */
731 NULL /* XXX - fill me in */
734 "SUPDUP", /* RFC 734, RFC 736 */
735 NULL, /* no suboption negotiation */
741 "SUPDUP Output", /* RFC 749 */
742 &ett_supdupout_subopt,
745 NULL /* XXX - fill me in */
748 "Send Location", /* RFC 779 */
752 NULL /* XXX - fill me in */
755 "Terminal Type", /* RFC 1091 */
756 &ett_termtype_subopt,
759 dissect_string_subopt
762 "End of Record", /* RFC 885 */
763 NULL, /* no suboption negotiation */
769 "TACACS User Identification", /* RFC 927 */
770 &ett_tacacsui_subopt,
773 NULL /* XXX - fill me in */
776 "Output Marking", /* RFC 933 */
780 dissect_outmark_subopt,
783 "Terminal Location Number", /* RFC 946 */
787 NULL /* XXX - fill me in */
790 "Telnet 3270 Regime", /* RFC 1041 */
791 &ett_tn3270reg_subopt,
794 NULL /* XXX - fill me in */
797 "X.3 PAD", /* RFC 1053 */
801 NULL /* XXX - fill me in */
804 "Negotiate About Window Size", /* RFC 1073, DW183 */
811 "Terminal Speed", /* RFC 1079 */
815 NULL /* XXX - fill me in */
818 "Remote Flow Control", /* RFC 1372 */
825 "Linemode", /* RFC 1184 */
826 &ett_linemode_subopt,
829 NULL /* XXX - fill me in */
832 "X Display Location", /* RFC 1096 */
836 dissect_string_subopt
839 "Environment Option", /* RFC 1408, RFC 1571 */
843 NULL /* XXX - fill me in */
846 "Authentication Option", /* RFC 2941 */
850 NULL /* XXX - fill me in */
853 "Encryption Option", /* RFC 2946 */
857 NULL /* XXX - fill me in */
860 "New Environment Option", /* RFC 1572 */
864 NULL /* XXX - fill me in */
867 "TN3270E", /* RFC 1647 */
871 NULL /* XXX - fill me in */
878 NULL /* XXX - fill me in */
881 "CHARSET", /* CHARSET */
885 NULL /* XXX - fill me in */
888 "Remote Serial Port", /* Remote Serial Port */
892 NULL /* XXX - fill me in */
895 "COM Port Control", /* RFC 2217 */
899 dissect_comport_subopt
904 #define NOPTIONS (sizeof options / sizeof options[0])
907 telnet_sub_option(proto_tree *telnet_tree, tvbuff_t *tvb, int start_offset)
909 proto_tree *ti, *option_tree;
910 int offset = start_offset;
917 void (*dissect)(const char *, tvbuff_t *, int, int, proto_tree *);
921 offset += 2; /* skip IAC and SB */
923 /* Get the option code */
924 opt_byte = tvb_get_guint8(tvb, offset);
925 if (opt_byte > NOPTIONS) {
926 opt = "<unknown option>";
927 ett = ett_telnet_subopt;
930 opt = options[opt_byte].name;
931 if (options[opt_byte].subtree_index != NULL)
932 ett = *(options[opt_byte].subtree_index);
934 ett = ett_telnet_subopt;
935 dissect = options[opt_byte].dissect;
939 /* Search for an unescaped IAC. */
942 len = tvb_length_remaining(tvb, offset);
944 iac_offset = tvb_find_guint8(tvb, cur_offset, len, TN_IAC);
946 if (iac_offset == -1) {
947 /* None found - run to the end of the packet. */
950 if (((guint)(iac_offset + 1) >= len) ||
951 (tvb_get_guint8(tvb, iac_offset + 1) != TN_IAC)) {
952 /* We really found a single IAC, so we're done */
956 * We saw an escaped IAC, so we have to move ahead to the
960 cur_offset = iac_offset + 2;
964 } while (!iac_found);
966 subneg_len = offset - start_offset;
968 ti = proto_tree_add_text(telnet_tree, tvb, start_offset, subneg_len,
969 "Suboption Begin: %s", opt);
970 option_tree = proto_item_add_subtree(ti, ett);
971 start_offset += 3; /* skip IAC, SB, and option code */
974 if (subneg_len > 0) {
975 switch (options[opt_byte].len_type) {
978 /* There isn't supposed to *be* sub-option negotiation for this. */
979 proto_tree_add_text(option_tree, tvb, start_offset, subneg_len,
980 "Bogus suboption data");
984 /* Make sure the length is what it's supposed to be. */
985 if (subneg_len != options[opt_byte].optlen) {
986 proto_tree_add_text(option_tree, tvb, start_offset, subneg_len,
987 "Suboption parameter length is %d, should be %d",
988 subneg_len, options[opt_byte].optlen);
993 case VARIABLE_LENGTH:
994 /* Make sure the length is greater than the minimum. */
995 if (subneg_len < options[opt_byte].optlen) {
996 proto_tree_add_text(option_tree, tvb, start_offset, subneg_len,
997 "Suboption parameter length is %d, should be at least %d",
998 subneg_len, options[opt_byte].optlen);
1004 /* Now dissect the suboption parameters. */
1005 if (dissect != NULL) {
1006 /* We have a dissector for this suboption's parameters; call it. */
1007 (*dissect)(opt, tvb, start_offset, subneg_len, option_tree);
1009 /* We don't have a dissector for them; just show them as data. */
1010 proto_tree_add_text(option_tree, tvb, start_offset, subneg_len,
1018 telnet_will_wont_do_dont(proto_tree *telnet_tree, tvbuff_t *tvb,
1019 int start_offset, char *type)
1021 int offset = start_offset;
1025 offset += 2; /* skip IAC and WILL,WONT,DO,DONT} */
1026 opt_byte = tvb_get_guint8(tvb, offset);
1027 if (opt_byte > NOPTIONS)
1028 opt = "<unknown option>";
1030 opt = options[opt_byte].name;
1033 proto_tree_add_text(telnet_tree, tvb, start_offset, 3,
1034 "Command: %s %s", type, opt);
1039 telnet_command(proto_tree *telnet_tree, tvbuff_t *tvb, int start_offset)
1041 int offset = start_offset;
1044 offset += 1; /* skip IAC */
1045 optcode = tvb_get_guint8(tvb, offset);
1050 proto_tree_add_text(telnet_tree, tvb, start_offset, 2,
1051 "Command: End of File");
1055 proto_tree_add_text(telnet_tree, tvb, start_offset, 2,
1056 "Command: Suspend Current Process");
1060 proto_tree_add_text(telnet_tree, tvb, start_offset, 2,
1061 "Command: Abort Process");
1065 proto_tree_add_text(telnet_tree, tvb, start_offset, 2,
1066 "Command: End of Record");
1070 proto_tree_add_text(telnet_tree, tvb, start_offset, 2,
1071 "Command: Suboption End");
1075 proto_tree_add_text(telnet_tree, tvb, start_offset, 2,
1076 "Command: No Operation");
1080 proto_tree_add_text(telnet_tree, tvb, start_offset, 2,
1081 "Command: Data Mark");
1085 proto_tree_add_text(telnet_tree, tvb, start_offset, 2,
1090 proto_tree_add_text(telnet_tree, tvb, start_offset, 2,
1091 "Command: Interrupt Process");
1095 proto_tree_add_text(telnet_tree, tvb, start_offset, 2,
1096 "Command: Abort Output");
1100 proto_tree_add_text(telnet_tree, tvb, start_offset, 2,
1101 "Command: Are You There?");
1105 proto_tree_add_text(telnet_tree, tvb, start_offset, 2,
1106 "Command: Escape Character");
1110 proto_tree_add_text(telnet_tree, tvb, start_offset, 2,
1111 "Command: Erase Line");
1115 proto_tree_add_text(telnet_tree, tvb, start_offset, 2,
1116 "Command: Go Ahead");
1120 offset = telnet_sub_option(telnet_tree, tvb, start_offset);
1124 offset = telnet_will_wont_do_dont(telnet_tree, tvb, start_offset,
1129 offset = telnet_will_wont_do_dont(telnet_tree, tvb, start_offset,
1134 offset = telnet_will_wont_do_dont(telnet_tree, tvb, start_offset,
1139 offset = telnet_will_wont_do_dont(telnet_tree, tvb, start_offset,
1144 proto_tree_add_text(telnet_tree, tvb, start_offset, 2,
1145 "Command: Unknown (0x%02x)", optcode);
1153 telnet_add_text(proto_tree *tree, tvbuff_t *tvb, int offset, int len)
1158 gboolean last_char_was_cr;
1160 while (len != 0 && tvb_offset_exists(tvb, offset)) {
1162 * Find the end of the line.
1164 linelen = tvb_find_line_end(tvb, offset, len, &next_offset, FALSE);
1165 len -= next_offset - offset; /* subtract out the line's characters */
1168 * In Telnet, CR NUL is the way you send a CR by itself in the
1169 * default ASCII mode; don't treat CR by itself as a line ending,
1170 * treat only CR NUL, CR LF, or LF by itself as a line ending.
1172 if (next_offset == offset + linelen + 1 && len >= 1) {
1174 * Well, we saw a one-character line ending, so either it's a CR
1175 * or an LF; we have at least two characters left, including the
1178 * If the line ending is a CR, skip all subsequent CRs; at
1179 * least one capture appeared to have multiple CRs at the end of
1182 if (tvb_get_guint8(tvb, offset + linelen) == '\r') {
1183 last_char_was_cr = TRUE;
1184 while (len != 0 && tvb_offset_exists(tvb, next_offset)) {
1185 c = tvb_get_guint8(tvb, next_offset);
1186 next_offset++; /* skip over that character */
1188 if (c == '\n' || (c == '\0' && last_char_was_cr)) {
1190 * LF is a line ending, whether preceded by CR or not.
1191 * NUL is a line ending if preceded by CR.
1195 last_char_was_cr = (c == '\r');
1201 * Now compute the length of the line *including* the end-of-line
1202 * indication, if any; we display it all.
1204 linelen = next_offset - offset;
1206 proto_tree_add_text(tree, tvb, offset, linelen,
1208 tvb_format_text(tvb, offset, linelen));
1209 offset = next_offset;
1214 dissect_telnet(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
1216 proto_tree *telnet_tree, *ti;
1218 if (check_col(pinfo->cinfo, COL_PROTOCOL))
1219 col_set_str(pinfo->cinfo, COL_PROTOCOL, "TELNET");
1221 if (check_col(pinfo->cinfo, COL_INFO))
1222 col_add_fstr(pinfo->cinfo, COL_INFO, "Telnet Data ...");
1230 ti = proto_tree_add_item(tree, proto_telnet, tvb, offset, -1, FALSE);
1231 telnet_tree = proto_item_add_subtree(ti, ett_telnet);
1234 * Scan through the buffer looking for an IAC byte.
1236 while ((len = tvb_length_remaining(tvb, offset)) > 0) {
1237 iac_offset = tvb_find_guint8(tvb, offset, len, TN_IAC);
1238 if (iac_offset != -1) {
1240 * We found an IAC byte.
1241 * If there's any data before it, add that data to the
1242 * tree, a line at a time.
1244 data_len = iac_offset - offset;
1246 telnet_add_text(telnet_tree, tvb, offset, data_len);
1249 * Now interpret the command.
1251 offset = telnet_command(telnet_tree, tvb, iac_offset);
1255 * We found no IAC byte, so what remains in the buffer
1256 * is the last of the data in the packet.
1257 * Add it to the tree, a line at a time, and then quit.
1259 telnet_add_text(telnet_tree, tvb, offset, len);
1267 proto_register_telnet(void)
1269 /* static hf_register_info hf[] = {
1271 { "Name", "telnet.abbreviation", TYPE, VALS_POINTER }},
1273 static gint *ett[] = {
1281 &ett_htstops_subopt,
1284 &ett_vtstops_subopt,
1288 &ett_bytemacro_subopt,
1290 &ett_supdupout_subopt,
1291 &ett_sendloc_subopt,
1292 &ett_termtype_subopt,
1293 &ett_tacacsui_subopt,
1294 &ett_outmark_subopt,
1295 &ett_tlocnum_subopt,
1296 &ett_tn3270reg_subopt,
1301 &ett_linemode_subopt,
1302 &ett_xdpyloc_subopt,
1307 &ett_tn3270e_subopt,
1309 &ett_charset_subopt,
1311 &ett_comport_subopt,
1314 proto_telnet = proto_register_protocol("Telnet", "TELNET", "telnet");
1315 /* proto_register_field_array(proto_telnet, hf, array_length(hf));*/
1316 proto_register_subtree_array(ett, array_length(ett));
1320 proto_reg_handoff_telnet(void)
1322 dissector_handle_t telnet_handle;
1324 telnet_handle = create_dissector_handle(dissect_telnet, proto_telnet);
1325 dissector_add("tcp.port", TCP_PORT_TELNET, telnet_handle);