1 /* packet-finger.c
2  * Routines for basic finger dissection (see https://tools.ietf.org/html/rfc742)
3  * Copyright 2013, Christopher Maynard <Christopher.Maynard@gtech.com>
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * SPDX-License-Identifier: GPL-2.0-or-later
10  */
11 
12 #include "config.h"
13 #include <epan/packet.h>
14 #include <epan/conversation.h>
15 #include <epan/expert.h>
16 
17 void proto_register_finger(void);
18 void proto_reg_handoff_finger(void);
19 
20 #define FINGER_PORT     79  /* This is the registered IANA port */
21 
22 static int proto_finger = -1;
23 static int hf_finger_query = -1;
24 static int hf_finger_response = -1;
25 static int hf_finger_response_in = -1;
26 static int hf_finger_response_to = -1;
27 static int hf_finger_response_time = -1;
28 
29 static expert_field ei_finger_nocrlf = EI_INIT;
30 
31 static gint ett_finger = -1;
32 
33 typedef struct _finger_transaction_t {
34     guint32  req_frame;
35     guint32  rep_frame;
36     nstime_t req_time;
37 } finger_transaction_t;
38 
39 static int
dissect_finger(tvbuff_t * tvb,packet_info * pinfo,proto_tree * tree,void * data _U_)40 dissect_finger(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
41     void *data _U_)
42 {
43     proto_item           *ti, *expert_ti;
44     proto_tree           *finger_tree;
45     conversation_t       *conversation;
46     finger_transaction_t *finger_trans;
47     gboolean              is_query;
48     guint                 len;
49 
50     col_set_str(pinfo->cinfo, COL_PROTOCOL, "FINGER");
51 
52     if (pinfo->destport == FINGER_PORT) {
53         is_query = TRUE;
54         col_set_str(pinfo->cinfo, COL_INFO, "Query");
55     } else {
56         is_query = FALSE;
57         col_set_str(pinfo->cinfo, COL_INFO, "Response");
58     }
59 
60     conversation = find_or_create_conversation(pinfo);
61     finger_trans = (finger_transaction_t *)conversation_get_proto_data(conversation, proto_finger);
62     if (finger_trans == NULL) {
63         finger_trans = wmem_new0(wmem_file_scope(), finger_transaction_t);
64         conversation_add_proto_data(conversation, proto_finger, finger_trans);
65     }
66 
67     len = tvb_reported_length(tvb);
68     if (!PINFO_FD_VISITED(pinfo)) {
69         if (pinfo->can_desegment) {
70             if (is_query) {
71                 if ((len < 2) || (tvb_memeql(tvb, len - 2, "\r\n", 2))) {
72                     pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
73                     pinfo->desegment_offset = 0;
74                     return -1;
75                 } else {
76                     finger_trans->req_frame = pinfo->num;
77                     finger_trans->req_time = pinfo->abs_ts;
78                 }
79             } else {
80                 pinfo->desegment_len = DESEGMENT_UNTIL_FIN;
81                 pinfo->desegment_offset = 0;
82                 return -1;
83             }
84         }
85     } else if (is_query && (finger_trans->req_frame == 0)) {
86         finger_trans->req_frame = pinfo->num;
87         finger_trans->req_time = pinfo->abs_ts;
88     }
89 
90     if (!is_query && (finger_trans->rep_frame == 0)) {
91         /* By comparing finger_trans->rep_frame to 0, if reassembly is turned
92          * on, finger_trans->rep_frame will be assigned to the reassembled frame
93          * number, and if reassembly is turned off, finger_trans->rep_frame will
94          * be assigned to the first frame number of the response.  This seems
95          * to match other protocols' behavior.  The alternative is:
96          *      if (pinfo->num > finger_trans->rep_frame)
97          * which will give us the same frame number either way.
98          */
99         finger_trans->rep_frame = pinfo->num;
100     }
101 
102     ti = proto_tree_add_protocol_format(tree, proto_finger, tvb, 0, -1,
103         "FINGER: %s", is_query ? "Query" : "Response");
104     finger_tree = proto_item_add_subtree(ti, ett_finger);
105 
106     if (is_query) {
107         expert_ti = proto_tree_add_item(finger_tree, hf_finger_query, tvb, 0, -1, ENC_ASCII|ENC_NA);
108         if ((len < 2) || (tvb_memeql(tvb, len - 2, "\r\n", 2))) {
109             /*
110              * From RFC742, Send a single "command line", ending with <CRLF>.
111              */
112             expert_add_info(pinfo, expert_ti, &ei_finger_nocrlf);
113         }
114         if (tree && finger_trans->rep_frame) {
115             ti = proto_tree_add_uint(finger_tree, hf_finger_response_in,
116                 tvb, 0, 0, finger_trans->rep_frame);
117             proto_item_set_generated(ti);
118         }
119     } else if (tree && finger_trans->rep_frame) {
120         proto_tree_add_item(finger_tree, hf_finger_response, tvb, 0, -1, ENC_ASCII|ENC_NA);
121         if (finger_trans->req_frame) {
122             nstime_t ns;
123 
124             ti = proto_tree_add_uint(finger_tree, hf_finger_response_to,
125                 tvb, 0, 0, finger_trans->req_frame);
126             proto_item_set_generated(ti);
127 
128             if (pinfo->num == finger_trans->rep_frame) {
129                 nstime_delta(&ns, &pinfo->abs_ts, &finger_trans->req_time);
130                 ti = proto_tree_add_time(finger_tree, hf_finger_response_time, tvb, 0, 0, &ns);
131                 proto_item_set_generated(ti);
132             }
133         }
134     }
135 
136     return tvb_captured_length(tvb);
137 }
138 
139 void
proto_register_finger(void)140 proto_register_finger(void)
141 {
142     expert_module_t *expert_finger;
143 
144     static hf_register_info hf[] = {
145         { &hf_finger_query,
146             { "Query", "finger.query", FT_STRING, BASE_NONE, NULL, 0x0,
147               NULL, HFILL }
148         },
149         { &hf_finger_response,
150             { "Response", "finger.response", FT_STRING, BASE_NONE, NULL, 0x0,
151               NULL, HFILL }
152         },
153         { &hf_finger_response_in,
154             { "Response In", "finger.response_in", FT_FRAMENUM, BASE_NONE, NULL,
155               0x0, "The response to this FINGER query is in this frame",
156               HFILL }
157         },
158         { &hf_finger_response_to,
159             { "Request In", "finger.response_to", FT_FRAMENUM, BASE_NONE, NULL,
160               0x0, "This is a response to the FINGER query in this frame",
161               HFILL }
162         },
163         { &hf_finger_response_time,
164             { "Response Time", "finger.response_time", FT_RELATIVE_TIME,
165               BASE_NONE, NULL, 0x0,
166               "The time between the Query and the Response", HFILL }
167         }
168     };
169 
170     static gint *ett[] = {
171         &ett_finger
172     };
173 
174     static ei_register_info ei[] = {
175         { &ei_finger_nocrlf,
176             { "finger.nocrlf", PI_MALFORMED, PI_WARN, "Missing <CR><LF>", EXPFILL}
177         }
178     };
179 
180     proto_finger = proto_register_protocol("finger", "FINGER", "finger");
181     proto_register_field_array(proto_finger, hf, array_length(hf));
182     proto_register_subtree_array(ett, array_length(ett));
183     expert_finger = expert_register_protocol(proto_finger);
184     expert_register_field_array(expert_finger, ei, array_length(ei));
185 }
186 
187 void
proto_reg_handoff_finger(void)188 proto_reg_handoff_finger(void)
189 {
190     static dissector_handle_t finger_handle;
191 
192     finger_handle = create_dissector_handle(dissect_finger, proto_finger);
193     dissector_add_uint_with_preference("tcp.port", FINGER_PORT, finger_handle);
194 }
195 
196 /*
197  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
198  *
199  * Local variables:
200  * c-basic-offset: 4
201  * tab-width: 8
202  * indent-tabs-mode: nil
203  * End:
204  *
205  * vi: set shiftwidth=4 tabstop=8 expandtab:
206  * :indentSize=4:tabSize=8:noTabs=true:
207  */
208 
209