예제 #1
0
        static public void configureNetwork(string externalIp, int port)
        {
            // Network configuration
            UPnP upnp = new UPnP();

            IxianHandler.publicPort = port;

            if (externalIp != "" && IPAddress.TryParse(externalIp, out _))
            {
                IxianHandler.publicIP = externalIp;
            }
            else
            {
                IxianHandler.publicIP = "";
                List <IPAndMask> local_ips = CoreNetworkUtils.GetAllLocalIPAddressesAndMasks();
                foreach (IPAndMask local_ip in local_ips)
                {
                    if (IPv4Subnet.IsPublicIP(local_ip.Address))
                    {
                        Logging.info(String.Format("Public IP detected: {0}, mask {1}.", local_ip.Address.ToString(), local_ip.SubnetMask.ToString()));
                        IxianHandler.publicIP = local_ip.Address.ToString();
                    }
                }
                if (IxianHandler.publicIP == "")
                {
                    IPAddress primary_local = CoreNetworkUtils.GetPrimaryIPAddress();
                    if (primary_local == null)
                    {
                        Logging.warn("Unable to determine primary IP address.");
                    }
                    else
                    {
                        Logging.warn(String.Format("None of the locally configured IP addresses are public. Attempting UPnP..."));
                        Task <IPAddress> public_ip = upnp.GetExternalIPAddress();
                        if (public_ip.Wait(1000))
                        {
                            if (public_ip.Result != null)
                            {
                                Logging.info(String.Format("UPNP-determined public IP: {0}. Attempting to configure a port-forwarding rule.", public_ip.Result.ToString()));
                                if (upnp.MapPublicPort(port, primary_local))
                                {
                                    IxianHandler.publicIP = public_ip.Result.ToString(); //upnp.getMappedIP();
                                    Logging.info(string.Format("Network configured. Public IP is: {0}", IxianHandler.publicIP));
                                }
                                else
                                {
                                    Logging.warn("UPnP configuration failed, please set port forwarding for port {0} manually.", port);
                                }
                            }
                            else
                            {
                                Logging.warn("UPnP configuration failed, please set port forwarding for port {0} manually.", port);
                            }
                        }
                    }
                }
            }
        }
예제 #2
0
        /// <summary>
        ///  Returns the first IPv4, unicast address on the local computer which has a gateway configured and is, therefore,
        ///  probably Internet-connectable.
        /// </summary>
        /// <returns>IP address object</returns>
        public static IPAddress GetPrimaryIPAddress()
        {
            // This is impossible to find, but we return the first IP which has a gateway configured
            List <IPAndMask> ips = new List <IPAndMask>();

            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface nic in nics)
            {
                if (nic.OperationalStatus == OperationalStatus.Up && nic.Supports(NetworkInterfaceComponent.IPv4))
                {
                    IPInterfaceProperties properties = nic.GetIPProperties();
                    if (properties.GatewayAddresses.Count == 0)
                    {
                        continue;
                    }
                    UnicastIPAddressInformationCollection unicast = properties.UnicastAddresses;
                    foreach (UnicastIPAddressInformation unicastIP in unicast)
                    {
                        if (unicastIP.Address.AddressFamily == AddressFamily.InterNetwork)
                        {
                            IPv4Subnet subnet = IPv4Subnet.FromSubnet(unicastIP.Address, unicastIP.IPv4Mask);
                            foreach (GatewayIPAddressInformation gw_addr in properties.GatewayAddresses)
                            {
                                if (gw_addr.Address.AddressFamily == AddressFamily.InterNetwork)
                                {
                                    if (subnet.IsIPInSubnet(gw_addr.Address))
                                    {
                                        return(unicastIP.Address);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(null);
        }