Skip to content

Commit a0bb7dd

Browse files
squeek502truemedian
authored andcommitted
udp: Add bind flags that got lost along the way
These flags were added in 1.42.0 and 1.49.0 but went unnoticed.
1 parent a60fcf6 commit a0bb7dd

File tree

4 files changed

+137
-6
lines changed

4 files changed

+137
-6
lines changed

docs/docs.lua

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2863,7 +2863,31 @@ local doc = {
28632863
method_form = 'udp:bind(host, port, [flags])',
28642864
desc = [[
28652865
Bind the UDP handle to an IP address and port. Any `flags` are set with a table
2866-
with fields `reuseaddr` or `ipv6only` equal to `true` or `false`.
2866+
with fields `reuseaddr`, `ipv6only`, `linux_recverr`, `reuseport` equal to `true` or `false`.
2867+
2868+
- `reuseaddr`: Indicates if SO_REUSEADDR will be set when binding the handle.
2869+
This sets the SO_REUSEPORT socket flag on the BSDs (except for
2870+
DragonFlyBSD), OS X, and other platforms where SO_REUSEPORTs don't
2871+
have the capability of load balancing, as the opposite of what
2872+
`reuseport` would do. On other Unix platforms, it sets the
2873+
SO_REUSEADDR flag. What that means is that multiple threads or
2874+
processes can bind to the same address without error (provided
2875+
they all set the flag) but only the last one to bind will receive
2876+
any traffic, in effect "stealing" the port from the previous listener.
2877+
- `ipv6only`: Disables dual stack mode.
2878+
- `linux_recverr`: Indicates if IP_RECVERR/IPV6_RECVERR will be set when binding the handle.
2879+
This sets IP_RECVERR for IPv4 and IPV6_RECVERR for IPv6 UDP sockets on
2880+
Linux. This stops the Linux kernel from suppressing some ICMP error messages
2881+
and enables full ICMP error reporting for faster failover.
2882+
This flag is no-op on platforms other than Linux.
2883+
- `reuseport`: Indicates if SO_REUSEPORT will be set when binding the handle.
2884+
This sets the SO_REUSEPORT socket option on supported platforms.
2885+
Unlike `reuseaddr`, this flag will make multiple threads or
2886+
processes that are binding to the same address and port "share"
2887+
the port, which means incoming datagrams are distributed across
2888+
the receiving sockets among threads or processes.
2889+
This flag is available only on Linux 3.9+, DragonFlyBSD 3.6+,
2890+
FreeBSD 12.0+, Solaris 11.4, and AIX 7.2.5+ for now.
28672891
]],
28682892
params = {
28692893
{ name = 'udp', type = 'uv_udp_t' },
@@ -2874,10 +2898,18 @@ local doc = {
28742898
type = opt(table({
28752899
{ 'ipv6only', opt_bool },
28762900
{ 'reuseaddr', opt_bool },
2901+
{ 'linux_recverr', opt_bool },
2902+
{ 'reuseport', opt_bool },
28772903
})),
28782904
},
28792905
},
28802906
returns = success_ret,
2907+
notes = {
2908+
[[
2909+
The flag `linux_recverr` is only supported with Libuv >= 1.42.0.
2910+
The flag `reuseport` is only supported with Libuv >= 1.49.0.
2911+
]],
2912+
},
28812913
},
28822914
{
28832915
name = 'udp_getsockname',

docs/docs.md

Lines changed: 30 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/meta.lua

Lines changed: 64 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/udp.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,16 @@ static int luv_udp_bind(lua_State* L) {
175175
lua_getfield(L, 4, "ipv6only");
176176
if (lua_toboolean(L, -1)) flags |= UV_UDP_IPV6ONLY;
177177
lua_pop(L, 1);
178+
#if LUV_UV_VERSION_GEQ(1, 42, 0)
179+
lua_getfield(L, 4, "linux_recverr");
180+
if (lua_toboolean(L, -1)) flags |= UV_UDP_LINUX_RECVERR;
181+
lua_pop(L, 1);
182+
#endif
183+
#if LUV_UV_VERSION_GEQ(1, 49, 0)
184+
lua_getfield(L, 4, "reuseport");
185+
if (lua_toboolean(L, -1)) flags |= UV_UDP_REUSEPORT;
186+
lua_pop(L, 1);
187+
#endif
178188
}
179189
ret = uv_udp_bind(handle, (struct sockaddr*)&addr, flags);
180190
return luv_result(L, ret);

0 commit comments

Comments
 (0)