C# Raw Sockets

A

Adam Clauss

I am (attempting) to move an existing socket application to use raw sockets.

Right now, my application is essentially a port forwarder. Upon receiving a
connection, it will open a connection to an "internal" server and simply
relay all information back and forth (when the client sends something, my
app sends that to the server, when server sends something, my app sends that
to the client).

While what I have right DOES work, the downside is that to the server, all
connections appear to come from the IP address where my program is running.
I want it to instead appear as if it came from the original client IP (as a
true port forwarding router/etc would).

I understand to do this I will have to use Raw sockets, and that I will need
to recreate the IP header. But beyond this, I am not sure where to go.

Partly in a conceptual sense - (HOW to actually create the IP header) and
partly in a literal sense - I tried creating a raw socket:
serverSocket = new Socket(AddressFamily.InterNetwork,
System.Net.Sockets.SocketType.Raw, ProtocolType.Raw);

and then connected to the server as I do for the normal sockets - but the
server did not even register the connection.

Any advise? Examples or good articles on this would be greatly
appreciated!!

Thanks,
Adam Clauss
(e-mail address removed)
 
R

Roy Fine

Adam,

at the very least, you will need to include the IP_HDRINCL socket option
with a value of TRUE. Something like this:

Socket sok = new
Socket(AddressFamily.InterNetwork,System.Net.Sockets.SocketType.Raw,
ProtocolType.Raw);

sok.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.HeaderIncluded,tru
e);

and then you MUST send the 28 bytes of IP header information.

Note - the IP_HDRINCL option, and raw sockets in general are a security
issue in Windows NT and later - and you need to be a member of the
administrators group to create sockes of this type. To disable this
restriction, the value of this registry variable to the value of 1 as DWORD
type:

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Afd\Parameters\DisableR
awSecurity

regards
roy fine
 

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