public void ArpRequest(IPv4 sourceIP, IPv4 targetIP, Multiplexer !targetMux, ArpRequestCallback callback, object cookie, TimeSpan timeout) { // //XXXX Check pending request does not already exist! // EthernetAddress localMac = targetMux.Adapter.HardwareAddress; AddPendingRequest(targetIP, callback, cookie, timeout); // initiate an arp request... DebugPrint("Initiating request " + "({0},{1}) --> ({2},{3})\n", sourceIP, localMac, targetIP, EthernetAddress.Zero); byte[] data = new byte[ArpFormat.Size + EthernetFormat.Size]; int pos = EthernetFormat.Write(data, 0, localMac, EthernetAddress.Broadcast, EthernetFormat.PROTOCOL_ARP); ArpFormat.Write(data, pos, localMac, sourceIP, ArpFormat.Type.ARP_REQUEST, EthernetAddress.Zero, targetIP); NetPacket pkt = new NetPacket(data); pkt.Mux = targetMux; OnProtocolSend(pkt); }
private void SendResponsePacket(DhcpFormat dhcp) { int packetSize = dhcp.Size + UdpFormat.Size + IPFormat.Size + EthernetFormat.Size; byte [] packet = new byte [packetSize]; // Write out DHCP packet int dhcpSize = dhcp.Size; int dhcpOffset = packet.Length - dhcpSize; dhcp.Write(packet, dhcpOffset); // Create UDP Header UdpFormat.UdpHeader udpHeader = new UdpFormat.UdpHeader(); udpHeader.srcPort = DhcpFormat.ServerPort; udpHeader.dstPort = DhcpFormat.ClientPort; udpHeader.length = (ushort)(UdpFormat.Size + dhcpSize); // Create IP Header IPFormat.IPHeader ipHeader = new NetStack.Protocols.IPFormat.IPHeader(); ipHeader.SetDefaults(IPFormat.Protocol.UDP); IPFormat.SetDontFragBit(ipHeader); ipHeader.Source = ServerAddress; ipHeader.Destination = IPv4.Broadcast; ipHeader.totalLength = (ushort)(IPFormat.Size + UdpFormat.Size + dhcpSize); // Write out IP and Header int udpOffset = packet.Length - dhcp.Size - UdpFormat.Size; int ipOffset = udpOffset - IPFormat.Size; UdpFormat.WriteUdpPacket(packet, ipOffset, ref ipHeader, ref udpHeader, packet, dhcpOffset, dhcpSize); // Add Ethernet Header EthernetFormat.Write(packet, 0, ServerMac, EthernetAddress.Broadcast, EthernetFormat.Protocol.IP); NetPacket np = new NetPacket(packet); if (adapter.ReceivePacket(np) == false) { Console.WriteLine("Failed to send packet"); SetState(ServerState.Failed); } }
internal bool Send(EthernetAddress dstAddr, DhcpFormat !dhcp) { int packetSize = dhcp.Size + UdpFormat.Size + IPFormat.Size + EthernetFormat.Size; byte [] packet = new byte [packetSize]; // Write out DHCP packet int dhcpSize = dhcp.Size; int dhcpOffset = packet.Length - dhcpSize; dhcp.Write(packet, dhcpOffset); // Create UDP Header UdpFormat.UdpHeader udpHeader = new UdpFormat.UdpHeader(); udpHeader.srcPort = DhcpFormat.ClientPort; udpHeader.dstPort = DhcpFormat.ServerPort; udpHeader.length = (ushort)(UdpFormat.Size + dhcpSize); // Create IP Header IPFormat.IPHeader ipHeader = new NetStack.Protocols.IPFormat.IPHeader(); ipHeader.SetDefaults(IPFormat.Protocol.UDP); IPFormat.SetDontFragBit(ipHeader); ipHeader.Source = IPv4.Any; ipHeader.Destination = IPv4.Broadcast; ipHeader.totalLength = (ushort)(IPFormat.Size + UdpFormat.Size + dhcpSize); // Write out IP and Header int udpOffset = packet.Length - dhcp.Size - UdpFormat.Size; int ipOffset = udpOffset - IPFormat.Size; UdpFormat.WriteUdpPacket(packet, ipOffset, ipHeader, ref udpHeader, packet, dhcpOffset, dhcpSize); // Add Ethernet Header EthernetFormat.Write(packet, 0, adapter.HardwareAddress, dstAddr, EthernetFormat.PROTOCOL_IP); NetPacket np = new NetPacket(packet); return(udpSession.WritePacket(np)); }