Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
Nodemcu Firmware
Commits
2c8961d1
Commit
2c8961d1
authored
Feb 06, 2017
by
Yury Popov
Committed by
Marcel Stör
Feb 06, 2017
Browse files
Add TTL support to net module (#1756)
parent
20a591f5
Changes
2
Show whitespace changes
Inline
Side-by-side
app/modules/net.c
View file @
2c8961d1
...
...
@@ -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
)
},
...
...
docs/en/modules/net.md
View file @
2c8961d1
...
...
@@ -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()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment