Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get raw TCP packet in C#?

I want to received raw TCP packet and then send it back with same workload.

It should look something like this:

void OnPacketReceived(TcpPacket p)
{
    byte [] body = p.GetBody();
}

NOTE : I need the TCP packet and not the Ethernet frame.

like image 292
Boppity Bop Avatar asked Jun 14 '12 14:06

Boppity Bop


People also ask

How do I create a TCP packet?

Creating a packet In scapy, packets are constructed by defining packet headers for each protocol at different layers of TCP/IP and then stacking these layers in order. To create a DNS query, you need to build Ether(sometimes optional), IP,UDP headers and stack them using / operator.

How do you make a raw socket?

To create a socket of type SOCK_RAW, call the socket or WSASocket function with the af parameter (address family) set to AF_INET or AF_INET6, the type parameter set to SOCK_RAW, and the protocol parameter set to the protocol number required.

What is a raw packet?

Raw packet is used when you dont have any, the first bytes captured are directly the IPv6 or IPv4 header. Raw IP; the packet begins with an IPv4 or IPv6 header, with the "version" field of the header indicating whether it's an IPv4 or IPv6 header.

Where are raw sockets created?

Raw sockets operate at the network OSI level, which means that transport-level headers such as TCP or UDP headers will not be automatically decoded. If you are implementing a a transport-level protocol, you'll have to write code to decode and encode the transport-level headers in your application.


1 Answers

If you implement the socket as a raw socket, you have access to the whole packet (and in fact must handle everything about the packet yourself).

Use SocketType.Raw and ProtocolType.Raw when creating the socket.

Just be aware that you're going to have to handle the details of TCP if you implement your socket this way.

For reference, here is the MSDN documentation about the Socket class: http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx

like image 171
dodexahedron Avatar answered Sep 24 '22 12:09

dodexahedron