2

I have written a simple C# application for desktop sharing using Windows desktop sharing API

http://msdn.microsoft.com/en-us/library/bb968809.aspx http://blogs.msdn.com/b/rds/archive/2007/03/08/windows-desktop-sharing-api.aspx

Most of my application code is based on this Microsoft's blog post http://blogs.msdn.com/b/rds/archive/2007/03/23/writing-a-desktop-sharing-application.aspx

I have also successfully implemented Reverse Connect http://msdn.microsoft.com/en-us/library/aa373312.aspx

My app works great within local network or VPN but I am unable to share desktop to PC's that are on public networks. Invitation generated by my host application looks something like this

<E>
  <A KH="3tSA+NXzzvG8ynVkXTh0RxsPCus=" ID="DIC/0Flybjfj3U5lPvy5B2TWwShPrX1oIkpUB0vrB4mZsjZWY7WCfGnvEUjwhvhK"/>
  <C>
    <T ID="1" SID="0">
      <L P="51390" N="fe80::596d:88b0:2ef6:bf13%4"/>
      <L P="51391" N="fe80::103c:155b:b1fc:9854%9"/>
      <L P="51392" N="2001:0:9d38:6abd:103c:155b:b1fc:9854"/>
      <L P="51393" N="2002:c31d:e328:1000:981c:91bc:adbd:6703"/>
      <L P="51394" N="2002:c31d:e328:1000:c878:a88a:f1fb:f25d"/>
      <L P="51395" N="fe80::981c:91bc:adbd:6703%10"/>
      <L P="51396" N="192.168.1.36"/>
    </T>
  </C>
</E>

I have noticed that there is only local IP address in this invitation. Am I doing something wrong here? Is it even possible to share desktop between PC on public network using Windows Desktop Sharing API?

If not, what are my options? Other then setting up VPN? Thank you

bouvierr
  • 3,563
  • 3
  • 27
  • 32
partyelite
  • 822
  • 1
  • 15
  • 26

4 Answers4

1

You could use this:

// you need to fix the port in order to know the port 
// that  StartReverseConnectListener will use
IRDPSRAPISessionProperties RdpProperties = Viewer.Properties as IRDPSRAPISessionProperties;
RdpProperties["PortId"] = Myport;


 private string AddExIP(string viewerConnectionString)
        {
            TextReader tr = new StringReader(viewerConnectionString);
            XDocument doc = XDocument.Load(tr);

            // get external ip 
            // From http://stackoverflow.com/a/16109156/2573450
            string url = "http://checkip.dyndns.org";
            System.Net.WebRequest req = System.Net.WebRequest.Create(url);
            System.Net.WebResponse resp = req.GetResponse();
            System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
            string response = sr.ReadToEnd().Trim();
            string[] a = response.Split(':');
            string a2 = a[1].Substring(1);
            string[] a3 = a2.Split('<');
            string a4 = a3[0];
            string ExternalIp = a4;

            // Add to connection string
            doc.Element("E").Element("C").Element("T").Add(new XElement("L",
                new XAttribute("P", MyportasString),
                new XAttribute("N", ExternalIp)
                ));
            return doc.ToString();
        }

string viewerConnString = Viewer.StartReverseConnectListener(SessionInvitation, Myname, Mypass);

String NewConnectionString = AddExIP(viewerConnString);
TadeoArmenta
  • 154
  • 6
  • 16
0

If you are trying to connect to a PC that doesn't have a public IP, you will need to configure your firewall or NAT with some port forwarding rules. Then, use that public IP (e.g. WAN IP on the router) in your invitation string.

ben
  • 341
  • 1
  • 4
  • How can I use public IP in the invitation string. The string is generated by CreateInvitaion method. m_pRdpSession.Invitations.CreateInvitation(null, "PresentationGroup", "12345", 1); – partyelite Jul 18 '14 at 12:12
  • It appears there is no way to do it via the API. You can try manually replacing the IP address in the invitation string. However, I haven't tested this myself, and from what I'm reading on your linked blog post, it doesn't appear to work. To summarize the current situation, if Viewer is behind NAT, connect like you did above. If Sharer is behind NAT, use IRDPSRAPISharingSession::ConnectToClient with the connection string generated by the Viewer. If BOTH Viewer and Sharer are behind NAT, there is no supported solution. – ben Jul 18 '14 at 12:30
0

In the desktop invitation, i see two public ipv6 address. One with 2001: its teredo IPv6 address created using ipv4 NAT traversal mechanism & another with 2002: prefix meaning its 6to4 address. If the other machine also has a public IPv6 address, this invitation should work. In all windows platform starting from vista, a teredo or 6to4 IPv6 address are initialized by default. 6to4 & teredo address will not be available if your machine is behind a symmetric NAT or behind proxy. Please check if the other machine has ipv6 address. If it has one, it should work.

dvasanth
  • 1,337
  • 1
  • 9
  • 10
0

You can tunnel the data over an aribitrary transport by implementing IRDPSRAPITransportStream. Pass your transport to the sharer using the IRDPSRAPISharingSession2::ConnectUsingTransportStream method, and to the client using IRDPSRAPIViewer::get_Properties with property "SetNetworkStream".

A full example available at https://github.com/mgaffigan/RemoteAssistanceSample

Mitch
  • 21,223
  • 6
  • 63
  • 86