File: cdpr.c

package info (click to toggle)
cdpr 2.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 292 kB
  • sloc: ansic: 1,074; perl: 72; makefile: 23; php: 23
file content (795 lines) | stat: -rw-r--r-- 18,921 bytes parent folder | download
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
863
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
/*
* cdpr - Cisco Discovery Protocol Reporter
* Copyright (c) 2002-2010 MonkeyMental.com
*
* This program will show you which Cisco device your machine is
* connected to based on CDP packets received.
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*
* Version History:
*
* 1.0.0	LO	02-06-30	Initial Release
* 1.0.1	LO	02-07-01	ifdef/endif DLT types to deal with older bpf.h headers
* 1.0.2 LO	02-07-02	New filter to better identify CDP packets
* 1.0.3 LO	02-07-03	loop on pcap_next until a valid packet is received.
*						(patch provided Martin Buck <[email protected]>)
* 1.0.4	LO	02-07-10	Added 1ms timeout to pcap_open_live to fix *BSD
* 1.0.5	LO	02-07-14	Copy packet data to local struct to resolve Bus Errors
*						on Sun machines.
* 1.0.6	LO	02-07-15	More alignment fixes.
* 1.0.7	LO	02-10-29	Port to Win32, autodetection and list generation of
*						PCAP capable devices.
* 1.0.8	LO	03-02-13	Port to arm processor (Zaurus) using a filter specific
* 						to arm processors to work around pcap bug
* 1.1.0	LO	03-04-25	Add central server reporting code
* 1.1.1	LO	03-04-26	Fix compile errors for Win32 on server code
* 1.1.2	LO	03-04-27	Add config file support
* 1.1.3	LO	03-04-28	Add location/description flag for extended info on server
*						Split server code and conf file code into seperate source files
*						Cleanup old server/config file code. Remove cond. compile for CDPRS
*						Create cdpr.h for ext. functions and common defines.
*						Add DNS support to the config file so you can specify a hostname
* 1.1.4	LO	03-06-20	Added -n flag to override hostname sent to server. Added error
*						checking to the socket code.
* 1.1.5	LO	03-06-23	Fix bug where hostname would not be transmitted w/o location set
* 2.0.0	LO	03-06-25	Release - Major revision change due to server reporting code
* 2.0.1	LO	03-07-01	Add sys/types.h to the includes in conffile.c for BSD support
* 2.0.2 LO	03-08-03	Enable timeouts so that cdpr won't wait forever looking for a packet
* 2.0.3	LO	03-08-04	Better cleanup after aborting due to timeout. 
* 2.0.4	LO	03-08-05	Timeout code that actually timesout. Set device to nonblocking
*						mode and loop until packet or timeout. Before, pcap_next would
*						block so the signal handler couldn't acutally exit.
* 2.0.5	LO	03-10-22	Went back to pcap_next() to get the packet, add a default timeout
*						of 5 minutes. Got rid of pktcap.c and timeout.c as they are no
*						longer needed.
* 2.1.0	LO	03-11-08	Release - Got timeout code working on WIN32 port
* 2.2.0	LO	04-04-14	Added the ability specify the port to send the cdpr updates to.
* 2.2.1 LO	06-07-28	Fixed segfault when not providing port to a command line selected server (-s)
* 2.3.0 LO	09-??-??	Decode IPv6 addresses
* 2.4.0 LO	10-02-15	Add ioctl to OSX to disable buffers, add ability to read from file (-r)
*/

/*#include "pcap.h" */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/socket.h>
#include <arpa/inet.h>
/* apple stuff */
#if defined(__APPLE_CC__) || defined(__APPLE__)
#include <sys/ioctl.h>
#include <net/bpf.h>
#endif
/* end apple stuff */
#ifdef WIN32
#include "xgetopt.h"
#include "time.h"
#else
#include <unistd.h>
#endif
#ifdef SOLARIS
#include "u_ints.h"
#endif
#include "cdpr.h"
#include "cdp.h"

/*
** Global variables
*/
int timeout;
int cdprs;
pcap_t *handle;

void
dump_ip (const u_char *ip)
{
	char switch_ip[100];
	if(cdprs>=1)
	{
		sprintf (switch_ip, "switch_ip=%d.%d.%d.%d&",
			(int) ip[0], (int) ip[1], (int) ip[2], (int) ip[3]);
		cdprs_action(CDPRS_DATA, switch_ip, 0);
	}
	printf ("%d.%d.%d.%d",
		(int) ip[0], (int) ip[1], (int) ip[2], (int) ip[3]);
}

void
dump_ipv6 (const u_char *ip)
{
    char ipv6_buf[INET6_ADDRSTRLEN];
    struct in6_addr addr;
    register const char *p_ipv6;
    char switch_ip6[600];

    memcpy(&addr, ip, sizeof(addr));
    p_ipv6 = inet_ntop(AF_INET6, &addr, ipv6_buf, sizeof(ipv6_buf));
    printf("%s", ipv6_buf);
    if(cdprs>=1)
    {
        sprintf(switch_ip6, "switch_ip6=%s&", ipv6_buf);
        cdprs_action(CDPRS_DATA, switch_ip6, 0);
    }
}

void
dump_hex (const u_char *p, int len)
{
	while (len--)
	{
		printf ("X ", *p  );
	}
}

void
dump_ascii (const u_char *p, int len)
{
	while (len--)
	{
		printf ("%c", (*p < ' ' || *p > '~') ? '.' : *p);
		  p;
	}
}

void
dump_data (const u_char *p, int len)
{
	int i;

	for (i = 0; i < len; i  = 16, p  = 16)
	{
		printf ("%p:  ", p);
		dump_hex (p, 16);
		printf (" ");
		dump_ascii (p, 16);
		printf ("\n");
	}
}
char *
get_cdp_type (int type)
{
    int i;

    for (i = 0; type_vals[i].type != 0;   i)
    {
    	if (type == type_vals[i].type)
    	{
    		return type_vals[i].val;
    	}
    }
    return "Unknown type";
}

void
print_cdp_address (u_char *v, int verbose)
{
	u_int32_t i;
	u_int32_t number;

	memcpy (&number, v, sizeof (number));
	number = ntohl (number);

	if (verbose > 0)
	{
		printf ("  number: %d\n", number);
	}

	v  = sizeof (u_int32_t);
	for (i = 0; i < number;   i)
	{
		u_char protocol      = *v;
		u_char protocol_len  = *(v 1);
		u_char *protocol_val = v 2;
/*		int    address_len   = ntohs(*((u_int16_t *)(v 2 protocol_len)));*/
		u_int16_t address_len;
		u_char *address_val  = v 2 protocol_len 2;

		memcpy (&address_len, (v 2 protocol_len), sizeof(address_len));
		address_len = ntohs(address_len);

		
		if (verbose > 0)
		{
			printf ("  protocol: x\n", protocol);
			printf ("  protocol len: x\n", protocol_len);
			printf ("  protocol val: ");
			dump_hex (protocol_val, protocol_len);
			printf ("\n");
		
			printf ("  address len: x\n", address_len);
			printf ("  address val: ");
		}
		else
		{
			printf ("  value:  ");
		}
		if (protocol_len == 1 && *protocol_val == 0xCC && address_len == 4)
			dump_ip (address_val);
//        else if (protocol_len == 8 && *protocol_val == "0xAAAA0300000086DD" && address_len == 16)
        else if (protocol_len == 8 && address_len == 16)
			dump_ipv6 (address_val);
		else
			dump_hex (address_val, address_len);
		printf ("\n");

		v  = (2 protocol_len 2 address_len);
	}
}

void
print_cdp_capabilities (u_char *v)
{
	u_int32_t cap;

	memcpy (&cap, v, sizeof (cap));
	cap = ntohl (cap);

	printf ("  value:  x\n", cap);
	if (cap & 0x01) printf ("          Performs level 3 routing for at least one network layer protocol.\n");
	if (cap & 0x02) printf ("          Performs level 2 transparent bridging.\n");
	if (cap & 0x04) printf ("          Performs level 2 source-route bridging.\n");
	if (cap & 0x08) printf ("          Performs level 2 switching.\n");
	if (cap & 0x10) printf ("          Sends and receives packets for at least one network layer protocol.\n");
	if (cap & 0x20) printf ("          The bridge or switch does not forward IGMP Report packets on nonrouter ports.\n");
	if (cap & 0x40) printf ("          Provides level 1 functionality.\n");
}

void
print_cdp_packet (const u_char *p, unsigned int plen, int verbose)
{
	CDP_HDR *h;
	CDP_DATA *d;

	h = (CDP_HDR *) p;


	// dump_data (p, 128);

	if (verbose > 1 )
	{
		printf ("\ncdp packet:\n");
		printf ("  version:      x\n", h->version);
		printf ("  time to live: x\n", h->time_to_live);
		printf ("  checksum:     x\n", ntohs (h->checksum));
	}

	d = (CDP_DATA *) (p   sizeof (CDP_HDR));
	plen -= sizeof (CDP_HDR);

	while (plen > sizeof (CDP_DATA))
	{
		int type;
		int length;
		u_char *v;  /* variable data */
		int vlen;   /* length of variable data */
		CDP_DATA data;

		memcpy (&data, d, sizeof (CDP_DATA));
		type = ntohs (data.type);
		length = ntohs (data.length);
		v = (u_char *) d   sizeof (CDP_DATA);
		vlen = length - sizeof (CDP_DATA);
	
		if(verbose > 0 )
		{
            printf ("\ncdp type/len/val:\n");
			printf ("  type:   x - %s\n", type, get_cdp_type (type));
			printf ("  length: x\n", length);
		}
		switch (type)
		{
		case TYPE_DEVICE_ID:
			printf ("%s\n", get_cdp_type (type));
			printf ("  value:  %.*s\n", vlen, v);
			break;

		case TYPE_ADDRESS:
			printf ("%s\n", get_cdp_type (type));
			print_cdp_address (v, verbose);
			break;

		case TYPE_PORT_ID:
			printf ("%s\n", get_cdp_type (type));
			printf ("  value:  %.*s\n", vlen, v);
			if(cdprs >= 1)
			{
				char port[1024];
				char *portval;
				int portlen;
				portval = urlencode((char *)v, strlen((char *)v), &portlen);
				sprintf(port, "port=%.*s", portlen, portval);
				free(portval);
				cdprs_action(CDPRS_DATA, port, verbose);
			}
			break;

		case TYPE_CAPABILITIES:
			if(verbose > 0)
			{
				printf ("%s\n", get_cdp_type (type));
				print_cdp_capabilities (v);
			}
			break;

		case TYPE_IOS_VERSION:
			if(verbose > 0)
			{
				printf ("%s\n", get_cdp_type (type));
				printf ("  value:  %.*s\n", vlen, v);
			}
			break;

		case TYPE_PLATFORM:
			if(verbose > 0)
			{
				printf ("%s\n", get_cdp_type (type));
				printf ("  value:  %.*s\n", vlen, v);
			}
			break;

		case TYPE_IP_PREFIX:
			if(verbose > 0)
			{
				printf ("%s\n", get_cdp_type (type));
				printf ("  value:  ");
				dump_hex (v, vlen);
				printf ("\n");
			}
			break;

		case TYPE_VTP_MGMT_DOMAIN:
			if(verbose > 0)
			{
				printf ("%s\n", get_cdp_type (type));
				printf ("  value:  %.*s\n", vlen, v);
			}
			break;

		case TYPE_NATIVE_VLAN:
			if(verbose > 0)
			{
				printf ("%s\n", get_cdp_type (type));
				printf ("  value:  ");
				dump_hex (v, vlen);
				printf ("\n");
			}
			break;
		
		case TYPE_DUPLEX:
			if(verbose > 0)
			{
				printf ("%s\n", get_cdp_type (type));
				printf ("  value:  ");
				dump_hex (v, vlen);
				printf ("\n");
			}
			break;

		default:
			if(verbose > 1)
			{
				printf ("%s\n", get_cdp_type (type));
				printf ("  value:  ");
				dump_hex (v, vlen);
				printf ("\n");
			}
		}

	    plen -= length;
	    d = (CDP_DATA *) ((u_char *) d   length);
	}
}


int
print_dlt(pcap_t *handle)
{
	int type;

	/* Print the DLL Type*/
	switch(type = pcap_datalink(handle))
	{
#ifdef DLT_NULL
		case DLT_NULL:
			printf("Data Link Type: BSD Loopback encapsulation.\n");
			break;
#endif
#ifdef DLT_EN10MB
		case DLT_EN10MB:
			printf("Data Link Type: Ethernet (10MB, 100MB, 1000MB and up).\n");
			break;
#endif
#ifdef DLT_IEEE802
		case DLT_IEEE802:
			printf("Data Link Type: IEEE 802.5 Token Ring.\n");
			break;
#endif
#ifdef DLT_ARCNET
		case DLT_ARCNET:
			printf("Data Link Type: ARCNET.\n");
			break;
#endif
#ifdef DLT_PPP
		case DLT_PPP:
			printf("Data Link Type: PPP (Point-to-Point Protocol).\n");
			break;
#endif
#ifdef DLT_FDDI
		case DLT_FDDI:
			printf("Data Link Type: FDDI.\n");
			break;
#endif
#ifdef DLT_ATM_RFC1483
		case DLT_ATM_RFC1483:
			printf("Data Link Type: RFC 1483 LLC/SNAP-encapsulated ATM.\n");
			break;
#endif
#ifdef DLT_RAW
		case DLT_RAW:
			printf("Data Link Type: raw IP.\n");
			break;
#endif
#ifdef DLT_PPP_SERIAL
		case DLT_PPP_SERIAL:
			printf("Data Link Type: PPP in HDLC-like framing.\n");
			break;
#endif
#ifdef DLT_PPP_ETHER
		case DLT_PPP_ETHER:
			printf("Data Link Type: PPPoE.\n");
			break;
#endif
#ifdef DLT_C_HDLC
		case DLT_C_HDLC:
			printf("Data Link Type: Cisco PPP with HDLC framing.\n");
			break;
#endif
#ifdef DLT_IEEE802_11
		case DLT_IEEE802_11:
			printf("Data Link Type: IEEE 802.11 wireless LAN.\n");
			break;
#endif
#ifdef DLT_LOOP
		case DLT_LOOP:
			printf("Data Link Type: OpenBSD loopback encapsulation.\n");
			break;
#endif
#ifdef DLT_LTALK
		case DLT_LTALK:
			printf("Data Link Type: Apple LocalTalk.\n");
			break;
#endif
		default:
			printf("%d is an unknown Data Link Transport\n", type);
	}

	return 0;
}

int
usage(void)
{
	puts("d: Specify device to use (eth0, hme0, etc.)");
	puts("h: Print this usage");
	puts("t: time in seconds to abort waiting for a packet (should be > 60)");
	puts("v[vv]: Set verbose mode");
	puts("r: read from file instead of network device");
	puts("\n** Options dealing with server updates: **");
	puts(" u: Send cdpr information to a cdpr server\n    requires config file as arg");
	puts(" l: Location/description of this port for use with -u or -s and -p");
	puts(" n: Override the hostname reported to the server for use with -u or -s and -p");
	puts(" s: Server to send information to specify port with a : after Server/IP\n     Example: 192.168.1.20:88 (default: 80) requires -p (overridden by -u)");
	puts(" p: Path of server script to send data to requires -s (overridden by -u)");

	exit(0);
}

int
main(int argc, char *argv[])
{
	pcap_t *handle;
	char *dev = NULL;
	char *conf = NULL;
	char *loc = NULL;
	char *name = NULL;
	char *svr = NULL;
	char *path = NULL;
	const char *fname = NULL;
	int rff = 0;
	char errbuf[PCAP_ERRBUF_SIZE];
	struct bpf_program filter;
	/*
	** Filter Expression: 01:00:0c:cc:cc:cc Multicast Mac Address
	** This filter doesn't work on the arm, so use all multicast
	** ether[20:2] = 0x2000: CDP signature in LLC
	*/
#ifdef ZAURUS
	char filter_app[] = "ether multicast and ether[20:2] = 0x2000";
#else
	char filter_app[] = "ether host 01:00:0c:cc:cc:cc and ether[20:2] = 0x2000";
#endif
	bpf_u_int32 mask;
	bpf_u_int32 net;
	struct pcap_pkthdr header;
	const u_char *packet = NULL;
	char version[] = "2.4";

	int c;
	int verbose=0;
	int locdetail=0;
	int nameoverride=0;
	int cdprs_config=0;
	int cdprs_cmdline=0;
	unsigned int seconds=300;
	time_t start_time=0;

	/* Zero out some global variables */
	timeout=1;
	cdprs=0;
	memset (errbuf, 0, sizeof (errbuf));

	/* Print out header */
	printf("cdpr - Cisco Discovery Protocol Reporter\nVersion %s\n", version);
	printf("Copyright (c) 2002-2010 - MonkeyMental.com\n\n");

	/* Check command-line options */
	while((c = getopt(argc, argv, "d:t:vhu:l:n:s:p:r:")) !=EOF)
		switch(c)
		{
			case 'd':
				dev = optarg;
				break;
			case 'v':
				verbose  ;
				break;
			case 't':
				seconds = atoi(optarg);
				printf("Timeout enabled for %u seconds\n", seconds);
				break;
			case 'u':
				conf = optarg;
				cdprs = 1;
				cdprs_config=1;
				if(cdprs_cmdline)
				{
					puts("Overriding command line server arguments with config file\n");
					cdprs_cmdline = 0;
				}
				cdprs_action(CDPRS_INIT, conf, 0);
				break;
			case 'l':
				loc = optarg;
				locdetail = 1;
				break;
			case 'n':
				name = optarg;
				nameoverride = 1;
				break;
			case 's':
				svr = optarg;
				if(cdprs_config)
				{
					puts("Config file overrides cmd line");
				}
				else
				{
					if(cdprs != 1) cdprs = 1;
					cdprs_cmdline  ;
				}
				break;
			case 'p':
				path = optarg;
				if(cdprs_config)
				{
					puts("Config file overrides cmd line");
				}
				else
				{
					if(cdprs != 1) cdprs = 1;
					cdprs_cmdline  ;
				}
				break;
			case 'r':
				fname = optarg;
				rff = 1;
				break;
			case 'h':
			case '?':
				usage();
				break;
		}

	/*
	** If we are using command line server paths, validate and buld
	** build data structure using functions that are normally used
	** when parsing the config file
	*/
	if(cdprs && (cdprs_cmdline == 2))
	{
		cdprs_action(CDPRS_INIT, "", 0);
		do_something_with(svr, path);
	}
	else if(cdprs && (cdprs_cmdline != 0))
	{
		puts("Error only -s or -p was specified but not both");
		exit(2);
	}

	/* Get a pcap capable device */
	if((dev == NULL) && (rff == 0))
	{
		int i = 0;
		int inum;
		pcap_if_t *d;
		pcap_if_t *alldevs;

		/* The user didn't provide a packet source: Retrieve the device list */
		if (pcap_findalldevs(&alldevs, errbuf) == -1)
		{
			fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
			exit(1);
		}
		
		/* Print the list */
		for(d=alldevs; d; d=d->next)
		{
			if (strcmp(d->name, "any") != 0)
			{
				printf("%d. %s",   i, d->name);
				if (d->description)
					printf(" (%s)\n", d->description);
				else
					printf(" (No description available)\n");
			}
		}
		
		if(i==0)
		{
			printf("\nNo interfaces found! Make sure pcap is installed.\n");
			return -1;
		}
		
		printf("Enter the interface number (1-%d):",i);
		scanf("%d", &inum);
		
		if(inum < 1 || inum > i)
		{
			printf("\nInterface number out of range.\n");
			/* Free the device list */
			pcap_freealldevs(alldevs);
			return -1;
		}
		
		/* Jump to the selected adapter */
		for(d=alldevs, i=0; d; d=d->next)
		{
			if (strcmp(d->name, "any") != 0)
			{
				i  ;
				if (i == inum) break;
			}
		}
		
		dev = d->name;
	}


	/* Get the network number and netmask */
	pcap_lookupnet(dev, &net, &mask, errbuf);

	/* Open the pcap device */
	if(rff == 0)
	{
		printf("Using Device: %s\n", dev);
		if((handle = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf)) == NULL)
		{
			printf("Error opening device (%s)\n", errbuf);
			exit(1);
		}
#if defined(__APPLE_CC__) || defined(__APPLE__)
	      int v = 1;
	      ioctl(pcap_fileno(handle), BIOCIMMEDIATE, &v);
#endif

	}
	else
	{
		printf("Using File %s\n", fname);
		if((handle = pcap_open_offline(fname, errbuf)) == NULL)
		{
			printf("Error opening file (%s)\n", fname);
			exit(1);
		}
	}
	if (strlen(errbuf))
	{
		printf("Warning opening device (%s)\n", errbuf);
	}

	/* Compile the pcap filter */
	pcap_compile(handle, &filter, filter_app, 0, net);

	/* Activate the pcap filter */
	pcap_setfilter(handle, &filter);
	pcap_freecode(&filter);

	/* Set non-blocking mode */
	if(pcap_setnonblock(handle, 1, errbuf))
	{
		pcap_perror(handle, NULL);
	}

	/* Get the next packet that comes in, we only need one */
	printf("Waiting for CDP advertisement:\n");
	printf("(default config is to transmit CDP packets every 60 seconds)\n");

	/* Get current time to check for timeout */
	start_time = time(NULL);
	do
	{
		packet = pcap_next(handle, &header);
#ifdef WIN32
		Sleep(10000);
#else
		usleep(10000);
#endif
	} while ((packet == NULL) && ( timeout=((start_time seconds) > (unsigned int)time(NULL))) );

	/*
	** timeout expired. clean up and exit
	*/
	if(timeout == 0)
	{
		puts("Aborting due to timeout");
		pcap_close(handle);
		return(2);
	}

	/* Print its length */
	if(verbose > 0)
	{
		printf("Received a CDP packet, header length: %d\n", header.len);
	}

	// print cdp packet, 22 bytes into packet
	print_cdp_packet (packet 22, header.len-22, verbose);

	if (verbose > 1)
	{
		print_dlt(handle);
	}
	
	pcap_close(handle);
	if(cdprs >=1)
	{
		if(locdetail >=1)
		{
			set_location(loc);
		}
		if(nameoverride >= 1)
		{
			get_hostname(nameoverride, name);
		}
		else
		{
			get_hostname(0, NULL);
		}

		cdprs_action(CDPRS_SEND, "", verbose);
	}
	return(0);
}