I am a student trying to learn more about the ARP and sockets in C#
To do this I am trying to send ARP requests and replies using a raw Socket
in C#.
I have manually reconstructed an ARP reply in an byte array and I am trying to send it using the Socket.Send
method.
static void Main(string[] args)
{
// Create a raw socket
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Raw);
// Setup ARP headers
byte[] buffer = new byte[]
{
0x34, 0x97, 0xf6, 0x22, 0x04, 0xe8, // Ethernet Destination mac
0x70, 0x1c, 0xe7, 0x51, 0x94, 0x0b, // Ethernet source mac
0x08, 0x06, // Type: ARP
00, 0x01, // Hardware type: Ethernet
0x08, 0x00, // Protocol type: IPv4
0x06, // Hardware size: 6
0x04, // Protocol size: 4
00, 0x02, // Opcode: Reply
0x70, 0x1c, 0xe7, 0x51, 0x94, 0x0b, // Sender mac addr
0xc0, 0xa8, 0x01, 0x34, // Sender IP addr 192.168.1.52
0x34, 0x97, 0xf6, 0x22, 0x04, 0xe8, // Target mac addr
0xc0, 0xa8, 0x01, 0x01 // Target ip addr 192.168.1.1
};
// Send ARP reply
socket.Send(buffer, buffer.Length, SocketFlags.None);
Console.ReadKey();
}
When I try to run this code, the application throws a SocketException
:
A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
Tho, to my understanding, I have supplied the destination MAC address in the request.
How should I correctly send an ARP reply(/request) using a Socket
?
PS: I know there is probably a library for this, but I think I won't learn much about sockets and ARP when using a library.