TTL on UDP sockets

  • I think the ability to set the TTL for a UDP datagram is a very important feature that is (as far as i could find) missing. Does anyone know if/when TTL support is planned for UDP sockets?

    or am i wrong? is t there and i'm just missing it?

  • First of all, TTL isn't a UDP "feature", but an IP one.

    Second, you're right, there's no function for that in Qt.

    Can you elaborate why do you think it's such an useful feature to you?

  • well, i mentioned TTL for UDP because of portability issues: from what i heard (though i'm no expert in TCP/win), for TCP connections you can't modify the TTL in windows, and thus (if this is true) this feature wouldn't be portable on windows. Sure, if this can be done on raw sockets on windows also (and not only as admin), then why not.

    What it's useful for: well, for just about any P2P program that you can think of which needs to traverse NATs: if you need to "break" a NAT, you need to make sure that it first sends a packet and only later receives a reply on a given port (or else you can end up with no connection and, worse, the port may become blocked for N minutes, this depends on the NAT's policy), and you need to do this on both ends of a P2P connection; and the only way to do this is to shoot some pings from one peer towards the other with a short TTL (so that the ping on one side doesn't reach the other side's NAT's port and potentially blocks it)

    To rise: no TTL (at least on UDP), no P2P over NATs

    CRY FOR HELP to whomever reads this post and considers not being able to write full-fledged P2P/Qt apps important: please rate this post up (the thumbs up icon on the right sidebar on this page) so that we get a chance to be noticed by the Qt developers. I mean, one way or another, P2P apps are the wave of the future, and Qt not supporting such apps out-of-the-box seems pretty much of a big issue to me...


  • And since i'm here, [kinda] the same question rephrased: is there any way to change the TTL on a Qt UDP socket at least for an outgoing datagram? (if yes, a code snippet would be nice :P)

  • I have no clue about windows, but: under Unix, changing the TTL field in the IP header requires raw sockets, and raw sockets require root(-like) privileges.

    I understand what you're trying to do, but Qt exposes no API for that.

  • Oh, i didn't know you need to be root to set TTL on linux. Are you sure about that? You mean 'traceroute' won't work from a non-root account? Or is 'treaceroute' implemented in two layers, with a non-root "client" and a root daemon which does the actual pinging with variable TTL? Maybe you can tell me a bit about what you know about this, or maybe point me to a reference doc which unequivocally specifies that you need to be root?

  • I always like this kind of questions because they make you dig a bit in networking code (and discard what is written in books): I figured out I was wrong.

    Linux lets unprivileged users manipulate the TTL field for a socket, using setsockopt(2) with IP_TTL. So just fetch the descriptor from QUdpSocket and call setsockopt on it :)

  • K dude, i love you :-) 10x!

    Still, what i said earlier about having a property directly in the Qt socket class (at least for UDP) would definitely be nice, so i'll leave my "cry for help" there, maybe the Qt developers will take notice (yeah, that's a pretty long shot for chasing a short TTL, i know :)

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.