0

i'm using the UDT library to transfer files beetween to computers behind NAT. I already did a hole punching protocol in python and i know how it works. Here, i'm trying to bind an UDT socket to an existing UDP socket.

int udt_holepunching(SOCKET sock, SOCKADDR_IN sin, int size, SOCKADDR_IN peeraddr)
    {
      UDTSOCKET u;
      bool rdv = true;



      UDT::setsockopt(u, 0, UDT_RENDEZVOUS, &rdv, sizeof(bool));
      UDT::bind(u, sock);
      UDT::connect(u, &peeraddr, sizeof(peeraddr));
    }

with sock my existing UDP socket and peeraddr the adress of the computer that i want to talk

i have this error :

client.cpp: In function ‘int udt_holepunching(SOCKET, SOCKADDR_IN, int, SOCKADDR_IN)’:
client.cpp:29:20: error: invalid conversion from ‘SOCKET {aka int}’ to ‘const sockaddr*’ [-fpermissive]
   UDT::bind(u, sock);
                    ^
client.cpp:29:20: error: too few arguments to function ‘int UDT::bind(UDTSOCKET, const sockaddr*, int)’
In file included from client.cpp:13:0:
../src/udt.h:315:13: note: declared here
 UDT_API int bind(UDTSOCKET u, const struct sockaddr* name, int namelen);

It's kind of strange because in the UDT bind documentation, it seems to be possible.

Quentin
  • 50
  • 1
  • 8
  • First of all, the code you show doesn't match with the error. Secondly, read the error again, it tells you the arguments needed for `bind` and it's not what you pass to it. – Some programmer dude Nov 06 '14 at 10:34
  • My bad, i edited it. I understand what you mean but in the documentation it's written that bind can take only one argument : the existing UDP socket. That's what i'm trying to do and it doesn't work – Quentin Nov 06 '14 at 10:44
  • I think this might be a documentation error, could you try using `bind2(UDTSOCKET, UDPSOCKET)` instead? – Hasturkun Nov 06 '14 at 11:58
  • Actually it was `bind2(udtsocket, udpsocket)` Thanks for the tips ! – Quentin Nov 06 '14 at 13:43

2 Answers2

0

The linked to reference shows two overloads. The compiler picks the first one (the tree argument overload) because you pass an UDTSOCKET as the first argument and have a second argument.

Since the the second argument in the three-argument overload is not a SOCKET you get the first error. And since you're only passing two arguments to a function expecting three you get the second error.

If you want to bind to a specific address (say the sin argument you pass to your function) then you need to pass a pointer to that address structure as the second argument, and the size of that structure as the third argument:

UDT::bind(u, (const SOCKADDR*) &sin, sizeof(sin));

If you want to use the second overload, then use it:

UDT::bind(sock);

You can't mix and match as you like, you have to pick one or the other.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I understand what you mean. Actually, i would like to bind to an existing UDP socket. So i wanted to use the second overload like it's describe in the doc. So i try something like this : `UDT::bind(sock)` but i have the same kind of error – Quentin Nov 06 '14 at 11:30
  • 1
    It works ! Actually it was a mistake in the documentation. I should use `UDT::bind2(UDPSOCKET, UDTSOCKET)` – Quentin Nov 06 '14 at 13:45
0

The technical answer is hidden in comments above: Use

bind2(udtsocket, udpsocket)

on created UDT/UDP sockets.

Concerning hole punching, this may be done without considering UDP, at all, see UDT Hole Punching Pattern. This also shows valid use of the RENDEVOUS option.

UDT in this scenario requires unique connection, i.e. at one time you either connect to the rendevous server or you are in P2P mode with another node; where this is the only proper way to establish a sequence of several independent P2P connections.

What is not perfect in the referenced ´UDT Hole Punching Pattern´ is, that the UDT socket is bound to a fix parameter address; better is to bind the socket to port 0. The system will allocate a suitable dynamic port, which can be retrieved by getsockname() and then be re-used in the P2P mode.

Sam Ginrich
  • 661
  • 6
  • 7