/// <summary> /// Builds a new port address given a HostAddress and a socket. /// </summary> /// <param name="address"></param> /// <param name="socket"></param> public PUPPort(HostAddress address, UInt32 socket) { Network = address.Network; Host = address.Host; Socket = socket; }
private void LoadHostTable() { _hostAddressTable = new Dictionary <byte, Dictionary <byte, string> >(); _hostNameTable = new Dictionary <string, HostAddress>(); using (StreamReader sr = new StreamReader(Path.Combine("Conf", "hosts.txt"))) { int lineNumber = 0; while (!sr.EndOfStream) { lineNumber++; // // A line is either: // '#' followed by comment to EOL // <inter-network name> <hostname> // Any whitespace is ignored // // Format for Inter-Network name expressions is either: // network#host# (to specify hosts on another network) // or // host# (to specify hosts on our network) // string line = sr.ReadLine().Trim().ToLowerInvariant(); if (line.StartsWith("#") || String.IsNullOrWhiteSpace(line)) { // Comment or empty, just ignore continue; } // Tokenize on whitespace string[] tokens = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); // We need at least two tokens (inter-network name and one hostname) if (tokens.Length < 2) { // Log warning and continue. Log.Write(LogType.Warning, LogComponent.DirectoryServices, "hosts.txt line {0}: Invalid syntax.", lineNumber); continue; } // First token should be an inter-network name, which should end with '#'. if (!tokens[0].EndsWith("#")) { // Log warning and continue. Log.Write(LogType.Warning, LogComponent.DirectoryServices, "hosts.txt line {0}: Improperly formed inter-network name '{1}'.", lineNumber, tokens[0]); continue; } // tokenize on '#' string[] networkTokens = tokens[0].Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries); HostAddress host = new HostAddress(0, 0); // 1 token means a local name, 2 means on other network, anything else is illegal if (networkTokens.Length == 1) { try { host.Host = Convert.ToByte(networkTokens[0], 8); host.Network = _localHost.Network; } catch { // Log warning and continue. Log.Write(LogType.Warning, LogComponent.DirectoryServices, "hosts.txt line {0}: Invalid host number in inter-network address '{1}'.", lineNumber, tokens[0]); continue; } } else if (networkTokens.Length == 2) { try { host.Network = Convert.ToByte(networkTokens[0], 8); host.Host = Convert.ToByte(networkTokens[1], 8); } catch { // Log warning and continue. Log.Write(LogType.Warning, LogComponent.DirectoryServices, "hosts.txt line {0}: Invalid host or network number in inter-network address '{1}'.", lineNumber, tokens[0]); continue; } } else { // Log warning and continue. Log.Write(LogType.Warning, LogComponent.DirectoryServices, "hosts.txt line {0}: Improperly formed inter-network name '{1}'.", lineNumber, tokens[0]); continue; } // Hash the host by one or more names for (int i = 1; i < tokens.Length; i++) { string hostName = tokens[i]; // Add to name table if (_hostNameTable.ContainsKey(hostName)) { // Duplicate name entry! Skip this line. Log.Write(LogType.Warning, LogComponent.DirectoryServices, "hosts.txt line {0}: Duplicate hostname '{1}'.", lineNumber, hostName); break; } _hostNameTable.Add(hostName, host); // Add to address table: // - See if network has entry Dictionary <byte, string> networkTable = null; if (_hostAddressTable.ContainsKey(host.Network)) { networkTable = _hostAddressTable[host.Network]; } else { // No entry for this network yet, add it now networkTable = new Dictionary <byte, string>(); _hostAddressTable.Add(host.Network, networkTable); } // Add to network table if (networkTable.ContainsKey(host.Host)) { // Duplicate host entry! Skip this line. Log.Write(LogType.Warning, LogComponent.DirectoryServices, "hosts.txt line {0}: Duplicate host ID '{1}'.", lineNumber, host.Host); break; } networkTable.Add(host.Host, hostName); } } } }