## UDP hole punching

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Fenrir
Party member
Posts: 222
Joined: Wed Nov 27, 2013 9:44 am
Contact:

### UDP hole punching

Hi all,

Anyone knows a LUA library to do UDP hole punching ?

Thanks!
Cheers,

Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Contact:

### Re: UDP hole punching

Maybe you should ask on lua-l mailing list.

Fenrir
Party member
Posts: 222
Joined: Wed Nov 27, 2013 9:44 am
Contact:

### Re: UDP hole punching

Thanks for your reply, I found nothing in the mailing list archive and I'm too lazy to subscibe, I think I'll just go try to implement something myself.

Cheers,

Fenrir
Party member
Posts: 222
Joined: Wed Nov 27, 2013 9:44 am
Contact:

### Re: UDP hole punching

Hi guys,

Just for the record I managed to get something working following this paper:
http://www.brynosaurus.com/pub/net/p2pnat/

I've got a "lobby" server running with a simple LUA script returning to each clients public and private IPs of the server (and vice-versa for the server). And on the LOVE side I'm using Enet to try to establish the connection to both IPs, one important thing is to try to connect from the client AND from the server to be sure to "punch" the hole on both side, then I only use the first connection which succeed and discard the other one.

Cheers,

PS: BTW, Enet totally rocks, thanks for adding it to LOVE!

jack0088
Prole
Posts: 33
Joined: Fri Mar 22, 2013 4:59 am

### Re: UDP hole punching

Hey @Fenrir

I'm facing the similar problem.

I want several clients to have a separate sqlite database, but keep all of the clients in sync. I use UDP for this and on the local network it works. But it would be better if I could sync from anywhere I have an internet connection. So, then I had the idea of having a remote server with open port 42000.

The remote server would basically work as a share station ("lobby") for UDP messages. Each client can send some message to the remote server and also at the same time listen for others messages and pick up the ones it needs.

But frankly that didn't work. It seems like the message is send, but I can not listen to the remote server. If I try to see what my :getsockname() is .. it's the local IP of my mashine instead of the remote servers ip, although I was bound to it.

Turn's out I maybe need to use some NAT hole punching technique to connect the wires... Would you share your code with me please?, so I get an idea what to do? I basically understand the concept, but don't know where to start. Do I need some script to run on the remote server S which connects Client A & Client B?

zorg
Party member
Posts: 2801
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

### Re: UDP hole punching

jack0088 wrote:
Fri Jun 30, 2017 7:41 pm
...
I'd consider telling you the same as what i tell anyone in this situation, but to be frank, this feels like one of the more reasonable reasons for necroing a 3 year old thread... we'll see.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

jack0088
Prole
Posts: 33
Joined: Fri Mar 22, 2013 4:59 am

### Re: UDP hole punching

Sorry, I'm not sure if you're being sarcastic or friendly.. but why does the thread age matter? Why would I create a new thread for the same question anyway? It's a discussion and some questions and solutions still apply today. Maybe I get an answer from somebody else, even if the original author might be not active anymore.

zorg
Party member
Posts: 2801
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

### Re: UDP hole punching

jack0088 wrote:
Sun Jul 02, 2017 5:39 pm
Sorry, I'm not sure if you're being sarcastic or friendly.. but why does the thread age matter? Why would I create a new thread for the same question anyway? It's a discussion and some questions and solutions still apply today. Maybe I get an answer from somebody else, even if the original author might be not active anymore.
A bit of both, to be honest.

And it matters because, depending on circumstances like what the thread is about, people usually necropost in threads that have obsolete information in them, for one reason or another (e.g. it was relevant to an old löve version only); but as you stated, since the topic is still relevant now, it's still one of the times that it's actually not an issue, as i originally said.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

### Re: UDP hole punching

jack0088 wrote:
Fri Jun 30, 2017 7:41 pm
So, then I had the idea of having a remote server with open port 42000.
[...]
Turn's out I maybe need to use some NAT hole punching technique to connect the wires...
If your server has an open port, then you don't need hole punching at all! Just make sure to have the client initiate the connection.
jack0088 wrote:
Fri Jun 30, 2017 7:41 pm
But frankly that didn't work. It seems like the message is send, but I can not listen to the remote server. If I try to see what my :getsockname() is .. it's the local IP of my mashine instead of the remote servers ip, although I was bound to it.
What do you mean you cannot listen to it? With UDP you don't really "listen" to any specific host, you just get whatever is sent to you. (Though if you've called connect you should only receive packets from the host you connected to.)

Also, getsockname is behaving correctly, since it returns what you're listening on: the ip your computer uses to reach the internet (which is usually the local ip).

jack0088
Prole
Posts: 33
Joined: Fri Mar 22, 2013 4:59 am

### Re: UDP hole punching

bartbes wrote:
Sun Jul 02, 2017 9:27 pm
If your server has an open port, then you don't need hole punching at all! Just make sure to have the client initiate the connection.
Thats the thing. I opened ports on my remote server so anyone can access it.

I do not have clients or servers in that sense, since the app runs with same code on all devices. Every device can post udp messages and receive others messages. I just open socket and send information to remote server. Then immediatelly change the socket to receive something from that server.

Code: Select all

udp = socket.udp()
udp:settimeout(0)
udp:setsockname(self_ip, port) --force sending through specified port
udp:sendto(msg, remote_server_ip, port) --seems to work

udp:settimeout(.5)
udp:close()