--- In [email protected], "Tyler Littlefield" <ty...@...> wrote:
>
> I give it -c 5. The destination and source ports aren't valid.
I give it these arguments (running as root) and I get:
compiling
applying
creating loop
although it doesn't appear to do anything after that. Is that
different to what you get?
Tidied up code below.
int main(int argc, char* argv[])
{
//check arg length:
if (argc <=1)
{
PrintErr("You must provide an argument.");
help(argv[0]);
exit(EXIT_FAILURE);
}
char* dev = NULL; //the device pointer
char ebuff[PCAP_ERRBUF_SIZE]; //the error buffer
bpf_u_int32 net_addr; //the network address
bpf_u_int32 mask_addr; //network mask.
in_addr addr; //used for translating addresses.
int i = 0; //used for loops.
string temp; //our temporary string.
int loop = 0; //how many times will we loop?
int wait = 1; //how long should we wait?
pcap_t* handle;
bool stop = true; //should we continue after the arg
parsing?
char filter[] = "port 22"; //our default filter
bpf_program prog; //used for applying the filter.
//get the device name:
if (!(dev = pcap_lookupdev(ebuff)))
{
PrintErr(ebuff);
exit(EXIT_FAILURE);
}
//get the subnet mask and network mask:
if ((pcap_lookupnet(dev, &net_addr, &mask_addr, ebuff)) == -1)
{
PrintErr(ebuff);
exit(EXIT_FAILURE);
}
//loop through args:
for (i = 1; i < argc; i++)
{
if ((temp = argv[i]) == "-i")
{
cout << "Interface: " << dev << endl; //print the
interface name
addr.s_addr = mask_addr;
cout << "Network mask: " << inet_ntoa(addr) << endl;
addr.s_addr = net_addr;
cout << "Network address: " << inet_ntoa(addr) << endl;
}
else if (temp == "-v")
{
version(argv[0]);
}
else if (temp=="-c") //the number of packets to capture:
{
if (argc == i)
{
PrintErr("-c takes an argument.");
help(argv[0]);
exit(EXIT_FAILURE);
}
loop = atoi(argv[i+1]);
stop = false;
}
}
//make sure we're not needlessly continuing.
if (stop)
{
exit(EXIT_SUCCESS);
}
//here we open the device.
//we'll set promisc to 0 so that this can be ran on non-root systems.
if (!(handle = pcap_open_live(dev, BUFSIZ, 0, wait, ebuff)))
{
PrintErr(ebuff);
exit(EXIT_FAILURE);
}
//now we "compile" our filter:
cout << "compiling" << endl;
if (pcap_compile(handle, &prog, filter, 1, net_addr) == -1)
{
PrintErr("Filter compilation error!");
exit(EXIT_FAILURE);
}
//now we apply the filter:
cout << "applying" << endl;
if (pcap_setfilter(handle, &prog) == -1)
{
PrintErr("Error in applying filter!");
exit(EXIT_FAILURE);
}
cout << "creating loop" << endl;
pcap_loop(handle, loop, capt_h, NULL);
pcap_close(handle);
return 0;
}