peer-to-peer relay server

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
hgm
Posts: 27790
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

peer-to-peer relay server

Post by hgm »

I know that "peer-to-peer server" is an oxymoron. But it seems useful to supply a service to connect two pears that would otherwise connect directly, for sparing them the hassle of setting up port translations to make the connection possible. Outgoing connections usually work 'out of the box', possibly after answering a firewall popup that you allow them.

So I tried to conceive how this could be made easier, and concluded that something like that could be made almost completely independent from the actual protocol it should mediate. Only a very simple handshake when making the connection suffices.

As I originally based the design on the protocol used by the p2p adapter to connect two GUIs, I assumed that the peer actively seeking the connection would start sending a passphrase, expecting as only conformation that it is not disconnected immediately (i.e. will not receive EOF from the TCP/IP socket it starts reading from). The peer answering the connection would send the message "Password required", which would normally be ignored by the receiver.

The only extension I added to the protocol is that it now starts waiting for a message that starts with "Password", and ignores any other input until it receives one. This would not interfere with establishing a direct connection between peers, as the answering peer would send the "Password required", to allow the connecting peer to proceed.

But a relay server would read the passphrase, adding it to its table of currently active passphrases if it was not there, and sending a "Wait" message to the first peer that connected with that passphrase, in stead of the "Password required" that an answering peer would have sent. If then a second peer arrives, using that same passphrase (so that the relay server finds it in the table), the first pear will be sent a "Password ..." message to make it proceed, and the second peer will be sent a "Paswword required" message immediately (never knowing it was not connected to another peer directly). After that, the relay server will just pass the messages between the two peers. (If a third peer would show up using that same pass phrase, it would be rejected, just as a peer approached for a connection would refuse one if he is already busy.)

After this simple handshake, any protocol could be handled, as the relay server just forwards the messages. The only addition to the peer code is that it has to keep reading until it has received at least one "Password ..." message from whatever it connects to, before considering itself connected. I have now put such a patch in the p2p adapter, and hope to set up a relay server on winboard.nl (port 27015, the default p2p port) soon.
JoshPettus
Posts: 730
Joined: Fri Oct 19, 2012 2:23 am

Re: peer-to-peer relay server

Post by JoshPettus »

I don't know if you saw my email on the Xboard dev list, but I'm thinking about putting out an Xboard.app 4.8.0c. Would you like me to include the p2p script? I've already updated Hachu.
User avatar
hgm
Posts: 27790
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: peer-to-peer relay server

Post by hgm »

It might be a useful addition. Although so far I also never distributed it with WinBoard. I am currently working on it to allow it to use the new WB 4.8 features, and be compatible with a relay server as described here, which I would still have to test.
JoshPettus
Posts: 730
Joined: Fri Oct 19, 2012 2:23 am

Re: peer-to-peer relay server

Post by JoshPettus »

No problem, I can wait. There is no bug or anything, I just found a better solution for the man/info pages. Whenever you get a chance, could make sure I didn't break anything... or everything?
User avatar
hgm
Posts: 27790
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: peer-to-peer relay server

Post by hgm »

The relay server seems to work, now. I started one on winboard.nl, port 27015 (the default p2p port).

Only problem is that p2p apparently is not able yet to understand symbolic URLs; it really needs the IP address (212.114.110.107, in this case). Still got to fix that. In any case, running

p2p 212.114.110.107 PASSWORD

as engine, would make you connect with the first other person that issues the same command (using the same PASSWORD), without anyone having to set up any port translation in his router/modem. I hope to release a version of p2p soon where this can be simplified to

p2p winboard.nl PASSWORD

Btw, I had some trouble setting it up. When I compiled the relay server, I got a warning that the library function inet_ntoa() could not be printed with a %s format, because it returned an int. The Linux manuals say it returns (char*), though. So I ran the binary anyway on my laptop, and it worked fine, printing the IP address nicely as "x.x.x.x". Then I installed it on my VPS, and it segfaulted in the printf. I could only get it to run when I supplied my own long-to-IPaddress conversion routine.

Is there anything known about a sickness in this function?
User avatar
hgm
Posts: 27790
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: peer-to-peer relay server

Post by hgm »

JoshPettus wrote:I don't know if you saw my email on the Xboard dev list, but I'm thinking about putting out an Xboard.app 4.8.0c. Would you like me to include the p2p script? I've already updated Hachu.
OK, I have a version of p2p now that seems to be working well (and has the domain-name recognition). I pushed the whole p2p repository to hgm.nubati.net. Only tested it on Windows, though.
JoshPettus
Posts: 730
Joined: Fri Oct 19, 2012 2:23 am

Re: peer-to-peer relay server

Post by JoshPettus »

Thanks Harm!

I get a couple of clang errors.

p2p.c

Code: Select all

./p2p.c:115:34: error: incomplete definition of type 'struct hostent'
    list = (struct in_addr **) he->h_addr_list;
                               ~~^
and
relay.c

Code: Select all

./relay.c:160:76: error: expected ';' after expression
    snprintf(buf, 80, "%d.%d.%d.%d", i>>24&255, i>>16&255, i>>8&255, i&255)

                                                                           ^
                                                                           ;
I should probably make a simple logo too :)
User avatar
hgm
Posts: 27790
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: peer-to-peer relay server

Post by hgm »

Hmm, that is what you get (missing trailing semicolon) when you retype the patches that work on one machine by hand on another. There is no need to compile relay.c, however; it is not of interest to ordinary users. These would use the relay.c that I have running at winboard.nl , if they don't manage to make a direct connection between their p2p.

So you only have to include p2p.exe. The error there is that in Linux you apparently have to #include <netdb.h> to declare struct hostent. I modified my repository, the p2p.c version it contains now compiles on Linux without warnings.

I have indeed no logo for p2p.
JoshPettus
Posts: 730
Joined: Fri Oct 19, 2012 2:23 am

Re: peer-to-peer relay server

Post by JoshPettus »

Thanks H.G. it compiles now, I'll give it a test on the local network first. As to logo. It's rather boring, but it does the job.

https://www.dropbox.com/s/qpab8lflbtcwsic/p2p.png?dl=0
JoshPettus
Posts: 730
Joined: Fri Oct 19, 2012 2:23 am

Re: peer-to-peer relay server

Post by JoshPettus »

Seems to work great on my network with direct ip. Although, I was using v .3 on a windows computer and so couldn't test the relay system, but I assume it works.