/// <summary> /// Find route "mac" address /// </summary> /// <param name="ip"></param> public unsafe static bool FindRoute(byte[] ip, byte *DestMac) { if (InLocalNetwork(ip)) { ARP.Lookup(ip, DestMac); if (DestMac[0] == 0x00 && DestMac[1] == 0x00 && DestMac[2] == 0x00 && DestMac[3] == 0x00 && DestMac[4] == 0x00 && DestMac[5] == 0x00) { byte[] mac = new byte[6]; for (int i = 0; i < 6; i++) { mac[i] = 0xFF; } ARP.ArpSend(ARP.OP_REQUEST, mac, ip); return(false); } } else { for (int i = 0; i < 6; i++) { DestMac[i] = m_gateway_mac[i]; } } return(true); }
/// <summary> /// FS finddir /// </summary> /// <param name="node">The node</param> /// <param name="name">The name to look for</param> /// <returns>The node</returns> private static unsafe Node findDirImpl(Node node, string name) { byte[] ip = NetworkTools.StringToIp(name); if (ip == null) { return(null); } byte *dstMac = (byte *)Heap.Alloc(6); Memory.Memset(dstMac, 0, 6); ARP.Lookup(ip, dstMac); if (dstMac[0] == 0x00 && dstMac[1] == 0x00 && dstMac[2] == 0x00 && dstMac[3] == 0x00 && dstMac[4] == 0x00 && dstMac[5] == 0x00) { byte[] mac = new byte[6]; for (int i = 0; i < 6; i++) { mac[i] = 0xFF; } ARP.ArpSend(ARP.OP_REQUEST, mac, ip); Heap.Free(mac); return(null); } Heap.Free(dstMac); return(new Node()); }
/// <summary> /// Find route "mac" address /// </summary> /// <param name="ip"></param> public unsafe static bool FindRoute(byte[] ip) { if (InLocalNetwork(ip)) { return(ARP.IpExists(ip)); } return(true); }
/// <summary> /// Set gateway /// </summary> /// <param name="ip">IP</param> public unsafe static void SetGateway(byte[] ip) { /* * We just always assume subnet is 255.255.255.0 */ for (int i = 0; i < 3; i++) { m_ip_local_network[i] = ip[i]; } ARP.Lookup(ip, (byte *)Util.ObjectToVoidPtr(m_gateway_mac)); }
/// <summary> /// Handle packet /// </summary> /// <param name="buffer">Buffer pointer</param> /// <param name="size">Packet size</param> private static unsafe void Handle(byte[] mac, byte *buffer, uint size) { IPV4Header *header = (IPV4Header *)buffer; byte proto = header->Protocol; byte[] ip = Util.PtrToArray(header->Source); // Fake ARP if (!(ip[0] == 255 && ip[1] == 255 && ip[2] == 255 && ip[3] == 255)) { ARP.FindOrAdd(ip, mac); } ushort sz = (ushort)(Byte.ReverseBytes(header->totalLength) - sizeof(IPV4Header)); m_handlers[proto]?.Invoke(ip, buffer + sizeof(IPV4Header), sz); }
/// <summary> /// Connection to IP /// </summary> /// <param name="ip">IP</param> /// <param name="port">Port</param> /// <returns></returns> public static unsafe TCPConnection Connect(byte[] ip, ushort port) { ushort inPort = RequestPort(); int startSeq = Random.Rand(); if (!ARP.IpExists(ip)) { byte[] mac = new byte[6]; for (int i = 0; i < 6; i++) { mac[i] = 0xFF; } ARP.ArpSend(ARP.OP_REQUEST, mac, ip); Heap.Free(mac); } while (!ARP.IpExists(ip)) { Tasking.Yield(); } TCPConnection con = new TCPConnection(); con.DestPort = port; con.InPort = inPort; con.State = TCPConnectionState.CLOSED; con.XID = Random.Rand(); con.SequenceNumber = (uint)startSeq; con.NextSequenceNumber = Byte.ReverseBytes(Byte.ReverseBytes(con.SequenceNumber) + 1); con.Type = TCPConnectionType.CONNECTION; con.ReceiveQueue = new Queue(); for (int i = 0; i < 4; i++) { con.IP[i] = ip[i]; } m_connections[inPort] = con; handleConnection(con, null, null, 0); return(con); }