1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2009 Benjamin Poirier <benjamin.poirier@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 #include <arpa/inet.h>
34 #include "data_structures.h"
38 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
41 // TCP sequence numbers use clock arithmetic, these comparison functions take
43 #define SEQ_LT(a,b) ((int32_t)((a)-(b)) < 0)
44 #define SEQ_LEQ(a,b) ((int32_t)((a)-(b)) <= 0)
45 #define SEQ_GT(a,b) ((int32_t)((a)-(b)) > 0)
46 #define SEQ_GEQ(a,b) ((int32_t)((a)-(b)) >= 0)
50 * Compare two ConnectionKey structures
53 * true if each field of the structure is equal
56 bool connectionKeyEqual(const ConnectionKey
* const a
, const
57 ConnectionKey
* const b
)
59 if (a
->saddr
== b
->saddr
&& a
->daddr
== b
->daddr
&& a
->source
== b
->source
60 && a
->dest
== b
->dest
)
72 * Check if a packet is an acknowledge of another packet.
75 * ackSegment packet that is the confirmation
76 * ackedSegment packet that contains the original data, both packets have to
77 * come from the same direction of the same connection. Both
78 * messages have to contain TCP events.
80 bool isAcking(const Message
* const ackSegment
, const Message
* const
83 g_assert(ackSegment
->inE
->type
== TCP
);
84 g_assert(ackSegment
->outE
->type
== TCP
);
86 if (SEQ_GT(ackSegment
->inE
->event
.tcpEvent
->segmentKey
->ack_seq
,
87 ackedSegment
->inE
->event
.tcpEvent
->segmentKey
->seq
))
99 * Convert an IP address from 32 bit form to dotted quad
102 * str: A preallocated string of length >= 16
105 void convertIP(char* const str
, const uint32_t addr
)
107 strcpy(str
, inet_ntoa((struct in_addr
) {.s_addr
= addr
}));
112 * Print the content of a TCP Message structure
114 void printTCPSegment(const Message
* const segment
)
116 char saddr
[16], daddr
[16];
117 SegmentKey
* segmentKey
;
119 g_assert(segment
->inE
->type
== TCP
);
120 g_assert(segment
->inE
->event
.tcpEvent
->segmentKey
==
121 segment
->outE
->event
.tcpEvent
->segmentKey
);
123 segmentKey
= segment
->inE
->event
.tcpEvent
->segmentKey
;
125 convertIP(saddr
, segmentKey
->connectionKey
.saddr
);
126 convertIP(daddr
, segmentKey
->connectionKey
.daddr
);
127 g_debug("%s:%u to %s:%u tot_len: %u ihl: %u seq: %u ack_seq: %u doff: %u "
128 "ack: %u rst: %u syn: %u fin: %u", saddr
,
129 segmentKey
->connectionKey
.source
, daddr
, segmentKey
->connectionKey
.dest
,
130 segmentKey
->tot_len
, segmentKey
->ihl
, segmentKey
->seq
,
131 segmentKey
->ack_seq
, segmentKey
->doff
, segmentKey
->ack
, segmentKey
->rst
,
132 segmentKey
->syn
, segmentKey
->fin
);
137 * A GHashFunc for g_hash_table_new()
139 * This function is for indexing TCPEvents in unMatched lists. All fields of
140 * the corresponding SegmentKey must match for two keys to be equal.
146 * A hash of all fields in the SegmentKey
148 guint
ghfSegmentKeyHash(gconstpointer key
)
153 p
= (SegmentKey
*) key
;
155 a
= p
->connectionKey
.source
+ (p
->connectionKey
.dest
<< 16);
156 b
= p
->connectionKey
.saddr
;
157 c
= p
->connectionKey
.daddr
;
160 a
+= p
->ihl
+ (p
->tot_len
<< 8) + (p
->doff
<< 24);
165 a
+= p
->ack
+ (p
->rst
<< 8) + (p
->syn
<< 16) + (p
->fin
<< 24);
168 g_debug("segment key hash %p: %u", p
, c
);
175 * A GEqualFunc for g_hash_table_new()
177 * This function is for indexing TCPEvents in unMatched lists. All fields of
178 * the corresponding SegmentKey must match for two keys to be equal.
184 * TRUE if both values are equal
186 gboolean
gefSegmentKeyEqual(gconstpointer a
, gconstpointer b
)
188 const SegmentKey
* sA
, * sB
;
193 if (connectionKeyEqual(&sA
->connectionKey
, &sB
->connectionKey
) &&
194 sA
->ihl
== sB
->ihl
&&
195 sA
->tot_len
== sB
->tot_len
&&
196 sA
->seq
== sB
->seq
&&
197 sA
->ack_seq
== sB
->ack_seq
&&
198 sA
->doff
== sB
->doff
&&
199 sA
->ack
== sB
->ack
&&
200 sA
->rst
== sB
->rst
&&
201 sA
->syn
== sB
->syn
&&
204 g_debug("segment key equal %p %p: TRUE", sA
, sB
);
209 g_debug("segment key equal %p %p: FALSE", sA
, sB
);
216 * A GDestroyNotify function for g_hash_table_new_full()
221 void gdnDestroyEvent(gpointer data
)
225 event
->destroy(event
);
230 * A GDestroyNotify function for g_hash_table_new_full()
233 * data: GQueue* list[Packet]
235 void gdnTCPSegmentListDestroy(gpointer data
)
239 list
= (GQueue
*) data
;
241 g_debug("XXXX gdnTCPSegmentListDestroy\n");
243 g_queue_foreach(list
, &gfTCPSegmentDestroy
, NULL
);
249 * A GFunc for g_queue_foreach()
252 * data Message*, TCP message to destroy
255 void gfTCPSegmentDestroy(gpointer data
, gpointer user_data
)
257 g_debug("XXXX gfTCPSegmentDestroy\n");
258 destroyTCPSegment((Message
*) data
);
263 * Free the memory used by a TCP Message and the memory of all its associated
267 * segment TCP Message to destroy
269 void destroyTCPSegment(Message
* const segment
)
271 TCPEvent
* inE
, *outE
;
273 g_debug("XXXX destroyTCPSegment");
274 segment
->print(segment
);
276 g_assert(segment
->inE
!= NULL
&& segment
->outE
!= NULL
);
277 g_assert(segment
->inE
->type
== TCP
&& segment
->outE
->type
== TCP
);
278 inE
= segment
->inE
->event
.tcpEvent
;
279 outE
= segment
->outE
->event
.tcpEvent
;
280 g_assert(inE
->segmentKey
== outE
->segmentKey
);
282 outE
->segmentKey
= NULL
;
284 destroyTCPEvent(segment
->inE
);
285 destroyTCPEvent(segment
->outE
);
292 * Free the memory used by a TCP Exchange and the memory of SOME of its
293 * associated resources. The message is not destroyed. Use destroyTCPSegment()
297 * exchange TCP Exchange to destroy. The .message must be NULL
299 void destroyTCPExchange(Exchange
* const exchange
)
301 g_assert(exchange
->message
== NULL
);
303 if (exchange
->acks
!= NULL
)
305 g_queue_foreach(exchange
->acks
, &gfTCPSegmentDestroy
, NULL
);
306 g_queue_free(exchange
->acks
);
314 * Free the memory used by a TCP Event and its associated resources
316 void destroyTCPEvent(Event
* const event
)
318 g_assert(event
->type
== TCP
);
320 if (event
->event
.tcpEvent
->segmentKey
!= NULL
)
322 free(event
->event
.tcpEvent
->segmentKey
);
324 free(event
->event
.tcpEvent
);
325 event
->event
.tcpEvent
= NULL
;
331 * Free the memory used by a base Event
333 void destroyEvent(Event
* const event
)
335 g_assert(event
->event
.tcpEvent
== NULL
);
342 * Free the memory used by a UDP Event and its associated resources
344 void destroyUDPEvent(Event
* const event
)
346 g_assert(event
->type
== UDP
);
348 if (event
->event
.udpEvent
->datagramKey
!= NULL
)
350 free(event
->event
.udpEvent
->datagramKey
);
352 free(event
->event
.udpEvent
);
353 event
->event
.udpEvent
= NULL
;
359 * A GCompareFunc for g_queue_find_custom()
362 * a Message* acked packet
363 * b Message* ack packet
368 gint
gcfTCPSegmentAckCompare(gconstpointer a
, gconstpointer b
)
370 if (isAcking((const Message
*) b
, (const Message
*) a
))
382 * A GHashFunc for g_hash_table_new()
384 * Hash TCP connection keys. Here are a few possible implementations:
386 * 2.4 kernels used tcp_hashfn()
388 * I've seen something about an XOR hash:
389 * http://tservice.net.ru/~s0mbre/blog/2006/05/14#2006_05_14:
390 * unsigned int h = (laddr ^ lport) ^ (faddr ^ fport);
395 * In 2.6 kernels, inet_ehashfn() handles connection hashing with the help of
396 * Jenkins hashing, jhash.h
398 * This function uses jenkins hashing. The hash is not the same for packets in
399 * opposite directions of the same connection. (Hence the name
400 * connection*key*hash)
405 guint
ghfConnectionKeyHash(gconstpointer key
)
407 ConnectionKey
* connectionKey
;
410 connectionKey
= (ConnectionKey
*) key
;
412 a
= connectionKey
->source
+ (connectionKey
->dest
<< 16);
413 b
= connectionKey
->saddr
;
414 c
= connectionKey
->daddr
;
422 * A GEqualFunc for g_hash_table_new()
425 * a, b ConnectionKey*
428 * TRUE if both values are equal
430 gboolean
gefConnectionKeyEqual(gconstpointer a
, gconstpointer b
)
432 // Two packets in the same direction
433 if (connectionKeyEqual((const ConnectionKey
*) a
, (const ConnectionKey
*) b
))
445 * A GDestroyNotify function for g_hash_table_new_full()
448 * data: ConnectionKey*
450 void gdnConnectionKeyDestroy(gpointer data
)
452 free((ConnectionKey
*) data
);
457 * A GHashFunc for g_hash_table_new()
462 guint
ghfDatagramKeyHash(gconstpointer key
)
464 DatagramKey
* datagramKey
;
467 datagramKey
= (DatagramKey
*) key
;
469 a
= datagramKey
->saddr
;
470 b
= datagramKey
->daddr
;
471 c
= datagramKey
->source
+ (datagramKey
->dest
<< 16);
474 a
+= datagramKey
->ulen
; // 16 bits left here
475 b
+= *((uint32_t*) datagramKey
->dataKey
);
476 c
+= *((uint32_t*) ((void*) datagramKey
->dataKey
+ 4));
484 * A GEqualFunc for g_hash_table_new()
490 * TRUE if both values are equal
492 gboolean
gefDatagramKeyEqual(gconstpointer a
, gconstpointer b
)
494 const DatagramKey
* dA
, * dB
;
496 dA
= (DatagramKey
*) a
;
497 dB
= (DatagramKey
*) b
;
499 if (dA
->saddr
== dB
->saddr
&& dA
->daddr
== dB
->daddr
&&
500 dA
->source
== dB
->source
&& dA
->dest
== dB
->dest
&&
501 dA
->ulen
== dB
->ulen
&&
502 memcmp(dA
->dataKey
, dB
->dataKey
, sizeof(dA
->dataKey
)) == 0)
514 * A GDestroyNotify function for g_hash_table_new_full()
519 void gdnDestroyDatagramKey(gpointer data
)
521 free((DatagramKey
*) data
);
526 * A GDestroyNotify function for g_hash_table_new_full()
531 void gdnDestroyBroadcast(gpointer data
)
533 destroyBroadcast((Broadcast
*) data
);
538 * Free a Broadcast struct and its associated ressources
541 * broadcast: Broadcast*
543 void destroyBroadcast(Broadcast
* const broadcast
)
545 g_queue_foreach(broadcast
->events
, &gfDestroyEvent
, NULL
);
546 g_queue_free(broadcast
->events
);
552 * A GFunc for g_queue_foreach()
558 void gfDestroyEvent(gpointer data
, gpointer user_data
)
562 event
->destroy(event
);
566 /* Subtract two WallTime structs
572 * The result of tA - tB, as a double. This may incur a loss of
575 double wallTimeSub(const WallTime
const* tA
, const WallTime
const* tB
)
577 return (double) tA
->seconds
- tB
->seconds
+ ((double) tA
->nanosec
- tB
->nanosec
) / 1e9
;
582 * Allocate and copy a base event
585 * newEvent: new event, pointer will be updated
586 * event: event to copy
588 void copyEvent(const Event
* const event
, Event
** const newEvent
)
590 g_assert(event
->event
.tcpEvent
== NULL
);
592 *newEvent
= malloc(sizeof(Event
));
593 memcpy(*newEvent
, event
, sizeof(Event
));
598 * Allocate and copy a TCP event
601 * newEvent: new event, pointer will be updated
602 * event: event to copy
604 void copyTCPEvent(const Event
* const event
, Event
** const newEvent
)
606 g_assert(event
->type
== TCP
);
608 *newEvent
= malloc(sizeof(Event
));
609 memcpy(*newEvent
, event
, sizeof(Event
));
611 (*newEvent
)->event
.tcpEvent
= malloc(sizeof(TCPEvent
));
612 memcpy((*newEvent
)->event
.tcpEvent
, event
->event
.tcpEvent
,
615 (*newEvent
)->event
.tcpEvent
->segmentKey
= malloc(sizeof(SegmentKey
));
616 memcpy((*newEvent
)->event
.tcpEvent
->segmentKey
,
617 event
->event
.tcpEvent
->segmentKey
, sizeof(SegmentKey
));
622 * Allocate and copy a UDP event
625 * newEvent: new event, pointer will be updated
626 * event: event to copy
628 void copyUDPEvent(const Event
* const event
, Event
** const newEvent
)
630 g_assert(event
->type
== UDP
);
632 *newEvent
= malloc(sizeof(Event
));
633 memcpy(*newEvent
, event
, sizeof(Event
));
635 (*newEvent
)->event
.udpEvent
= malloc(sizeof(UDPEvent
));
636 memcpy((*newEvent
)->event
.udpEvent
, event
->event
.udpEvent
,
639 (*newEvent
)->event
.udpEvent
->datagramKey
= malloc(sizeof(DatagramKey
));
640 memcpy((*newEvent
)->event
.udpEvent
->datagramKey
,
641 event
->event
.udpEvent
->datagramKey
, sizeof(DatagramKey
));
646 * A GFunc for g_queue_foreach()
649 * data Event*, event to add
650 * user_data GArray*, array to add to
652 void gfAddEventToArray(gpointer data
, gpointer user_data
)
654 g_array_append_val((GArray
*) user_data
, data
);
This page took 0.054924 seconds and 4 git commands to generate.