Ejemplo n.º 1
0
            static public void configureNetwork()
            {
                // Network configuration
                UPnP upnp = new UPnP();

                if (Config.externalIp != "" && IPAddress.TryParse(Config.externalIp, out _))
                {
                    Config.publicServerIP = Config.externalIp;
                }
                else
                {
                    Config.publicServerIP = "";
                    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()));
                            Config.publicServerIP = local_ip.Address.ToString();
                        }
                    }
                    if (Config.publicServerIP == "")
                    {
                        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(Config.serverPort, primary_local))
                                    {
                                        Config.publicServerIP = public_ip.Result.ToString(); //upnp.getMappedIP();
                                        Logging.info(string.Format("Network configured. Public IP is: {0}", Config.publicServerIP));
                                    }
                                }
                                else
                                {
                                    Logging.warn("UPnP configuration failed.");
                                }
                            }
                        }
                    }
                }
            }
Ejemplo n.º 2
0
            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);
            }
Ejemplo n.º 3
0
 protected override void doFromHash(Dictionary <string, string> data)
 {
     this._network   = IPv4Subnet.FromString(data[TableSpec.FIELD_NETWORK]);
     this._comment   = data[TableSpec.FIELD_COMMENT];
     this._isEnabled = Util.string2bool(data[TableSpec.FIELD_ISENABLED]);
 }