Sending packets over raw sockets

W

White Spirit

I can send data over a raw socket in C#. The part that is currently a
problem is constructing the packet to be sent. To construct the packet,
is it necessary to construct the packet 'by hand' and store it as a
byte[] array, computing the checksum etc before sending? I can't find
any C# classes that deal with packets.
 
J

Jon Skeet [C# MVP]

White said:
I can send data over a raw socket in C#. The part that is currently a
problem is constructing the packet to be sent. To construct the packet,
is it necessary to construct the packet 'by hand' and store it as a
byte[] array, computing the checksum etc before sending? I can't find
any C# classes that deal with packets.

Why do you need to construct the packets by hand? Are you using TCP or
UDP? TCP is stream-based, so normally you just ask the socket to write
data and it splits it into packets as it sees fit.

Jon
 
W

White Spirit

I should add that the purpose is to write a security application that
scans a range of IP addresses using a SYN scan. The problem with raw
sockets being blocked in certain versions of Windows isn't a problem as
I'm using the Mono C# development environment under Linux.
 
J

Jon Skeet [C# MVP]

White said:
I should add that the purpose is to write a security application that
scans a range of IP addresses using a SYN scan. The problem with raw
sockets being blocked in certain versions of Windows isn't a problem as
I'm using the Mono C# development environment under Linux.

Okay - I'm not sure there are any managed classes which give you the
raw access to that extent, at least not in .NET itself. You may find a
third party one, or possibly use P/Invoke to call through to the "raw
C" functions.

Jon
 
B

Ben Voigt

White Spirit said:
I should add that the purpose is to write a security application that scans
a range of IP addresses using a SYN scan. The problem with raw

Why would you expect a class library to take care of domain-specific
behavior? The reason you need to create the packets yourself, is because
the behavior you want is different from what the libraries create, not
because the libraries don't exist.

Of course, it's possible to write a class to expose all the standard
elements of the IP and TCP header... but it becomes quite difficult if you
need to handle arbitrary options. It will also be quite inefficient
compared to direct bit manipulation.

The best would probably to use gather sends to collect the various headers
and payload... C# is just a poor choice for this. Under windows, you could
mix C++/CLI for the lowest layers with C# for business logic. Under mono,
there's nothing like MS IJW. The sooner you switch to a different platform,
one designed for low-level system control, the better.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

| White Spirit wrote:
| > I should add that the purpose is to write a security application that
| > scans a range of IP addresses using a SYN scan. The problem with raw
| > sockets being blocked in certain versions of Windows isn't a problem as
| > I'm using the Mono C# development environment under Linux.
|
| Okay - I'm not sure there are any managed classes which give you the
| raw access to that extent, at least not in .NET itself. You may find a
| third party one, or possibly use P/Invoke to call through to the "raw
| C" functions.

I don't think that P/invoke is included in Mono.

OP:
You should better use C in this case, as you are working in Linux you will
have a big pool of source code you can use.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top