Author Topic: UPnP.OpenTCP and UPnP.OpenUDP are opening port on 127.0.0.1  (Read 6966 times)

pyscho2day

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 74
    • View Profile
UPnP.OpenTCP and UPnP.OpenUDP are opening port on 127.0.0.1
« on: December 10, 2013, 11:57:09 PM »
This is not happening all of the time and it hasn't happened in while.  I have not modified any of your code that would control that what so ever.  Would it be possible to get a check in the logic that if for some reason it get 127.0.0.1 when it tries to open the ports to try again? Or maybe to kick out an exception.

I currently check that the ports open and I don't get an error, but this is just as bad as an error and so far i have not found a way with out modifying your code to find out what ports were opened.

Not in a rush but if you could put this on your radar it would be appreciated.  Also if anyone else has had this issue post here. if no one then maybe I am just the lucky one.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UPnP.OpenTCP and UPnP.OpenUDP are opening port on 127.0.0.1
« Reply #1 on: December 11, 2013, 02:17:23 AM »
I'm not quite clear on this one... what do you mean? 127.0.0.1 is a loopback address pointing to your machine, so I am not quite sure what it has to do with open ports.

Do you mean that it doesn't detect the correct IP sometimes and sends that info to the lobby server?

pyscho2day

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 74
    • View Profile
Re: UPnP.OpenTCP and UPnP.OpenUDP are opening port on 127.0.0.1
« Reply #2 on: December 11, 2013, 10:13:03 AM »
Actually the lobby server just gets 2 entries for the local ip, not the local ip and the outside ip. The issue is that the ports being opened on the router get the 127.0.0.1 ip address assigned to them and not the local ip 10.0.0.### that it should.  When this happens the router does not know where to send the incoming traffic.

Rogdor

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: UPnP.OpenTCP and UPnP.OpenUDP are opening port on 127.0.0.1
« Reply #3 on: December 11, 2013, 05:55:36 PM »
I'm not sure if this is exactly related:

For me, the TNet server sets the external IP incorrectly, as the same as my internal. This happens about a third of the time when I launch it. TNet.Tools.externalAddress also randomly grabs the internal address sometimes.

pyscho2day

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 74
    • View Profile
Re: UPnP.OpenTCP and UPnP.OpenUDP are opening port on 127.0.0.1
« Reply #4 on: December 11, 2013, 11:17:21 PM »
I did some testing to night and every time the upnp ports were opened with the localhost ip the ips that were reported to the lobby server were both the private ip not the public ip.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UPnP.OpenTCP and UPnP.OpenUDP are opening port on 127.0.0.1
« Reply #5 on: December 12, 2013, 06:27:34 AM »
The external IP detection method involves querying a remote website, so it may not always work. Ideally you would put the query script on your own server instead. I did that with starlink. It's a short PHP script that I've posted before.
  1. <?php
  2. echo 'Current IP Address: ' . $_SERVER['REMOTE_ADDR'];
  3. ?>

pyscho2day

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 74
    • View Profile
Re: UPnP.OpenTCP and UPnP.OpenUDP are opening port on 127.0.0.1
« Reply #6 on: December 12, 2013, 11:05:40 AM »
I do use the detection on an external website, and if it was not reachable that would be fine.  I am more concerned with the ports being opened on 127.0.0.1.   I made a small script copying what you do in the TNTools.cs to obtain the local ip and verify it. I let this run for 4 hours and not once did i get 127.0.0.1 as the local (lan) ip.  I then tried to start a server and the first time it used 127.0.0.1 as the ip to open the ports on.  I am trying to avoid modifying your code as when we get an update it gets wiped out. I thought about inheriting the TNet.UPnP but there are protected members that are not accessible (one being the local ip). I was going to do that so i could check the ip and if it was the 127.0.0.1 the check again to make sure i get the correct local ip.

I had put a fix in version 1.7.x on mine in the past but have updated since then. What I did was get the local ip and put that into a variable and then checked it, if it was 127.0.0.1 i would then get and check again.

I believe this is what I had added. (* in front of now or edited lines)
  1. void Open (int port, bool tcp, OnPortRequest callback)
  2. {
  3.   int id = (port << 8) | (tcp ? 1 : 0);
  4.  
  5.   if (port > 0 && !mPorts.Contains(id) && mStatus != Status.Failure)
  6.   {
  7.     mPorts.Add(id);
  8. *   IPAddress tempLocal = Tools.localAddress;
  9. *   for (int i = 0; i < 10; i++)
  10. *   {
  11. *     if (tempLocal.ToString() != "127.0.0.1")
  12. *     {
  13. *       return;
  14. *     }
  15. *     else
  16. *     {
  17. *       tempLocal = Tools.localAddress;
  18. *     }
  19. *   }
  20.  
  21.     ExtraParams xp = new ExtraParams();
  22.     xp.callback = callback;
  23.     xp.port = port;
  24.     xp.protocol = tcp ? ProtocolType.Tcp : ProtocolType.Udp;
  25.     xp.action = "AddPortMapping";
  26.     xp.request = "<NewRemoteHost></NewRemoteHost>\n" +
  27.       "<NewExternalPort>" + port + "</NewExternalPort>\n" +
  28.       "<NewProtocol>" + (tcp ? "TCP" : "UDP") + "</NewProtocol>\n" +
  29.       "<NewInternalPort>" + port + "</NewInternalPort>\n" +
  30. *     "<NewInternalClient>" + tempLocal + "</NewInternalClient>\n" +
  31.       "<NewEnabled>1</NewEnabled>\n" +
  32.       "<NewPortMappingDescription>" + name + "</NewPortMappingDescription>\n" +
  33.       "<NewLeaseDuration>0</NewLeaseDuration>\n";
  34.  
  35.     xp.th = new Thread(OpenRequest);
  36.     lock (mThreads) mThreads.Add(xp.th);
  37.     xp.th.Start(xp);
  38.   }
  39.   else if (callback != null)
  40.   {
  41.     callback(this, port, tcp ? ProtocolType.Tcp : ProtocolType.Udp, false);
  42.   }
  43. }
  44.  

I am at work right now so I can not verify, but if you would like I can try this when I get home and make sure that 127.0.0.1 is no longer being used.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UPnP.OpenTCP and UPnP.OpenUDP are opening port on 127.0.0.1
« Reply #7 on: December 12, 2013, 11:10:51 AM »
Certainly, and keep in mind you can just email me your changes (provided you used the latest version which is currently 1.8.4).

pyscho2day

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 74
    • View Profile
Re: UPnP.OpenTCP and UPnP.OpenUDP are opening port on 127.0.0.1
« Reply #8 on: February 03, 2014, 11:47:46 AM »
Aren,

I have been running this fix for a month now and have no seen the 127.0.0.1 show up when opening ports.

Note: Lines with a * are added/modified.
  1.         void Open (int port, bool tcp, OnPortRequest callback)
  2.         {
  3.                 int id = (port << 8) | (tcp ? 1 : 0);
  4.  
  5.                 if (port > 0 && !mPorts.Contains(id) && mStatus != Status.Failure)
  6.                 {
  7.                         mPorts.Add(id);
  8.  
  9. *                       IPAddress tempLocal = Tools.localAddress;
  10. *                       int tempCnt = 0;
  11. *                       while (tempLocal.ToString() == "127.0.0.1" && tempCnt < 20)
  12. *                       {
  13. *                               tempCnt++;
  14. *                               tempLocal = Tools.localAddress;
  15. *                               Thread.Sleep(10);
  16. *                       }
  17.  
  18.  
  19.                         ExtraParams xp = new ExtraParams();
  20.                         xp.callback = callback;
  21.                         xp.port = port;
  22.                         xp.protocol = tcp ? ProtocolType.Tcp : ProtocolType.Udp;
  23.                         xp.action = "AddPortMapping";
  24.                         xp.request = "<NewRemoteHost></NewRemoteHost>\n" +
  25.                                 "<NewExternalPort>" + port + "</NewExternalPort>\n" +
  26.                                 "<NewProtocol>" + (tcp ? "TCP" : "UDP") + "</NewProtocol>\n" +
  27.                                 "<NewInternalPort>" + port + "</NewInternalPort>\n" +
  28. *                               "<NewInternalClient>" + tempLocal + "</NewInternalClient>\n" +
  29.                                 "<NewEnabled>1</NewEnabled>\n" +
  30.                                 "<NewPortMappingDescription>" + name + "</NewPortMappingDescription>\n" +
  31.                                 "<NewLeaseDuration>0</NewLeaseDuration>\n";
  32.  
  33.                         xp.th = new Thread(OpenRequest);
  34.                         lock (mThreads) mThreads.Add(xp.th);
  35.                         xp.th.Start(xp);
  36.                 }
  37.                 else if (callback != null)
  38.                 {
  39.                         callback(this, port, tcp ? ProtocolType.Tcp : ProtocolType.Udp, false);
  40.                 }
  41.         }

edit: Added a couple line to exit the while loop if a suitable ip can not be obtained. Thanks to @GantZ_Yaka for letting me know that he has a laptop that is never able to get the local ip and always returns the 127.0.0.1
« Last Edit: February 03, 2014, 10:04:32 PM by pyscho2day »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UPnP.OpenTCP and UPnP.OpenUDP are opening port on 127.0.0.1
« Reply #9 on: February 04, 2014, 03:59:33 AM »
I've actually modified TNTools.localAddress to use GetHostAddresses instead in the Pro version. I wonder if this would also resolve the DNS issue?
  1.         static public IPAddress localAddress
  2.         {
  3.                 get
  4.                 {
  5.                         if (mLocalAddress == null)
  6.                         {
  7. #if UNITY_IPHONE
  8.                                 NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();
  9.  
  10.                                 foreach (NetworkInterface ni in nis)
  11.                                 {
  12.                                         IPInterfaceProperties IPInterfaceProperties = ni.GetIPProperties();
  13.                                         UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = IPInterfaceProperties.UnicastAddresses;
  14.  
  15.                                         foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection)
  16.                                         {
  17.                                                 if (UnicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)
  18.                                                 {
  19.                                                         mLocalAddress = UnicastIPAddressInformation.Address;
  20.                                                         break;
  21.                                                 }
  22.                                         }
  23.                                 }
  24. #else
  25.                                 try
  26.                                 {
  27.                                         IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());
  28.  
  29.                                         for (int i = 0; i < ips.Length; ++i)
  30.                                         {
  31.                                                 if (IsValidAddress(ips[i]))
  32.                                                 {
  33.                                                         mLocalAddress = ips[i];
  34.                                                         break;
  35.                                                 }
  36.                                         }
  37.  
  38.                                         //IPHostEntry ent = Dns.GetHostEntry(Dns.GetHostName());
  39.  
  40.                                         //foreach (IPAddress ip in ent.AddressList)
  41.                                         //{
  42.                                         //    if (IsValidAddress(ip))
  43.                                         //    {
  44.                                         //        mLocalAddress = ip;
  45.                                         //        break;
  46.                                         //    }
  47.                                         //}
  48.                                 }
  49. #if DEBUG
  50.                                 catch (System.Exception ex)
  51.                                 {
  52.                                         System.Console.WriteLine("TNTools.LocalAddress: " + ex.Message);
  53.                                         mLocalAddress = IPAddress.Loopback;
  54.                                 }
  55. #else
  56.                                 catch (System.Exception) { mLocalAddress = IPAddress.Loopback; }
  57. #endif // DEBUG
  58. #endif // UNITY_IPHONE
  59.                         }
  60.                         return mLocalAddress;
  61.                 }
  62.         }

pyscho2day

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 74
    • View Profile
Re: UPnP.OpenTCP and UPnP.OpenUDP are opening port on 127.0.0.1
« Reply #10 on: February 04, 2014, 10:40:48 AM »
That is possible. Unfortunatly I don't have the pro license yet.  Once I build my new pc I will probably upgrade as its only 30$ more.

Side note, @GantZ_Yaka was having issues with one of his machines always returning with 127.0.0.1 and here is what he found.  (he was messaging me directly)

Quote from: GantZ_Yaka
Windows 7,  I found the reason due to which the IP was always taken 127.0.0.1.
Fault location:
IPHostEntry iphostentry = Dns.GetHostByName(strHostName);
Im from russia and in the name of the computer (My computer - Properties) except the letters were also Russian letters.
When iphostentry  is taken not only of the Latin letters, the program itself could not find the local IP using that Hostname.
When I changed the network name in Latin letters, everything was working correctly

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UPnP.OpenTCP and UPnP.OpenUDP are opening port on 127.0.0.1
« Reply #11 on: February 05, 2014, 04:46:57 PM »
You don't need the Pro license, I posted the code I changed.