#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <netinet/in.h>
#include <net/ethernet.h>
#include <arpa/inet.h>
#include "mymodule.h"
#include "my_params.h"
#include "iomodule.h"
using namespace std;
static mymodule * ptrtomod;
// Module Constructor
mymodule::mymodule()
{
CollisionCount = 0;
iomodule myio;
}
// Module Constructor used with object pointers
mymodule::mymodule(void*)
{
}
// Module Destructor
mymodule::~mymodule()
{
}
// Module initialization function
// Will initialize all of the elements that it needs before usage.
// Some danger doing some of these in the constructor because other objects might not
// fully be built yet.
void mymodule::Init()
{
myio.Init();
}
// Returns the current time for printing
// Formats the current time nicely for HH:MM:SS ?M
// Makes use of the printTime(time_t in_time) function
char* mymodule::printTime()
{
return printTime(time(NULL));
}
// Returns the time for printing
// Formats the time given nicely for printing.
char* mymodule::printTime(time_t in_time)
{
struct tm * lt;
static char rettime[12];
lt = localtime(&in_time);
if(lt->tm_hour > 12)
sprintf(rettime, "%i:%02i:%02i pm", lt->tm_hour-12, lt->tm_min, lt->tm_sec);
else
sprintf(rettime, "%i:%02i:%02i am", lt->tm_hour, lt->tm_min, lt->tm_sec);
return rettime;
}
// Waits for ms milliseconds then returns
// Ideally this could be replaced with a sleep function of some sort, but
// the resolution of sleep doesn't appear to be very good.
void busywait(unsigned int ms)
{
clock_t finish = (ms*CLOCKS_PER_SEC/1000) + clock();
while (finish > clock());
return;
}
// print current stats
void mymodule::printStats()
{
time_t curtime;
curtime = time(NULL);
cout << "Current Time: " << printTime(curtime) << endl;
cout << "Current Address: " << inet_ntoa(Current_Address) << endl;
cout << "Collision Count: " << CollisionCount << endl;
cout << "Last Collision Time: " << printTime(LastCollisionTime) << endl;
cout << "Last Collision Time: " << asctime ( localtime ( &LastCollisionTime)) << endl;
cout << " Seconds Ago: " << difftime(curtime, LastCollisionTime) << endl;
}
int mymodule::NetJoin(int joinType)
{
int tmp;
time_t time_lastprobe;
time_t time_lastclaim;
time_t time_annouce;
char ipstr[15] = DFT_LL_ADDRESS_STR; // ip address dotted string
struct in_addr ipaddr;
struct libnet_ether_addr *hw_addr;
hw_addr = myio.getHWAddress();
sprintf(ipstr,"%s%d.%d",DFT_LL_ADDRESS_STR, hw_addr->ether_addr_octet[4],
hw_addr->ether_addr_octet[5]); // Use the last 2 of the MAC address for initial IP
inet_aton(ipstr, &ipaddr); // set the ipaddr structure up with the chosen address.
Current_Address = ipaddr;
cout << " Address is now " << inet_ntoa(Current_Address) << endl;
}
// This is a static wrapper function that is used for the callback.
static void NetRunningWrapper(u_char *arg, const struct pcap_pkthdr* packet_header, const unsigned char * packet)
{
mymodule mySelf = *ptrtomod;
mySelf.NetRunningCallback(mySelf.getIO().getCallbackPacket(arg, packet_header, packet));
}
iomodule mymodule::getIO()
{
return myio;
}
// This function sets up and starts the arp listener.
int mymodule::StartNetRunning()
{
// This function will start the packet listener
// When we want to stop the net (or if we need to reconfigure the IP we need
// to call pcap_breakloop() which will stop the loop from processing.
// This way it will return and we can re-start the net running as long as we wish.
ptrtomod = this;
myio.startARPListener(Current_Address, NetRunningWrapper);
}
// This function stops the arp listener.
int mymodule::StopNetRunning()
{
myio.stopARPListener();
}
// Callback function to handle relevant ARP packets
// This function will handle the conflict detection for ZeroConf once
// the network is up and running.
void mymodule::NetRunningCallback(ARP_Packet *pkt)
{
LastCollisionTime = time(NULL); // We have a collision, set the time
CollisionCount++;
cout << "Collision Detected:"<<endl;
cout << " Col Count: " << CollisionCount << endl;
cout << " Col Time: " << printTime(LastCollisionTime) << endl;
cout << " Col Time: " << asctime ( localtime ( &LastCollisionTime )) << endl;
return;
}
</pre> |