Add TTL support to net module (#1756)

This commit is contained in:
Yury Popov 2017-02-06 15:54:20 +03:00 committed by Marcel Stör
parent 20a591f59a
commit 2c8961d153
2 changed files with 52 additions and 0 deletions

View File

@ -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 ) },

View File

@ -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()