diff --git a/app/modules/net.c b/app/modules/net.c index 4d92479a..7203d277 100644 --- a/app/modules/net.c +++ b/app/modules/net.c @@ -638,6 +638,27 @@ int net_dns( lua_State *L ) { return 0; } +// Lua: client/socket:ttl([ttl]) +int net_ttl( lua_State *L ) { + lnet_userdata *ud = net_get_udata(L); + if (!ud || ud->type == TYPE_TCP_SERVER) + return luaL_error(L, "invalid user data"); + if (!ud->pcb) + return luaL_error(L, "socket is not open/bound yet"); + int ttl = luaL_optinteger(L, 2, -1); + // Since `ttl` field is part of IP_PCB macro + // (which are at beginning of both udp_pcb/tcp_pcb) + // and PCBs declared as `union` there is safe to + // access ttl field without checking for type. + if (ttl == -1) { + ttl = ud->udp_pcb->ttl; + } else { + ud->udp_pcb->ttl = ttl; + } + lua_pushinteger(L, ttl); + return 1; +} + // Lua: client:getpeer() int net_getpeer( lua_State *L ) { lnet_userdata *ud = net_get_udata(L); @@ -951,6 +972,7 @@ static const LUA_REG_TYPE net_tcpsocket_map[] = { { LSTRKEY( "hold" ), LFUNCVAL( net_hold ) }, { LSTRKEY( "unhold" ), LFUNCVAL( net_unhold ) }, { LSTRKEY( "dns" ), LFUNCVAL( net_dns ) }, + { LSTRKEY( "ttl" ), LFUNCVAL( net_ttl ) }, { LSTRKEY( "getpeer" ), LFUNCVAL( net_getpeer ) }, { LSTRKEY( "getaddr" ), LFUNCVAL( net_getaddr ) }, { LSTRKEY( "__gc" ), LFUNCVAL( net_delete ) }, @@ -964,6 +986,7 @@ static const LUA_REG_TYPE net_udpsocket_map[] = { { LSTRKEY( "on" ), LFUNCVAL( net_on ) }, { LSTRKEY( "send" ), LFUNCVAL( net_send ) }, { LSTRKEY( "dns" ), LFUNCVAL( net_dns ) }, + { LSTRKEY( "ttl" ), LFUNCVAL( net_ttl ) }, { LSTRKEY( "getaddr" ), LFUNCVAL( net_getaddr ) }, { LSTRKEY( "__gc" ), LFUNCVAL( net_delete ) }, { LSTRKEY( "__index" ), LROVAL( net_udpsocket_map ) }, diff --git a/docs/en/modules/net.md b/docs/en/modules/net.md index 58a425d0..67e0fa11 100644 --- a/docs/en/modules/net.md +++ b/docs/en/modules/net.md @@ -400,6 +400,29 @@ end) #### See also [`net.socket:on()`](#netsocketon) +## net.socket:ttl() + +Changes or retrieves Time-To-Live value on socket. + +#### Syntax +`ttl([ttl])` + +#### Parameters +- `ttl` (optional) new time-to-live value + +#### Returns +current / new ttl value + +#### Example +```lua +sk = net.createConnection(net.TCP, 0) +sk:connect(80, '192.168.1.1') +sk:ttl(1) -- restrict frames to single subnet +``` + +#### See also +[`net.createConnection()`](#netcreateconnection) + ## net.socket:unhold() Unblock TCP receiving data by revocation of a preceding `hold()`. @@ -492,6 +515,12 @@ Retrieve local port and ip of socket. The syntax and functional identical to [`net.socket:getaddr()`](#netsocketgetaddr). +## net.udpsocket:ttl() + +Changes or retrieves Time-To-Live value on socket. + +The syntax and functional identical to [`net.socket:ttl()`](#netsocketttl). + # net.dns Module ## net.dns.getdnsserver()