Socket.SendTo() and headerincluded

  • Thread starter Thread starter Just
  • Start date Start date
J

Just

Hi everybody



I was wondering if it is possible to use SendTo to send a whole IP package
including header to another IP than specified in the package. Say I have a
complete package and in it the destination IP is set to 10.0.0.3, but now I
want to send the package to a server which has destination IP 10.0.0.2



The reason for this is that the server has to act as a router and find the
best route for the package, and therefore the original IP in the package
cannot be altered.



My plan was to do following.



Sock.SetSocketOption(SocketOptionName.IP, SocketOptionLevel.HeaderIncluded,
1);

Sock.sendTo( ServerEndpoint, IPpackage );



But is this possible, or do I have to do something else?



Any help would be appreciated, Thanks.

/Just
 
Hey,

I don't think you can say: "I want to send this packet to ip1, but be sure
to go past ip 2". Else, if ip1 is available, what the heck should ip2 do
with the packet? The protocols itself take care of routing.
If you want to route it, just send it to 10.0.0.2, and add the dest. ip as
data, so send this to 10.0.0.2:
DEST.IPADDRESS | NORMAL DATA

Then you can forward it yourself. I think this is the easiest way.

Greetz,
-- Rob.
 
I don't think you can say: "I want to send this packet to ip1, but be sure
to go past ip 2". Else, if ip1 is available, what the heck should ip2 do
with the packet?

The package only goes into my program if ip1 is not available, because the
clients use the server as gateway, so I recieve an ippackage with another IP
than the servers, and now the tricky part is to send the data to the next
router, if the klient itself is not avaliable on the servers second network
card, and then it is the next routers job to forward to the client.

At this point i am considering to use a NDIS driver, and write an wrapper,
so I can acces the networkcards directly..

/Just
 
Just said:
Hi everybody

I was wondering if it is possible to use SendTo to send a whole IP package
including header to another IP than specified in the package. Say I have a
complete package and in it the destination IP is set to 10.0.0.3, but now I
want to send the package to a server which has destination IP 10.0.0.2
Just -

You can do this using a raw socket type, but you have to build the
complete IP header, including the checksum values. A short sample
would be:

Socket s = new Socket(AddressFamily.InterNetwork,
SocketType.Raw,
ProtocolType.IP);
IPEndPoint iep = new
IPEndPoint(IPAddress.Parse("192.168.1.1"),0);
s.Bind(iep);
byte[] data = {0x45, 0x00, 0x00, 0x30, 0xc8, 0x40, 0x40, 0x00,
0x80,
0x06, 0x00, 0x00, 0xC0, 0xA8, 0x01, 0x01, 0xC0, 0xA8, 0x01,
0x05,
0x07, 0xe5, 0x00, 0x17, 0xd3, 0x16, 0x3f, 0xe6, 0x00, 0x00,
0x00, 0x00, 0x70, 0x02, 0xff, 0xff, 0xe4, 0xcf, 0x00, 0x00,
0x02, 0x04, 0x05, 0xb4, 0x01, 0x01, 0x04, 0x02};

IPEndPoint fakeIP = new
IPEndPoint(IPAddress.Parse("192.168.1.5"), 0);
s.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.HeaderIncluded, 1);
s.SendTo(data, data.Length, SocketFlags.None, fakeIP);

The first IPEndPoint value must point to a valid local IP address
so .Net knows what interface to send the packet out on. The second
IPEndPoint value can point to any fake address, it is just there for
filler in the SendTo() method. The IP addresses specified in the raw
data are what appear in the outbound packet. Hope this helps solve
your problem. Good luck.

Rich Blum - Author
"C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176
"Network Performance Open Source Toolkit" (Wiley)
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0471433012.html
 
Rich Blum said:
"Just" <[email protected]> wrote in message
Hi everybody

I was wondering if it is possible to use SendTo to send a whole IP package
including header to another IP than specified in the package. Say I have a
complete package and in it the destination IP is set to 10.0.0.3, but now I
want to send the package to a server which has destination IP 10.0.0.2
Just -

You can do this using a raw socket type, but you have to build the
complete IP header, including the checksum values. A short sample
would be:

Socket s = new Socket(AddressFamily.InterNetwork,
SocketType.Raw,
ProtocolType.IP);
IPEndPoint iep = new
IPEndPoint(IPAddress.Parse("192.168.1.1"),0);
s.Bind(iep);
byte[] data = {0x45, 0x00, 0x00, 0x30, 0xc8, 0x40, 0x40, 0x00,
0x80,
0x06, 0x00, 0x00, 0xC0, 0xA8, 0x01, 0x01, 0xC0, 0xA8, 0x01,
0x05,
0x07, 0xe5, 0x00, 0x17, 0xd3, 0x16, 0x3f, 0xe6, 0x00, 0x00,
0x00, 0x00, 0x70, 0x02, 0xff, 0xff, 0xe4, 0xcf, 0x00, 0x00,
0x02, 0x04, 0x05, 0xb4, 0x01, 0x01, 0x04, 0x02};

IPEndPoint fakeIP = new
IPEndPoint(IPAddress.Parse("192.168.1.5"), 0);
s.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.HeaderIncluded, 1);
s.SendTo(data, data.Length, SocketFlags.None, fakeIP);

The first IPEndPoint value must point to a valid local IP address
so .Net knows what interface to send the packet out on. The second
IPEndPoint value can point to any fake address, it is just there for
filler in the SendTo() method. The IP addresses specified in the raw
data are what appear in the outbound packet. Hope this helps solve
your problem. Good luck.

Thanks for the answer...



From that example I can conclude that I can not send an ippackage with the
sendTo function and expect it to go to the specified destination (because of
the fake endpoint).

The problem is, that if I have a package with a destination which is not on
my LAN, I have to send it to the next router, and this means that I cannot
alter the IP destination address, just modify the destination MAC address.
But this is properly to low-level for C#.

I hoped that the sendTo function, somehow was connected to the Datalink
layer and just passed a destination to the datalink layer and here the
destination MAC address was attached.

Now I can see that I have to use an NDIS driver or similar to go down to the
layer and manipulate the package.

Rich Blum - Author
"C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176

BTW great book, bought it a couple of weeks ago. :o)
 
Back
Top