CGC_Codes

Get IP address from notation or hostname

Mar 13th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Net;
  5. using System.Text.RegularExpressions;
  6. using System.Globalization;
  7. using System.Net.Sockets;
  8. using System.Runtime.InteropServices;
  9. using System.Diagnostics;
  10. using System.Net.NetworkInformation;
  11.  
  12. namespace Lidgren.Network
  13. {
  14.   public static class NetUtility
  15.   {
  16.     private static Regex s_regIP;
  17.  
  18.     public static IPAddress Resolve(string ipOrHost)
  19.     {
  20.       if (string.IsNullOrEmpty(ipOrHost))
  21.         throw new ArgumentException("Supplied string must not be empty", "ipOrHost");
  22.  
  23.       ipOrHost = ipOrHost.Trim();
  24.  
  25.       if (s_regIP == null)
  26.       {
  27.         string expression = "\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b";
  28.         RegexOptions options = RegexOptions.Compiled;
  29.         s_regIP = new Regex(expression, options);
  30.       }
  31.  
  32.       IPAddress ipAddress = null;
  33.       if (s_regIP.Match(ipOrHost).Success && IPAddress.TryParse(ipOrHost, out ipAddress))
  34.         return ipAddress;
  35.  
  36.       IPHostEntry entry;
  37.       try
  38.       {
  39.         entry = Dns.GetHostEntry(ipOrHost);
  40.         if (entry == null)
  41.           return null;
  42.  
  43.         foreach (IPAddress ipCurrent in entry.AddressList)
  44.         {
  45.           string sIP = ipCurrent.ToString();
  46.           bool isIP = s_regIP.Match(sIP).Success && IPAddress.TryParse(sIP, out ipAddress);
  47.           if (isIP)
  48.             break;
  49.         }
  50.         if (ipAddress == null)
  51.           return null;
  52.  
  53.         return ipAddress;
  54.       }
  55.       catch (SocketException ex)
  56.       {
  57.         if (ex.SocketErrorCode == SocketError.HostNotFound)
  58.         {
  59.           //LogWrite(string.Format(CultureInfo.InvariantCulture, "Failed to resolve host '{0}'.", ipOrHost));
  60.           return null;
  61.         }
  62.         else
  63.         {
  64.           throw;
  65.         }
  66.       }
  67.     }
  68.  
  69.     private static NetworkInterface GetNetworkInterface()
  70.     {
  71.       IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
  72.       if (computerProperties == null)
  73.         return null;
  74.  
  75.         NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
  76.       if (nics == null || nics.Length < 1)
  77.         return null;
  78.  
  79.       foreach (NetworkInterface adapter in nics)
  80.       {
  81.         if (adapter.OperationalStatus != OperationalStatus.Up)
  82.           continue;
  83.         if (adapter.NetworkInterfaceType == NetworkInterfaceType.Loopback || adapter.NetworkInterfaceType == NetworkInterfaceType.Unknown)
  84.           continue;
  85.         if (!adapter.Supports(NetworkInterfaceComponent.IPv4))
  86.           continue;
  87.  
  88.         return adapter;
  89.       }
  90.       return null;
  91.     }
  92.  
  93.   }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment