public static (string, string, string) NatTypeTestCore(string local, string server, int port) { try { if (string.IsNullOrWhiteSpace(server)) { Debug.WriteLine(@"[ERROR]: Please specify STUN server !"); return(string.Empty, DefaultLocalEnd, string.Empty); } using var socketV4 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); var ipe = ParseEndpoint(local) ?? new IPEndPoint(IPAddress.Any, 0); socketV4.Bind(ipe); var result = StunClient.Query(server, port, socketV4); return( result.NatType.ToString(), socketV4.LocalEndPoint.ToString(), result.NatType != NatType.UdpBlocked ? result.PublicEndPoint.ToString() : string.Empty ); } catch (Exception ex) { Debug.WriteLine($@"[ERROR]: {ex}"); return(string.Empty, DefaultLocalEnd, string.Empty); } }
/// <summary> /// Retrieves an enumerable collection of th XMPP client's external IP /// addresses. /// </summary> /// <returns>An enumerable collection of the XMPP client's external IP /// addresses.</returns> /// <exception cref="NotSupportedException">The external IP address(es) /// could not be determined.</exception> /// <remarks>In simple configurations this should usually yield one external /// IP address only.</remarks> private async Task <IEnumerable <IPAddress> > GetExternalAddresses() { // Use a set so we don't get duplicate addresses. ISet <IPAddress> set = new HashSet <IPAddress>(); try { set.Add(await serverIpCheck.GetExternalAddress()); } catch { // Fall through if server does not support the 'Server IP Check' extension. } // Next, try to retrieve external IP addresses from UPnP-enabled devices. if (UseUPnP) { throw new NotImplementedException(".net Core version doesn't support UPnP"); } // Finally, perform a STUN query. try { set.Add(await StunClient.Query(StunServer.Host, StunServer.Port, 3000)); } catch { // Nothing to do here. } // See if we could gather any external addresses at all. if (set.Count == 0) { throw new NotSupportedException("The external IP address(es) could not " + "be obtained."); } return(set); }
/// <summary> /// Retrieves an enumerable collection of th XMPP client's external IP /// addresses. /// </summary> /// <returns>An enumerable collection of the XMPP client's external IP /// addresses.</returns> /// <exception cref="NotSupportedException">The external IP address(es) /// could not be determined.</exception> /// <remarks>In simple configurations this should usually yield one external /// IP address only.</remarks> IEnumerable <IPAddress> GetExternalAddresses() { // Use a set so we don't get duplicate addresses. ISet <IPAddress> set = new HashSet <IPAddress>(); try { set.Add(serverIpCheck.GetExternalAddress()); } catch { // Fall through if server does not support the 'Server IP Check' extension. } // Next, try to retrieve external IP addresses from UPnP-enabled devices. /*if (UseUPnP) { * try { * foreach (var address in UPnP.GetExternalAddresses()) * set.Add(address); * } catch (Exception) { * // Fall through in case any device querying goes wrong. * } * }*/ // Finally, perform a STUN query. try { set.Add(StunClient.Query(StunServer.Host, StunServer.Port, 3000)); } catch { // Nothing to do here. } // See if we could gather any external addresses at all. if (set.Count == 0) { throw new NotSupportedException("The external IP address(es) could not " + "be obtained."); } return(set); }
private IEnumerable <IPAddress> GetExternalAddresses() { ISet <IPAddress> set = new HashSet <IPAddress>(); try { set.Add(this.serverIpCheck.GetExternalAddress()); } catch { } if (this.UseUPnP) { try { foreach (IPAddress address in UPnP.GetExternalAddresses()) { set.Add(address); } } catch (Exception) { } } try { set.Add(StunClient.Query(this.StunServer.Host, this.StunServer.Port, 0xbb8)); } catch { } if (set.Count == 0) { throw new NotSupportedException("The external IP address(es) could not be obtained."); } return(set); }