3

I want to send a TCP packet (with a custom header) in C#. The building of such packets is no problem, and I have the data in a byte array. But how can I send this packet over a socket?

I tried something like this:

using (Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP))
{
    TcpPacket tcpPacket = new TcpPacket();
    // fill tcpPacket with data
    sock.Bind(new IPEndPoint(MYADDRESS, MYPORT));
    byte[] data = tcpPacket.GetBytes();
    sock.SendTo(data, new IPEndPoint(DESTADDRESS, DESTPORT));
}

This runs without any exception but sniffing the network shows that nothing is send. What is the solution?

I use Windows 7 Professional, and I don't want the system to create the full TCP connection all alone.

PS: I don't want to use some other library.

PS: Building IP packets is not a problem, either.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ordag
  • 2,497
  • 5
  • 26
  • 35

6 Answers6

3

For sending your own crafted TCP packet on Windows 7, you will need a driver like WinPcap. If you use WinPcap, you can use one of the many .NET wrappers or code your own. Sending a raw frame only with objects provided by the Windows API (like sockets) will not work.

Just look in TCP/IP Raw Sockets.

The only alternative would be to create your own network monitoring driver, or to buy a commercial version of WinPcap which does not require installation but integrates seamlessly into your program.

On Windows 7, Windows Vista, Windows XP with Service Pack 2 (SP2), and Windows XP with Service Pack 3 (SP3), the ability to send traffic over raw sockets has been restricted in several ways:

TCP data cannot be sent over raw sockets.

For the case you change your mind. Maybe you can find something you need in the library eExNetworkLibrary.

It includes a WinPcap wrapper and a lot of methods and objects to craft and analyze packets. May it will be useful.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Emiswelt
  • 3,909
  • 1
  • 38
  • 56
1

Are you attempting to send a TCP packet or a UDP packet? If you want to send a TCP packet you need to Connect() to your the remote end point before you attempt to send the packet.

CodingGorilla
  • 19,612
  • 4
  • 45
  • 65
  • Thats my problem. I want to open the connection on my own with my custom packets =/ – ordag Dec 09 '10 at 15:00
  • You can open the connection on your own, just call the `Connect` method on your `sock` object. It seeems to me you're missing 1 line of code here and that's it. – CodingGorilla Dec 09 '10 at 15:02
  • If i Connect() my socket to a closed port i get an exception, but i want to read the Ack-Rst-Packet – ordag Dec 09 '10 at 15:20
1

For TCP you need to connect to the remote endpoint.

It is better if you use the TcpClient Class to create your socket, see TcpClient.Client Property.

Check out the sample in Socket Send and Receive [C#].

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
basarat
  • 261,912
  • 58
  • 460
  • 511
  • yes as you see in http://www.csharp-examples.net/socket-send-receive/ you have access to the underlying socket. PS: Use this to create : http://msdn.microsoft.com/en-us/library/3bsb3c8f.aspx , and to connect http://msdn.microsoft.com/en-us/library/d6kc0793.aspx – basarat Dec 09 '10 at 15:02
  • But the Connect() method opens the full connection, does it? – ordag Dec 09 '10 at 15:26
  • Yes it opens a full connection – basarat Dec 09 '10 at 16:14
  • Then its not what I search, sry should have made my question more precisely, but thx – ordag Dec 09 '10 at 16:30
  • I understand. That can only be done with some pcap derivative. And therefore my second answer. – basarat Dec 09 '10 at 16:57
1

You are Binding your socket but you should Connect() (to the other endpoint).

H H
  • 263,252
  • 30
  • 330
  • 514
1

For making custom packets and sending on the network you should take a look at Pcap.Net. Check out the sample code presented in an answer to Stack Overflow question "IP address spoofing using SharpPcap on C#".

Community
  • 1
  • 1
basarat
  • 261,912
  • 58
  • 460
  • 511
0

You should use SocketType.Stream:

SocketType.Raw: Supports access to the underlying transport protocol. Using the SocketTypeRaw, you can communicate using protocols like Internet Control Message Protocol (Icmp) and Internet Group Management Protocol (Igmp). Your application must provide a complete IP header when sending. Received datagrams return with the IP header and options intact.

SocketType.Stream: Supports reliable, two-way, connection-based byte streams without the duplication of data and without preservation of boundaries. A Socket of this type communicates with a single peer and requires a remote host connection before communication can begin. Stream uses the Transmission Control Protocol (Tcp) ProtocolType and the InterNetworkAddressFamily.

Community
  • 1
  • 1