nodemcu-firmware/docs/modules/net.md

632 lines
15 KiB
Markdown
Raw Normal View History

2016-01-11 23:30:30 +01:00
# net Module
| Since | Origin / Contributor | Maintainer | Source |
| :----- | :-------------------- | :---------- | :------ |
| 2014-12-22 | [Zeroday](https://github.com/funshine) | [PhoeniX](https://github.com/djphoenix) | [net.c](../../app/modules/net.c)|
** TLS operations was moved to the [TLS](tls.md) module **
2016-01-11 23:30:30 +01:00
## Constants
Constants to be used in other functions: `net.TCP`, `net.UDP`
2016-01-11 23:30:30 +01:00
## net.createConnection()
Networking rampage and accumulated fixes (#3060) * espconn: remove unused espconn code, take 1 This is the easiest part of https://github.com/nodemcu/nodemcu-firmware/issues/3004 . It removes a bunch of functions that were never called in our tree. * espconn: De-orbit espconn_gethostbyname Further work on https://github.com/nodemcu/nodemcu-firmware/issues/3004 While here, remove `mqtt`'s charming DNS-retry logic (which is neither shared with nor duplicated in other modules) and update its :connect() return value behavior and documentation. * espconn: remove scary global pktinfo A write-only global! How about that. * net: remove deprecated methods All the TLS stuff moved over there a long time ago, and net_createUDPSocket should just do what it says on the tin. * espconn_secure: remove ESPCONN_SERVER support We can barely function as a TLS client; being a TLS server seems like a real stretch. This code was never called from Lua anyway. * espconn_secure: more code removal * espconn_secure: simplify ssl options structure There is nothing "ssl_packet" about this structure. Get rid of the terrifying "pbuffer" pointer. Squash two structure types together and eliminate an unused field. * espconn_secure: refactor mbedtls_msg_info_load Split out espconn_mbedtls_parse, which we can use as part of our effort towards addressing https://github.com/nodemcu/nodemcu-firmware/issues/3032 * espconn_secure: introduce TLS cert/key callbacks The new feature part of https://github.com/nodemcu/nodemcu-firmware/issues/3032 Subsequent work will remove the old mechanism. * tls: add deprecation warnings * luacheck: net.ifinfo is a thing now * tls: remove use of espconn->reverse * mqtt: stop using espconn->reverse Instead, just place the espconn structure itself at the top of the user data. This enlarges the structure somewhat but removes one more layer of dynamic heap usage and NULL checks. While here, simplify the code a bit. * mqtt: remove redundant pointer to connect_info Everywhere we have the mqtt_state_t we also have the lmqtt_userdata. * mqtt: doc fixes * mqtt: note bug * tls: allow :on(...,nil) to unregister a callback
2020-04-07 14:06:27 +02:00
Creates a TCP client.
2016-01-11 23:30:30 +01:00
#### Syntax
Networking rampage and accumulated fixes (#3060) * espconn: remove unused espconn code, take 1 This is the easiest part of https://github.com/nodemcu/nodemcu-firmware/issues/3004 . It removes a bunch of functions that were never called in our tree. * espconn: De-orbit espconn_gethostbyname Further work on https://github.com/nodemcu/nodemcu-firmware/issues/3004 While here, remove `mqtt`'s charming DNS-retry logic (which is neither shared with nor duplicated in other modules) and update its :connect() return value behavior and documentation. * espconn: remove scary global pktinfo A write-only global! How about that. * net: remove deprecated methods All the TLS stuff moved over there a long time ago, and net_createUDPSocket should just do what it says on the tin. * espconn_secure: remove ESPCONN_SERVER support We can barely function as a TLS client; being a TLS server seems like a real stretch. This code was never called from Lua anyway. * espconn_secure: more code removal * espconn_secure: simplify ssl options structure There is nothing "ssl_packet" about this structure. Get rid of the terrifying "pbuffer" pointer. Squash two structure types together and eliminate an unused field. * espconn_secure: refactor mbedtls_msg_info_load Split out espconn_mbedtls_parse, which we can use as part of our effort towards addressing https://github.com/nodemcu/nodemcu-firmware/issues/3032 * espconn_secure: introduce TLS cert/key callbacks The new feature part of https://github.com/nodemcu/nodemcu-firmware/issues/3032 Subsequent work will remove the old mechanism. * tls: add deprecation warnings * luacheck: net.ifinfo is a thing now * tls: remove use of espconn->reverse * mqtt: stop using espconn->reverse Instead, just place the espconn structure itself at the top of the user data. This enlarges the structure somewhat but removes one more layer of dynamic heap usage and NULL checks. While here, simplify the code a bit. * mqtt: remove redundant pointer to connect_info Everywhere we have the mqtt_state_t we also have the lmqtt_userdata. * mqtt: doc fixes * mqtt: note bug * tls: allow :on(...,nil) to unregister a callback
2020-04-07 14:06:27 +02:00
`net.createConnection()`
2016-01-11 23:30:30 +01:00
#### Returns
Networking rampage and accumulated fixes (#3060) * espconn: remove unused espconn code, take 1 This is the easiest part of https://github.com/nodemcu/nodemcu-firmware/issues/3004 . It removes a bunch of functions that were never called in our tree. * espconn: De-orbit espconn_gethostbyname Further work on https://github.com/nodemcu/nodemcu-firmware/issues/3004 While here, remove `mqtt`'s charming DNS-retry logic (which is neither shared with nor duplicated in other modules) and update its :connect() return value behavior and documentation. * espconn: remove scary global pktinfo A write-only global! How about that. * net: remove deprecated methods All the TLS stuff moved over there a long time ago, and net_createUDPSocket should just do what it says on the tin. * espconn_secure: remove ESPCONN_SERVER support We can barely function as a TLS client; being a TLS server seems like a real stretch. This code was never called from Lua anyway. * espconn_secure: more code removal * espconn_secure: simplify ssl options structure There is nothing "ssl_packet" about this structure. Get rid of the terrifying "pbuffer" pointer. Squash two structure types together and eliminate an unused field. * espconn_secure: refactor mbedtls_msg_info_load Split out espconn_mbedtls_parse, which we can use as part of our effort towards addressing https://github.com/nodemcu/nodemcu-firmware/issues/3032 * espconn_secure: introduce TLS cert/key callbacks The new feature part of https://github.com/nodemcu/nodemcu-firmware/issues/3032 Subsequent work will remove the old mechanism. * tls: add deprecation warnings * luacheck: net.ifinfo is a thing now * tls: remove use of espconn->reverse * mqtt: stop using espconn->reverse Instead, just place the espconn structure itself at the top of the user data. This enlarges the structure somewhat but removes one more layer of dynamic heap usage and NULL checks. While here, simplify the code a bit. * mqtt: remove redundant pointer to connect_info Everywhere we have the mqtt_state_t we also have the lmqtt_userdata. * mqtt: doc fixes * mqtt: note bug * tls: allow :on(...,nil) to unregister a callback
2020-04-07 14:06:27 +02:00
- net.socket sub module
2016-01-11 23:30:30 +01:00
#### See also
[`net.createServer()`](#netcreateserver), [`net.createUDPSocket()`](#netcreateudpsocket), [`tls.createConnection()`](tls.md#tlscreateconnection)
2016-01-11 23:30:30 +01:00
## net.createServer()
Networking rampage and accumulated fixes (#3060) * espconn: remove unused espconn code, take 1 This is the easiest part of https://github.com/nodemcu/nodemcu-firmware/issues/3004 . It removes a bunch of functions that were never called in our tree. * espconn: De-orbit espconn_gethostbyname Further work on https://github.com/nodemcu/nodemcu-firmware/issues/3004 While here, remove `mqtt`'s charming DNS-retry logic (which is neither shared with nor duplicated in other modules) and update its :connect() return value behavior and documentation. * espconn: remove scary global pktinfo A write-only global! How about that. * net: remove deprecated methods All the TLS stuff moved over there a long time ago, and net_createUDPSocket should just do what it says on the tin. * espconn_secure: remove ESPCONN_SERVER support We can barely function as a TLS client; being a TLS server seems like a real stretch. This code was never called from Lua anyway. * espconn_secure: more code removal * espconn_secure: simplify ssl options structure There is nothing "ssl_packet" about this structure. Get rid of the terrifying "pbuffer" pointer. Squash two structure types together and eliminate an unused field. * espconn_secure: refactor mbedtls_msg_info_load Split out espconn_mbedtls_parse, which we can use as part of our effort towards addressing https://github.com/nodemcu/nodemcu-firmware/issues/3032 * espconn_secure: introduce TLS cert/key callbacks The new feature part of https://github.com/nodemcu/nodemcu-firmware/issues/3032 Subsequent work will remove the old mechanism. * tls: add deprecation warnings * luacheck: net.ifinfo is a thing now * tls: remove use of espconn->reverse * mqtt: stop using espconn->reverse Instead, just place the espconn structure itself at the top of the user data. This enlarges the structure somewhat but removes one more layer of dynamic heap usage and NULL checks. While here, simplify the code a bit. * mqtt: remove redundant pointer to connect_info Everywhere we have the mqtt_state_t we also have the lmqtt_userdata. * mqtt: doc fixes * mqtt: note bug * tls: allow :on(...,nil) to unregister a callback
2020-04-07 14:06:27 +02:00
Creates a TCP listening socket (a server).
2016-01-11 23:30:30 +01:00
#### Syntax
Networking rampage and accumulated fixes (#3060) * espconn: remove unused espconn code, take 1 This is the easiest part of https://github.com/nodemcu/nodemcu-firmware/issues/3004 . It removes a bunch of functions that were never called in our tree. * espconn: De-orbit espconn_gethostbyname Further work on https://github.com/nodemcu/nodemcu-firmware/issues/3004 While here, remove `mqtt`'s charming DNS-retry logic (which is neither shared with nor duplicated in other modules) and update its :connect() return value behavior and documentation. * espconn: remove scary global pktinfo A write-only global! How about that. * net: remove deprecated methods All the TLS stuff moved over there a long time ago, and net_createUDPSocket should just do what it says on the tin. * espconn_secure: remove ESPCONN_SERVER support We can barely function as a TLS client; being a TLS server seems like a real stretch. This code was never called from Lua anyway. * espconn_secure: more code removal * espconn_secure: simplify ssl options structure There is nothing "ssl_packet" about this structure. Get rid of the terrifying "pbuffer" pointer. Squash two structure types together and eliminate an unused field. * espconn_secure: refactor mbedtls_msg_info_load Split out espconn_mbedtls_parse, which we can use as part of our effort towards addressing https://github.com/nodemcu/nodemcu-firmware/issues/3032 * espconn_secure: introduce TLS cert/key callbacks The new feature part of https://github.com/nodemcu/nodemcu-firmware/issues/3032 Subsequent work will remove the old mechanism. * tls: add deprecation warnings * luacheck: net.ifinfo is a thing now * tls: remove use of espconn->reverse * mqtt: stop using espconn->reverse Instead, just place the espconn structure itself at the top of the user data. This enlarges the structure somewhat but removes one more layer of dynamic heap usage and NULL checks. While here, simplify the code a bit. * mqtt: remove redundant pointer to connect_info Everywhere we have the mqtt_state_t we also have the lmqtt_userdata. * mqtt: doc fixes * mqtt: note bug * tls: allow :on(...,nil) to unregister a callback
2020-04-07 14:06:27 +02:00
`net.createServer(timeout)`
2016-01-11 23:30:30 +01:00
#### Parameters
Networking rampage and accumulated fixes (#3060) * espconn: remove unused espconn code, take 1 This is the easiest part of https://github.com/nodemcu/nodemcu-firmware/issues/3004 . It removes a bunch of functions that were never called in our tree. * espconn: De-orbit espconn_gethostbyname Further work on https://github.com/nodemcu/nodemcu-firmware/issues/3004 While here, remove `mqtt`'s charming DNS-retry logic (which is neither shared with nor duplicated in other modules) and update its :connect() return value behavior and documentation. * espconn: remove scary global pktinfo A write-only global! How about that. * net: remove deprecated methods All the TLS stuff moved over there a long time ago, and net_createUDPSocket should just do what it says on the tin. * espconn_secure: remove ESPCONN_SERVER support We can barely function as a TLS client; being a TLS server seems like a real stretch. This code was never called from Lua anyway. * espconn_secure: more code removal * espconn_secure: simplify ssl options structure There is nothing "ssl_packet" about this structure. Get rid of the terrifying "pbuffer" pointer. Squash two structure types together and eliminate an unused field. * espconn_secure: refactor mbedtls_msg_info_load Split out espconn_mbedtls_parse, which we can use as part of our effort towards addressing https://github.com/nodemcu/nodemcu-firmware/issues/3032 * espconn_secure: introduce TLS cert/key callbacks The new feature part of https://github.com/nodemcu/nodemcu-firmware/issues/3032 Subsequent work will remove the old mechanism. * tls: add deprecation warnings * luacheck: net.ifinfo is a thing now * tls: remove use of espconn->reverse * mqtt: stop using espconn->reverse Instead, just place the espconn structure itself at the top of the user data. This enlarges the structure somewhat but removes one more layer of dynamic heap usage and NULL checks. While here, simplify the code a bit. * mqtt: remove redundant pointer to connect_info Everywhere we have the mqtt_state_t we also have the lmqtt_userdata. * mqtt: doc fixes * mqtt: note bug * tls: allow :on(...,nil) to unregister a callback
2020-04-07 14:06:27 +02:00
- `timeout`: seconds until disconnecting an inactive client; 1~28'800 seconds, 30 sec by default.
2016-01-11 23:30:30 +01:00
#### Returns
Networking rampage and accumulated fixes (#3060) * espconn: remove unused espconn code, take 1 This is the easiest part of https://github.com/nodemcu/nodemcu-firmware/issues/3004 . It removes a bunch of functions that were never called in our tree. * espconn: De-orbit espconn_gethostbyname Further work on https://github.com/nodemcu/nodemcu-firmware/issues/3004 While here, remove `mqtt`'s charming DNS-retry logic (which is neither shared with nor duplicated in other modules) and update its :connect() return value behavior and documentation. * espconn: remove scary global pktinfo A write-only global! How about that. * net: remove deprecated methods All the TLS stuff moved over there a long time ago, and net_createUDPSocket should just do what it says on the tin. * espconn_secure: remove ESPCONN_SERVER support We can barely function as a TLS client; being a TLS server seems like a real stretch. This code was never called from Lua anyway. * espconn_secure: more code removal * espconn_secure: simplify ssl options structure There is nothing "ssl_packet" about this structure. Get rid of the terrifying "pbuffer" pointer. Squash two structure types together and eliminate an unused field. * espconn_secure: refactor mbedtls_msg_info_load Split out espconn_mbedtls_parse, which we can use as part of our effort towards addressing https://github.com/nodemcu/nodemcu-firmware/issues/3032 * espconn_secure: introduce TLS cert/key callbacks The new feature part of https://github.com/nodemcu/nodemcu-firmware/issues/3032 Subsequent work will remove the old mechanism. * tls: add deprecation warnings * luacheck: net.ifinfo is a thing now * tls: remove use of espconn->reverse * mqtt: stop using espconn->reverse Instead, just place the espconn structure itself at the top of the user data. This enlarges the structure somewhat but removes one more layer of dynamic heap usage and NULL checks. While here, simplify the code a bit. * mqtt: remove redundant pointer to connect_info Everywhere we have the mqtt_state_t we also have the lmqtt_userdata. * mqtt: doc fixes * mqtt: note bug * tls: allow :on(...,nil) to unregister a callback
2020-04-07 14:06:27 +02:00
- net.server sub module
2016-01-11 23:30:30 +01:00
#### Example
```lua
Networking rampage and accumulated fixes (#3060) * espconn: remove unused espconn code, take 1 This is the easiest part of https://github.com/nodemcu/nodemcu-firmware/issues/3004 . It removes a bunch of functions that were never called in our tree. * espconn: De-orbit espconn_gethostbyname Further work on https://github.com/nodemcu/nodemcu-firmware/issues/3004 While here, remove `mqtt`'s charming DNS-retry logic (which is neither shared with nor duplicated in other modules) and update its :connect() return value behavior and documentation. * espconn: remove scary global pktinfo A write-only global! How about that. * net: remove deprecated methods All the TLS stuff moved over there a long time ago, and net_createUDPSocket should just do what it says on the tin. * espconn_secure: remove ESPCONN_SERVER support We can barely function as a TLS client; being a TLS server seems like a real stretch. This code was never called from Lua anyway. * espconn_secure: more code removal * espconn_secure: simplify ssl options structure There is nothing "ssl_packet" about this structure. Get rid of the terrifying "pbuffer" pointer. Squash two structure types together and eliminate an unused field. * espconn_secure: refactor mbedtls_msg_info_load Split out espconn_mbedtls_parse, which we can use as part of our effort towards addressing https://github.com/nodemcu/nodemcu-firmware/issues/3032 * espconn_secure: introduce TLS cert/key callbacks The new feature part of https://github.com/nodemcu/nodemcu-firmware/issues/3032 Subsequent work will remove the old mechanism. * tls: add deprecation warnings * luacheck: net.ifinfo is a thing now * tls: remove use of espconn->reverse * mqtt: stop using espconn->reverse Instead, just place the espconn structure itself at the top of the user data. This enlarges the structure somewhat but removes one more layer of dynamic heap usage and NULL checks. While here, simplify the code a bit. * mqtt: remove redundant pointer to connect_info Everywhere we have the mqtt_state_t we also have the lmqtt_userdata. * mqtt: doc fixes * mqtt: note bug * tls: allow :on(...,nil) to unregister a callback
2020-04-07 14:06:27 +02:00
net.createServer(30) -- 30s timeout
2016-01-11 23:30:30 +01:00
```
#### See also
[`net.createConnection()`](#netcreateconnection), [`net.createUDPSocket()`](#netcreateudpsocket)
## net.createUDPSocket()
Creates an UDP socket.
#### Syntax
`net.createUDPSocket()`
#### Parameters
none
#### Returns
2017-02-01 18:12:56 +01:00
[net.udpsocket sub module](#netudpsocket-module)
2016-01-11 23:30:30 +01:00
#### See also
[`net.createConnection()`](#netcreateconnection)
## net.ifinfo()
Return information about a network interface, specified by index.
#### Syntax
`net.ifinfo(if_index)`
#### Parameters
- `if_index` the interface index; on ESP8266, `0` is the wifi client (STA) and `1`
is the wifi AP.
#### Returns
`nil` if the given `if_index` does not correspond to an interface. Otherwise,
a table containing ...
* `ip`, `netmask`, and `gateway` configured for this interface, as dotted quad strings
or `nil` if none is set.
* if DHCP was used to configure the interface, then `dhcp` will be a table containing...
* `server_ip` -- the DHCP server itself, as a dotted quad
* `client_ip` -- the IP address suggested for the client; likely, this equals `ip`
above, unless the configuration has been overridden.
* `ntp_server` -- the NTP server suggested by the DHCP server.
DNS servers are not tracked per-interface in LwIP and, as such, are not
reported here; use [`net.dns:getdnsserver()`](#netdnsgetdnsserver).
#### Example
`print(net.ifinfo(0).dhcp.ntp_server)` will show the NTP server suggested by
the DHCP server.
## net.multicastJoin()
Join multicast group.
#### Syntax
`net.multicastJoin(if_ip, multicast_ip)`
#### Parameters
- `if_ip` string containing the interface ip to join the multicast group. "any" or "" affects all interfaces.
- `multicast_ip` of the group to join
#### Returns
`nil`
## net.multicastLeave()
Leave multicast group.
#### Syntax
`net.multicastLeave(if_ip, multicast_ip)`
#### Parameters
- `if_ip` string containing the interface ip to leave the multicast group. "any" or "" affects all interfaces.
- `multicast_ip` of the group to leave
#### Returns
`nil`
2016-01-11 23:30:30 +01:00
# net.server Module
## net.server:close()
Closes the server.
#### Syntax
`net.server.close()`
#### Parameters
none
#### Returns
`nil`
#### Example
```lua
-- creates a server
sv = net.createServer(net.TCP, 30)
-- closes the server
sv:close()
```
#### See also
[`net.createServer()`](#netcreateserver)
## net.server:listen()
Listen on port from IP address.
#### Syntax
`net.server.listen([port],[ip],function(net.socket))`
2016-01-11 23:30:30 +01:00
#### Parameters
- `port` port number, can be omitted (random port will be chosen)
2016-01-11 23:30:30 +01:00
- `ip` IP address string, can be omitted
- `function(net.socket)` callback function, pass to caller function as param if a connection is created successfully
#### Returns
`nil`
#### Example
```lua
-- server listens on 80, if data received, print data to console and send "hello world" back to caller
-- 30s time out for a inactive client
sv = net.createServer(net.TCP, 30)
function receiver(sck, data)
print(data)
sck:close()
end
if sv then
sv:listen(80, function(conn)
conn:on("receive", receiver)
conn:send("hello world")
end)
end
2016-01-11 23:30:30 +01:00
```
#### See also
[`net.createServer()`](#netcreateserver)
## net.server:getaddr()
Returns server local address/port.
#### Syntax
`net.server.getaddr()`
#### Parameters
none
#### Returns
`port`, `ip` (or `nil, nil` if not listening)
#### See also
[`net.server:listen()`](#netserverlisten)
2016-01-11 23:30:30 +01:00
# net.socket Module
## net.socket:close()
Closes socket.
#### Syntax
`close()`
#### Parameters
none
#### Returns
`nil`
#### See also
[`net.createServer()`](#netcreateserver)
## net.socket:connect()
Connect to a remote server.
#### Syntax
`connect(port, ip|domain)`
#### Parameters
- `port` port number
- `ip` IP address or domain name string
#### Returns
`nil`
#### See also
[`net.socket:on()`](#netsocketon)
## net.socket:dns()
Provides DNS resolution for a hostname.
#### Syntax
`dns(domain, function(net.socket, ip))`
#### Parameters
- `domain` domain name
- `function(net.socket, ip)` callback function. The first parameter is the socket, the second parameter is the IP address as a string.
2019-07-27 14:18:34 +02:00
If a callback `c` is provided, it is equivalent to having called `:on("dns",
c)` on this socket; this callback will, hereafter, receive any pending
resolution results recieved for this socket!
2016-01-11 23:30:30 +01:00
#### Returns
`nil`
#### Example
```lua
sk = net.createConnection(net.TCP, 0)
sk:dns("www.nodemcu.com", function(conn, ip) print(ip) end)
sk = nil
```
#### See also
[`net.createServer()`](#netcreateserver)
## net.socket:getpeer()
Retrieve port and ip of remote peer.
#### Syntax
`getpeer()`
#### Parameters
none
#### Returns
`port`, `ip` (or `nil, nil` if not connected)
## net.socket:getaddr()
Retrieve local port and ip of socket.
#### Syntax
`getaddr()`
#### Parameters
none
#### Returns
`port`, `ip` (or `nil, nil` if not connected)
## net.socket:hold()
Throttle data reception by placing a request to block the TCP receive function. This request is not effective immediately, Espressif recommends to call it while reserving 5*1460 bytes of memory.
#### Syntax
`hold()`
#### Parameters
none
#### Returns
`nil`
#### See also
[`net.socket:unhold()`](#netsocketunhold)
2016-01-11 23:30:30 +01:00
## net.socket:on()
Register callback functions for specific events.
#### Syntax
`on(event, function())`
#### Parameters
- `event` string, which can be "connection", "reconnection", "disconnection", "receive" or "sent"
- `function(net.socket[, string])` callback function. Can be `nil` to remove callback.
The first parameter of callback is the socket.
- If event is "receive", the second parameter is the received data as string.
- If event is "disconnection" or "reconnection", the second parameter is error code.
If reconnection event is specified, disconnection receives only "normal close" events.
Otherwise, all connection errors (with normal close) passed to disconnection event.
2016-01-11 23:30:30 +01:00
#### Returns
`nil`
#### Example
```lua
srv = net.createConnection(net.TCP, 0)
srv:on("receive", function(sck, c) print(c) end)
2017-04-21 07:49:27 +02:00
-- Wait for connection before sending.
srv:on("connection", function(sck, c)
2019-02-17 19:26:29 +01:00
-- 'Connection: close' rather than 'Connection: keep-alive' to have server
-- initiate a close of the connection after final response (frees memory
-- earlier here), https://tools.ietf.org/html/rfc7230#section-6.6
2017-04-21 07:49:27 +02:00
sck:send("GET /get HTTP/1.1\r\nHost: httpbin.org\r\nConnection: close\r\nAccept: */*\r\n\r\n")
2016-01-11 23:30:30 +01:00
end)
srv:connect(80,"httpbin.org")
2016-01-11 23:30:30 +01:00
```
!!! note
The `receive` event is fired for every network frame! Hence, if the data sent to the device exceeds 1460 bytes (derived from [Ethernet frame size](https://en.wikipedia.org/wiki/Ethernet_frame)) it will fire more than once. There may be other situations where incoming data is split across multiple frames (e.g. HTTP POST with `multipart/form-data`). You need to manually buffer the data and find means to determine if all data was received.
2019-02-17 19:26:29 +01:00
```lua
local buffer = nil
srv:on("receive", function(sck, c)
if buffer == nil then
buffer = c
else
buffer = buffer .. c
end
end)
-- throttling could be implemented using socket:hold()
-- example: https://github.com/nodemcu/nodemcu-firmware/blob/master/lua_examples/pcm/play_network.lua#L83
2019-02-17 19:26:29 +01:00
```
2016-01-11 23:30:30 +01:00
#### See also
- [`net.createServer()`](#netcreateserver)
- [`net.socket:hold()`](#netsockethold)
2016-01-11 23:30:30 +01:00
## net.socket:send()
Sends data to remote peer.
2016-01-11 23:30:30 +01:00
#### Syntax
`send(string[, function(sent)])`
`sck:send(data, fnA)` is functionally equivalent to `sck:send(data) sck:on("sent", fnA)`.
2016-01-11 23:30:30 +01:00
#### Parameters
- `string` data in string which will be sent to server
- `function(sent)` callback function for sending string
#### Returns
`nil`
#### Note
Multiple consecutive `send()` calls aren't guaranteed to work (and often don't) as network requests are treated as separate tasks by the SDK. Instead, subscribe to the "sent" event on the socket and send additional data (or close) in that callback. See [#730](https://github.com/nodemcu/nodemcu-firmware/issues/730#issuecomment-154241161) for details.
#### Example
```lua
srv = net.createServer(net.TCP)
function receiver(sck, data)
local response = {}
-- if you're sending back HTML over HTTP you'll want something like this instead
-- local response = {"HTTP/1.0 200 OK\r\nServer: NodeMCU on ESP8266\r\nContent-Type: text/html\r\n\r\n"}
response[#response + 1] = "lots of data"
response[#response + 1] = "even more data"
response[#response + 1] = "e.g. content read from a file"
-- sends and removes the first element from the 'response' table
local function send(localSocket)
2016-12-25 16:10:31 +01:00
if #response > 0 then
localSocket:send(table.remove(response, 1))
else
localSocket:close()
response = nil
end
end
-- triggers the send() function again once the first chunk of data was sent
sck:on("sent", send)
send(sck)
end
srv:listen(80, function(conn)
conn:on("receive", receiver)
end)
```
If you do not or can not keep all the data you send back in memory at one time (remember that `response` is an aggregation) you may use explicit callbacks instead of building up a table like so:
```lua
2019-02-17 19:26:29 +01:00
sck:send(header, function()
local data1 = "some large chunk of dynamically loaded data"
sck:send(data1, function()
local data2 = "even more dynamically loaded data"
2019-02-17 19:26:29 +01:00
sck:send(data2, function(sk)
sk:close()
end)
end)
end)
```
2016-01-11 23:30:30 +01:00
#### See also
[`net.socket:on()`](#netsocketon)
2017-02-06 13:54:20 +01:00
## 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()`.
#### Syntax
`unhold()`
#### Parameters
none
#### Returns
`nil`
#### See also
[`net.socket:hold()`](#netsockethold)
# net.udpsocket Module
Remember that in contrast to TCP [UDP](https://en.wikipedia.org/wiki/User_Datagram_Protocol) is connectionless. Therefore, there is a minor but natural mismatch as for TCP/UDP functions in this module. While you would call [net.createConnection()](#netcreateconnection) for TCP it is [net.createUDPSocket()](#netcreateudpsocket) for UDP.
Other points worth noting:
- UDP sockets do not have a connection callback for the [`listen`](#netudpsocketlisten) function.
- UDP sockets do not have a `connect` function. Remote IP and port thus need to be defined in [`send()`](#netudpsocketsend).
- UDP socket's `receive` callback receives port/ip after the `data` argument.
## net.udpsocket:close()
Closes UDP socket.
The syntax and functional identical to [`net.socket:close()`](#netsocketclose).
## net.udpsocket:listen()
Listen on port from IP address.
The syntax and functional similar to [`net.server:listen()`](#netserverlisten), but callback parameter is not provided.
## net.udpsocket:on()
Register callback functions for specific events.
2017-02-01 18:12:56 +01:00
The syntax and functional similar to [`net.socket:on()`](#netsocketon). However, only "receive", "sent" and "dns" are supported events.
!!! note
2017-02-01 18:12:56 +01:00
The `receive` callback receives `port` and `ip` *after* the `data` argument.
## net.udpsocket:send()
Sends data to specific remote peer.
#### Syntax
`send(port, ip, data)`
#### Parameters
- `port` remote socket port
- `ip` remote socket IP
- `data` the payload to send
#### Returns
`nil`
#### Example
```lua
udpSocket = net.createUDPSocket()
udpSocket:listen(5000)
udpSocket:on("receive", function(s, data, port, ip)
print(string.format("received '%s' from %s:%d", data, ip, port))
s:send(port, ip, "echo: " .. data)
end)
port, ip = udpSocket:getaddr()
print(string.format("local UDP socket address / port: %s:%d", ip, port))
```
On *nix systems that can then be tested by issuing
```
echo -n "foo" | nc -w1 -u <device-IP-address> 5000
```
## net.udpsocket:dns()
Provides DNS resolution for a hostname.
The syntax and functional identical to [`net.socket:dns()`](#netsocketdns).
## net.udpsocket:getaddr()
Retrieve local port and ip of socket.
The syntax and functional identical to [`net.socket:getaddr()`](#netsocketgetaddr).
2017-02-06 13:54:20 +01:00
## net.udpsocket:ttl()
Changes or retrieves Time-To-Live value on socket.
The syntax and functional identical to [`net.socket:ttl()`](#netsocketttl).
2016-01-11 23:30:30 +01:00
# net.dns Module
## net.dns.getdnsserver()
Gets the IP address of the DNS server used to resolve hostnames.
#### Syntax
`net.dns.getdnsserver(dns_index)`
#### Parameters
dns_index which DNS server to get (range 0~1)
#### Returns
IP address (string) of DNS server
#### Example
```lua
print(net.dns.getdnsserver(0)) -- 208.67.222.222
print(net.dns.getdnsserver(1)) -- nil
net.dns.setdnsserver("8.8.8.8", 0)
net.dns.setdnsserver("192.168.1.252", 1)
print(net.dns.getdnsserver(0)) -- 8.8.8.8
print(net.dns.getdnsserver(1)) -- 192.168.1.252
```
#### See also
[`net.dns:setdnsserver()`](#netdnssetdnsserver)
## net.dns.resolve()
Resolve a hostname to an IP address. Doesn't require a socket like [`net.socket.dns()`](#netsocketdns).
#### Syntax
`net.dns.resolve(host, function(sk, ip))`
2016-01-11 23:30:30 +01:00
#### Parameters
- `host` hostname to resolve
- `function(sk, ip)` callback called when the name was resolved. `sk` is always `nil`
2016-01-11 23:30:30 +01:00
#### Returns
`nil` but may raise errors for severe network stack issues (e.g., out of DNS query table slots)
2016-01-11 23:30:30 +01:00
#### Example
```lua
net.dns.resolve("www.google.com", function(sk, ip)
if (ip == nil) then print("DNS fail!") else print(ip) end
end)
```
#### See also
[`net.socket:dns()`](#netsocketdns)
## net.dns.setdnsserver()
Sets the IP of the DNS server used to resolve hostnames. Default: resolver1.opendns.com (208.67.222.222). You can specify up to 2 DNS servers.
#### Syntax
`net.dns.setdnsserver(dns_ip_addr, dns_index)`
#### Parameters
- `dns_ip_addr` IP address of a DNS server
2019-02-17 19:26:29 +01:00
- `dns_index` which DNS server to set (range 0~1). Hence, it supports max. 2 servers.
2016-01-11 23:30:30 +01:00
#### Returns
`nil`
#### See also
[`net.dns:getdnsserver()`](#netdnsgetdnsserver)
Adds support for verifying the certificate offered by an SSL server. Squashed commit of the following: commit 4439b8c45192f6dee1222df78bbb59f74509e0ee Author: philip <philip@gladstonefamily.net> Date: Sun Mar 6 20:23:21 2016 -0500 Fix the ignore commit a07ee5acdf91286607c3e2dce128c9b8bfd7bd80 Author: philip <philip@gladstonefamily.net> Date: Sun Mar 6 20:20:41 2016 -0500 Remove uneeded stuff commit b3604ace92fc13b30161d385c354b0f1c5fe4046 Author: philip <philip@gladstonefamily.net> Date: Sun Mar 6 20:15:26 2016 -0500 Remove client cert auth commit 6e48c633569630736a986cd07a59a12de954391e Author: philip <philip@gladstonefamily.net> Date: Sun Mar 6 20:11:42 2016 -0500 More cleanup commit d40eade405ef071d0d1b60d038566b5b8f2cafa3 Author: philip <philip@gladstonefamily.net> Date: Sat Mar 5 10:56:56 2016 -0500 Move to almost working version commit 1860a2d90afa94461c53bd41251d4870d6527f9d Author: philip <philip@gladstonefamily.net> Date: Fri Mar 4 08:04:09 2016 -0500 Changed the naem to server-ca.crt commit e7a315660843273fe62943b7fe8ee6c0541dada2 Author: philip <philip@gladstonefamily.net> Date: Thu Mar 3 21:16:26 2016 -0500 Update gitignores commit 2b037d185c396209b64381399c40821c15e1840e Author: philip <philip@gladstonefamily.net> Date: Thu Mar 3 08:56:17 2016 -0500 Getting better commit 763255cffba8e279158cd7f43391a3573efdeca8 Author: philip <philip@gladstonefamily.net> Date: Wed Mar 2 22:28:21 2016 -0500 Works a bit better commit a38325d1a47dbad255cb3e681da8415e8cf699ea Author: philip <philip@gladstonefamily.net> Date: Wed Mar 2 09:11:04 2016 -0500 First building version commit 4aef13da33470ed954f2eaf5f7ac0ac3dcdf3774 Merge: 180e147 ebb0c33 Author: philip <philip@gladstonefamily.net> Date: Tue Mar 1 22:03:06 2016 -0500 Merge remote-tracking branch 'upstream/dev' into ssl-client commit 180e147c1abdcf4046ad9be9b3c1a48f4a875312 Author: philip <philip@gladstonefamily.net> Date: Sun Feb 28 21:34:21 2016 -0500 Missing files from espressif Try to imporve layout Align the file names with the contents Missing file Review comments More review coments
2016-03-07 02:25:05 +01:00
# net.cert Module
This part gone to the [TLS](tls.md) module, link kept for backward compatibility.