cdpr-2.4/0000755000175000017500000000000011336550214011235 5ustar lancelancecdpr-2.4/cdp.h0000644000175000017500000000435611336313073012164 0ustar lancelance/* * 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. */ #ifndef WIN32 #include #endif /* Define the constants and text values for the 'type' field: */ #define TYPE_DEVICE_ID 0x0001 #define TYPE_ADDRESS 0x0002 #define TYPE_PORT_ID 0x0003 #define TYPE_CAPABILITIES 0x0004 #define TYPE_IOS_VERSION 0x0005 #define TYPE_PLATFORM 0x0006 #define TYPE_IP_PREFIX 0x0007 #define TYPE_VTP_MGMT_DOMAIN 0x0009 #define TYPE_NATIVE_VLAN 0x000a #define TYPE_DUPLEX 0x000b struct { int type; char *val; } type_vals[] = { { TYPE_DEVICE_ID, "Device ID" }, { TYPE_ADDRESS, "Addresses" }, { TYPE_PORT_ID, "Port ID" }, { TYPE_CAPABILITIES, "Capabilities" }, { TYPE_IOS_VERSION, "Software version" }, { TYPE_PLATFORM, "Platform" }, { TYPE_IP_PREFIX, "IP Prefix (used for ODR)" }, { TYPE_VTP_MGMT_DOMAIN, "VTP Management Domain" }, { TYPE_NATIVE_VLAN, "Native VLAN" }, { TYPE_DUPLEX, "Duplex" }, { 0, NULL }, }; typedef struct _cdp_packet_header { u_int8_t version; // always one u_int8_t time_to_live; // in seconds u_int16_t checksum; // ip checksum } CDP_HDR; typedef struct _cfp_packet_data { u_int16_t type; // see TYPE_ above u_int16_t length; // total length of type/length/value // value; // variable length value } CDP_DATA; cdpr-2.4/cdpr.c0000644000175000017500000004446411336322477012355 0ustar lancelance/* * 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 ) * 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 #include #include #include #include #include /* apple stuff */ #if defined(__APPLE_CC__) || defined(__APPLE__) #include #include #endif /* end apple stuff */ #ifdef WIN32 #include "xgetopt.h" #include "time.h" #else #include #endif #ifdef SOLARIS #include "u_ints.h" #endif #include "cdpr.h" #include "cdp.h" 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 ("%02X ", *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: %02x\n", protocol); printf (" protocol len: %02x\n", protocol_len); printf (" protocol val: "); dump_hex (protocol_val, protocol_len); printf ("\n"); printf (" address len: %02x\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: %08x\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: %02x\n", h->version); printf (" time to live: %02x\n", h->time_to_live); printf (" checksum: %04x\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: %04x - %s\n", type, get_cdp_type (type)); printf (" length: %04x\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) { 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; i< inum-1 ;d=d->next, i++) { ; } 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); } cdpr-2.4/cdpr.conf0000644000175000017500000000004010462566007013034 0ustar lancelancelocalhost:88 /cgi-bin/cdprs.cgi cdpr-2.4/Cdpr.dsp0000644000175000017500000001042507764220002012636 0ustar lancelance# Microsoft Developer Studio Project File - Name="Cdpr" - Package Owner=<4> # Microsoft Developer Studio Generated Build File, Format Version 6.00 # ** DO NOT EDIT ** # TARGTYPE "Win32 (x86) Console Application" 0x0103 CFG=Cdpr - Win32 Debug !MESSAGE This is not a valid makefile. To build this project using NMAKE, !MESSAGE use the Export Makefile command and run !MESSAGE !MESSAGE NMAKE /f "Cdpr.mak". !MESSAGE !MESSAGE You can specify a configuration when running NMAKE !MESSAGE by defining the macro CFG on the command line. For example: !MESSAGE !MESSAGE NMAKE /f "Cdpr.mak" CFG="Cdpr - Win32 Debug" !MESSAGE !MESSAGE Possible choices for configuration are: !MESSAGE !MESSAGE "Cdpr - Win32 Release" (based on "Win32 (x86) Console Application") !MESSAGE "Cdpr - Win32 Debug" (based on "Win32 (x86) Console Application") !MESSAGE # Begin Project # PROP AllowPerConfigDependencies 0 # PROP Scc_ProjName "" # PROP Scc_LocalPath "" CPP=cl.exe RSC=rc.exe !IF "$(CFG)" == "Cdpr - Win32 Release" # PROP BASE Use_MFC 0 # PROP BASE Use_Debug_Libraries 0 # PROP BASE Output_Dir "Release" # PROP BASE Intermediate_Dir "Release" # PROP BASE Target_Dir "" # PROP Use_MFC 0 # PROP Use_Debug_Libraries 0 # PROP Output_Dir "Release" # PROP Intermediate_Dir "Release" # PROP Ignore_Export_Lib 0 # PROP Target_Dir "" # ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c # ADD CPP /nologo /W3 /GX /O2 /I "wpdpack\Include" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c # ADD BASE RSC /l 0x409 /d "NDEBUG" # ADD RSC /l 0x409 /d "NDEBUG" BSC32=bscmake.exe # ADD BASE BSC32 /nologo # ADD BSC32 /nologo LINK32=link.exe # ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 # ADD LINK32 ws2_32.lib wpcap.lib /nologo /subsystem:console /machine:I386 /libpath:"wpdpack\Lib" !ELSEIF "$(CFG)" == "Cdpr - Win32 Debug" # PROP BASE Use_MFC 0 # PROP BASE Use_Debug_Libraries 1 # PROP BASE Output_Dir "Debug" # PROP BASE Intermediate_Dir "Debug" # PROP BASE Target_Dir "" # PROP Use_MFC 0 # PROP Use_Debug_Libraries 1 # PROP Output_Dir "Debug" # PROP Intermediate_Dir "Debug" # PROP Ignore_Export_Lib 0 # PROP Target_Dir "" # ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c # ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "wpdpack\Include" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c # ADD BASE RSC /l 0x409 /d "_DEBUG" # ADD RSC /l 0x409 /d "_DEBUG" BSC32=bscmake.exe # ADD BASE BSC32 /nologo # ADD BSC32 /nologo LINK32=link.exe # ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept # ADD LINK32 ws2_32.lib wpcap.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept /libpath:"wpdpack\Lib" !ENDIF # Begin Target # Name "Cdpr - Win32 Release" # Name "Cdpr - Win32 Debug" # Begin Group "Source Files" # PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" # Begin Source File SOURCE=.\cdpr.c # End Source File # Begin Source File SOURCE=.\cdprs.c # End Source File # Begin Source File SOURCE=.\conffile.c # End Source File # Begin Source File SOURCE=.\XGetopt.c # End Source File # End Group # Begin Group "Header Files" # PROP Default_Filter "h;hpp;hxx;hm;inl" # Begin Source File SOURCE=.\cdp.h # End Source File # Begin Source File SOURCE=.\cdpr.h # End Source File # Begin Source File SOURCE=.\u_ints.h # End Source File # Begin Source File SOURCE=.\XGetopt.h # End Source File # End Group # Begin Group "Resource Files" # PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe" # End Group # Begin Source File SOURCE=.\wpdpack\Lib\wpcap.lib # End Source File # End Target # End Project cdpr-2.4/Cdpr.dsw0000644000175000017500000000102507764220002012641 0ustar lancelanceMicrosoft Developer Studio Workspace File, Format Version 6.00 # WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE! ############################################################################### Project: "Cdpr"=".\Cdpr.dsp" - Package Owner=<4> Package=<5> {{{ }}} Package=<4> {{{ }}} ############################################################################### Global: Package=<5> {{{ }}} Package=<3> {{{ }}} ############################################################################### cdpr-2.4/cdpr.h0000644000175000017500000000330111336312771012337 0ustar lancelance/* * 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. */ #ifndef CDPR_H #define CDPR_H /* ** Includes */ #include "pcap.h" /* ** Defines */ #define CDPRS_INIT 1 #define CDPRS_SETIP 2 #define CDPRS_SETPORT 3 #define CDPRS_DATA 4 #define CDPRS_SEND 5 struct singleton { struct pcap_pkthdr *hdr; const u_char *pkt; }; /* ** Global variables */ int timeout; int cdprs; pcap_t *handle; /* ** Function Prototypes */ int cdprs_action(int action, char *string, int verbose); void set_location(char *loc); void get_hostname(int nameoverride, char *name); void read_file(char *file); char * urlencode(char *s, int slen, int *new_len); int enable_timeout(void); int set_timeout(unsigned int seconds); const u_char * pkt_next(pcap_t *p, struct pcap_pkthdr *h); void do_something_with(char *ip, char *url); #endif cdpr-2.4/cdprs/0000755000175000017500000000000011336550227012354 5ustar lancelancecdpr-2.4/cdprs/cdprs.cgi0000755000175000017500000001073011064762706014164 0ustar lancelance#!/usr/bin/perl # # cdprs.cgi: Sample perl cgi to collect cdpr data to a file # # Copyright (c) 2003-2008 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. ####################################################### # Subroutine: ReadGetData # # On a get request, this is the function that gets the # information from the browser, and puts it into a # string that the cgi script can use. sub ReadGetData { local(*queryString) = @_ if @_; $queryString = $ENV{"QUERY_STRING"}; return 1; } ####################################################### # Subroutine: ReadPostData # # On apost request, this is the function that gets the # information from the browser, and puts in into a # string that the cgi script can use sub ReadPostData { local(*queryString) = @_; local($contentLength); $contentLength = $ENV{"CONTENT_LENGTH"}; if ($contentLength) { read(STDIN,$queryString,$contentLength); } return 1; } ####################################################### # Subroutine: DecodeData # # Since the information passed from the browser is # encrypted, it must be decoded before use. The method # for encryption is as such: # Spaces are turned into plus signs (+) # # Non-alphanumeric characters are turned into their # hex equivalent preceded by a percent sign (%) # (an apostrophe (') is %27) # # This function is called from ParseData sub DecodeData { local(*convert) = @_ if @_; # Replace all +'s with spaces $convert =~ s/\+/ /g; # Replace all hex values with the proper character $convert =~ s/%([0-9A-Fa-f]{2})/pack("c",hex($1))/ge; # Return success return 1; } ####################################################### # Subroutine: ParseData # # This function breaks the string recieved from # ReadGetData or ReadPostData into values that can # be used later in the script. It calls DecodeData # to decode the encrypted strings. sub ParseData { local(*queryString) = @_ if @_; @Array = split(/&/,$queryString); foreach $curString(@Array) { ($key, $value) = split(/=/, $curString); &DecodeData(*key); &DecodeData(*value); # To save off the values passed from the browser into known # variables, use the following format. # # if ($key eq "KeyName" && $value ne "") # { # $KeyName = $value; # } # if ($key eq "switch_ip" && $value ne "") { $switch_ip = $value; } if ($key eq "switch_ip6" && $value ne "") { $switch_ip6 = $value; } if ($key eq "port" && $value ne "") { $port = $value; } if ($key eq "host" && $value ne "") { $host = $value; } if ($key eq "loc" && $value ne "") { $loc = $value; } } return 1; } ####################################################### # Defines $logfile = ">>/tmp/cdprs.csv"; # Main() # Let the browser know what type of information we are # going to be sending back to it, common information # types are: # Content-type: text/html # Content-type: text/plain # # It is necessary to end the string with two CR/LF's # (\n\n) print "Content-type: text/plain\n\n"; ####################################################### # Find out what type of request we are processing as # get and post requests use different methods to obtain # the data. $requestType will either be "GET" or "POST" $requestType = $ENV{"REQUEST_METHOD"}; ####################################################### # This is a POST request, so use ReadPostData # ParseData is a common routine to both GET and POST # requests # Not using a post interface... if ($requestType eq "POST") { &ReadPostData(*data); &ParseData(*data); } if ($requestType eq "GET") { &ReadGetData(*data); &ParseData(*data); open(LOGFILE, $logfile); print(LOGFILE "Switch: $switch_ip, Switch6: $switch_ip6, Port: $port, Host: $host, Location: $loc\n"); close(LOGFILE); } cdpr-2.4/cdprs/cdprs.php0000644000175000017500000000272311064762674014215 0ustar lancelance cdpr-2.4/cdprs.c0000644000175000017500000001206211336313005012510 0ustar lancelance/* * 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. */ #include #include #include #if WIN32 #include "Winsock2.h" #else #include #include #include #include #include #endif #include "cdpr.h" #ifdef WIN32 void win32_socket_init() { WSADATA wsaData; if(WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) { puts("WSAStartup Failed"); exit(1); } } void win32_socket_cleanup() { WSACleanup(); } #endif void cdprs_footer(void) { char *footer=" HTTP/1.0\r\n\r\n"; cdprs_action(CDPRS_DATA, footer,1); // strcat(msg,footer); } void get_hostname(int nameoverride, char *name) { char *hheader="&host="; cdprs_action(CDPRS_DATA, hheader, 0); if(!nameoverride) { char uname[256]; gethostname(uname, sizeof(uname)); cdprs_action(CDPRS_DATA, uname, 0); } else { cdprs_action(CDPRS_DATA, name, 0); } } void set_location(char *loc) { char location[500]={0}; int loc_len = 0; char *url = urlencode (loc, strlen (loc), &loc_len); if (url) { sprintf(location, "&loc=%s", url); free (url); } cdprs_action(CDPRS_DATA, location, 0); } char * urlencode(char *s, int slen, int *new_len) { register int x, y; unsigned char *str; static unsigned const char hexchars[] = "0123456789ABCDEF"; str = (unsigned char *) malloc(3 * slen + 1); if(str==NULL) { puts("malloc failed"); exit(1); } else { memset(str, 0, (3*slen+1)); } for (x = 0, y = 0; slen--; x++, y++) { str[y] = (unsigned char) s[x]; if ((str[y] < '0' && str[y] != '-' && str[y] != '.') || (str[y] < 'A' && str[y] > '9') || (str[y] > 'Z' && str[y] < 'a' && str[y] != '_') || (str[y] > 'z')) { str[y++] = '%'; str[y++] = hexchars[(unsigned char) s[x] >> 4]; str[y] = hexchars[(unsigned char) s[x] & 15]; } } str[y] = '\0'; if (new_len) { *new_len = y; } return ((char *) str); } int send_update(char *ip, char *msg, int port, int verbose) { int sockfd, msg_len, bytes_sent; struct sockaddr_in cdprs_addr; sockfd=socket(AF_INET, SOCK_STREAM, 0); if(sockfd < 0) { perror("socket"); return 1; } cdprs_addr.sin_family = AF_INET; cdprs_addr.sin_port = htons(port); cdprs_addr.sin_addr.s_addr = inet_addr(ip); memset(&(cdprs_addr.sin_zero), '\0', 8); if(verbose >=2) { printf("Message: %s\n", msg); } if(connect(sockfd, (struct sockaddr *)&cdprs_addr, sizeof(struct sockaddr)) < 0) { perror("connect"); return 1; } msg_len = strlen(msg); bytes_sent = send(sockfd, msg, msg_len, 0); if(bytes_sent < 0) { perror("send"); return 1; } if(verbose >=2) { printf("Sent %d of %d bytes\n", bytes_sent, msg_len); } if(shutdown(sockfd,2) < 0) { perror("shutdown"); return 1; } return 0; } int cdprs_action(int action, char *string, int verbose) { static char *msg; static char *ip; static int port; static int init_done = 0; const char http_hdr[] = "GET "; switch(action) { case CDPRS_INIT: /* Init msg buffer, malloc mem, put in header, etc.*/ if(!init_done) { msg=malloc(4096); if(msg == NULL) { printf("malloc failed\n"); exit(1); } else { memset(msg, 0, 4096); strcpy(msg, http_hdr); #ifdef WIN32 win32_socket_init(); #endif /* Get the IP and URL from the config file */ if(strlen(string)) { read_file(string); } init_done = 1; } } break; case CDPRS_SETIP: /* Set IP Address */ ip = string; break; case CDPRS_SETPORT: /* Set the network port */ port = verbose; break; case CDPRS_DATA: /* Append string to msg */ strcat(msg,string); break; case CDPRS_SEND: /* Tack on the hostname and footer and send data to server */ // get_hostname(); cdprs_footer(); send_update(ip,msg,port,verbose); /* We have sent the msg to the server, free the mem used by msg */ free(msg); #ifdef WIN32 win32_socket_cleanup(); #endif break; default: /* No default action */ break; } return 0; } cdpr-2.4/conffile.c0000644000175000017500000000760111336313047013173 0ustar lancelance/* * 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. */ #include "stdio.h" #include "stdlib.h" #include "string.h" #include "cdpr.h" #if WIN32 #include "Winsock2.h" #else #include "sys/types.h" #include "sys/socket.h" #include "netinet/in.h" #include "arpa/inet.h" #include "netdb.h" #if SOLARIS #include "u_ints.h" #endif #endif int is_ip (char *cp) { // Return true if we can convert cp into an ip address, false if not. // This test may be too simplistic as inet_addr() accepts "1.2.3.4", // "1.2.3", "1.2", and "1" as valid ip addresses. if (cp && *cp) { return inet_addr (cp) != INADDR_NONE; } return 0; } void do_something_with (char *ip, char *url) { struct hostent *h; char *addy; int port; if (ip && url) { if((strstr(ip,":")) == NULL) { printf("Didn't find a port, assuming 80\n"); addy = ip; port = 80; printf("Sending updates to %s:%d\n", addy, port); } else { strtok(ip,":"); char *tport = strtok(NULL,":"); port = atoi(tport); } /* if((addy = strtok(ip,":")) == NULL) { port = 80; } else { } */ if (is_ip (ip)) { printf ("ip addr = \"%s:%d\", url = \"%s\"\n", ip, port, url); cdprs_action(CDPRS_SETIP, ip, 1); cdprs_action(CDPRS_SETPORT, NULL, port); cdprs_action(CDPRS_DATA, url, 1); /* Add the ? to the end of the url */ cdprs_action(CDPRS_DATA, "?", 1); return; } else { printf ("hostname = \"%s\", url = \"%s\"\n", ip, url); /* Get the IP of the hostname */ if((h=gethostbyname(ip)) == NULL) { /* herror is obsolete, and doesn't exist on Solaris ** herror("gethostbyname"); */ puts("gethostbyname failed"); exit(1); } else { cdprs_action(CDPRS_SETIP, inet_ntoa(*((struct in_addr *)h->h_addr)), 1); cdprs_action(CDPRS_SETPORT, NULL, port); cdprs_action(CDPRS_DATA, url, 1); /* Add the ? to the end of the url */ cdprs_action(CDPRS_DATA, "?", 1); } } } } void process_line (char *buf) { if (buf) { char *ip = strtok (buf, " \t"); if (ip) { if (*ip == '#') { // Ignore lines beginning with '#' as comments } else { char *url = strtok (NULL, " \t\n"); if (url) { do_something_with (ip, url); } } } } } void read_fp (FILE *fp) { // Read the file line by line if (fp) { char buf[666] = {0}; while (fgets (buf, sizeof (buf)-1, fp) != NULL) { process_line (buf); } } } void read_file (char *file) { // Open the file if (file && *file) { FILE *fp = fopen (file, "r"); if (fp == NULL) { printf ("Can't open "); perror (file); } else { read_fp (fp); fclose (fp); } } } /* int main (int argc, char **argv) { if (argc == 1) { // If no command line arguments, put up help puts ("usage: ReadWebIp [config filenames]"); } else { // Read each file on command line while (--argc) { read_file (*++argv); } } return 0; } */ cdpr-2.4/COPYING0000644000175000017500000004362507764220002012301 0ustar lancelance GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy 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 Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19yy name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Library General Public License instead of this License. cdpr-2.4/INSTALL0000644000175000017500000000047411336312761012276 0ustar lancelancecdpr - Cisco Discovery Protocol Reporter Copyright (c) 2002-2010 - MonkeyMental.com cdpr requires the pcap library (libpcap). Since this is a really simple program, there is no real build/install process. To build: make To install: copy the resulting binary (cdpr) to wherever you would like it. cdpr-2.4/license.txt0000644000175000017500000000161111336313015013413 0ustar lancelance/* * 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. cdpr-2.4/Makefile0000644000175000017500000000131011336321405012666 0ustar lancelance# Seems that RedHat keeps the pcap headers elsewhere... # Uncomment the below line for RH systems. #CFLAGS=-Wall -ggdb -lpcap -I/usr/include/pcap # Standard build with pcap installed on the system CFLAGS = -Wall -W -O2 #CFLAGS = -Wall -W -ggdb # Build for the Zaurus with the pcap headers/libs one dir up #CFLAGS = -DZAURUS -Wall -I. -I../libpcap-0.7.1 -L../libpcap-0.7.1 -ggdb # Build for Solaris with the pcap headers/libs one dir up #CFLAGS = -DSOLARIS -Wall -I. -I../libpcap-0.7.1 -L../libpcap-0.7.1 -ggdb LDFLAGS = -lpcap # Build for Solaris #LDFLAGS = -lsocket -lnsl -lpcap cdpr: cdpr.c cdprs.c conffile.c cdp.h cdpr.h gcc $(CFLAGS) cdpr.c cdprs.c conffile.c $(LDFLAGS) -o cdpr clean: rm -f cdpr cdpr-2.4/README0000644000175000017500000001446111336321324012121 0ustar lancelancecdpr - Cisco Discovery Protocol Reporter Copyright (c) 2002-2008 MonkeyMental.com *Version 2.4 Work around OSX buffering issue that prevented the bpf interface from passing the packets up to the decoder Allow reading from a pcap file using -r for debugging purposes. *Version 2.3 Decode IPv6 responses in the CDP packet and send them in the http response if using cdpr server updates. Sample of GET request: ?switch_ip=6x.3x.2xx.2xx&switch_ip6=fxxx::2xx:cxxx:fxxx:2xxx&switch_ip6=2xxx:1xxx:2xx::1&port=GigabitEthernet1%2F45&host=monkeymental.com *Version 2.2.1 Fixed the logic that will select a default port of 80 if no port is specified. Updated copyrights and license details on all source files with the exception of the MS Visual Studio project files. I'm unsure if I can add license and copyright info to these files and still have them work. However, these are not source files, so think of them as nothing more than Makefiles for Visual Studio. *Version 2.2.0 Added the ability to specify what port to send the updates to. *Version 2.1.0 This version provides the ability to include a timeout so that you can put cdpr into startup scripts and not have it hang if for some reason a CDP packet is never received (i.e. CDP is disabled, machine is plugged into a hub, etc.) The following command line option has been added: -t timeout: Specify a timeout (in seconds) to wait for a CDP packet. Please note that the default time between CDP packets is 60 seconds so you should set your timeout to something greater than 60. There is a default timeout of 5 minutes so that should you not provide a timeout, cdpr will still exit if there is no CDP packet received within 5 minutes. This version also allows you to specify the server and URL to send CDP updates to on the command line. The following command line options have been added: -s server: Server to send information to requires -p (overridden by -u) -p path: Path of server script to send data to requires -s (overridden by -u) As you can see if you specify -s, -p and -u on the command line, the config file is actually used, and the -s and -p options are ignored. Both the -s and -p options must be specified and cdpr will exit if only one of them is given. An example as to how to use the -s and -p flags: cdpr -d eth0 -s localhost -p /cgi-bin/cdprs.cgi -t 120 This will listen for a CDP announcement on device eth0 for up to 2 minutes (120 seconds) and send the results to the webserver running on the local machine by calling the /cgi-bin/cdprs.cgi page. *Versions 2.0.1 - 2.0.5 These version were never officially released, they were code cleanups and timeout implementation. *Version 2.0.0 This version provides the functionallity to report CDP data back to a centralized server. Please see the file README.cdprs for details, but in general, the following command line options have been added: -u conf.file: Send updates to the host listed in the config file. -l location: Send a location string to help identify the port. -n hostname: Override the hostname sent to the server. Platform Update: Some of the newer HP Switch firmware will broadcast CDP packets. cdpr will decode these packets just the same as they would if they came from a Cisco product. It was tested on a HP ProCurve 4000M running revision C.09.16. *Version 1.0.8 Support for ARM processor. Tested on the Sharp Zaurus SL-5500. *Version 1.0.7 Support for Win32 clients please read README.Win32. To support the Win32 clients, modified so that if a network device is not specified on the command line with -d, then to find all devices that are capable of being used by the pcap libary and provide a selection list. *Version 1.0.6 Fixed up some more bus errors/segmentation faults. *Version 1.0.5 Copy packet data to local variable to resolve alignment issues that caused Bus Errors on some architectures. cdpr has now been compiled and tested on the following Operating Systems: - Linux 2.4.18 (x86) - FreeBSD 4.6 (x86) - Sun Solaris 8 (SPARC) - HP HP-UX 11.11 (PA-Risc) - IBM AIX 4.3.3 (PowerPC) On some operating systems (like Solaris) the u_int* typedefs are not defined, to resolve this issue, include u_ints.h in cdp.h. On AIX you must load the dlpi kernel module using the following command: strload -f /etc/dlpi.conf On AIX you may recieve some warnings about "Primitive issued in improper state", these can be safely ignored. On HP-UX you may recieve a warning about "any missing unit number", this can be safely ignored *Version 1.0.4 Added 1 ms timeout to pcap_open_live to fix BSD hang issues. *Version 1.0.3 Loop on pcap_next until a valid packet is received, some implementations of pcap will return a null. Patch provided by Martin Buck . *Version 1.0.2 Use a new filter to better identify CDP packets. Solves the majority of the segfaults due to trying to decode a non-CDP packet (i.e. a VTP packet) *Version 1.0.1 Add preprocessor macros to only include the Link Type defines provided by the pcap and bpf headers. *Version 1.0.0 I originally wrote this program to report which switch, and port a machine is connected to. In a large environment sometimes it is difficult to determine which port on which switch a machine is connected to. By default (with no command line options) cdpr will determine a network interface to open, put into promiscuous mode and wait for a single CDP packet to be received. On multi-homed machines, you can specify which interface to use with the -d option. Example (to determine which switch/port eth1 is connected to): cdpr -d eth1 You can dump the whole cdp packet using -v (more v's = more output). Sample Output: cdpr - Cisco Discovery Protocol Reporter Version 1.0.0 Copyright (c) 2002 - MonkeyMental.com Using Device: eth0 Waiting for CDP advertisement, default config is to transmit CDP packets every 60 seconds Device ID value: mtview-3548-access-1.i2.com Addresses value: 10.100.230.14 Port ID value: FastEthernet0/4 As you can see above, by default CDP packets are broadcast every 60 seconds, cdpr will wait until a CDP packet is received. I hope you find this utility useful. Thanks to Manfred Siemsen for helping to decode the packet data. Regards, Lance O'Connor lance@monkeymental.com cdpr-2.4/README.cdprs0000644000175000017500000000452611064763142013242 0ustar lancelanceVersion 2.0.0 of cdpr now supports transmiting the data decoded to a server. In lieu of writing my own cdpr server, I decided that I would send the data to a web server using a GET request. This has several advantages: - I didn't have to write a server - You can have your webserver do whatever you want with the data - Put it in a database - Log it to a text file - etc. - You can write your server piece in the language of your choice (PHP, ASP, Perl, C, etc.) as it's just a standard HTTP GET request. *Details on the config file The config file should contain one line that has the host to connect to and the relative URL to request. For example: [cdpr.conf] cdpr_svr.monkeymental.com /cdprs.php This will instruct cdpr to connect to the machine cdpr_svr.monkeymental.com and request the URL /cdprs/cpdrs.php. This would be the same as http://cdpr_svr.monkeymental.com/cdprs/cdprs.php in a web browser. The server and the URL should be seperated by a whitespace character (i.e. a [Tab]) *Details on the GET request: cdpr will execute the following query to port 80 of the server in the config file: GET /cdprs.php??switch_ip=6x.3x.2xx.2xx&switch_ip6=fxxx::2xx:cxxx:fxxx:2xxx&switch_ip6=2xxx:1xxx:2xx::1&port=GigabitEthernet1%2F45&host=monkeymental.com HTTP/1.0 The request will have the following variables assosciated with it: switch_ip: The IP address of the switch the machine running cdpr is connected to. switch_ip6: The IPv6 address of the switch the machine running cdpr is connected to (may be more than one.) port: The port that the machine running cdpr is connected to. loc: The optional location identifier passed on the command line using -l host: The hostname of the machine running cdpr (or whatever is passed on the command line if using the hostname override option -n) How you access this data depends on what language you are writing your scripts in. If you were using PHP, you could access them with $_GET["variable"] (i.e. $_GET["switch_ip"]) Once you have the data in your web page/cgi script, you can do with it as you wish, such as store it in a text file, Populate a SQL database, etc. *Notes: A few things to note about the CDPR Server functionallity: - Any output from the webserver is ignored - Only one attempt is made to send the data - Port and location are urlencoded to handle any "odd" characters (like a /) cdpr-2.4/README.Win320000644000175000017500000000136107764220002013016 0ustar lancelanceVersion 1.0.8+ update: Supports the WinPcap Version 3.0 release cdpr version 1.0.7 supports Win32 clients, it has been tested against WinPcap version 3.0 alpha 4 (http://winpcap.polito.it/install/default.htm). I have compiled it with Microsoft Visual C++ 6.0. To build it from source download the WinPcap Developers Pack version 3.0 alpha 4 and unzip it into the cdpr source tree. Your directory structure should looks something like this: \cdpr +-wpdpack +-docs +-Examples +-Include +-Lib I have included the Workspace files so that you should be able to just open cdpr.dsw When you build it it will create a directory called Release and Debug and put the appropriate binaries in each (cdpr.exe) Lance O'Connor lance@monkeymental.com cdpr-2.4/u_ints.h0000644000175000017500000000211711336313033012704 0ustar lancelance/* * 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. */ #define INADDR_NONE ((in_addr_t) 0xffffffff) typedef unsigned char u_int8_t; typedef unsigned short int u_int16_t; typedef unsigned int u_int32_t; cdpr-2.4/XGetopt.c0000644000175000017500000001337507764220002013003 0ustar lancelance// XGetopt.cpp Version 1.1 // // Author: Hans Dietrich // hdietrich2@hotmail.com // // This software is released into the public domain. // You are free to use it in any way you like. // // This software is provided "as is" with no expressed // or implied warranty. I accept no liability for any // damage or loss of business that this software may cause. // /////////////////////////////////////////////////////////////////////////////// //#include "stdafx.h" #include #include #include #include "XGetopt.h" /////////////////////////////////////////////////////////////////////////////// // // X G e t o p t . c p p // // // NAME // getopt -- parse command line options // // SYNOPSIS // int getopt(int argc, char *argv[], char *optstring) // // extern char *optarg; // extern int optind; // // DESCRIPTION // The getopt() function parses the command line arguments. Its // arguments argc and argv are the argument count and array as // passed into the application on program invocation. In the case // of Visual C++ programs, argc and argv are available via the // variables __argc and __argv (double underscores), respectively. // getopt returns the next option letter in argv that matches a // letter in optstring. // // optstring is a string of recognized option letters; if a letter // is followed by a colon, the option is expected to have an argument // that may or may not be separated from it by white space. optarg // is set to point to the start of the option argument on return from // getopt. // // Option letters may be combined, e.g., "-ab" is equivalent to // "-a -b". Option letters are case sensitive. // // getopt places in the external variable optind the argv index // of the next argument to be processed. optind is initialized // to 0 before the first call to getopt. // // When all options have been processed (i.e., up to the first // non-option argument), getopt returns EOF, optarg will point // to the argument, and optind will be set to the argv index of // the argument. If there are no non-option arguments, optarg // will be set to NULL. // // The special option "--" may be used to delimit the end of the // options; EOF will be returned, and "--" (and everything after it) // will be skipped. // // RETURN VALUE // For option letters contained in the string optstring, getopt // will return the option letter. getopt returns a question mark (?) // when it encounters an option letter not included in optstring. // EOF is returned when processing is finished. // // BUGS // 1) Long options are not supported. // 2) The GNU double-colon extension is not supported. // 3) The environment variable POSIXLY_CORRECT is not supported. // 4) The + syntax is not supported. // 5) The automatic permutation of arguments is not supported. // 6) This implementation of getopt() returns EOF if an error is // encountered, instead of -1 as the latest standard requires. // // EXAMPLE // BOOL CMyApp::ProcessCommandLine(int argc, char *argv[]) // { // int c; // // while ((c = getopt(argc, argv, "aBn:")) != EOF) // { // switch (c) // { // case 'a': // TRACE(_T("option a\n")); // // // // set some flag here // // // break; // // case 'B': // TRACE( _T("option B\n")); // // // // set some other flag here // // // break; // // case 'n': // TRACE(_T("option n: value=%d\n"), atoi(optarg)); // // // // do something with value here // // // break; // // case '?': // TRACE(_T("ERROR: illegal option %s\n"), argv[optind-1]); // return FALSE; // break; // // default: // TRACE(_T("WARNING: no handler for option %c\n"), c); // return FALSE; // break; // } // } // // // // check for non-option args here // // // return TRUE; // } // /////////////////////////////////////////////////////////////////////////////// char *optarg; // global argument pointer int optind = 0; // global argv index int getopt(int argc, char *argv[], char *optstring) { char c; char *cp; static char *next = NULL; if (optind == 0) next = NULL; optarg = NULL; if (next == NULL || *next == '\0') { if (optind == 0) optind++; if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0') { optarg = NULL; if (optind < argc) optarg = argv[optind]; return EOF; } if (strcmp(argv[optind], "--") == 0) { optind++; optarg = NULL; if (optind < argc) optarg = argv[optind]; return EOF; } next = argv[optind]+1; optind++; } c = *next++; cp = strchr(optstring, c); if (cp == NULL || c == ':') return '?'; cp++; if (*cp == ':') { if (*next != '\0') { optarg = next; next = NULL; } else if (optind < argc) { optarg = argv[optind]; optind++; } else { return '?'; } } return c; } cdpr-2.4/XGetopt.h0000644000175000017500000000115307764220002012777 0ustar lancelance// XGetopt.h // // Author: Hans Dietrich // hdietrich2@hotmail.com // // This software is released into the public domain. // You are free to use it in any way you like. // // This software is provided "as is" with no expressed // or implied warranty. I accept no liability for any // damage or loss of business that this software may cause. // /////////////////////////////////////////////////////////////////////////////// #ifndef XGETOPT_H #define XGETOPT_H extern int optind, opterr; extern char *optarg; int getopt(int argc, char *argv[], char *optstring); #endif //XGETOPT_H