1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2009, 2010 Benjamin Poirier <benjamin.poirier@polymtl.ca>
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, either version 2.1 of the License, or (at
7 * your option) any later version.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
12 * License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <arpa/inet.h>
33 #include "data_structures.h"
36 // TCP sequence numbers use clock arithmetic, these comparison functions take
38 #define SEQ_LT(a,b) ((int32_t)((a)-(b)) < 0)
39 #define SEQ_LEQ(a,b) ((int32_t)((a)-(b)) <= 0)
40 #define SEQ_GT(a,b) ((int32_t)((a)-(b)) > 0)
41 #define SEQ_GEQ(a,b) ((int32_t)((a)-(b)) >= 0)
45 * Compare two ConnectionKey structures
48 * true if each field of the structure is equal
51 bool connectionKeyEqual(const ConnectionKey
* const a
, const
52 ConnectionKey
* const b
)
54 if (a
->saddr
== b
->saddr
&& a
->daddr
== b
->daddr
&& a
->source
== b
->source
55 && a
->dest
== b
->dest
)
67 * Check if a packet is an acknowledge of another packet.
70 * ackSegment packet that is the confirmation
71 * ackedSegment packet that contains the original data, both packets have to
72 * come from the same direction of the same connection. Both
73 * messages have to contain TCP events.
75 bool isAcking(const Message
* const ackSegment
, const Message
* const
78 g_assert(ackSegment
->inE
->type
== TCP
);
79 g_assert(ackSegment
->outE
->type
== TCP
);
81 if (SEQ_GT(ackSegment
->inE
->event
.tcpEvent
->segmentKey
->ack_seq
,
82 ackedSegment
->inE
->event
.tcpEvent
->segmentKey
->seq
))
94 * Convert an IP address from 32 bit form to dotted quad
97 * str: A preallocated string of length >= 16
100 void convertIP(char* const str
, const uint32_t addr
)
102 strcpy(str
, inet_ntoa((struct in_addr
) {.s_addr
= addr
}));
107 * Print the content of a TCP Message structure
109 void printTCPSegment(const Message
* const segment
)
111 char saddr
[16], daddr
[16];
112 SegmentKey
* segmentKey
;
114 g_assert(segment
->inE
->type
== TCP
);
115 g_assert(segment
->inE
->event
.tcpEvent
->segmentKey
==
116 segment
->outE
->event
.tcpEvent
->segmentKey
);
118 segmentKey
= segment
->inE
->event
.tcpEvent
->segmentKey
;
120 convertIP(saddr
, segmentKey
->connectionKey
.saddr
);
121 convertIP(daddr
, segmentKey
->connectionKey
.daddr
);
122 g_debug("%s:%u to %s:%u tot_len: %u ihl: %u seq: %u ack_seq: %u doff: %u "
123 "ack: %u rst: %u syn: %u fin: %u", saddr
,
124 segmentKey
->connectionKey
.source
, daddr
, segmentKey
->connectionKey
.dest
,
125 segmentKey
->tot_len
, segmentKey
->ihl
, segmentKey
->seq
,
126 segmentKey
->ack_seq
, segmentKey
->doff
, segmentKey
->ack
, segmentKey
->rst
,
127 segmentKey
->syn
, segmentKey
->fin
);
132 * A GHashFunc for g_hash_table_new()
134 * This function is for indexing TCPEvents in unMatched lists. All fields of
135 * the corresponding SegmentKey must match for two keys to be equal.
141 * A hash of all fields in the SegmentKey
143 guint
ghfSegmentKeyHash(gconstpointer key
)
148 p
= (SegmentKey
*) key
;
150 a
= p
->connectionKey
.source
+ (p
->connectionKey
.dest
<< 16);
151 b
= p
->connectionKey
.saddr
;
152 c
= p
->connectionKey
.daddr
;
155 a
+= p
->ihl
+ (p
->tot_len
<< 8) + (p
->doff
<< 24);
160 a
+= p
->ack
+ (p
->rst
<< 8) + (p
->syn
<< 16) + (p
->fin
<< 24);
163 g_debug("segment key hash %p: %u", p
, c
);
170 * A GEqualFunc for g_hash_table_new()
172 * This function is for indexing TCPEvents in unMatched lists. All fields of
173 * the corresponding SegmentKey must match for two keys to be equal.
179 * TRUE if both values are equal
181 gboolean
gefSegmentKeyEqual(gconstpointer a
, gconstpointer b
)
183 const SegmentKey
* sA
, * sB
;
188 if (connectionKeyEqual(&sA
->connectionKey
, &sB
->connectionKey
) &&
189 sA
->ihl
== sB
->ihl
&&
190 sA
->tot_len
== sB
->tot_len
&&
191 sA
->seq
== sB
->seq
&&
192 sA
->ack_seq
== sB
->ack_seq
&&
193 sA
->doff
== sB
->doff
&&
194 sA
->ack
== sB
->ack
&&
195 sA
->rst
== sB
->rst
&&
196 sA
->syn
== sB
->syn
&&
199 g_debug("segment key equal %p %p: TRUE", sA
, sB
);
204 g_debug("segment key equal %p %p: FALSE", sA
, sB
);
211 * A GDestroyNotify function for g_hash_table_new_full()
216 void gdnDestroyEvent(gpointer data
)
220 event
->destroy(event
);
225 * A GDestroyNotify function for g_hash_table_new_full()
228 * data: GQueue* list[Packet]
230 void gdnTCPSegmentListDestroy(gpointer data
)
234 list
= (GQueue
*) data
;
236 g_queue_foreach(list
, &gfTCPSegmentDestroy
, NULL
);
242 * A GFunc for g_queue_foreach()
245 * data Message*, TCP message to destroy
248 void gfTCPSegmentDestroy(gpointer data
, gpointer user_data
)
250 destroyTCPSegment((Message
*) data
);
255 * Free the memory used by a TCP Message and the memory of all its associated
259 * segment TCP Message to destroy
261 void destroyTCPSegment(Message
* const segment
)
263 TCPEvent
* inE
, *outE
;
265 segment
->print(segment
);
267 g_assert(segment
->inE
!= NULL
&& segment
->outE
!= NULL
);
268 g_assert(segment
->inE
->type
== TCP
&& segment
->outE
->type
== TCP
);
269 inE
= segment
->inE
->event
.tcpEvent
;
270 outE
= segment
->outE
->event
.tcpEvent
;
271 g_assert(inE
->segmentKey
== outE
->segmentKey
);
273 outE
->segmentKey
= NULL
;
275 destroyTCPEvent(segment
->inE
);
276 destroyTCPEvent(segment
->outE
);
283 * Free the memory used by a TCP Exchange and the memory of SOME of its
284 * associated resources. The message is not destroyed. Use destroyTCPSegment()
288 * exchange TCP Exchange to destroy. The .message must be NULL
290 void destroyTCPExchange(Exchange
* const exchange
)
292 g_assert(exchange
->message
== NULL
);
294 if (exchange
->acks
!= NULL
)
296 g_queue_foreach(exchange
->acks
, &gfTCPSegmentDestroy
, NULL
);
297 g_queue_free(exchange
->acks
);
305 * Free the memory used by a TCP Event and its associated resources
307 void destroyTCPEvent(Event
* const event
)
309 g_assert(event
->type
== TCP
);
311 if (event
->event
.tcpEvent
->segmentKey
!= NULL
)
313 free(event
->event
.tcpEvent
->segmentKey
);
315 free(event
->event
.tcpEvent
);
316 event
->event
.tcpEvent
= NULL
;
322 * Free the memory used by a base Event
324 void destroyEvent(Event
* const event
)
326 g_assert(event
->event
.tcpEvent
== NULL
);
333 * Free the memory used by a UDP Event and its associated resources
335 void destroyUDPEvent(Event
* const event
)
337 g_assert(event
->type
== UDP
);
339 if (event
->event
.udpEvent
->datagramKey
!= NULL
)
341 free(event
->event
.udpEvent
->datagramKey
);
343 free(event
->event
.udpEvent
);
344 event
->event
.udpEvent
= NULL
;
350 * A GCompareFunc for g_queue_find_custom()
353 * a Message* acked packet
354 * b Message* ack packet
359 gint
gcfTCPSegmentAckCompare(gconstpointer a
, gconstpointer b
)
361 if (isAcking((const Message
*) b
, (const Message
*) a
))
373 * A GHashFunc for g_hash_table_new()
375 * Hash TCP connection keys. Here are a few possible implementations:
377 * 2.4 kernels used tcp_hashfn()
379 * I've seen something about an XOR hash:
380 * http://tservice.net.ru/~s0mbre/blog/2006/05/14#2006_05_14:
381 * unsigned int h = (laddr ^ lport) ^ (faddr ^ fport);
386 * In 2.6 kernels, inet_ehashfn() handles connection hashing with the help of
387 * Jenkins hashing, jhash.h
389 * This function uses jenkins hashing. The hash is not the same for packets in
390 * opposite directions of the same connection. (Hence the name
391 * connection*key*hash)
396 guint
ghfConnectionKeyHash(gconstpointer key
)
398 ConnectionKey
* connectionKey
;
401 connectionKey
= (ConnectionKey
*) key
;
403 a
= connectionKey
->source
+ (connectionKey
->dest
<< 16);
404 b
= connectionKey
->saddr
;
405 c
= connectionKey
->daddr
;
413 * A GEqualFunc for g_hash_table_new()
416 * a, b ConnectionKey*
419 * TRUE if both values are equal
421 gboolean
gefConnectionKeyEqual(gconstpointer a
, gconstpointer b
)
423 // Two packets in the same direction
424 if (connectionKeyEqual((const ConnectionKey
*) a
, (const ConnectionKey
*) b
))
436 * A GDestroyNotify function for g_hash_table_new_full()
439 * data: ConnectionKey*
441 void gdnConnectionKeyDestroy(gpointer data
)
443 free((ConnectionKey
*) data
);
448 * A GHashFunc for g_hash_table_new()
453 guint
ghfDatagramKeyHash(gconstpointer key
)
455 DatagramKey
* datagramKey
;
458 uint32_t hashableKey
[2];
462 datagramKey
= (DatagramKey
*) key
;
463 memcpy(dataKey
.byteKey
, datagramKey
->dataKey
, sizeof(dataKey
.byteKey
));
465 a
= datagramKey
->saddr
;
466 b
= datagramKey
->daddr
;
467 c
= datagramKey
->source
+ (datagramKey
->dest
<< 16);
470 a
+= datagramKey
->ulen
; // 16 bits left here
471 b
+= dataKey
.hashableKey
[0];
472 c
+= dataKey
.hashableKey
[1];
480 * A GEqualFunc for g_hash_table_new()
486 * TRUE if both values are equal
488 gboolean
gefDatagramKeyEqual(gconstpointer a
, gconstpointer b
)
490 const DatagramKey
* dA
, * dB
;
492 dA
= (DatagramKey
*) a
;
493 dB
= (DatagramKey
*) b
;
495 if (dA
->saddr
== dB
->saddr
&& dA
->daddr
== dB
->daddr
&&
496 dA
->source
== dB
->source
&& dA
->dest
== dB
->dest
&&
497 dA
->ulen
== dB
->ulen
&&
498 memcmp(dA
->dataKey
, dB
->dataKey
, sizeof(dA
->dataKey
)) == 0)
510 * A GDestroyNotify function for g_hash_table_new_full()
515 void gdnDestroyDatagramKey(gpointer data
)
517 free((DatagramKey
*) data
);
522 * A GDestroyNotify function for g_hash_table_new_full()
527 void gdnDestroyBroadcast(gpointer data
)
529 destroyBroadcast((Broadcast
*) data
);
534 * Free a Broadcast struct and its associated ressources
537 * broadcast: Broadcast*
539 void destroyBroadcast(Broadcast
* const broadcast
)
541 g_queue_foreach(broadcast
->events
, &gfDestroyEvent
, NULL
);
542 g_queue_free(broadcast
->events
);
548 * A GFunc for g_queue_foreach()
554 void gfDestroyEvent(gpointer data
, gpointer user_data
)
558 event
->destroy(event
);
562 /* Subtract two WallTime structs
568 * The result of tA - tB, as a double. This may incur a loss of
571 double wallTimeSub(const WallTime
const* tA
, const WallTime
const* tB
)
573 return (double) tA
->seconds
- tB
->seconds
+ ((double) tA
->nanosec
- tB
->nanosec
) / 1e9
;
578 * Allocate and copy a base event
581 * newEvent: new event, pointer will be updated
582 * event: event to copy
584 void copyEvent(const Event
* const event
, Event
** const newEvent
)
586 g_assert(event
->event
.tcpEvent
== NULL
);
588 *newEvent
= malloc(sizeof(Event
));
589 memcpy(*newEvent
, event
, sizeof(Event
));
594 * Allocate and copy a TCP event
597 * newEvent: new event, pointer will be updated
598 * event: event to copy
600 void copyTCPEvent(const Event
* const event
, Event
** const newEvent
)
602 g_assert(event
->type
== TCP
);
604 *newEvent
= malloc(sizeof(Event
));
605 memcpy(*newEvent
, event
, sizeof(Event
));
607 (*newEvent
)->event
.tcpEvent
= malloc(sizeof(TCPEvent
));
608 memcpy((*newEvent
)->event
.tcpEvent
, event
->event
.tcpEvent
,
611 (*newEvent
)->event
.tcpEvent
->segmentKey
= malloc(sizeof(SegmentKey
));
612 memcpy((*newEvent
)->event
.tcpEvent
->segmentKey
,
613 event
->event
.tcpEvent
->segmentKey
, sizeof(SegmentKey
));
618 * Allocate and copy a UDP event
621 * newEvent: new event, pointer will be updated
622 * event: event to copy
624 void copyUDPEvent(const Event
* const event
, Event
** const newEvent
)
626 g_assert(event
->type
== UDP
);
628 *newEvent
= malloc(sizeof(Event
));
629 memcpy(*newEvent
, event
, sizeof(Event
));
631 (*newEvent
)->event
.udpEvent
= malloc(sizeof(UDPEvent
));
632 memcpy((*newEvent
)->event
.udpEvent
, event
->event
.udpEvent
,
635 (*newEvent
)->event
.udpEvent
->datagramKey
= malloc(sizeof(DatagramKey
));
636 memcpy((*newEvent
)->event
.udpEvent
->datagramKey
,
637 event
->event
.udpEvent
->datagramKey
, sizeof(DatagramKey
));
642 * A GFunc for g_queue_foreach()
645 * data Event*, event to add
646 * user_data GArray*, array to add to
648 void gfAddEventToArray(gpointer data
, gpointer user_data
)
650 g_array_append_val((GArray
*) user_data
, data
);
This page took 0.065085 seconds and 4 git commands to generate.