views:

422

answers:

5

Hi everyone!

I've same data stored in a byte-array. The data contains a IPv4 packet (which contains a udp-packet).

I want to send these 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?

Sorry for my bad English and thanks for your help in advance!!!

+4  A: 

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
thanks... I'm trying! +1 for your nice answer!!!
youllknow
do you think it could work with: Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Raw);
youllknow
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
A: 
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
Thank you very much!!! Can I specify the source ip-address as well?
youllknow
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
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!
youllknow
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IPv4); throws an SocketException when I execute it
youllknow
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
@ben voigt: any suggestions to get rid of the ip+tcp headers on the top of my existing byte[]???
youllknow
A: 

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
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
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).
youllknow
A: 

I found a solution for may problem: see answer at: http://stackoverflow.com/questions/2493384/how-to-fake-source-ip-address-of-a-udp-packet

youllknow
A: 

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