1

I'm trying to send a raw UDP packet with Packet.Net but the device.SendPacket() function only seems to accept link level PDUs which means I have to figure out the source and destination MAC addresses myself. (Using ARP or something)

How can I create an IP packet with Packet.Net but have the correct Ethernet frame generated for me?

takteek
  • 7,020
  • 2
  • 39
  • 70
  • Since you're using UDP, is there any reason why you don't want to simply send it as normal, and let the lower layers handle it all for you? – Brad Jan 30 '12 at 02:13
  • @Brad My current reason is that I need to send data from a port that is already bound on the current computer and I couldn't find a way to do that with .Net without it throwing exceptions. Later I will probably need more control also. – takteek Jan 30 '12 at 02:17
  • @takteek - There is a socket option to allow multiple bindings to the same port. You should be able to set `socket.ExclusiveAddressUse = false;` to do this. – M.Babcock Jan 30 '12 at 02:34
  • @takteek - Also if you're going to go through the pain of managing this using either Packet.NET or SharpPCAP then why bother binding the port in the first place? – M.Babcock Jan 30 '12 at 02:41

1 Answers1

1

You'll have to know the destination mac address. As you suggested the best approach is to use arp to discover the mac address. The source address you can get from the adapter itself. There are examples for both arp and building packets in the source releases of sharppcap and packet.net.

Chris

Author of sharppcap/packet.net

Chris Morgan
  • 1,277
  • 1
  • 13
  • 33
  • Okay, consider this a feature request then. :P It's a bit confusing that device.SendPacket accepts network layer packets without complaint but then fails to send them. Maybe it should be called SendFrame or something. – takteek Jan 31 '12 at 03:20
  • Oh I see what you mean. What if SendPacket() were modified to only take packets of type InternetLinkLayerPacket? That would include Ethernet, LinuxSLL and Ieee80211.RadioPacket and Ieee80211.PpiPacket. That would at least make it clear what type of packet had to be passed in and avoid such a condition. – Chris Morgan Jan 31 '12 at 04:37
  • Would it also be helpful if you could generate an EthernetPacket from an IpPacket? This would be something that could perform the Arp etc. It would have to be a part of SharpPcap and not Packet.Net since SharpPcap handles devices, Packet.Net handles packet parsing etc. Maybe some utility function that would take in an IpPacket and perform the Arp, then return the built EthernetPacket? Not sure if its too specialized since the ARP.Resolve() method is pretty straight forward and there are really only three fields in EthernetPacket to fill in, the src, dest and PayloadPacket. – Chris Morgan Jan 31 '12 at 04:40