Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pcap-Net
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Pcap-Net
Commits
ca7a7b84
Commit
ca7a7b84
authored
Jun 26, 2009
by
Brickner_cp
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
--no commit message
--no commit message
parent
53944050
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
118 additions
and
16 deletions
+118
-16
Program.cs
PcapDotNet/src/PcapDotNet.Console/Program.cs
+20
-1
PcapDevice.cpp
PcapDotNet/src/PcapDotNet.Core/PcapDevice.cpp
+1
-1
PcapDevice.h
PcapDotNet/src/PcapDotNet.Core/PcapDevice.h
+2
-0
PcapDeviceHandler.cpp
PcapDotNet/src/PcapDotNet.Core/PcapDeviceHandler.cpp
+65
-8
PcapDeviceHandler.h
PcapDotNet/src/PcapDotNet.Core/PcapDeviceHandler.h
+30
-6
No files found.
PcapDotNet/src/PcapDotNet.Console/Program.cs
View file @
ca7a7b84
...
@@ -38,6 +38,11 @@ namespace WinPcapDotNet.Console
...
@@ -38,6 +38,11 @@ namespace WinPcapDotNet.Console
IPcapDevice
chosenDevice
=
devices
[
index
];
IPcapDevice
chosenDevice
=
devices
[
index
];
System
.
Console
.
WriteLine
(
"Start GetNextPackets"
);
GetNextPackets
(
chosenDevice
,
filter
);
System
.
Console
.
WriteLine
(
"Finished GetNextPackets"
);
System
.
Console
.
ReadKey
();
System
.
Console
.
WriteLine
(
"Start Statistics Mode"
);
System
.
Console
.
WriteLine
(
"Start Statistics Mode"
);
StatisticsMode
(
chosenDevice
,
filter
);
StatisticsMode
(
chosenDevice
,
filter
);
System
.
Console
.
WriteLine
(
"Finished Statistics Mode"
);
System
.
Console
.
WriteLine
(
"Finished Statistics Mode"
);
...
@@ -58,6 +63,20 @@ namespace WinPcapDotNet.Console
...
@@ -58,6 +63,20 @@ namespace WinPcapDotNet.Console
System
.
Console
.
WriteLine
(
"Finished Transmitting packets"
);
System
.
Console
.
WriteLine
(
"Finished Transmitting packets"
);
}
}
private
static
void
GetNextPackets
(
IPcapDevice
device
,
string
filter
)
{
using
(
PcapDeviceHandler
deviceHandler
=
device
.
Open
(
PcapDevice
.
DefaultSnapLen
,
PcapDeviceOpenFlags
.
Promiscuous
,
1
*
1000
))
{
deviceHandler
.
SetFilter
(
filter
);
int
numPacketsGot
;
deviceHandler
.
GetNextPackets
(
1000
,
packet
=>
System
.
Console
.
WriteLine
(
"Got packet with "
+
packet
.
Timestamp
+
" timestamp and "
+
packet
.
Length
+
" size"
),
out
numPacketsGot
);
}
}
private
static
void
StatisticsMode
(
IPcapDevice
device
,
string
filter
)
private
static
void
StatisticsMode
(
IPcapDevice
device
,
string
filter
)
{
{
using
(
PcapDeviceHandler
deviceHandler
=
device
.
Open
())
using
(
PcapDeviceHandler
deviceHandler
=
device
.
Open
())
...
@@ -65,7 +84,7 @@ namespace WinPcapDotNet.Console
...
@@ -65,7 +84,7 @@ namespace WinPcapDotNet.Console
deviceHandler
.
SetFilter
(
filter
);
deviceHandler
.
SetFilter
(
filter
);
deviceHandler
.
Mode
=
DeviceHandlerMode
.
Statistics
;
deviceHandler
.
Mode
=
DeviceHandlerMode
.
Statistics
;
for
(
int
i
=
0
;
i
!=
10
0
;
++
i
)
for
(
int
i
=
0
;
i
!=
10
;
++
i
)
{
{
PcapStatistics
statistics
;
PcapStatistics
statistics
;
DeviceHandlerResult
result
=
deviceHandler
.
GetNextStatistics
(
out
statistics
);
DeviceHandlerResult
result
=
deviceHandler
.
GetNextStatistics
(
out
statistics
);
...
...
PcapDotNet/src/PcapDotNet.Core/PcapDevice.cpp
View file @
ca7a7b84
...
@@ -4,5 +4,5 @@ using namespace PcapDotNet;
...
@@ -4,5 +4,5 @@ using namespace PcapDotNet;
PcapDeviceHandler
^
PcapDevice
::
Open
()
PcapDeviceHandler
^
PcapDevice
::
Open
()
{
{
return
Open
(
65536
,
PcapDeviceOpenFlags
::
Promiscuous
,
1000
);
return
Open
(
DefaultSnapLen
,
PcapDeviceOpenFlags
::
Promiscuous
,
1000
);
}
}
PcapDotNet/src/PcapDotNet.Core/PcapDevice.h
View file @
ca7a7b84
...
@@ -7,6 +7,8 @@ namespace PcapDotNet
...
@@ -7,6 +7,8 @@ namespace PcapDotNet
public
ref
class
PcapDevice
abstract
:
IPcapDevice
public
ref
class
PcapDevice
abstract
:
IPcapDevice
{
{
public
:
public
:
static
const
int
DefaultSnapLen
=
65536
;
virtual
property
System
::
String
^
Name
virtual
property
System
::
String
^
Name
{
{
System
::
String
^
get
()
=
0
;
System
::
String
^
get
()
=
0
;
...
...
PcapDotNet/src/PcapDotNet.Core/PcapDeviceHandler.cpp
View file @
ca7a7b84
...
@@ -10,6 +10,8 @@ using namespace BPacket;
...
@@ -10,6 +10,8 @@ using namespace BPacket;
using
namespace
PcapDotNet
;
using
namespace
PcapDotNet
;
using
namespace
System
::
Runtime
::
InteropServices
;
using
namespace
System
::
Runtime
::
InteropServices
;
void
packet_handler
(
u_char
*
param
,
const
struct
pcap_pkthdr
*
header
,
const
u_char
*
pkt_data
);
PcapDeviceHandler
::
PcapDeviceHandler
(
const
char
*
source
,
int
snapLen
,
PcapDeviceOpenFlags
flags
,
int
readTimeout
,
pcap_rmtauth
*
auth
,
SocketAddress
^
netmask
)
PcapDeviceHandler
::
PcapDeviceHandler
(
const
char
*
source
,
int
snapLen
,
PcapDeviceOpenFlags
flags
,
int
readTimeout
,
pcap_rmtauth
*
auth
,
SocketAddress
^
netmask
)
{
{
// Open the device
// Open the device
...
@@ -50,7 +52,7 @@ DeviceHandlerResult PcapDeviceHandler::GetNextPacket([Out] Packet^% packet)
...
@@ -50,7 +52,7 @@ DeviceHandlerResult PcapDeviceHandler::GetNextPacket([Out] Packet^% packet)
pcap_pkthdr
*
packetHeader
;
pcap_pkthdr
*
packetHeader
;
const
unsigned
char
*
packetData
;
const
unsigned
char
*
packetData
;
DeviceHandlerResult
result
=
safe_cast
<
DeviceHandlerResult
>
(
pcap_next_ex
(
_pcapDescriptor
,
&
packetHeader
,
&
packetData
)
);
DeviceHandlerResult
result
=
RunPcapNextEx
(
&
packetHeader
,
&
packetData
);
if
(
result
!=
DeviceHandlerResult
::
Ok
)
if
(
result
!=
DeviceHandlerResult
::
Ok
)
{
{
...
@@ -58,14 +60,36 @@ DeviceHandlerResult PcapDeviceHandler::GetNextPacket([Out] Packet^% packet)
...
@@ -58,14 +60,36 @@ DeviceHandlerResult PcapDeviceHandler::GetNextPacket([Out] Packet^% packet)
return
result
;
return
result
;
}
}
timeval
pcapTimestamp
=
packetHeader
->
ts
;
packet
=
CreatePacket
(
*
packetHeader
,
packetData
)
;
DateTime
timestamp
;
return
result
;
Timestamp
::
PcapTimestampToDateTime
(
packetHeader
->
ts
,
timestamp
);
}
array
<
Byte
>^
managedPacketData
=
MarshalingServices
::
UnamangedToManagedByteArray
(
packetData
,
0
,
packetHeader
->
caplen
);
DeviceHandlerResult
PcapDeviceHandler
::
GetNextPackets
(
int
maxPackets
,
HandlePacket
^
callBack
,
[
Out
]
int
%
numPacketsGot
)
packet
=
gcnew
Packet
(
managedPacketData
,
timestamp
);
{
if
(
Mode
!=
DeviceHandlerMode
::
Capture
)
throw
gcnew
InvalidOperationException
(
"Must be in capture mode to get packets"
);
return
result
;
PacketHandler
^
packetHandler
=
gcnew
PacketHandler
(
callBack
);
PacketHandler
::
Delegate
^
packetHandlerDelegate
=
gcnew
PacketHandler
::
Delegate
(
packetHandler
,
&
PacketHandler
::
Handle
);
pcap_handler
functionPointer
=
(
pcap_handler
)
Marshal
::
GetFunctionPointerForDelegate
(
packetHandlerDelegate
).
ToPointer
();
numPacketsGot
=
pcap_dispatch
(
_pcapDescriptor
,
maxPackets
,
functionPointer
,
NULL
);
if
(
numPacketsGot
==
-
1
)
{
throw
gcnew
InvalidOperationException
(
"Failed Getting packets. Error: "
+
gcnew
String
(
pcap_geterr
(
_pcapDescriptor
)));
}
if
(
numPacketsGot
==
-
2
)
{
return
DeviceHandlerResult
::
BreakLoop
;
}
return
DeviceHandlerResult
::
Ok
;
}
}
DeviceHandlerResult
PcapDeviceHandler
::
GetNextStatistics
([
Out
]
PcapStatistics
^%
statistics
)
DeviceHandlerResult
PcapDeviceHandler
::
GetNextStatistics
([
Out
]
PcapStatistics
^%
statistics
)
...
@@ -75,7 +99,7 @@ DeviceHandlerResult PcapDeviceHandler::GetNextStatistics([Out] PcapStatistics^%
...
@@ -75,7 +99,7 @@ DeviceHandlerResult PcapDeviceHandler::GetNextStatistics([Out] PcapStatistics^%
pcap_pkthdr
*
packetHeader
;
pcap_pkthdr
*
packetHeader
;
const
unsigned
char
*
packetData
;
const
unsigned
char
*
packetData
;
DeviceHandlerResult
result
=
safe_cast
<
DeviceHandlerResult
>
(
pcap_next_ex
(
_pcapDescriptor
,
&
packetHeader
,
&
packetData
)
);
DeviceHandlerResult
result
=
RunPcapNextEx
(
&
packetHeader
,
&
packetData
);
if
(
result
!=
DeviceHandlerResult
::
Ok
)
if
(
result
!=
DeviceHandlerResult
::
Ok
)
{
{
...
@@ -147,4 +171,37 @@ PcapDeviceHandler::~PcapDeviceHandler()
...
@@ -147,4 +171,37 @@ PcapDeviceHandler::~PcapDeviceHandler()
pcap_t
*
PcapDeviceHandler
::
Descriptor
::
get
()
pcap_t
*
PcapDeviceHandler
::
Descriptor
::
get
()
{
{
return
_pcapDescriptor
;
return
_pcapDescriptor
;
}
// static
Packet
^
PcapDeviceHandler
::
CreatePacket
(
const
pcap_pkthdr
&
packetHeader
,
const
unsigned
char
*
packetData
)
{
DateTime
timestamp
;
Timestamp
::
PcapTimestampToDateTime
(
packetHeader
.
ts
,
timestamp
);
array
<
Byte
>^
managedPacketData
=
MarshalingServices
::
UnamangedToManagedByteArray
(
packetData
,
0
,
packetHeader
.
caplen
);
return
gcnew
Packet
(
managedPacketData
,
timestamp
);
}
DeviceHandlerResult
PcapDeviceHandler
::
RunPcapNextEx
(
pcap_pkthdr
**
packetHeader
,
const
unsigned
char
**
packetData
)
{
int
result
=
pcap_next_ex
(
_pcapDescriptor
,
packetHeader
,
packetData
);
switch
(
result
)
{
case
-
2
:
return
DeviceHandlerResult
::
Eof
;
case
-
1
:
return
DeviceHandlerResult
::
Error
;
case
0
:
return
DeviceHandlerResult
::
Timeout
;
case
1
:
return
DeviceHandlerResult
::
Ok
;
default
:
throw
gcnew
InvalidOperationException
(
"Result value "
+
result
+
" is undefined"
);
}
}
void
PcapDeviceHandler
::
PacketHandler
::
Handle
(
unsigned
char
*
user
,
const
struct
pcap_pkthdr
*
packetHeader
,
const
unsigned
char
*
packetData
)
{
_callBack
->
Invoke
(
CreatePacket
(
*
packetHeader
,
packetData
));
}
}
\ No newline at end of file
PcapDotNet/src/PcapDotNet.Core/PcapDeviceHandler.h
View file @
ca7a7b84
...
@@ -10,10 +10,11 @@ namespace PcapDotNet
...
@@ -10,10 +10,11 @@ namespace PcapDotNet
{
{
public
enum
class
DeviceHandlerResult
:
int
public
enum
class
DeviceHandlerResult
:
int
{
{
Ok
=
1
,
// if the packet has been read without problems
Ok
,
// if the packet has been read without problems
Timeout
=
0
,
// if the timeout set with Open() has elapsed.
Timeout
,
// if the timeout set with Open() has elapsed.
Error
=
-
1
,
// if an error occurred
Error
,
// if an error occurred
Eof
=
-
2
// if EOF was reached reading from an offline capture
Eof
,
// if EOF was reached reading from an offline capture
BreakLoop
//
};
};
public
enum
class
DeviceHandlerMode
:
int
public
enum
class
DeviceHandlerMode
:
int
...
@@ -29,7 +30,6 @@ namespace PcapDotNet
...
@@ -29,7 +30,6 @@ namespace PcapDotNet
public
:
public
:
PcapDeviceHandler
(
const
char
*
source
,
int
snapLen
,
PcapDeviceOpenFlags
flags
,
int
readTimeout
,
pcap_rmtauth
*
auth
,
PcapDeviceHandler
(
const
char
*
source
,
int
snapLen
,
PcapDeviceOpenFlags
flags
,
int
readTimeout
,
pcap_rmtauth
*
auth
,
SocketAddress
^
netmask
);
SocketAddress
^
netmask
);
//PcapDeviceHandler(pcap_t* pcapDescriptor, SocketAddress^ netmask);
property
DeviceHandlerMode
Mode
property
DeviceHandlerMode
Mode
{
{
...
@@ -37,7 +37,10 @@ namespace PcapDotNet
...
@@ -37,7 +37,10 @@ namespace PcapDotNet
void
set
(
DeviceHandlerMode
value
);
void
set
(
DeviceHandlerMode
value
);
}
}
delegate
void
HandlePacket
(
BPacket
::
Packet
^
packet
);
DeviceHandlerResult
GetNextPacket
([
System
::
Runtime
::
InteropServices
::
Out
]
BPacket
::
Packet
^%
packet
);
DeviceHandlerResult
GetNextPacket
([
System
::
Runtime
::
InteropServices
::
Out
]
BPacket
::
Packet
^%
packet
);
DeviceHandlerResult
GetNextPackets
(
int
maxPackets
,
HandlePacket
^
callBack
,
[
System
::
Runtime
::
InteropServices
::
Out
]
int
%
numPacketsGot
);
DeviceHandlerResult
GetNextStatistics
([
System
::
Runtime
::
InteropServices
::
Out
]
PcapStatistics
^%
statistics
);
DeviceHandlerResult
GetNextStatistics
([
System
::
Runtime
::
InteropServices
::
Out
]
PcapStatistics
^%
statistics
);
void
SendPacket
(
BPacket
::
Packet
^
packet
);
void
SendPacket
(
BPacket
::
Packet
^
packet
);
...
@@ -56,9 +59,30 @@ namespace PcapDotNet
...
@@ -56,9 +59,30 @@ namespace PcapDotNet
pcap_t
*
get
();
pcap_t
*
get
();
}
}
private
:
static
BPacket
::
Packet
^
CreatePacket
(
const
pcap_pkthdr
&
packetHeader
,
const
unsigned
char
*
packetData
);
DeviceHandlerResult
RunPcapNextEx
(
pcap_pkthdr
**
packetHeader
,
const
unsigned
char
**
packetData
);
ref
class
PacketHandler
{
public
:
PacketHandler
(
HandlePacket
^
callBack
)
{
_callBack
=
callBack
;
}
[
System
::
Runtime
::
InteropServices
::
UnmanagedFunctionPointer
(
System
::
Runtime
::
InteropServices
::
CallingConvention
::
Cdecl
)]
delegate
void
Delegate
(
unsigned
char
*
user
,
const
struct
pcap_pkthdr
*
packetHeader
,
const
unsigned
char
*
packetData
);
void
Handle
(
unsigned
char
*
user
,
const
struct
pcap_pkthdr
*
packetHeader
,
const
unsigned
char
*
packetData
);
HandlePacket
^
_callBack
;
};
private
:
private
:
pcap_t
*
_pcapDescriptor
;
pcap_t
*
_pcapDescriptor
;
IpV4SocketAddress
^
_ipV4Netmask
;
IpV4SocketAddress
^
_ipV4Netmask
;
DeviceHandlerMode
_mode
;
DeviceHandlerMode
_mode
;
};
};
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment