Permalink
Find file
75d8cd2 Feb 19, 2012
115 lines (103 sloc) 3.12 KB
<?php
/* Include SWIG autogenerated proxies */
require_once __DIR__ . '/../build/SimplePcap.php';
/* Validate cli arguments */
if ($argc != 5) {
echo "Use: $argv[0]: <interfaceName> <libpcapFilterExpression> <snapLen> <timeout>\n";
exit(250);
}
/* Get cli arguments */
$device = $argv[1];
$filter = $argv[2];
$snapLen = $argv[3];
$timeout = $argv[4];
/* Get a SimplePcapObject */
$simplePcap = new SimplePcap($device, $filter, $snapLen, $timeout);
echo "Sniffing on: $device\n";
/* Main sniffing loop */
while(true) {
// "Decorate" the Packet received and dump it
echo new DumpablePacket($simplePcap->get());
echo "\n";
usleep(100);
}
/********** Done **********************/
/**
* You really dont need anything below here, just getting fancy.
* This class will "decorate" a Packet and make it dumpable,
* tcpdump style
*/
class DumpablePacket
{
private $packet;
const BYTES_PER_COLUMN = 8;
const COLUMNS_PER_ROW = 2;
const BYTES_PER_ROW = 16;
public function __toString()
{
$ret = $this->packet->__toString();
$ret .= "\n";
$ret .= $this->dumpPacketData($this->packet->getData());
return $ret;
}
private function dumpPacketData($data)
{
$ret = '';
$total = strlen($data);
$totalRows = ceil($total / self::BYTES_PER_ROW);
for ($row = 0, $offset = 0; $row < $totalRows; $row++) {
$ret .= str_pad($row * self::BYTES_PER_ROW, 5, "0", STR_PAD_LEFT);
$subdata = substr($data, $offset, self::BYTES_PER_ROW);
$ret .= $this->dumpHex($subdata);
$ret .= $this->dumpAscii($subdata);
$offset += self::BYTES_PER_ROW;
$ret .= "\n";
}
$ret .= "\n";
return $ret;
}
private function dumpHex($data)
{
$ret = '';
// Write N columas with the hex value of what we sniffed
for ($column = 0; $column < self::BYTES_PER_ROW; $column++) {
if (($column % self::BYTES_PER_COLUMN) == 0) {
$ret .= " ";
}
if (!isset($data[$column])) {
$ret .= ' ';
} else {
$value = $data[$column];
$ordValue = ord($value);
$ret .= str_pad(sprintf("%x", $ordValue), 2, "0", STR_PAD_LEFT) . " ";
}
}
return $ret;
}
private function dumpAscii($data)
{
$ret = '';
// Write a column with the ASCII of what we sniffed
for ($column = 0; $column < self::BYTES_PER_ROW; $column++) {
if (($column % self::BYTES_PER_COLUMN) == 0) {
$ret .= " ";
}
if (!isset($data[$column])) {
$ret .= ' ';
} else {
$value = $data[$column];
$ordValue = ord($value);
if ($ordValue > 31 && $ordValue < 128) {
$ret .= $data[$column];
} else {
$ret .= ".";
}
}
}
return $ret;
}
public function __construct(Packet $packet)
{
$this->packet = $packet;
}
}