File: protocols_l3.c

package info (click to toggle)
libtrace3 3.0.22-0.2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,452 kB
  • sloc: ansic: 24,574; sh: 11,372; cpp: 1,811; makefile: 460; yacc: 96; lex: 50
file content (811 lines) | stat: -rw-r--r-- 20,537 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
/*
 * This file is part of libtrace
 *
 * Copyright (c) 2007,2008,2009,2010 The University of Waikato, Hamilton, 
 * New Zealand.
 *
 * Authors: Daniel Lawson 
 *          Perry Lorier
 *          Shane Alcock 
 *          
 * All rights reserved.
 *
 * This code has been developed by the University of Waikato WAND 
 * research group. For further information please see http://www.wand.net.nz/
 *
 * libtrace is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * libtrace is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with libtrace; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id$
 *
 */


#include "libtrace_int.h"
#include "libtrace.h"
#include "protocols.h"
#include "checksum.h"
#include <assert.h>
#include <stdlib.h>
#include <arpa/inet.h>

#ifdef HAVE_NETPACKET_PACKET_H
#include <sys/socket.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
#include <net/if_arp.h>
#include <string.h>
#else
#include <net/if_dl.h>
#include <string.h>
#endif

/* This file contains all the protocol decoding functions for layer 3
 * (the IP layer) protocols. This includes functions for accessing IP
 * addresses. 
 *
 * Supported protocols include:
 * 	IPv4
 * 	IPv6
 */

/* Gets an IPv4 header */
libtrace_ip_t *trace_get_ip(libtrace_packet_t *packet) 
{
	uint16_t ethertype;
	void *ret;

	uint32_t remaining = trace_get_capture_length(packet);

	ret = trace_get_layer3(packet,&ethertype,&remaining);

	if (!ret || ethertype!=TRACE_ETHERTYPE_IP)
		return NULL;

	/* Make sure we have at least a base IPv4 header */
	if (remaining < sizeof(libtrace_ip_t)) 
		return NULL;
	
	/* Not an IPv4 packet */
	if (((libtrace_ip_t*)ret)->ip_v != 4)
		return NULL;

	return (libtrace_ip_t*)ret;
}

libtrace_ip6_t *trace_get_ip6(libtrace_packet_t *packet) 
{
	uint16_t ethertype;
	void *ret;

	uint32_t remaining = trace_get_capture_length(packet);

	ret = trace_get_layer3(packet,&ethertype,&remaining);

	if (!ret || ethertype!=TRACE_ETHERTYPE_IPV6)
		return NULL;

	/* Make sure we have at least the base IPv6 header */
	if (remaining < sizeof(libtrace_ip6_t))
		return NULL;

	return (libtrace_ip6_t*)ret;
}

#define SW_IP_OFFMASK 0x1fff

DLLEXPORT void *trace_get_payload_from_ip(libtrace_ip_t *ipptr, uint8_t *prot,
		uint32_t *remaining) 
{
        void *trans_ptr = 0;

        assert(ipptr != NULL);
	
	/* Er? IPv5? */
	if (ipptr->ip_v != 4)
		return NULL;

	if ((ntohs(ipptr->ip_off) & SW_IP_OFFMASK) != 0) {
		if (remaining)
			*remaining = 0;		
		return NULL;
	}

	if (remaining) {
		if (*remaining<(ipptr->ip_hl*4U)) {
			*remaining = 0;
			return NULL;
		}
		/* If the packet features extra "padding", we probably
		 * don't want that counting as possible payload, e.g. for
		 * payload length calculations */
		//if (*remaining > ntohs(ipptr->ip_len))
		//	*remaining = ntohs(ipptr->ip_len);

		*remaining-=(ipptr->ip_hl * 4);
	}

        trans_ptr = (void *)((char *)ipptr + (ipptr->ip_hl * 4));

	if (prot) *prot = ipptr->ip_p;

        return trans_ptr;
}

void *trace_get_payload_from_ip6(libtrace_ip6_t *ipptr, uint8_t *prot,
		uint32_t *remaining) 
{
	void *payload = (char*)ipptr+sizeof(libtrace_ip6_t);
	uint8_t nxt;
	uint16_t len;

	assert (ipptr != NULL);
 	nxt = ipptr->nxt;	
	if (remaining) {
		if (*remaining<sizeof(libtrace_ip6_t)) {
			*remaining = 0;
			return NULL;
		}
		*remaining-=sizeof(libtrace_ip6_t);
	}

	while(1) {
		switch (nxt) {
			case 0: /* hop by hop options */
			case TRACE_IPPROTO_ROUTING:
			case TRACE_IPPROTO_AH: 
			case TRACE_IPPROTO_DSTOPTS: 
			{
				/* Length does not include the first 8 bytes */
				len=((libtrace_ip6_ext_t*)payload)->len * 8;
				len += 8;


				if (remaining) {
					if (*remaining < len) {
						/* Snap too short */
						*remaining = 0;
						return NULL;
					}
					*remaining-=len;
				}

				nxt=((libtrace_ip6_ext_t*)payload)->nxt;
				payload=(char*)payload+len;
				continue;
			}
			case TRACE_IPPROTO_ESP: 
			{
				if (prot) *prot=TRACE_IPPROTO_ESP;
				return payload;
			}
			case TRACE_IPPROTO_FRAGMENT:
				{
					len = sizeof(libtrace_ip6_frag_t);
					if (remaining) {
						if (*remaining < len) {
							/* Snap too short */
							*remaining = 0;
							return NULL;
						}
						*remaining-=len;
					}
					nxt=((libtrace_ip6_frag_t*)payload)->nxt;
					payload=(char*)payload+len;
					continue;
				}

			default:
				if (prot) *prot=nxt;
				return payload;
		}
	}
}

DLLEXPORT void *trace_get_layer3(const libtrace_packet_t *packet,
		uint16_t *ethertype,
		uint32_t *remaining)
{
	void *iphdr;
	uint16_t dummy_ethertype;
	void *link;
	uint32_t dummy_remaining;
	libtrace_linktype_t linktype;

	if (!ethertype) ethertype=&dummy_ethertype;

	if (!remaining) remaining=&dummy_remaining;

	/* use l3 cache */
	if (packet->l3_header)
	{
		/*
		link = trace_get_packet_buffer(packet,&linktype,remaining);

		if (!link)
			return NULL;
		*/

		*ethertype = packet->l3_ethertype;
		/* *remaining -= (packet->l3_header - link); */
		*remaining = packet->l3_remaining;

		return packet->l3_header;
	}

	link = trace_get_layer2(packet,&linktype,remaining);
	iphdr = trace_get_payload_from_layer2(
			link,
			linktype,
			ethertype,
			remaining);

	for(;;) {
		if (!iphdr || *remaining == 0)
			break;
		switch(*ethertype) {
		case TRACE_ETHERTYPE_8021Q: /* VLAN */
			iphdr=trace_get_payload_from_vlan(
					  iphdr,ethertype,remaining);
			continue;
		case TRACE_ETHERTYPE_MPLS: /* MPLS */
			iphdr=trace_get_payload_from_mpls(
					  iphdr,ethertype,remaining);

			if (iphdr && ethertype == 0x0) {
				iphdr=trace_get_payload_from_ethernet(
						iphdr,ethertype,remaining);
			}
			continue;
		case TRACE_ETHERTYPE_PPP_SES: /* PPPoE */
			iphdr = trace_get_payload_from_pppoe(iphdr, ethertype,
					remaining);
			continue;
		default:
			break;
		}

		break;
	}

	if (!iphdr || *remaining == 0)
		return NULL;

	/* Store values in the cache for later */
	/* Cast away constness, nasty, but this is just a cache */
	((libtrace_packet_t*)packet)->l3_ethertype = *ethertype;
	((libtrace_packet_t*)packet)->l3_header = iphdr;
	((libtrace_packet_t*)packet)->l3_remaining = *remaining;

	return iphdr;
}

/* Parse an ip or tcp option
 * @param[in,out] ptr	the pointer to the current option
 * @param[in,out] len	the length of the remaining buffer
 * @param[out] type	the type of the option
 * @param[out] optlen 	the length of the option
 * @param[out] data	the data of the option
 *
 * @returns bool true if there is another option (and the fields are filled in)
 *               or false if this was the last option.
 *
 * This updates ptr to point to the next option after this one, and updates
 * len to be the number of bytes remaining in the options area.  Type is updated
 * to be the code of this option, and data points to the data of this option,
 * with optlen saying how many bytes there are.
 *
 * @note Beware of fragmented packets.
 * @author Perry Lorier
 */
DLLEXPORT int trace_get_next_option(unsigned char **ptr,int *len,
			unsigned char *type,
			unsigned char *optlen,
			unsigned char **data)
{
	if (*len<=0)
		return 0;
	*type=**ptr;
	switch(*type) {
		case 0: /* End of options */
			return 0;
		case 1: /* Pad */
			(*ptr)++;
			(*len)--;
			return 1;
		default:
			*optlen = *(*ptr+1);
			if (*optlen<2)
				return 0; /* I have no idea wtf is going on
					   * with these packets
					   */

			/* Ensure that optlen is not greater than the
			 * amount of buffer remaining */
			if (*optlen > *len) 
				return 0;
			
			(*len)-=*optlen;
			(*data)=(*ptr+2);
			(*ptr)+=*optlen;
			if (*len<0)
				return 0;
			return 1;
	}
	assert(0);
}

static char *sockaddr_to_string(struct sockaddr *addrptr, char *space,
		int spacelen) {

	assert(addrptr && space);
	assert(spacelen > 0);
	
	if (addrptr->sa_family == AF_INET) {
		struct sockaddr_in *v4 = (struct sockaddr_in *)addrptr;
		inet_ntop(AF_INET, &(v4->sin_addr), space, spacelen);
	}

	else if (addrptr->sa_family == AF_INET6) {
		struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)addrptr;
		inet_ntop(AF_INET6, &(v6->sin6_addr), space, spacelen);
	}
#ifdef HAVE_NETPACKET_PACKET_H
	else if (addrptr->sa_family == AF_PACKET) {
		struct sockaddr_ll *l2addr = (struct sockaddr_ll *)addrptr;
		uint8_t *macbytes = (uint8_t *)l2addr->sll_addr;

		snprintf(space, spacelen, "%02x:%02x:%02x:%02x:%02x:%02x",
				macbytes[0], macbytes[1], macbytes[2],
				macbytes[3], macbytes[4], macbytes[5]);

	}
#else
	else if (addrptr->sa_family == AF_LINK) {
		struct sockaddr_dl *l2addr = (struct sockaddr_dl *)addrptr;
		uint8_t *macbytes = (uint8_t *)l2addr->sdl_data;

		snprintf(space, spacelen, "%02x:%02x:%02x:%02x:%02x:%02x",
				macbytes[0], macbytes[1], macbytes[2],
				macbytes[3], macbytes[4], macbytes[5]);
	
	}
#endif
	else {
		space[0] = '\0';
		return NULL;
	}

	return space;

}

/* Extract the source mac address from a frame and bundle it up into a sockaddr */
static struct sockaddr *get_source_ethernet_address(
	libtrace_ether_t *ethernet, struct sockaddr *addr)
{
	static struct sockaddr_storage dummy;
#ifdef HAVE_NETPACKET_PACKET_H
/* Use linux's sockaddr_ll structure */
	struct sockaddr_ll *l2addr;

	if (addr)
		l2addr = (struct sockaddr_ll*)addr;
	else
		l2addr = (struct sockaddr_ll*)&dummy;
	
	l2addr->sll_family = AF_PACKET;
	l2addr->sll_protocol = ethernet->ether_type;
	l2addr->sll_ifindex = 0; /* Irrelevant */
	l2addr->sll_hatype = ARPHRD_ETHER; 
	l2addr->sll_pkttype = PACKET_OTHERHOST;
	l2addr->sll_halen = 6;
	memcpy(l2addr->sll_addr,ethernet->ether_shost, 6);

	return (struct sockaddr*)l2addr;
#else
/* Use BSD's sockaddr_dl structure */
	struct sockaddr_dl *l2addr;

	if (addr)
		l2addr = (struct sockaddr_dl *)addr;
	else
		l2addr = (struct sockaddr_dl *)&dummy;
	
	l2addr->sdl_family = AF_LINK;
#if HAVE_SDL_LEN == 1
	l2addr->sdl_len = sizeof(struct sockaddr_dl);
#endif
	l2addr->sdl_index = 0; /* Unused */
	l2addr->sdl_alen = 6; /* Address length  */
	l2addr->sdl_nlen = 0; /* No name in here - this *should* work, right? */
	l2addr->sdl_slen = 0;	
	l2addr->sdl_type = 0; /* Hopefully zero is OK for this value too */
	memcpy(l2addr->sdl_data, ethernet->ether_shost, 6);

	return (struct sockaddr *)l2addr;
#endif
}

static struct sockaddr *get_source_l2_address(
	const libtrace_packet_t *packet, struct sockaddr *addr)
{
	static struct sockaddr_storage dummy;
	void *l2;
	libtrace_linktype_t linktype;
	uint32_t remaining;

	if (!addr)
		addr =(struct sockaddr*)&dummy;

	l2=trace_get_layer2(packet, &linktype, &remaining);
	if (!l2) {
		return NULL;
	}

	switch (linktype) {
		case TRACE_TYPE_ETH:
			return get_source_ethernet_address((libtrace_ether_t*)l2, addr);
		default:
			return NULL;
	}
}

DLLEXPORT struct sockaddr *trace_get_source_address(
		const libtrace_packet_t *packet, struct sockaddr *addr)
{
	uint16_t ethertype;
	uint32_t remaining;
	void *l3;
	struct ports_t *ports;
	static struct sockaddr_storage dummy;

	if (!addr)
		addr=(struct sockaddr*)&dummy;

	l3 = trace_get_layer3(packet,&ethertype,&remaining);

	if (!l3)
		return get_source_l2_address(packet,addr);

	switch (ethertype) {
		case TRACE_ETHERTYPE_IP: /* IPv4 */
		{
			struct sockaddr_in *addr4=(struct sockaddr_in*)addr;
			libtrace_ip_t *ip = (libtrace_ip_t*)l3;
			ports = (struct ports_t*)
				trace_get_payload_from_ip(ip,NULL,&remaining);
			addr4->sin_family=AF_INET;
			if (ports && remaining>=sizeof(*ports))
				addr4->sin_port=ports->src;
			else
				addr4->sin_port=0;
			addr4->sin_addr=ip->ip_src;
			return addr;
		}
		case TRACE_ETHERTYPE_IPV6: /* IPv6 */
		{
			struct sockaddr_in6 *addr6=(struct sockaddr_in6*)addr;
			libtrace_ip6_t *ip6 = (libtrace_ip6_t*)l3;
			ports = (struct ports_t*)
				trace_get_payload_from_ip6(ip6,NULL,&remaining);
			addr6->sin6_family=AF_INET6;
			if (ports && remaining>=sizeof(*ports))
				addr6->sin6_port=ports->src;
			else
				addr6->sin6_port=0;
			addr6->sin6_flowinfo=0;
			addr6->sin6_addr=ip6->ip_src;
			addr6->sin6_scope_id = 0;
			return addr;
		}
		default:
			return get_source_l2_address(packet, addr);
	}
}


DLLEXPORT char *trace_get_source_address_string(
		const libtrace_packet_t *packet, char *space, int spacelen) {

	static char staticspace[INET6_ADDRSTRLEN];
	struct sockaddr_storage addr;
	struct sockaddr *addrptr;
	

	if (space == NULL || spacelen == 0) {
		space = staticspace;
		spacelen = INET6_ADDRSTRLEN;
	}

	addrptr = trace_get_source_address(packet, (struct sockaddr *)&addr);

	if (addrptr == NULL)
		return NULL;
	
	return sockaddr_to_string(addrptr, space, spacelen);
}

static struct sockaddr *get_destination_ethernet_address(
	libtrace_ether_t *ethernet, struct sockaddr *addr)
{
	static struct sockaddr_storage dummy;
#ifdef HAVE_NETPACKET_PACKET_H
/* Use linux's sockaddr_ll structure */
	struct sockaddr_ll *l2addr;
	if (addr)
		l2addr = (struct sockaddr_ll*)addr;
	else
		l2addr = (struct sockaddr_ll*)&dummy;
	
	l2addr->sll_family = AF_PACKET;
	l2addr->sll_protocol = ethernet->ether_type;
	l2addr->sll_ifindex = 0; /* Irrelevant */
	l2addr->sll_hatype = ARPHRD_ETHER; 
	l2addr->sll_pkttype = PACKET_OTHERHOST;
	l2addr->sll_halen = 6;
	memcpy(l2addr->sll_addr,ethernet->ether_dhost, 6);

	return (struct sockaddr*)l2addr;
#else
/* Use BSD's sockaddr_dl structure */
	struct sockaddr_dl *l2addr;

	if (addr)
		l2addr = (struct sockaddr_dl *)addr;
	else
		l2addr = (struct sockaddr_dl *)&dummy;
	
	l2addr->sdl_family = AF_LINK;
#if HAVE_SDL_LEN == 1
	l2addr->sdl_len = sizeof(struct sockaddr_dl);
#endif
	l2addr->sdl_index = 0; /* Unused */
	l2addr->sdl_alen = 6; /* Address length  */
	l2addr->sdl_nlen = 0; /* No name in here - this *should* work, right? */
	l2addr->sdl_slen = 0;	
	l2addr->sdl_type = 0; /* Hopefully zero is OK for this value too */
	memcpy(l2addr->sdl_data, ethernet->ether_dhost, 6);

	return (struct sockaddr *)l2addr;
#endif
}

static struct sockaddr *get_destination_l2_address(
	const libtrace_packet_t *packet, struct sockaddr *addr)
{
	static struct sockaddr_storage dummy;
	void *l2;
	libtrace_linktype_t linktype;
	uint32_t remaining;
	if (!addr)
		addr =(struct sockaddr*)&dummy;
	l2=trace_get_layer2(packet, &linktype, &remaining);
	if (!l2)
		return NULL;

	switch (linktype) {
		case TRACE_TYPE_ETH:
			return get_destination_ethernet_address((libtrace_ether_t*)l2, addr);
		default:
			return NULL;
	}
}

DLLEXPORT struct sockaddr *trace_get_destination_address(
		const libtrace_packet_t *packet, struct sockaddr *addr)
{
	uint16_t ethertype;
	uint32_t remaining;
	void *l3;
	struct ports_t *ports;
	static struct sockaddr_storage dummy;

	if (!addr)
		addr=(struct sockaddr*)&dummy;

	l3 = trace_get_layer3(packet,&ethertype,&remaining);

	if (!l3)
		return get_destination_l2_address(packet,addr);

	switch (ethertype) {
		case TRACE_ETHERTYPE_IP: /* IPv4 */
		{
			struct sockaddr_in *addr4=(struct sockaddr_in*)addr;
			libtrace_ip_t *ip = (libtrace_ip_t*)l3;
			ports = (struct ports_t*)
				trace_get_payload_from_ip(ip,NULL,&remaining);
			addr4->sin_family=AF_INET;
			if (ports && remaining>=sizeof(*ports))
				addr4->sin_port=ports->dst;
			else
				addr4->sin_port=0;
			addr4->sin_addr=ip->ip_dst;
			return addr;
		}
		case TRACE_ETHERTYPE_IPV6: /* IPv6 */
		{
			struct sockaddr_in6 *addr6=(struct sockaddr_in6*)addr;
			libtrace_ip6_t *ip6 = (libtrace_ip6_t*)l3;
			ports = (struct ports_t*)
				trace_get_payload_from_ip6(ip6,NULL,&remaining);
			addr6->sin6_family=AF_INET6;
			if (ports && remaining>=sizeof(*ports))
				addr6->sin6_port=ports->dst;
			else
				addr6->sin6_port=0;
			addr6->sin6_flowinfo=0;
			addr6->sin6_addr=ip6->ip_dst;
			return addr;
		}
		default:
			return get_destination_l2_address(packet, addr);
	}
}

DLLEXPORT char *trace_get_destination_address_string(
		const libtrace_packet_t *packet, char *space, int spacelen) {

	struct sockaddr_storage addr;
	struct sockaddr *addrptr;
	
	static char staticspace[INET6_ADDRSTRLEN];

	if (space == NULL || spacelen == 0) {
		space = staticspace;
		spacelen = INET6_ADDRSTRLEN;
	}

	addrptr = trace_get_destination_address(packet, 
			(struct sockaddr *)&addr);

	if (addrptr == NULL)
		return NULL;
	
	return sockaddr_to_string(addrptr, space, spacelen);
}

DLLEXPORT uint16_t *trace_checksum_layer3(libtrace_packet_t *packet, 
		uint16_t *csum) {

	void *l3;
	uint16_t ethertype;
	uint32_t remaining;
	uint16_t *csum_ptr;

	uint8_t safety[65536];

	if (csum == NULL)
		return NULL;
	
	l3 = trace_get_layer3(packet, &ethertype, &remaining);
		
	if (l3 == NULL)
		return NULL;
	
	if (ethertype == TRACE_ETHERTYPE_IP) {
		libtrace_ip_t *ip = (libtrace_ip_t *)l3;
		if (remaining < sizeof(libtrace_ip_t))
			return NULL;

		csum_ptr = &ip->ip_sum;

		/* I hate memcpys, but this is the only truly safe way to
		 * do this without modifying the packet. I'm trying to be
		 * careful about not creating any more thread-safety issues
		 * than there already are :) */
		memcpy(safety, ip, ip->ip_hl * sizeof(uint32_t));
		
		/* Set the checksum to zero, so we can do the calculation */
		ip = (libtrace_ip_t *)safety;
		ip->ip_sum = 0;

		*csum = checksum_buffer(safety, ip->ip_hl * sizeof(uint32_t));
		
		/* Remember to byteswap appropriately */
		*csum = ntohs(*csum);
		
		return csum_ptr;
	}

	return NULL;
}

DLLEXPORT uint16_t trace_get_fragment_offset(const libtrace_packet_t *packet, 
                uint8_t *more) {

        void *l3;
        uint16_t ethertype;
        uint32_t remaining;

        *more = 0;

        l3 = trace_get_layer3(packet, &ethertype, &remaining);
        if (l3 == NULL)
                return 0;

        if (ethertype == TRACE_ETHERTYPE_IP) {
                libtrace_ip_t *ip = (libtrace_ip_t *)l3;
                uint16_t offset = 0;

                /* Fragment offset appears in 7th and 8th bytes */
                if (remaining < 8)
                        return 0;
                 
                offset = ntohs(ip->ip_off);

                if ((offset & 0x2000) != 0)
                        *more = 1;
                return (offset & 0x1FFF) * 8;
        }

        if (ethertype == TRACE_ETHERTYPE_IPV6) {
                libtrace_ip6_t *ip6 = (libtrace_ip6_t *)l3;
                void *payload = ip6++;
                uint8_t nxt = ip6->nxt;
                uint16_t len;
                
                /* First task, find a Fragment header if present */
                if (remaining < sizeof(libtrace_ip6_t))
                        return 0;
                remaining -= sizeof(libtrace_ip6_t);

                /* Adapted from trace_get_payload_from_ip6 */
                while (1) {
                        switch (nxt) {
                        case 0:
                        case TRACE_IPPROTO_ROUTING:
			case TRACE_IPPROTO_AH: 
			case TRACE_IPPROTO_DSTOPTS: 
                        {

				/* Length does not include the first 8 bytes */
				len=((libtrace_ip6_ext_t*)payload)->len * 8;
				len += 8;

			        if (remaining < len) {
                                        /* Snap too short */
                                        return 0;
                                }
                                remaining-=len;

				nxt=((libtrace_ip6_ext_t*)payload)->nxt;
				continue;
			}
			case TRACE_IPPROTO_FRAGMENT:
                        {
                                libtrace_ip6_frag_t *frag = (libtrace_ip6_frag_t *)payload;
                                uint16_t offset;
                                len = sizeof(libtrace_ip6_frag_t);
                                if (remaining < len) {
                                        /* Snap too short */
                                        return 0;
                                }
                                remaining-=len;

                                offset = ntohs(frag->frag_off);
                                if ((offset & 0x0001) != 0) 
                                        *more = 1;

                                return ((offset & 0xFFF8) >> 3) * 8;
                         }
                         default:
                                return 0;
                         }
                }

        }
        return 0;
}