/// <summary>Returns InetAddress from peer.</summary> /// <remarks> /// Returns InetAddress from peer. The getRemoteAddressString has the form /// [host][/ip-address]:port. The host may be missing. The IP address (and /// preceding '/') may be missing. The port preceded by ':' is always present. /// </remarks> /// <param name="peer"/> /// <returns>InetAddress from peer</returns> public static IPAddress GetPeerAddress(Peer peer) { string remoteAddr = peer.GetRemoteAddressString().Split(":")[0]; int slashIdx = remoteAddr.IndexOf('/'); return(InetAddresses.ForString(slashIdx != -1 ? Sharpen.Runtime.Substring(remoteAddr , slashIdx + 1, remoteAddr.Length) : remoteAddr)); }
/// <summary>Given a full hostname, return the word upto the first dot.</summary> /// <param name="fullHostname">the full hostname</param> /// <returns>the hostname to the first dot</returns> public static string SimpleHostname(string fullHostname) { if (InetAddresses.IsInetAddress(fullHostname)) { return(fullHostname); } int offset = fullHostname.IndexOf('.'); if (offset != -1) { return(Runtime.Substring(fullHostname, 0, offset)); } return(fullHostname); }
public Endpoint Invert() { return(new Endpoint(InetAddresses.IP4ToInt(this.ipv4), (short)(port ?? 0), serviceName.ToLower())); }
public override string ToString() { return(InetAddresses.ToAddrString(addr.Address) + ':' + addr.Port); }
/// <summary>Accepts a collection of ip/cidr/host addresses</summary> /// <param name="hostEntries"/> /// <param name="addressFactory">addressFactory to convert host to InetAddress</param> public MachineList(ICollection <string> hostEntries, MachineList.InetAddressFactory addressFactory) { this.addressFactory = addressFactory; if (hostEntries != null) { if ((hostEntries.Count == 1) && (hostEntries.Contains(WildcardValue))) { all = true; ipAddresses = null; hostNames = null; cidrAddresses = null; } else { all = false; ICollection <string> ips = new HashSet <string>(); IList <SubnetUtils.SubnetInfo> cidrs = new List <SubnetUtils.SubnetInfo>(); ICollection <string> hosts = new HashSet <string>(); foreach (string hostEntry in hostEntries) { //ip address range if (hostEntry.IndexOf("/") > -1) { try { SubnetUtils subnet = new SubnetUtils(hostEntry); subnet.SetInclusiveHostCount(true); cidrs.AddItem(subnet.GetInfo()); } catch (ArgumentException e) { Log.Warn("Invalid CIDR syntax : " + hostEntry); throw; } } else { if (InetAddresses.IsInetAddress(hostEntry)) { //ip address ips.AddItem(hostEntry); } else { //hostname hosts.AddItem(hostEntry); } } } ipAddresses = (ips.Count > 0) ? ips : null; cidrAddresses = (cidrs.Count > 0) ? cidrs : null; hostNames = (hosts.Count > 0) ? hosts : null; } } else { all = false; ipAddresses = null; hostNames = null; cidrAddresses = null; } }