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"
37 // TCP sequence numbers use clock arithmetic, these comparison functions take
39 #define SEQ_LT(a,b) ((int32_t)((a)-(b)) < 0)
40 #define SEQ_LEQ(a,b) ((int32_t)((a)-(b)) <= 0)
41 #define SEQ_GT(a,b) ((int32_t)((a)-(b)) > 0)
42 #define SEQ_GEQ(a,b) ((int32_t)((a)-(b)) >= 0)
46 * Compare two ConnectionKey structures
49 * true if each field of the structure is equal
52 bool connectionKeyEqual(const ConnectionKey
* const a
, const
53 ConnectionKey
* const b
)
55 if (a
->saddr
== b
->saddr
&& a
->daddr
== b
->daddr
&& a
->source
== b
->source
56 && a
->dest
== b
->dest
)
68 * Check if a packet is an acknowledge of another packet.
71 * ackSegment packet that is the confirmation
72 * ackedSegment packet that contains the original data, both packets have to
73 * come from the same direction of the same connection. Both
74 * messages have to contain TCP events.
76 bool isAcking(const Message
* const ackSegment
, const Message
* const
79 g_assert(ackSegment
->inE
->type
== TCP
);
80 g_assert(ackSegment
->outE
->type
== TCP
);
82 if (SEQ_GT(ackSegment
->inE
->event
.tcpEvent
->segmentKey
->ack_seq
,
83 ackedSegment
->inE
->event
.tcpEvent
->segmentKey
->seq
))
95 * Convert an IP address from 32 bit form to dotted quad
98 * str: A preallocated string of length >= 16
101 void convertIP(char* const str
, const uint32_t addr
)
103 strcpy(str
, inet_ntoa((struct in_addr
) {.s_addr
= addr
}));
108 * Print the content of a TCP Message structure
110 void printTCPSegment(const Message
* const segment
)
112 char saddr
[16], daddr
[16];
113 SegmentKey
* segmentKey
;
115 g_assert(segment
->inE
->type
== TCP
);
116 g_assert(segment
->inE
->event
.tcpEvent
->segmentKey
==
117 segment
->outE
->event
.tcpEvent
->segmentKey
);
119 segmentKey
= segment
->inE
->event
.tcpEvent
->segmentKey
;
121 convertIP(saddr
, segmentKey
->connectionKey
.saddr
);
122 convertIP(daddr
, segmentKey
->connectionKey
.daddr
);
123 g_debug("%s:%u to %s:%u tot_len: %u ihl: %u seq: %u ack_seq: %u doff: %u "
124 "ack: %u rst: %u syn: %u fin: %u", saddr
,
125 segmentKey
->connectionKey
.source
, daddr
, segmentKey
->connectionKey
.dest
,
126 segmentKey
->tot_len
, segmentKey
->ihl
, segmentKey
->seq
,
127 segmentKey
->ack_seq
, segmentKey
->doff
, segmentKey
->ack
, segmentKey
->rst
,
128 segmentKey
->syn
, segmentKey
->fin
);
133 * A GHashFunc for g_hash_table_new()
135 * This function is for indexing TCPEvents in unMatched lists. All fields of
136 * the corresponding SegmentKey must match for two keys to be equal.
142 * A hash of all fields in the SegmentKey
144 guint
ghfSegmentKeyHash(gconstpointer key
)
149 p
= (SegmentKey
*) key
;
151 a
= p
->connectionKey
.source
+ (p
->connectionKey
.dest
<< 16);
152 b
= p
->connectionKey
.saddr
;
153 c
= p
->connectionKey
.daddr
;
156 a
+= p
->ihl
+ (p
->tot_len
<< 8) + (p
->doff
<< 24);
161 a
+= p
->ack
+ (p
->rst
<< 8) + (p
->syn
<< 16) + (p
->fin
<< 24);
164 g_debug("segment key hash %p: %u", p
, c
);
171 * A GEqualFunc for g_hash_table_new()
173 * This function is for indexing TCPEvents in unMatched lists. All fields of
174 * the corresponding SegmentKey must match for two keys to be equal.
180 * TRUE if both values are equal
182 gboolean
gefSegmentKeyEqual(gconstpointer a
, gconstpointer b
)
184 const SegmentKey
* sA
, * sB
;
189 if (connectionKeyEqual(&sA
->connectionKey
, &sB
->connectionKey
) &&
190 sA
->ihl
== sB
->ihl
&&
191 sA
->tot_len
== sB
->tot_len
&&
192 sA
->seq
== sB
->seq
&&
193 sA
->ack_seq
== sB
->ack_seq
&&
194 sA
->doff
== sB
->doff
&&
195 sA
->ack
== sB
->ack
&&
196 sA
->rst
== sB
->rst
&&
197 sA
->syn
== sB
->syn
&&
200 g_debug("segment key equal %p %p: TRUE", sA
, sB
);
205 g_debug("segment key equal %p %p: FALSE", sA
, sB
);
212 * A GDestroyNotify function for g_hash_table_new_full()
217 void gdnDestroyEvent(gpointer data
)
221 event
->destroy(event
);
226 * A GDestroyNotify function for g_hash_table_new_full()
229 * data: GQueue* list[Packet]
231 void gdnTCPSegmentListDestroy(gpointer data
)
235 list
= (GQueue
*) data
;
237 g_queue_foreach(list
, &gfTCPSegmentDestroy
, NULL
);
243 * A GFunc for g_queue_foreach()
246 * data Message*, TCP message to destroy
249 void gfTCPSegmentDestroy(gpointer data
, gpointer user_data
)
251 destroyTCPSegment((Message
*) data
);
256 * Free the memory used by a TCP Message and the memory of all its associated
260 * segment TCP Message to destroy
262 void destroyTCPSegment(Message
* const segment
)
264 TCPEvent
* inE
, *outE
;
266 segment
->print(segment
);
268 g_assert(segment
->inE
!= NULL
&& segment
->outE
!= NULL
);
269 g_assert(segment
->inE
->type
== TCP
&& segment
->outE
->type
== TCP
);
270 inE
= segment
->inE
->event
.tcpEvent
;
271 outE
= segment
->outE
->event
.tcpEvent
;
272 g_assert(inE
->segmentKey
== outE
->segmentKey
);
274 outE
->segmentKey
= NULL
;
276 destroyTCPEvent(segment
->inE
);
277 destroyTCPEvent(segment
->outE
);
284 * Free the memory used by a TCP Exchange and the memory of SOME of its
285 * associated resources. The message is not destroyed. Use destroyTCPSegment()
289 * exchange TCP Exchange to destroy. The .message must be NULL
291 void destroyTCPExchange(Exchange
* const exchange
)
293 g_assert(exchange
->message
== NULL
);
295 if (exchange
->acks
!= NULL
)
297 g_queue_foreach(exchange
->acks
, &gfTCPSegmentDestroy
, NULL
);
298 g_queue_free(exchange
->acks
);
306 * Free the memory used by a TCP Event and its associated resources
308 void destroyTCPEvent(Event
* const event
)
310 g_assert(event
->type
== TCP
);
312 if (event
->event
.tcpEvent
->segmentKey
!= NULL
)
314 free(event
->event
.tcpEvent
->segmentKey
);
316 free(event
->event
.tcpEvent
);
317 event
->event
.tcpEvent
= NULL
;
323 * Free the memory used by a base Event
325 void destroyEvent(Event
* const event
)
327 g_assert(event
->event
.tcpEvent
== NULL
);
334 * Free the memory used by a UDP Event and its associated resources
336 void destroyUDPEvent(Event
* const event
)
338 g_assert(event
->type
== UDP
);
340 if (event
->event
.udpEvent
->datagramKey
!= NULL
)
342 free(event
->event
.udpEvent
->datagramKey
);
344 free(event
->event
.udpEvent
);
345 event
->event
.udpEvent
= NULL
;
351 * A GCompareFunc for g_queue_find_custom()
354 * a Message* acked packet
355 * b Message* ack packet
360 gint
gcfTCPSegmentAckCompare(gconstpointer a
, gconstpointer b
)
362 if (isAcking((const Message
*) b
, (const Message
*) a
))
374 * A GHashFunc for g_hash_table_new()
376 * Hash TCP connection keys. Here are a few possible implementations:
378 * 2.4 kernels used tcp_hashfn()
380 * I've seen something about an XOR hash:
381 * http://tservice.net.ru/~s0mbre/blog/2006/05/14#2006_05_14:
382 * unsigned int h = (laddr ^ lport) ^ (faddr ^ fport);
387 * In 2.6 kernels, inet_ehashfn() handles connection hashing with the help of
388 * Jenkins hashing, jhash.h
390 * This function uses jenkins hashing. The hash is not the same for packets in
391 * opposite directions of the same connection. (Hence the name
392 * connection*key*hash)
397 guint
ghfConnectionKeyHash(gconstpointer key
)
399 ConnectionKey
* connectionKey
;
402 connectionKey
= (ConnectionKey
*) key
;
404 a
= connectionKey
->source
+ (connectionKey
->dest
<< 16);
405 b
= connectionKey
->saddr
;
406 c
= connectionKey
->daddr
;
414 * A GEqualFunc for g_hash_table_new()
417 * a, b ConnectionKey*
420 * TRUE if both values are equal
422 gboolean
gefConnectionKeyEqual(gconstpointer a
, gconstpointer b
)
424 // Two packets in the same direction
425 if (connectionKeyEqual((const ConnectionKey
*) a
, (const ConnectionKey
*) b
))
437 * A GDestroyNotify function for g_hash_table_new_full()
440 * data: ConnectionKey*
442 void gdnConnectionKeyDestroy(gpointer data
)
444 free((ConnectionKey
*) data
);
449 * A GHashFunc for g_hash_table_new()
454 guint
ghfDatagramKeyHash(gconstpointer key
)
456 DatagramKey
* datagramKey
;
459 datagramKey
= (DatagramKey
*) key
;
461 a
= datagramKey
->saddr
;
462 b
= datagramKey
->daddr
;
463 c
= datagramKey
->source
+ (datagramKey
->dest
<< 16);
466 a
+= datagramKey
->ulen
; // 16 bits left here
467 b
+= *((uint32_t*) datagramKey
->dataKey
);
468 c
+= *((uint32_t*) ((void*) datagramKey
->dataKey
+ 4));
476 * A GEqualFunc for g_hash_table_new()
482 * TRUE if both values are equal
484 gboolean
gefDatagramKeyEqual(gconstpointer a
, gconstpointer b
)
486 const DatagramKey
* dA
, * dB
;
488 dA
= (DatagramKey
*) a
;
489 dB
= (DatagramKey
*) b
;
491 if (dA
->saddr
== dB
->saddr
&& dA
->daddr
== dB
->daddr
&&
492 dA
->source
== dB
->source
&& dA
->dest
== dB
->dest
&&
493 dA
->ulen
== dB
->ulen
&&
494 memcmp(dA
->dataKey
, dB
->dataKey
, sizeof(dA
->dataKey
)) == 0)
506 * A GDestroyNotify function for g_hash_table_new_full()
511 void gdnDestroyDatagramKey(gpointer data
)
513 free((DatagramKey
*) data
);
518 * A GDestroyNotify function for g_hash_table_new_full()
523 void gdnDestroyBroadcast(gpointer data
)
525 destroyBroadcast((Broadcast
*) data
);
530 * Free a Broadcast struct and its associated ressources
533 * broadcast: Broadcast*
535 void destroyBroadcast(Broadcast
* const broadcast
)
537 g_queue_foreach(broadcast
->events
, &gfDestroyEvent
, NULL
);
538 g_queue_free(broadcast
->events
);
544 * A GFunc for g_queue_foreach()
550 void gfDestroyEvent(gpointer data
, gpointer user_data
)
554 event
->destroy(event
);
558 /* Subtract two WallTime structs
564 * The result of tA - tB, as a double. This may incur a loss of
567 double wallTimeSub(const WallTime
const* tA
, const WallTime
const* tB
)
569 return (double) tA
->seconds
- tB
->seconds
+ ((double) tA
->nanosec
- tB
->nanosec
) / 1e9
;
574 * Allocate and copy a base event
577 * newEvent: new event, pointer will be updated
578 * event: event to copy
580 void copyEvent(const Event
* const event
, Event
** const newEvent
)
582 g_assert(event
->event
.tcpEvent
== NULL
);
584 *newEvent
= malloc(sizeof(Event
));
585 memcpy(*newEvent
, event
, sizeof(Event
));
590 * Allocate and copy a TCP event
593 * newEvent: new event, pointer will be updated
594 * event: event to copy
596 void copyTCPEvent(const Event
* const event
, Event
** const newEvent
)
598 g_assert(event
->type
== TCP
);
600 *newEvent
= malloc(sizeof(Event
));
601 memcpy(*newEvent
, event
, sizeof(Event
));
603 (*newEvent
)->event
.tcpEvent
= malloc(sizeof(TCPEvent
));
604 memcpy((*newEvent
)->event
.tcpEvent
, event
->event
.tcpEvent
,
607 (*newEvent
)->event
.tcpEvent
->segmentKey
= malloc(sizeof(SegmentKey
));
608 memcpy((*newEvent
)->event
.tcpEvent
->segmentKey
,
609 event
->event
.tcpEvent
->segmentKey
, sizeof(SegmentKey
));
614 * Allocate and copy a UDP event
617 * newEvent: new event, pointer will be updated
618 * event: event to copy
620 void copyUDPEvent(const Event
* const event
, Event
** const newEvent
)
622 g_assert(event
->type
== UDP
);
624 *newEvent
= malloc(sizeof(Event
));
625 memcpy(*newEvent
, event
, sizeof(Event
));
627 (*newEvent
)->event
.udpEvent
= malloc(sizeof(UDPEvent
));
628 memcpy((*newEvent
)->event
.udpEvent
, event
->event
.udpEvent
,
631 (*newEvent
)->event
.udpEvent
->datagramKey
= malloc(sizeof(DatagramKey
));
632 memcpy((*newEvent
)->event
.udpEvent
->datagramKey
,
633 event
->event
.udpEvent
->datagramKey
, sizeof(DatagramKey
));
638 * A GFunc for g_queue_foreach()
641 * data Event*, event to add
642 * user_data GArray*, array to add to
644 void gfAddEventToArray(gpointer data
, gpointer user_data
)
646 g_array_append_val((GArray
*) user_data
, data
);
This page took 0.04221 seconds and 4 git commands to generate.