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
c0c69224
Commit
c0c69224
authored
Jul 04, 2009
by
Brickner_cp
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
--no commit message
--no commit message
parent
252d0cf2
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
112 additions
and
15 deletions
+112
-15
BerkeleyPacketFilterTests.cs
...Net/src/PcapDotNet.Core.Test/BerkeleyPacketFilterTests.cs
+37
-1
PacketSendQueueTests.cs
PcapDotNet/src/PcapDotNet.Core.Test/PacketSendQueueTests.cs
+4
-1
BerkeleyPacketFilter.cpp
PcapDotNet/src/PcapDotNet.Core/BerkeleyPacketFilter.cpp
+43
-13
BerkeleyPacketFilter.h
PcapDotNet/src/PcapDotNet.Core/BerkeleyPacketFilter.h
+7
-0
PcapDataLink.cpp
PcapDotNet/src/PcapDotNet.Core/PcapDataLink.cpp
+17
-0
PcapDataLink.h
PcapDotNet/src/PcapDotNet.Core/PcapDataLink.h
+4
-0
No files found.
PcapDotNet/src/PcapDotNet.Core.Test/BerkeleyPacketFilterTests.cs
View file @
c0c69224
using
System
;
using
System
;
using
Microsoft.VisualStudio.TestTools.UnitTesting
;
using
Microsoft.VisualStudio.TestTools.UnitTesting
;
using
Packets
;
namespace
PcapDotNet.Core.Test
namespace
PcapDotNet.Core.Test
{
{
...
@@ -56,7 +57,6 @@ namespace PcapDotNet.Core.Test
...
@@ -56,7 +57,6 @@ namespace PcapDotNet.Core.Test
//
//
#
endregion
#
endregion
[
TestMethod
]
[
TestMethod
]
[
ExpectedException
(
typeof
(
InvalidOperationException
))]
[
ExpectedException
(
typeof
(
InvalidOperationException
))]
public
void
BadFilterErrorTest
()
public
void
BadFilterErrorTest
()
...
@@ -66,5 +66,41 @@ namespace PcapDotNet.Core.Test
...
@@ -66,5 +66,41 @@ namespace PcapDotNet.Core.Test
communicator
.
SetFilter
(
"illegal filter string"
);
communicator
.
SetFilter
(
"illegal filter string"
);
}
}
}
}
[
TestMethod
]
public
void
NoCommunicatorConstructorTest
()
{
const
string
SourceMac
=
"11:22:33:44:55:66"
;
const
string
DestinationMac
=
"77:88:99:AA:BB:CC"
;
Packet
expectedPacket
=
MoreRandom
.
BuildRandomPacket
(
SourceMac
,
DestinationMac
,
100
);
Packet
unexpectedPacket
=
MoreRandom
.
BuildRandomPacket
(
DestinationMac
,
SourceMac
,
100
);
using
(
BerkeleyPacketFilter
filter
=
new
BerkeleyPacketFilter
(
"ether src "
+
SourceMac
+
" and ether dst "
+
DestinationMac
,
100
,
DataLinkKind
.
Ethernet
))
{
using
(
PacketCommunicator
communicator
=
LivePacketDeviceTests
.
OpenLiveDevice
())
{
communicator
.
SetFilter
(
filter
);
for
(
int
i
=
0
;
i
!=
5
;
++
i
)
{
communicator
.
SendPacket
(
expectedPacket
);
communicator
.
SendPacket
(
unexpectedPacket
);
}
Packet
packet
;
PacketCommunicatorReceiveResult
result
;
for
(
int
i
=
0
;
i
!=
5
;
++
i
)
{
result
=
communicator
.
GetPacket
(
out
packet
);
Assert
.
AreEqual
(
PacketCommunicatorReceiveResult
.
Ok
,
result
);
Assert
.
AreEqual
(
expectedPacket
,
packet
);
}
result
=
communicator
.
GetPacket
(
out
packet
);
Assert
.
AreEqual
(
PacketCommunicatorReceiveResult
.
Timeout
,
result
);
Assert
.
IsNull
(
packet
);
}
}
}
}
}
}
}
\ No newline at end of file
PcapDotNet/src/PcapDotNet.Core.Test/PacketSendQueueTests.cs
View file @
c0c69224
...
@@ -121,7 +121,10 @@ namespace PcapDotNet.Core.Test
...
@@ -121,7 +121,10 @@ namespace PcapDotNet.Core.Test
expectedDiff
=
TimeSpan
.
Zero
;
expectedDiff
=
TimeSpan
.
Zero
;
}
}
TimeSpan
actualDiff
=
packet
.
Timestamp
-
lastTimestamp
;
TimeSpan
actualDiff
=
packet
.
Timestamp
-
lastTimestamp
;
Assert
.
AreEqual
(
expectedDiff
,
actualDiff
);
MoreAssert
.
IsInRange
(
expectedDiff
.
Subtract
(
TimeSpan
.
FromSeconds
(
0.01
)),
expectedDiff
.
Add
(
TimeSpan
.
FromSeconds
(
0.01
)),
actualDiff
);
}
}
lastTimestamp
=
packet
.
Timestamp
;
lastTimestamp
=
packet
.
Timestamp
;
++
numPacketsHandled
;
++
numPacketsHandled
;
...
...
PcapDotNet/src/PcapDotNet.Core/BerkeleyPacketFilter.cpp
View file @
c0c69224
...
@@ -2,11 +2,54 @@
...
@@ -2,11 +2,54 @@
#include "Pcap.h"
#include "Pcap.h"
#include "MarshalingServices.h"
#include "MarshalingServices.h"
#include "PcapError.h"
#include "PcapError.h"
#include "PcapDataLink.h"
using
namespace
System
;
using
namespace
System
;
using
namespace
PcapDotNet
::
Core
;
using
namespace
PcapDotNet
::
Core
;
using
namespace
Packets
;
BerkeleyPacketFilter
::
BerkeleyPacketFilter
(
String
^
filterString
,
int
snapshotLength
,
DataLinkKind
kind
,
IpV4SocketAddress
^
netmask
)
{
Initialize
(
filterString
,
snapshotLength
,
kind
,
netmask
);
}
BerkeleyPacketFilter
::
BerkeleyPacketFilter
(
String
^
filterString
,
int
snapshotLength
,
DataLinkKind
kind
)
{
Initialize
(
filterString
,
snapshotLength
,
kind
,
nullptr
);
}
BerkeleyPacketFilter
::
BerkeleyPacketFilter
(
pcap_t
*
pcapDescriptor
,
String
^
filterString
,
IpV4SocketAddress
^
netmask
)
BerkeleyPacketFilter
::
BerkeleyPacketFilter
(
pcap_t
*
pcapDescriptor
,
String
^
filterString
,
IpV4SocketAddress
^
netmask
)
{
Initialize
(
pcapDescriptor
,
filterString
,
netmask
);
}
void
BerkeleyPacketFilter
::
SetFilter
(
pcap_t
*
pcapDescriptor
)
{
if
(
pcap_setfilter
(
pcapDescriptor
,
_bpf
)
!=
0
)
throw
PcapError
::
BuildInvalidOperation
(
"Failed setting bpf filter"
,
pcapDescriptor
);
}
BerkeleyPacketFilter
::~
BerkeleyPacketFilter
()
{
pcap_freecode
(
_bpf
);
delete
_bpf
;
}
void
BerkeleyPacketFilter
::
Initialize
(
String
^
filterString
,
int
snapshotLength
,
DataLinkKind
kind
,
IpV4SocketAddress
^
netmask
)
{
PcapDataLink
dataLink
=
PcapDataLink
(
kind
);
pcap_t
*
pcapDescriptor
=
pcap_open_dead
(
dataLink
.
Value
,
snapshotLength
);
try
{
Initialize
(
pcapDescriptor
,
filterString
,
netmask
);
}
finally
{
pcap_close
(
pcapDescriptor
);
}
}
void
BerkeleyPacketFilter
::
Initialize
(
pcap_t
*
pcapDescriptor
,
String
^
filterString
,
IpV4SocketAddress
^
netmask
)
{
{
std
::
string
unmanagedFilterString
=
MarshalingServices
::
ManagedToUnmanagedString
(
filterString
);
std
::
string
unmanagedFilterString
=
MarshalingServices
::
ManagedToUnmanagedString
(
filterString
);
...
@@ -29,16 +72,3 @@ BerkeleyPacketFilter::BerkeleyPacketFilter(pcap_t* pcapDescriptor, String^ filte
...
@@ -29,16 +72,3 @@ BerkeleyPacketFilter::BerkeleyPacketFilter(pcap_t* pcapDescriptor, String^ filte
throw
;
throw
;
}
}
}
}
void
BerkeleyPacketFilter
::
SetFilter
(
pcap_t
*
pcapDescriptor
)
{
if
(
pcap_setfilter
(
pcapDescriptor
,
_bpf
)
!=
0
)
throw
PcapError
::
BuildInvalidOperation
(
"Failed setting bpf filter"
,
pcapDescriptor
);
}
BerkeleyPacketFilter
::~
BerkeleyPacketFilter
()
{
pcap_freecode
(
_bpf
);
delete
_bpf
;
}
PcapDotNet/src/PcapDotNet.Core/BerkeleyPacketFilter.h
View file @
c0c69224
...
@@ -8,6 +8,9 @@ namespace PcapDotNet { namespace Core
...
@@ -8,6 +8,9 @@ namespace PcapDotNet { namespace Core
public
ref
class
BerkeleyPacketFilter
:
System
::
IDisposable
public
ref
class
BerkeleyPacketFilter
:
System
::
IDisposable
{
{
public
:
public
:
BerkeleyPacketFilter
(
System
::
String
^
filterString
,
int
snapshotLength
,
Packets
::
DataLinkKind
kind
,
IpV4SocketAddress
^
netmask
);
BerkeleyPacketFilter
(
System
::
String
^
filterString
,
int
snapshotLength
,
Packets
::
DataLinkKind
kind
);
void
SetFilter
(
pcap_t
*
pcapDescriptor
);
void
SetFilter
(
pcap_t
*
pcapDescriptor
);
~
BerkeleyPacketFilter
();
// IDisposable
~
BerkeleyPacketFilter
();
// IDisposable
...
@@ -15,6 +18,10 @@ namespace PcapDotNet { namespace Core
...
@@ -15,6 +18,10 @@ namespace PcapDotNet { namespace Core
internal
:
internal
:
BerkeleyPacketFilter
(
pcap_t
*
pcapDescriptor
,
System
::
String
^
filterString
,
IpV4SocketAddress
^
netmask
);
BerkeleyPacketFilter
(
pcap_t
*
pcapDescriptor
,
System
::
String
^
filterString
,
IpV4SocketAddress
^
netmask
);
private
:
void
Initialize
(
System
::
String
^
filterString
,
int
snapshotLength
,
Packets
::
DataLinkKind
kind
,
IpV4SocketAddress
^
netmask
);
void
Initialize
(
pcap_t
*
pcapDescriptor
,
System
::
String
^
filterString
,
IpV4SocketAddress
^
netmask
);
private
:
private
:
bpf_program
*
_bpf
;
bpf_program
*
_bpf
;
};
};
...
...
PcapDotNet/src/PcapDotNet.Core/PcapDataLink.cpp
View file @
c0c69224
...
@@ -9,6 +9,11 @@ using namespace System;
...
@@ -9,6 +9,11 @@ using namespace System;
using
namespace
Packets
;
using
namespace
Packets
;
using
namespace
PcapDotNet
::
Core
;
using
namespace
PcapDotNet
::
Core
;
PcapDataLink
::
PcapDataLink
(
DataLinkKind
kind
)
{
_value
=
KindToValue
(
kind
);
}
PcapDataLink
::
PcapDataLink
(
int
value
)
PcapDataLink
::
PcapDataLink
(
int
value
)
{
{
_value
=
value
;
_value
=
value
;
...
@@ -64,3 +69,15 @@ String^ PcapDataLink::ToString()
...
@@ -64,3 +69,15 @@ String^ PcapDataLink::ToString()
{
{
return
Name
+
" ("
+
Description
+
")"
;
return
Name
+
" ("
+
Description
+
")"
;
}
}
// static
int
PcapDataLink
::
KindToValue
(
DataLinkKind
kind
)
{
switch
(
kind
)
{
case
DataLinkKind
:
:
Ethernet
:
return
1
;
default
:
throw
gcnew
NotSupportedException
(
"PcapDataLink kind "
+
kind
.
ToString
()
+
" is unsupported"
);
}
}
PcapDotNet/src/PcapDotNet.Core/PcapDataLink.h
View file @
c0c69224
...
@@ -5,6 +5,7 @@ namespace PcapDotNet { namespace Core
...
@@ -5,6 +5,7 @@ namespace PcapDotNet { namespace Core
public
value
class
PcapDataLink
:
Packets
::
IDataLink
public
value
class
PcapDataLink
:
Packets
::
IDataLink
{
{
public
:
public
:
PcapDataLink
(
Packets
::
DataLinkKind
kind
);
PcapDataLink
(
int
value
);
PcapDataLink
(
int
value
);
PcapDataLink
(
System
::
String
^
name
);
PcapDataLink
(
System
::
String
^
name
);
...
@@ -30,6 +31,9 @@ namespace PcapDotNet { namespace Core
...
@@ -30,6 +31,9 @@ namespace PcapDotNet { namespace Core
virtual
System
::
String
^
ToString
()
override
;
virtual
System
::
String
^
ToString
()
override
;
private
:
static
int
KindToValue
(
Packets
::
DataLinkKind
kind
);
private
:
private
:
int
_value
;
int
_value
;
};
};
...
...
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