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
|
/*
* cdpr - Cisco Discovery Protocol Reporter
* Copyright (c) 2002-2006 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;
}
*/
|