TODO netlogon_user_flags_ntlmv2_enabled
[wireshark-sm.git] / ui / tap-tcp-stream.c
blobde23f93f933d472b820e555a5d806fa39b45d834
1 /* tap-tcp-stream.c
2 * TCP stream statistics
3 * Originally from tcp_graph.c by Pavel Mores <pvl@uh.cz>
4 * Win32 port: rwh@unifiedtech.com
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later
13 #include "config.h"
16 #include <stdlib.h>
18 #include <file.h>
20 #include <epan/epan_dissect.h>
21 #include <epan/packet.h>
22 #include <epan/tap.h>
24 #include <epan/dissectors/packet-tcp.h>
26 #include "ui/simple_dialog.h"
28 #include "tap-tcp-stream.h"
30 typedef struct _tcp_scan_t {
31 int direction;
32 struct tcp_graph *tg;
33 struct segment *last;
34 } tcp_scan_t;
37 static tap_packet_status
38 tapall_tcpip_packet(void *pct, packet_info *pinfo, epan_dissect_t *edt _U_, const void *vip, tap_flags_t flags _U_)
40 tcp_scan_t *ts = (tcp_scan_t *)pct;
41 struct tcp_graph *tg = ts->tg;
42 const struct tcpheader *tcphdr = (const struct tcpheader *)vip;
44 if (tg->stream == tcphdr->th_stream
45 && (tg->src_address.type == AT_NONE || tg->dst_address.type == AT_NONE)) {
47 * We only know the stream number. Fill in our connection data.
48 * We assume that the server response is more interesting.
50 bool server_is_src;
51 if (tcphdr->th_flags & TH_SYN) {
52 if (tcphdr->th_flags & TH_ACK) {
53 /* SYN-ACK packet, so the server is the source. */
54 server_is_src = true;
55 } else {
56 /* SYN packet, so the server is the destination. */
57 server_is_src = false;
59 } else {
60 /* Fallback to assuming the lower numbered port is the server. */
61 server_is_src = tcphdr->th_sport < tcphdr->th_dport;
63 if (server_is_src) {
64 copy_address(&tg->src_address, &tcphdr->ip_src);
65 tg->src_port = tcphdr->th_sport;
66 copy_address(&tg->dst_address, &tcphdr->ip_dst);
67 tg->dst_port = tcphdr->th_dport;
68 } else {
69 copy_address(&tg->src_address, &tcphdr->ip_dst);
70 tg->src_port = tcphdr->th_dport;
71 copy_address(&tg->dst_address, &tcphdr->ip_src);
72 tg->dst_port = tcphdr->th_sport;
76 if (compare_headers(&tg->src_address, &tg->dst_address,
77 tg->src_port, tg->dst_port,
78 &tcphdr->ip_src, &tcphdr->ip_dst,
79 tcphdr->th_sport, tcphdr->th_dport,
80 ts->direction)
81 && tg->stream == tcphdr->th_stream)
83 struct segment *segment = g_new(struct segment, 1);
84 segment->next = NULL;
85 segment->num = pinfo->num;
86 segment->rel_secs = (uint32_t)pinfo->rel_ts.secs;
87 segment->rel_usecs = pinfo->rel_ts.nsecs/1000;
88 /* Currently unused
89 segment->abs_secs = pinfo->abs_ts.secs;
90 segment->abs_usecs = pinfo->abs_ts.nsecs/1000;
92 /* tcphdr->th_rawseq is always the absolute sequence number.
93 * tcphdr->th_seq is either the relative or absolute sequence number
94 * depending on the TCP dissector preferences.
95 * The sack entries are also either the relative or absolute sequence
96 * number depending on the TCP dissector preferences.
97 * The TCP stream graphs have their own action / button press to
98 * switch between relative and absolute sequence numbers on the fly;
99 * if the TCP dissector hasn't calculated the relative sequence numbers,
100 * the tap will do so. (XXX - The calculation is cheap enough that we
101 * could do it here and store the offsets at the graph level to save
102 * memory. The TCP dissector could include its calculated base seq in
103 * the tap information to ensure consistency.)
105 segment->th_seq = tcphdr->th_seq;
106 segment->th_ack = tcphdr->th_ack;
107 segment->th_rawseq = tcphdr->th_rawseq;
108 segment->th_rawack = tcphdr->th_rawack;
109 segment->th_win = tcphdr->th_win;
110 segment->th_flags = tcphdr->th_flags;
111 segment->th_sport = tcphdr->th_sport;
112 segment->th_dport = tcphdr->th_dport;
113 segment->th_seglen = tcphdr->th_seglen;
114 copy_address(&segment->ip_src, &tcphdr->ip_src);
115 copy_address(&segment->ip_dst, &tcphdr->ip_dst);
117 segment->ack_karn=tcphdr->flagkarn;
119 segment->num_sack_ranges = MIN(MAX_TCP_SACK_RANGES, tcphdr->num_sack_ranges);
120 if (segment->num_sack_ranges > 0) {
121 /* Copy entries in the order they happen */
122 memcpy(&segment->sack_left_edge, &tcphdr->sack_left_edge, sizeof(segment->sack_left_edge));
123 memcpy(&segment->sack_right_edge, &tcphdr->sack_right_edge, sizeof(segment->sack_right_edge));
126 if (ts->tg->segments) {
127 ts->last->next = segment;
128 } else {
129 ts->tg->segments = segment;
131 ts->last = segment;
134 return TAP_PACKET_DONT_REDRAW;
137 /* here we collect all the external data we will ever need */
138 void
139 graph_segment_list_get(capture_file *cf, struct tcp_graph *tg)
141 GString *error_string;
142 tcp_scan_t ts;
144 if (!cf || !tg) {
145 return;
148 /* rescan all the packets and pick up all interesting tcp headers.
149 * we only filter for TCP here for speed and do the actual compare
150 * in the tap listener
152 ts.direction = COMPARE_ANY_DIR;
153 ts.tg = tg;
154 ts.last = NULL;
155 error_string = register_tap_listener("tcp", &ts, "tcp", 0, NULL, tapall_tcpip_packet, NULL, NULL);
156 if (error_string) {
157 fprintf(stderr, "wireshark: Couldn't register tcp_graph tap: %s\n",
158 error_string->str);
159 g_string_free(error_string, TRUE);
160 exit(1); /* XXX: fix this */
162 cf_retap_packets(cf);
163 remove_tap_listener(&ts);
166 void
167 graph_segment_list_free(struct tcp_graph *tg)
169 struct segment *segment;
171 free_address(&tg->src_address);
172 free_address(&tg->dst_address);
174 while (tg->segments) {
175 segment = tg->segments->next;
176 free_address(&tg->segments->ip_src);
177 free_address(&tg->segments->ip_dst);
178 g_free(tg->segments);
179 tg->segments = segment;
184 compare_headers(address *saddr1, address *daddr1, uint16_t sport1, uint16_t dport1, const address *saddr2, const address *daddr2, uint16_t sport2, uint16_t dport2, int dir)
186 int dir1, dir2;
188 dir1 = ((!(cmp_address(saddr1, saddr2))) &&
189 (!(cmp_address(daddr1, daddr2))) &&
190 (sport1==sport2) &&
191 (dport1==dport2));
193 if (dir == COMPARE_CURR_DIR) {
194 return dir1;
195 } else {
196 dir2 = ((!(cmp_address(saddr1, daddr2))) &&
197 (!(cmp_address(daddr1, saddr2))) &&
198 (sport1 == dport2) &&
199 (dport1 == sport2));
200 return dir1 || dir2;
205 get_num_dsegs(struct tcp_graph *tg)
207 int count;
208 struct segment *tmp;
210 for (tmp=tg->segments, count=0; tmp; tmp=tmp->next) {
211 if (compare_headers(&tg->src_address, &tg->dst_address,
212 tg->src_port, tg->dst_port,
213 &tmp->ip_src, &tmp->ip_dst,
214 tmp->th_sport, tmp->th_dport,
215 COMPARE_CURR_DIR)) {
216 count++;
219 return count;
223 get_num_acks(struct tcp_graph *tg, int *num_sack_ranges)
225 int count;
226 struct segment *tmp;
228 for (tmp = tg->segments, count=0; tmp; tmp = tmp->next) {
229 if (!compare_headers(&tg->src_address, &tg->dst_address,
230 tg->src_port, tg->dst_port,
231 &tmp->ip_src, &tmp->ip_dst,
232 tmp->th_sport, tmp->th_dport,
233 COMPARE_CURR_DIR)) {
234 count++;
235 *num_sack_ranges += tmp->num_sack_ranges;
238 return count;
241 typedef struct _th_t {
242 int num_hdrs;
243 #define MAX_SUPPORTED_TCP_HEADERS 8
244 struct tcpheader *tcphdrs[MAX_SUPPORTED_TCP_HEADERS];
245 } th_t;
247 static tap_packet_status
248 tap_tcpip_packet(void *pct, packet_info *pinfo _U_, epan_dissect_t *edt _U_, const void *vip, tap_flags_t flags _U_)
250 int n;
251 bool is_unique = true;
252 th_t *th = (th_t *)pct;
253 const struct tcpheader *header = (const struct tcpheader *)vip;
255 /* Check new header details against any/all stored ones */
256 for (n=0; n < th->num_hdrs; n++) {
257 struct tcpheader *stored = th->tcphdrs[n];
259 if (compare_headers(&stored->ip_src, &stored->ip_dst,
260 stored->th_sport, stored->th_dport,
261 &header->ip_src, &header->ip_dst,
262 header->th_sport, stored->th_dport,
263 COMPARE_CURR_DIR)) {
264 is_unique = false;
265 break;
269 /* Add address if unique and have space for it */
270 if (is_unique && (th->num_hdrs < MAX_SUPPORTED_TCP_HEADERS)) {
271 /* Need to take a deep copy of the tap struct, it may not be valid
272 to read after this function returns? */
273 th->tcphdrs[th->num_hdrs] = g_new(struct tcpheader, 1);
274 *(th->tcphdrs[th->num_hdrs]) = *header;
275 copy_address(&th->tcphdrs[th->num_hdrs]->ip_src, &header->ip_src);
276 copy_address(&th->tcphdrs[th->num_hdrs]->ip_dst, &header->ip_dst);
278 th->num_hdrs++;
281 return TAP_PACKET_DONT_REDRAW;
284 /* XXX should be enhanced so that if we have multiple TCP layers in the trace
285 * then present the user with a dialog where the user can select WHICH tcp
286 * session to graph.
288 uint32_t
289 select_tcpip_session(capture_file *cf)
291 frame_data *fdata;
292 epan_dissect_t edt;
293 dfilter_t *sfcode;
294 uint32_t th_stream;
295 df_error_t *df_err;
296 GString *error_string;
297 th_t th = {0, {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}};
299 if (!cf) {
300 return UINT32_MAX;
303 /* no real filter yet */
304 if (!dfilter_compile("tcp", &sfcode, &df_err)) {
305 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", df_err->msg);
306 df_error_free(&df_err);
307 return UINT32_MAX;
310 /* dissect the current record */
311 if (!cf_read_current_record(cf)) {
312 return UINT32_MAX; /* error reading the record */
315 fdata = cf->current_frame;
317 error_string = register_tap_listener("tcp", &th, NULL, 0, NULL, tap_tcpip_packet, NULL, NULL);
318 if (error_string) {
319 fprintf(stderr, "wireshark: Couldn't register tcp_graph tap: %s\n",
320 error_string->str);
321 g_string_free(error_string, TRUE);
322 exit(1);
325 epan_dissect_init(&edt, cf->epan, true, false);
326 epan_dissect_prime_with_dfilter(&edt, sfcode);
327 epan_dissect_run_with_taps(&edt, cf->cd_t, &cf->rec, fdata, NULL);
328 epan_dissect_cleanup(&edt);
329 remove_tap_listener(&th);
330 dfilter_free(sfcode);
332 if (th.num_hdrs == 0) {
333 /* This "shouldn't happen", as our menu items shouldn't
334 * even be enabled if the selected packet isn't a TCP
335 * segment, as tcp_graph_selected_packet_enabled() is used
336 * to determine whether to enable any of our menu items. */
337 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
338 "Selected packet isn't a TCP segment or is truncated");
339 return UINT32_MAX;
341 /* XXX fix this later, we should show a dialog allowing the user
342 to select which session he wants here
344 if (th.num_hdrs > 1) {
345 /* can only handle a single tcp layer yet */
346 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
347 "The selected packet has more than one TCP unique conversation "
348 "in it.");
349 return UINT32_MAX;
352 /* For now, still always choose the first/only one */
353 th_stream = th.tcphdrs[0]->th_stream;
355 for (int n = 0; n < th.num_hdrs; n++) {
356 free_address(&th.tcphdrs[n]->ip_src);
357 free_address(&th.tcphdrs[n]->ip_dst);
358 g_free(th.tcphdrs[n]);
361 return th_stream;
364 bool rtt_is_retrans(struct rtt_unack *list, unsigned int seqno)
366 struct rtt_unack *u;
368 for (u=list; u; u=u->next) {
369 if (tcp_seq_eq_or_after(seqno, u->seqno) &&
370 tcp_seq_before(seqno, u->end_seqno)) {
371 return true;
374 return false;
377 struct rtt_unack *
378 rtt_get_new_unack(double time_val, unsigned int seqno, unsigned int seglen)
380 struct rtt_unack *u;
382 u = g_new(struct rtt_unack, 1);
383 u->next = NULL;
384 u->time = time_val;
385 u->seqno = seqno;
386 u->end_seqno = seqno + seglen;
387 return u;
390 void rtt_put_unack_on_list(struct rtt_unack **l, struct rtt_unack *new_unack)
392 struct rtt_unack *u, *list = *l;
394 for (u=list; u; u=u->next) {
395 if (!u->next) {
396 break;
399 if (u) {
400 u->next = new_unack;
401 } else {
402 *l = new_unack;
406 void rtt_delete_unack_from_list(struct rtt_unack **l, struct rtt_unack *dead)
408 struct rtt_unack *u, *list = *l;
410 if (!dead || !list) {
411 return;
414 if (dead == list) {
415 *l = list->next;
416 g_free(list);
417 } else {
418 for (u=list; u; u=u->next) {
419 if (u->next == dead) {
420 u->next = u->next->next;
421 g_free(dead);
422 break;
428 void rtt_destroy_unack_list(struct rtt_unack **l ) {
429 while (*l) {
430 struct rtt_unack *head = *l;
431 *l = head->next;
432 g_free(head);