4

I have some data stored in a byte-array. The data contains an IPv4 packet (which contains a UDP packet).

I want to send this array raw over the network using C# (preferred) or C++. I don't want to use C#'s udp-client for example.

Does anyone know how to perform this?

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
raisyn
  • 4,514
  • 9
  • 36
  • 55

4 Answers4

5

Try raw sockets (specify SOCK_RAW for the socket type).
You will be responsible for calculating the IP checksums as well. This can be a little annoying.

Matthias Wandel
  • 6,383
  • 10
  • 33
  • 31
  • thanks... I'm trying! +1 for your nice answer!!! – raisyn Mar 22 '10 at 17:54
  • do you think it could work with: Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Raw); – raisyn Mar 22 '10 at 17:57
  • Just so you know, receiving via raw sockets on Windows requires admin privileges. Transmitting via raw sockets is restricted in several ways beginning with Windows XP SP2 (http://msdn.microsoft.com/en-us/library/ms740548(VS.85).aspx). – Matt Davis Mar 22 '10 at 20:14
2
using System.Net;
using System.Net.Sockets;

public class Test
{
    public void Send(byte[] rawData, IPEndPoint target)
    {
        // change what you pass to this constructor to your needs
        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IPv4);

        try
        {
            s.Connect(target);
            s.Send(rawData);
        }
        catch(Exception ex)
        {
            // handle this exception
        }
    }
}
John Ruiz
  • 2,371
  • 3
  • 20
  • 29
  • Thank you very much!!! Can I specify the source ip-address as well? – raisyn Mar 22 '10 at 18:08
  • 1
    That's going to add IP+TCP headers on top of your existing byte[]. Doesn't your packet contain the destination address in the header already, why would you then specify _target_ when sending it? – Ben Voigt Mar 22 '10 at 18:12
  • yes the rawData-Array already contains a fully-formatted ipv4 packet!!! (so source and destination are specified in the array). I just want to array! `s.Connect(target);` confused me! – raisyn Mar 22 '10 at 18:16
  • Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IPv4); throws an SocketException when I execute it – raisyn Mar 22 '10 at 18:23
  • to get rid of the exception, use ProtocolType.Tcp instead of IPv4. But I agree with Ben Voigt - unless you want IP+TCP headers on top of your existing byte[], you should not use the code I gave you. If you don't care, it will work fine. – John Ruiz Mar 22 '10 at 18:34
  • @ben voigt: any suggestions to get rid of the ip+tcp headers on the top of my existing byte[]??? – raisyn Mar 22 '10 at 18:40
0

Here is a way to send raw data over NIC http://www.codeproject.com/KB/IP/sendrawpacket.aspx As mentioned above Windows is restricting raw socket operations, you have to modify NDIS driver to be able to send whatever you want. Of course you will then have a problem with digital driver signing on Vista/7 (can be temporary bypassed with test mode).

raf
  • 156
  • 1
  • 3
-1

When you have raw data (ie a byte-array) and you want to send it over a network, then you need some sort of encoding:

  1. If you send multiple blocks (whole arrays), the recipient needs to be able to differentiate between the end of one and the start of the next.
  2. If the data is really large its is better to split it into smaller blocks (yes, packets) to play well with other users of the network.
  3. You need to know that the data is error-free at the client as networks have the tendency to be unreliable at exactly the wrong time for you.

Encoding solves the first point above.
TCP is the conventional solution to the second two points.

Examples of encoding are:

  • HTTP encodes the length in cr delimited lines, then a pure binary blob.
  • Text Files could be ctrl-z delimited.
  • XML can be delimited simply by its syntax of tags.
quamrana
  • 37,849
  • 12
  • 53
  • 71
  • the byte[] already contains an IPv4 packet. He doesn't need more encoding, he needs to stop the TCP/IP stack from encoding it further (by adding another IP+UDP header envelope, etc). – Ben Voigt Mar 22 '10 at 18:10
  • the array is very small, it fits easily into one packet. The main problem is that the metainformation (source-ip, destination-ip...) should be send raw (there are stored in the array). – raisyn Mar 22 '10 at 18:11