~ubuntu-branches/ubuntu/trusty/cdpr/trusty

« back to all changes in this revision

Viewing changes to cdpr.c

  • Committer: Bazaar Package Importer
  • Author(s): Matt Zagrabelny
  • Date: 2011-05-05 17:42:51 UTC
  • mfrom: (1.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110505174251-jmbtcq1iptt0wkob
Tags: 2.4-1
* New upstream release, closes: #570916
* Bumped Standards-Version to 3.9.2
* Added ${misc:Depends} to binary depends
* Added a Homepage entry to the source fields
* Checked cdpr into git
* Upstream fixed blocking interface, closes: #566983
* Added in patch to remove pseudo device "any" from list of interfaces,
  closes: #527245
* Updated man page. '--help' and '-?' are invalid options and hence removed
* Updated man page. Added '-r' for read a pcap-file
* Updated copyright file to use
  http://svn.debian.org/wsvn/dep/web/deps/dep5.mdwn?op=file&rev=174
* Added watch file
* Added perl cgi and php server examples
* Added example cdpr config file, for server usage
* Bumped debhelper compat to v8

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
* cdpr - Cisco Discovery Protocol Reporter
3
 
* Copyright (c) 2002-2006 MonkeyMental.com
 
3
* Copyright (c) 2002-2010 MonkeyMental.com
4
4
*
5
5
* This program will show you which Cisco device your machine is
6
6
* connected to based on CDP packets received.
58
58
* 2.1.0 LO      03-11-08        Release - Got timeout code working on WIN32 port
59
59
* 2.2.0 LO      04-04-14        Added the ability specify the port to send the cdpr updates to.
60
60
* 2.2.1 LO      06-07-28        Fixed segfault when not providing port to a command line selected server (-s)
 
61
* 2.3.0 LO      09-??-??        Decode IPv6 addresses
 
62
* 2.4.0 LO      10-02-15        Add ioctl to OSX to disable buffers, add ability to read from file (-r)
61
63
*/
62
64
 
63
65
/*#include "pcap.h" */
65
67
#include <stdlib.h>
66
68
#include <string.h>
67
69
#include <time.h>
 
70
#include <sys/socket.h>
 
71
#include <arpa/inet.h>
 
72
/* apple stuff */
 
73
#if defined(__APPLE_CC__) || defined(__APPLE__)
 
74
#include <sys/ioctl.h>
 
75
#include <net/bpf.h>
 
76
#endif
 
77
/* end apple stuff */
68
78
#ifdef WIN32
69
79
#include "xgetopt.h"
70
80
#include "time.h"
83
93
        char switch_ip[100];
84
94
        if(cdprs>=1)
85
95
        {
86
 
                sprintf (switch_ip, "switch_ip=%d.%d.%d.%d",
 
96
                sprintf (switch_ip, "switch_ip=%d.%d.%d.%d&",
87
97
                        (int) ip[0], (int) ip[1], (int) ip[2], (int) ip[3]);
88
98
                cdprs_action(CDPRS_DATA, switch_ip, 0);
89
99
        }
92
102
}
93
103
 
94
104
void
 
105
dump_ipv6 (const u_char *ip)
 
106
{
 
107
    char ipv6_buf[INET6_ADDRSTRLEN];
 
108
    struct in6_addr addr;
 
109
    register const char *p_ipv6;
 
110
    char switch_ip6[600];
 
111
 
 
112
    memcpy(&addr, ip, sizeof(addr));
 
113
    p_ipv6 = inet_ntop(AF_INET6, &addr, ipv6_buf, sizeof(ipv6_buf));
 
114
    printf("%s", ipv6_buf);
 
115
    if(cdprs>=1)
 
116
    {
 
117
        sprintf(switch_ip6, "switch_ip6=%s&", ipv6_buf);
 
118
        cdprs_action(CDPRS_DATA, switch_ip6, 0);
 
119
    }
 
120
}
 
121
 
 
122
void
95
123
dump_hex (const u_char *p, int len)
96
124
{
97
125
        while (len--)
184
212
                }
185
213
                if (protocol_len == 1 && *protocol_val == 0xCC && address_len == 4)
186
214
                        dump_ip (address_val);
 
215
//        else if (protocol_len == 8 && *protocol_val == "0xAAAA0300000086DD" && address_len == 16)
 
216
        else if (protocol_len == 8 && address_len == 16)
 
217
                        dump_ipv6 (address_val);
187
218
                else
188
219
                        dump_hex (address_val, address_len);
189
220
                printf ("\n");
272
303
                                char port[1024];
273
304
                                char *portval;
274
305
                                int portlen;
275
 
                                portval = urlencode(v, strlen(v), &portlen);
276
 
                                sprintf(port, "&port=%.*s", portlen, portval);
 
306
                                portval = urlencode((char *)v, strlen((char *)v), &portlen);
 
307
                                sprintf(port, "port=%.*s", portlen, portval);
277
308
                                free(portval);
278
309
                                cdprs_action(CDPRS_DATA, port, verbose);
279
310
                        }
449
480
        puts("h: Print this usage");
450
481
        puts("t: time in seconds to abort waiting for a packet (should be > 60)");
451
482
        puts("v[vv]: Set verbose mode");
 
483
        puts("r: read from file instead of network device");
452
484
        puts("\n** Options dealing with server updates: **");
453
485
        puts(" u: Send cdpr information to a cdpr server\n    requires config file as arg");
454
486
        puts(" l: Location/description of this port for use with -u or -s and -p");
469
501
        char *name = NULL;
470
502
        char *svr = NULL;
471
503
        char *path = NULL;
 
504
        const char *fname = NULL;
 
505
        int rff = 0;
472
506
        char errbuf[PCAP_ERRBUF_SIZE];
473
507
        struct bpf_program filter;
474
508
        /*
484
518
        bpf_u_int32 mask;
485
519
        bpf_u_int32 net;
486
520
        struct pcap_pkthdr header;
487
 
        const u_char *packet;
488
 
        char version[] = "2.2.1";
 
521
        const u_char *packet = NULL;
 
522
        char version[] = "2.4";
489
523
 
490
524
        int c;
491
525
        int verbose=0;
503
537
 
504
538
        /* Print out header */
505
539
        printf("cdpr - Cisco Discovery Protocol Reporter\nVersion %s\n", version);
506
 
        printf("Copyright (c) 2002-2006 - MonkeyMental.com\n\n");
 
540
        printf("Copyright (c) 2002-2010 - MonkeyMental.com\n\n");
507
541
 
508
542
        /* Check command-line options */
509
 
        while((c = getopt(argc, argv, "d:t:vhu:l:n:s:p:")) !=EOF)
 
543
        while((c = getopt(argc, argv, "d:t:vhu:l:n:s:p:r:")) !=EOF)
510
544
                switch(c)
511
545
                {
512
546
                        case 'd':
562
596
                                        cdprs_cmdline++;
563
597
                                }
564
598
                                break;
 
599
                        case 'r':
 
600
                                fname = optarg;
 
601
                                rff = 1;
 
602
                                break;
565
603
                        case 'h':
566
604
                        case '?':
567
605
                                usage();
585
623
        }
586
624
 
587
625
        /* Get a pcap capable device */
588
 
        if(dev == NULL)
 
626
        if((dev == NULL) && (rff == 0))
589
627
        {
590
628
                int i = 0;
591
629
                int inum;
602
640
                /* Print the list */
603
641
                for(d=alldevs; d; d=d->next)
604
642
                {
605
 
                        printf("%d. %s", ++i, d->name);
606
 
                        if (d->description)
607
 
                                printf(" (%s)\n", d->description);
608
 
                        else
609
 
                                printf(" (No description available)\n");
 
643
                        if (strcmp(d->name, "any") != 0)
 
644
                        {
 
645
                                printf("%d. %s", ++i, d->name);
 
646
                                if (d->description)
 
647
                                        printf(" (%s)\n", d->description);
 
648
                                else
 
649
                                        printf(" (No description available)\n");
 
650
                        }
610
651
                }
611
652
                
612
653
                if(i==0)
627
668
                }
628
669
                
629
670
                /* Jump to the selected adapter */
630
 
                for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++)
 
671
                for(d=alldevs, i=0; d; d=d->next)
631
672
                {
632
 
                        ;
 
673
                        if (strcmp(d->name, "any") != 0)
 
674
                        {
 
675
                                i++;
 
676
                                if (i == inum) break;
 
677
                        }
633
678
                }
634
679
                
635
680
                dev = d->name;
636
681
        }
637
682
 
638
 
        printf("Using Device: %s\n", dev);
639
683
 
640
684
        /* Get the network number and netmask */
641
685
        pcap_lookupnet(dev, &net, &mask, errbuf);
642
686
 
643
687
        /* Open the pcap device */
644
 
        if((handle = pcap_open_live(dev, BUFSIZ, 1, 0, errbuf)) == NULL)
645
 
        {
646
 
                printf("Error opening device (%s)\n", errbuf);
647
 
                exit(1);
648
 
        }
649
 
        else if (strlen(errbuf))
 
688
        if(rff == 0)
 
689
        {
 
690
                printf("Using Device: %s\n", dev);
 
691
                if((handle = pcap_open_live(dev, BUFSIZ, 1, 1000, errbuf)) == NULL)
 
692
                {
 
693
                        printf("Error opening device (%s)\n", errbuf);
 
694
                        exit(1);
 
695
                }
 
696
#if defined(__APPLE_CC__) || defined(__APPLE__)
 
697
              int v = 1;
 
698
              ioctl(pcap_fileno(handle), BIOCIMMEDIATE, &v);
 
699
#endif
 
700
 
 
701
        }
 
702
        else
 
703
        {
 
704
                printf("Using File %s\n", fname);
 
705
                if((handle = pcap_open_offline(fname, errbuf)) == NULL)
 
706
                {
 
707
                        printf("Error opening file (%s)\n", fname);
 
708
                        exit(1);
 
709
                }
 
710
        }
 
711
        if (strlen(errbuf))
650
712
        {
651
713
                printf("Warning opening device (%s)\n", errbuf);
652
714
        }
678
740
#else
679
741
                usleep(10000);
680
742
#endif
681
 
        } while ((!packet) && ( timeout=((start_time+seconds) > (unsigned int)time(NULL))) );
 
743
        } while ((packet == NULL) && ( timeout=((start_time+seconds) > (unsigned int)time(NULL))) );
682
744
 
683
745
        /*
684
746
        ** timeout expired. clean up and exit