1
votes

How do I set Time-To-Live socket option in C/C++ on Windows, Mac, and Linux using using the C/C++ standard libraries? It needs to work without Boost. Either C or C++ is fine. IP header modification for outgoing UDP packets. No multicast.

1

1 Answers

8
votes

Basically this is done via the standard setsockopt. You need to use the IPPROTO_IP level and the option is IP_TTL.

I couldn't find a link for this on gnu.org, but (for example..) on freebsd you have this manual page.

Use:

int ttl = 60; /* max = 255 */
setsockopt(s, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));