Commit ff8d2a1d authored by Stansberry, Dale's avatar Stansberry, Dale
Browse files

- Added ipharvest tool

parent 8240de3c
CXX=g++
RM=rm -f
CPPFLAGS=-g -O2
LDFLAGS=-g
LDLIBS=
SRCS=ipharvest.cpp
OBJS=$(subst .cpp,.o,$(SRCS))
all: ipharvest
ipharvest: $(OBJS)
$(CXX) $(LDFLAGS) -o ipharvest $(OBJS) $(LDLIBS)
clean:
$(RM) $(OBJS)
#include <iostream>
#include <fstream>
#include <string>
#include <set>
#include <sys/types.h>
#include <ifaddrs.h>
#include <errno.h>
#include <string.h>
#include <getopt.h>
#include <arpa/inet.h>
#include <unistd.h>
using namespace std;
enum INetAddressFamily
{
IPV4,
IPV6,
OTHER
};
struct INetAddress
{
INetAddressFamily family;
string address;
unsigned short port;
};
void convertINetAddr( struct sockaddr *a_in, INetAddress &a_out )
{
char text[INET6_ADDRSTRLEN];
if ( a_in->sa_family == AF_INET )
{
a_out.family = IPV4;
a_out.port = ((struct sockaddr_in*)a_in)->sin_port;
a_out.address = inet_ntop( a_in->sa_family, &((struct sockaddr_in*)a_in)->sin_addr, text, INET_ADDRSTRLEN );
}
else if ( a_in->sa_family == AF_INET6 )
{
a_out.family = IPV6;
a_out.port = ((struct sockaddr_in6*)a_in)->sin6_port;
a_out.address = inet_ntop( a_in->sa_family, &((struct sockaddr_in6*)a_in)->sin6_addr, text, INET6_ADDRSTRLEN );
}
else
{
a_out.family = OTHER;
a_out.port = 0;
a_out.address.clear();
}
}
int main( int argc, char **argv )
{
set<string> ifnames;
set<unsigned short> ipver;
int opt;
while (( opt = getopt( argc, argv, "?i:v:" )) != -1 )
{
switch( opt )
{
case '?':
cout << "ipharvest options:" << endl;
cout << "? - show help" << endl;
cout << "i - interface name to include (default all)" << endl;
cout << "v - IP version to include (4, 6, default all)" << endl;
return 0;
case 'i':
ifnames.insert( optarg );
break;
case 'v':
if ( strcmp( optarg, "4" ) == 0 )
ipver.insert( IPV4 );
else if ( strcmp( optarg, "6" ) == 0 )
ipver.insert( IPV6 );
else
{
cerr << "Invalid IP version number: " << optarg << "\n";
return 1;
}
break;
}
}
char hostname[1024];
hostname[1023] = '\0';
gethostname(hostname, 1023);
set<string>::iterator iifn;
set<unsigned short>::iterator iv;
struct ifaddrs *interfaces;
if ( getifaddrs( &interfaces ) != 0 )
{
cerr << "getifaddrs failed: " << strerror(errno) << "\n";
return 1;
}
INetAddress inetaddr;
const char *afnames[3] = {"IPv4","IPv6","Other"};
if ( interfaces )
{
struct ifaddrs * i = interfaces;
while ( i )
{
if ( !ifnames.size() || ifnames.find( i->ifa_name ) != ifnames.end() )
{
convertINetAddr( i->ifa_addr, inetaddr );
if ( inetaddr.family != OTHER && ( !ipver.size() || ipver.find( inetaddr.family ) != ipver.end() ))
cout << hostname << "," << i->ifa_name << "," << afnames[inetaddr.family] << "," << inetaddr.address << "\n";
}
i = i->ifa_next;
}
freeifaddrs( interfaces );
}
else
{
cerr << "No interfaces found\n";
}
return 0;
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment