Commit af426d03 authored by Nathaniel Wesley Filardo's avatar Nathaniel Wesley Filardo Committed by Marcel Stör
Browse files

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
parent 4a78580d
...@@ -563,21 +563,20 @@ void ICACHE_FLASH_ATTR http_raw_request( const char * hostname, int port, bool s ...@@ -563,21 +563,20 @@ void ICACHE_FLASH_ATTR http_raw_request( const char * hostname, int port, bool s
req->redirect_follow_count = redirect_follow_count; req->redirect_follow_count = redirect_follow_count;
ip_addr_t addr; ip_addr_t addr;
err_t error = espconn_gethostbyname( (struct espconn *) req, /* It seems we don't need a real espconn pointer here. */ err_t error = dns_gethostbyname( hostname, &addr, http_dns_callback, req );
hostname, &addr, http_dns_callback );
if ( error == ESPCONN_INPROGRESS ) if ( error == ERR_INPROGRESS )
{ {
HTTPCLIENT_DEBUG( "DNS pending" ); HTTPCLIENT_DEBUG( "DNS pending" );
} }
else if ( error == ESPCONN_OK ) else if ( error == ERR_OK )
{ {
/* Already in the local names table (or hostname was an IP address), execute the callback ourselves. */ /* Already in the local names table (or hostname was an IP address), execute the callback ourselves. */
http_dns_callback( hostname, &addr, req ); http_dns_callback( hostname, &addr, req );
} }
else else
{ {
if ( error == ESPCONN_ARG ) if ( error == ERR_ARG )
{ {
HTTPCLIENT_ERR( "DNS arg error %s", hostname ); HTTPCLIENT_ERR( "DNS arg error %s", hostname );
}else { }else {
......
...@@ -149,12 +149,6 @@ enum espconn_option{ ...@@ -149,12 +149,6 @@ enum espconn_option{
ESPCONN_END ESPCONN_END
}; };
enum espconn_level{
ESPCONN_KEEPIDLE,
ESPCONN_KEEPINTVL,
ESPCONN_KEEPCNT
};
enum espconn_mode{ enum espconn_mode{
ESPCONN_NOMODE, ESPCONN_NOMODE,
ESPCONN_TCPSERVER_MODE, ESPCONN_TCPSERVER_MODE,
...@@ -300,16 +294,6 @@ bool espconn_find_connection(struct espconn *pespconn, espconn_msg **pnode); ...@@ -300,16 +294,6 @@ bool espconn_find_connection(struct espconn *pespconn, espconn_msg **pnode);
sint8 espconn_get_connection_info(struct espconn *pespconn, remot_info **pcon_info, uint8 typeflags); sint8 espconn_get_connection_info(struct espconn *pespconn, remot_info **pcon_info, uint8 typeflags);
/******************************************************************************
* FunctionName : espconn_get_packet_info
* Description : get the packet info with host
* Parameters : espconn -- the espconn used to disconnect the connection
* infoarg -- the packet info
* Returns : the errur code
*******************************************************************************/
sint8 espconn_get_packet_info(struct espconn *espconn, struct espconn_packet* infoarg);
/****************************************************************************** /******************************************************************************
* FunctionName : espconn_connect * FunctionName : espconn_connect
* Description : The function given as the connect * Description : The function given as the connect
...@@ -355,23 +339,6 @@ extern sint8 espconn_accept(struct espconn *espconn); ...@@ -355,23 +339,6 @@ extern sint8 espconn_accept(struct espconn *espconn);
extern sint8 espconn_create(struct espconn *espconn); extern sint8 espconn_create(struct espconn *espconn);
/******************************************************************************
* FunctionName : espconn_tcp_get_wnd
* Description : get the window size of simulatenously active TCP connections
* Parameters : none
* Returns : the number of TCP_MSS active TCP connections
*******************************************************************************/
extern uint8 espconn_tcp_get_wnd(void);
/******************************************************************************
* FunctionName : espconn_tcp_set_max_con
* Description : set the window size simulatenously active TCP connections
* Parameters : num -- the number of TCP_MSS
* Returns : ESPCONN_ARG -- Illegal argument
* ESPCONN_OK -- No error
*******************************************************************************/
extern sint8 espconn_tcp_set_wnd(uint8 num);
/****************************************************************************** /******************************************************************************
* FunctionName : espconn_tcp_get_max_con * FunctionName : espconn_tcp_get_max_con
* Description : get the number of simulatenously active TCP connections * Description : get the number of simulatenously active TCP connections
...@@ -381,50 +348,6 @@ extern sint8 espconn_tcp_set_wnd(uint8 num); ...@@ -381,50 +348,6 @@ extern sint8 espconn_tcp_set_wnd(uint8 num);
extern uint8 espconn_tcp_get_max_con(void); extern uint8 espconn_tcp_get_max_con(void);
/******************************************************************************
* FunctionName : espconn_tcp_set_max_con
* Description : set the number of simulatenously active TCP connections
* Parameters : num -- total number
* Returns : none
*******************************************************************************/
extern sint8 espconn_tcp_set_max_con(uint8 num);
/******************************************************************************
* FunctionName : espconn_tcp_get_max_retran
* Description : get the Maximum number of retransmissions of data active TCP connections
* Parameters : none
* Returns : the Maximum number of retransmissions
*******************************************************************************/
extern uint8 espconn_tcp_get_max_retran(void);
/******************************************************************************
* FunctionName : espconn_tcp_set_max_retran
* Description : set the Maximum number of retransmissions of data active TCP connections
* Parameters : num -- the Maximum number of retransmissions
* Returns : result
*******************************************************************************/
extern sint8 espconn_tcp_set_max_retran(uint8 num);
/******************************************************************************
* FunctionName : espconn_tcp_get_max_syn
* Description : get the Maximum number of retransmissions of SYN segments
* Parameters : none
* Returns : the Maximum number of retransmissions
*******************************************************************************/
extern uint8 espconn_tcp_get_max_syn(void);
/******************************************************************************
* FunctionName : espconn_tcp_set_max_syn
* Description : set the Maximum number of retransmissions of SYN segments
* Parameters : num -- the Maximum number of retransmissions
* Returns : result
*******************************************************************************/
extern sint8 espconn_tcp_set_max_syn(uint8 num);
/****************************************************************************** /******************************************************************************
* FunctionName : espconn_tcp_get_max_con_allow * FunctionName : espconn_tcp_get_max_con_allow
* Description : get the count of simulatenously active connections on the server * Description : get the count of simulatenously active connections on the server
...@@ -434,15 +357,6 @@ extern sint8 espconn_tcp_set_max_syn(uint8 num); ...@@ -434,15 +357,6 @@ extern sint8 espconn_tcp_set_max_syn(uint8 num);
extern sint8 espconn_tcp_get_max_con_allow(struct espconn *espconn); extern sint8 espconn_tcp_get_max_con_allow(struct espconn *espconn);
/******************************************************************************
* FunctionName : espconn_tcp_set_max_con_allow
* Description : set the count of simulatenously active connections on the server
* Parameters : espconn -- espconn to set the count
* Returns : result
*******************************************************************************/
extern sint8 espconn_tcp_set_max_con_allow(struct espconn *espconn, uint8 num);
/****************************************************************************** /******************************************************************************
* FunctionName : espconn_tcp_set_buf_count * FunctionName : espconn_tcp_set_buf_count
* Description : set the total number of espconn_buf on the unsent lists * Description : set the total number of espconn_buf on the unsent lists
...@@ -561,65 +475,6 @@ extern sint8 espconn_regist_disconcb(struct espconn *espconn, espconn_connect_ca ...@@ -561,65 +475,6 @@ extern sint8 espconn_regist_disconcb(struct espconn *espconn, espconn_connect_ca
extern uint32 espconn_port(void); extern uint32 espconn_port(void);
/******************************************************************************
* FunctionName : espconn_set_opt
* Description : access port value for client so that we don't end up bouncing
* all connections at the same time .
* Parameters : none
* Returns : access port value
*******************************************************************************/
extern sint8 espconn_set_opt(struct espconn *espconn, uint8 opt);
/******************************************************************************
* FunctionName : espconn_set_keepalive
* Description : access level value for connection so that we set the value for
* keep alive
* Parameters : espconn -- the espconn used to set the connection
* level -- the connection's level
* value -- the value of time(s)
* Returns : access port value
*******************************************************************************/
extern sint8 espconn_set_keepalive(struct espconn *espconn, uint8 level, void* optarg);
/******************************************************************************
* FunctionName : espconn_get_keepalive
* Description : access level value for connection so that we get the value for
* keep alive
* Parameters : espconn -- the espconn used to get the connection
* level -- the connection's level
* Returns : access keep alive value
*******************************************************************************/
extern sint8 espconn_get_keepalive(struct espconn *espconn, uint8 level, void *optarg);
/******************************************************************************
* FunctionName : espconn_gethostbyname
* Description : Resolve a hostname (string) into an IP address.
* Parameters : pespconn -- espconn to resolve a hostname
* hostname -- the hostname that is to be queried
* addr -- pointer to a ip_addr_t where to store the address if
* it is already cached in the dns_table (only valid if
* ESPCONN_OK is returned!)
* found -- a callback function to be called on success, failure
* or timeout (only if ERR_INPROGRESS is returned!)
* Returns : err_t return code
* - ESPCONN_OK if hostname is a valid IP address string or the host
* name is already in the local names table.
* - ESPCONN_INPROGRESS enqueue a request to be sent to the DNS server
* for resolution if no errors are present.
* - ESPCONN_ARG: dns client not initialized or invalid hostname
*******************************************************************************/
extern err_t espconn_gethostbyname(struct espconn *pespconn, const char *name, ip_addr_t *addr, dns_found_callback found);
/******************************************************************************
* FunctionName : espconn_abort
* Description : Forcely abort with host
* Parameters : espconn -- the espconn used to connect with the host
* Returns : result
*******************************************************************************/
extern sint8 espconn_abort(struct espconn *espconn);
/****************************************************************************** /******************************************************************************
* FunctionName : espconn_encry_connect * FunctionName : espconn_encry_connect
* Description : The function given as connection * Description : The function given as connection
...@@ -660,27 +515,6 @@ extern sint8 espconn_secure_send(struct espconn *espconn, uint8 *psent, uint16 l ...@@ -660,27 +515,6 @@ extern sint8 espconn_secure_send(struct espconn *espconn, uint8 *psent, uint16 l
extern sint8 espconn_secure_sent(struct espconn *espconn, uint8 *psent, uint16 length); extern sint8 espconn_secure_sent(struct espconn *espconn, uint8 *psent, uint16 length);
/******************************************************************************
* FunctionName : espconn_secure_set_size
* Description : set the buffer size for client or server
* Parameters : level -- set for client or server
* 1: client,2:server,3:client and server
* size -- buffer size
* Returns : true or false
*******************************************************************************/
extern bool espconn_secure_set_size(uint8 level, uint16 size);
/******************************************************************************
* FunctionName : espconn_secure_get_size
* Description : get buffer size for client or server
* Parameters : level -- set for client or server
* 1: client,2:server,3:client and server
* Returns : buffer size for client or server
*******************************************************************************/
extern sint16 espconn_secure_get_size(uint8 level);
/****************************************************************************** /******************************************************************************
* FunctionName : espconn_secure_ca_enable * FunctionName : espconn_secure_ca_enable
* Description : enable the certificate authenticate and set the flash sector * Description : enable the certificate authenticate and set the flash sector
...@@ -726,65 +560,6 @@ extern bool espconn_secure_cert_req_enable(uint8 level, uint32 flash_sector ); ...@@ -726,65 +560,6 @@ extern bool espconn_secure_cert_req_enable(uint8 level, uint32 flash_sector );
extern bool espconn_secure_cert_req_disable(uint8 level); extern bool espconn_secure_cert_req_disable(uint8 level);
/******************************************************************************
* FunctionName : espconn_secure_set_default_certificate
* Description : Load the certificates in memory depending on compile-time
* and user options.
* Parameters : certificate -- Load the certificate
* length -- Load the certificate length
* Returns : result true or false
*******************************************************************************/
extern bool espconn_secure_set_default_certificate(const uint8* certificate, uint16 length);
/******************************************************************************
* FunctionName : espconn_secure_set_default_private_key
* Description : Load the key in memory depending on compile-time
* and user options.
* Parameters : private_key -- Load the key
* length -- Load the key length
* Returns : result true or false
*******************************************************************************/
extern bool espconn_secure_set_default_private_key(const uint8* private_key, uint16 length);
/******************************************************************************
* FunctionName : espconn_secure_accept
* Description : The function given as the listen
* Parameters : espconn -- the espconn used to listen the connection
* Returns : result
*******************************************************************************/
extern sint8 espconn_secure_accept(struct espconn *espconn);
/******************************************************************************
* FunctionName : espconn_secure_accepts
* Description : delete the secure server host
* Parameters : espconn -- the espconn used to listen the connection
* Returns : result
*******************************************************************************/
extern sint8 espconn_secure_delete(struct espconn *espconn);
/******************************************************************************
* FunctionName : espconn_igmp_join
* Description : join a multicast group
* Parameters : host_ip -- the ip address of udp server
* multicast_ip -- multicast ip given by user
* Returns : none
*******************************************************************************/
extern sint8 espconn_igmp_join(ip_addr_t *host_ip, ip_addr_t *multicast_ip);
/******************************************************************************
* FunctionName : espconn_igmp_leave
* Description : leave a multicast group
* Parameters : host_ip -- the ip address of udp server
* multicast_ip -- multicast ip given by user
* Returns : none
*******************************************************************************/
extern sint8 espconn_igmp_leave(ip_addr_t *host_ip, ip_addr_t *multicast_ip);
/****************************************************************************** /******************************************************************************
* FunctionName : espconn_recv_hold * FunctionName : espconn_recv_hold
* Description : hold tcp receive * Description : hold tcp receive
...@@ -801,86 +576,5 @@ extern sint8 espconn_recv_hold(struct espconn *pespconn); ...@@ -801,86 +576,5 @@ extern sint8 espconn_recv_hold(struct espconn *pespconn);
*******************************************************************************/ *******************************************************************************/
extern sint8 espconn_recv_unhold(struct espconn *pespconn); extern sint8 espconn_recv_unhold(struct espconn *pespconn);
/******************************************************************************
* FunctionName : espconn_mdns_init
* Description : register a device with mdns
* Parameters : ipAddr -- the ip address of device
* hostname -- the hostname of device
* Returns : none
*******************************************************************************/
extern void espconn_mdns_init(struct mdns_info *info);
/******************************************************************************
* FunctionName : espconn_mdns_init
* Description : close mdns socket
* Parameters : void
* Returns : none
*******************************************************************************/
extern void espconn_mdns_close(void);
/******************************************************************************
* FunctionName : mdns_server_register
* Description : register a server and join a multicast group
* Parameters : none
* Returns : none
*******************************************************************************/
extern void espconn_mdns_server_register(void);
/******************************************************************************
* FunctionName : mdns_server_register
* Description : unregister server and leave multicast group
* Parameters : none
* Returns : none
*******************************************************************************/
extern void espconn_mdns_server_unregister(void);
/******************************************************************************
* FunctionName : espconn_mdns_get_servername
* Description : get server name
* Parameters : none
* Returns : server name
*******************************************************************************/
extern char* espconn_mdns_get_servername(void);
/******************************************************************************
* FunctionName : espconn_mdns_get_servername
* Description : set server name
* Parameters : server name
* Returns : none
*******************************************************************************/
extern void espconn_mdns_set_servername(const char *name);
/******************************************************************************
* FunctionName : espconn_mdns_set_hostname
* Description : set host name
* Parameters : host name
* Returns : none
*******************************************************************************/
extern void espconn_mdns_set_hostname(char *name);
/******************************************************************************
* FunctionName : espconn_mdns_init
* Description : get host name
* Parameters : void
* Returns : hostname
*******************************************************************************/
extern char* espconn_mdns_get_hostname(void);
/******************************************************************************
* FunctionName : espconn_mdns_disable
* Description : join a multicast group
* Parameters : host_ip -- the ip address of udp server
* multicast_ip -- multicast ip given by user
* Returns : none
*******************************************************************************/
extern void espconn_mdns_disable(void);
/******************************************************************************
* FunctionName : espconn_mdns_enable
* Description : enable mdns
* Parameters : void
* Returns : none
*******************************************************************************/
extern void espconn_mdns_enable(void);
/******************************************************************************
* FunctionName : espconn_dns_setserver
* Description : Initialize one of the DNS servers.
* Parameters : numdns -- the index of the DNS server to set must
* be < DNS_MAX_SERVERS = 2
* dnsserver -- IP address of the DNS server to set
* Returns : none
*******************************************************************************/
extern void espconn_dns_setserver(u8_t numdns, ip_addr_t *dnsserver);
#endif #endif
...@@ -51,7 +51,6 @@ typedef struct{ ...@@ -51,7 +51,6 @@ typedef struct{
int record_len; int record_len;
pmbedtls_session psession; pmbedtls_session psession;
mbedtls_net_context fd; mbedtls_net_context fd;
mbedtls_net_context listen_fd;
mbedtls_ctr_drbg_context ctr_drbg; mbedtls_ctr_drbg_context ctr_drbg;
mbedtls_ssl_context ssl; mbedtls_ssl_context ssl;
mbedtls_ssl_config conf; mbedtls_ssl_config conf;
...@@ -65,14 +64,11 @@ typedef enum { ...@@ -65,14 +64,11 @@ typedef enum {
ESPCONN_CERT_OWN, ESPCONN_CERT_OWN,
ESPCONN_CERT_AUTH, ESPCONN_CERT_AUTH,
ESPCONN_PK, ESPCONN_PK,
ESPCONN_PASSWORD
}mbedtls_auth_type; }mbedtls_auth_type;
typedef enum { typedef enum {
ESPCONN_IDLE = 0, ESPCONN_IDLE = 0,
ESPCONN_CLIENT, ESPCONN_CLIENT,
ESPCONN_SERVER,
ESPCONN_BOTH,
ESPCONN_MAX ESPCONN_MAX
}espconn_level; }espconn_level;
...@@ -91,23 +87,14 @@ typedef struct _ssl_sector{ ...@@ -91,23 +87,14 @@ typedef struct _ssl_sector{
bool flag; bool flag;
}ssl_sector; }ssl_sector;
struct ssl_packet{ struct ssl_options {
uint8* pbuffer;
uint16 buffer_size; uint16 buffer_size;
ssl_sector cert_ca_sector; ssl_sector cert_ca_sector;
ssl_sector cert_req_sector; ssl_sector cert_req_sector;
};
typedef struct _ssl_opt {
struct ssl_packet server;
struct ssl_packet client;
uint8 type;
}ssl_opt;
typedef struct{ int cert_verify_callback;
mbedtls_auth_type auth_type; int cert_auth_callback;
espconn_level auth_level; };
}mbedtls_auth_info;
#define SSL_KEEP_INTVL 1 #define SSL_KEEP_INTVL 1
#define SSL_KEEP_CNT 5 #define SSL_KEEP_CNT 5
...@@ -122,13 +109,12 @@ enum { ...@@ -122,13 +109,12 @@ enum {
#define ESPCONN_SECURE_MAX_SIZE 8192 #define ESPCONN_SECURE_MAX_SIZE 8192
#define ESPCONN_SECURE_DEFAULT_HEAP 0x3800 #define ESPCONN_SECURE_DEFAULT_HEAP 0x3800
#define ESPCONN_SECURE_DEFAULT_SIZE SSL_BUFFER_SIZE
#define ESPCONN_HANDSHAKE_TIMEOUT 0x3C #define ESPCONN_HANDSHAKE_TIMEOUT 0x3C
#define ESPCONN_INVALID_TYPE 0xFFFFFFFF #define ESPCONN_INVALID_TYPE 0xFFFFFFFF
#define MBEDTLS_SSL_PLAIN_ADD TCP_MSS #define MBEDTLS_SSL_PLAIN_ADD TCP_MSS
#define FLASH_SECTOR_SIZE 4096 #define FLASH_SECTOR_SIZE 4096
extern ssl_opt ssl_option; extern struct ssl_options ssl_client_options;
typedef struct{ typedef struct{
uint32 parame_sec; uint32 parame_sec;
...@@ -199,25 +185,6 @@ typedef enum{ ...@@ -199,25 +185,6 @@ typedef enum{
} \ } \
} while (0) } while (0)
/******************************************************************************
* FunctionName : mbedtls_load_default_obj
* Description : Initialize the server: set up a listen PCB and bind it to
* the defined port
* Parameters : espconn -- the espconn used to build client
* Returns : none
*******************************************************************************/
bool mbedtls_load_default_obj(uint32 flash_sector, int obj_type, const unsigned char *load_buf, uint16 length);
/******************************************************************************
* FunctionName : sslserver_start
* Description : Initialize the server: set up a listen PCB and bind it to
* the defined port
* Parameters : espconn -- the espconn used to build client
* Returns : none
*******************************************************************************/
extern sint8 espconn_ssl_server(struct espconn *espconn);
/****************************************************************************** /******************************************************************************
* FunctionName : espconn_ssl_client * FunctionName : espconn_ssl_client
* Description : Initialize the client: set up a connect PCB and bind it to * Description : Initialize the client: set up a connect PCB and bind it to
...@@ -248,16 +215,6 @@ extern void espconn_ssl_sent(void *arg, uint8 *psent, uint16 length); ...@@ -248,16 +215,6 @@ extern void espconn_ssl_sent(void *arg, uint8 *psent, uint16 length);
extern void espconn_ssl_disconnect(espconn_msg *pdis); extern void espconn_ssl_disconnect(espconn_msg *pdis);
/******************************************************************************
* FunctionName : espconn_secure_get_size
* Description : get buffer size for client or server
* Parameters : level -- set for client or server
* 1: client,2:server,3:client and server
* Returns : buffer size for client or server
*******************************************************************************/
extern sint16 espconn_secure_get_size(uint8 level);
#endif #endif
......
...@@ -303,17 +303,11 @@ extern void mbedtls_free_wrap(void *p); ...@@ -303,17 +303,11 @@ extern void mbedtls_free_wrap(void *p);
//#define MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT 86400 /**< 1 day */ //#define MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT 86400 /**< 1 day */
//#define MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES 50 /**< Maximum entries in cache */ //#define MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES 50 /**< Maximum entries in cache */
#if 0
// dynamic buffer sizing with espconn_secure_set_size()
extern unsigned int max_content_len;
#define MBEDTLS_SSL_MAX_CONTENT_LEN max_content_len;
#else
// the current mbedtls integration doesn't allow to set the buffer size dynamically: // the current mbedtls integration doesn't allow to set the buffer size dynamically:
// MBEDTLS_SSL_MAX_FRAGMENT_LENGTH feature and dynamic sizing are mutually exclusive // MBEDTLS_SSL_MAX_FRAGMENT_LENGTH feature and dynamic sizing are mutually exclusive
// due to non-constant initializer element in app/mbedtls/library/ssl_tls.c:150 // due to non-constant initializer element in app/mbedtls/library/ssl_tls.c:150
// the buffer size is hardcoded here and value is taken from SSL_BUFFER_SIZE (user_config.h) // the buffer size is hardcoded here and value is taken from SSL_BUFFER_SIZE (user_config.h)
#define MBEDTLS_SSL_MAX_CONTENT_LEN SSL_BUFFER_SIZE /**< Maxium fragment length in bytes, determines the size of each of the two internal I/O buffers */ #define MBEDTLS_SSL_MAX_CONTENT_LEN SSL_BUFFER_SIZE /**< Maxium fragment length in bytes, determines the size of each of the two internal I/O buffers */
#endif
//#define MBEDTLS_SSL_DEFAULT_TICKET_LIFETIME 86400 /**< Lifetime of session tickets (if enabled) */ //#define MBEDTLS_SSL_DEFAULT_TICKET_LIFETIME 86400 /**< Lifetime of session tickets (if enabled) */
//#define MBEDTLS_PSK_MAX_LEN 32 /**< Max size of TLS pre-shared keys, in bytes (default 256 bits) */ //#define MBEDTLS_PSK_MAX_LEN 32 /**< Max size of TLS pre-shared keys, in bytes (default 256 bits) */
......
...@@ -33,8 +33,6 @@ espconn_msg *plink_active = NULL; ...@@ -33,8 +33,6 @@ espconn_msg *plink_active = NULL;
espconn_msg *pserver_list = NULL; espconn_msg *pserver_list = NULL;
remot_info premot[linkMax]; remot_info premot[linkMax];
struct espconn_packet pktinfo[2];
static uint8 espconn_tcp_get_buf_count(espconn_buf *pesp_buf); static uint8 espconn_tcp_get_buf_count(espconn_buf *pesp_buf);
/****************************************************************************** /******************************************************************************
* FunctionName : espconn_copy_partial * FunctionName : espconn_copy_partial
...@@ -472,33 +470,6 @@ sint16 ICACHE_FLASH_ATTR espconn_recv(struct espconn *espconn, void *mem, size_t ...@@ -472,33 +470,6 @@ sint16 ICACHE_FLASH_ATTR espconn_recv(struct espconn *espconn, void *mem, size_t
return ESPCONN_ARG; return ESPCONN_ARG;
} }
/******************************************************************************
* FunctionName : espconn_sendto
* Description : send data for UDP
* Parameters : espconn -- espconn to set for UDP
* psent -- data to send
* length -- length of data to send
* Returns : error
*******************************************************************************/
sint16 ICACHE_FLASH_ATTR
espconn_sendto(struct espconn *espconn, uint8 *psent, uint16 length)
{
espconn_msg *pnode = NULL;
bool value = false;
err_t error = ESPCONN_OK;
if (espconn == NULL || psent == NULL || length == 0) {
return ESPCONN_ARG;
}
/*Find the node depend on the espconn message*/
value = espconn_find_connection(espconn, &pnode);
if (value && espconn->type == ESPCONN_UDP)
return espconn_udp_sendto(pnode, psent, length);
else
return ESPCONN_ARG;
}
/****************************************************************************** /******************************************************************************
* FunctionName : espconn_send * FunctionName : espconn_send
* Description : sent data for client or server * Description : sent data for client or server
...@@ -510,51 +481,6 @@ espconn_sendto(struct espconn *espconn, uint8 *psent, uint16 length) ...@@ -510,51 +481,6 @@ espconn_sendto(struct espconn *espconn, uint8 *psent, uint16 length)
sint8 espconn_send(struct espconn *espconn, uint8 *psent, uint16 length) __attribute__((alias("espconn_sent"))); sint8 espconn_send(struct espconn *espconn, uint8 *psent, uint16 length) __attribute__((alias("espconn_sent")));
/******************************************************************************
* FunctionName : espconn_tcp_get_wnd
* Description : get the window size of simulatenously active TCP connections
* Parameters : none
* Returns : the number of TCP_MSS active TCP connections
*******************************************************************************/
uint8 ICACHE_FLASH_ATTR espconn_tcp_get_wnd(void)
{
uint8 tcp_num = 0;
tcp_num = (TCP_WND / TCP_MSS);
return tcp_num;
}
/******************************************************************************
* FunctionName : espconn_tcp_set_max_con
* Description : set the window size simulatenously active TCP connections
* Parameters : num -- the number of TCP_MSS
* Returns : ESPCONN_ARG -- Illegal argument
* ESPCONN_OK -- No error
*******************************************************************************/
sint8 ICACHE_FLASH_ATTR espconn_tcp_set_wnd(uint8 num)
{
if (num == 0 || num > linkMax)
return ESPCONN_ARG;
TCP_WND = (num * TCP_MSS);
return ESPCONN_OK;
}
/******************************************************************************
* FunctionName : espconn_tcp_get_mss
* Description : get the mss size of simulatenously active TCP connections
* Parameters : none
* Returns : the size of TCP_MSS active TCP connections
*******************************************************************************/
uint16 ICACHE_FLASH_ATTR espconn_tcp_get_mss(void)
{
uint16 tcp_num = 0;
tcp_num = TCP_MSS;
return tcp_num;
}
/****************************************************************************** /******************************************************************************
* FunctionName : espconn_tcp_get_max_con * FunctionName : espconn_tcp_get_max_con
* Description : get the number of simulatenously active TCP connections * Description : get the number of simulatenously active TCP connections
...@@ -570,81 +496,6 @@ uint8 ICACHE_FLASH_ATTR espconn_tcp_get_max_con(void) ...@@ -570,81 +496,6 @@ uint8 ICACHE_FLASH_ATTR espconn_tcp_get_max_con(void)
return tcp_num; return tcp_num;
} }
/******************************************************************************
* FunctionName : espconn_tcp_set_max_con
* Description : set the number of simulatenously active TCP connections
* Parameters : espconn -- espconn to set the connect callback
* Returns : none
*******************************************************************************/
sint8 ICACHE_FLASH_ATTR espconn_tcp_set_max_con(uint8 num)
{
if (num == 0 || num > linkMax)
return ESPCONN_ARG;
MEMP_NUM_TCP_PCB = num;
return ESPCONN_OK;
}
/******************************************************************************
* FunctionName : espconn_tcp_get_max_retran
* Description : get the Maximum number of retransmissions of data active TCP connections
* Parameters : none
* Returns : the Maximum number of retransmissions
*******************************************************************************/
uint8 ICACHE_FLASH_ATTR espconn_tcp_get_max_retran(void)
{
uint8 tcp_num = 0;
tcp_num = TCP_MAXRTX;
return tcp_num;
}
/******************************************************************************
* FunctionName : espconn_tcp_set_max_retran
* Description : set the Maximum number of retransmissions of data active TCP connections
* Parameters : num -- the Maximum number of retransmissions
* Returns : result
*******************************************************************************/
sint8 ICACHE_FLASH_ATTR espconn_tcp_set_max_retran(uint8 num)
{
if (num == 0 || num > 12)
return ESPCONN_ARG;
TCP_MAXRTX = num;
return ESPCONN_OK;
}
/******************************************************************************
* FunctionName : espconn_tcp_get_max_syn
* Description : get the Maximum number of retransmissions of SYN segments
* Parameters : none
* Returns : the Maximum number of retransmissions
*******************************************************************************/
uint8 ICACHE_FLASH_ATTR espconn_tcp_get_max_syn(void)
{
uint8 tcp_num = 0;
tcp_num = TCP_SYNMAXRTX;
return tcp_num;
}
/******************************************************************************
* FunctionName : espconn_tcp_set_max_syn
* Description : set the Maximum number of retransmissions of SYN segments
* Parameters : num -- the Maximum number of retransmissions
* Returns : result
*******************************************************************************/
sint8 ICACHE_FLASH_ATTR espconn_tcp_set_max_syn(uint8 num)
{
if (num == 0 || num > 12)
return ESPCONN_ARG;
TCP_SYNMAXRTX = num;
return ESPCONN_OK;
}
/****************************************************************************** /******************************************************************************
* FunctionName : espconn_tcp_get_max_con_allow * FunctionName : espconn_tcp_get_max_con_allow
* Description : get the count of simulatenously active connections on the server * Description : get the count of simulatenously active connections on the server
...@@ -667,29 +518,6 @@ sint8 ICACHE_FLASH_ATTR espconn_tcp_get_max_con_allow(struct espconn *espconn) ...@@ -667,29 +518,6 @@ sint8 ICACHE_FLASH_ATTR espconn_tcp_get_max_con_allow(struct espconn *espconn)
return ESPCONN_ARG; return ESPCONN_ARG;
} }
/******************************************************************************
* FunctionName : espconn_tcp_set_max_con_allow
* Description : set the count of simulatenously active connections on the server
* Parameters : espconn -- espconn to set the count
* Returns : result
*******************************************************************************/
sint8 ICACHE_FLASH_ATTR espconn_tcp_set_max_con_allow(struct espconn *espconn, uint8 num)
{
espconn_msg *pset_msg = NULL;
if ((espconn == NULL) || (num > MEMP_NUM_TCP_PCB) || (espconn->type == ESPCONN_UDP))
return ESPCONN_ARG;
pset_msg = pserver_list;
while (pset_msg != NULL){
if (pset_msg->pespconn == espconn){
pset_msg->count_opt = num;
return ESPCONN_OK;
}
pset_msg = pset_msg->pnext;
}
return ESPCONN_ARG;
}
/****************************************************************************** /******************************************************************************
* FunctionName : espconn_tcp_set_buf_count * FunctionName : espconn_tcp_set_buf_count
* Description : set the total number of espconn_buf on the unsent lists for one * Description : set the total number of espconn_buf on the unsent lists for one
...@@ -998,258 +826,6 @@ espconn_disconnect(struct espconn *espconn) ...@@ -998,258 +826,6 @@ espconn_disconnect(struct espconn *espconn)
return ESPCONN_ARG; return ESPCONN_ARG;
} }
/******************************************************************************
* FunctionName : espconn_abort
* Description : Forcely abort with host
* Parameters : espconn -- the espconn used to disconnect the connection
* Returns : none
*******************************************************************************/
sint8 ICACHE_FLASH_ATTR
espconn_abort(struct espconn *espconn)
{
espconn_msg *pnode = NULL;
bool value = false;
if (espconn == NULL) {
return ESPCONN_ARG;;
} else if (espconn ->type != ESPCONN_TCP)
return ESPCONN_ARG;
/*Find the node depend on the espconn message*/
value = espconn_find_connection(espconn, &pnode);
if (value){
/*protect for redisconnection*/
if (espconn->state == ESPCONN_CLOSE)
return ESPCONN_INPROGRESS;
espconn_tcp_disconnect(pnode,1); //1 force, 0 normal
return ESPCONN_OK;
} else
return ESPCONN_ARG;
}
/******************************************************************************
* FunctionName : espconn_get_packet_info
* Description : get the packet info with host
* Parameters : espconn -- the espconn used to disconnect the connection
* infoarg -- the packet info
* Returns : the errur code
*******************************************************************************/
sint8 ICACHE_FLASH_ATTR
espconn_get_packet_info(struct espconn *espconn, struct espconn_packet* infoarg)
{
espconn_msg *pnode = NULL;
err_t err;
bool value = false;
if (espconn == NULL || infoarg == NULL) {
return ESPCONN_ARG;;
} else if (espconn->type != ESPCONN_TCP)
return ESPCONN_ARG;
/*Find the node depend on the espconn message*/
value = espconn_find_connection(espconn, &pnode);
if (value) {
struct tcp_pcb *pcb = pnode->pcommon.pcb;
if (pcb == NULL)
return ESPCONN_ARG;
pnode->pcommon.packet_info.packseq_nxt = pcb->rcv_nxt;
pnode->pcommon.packet_info.packseqno = pcb->snd_nxt;
pnode->pcommon.packet_info.snd_buf_size = pcb->snd_buf;
pnode->pcommon.packet_info.total_queuelen = TCP_SND_QUEUELEN;
pnode->pcommon.packet_info.snd_queuelen = pnode->pcommon.packet_info.total_queuelen - pcb->snd_queuelen;
os_memcpy(infoarg,(void*)&pnode->pcommon.packet_info, sizeof(struct espconn_packet));
return ESPCONN_OK;
} else {
switch (espconn->state){
case ESPCONN_CLOSE:
os_memcpy(infoarg,(void*)&pktinfo[0], sizeof(struct espconn_packet));
err = ESPCONN_OK;
break;
case ESPCONN_NONE:
os_memcpy(infoarg,(void*)&pktinfo[1], sizeof(struct espconn_packet));
err = ESPCONN_OK;
break;
default:
err = ESPCONN_ARG;
break;
}
return err;
}
}
/******************************************************************************
* FunctionName : espconn_set_opt
* Description : set the option for connections so that we don't end up bouncing
* all connections at the same time .
* Parameters : espconn -- the espconn used to set the connection
* opt -- the option for set
* Returns : the result
*******************************************************************************/
sint8 ICACHE_FLASH_ATTR
espconn_set_opt(struct espconn *espconn, uint8 opt)
{
espconn_msg *pnode = NULL;
struct tcp_pcb *tpcb;
bool value = false;
if (espconn == NULL) {
return ESPCONN_ARG;;
} else if (espconn->type != ESPCONN_TCP)
return ESPCONN_ARG;
/*Find the node depend on the espconn message*/
value = espconn_find_connection(espconn, &pnode);
if (value) {
pnode->pcommon.espconn_opt |= opt;
tpcb = pnode->pcommon.pcb;
if (NULL == tpcb)
return ESPCONN_OK;
if (espconn_delay_disabled(pnode))
tcp_nagle_disable(tpcb);
if (espconn_keepalive_disabled(pnode))
espconn_keepalive_enable(tpcb);
return ESPCONN_OK;
} else
return ESPCONN_ARG;
}
/******************************************************************************
* FunctionName : espconn_clear_opt
* Description : clear the option for connections so that we don't end up bouncing
* all connections at the same time .
* Parameters : espconn -- the espconn used to set the connection
* opt -- the option for clear
* Returns : the result
*******************************************************************************/
sint8 ICACHE_FLASH_ATTR
espconn_clear_opt(struct espconn *espconn, uint8 opt)
{
espconn_msg *pnode = NULL;
struct tcp_pcb *tpcb;
bool value = false;
if (espconn == NULL) {
return ESPCONN_ARG;;
} else if (espconn->type != ESPCONN_TCP)
return ESPCONN_ARG;
/*Find the node depend on the espconn message*/
value = espconn_find_connection(espconn, &pnode);
if (value) {
pnode->pcommon.espconn_opt &= ~opt;
tpcb = pnode->pcommon.pcb;
if (espconn_keepalive_enabled(pnode))
espconn_keepalive_disable(tpcb);
if (NULL == tpcb)
return ESPCONN_OK;
if (espconn_delay_enabled(pnode))
tcp_nagle_enable(tpcb);
return ESPCONN_OK;
} else
return ESPCONN_ARG;
}
/******************************************************************************
* FunctionName : espconn_set_keepalive
* Description : access level value for connection so that we set the value for
* keep alive
* Parameters : espconn -- the espconn used to set the connection
* level -- the connection's level
* value -- the value of time(s)
* Returns : access port value
*******************************************************************************/
sint8 ICACHE_FLASH_ATTR espconn_set_keepalive(struct espconn *espconn, uint8 level, void* optarg)
{
espconn_msg *pnode = NULL;
bool value = false;
sint8 ret = ESPCONN_OK;
if (espconn == NULL || optarg == NULL) {
return ESPCONN_ARG;;
} else if (espconn->type != ESPCONN_TCP)
return ESPCONN_ARG;
/*Find the node depend on the espconn message*/
value = espconn_find_connection(espconn, &pnode);
if (value && espconn_keepalive_disabled(pnode)) {
struct tcp_pcb *pcb = pnode->pcommon.pcb;
if (NULL == pcb)
return ESPCONN_OK;
switch (level){
case ESPCONN_KEEPIDLE:
pcb->keep_idle = 1000 * (u32_t)(*(int*)optarg);
ret = ESPCONN_OK;
break;
case ESPCONN_KEEPINTVL:
pcb->keep_intvl = 1000 * (u32_t)(*(int*)optarg);
ret = ESPCONN_OK;
break;
case ESPCONN_KEEPCNT:
pcb->keep_cnt = (u32_t)(*(int*)optarg);
ret = ESPCONN_OK;
break;
default:
ret = ESPCONN_ARG;
break;
}
return ret;
} else
return ESPCONN_ARG;
}
/******************************************************************************
* FunctionName : espconn_get_keepalive
* Description : access level value for connection so that we get the value for
* keep alive
* Parameters : espconn -- the espconn used to get the connection
* level -- the connection's level
* Returns : access keep alive value
*******************************************************************************/
sint8 ICACHE_FLASH_ATTR espconn_get_keepalive(struct espconn *espconn, uint8 level, void *optarg)
{
espconn_msg *pnode = NULL;
bool value = false;
sint8 ret = ESPCONN_OK;
if (espconn == NULL || optarg == NULL) {
return ESPCONN_ARG;;
} else if (espconn->type != ESPCONN_TCP)
return ESPCONN_ARG;
/*Find the node depend on the espconn message*/
value = espconn_find_connection(espconn, &pnode);
if (value && espconn_keepalive_disabled(pnode)) {
struct tcp_pcb *pcb = pnode->pcommon.pcb;
if (NULL == pcb)
return ESPCONN_OK;
switch (level) {
case ESPCONN_KEEPIDLE:
*(int*)optarg = (int)(pcb->keep_idle/1000);
ret = ESPCONN_OK;
break;
case ESPCONN_KEEPINTVL:
*(int*)optarg = (int)(pcb->keep_intvl/1000);
ret = ESPCONN_OK;
break;
case ESPCONN_KEEPCNT:
*(int*)optarg = (int)(pcb->keep_cnt);
ret = ESPCONN_OK;
break;
default:
ret = ESPCONN_ARG;
break;
}
return ret;
} else
return ESPCONN_ARG;
}
/****************************************************************************** /******************************************************************************
* FunctionName : espconn_delete * FunctionName : espconn_delete
* Description : disconnect with host * Description : disconnect with host
...@@ -1309,41 +885,3 @@ espconn_port(void) ...@@ -1309,41 +885,3 @@ espconn_port(void)
return port; return port;
} }
/******************************************************************************
* FunctionName : espconn_gethostbyname
* Description : Resolve a hostname (string) into an IP address.
* Parameters : pespconn -- espconn to resolve a hostname
* hostname -- the hostname that is to be queried
* addr -- pointer to a ip_addr_t where to store the address if
* it is already cached in the dns_table (only valid if
* ESPCONN_OK is returned!)
* found -- a callback function to be called on success, failure
* or timeout (only if ERR_INPROGRESS is returned!)
* Returns : err_t return code
* - ESPCONN_OK if hostname is a valid IP address string or the host
* name is already in the local names table.
* - ESPCONN_INPROGRESS enqueue a request to be sent to the DNS server
* for resolution if no errors are present.
* - ESPCONN_ARG: dns client not initialized or invalid hostname
*******************************************************************************/
err_t ICACHE_FLASH_ATTR
espconn_gethostbyname(struct espconn *pespconn, const char *hostname, ip_addr_t *addr, dns_found_callback found)
{
return dns_gethostbyname(hostname, addr, found, pespconn);
}
/******************************************************************************
* FunctionName : espconn_dns_setserver
* Description : Initialize one of the DNS servers.
* Parameters : numdns -- the index of the DNS server to set must
* be < DNS_MAX_SERVERS = 2
* dnsserver -- IP address of the DNS server to set
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
espconn_dns_setserver(u8_t numdns, ip_addr_t *dnsserver)
{
dns_setserver(numdns,dnsserver);
}
/******************************************************************************
* Copyright 2013-2014 Espressif Systems (Wuxi)
*
* FileName: espconn_mdns.c
*
* Description: udp proto interface
*
* Modification history:
* 2014/3/31, v1.0 create this file.
*******************************************************************************/
#include "ets_sys.h"
#include "os_type.h"
#include "lwip/mdns.h"
/******************************************************************************
* FunctionName : espconn_mdns_enable
* Description : join a multicast group
* Parameters : host_ip -- the ip address of udp server
* multicast_ip -- multicast ip given by user
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
espconn_mdns_enable(void)
{
mdns_enable();
}
/******************************************************************************
* FunctionName : espconn_mdns_disable
* Description : join a multicast group
* Parameters : host_ip -- the ip address of udp server
* multicast_ip -- multicast ip given by user
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
espconn_mdns_disable(void)
{
mdns_disable();
}
/******************************************************************************
* FunctionName : espconn_mdns_set_hostname
* Description : join a multicast group
* Parameters : host_ip -- the ip address of udp server
* multicast_ip -- multicast ip given by user
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
espconn_mdns_set_hostname(char *name)
{
mdns_set_hostname(name);
}
/******************************************************************************
* FunctionName : espconn_mdns_init
* Description : join a multicast group
* Parameters : host_ip -- the ip address of udp server
* multicast_ip -- multicast ip given by user
* Returns : none
*******************************************************************************/
char* ICACHE_FLASH_ATTR
espconn_mdns_get_hostname(void)
{
return (char *)mdns_get_hostname();
}
/******************************************************************************
* FunctionName : espconn_mdns_get_servername
* Description : join a multicast group
* Parameters : info -- the info of mdns
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
espconn_mdns_set_servername(const char *name)
{
mdns_set_servername(name);
}
/******************************************************************************
* FunctionName : espconn_mdns_get_servername
* Description : join a multicast group
* Parameters : info -- the info of mdns
* Returns : none
*******************************************************************************/
char* ICACHE_FLASH_ATTR
espconn_mdns_get_servername(void)
{
return (char *)mdns_get_servername();
}
/******************************************************************************
* FunctionName : mdns_server_register
* Description : join a multicast group
* Parameters : info -- the info of mdns
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
espconn_mdns_server_register(void)
{
mdns_server_register();
}
/******************************************************************************
* FunctionName : mdns_server_register
* Description : join a multicast group
* Parameters : info -- the info of mdns
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
espconn_mdns_server_unregister(void)
{
mdns_server_unregister();
}
/******************************************************************************
* FunctionName : espconn_mdns_init
* Description : join a multicast group
* Parameters : host_ip -- the ip address of udp server
* multicast_ip -- multicast ip given by user
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
espconn_mdns_close(void)
{
mdns_close();
}
/******************************************************************************
* FunctionName : espconn_mdns_init
* Description : join a multicast group
* Parameters : host_ip -- the ip address of udp server
* multicast_ip -- multicast ip given by user
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
espconn_mdns_init(struct mdns_info *info)
{
mdns_init(info);
}
...@@ -30,7 +30,6 @@ static const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__; ...@@ -30,7 +30,6 @@ static const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__;
extern espconn_msg *plink_active; extern espconn_msg *plink_active;
extern espconn_msg *pserver_list; extern espconn_msg *pserver_list;
extern struct espconn_packet pktinfo[2];
extern struct tcp_pcb ** const tcp_pcb_lists[]; extern struct tcp_pcb ** const tcp_pcb_lists[];
os_event_t espconn_TaskQueue[espconn_TaskQueueLen]; os_event_t espconn_TaskQueue[espconn_TaskQueueLen];
...@@ -288,8 +287,6 @@ espconn_tcp_reconnect(void *arg) ...@@ -288,8 +287,6 @@ espconn_tcp_reconnect(void *arg)
os_free(perr_back); os_free(perr_back);
perr_back = NULL; perr_back = NULL;
} }
os_bzero(&pktinfo[1], sizeof(struct espconn_packet));
os_memcpy(&pktinfo[1], (void*)&precon_cb->pcommon.packet_info, sizeof(struct espconn_packet));
if (espconn && espconn->proto.tcp && espconn->proto.tcp->reconnect_callback != NULL) { if (espconn && espconn->proto.tcp && espconn->proto.tcp->reconnect_callback != NULL) {
espconn->proto.tcp->reconnect_callback(espconn, re_err); espconn->proto.tcp->reconnect_callback(espconn, re_err);
...@@ -382,8 +379,6 @@ espconn_tcp_disconnect_successful(void *arg) ...@@ -382,8 +379,6 @@ espconn_tcp_disconnect_successful(void *arg)
os_free(pdis_back); os_free(pdis_back);
pdis_back = NULL; pdis_back = NULL;
} }
os_bzero(&pktinfo[0], sizeof(struct espconn_packet));
os_memcpy(&pktinfo[0], (void*)&pdiscon_cb->pcommon.packet_info, sizeof(struct espconn_packet));
if (espconn->proto.tcp && espconn->proto.tcp->disconnect_callback != NULL) { if (espconn->proto.tcp && espconn->proto.tcp->disconnect_callback != NULL) {
espconn->proto.tcp->disconnect_callback(espconn); espconn->proto.tcp->disconnect_callback(espconn);
......
...@@ -386,40 +386,3 @@ espconn_udp_server(struct espconn *pespconn) ...@@ -386,40 +386,3 @@ espconn_udp_server(struct espconn *pespconn)
return ESPCONN_OK; return ESPCONN_OK;
} }
} }
/******************************************************************************
* FunctionName : espconn_igmp_leave
* Description : leave a multicast group
* Parameters : host_ip -- the ip address of udp server
* multicast_ip -- multicast ip given by user
* Returns : none
*******************************************************************************/
sint8 ICACHE_FLASH_ATTR
espconn_igmp_leave(ip_addr_t *host_ip, ip_addr_t *multicast_ip)
{
if (igmp_leavegroup(host_ip, multicast_ip) != ERR_OK) {
LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("udp_leave_multigrup failed!\n"));
return -1;
};
return ESPCONN_OK;
}
/******************************************************************************
* FunctionName : espconn_igmp_join
* Description : join a multicast group
* Parameters : host_ip -- the ip address of udp server
* multicast_ip -- multicast ip given by user
* Returns : none
*******************************************************************************/
sint8 ICACHE_FLASH_ATTR
espconn_igmp_join(ip_addr_t *host_ip, ip_addr_t *multicast_ip)
{
if (igmp_joingroup(host_ip, multicast_ip) != ERR_OK) {
LWIP_DEBUGF(ESPCONN_UDP_DEBUG, ("udp_join_multigrup failed!\n"));
return -1;
};
/* join to any IP address at the port */
return ESPCONN_OK;
}
This diff is collapsed.
...@@ -31,6 +31,8 @@ ...@@ -31,6 +31,8 @@
#include "ets_sys.h" #include "ets_sys.h"
#include "os_type.h" #include "os_type.h"
#include "lauxlib.h"
#ifdef MEMLEAK_DEBUG #ifdef MEMLEAK_DEBUG
static const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__; static const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__;
#endif #endif
...@@ -39,13 +41,8 @@ static const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__; ...@@ -39,13 +41,8 @@ static const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__;
#include "sys/espconn_mbedtls.h" #include "sys/espconn_mbedtls.h"
ssl_opt ssl_option = { struct ssl_options ssl_client_options = {SSL_BUFFER_SIZE, 0, false, 0, false, LUA_NOREF, LUA_NOREF};
{NULL, ESPCONN_SECURE_DEFAULT_SIZE, 0, false, 0, false},
{NULL, ESPCONN_SECURE_DEFAULT_SIZE, 0, false, 0, false},
0
};
unsigned int max_content_len = ESPCONN_SECURE_DEFAULT_SIZE;
/****************************************************************************** /******************************************************************************
* FunctionName : espconn_encry_connect * FunctionName : espconn_encry_connect
* Description : The function given as the connect * Description : The function given as the connect
...@@ -95,7 +92,7 @@ espconn_secure_connect(struct espconn *espconn) ...@@ -95,7 +92,7 @@ espconn_secure_connect(struct espconn *espconn)
} }
} }
} }
current_size = espconn_secure_get_size(ESPCONN_CLIENT); current_size = SSL_BUFFER_SIZE;
current_size += ESPCONN_SECURE_DEFAULT_HEAP; current_size += ESPCONN_SECURE_DEFAULT_HEAP;
// ssl_printf("heap_size %d %d\n", system_get_free_heap_size(), current_size); // ssl_printf("heap_size %d %d\n", system_get_free_heap_size(), current_size);
if (system_get_free_heap_size() <= current_size) if (system_get_free_heap_size() <= current_size)
...@@ -173,52 +170,6 @@ espconn_secure_sent(struct espconn *espconn, uint8 *psent, uint16 length) ...@@ -173,52 +170,6 @@ espconn_secure_sent(struct espconn *espconn, uint8 *psent, uint16 length)
sint8 espconn_secure_send(struct espconn *espconn, uint8 *psent, uint16 length) __attribute__((alias("espconn_secure_sent"))); sint8 espconn_secure_send(struct espconn *espconn, uint8 *psent, uint16 length) __attribute__((alias("espconn_secure_sent")));
sint8 ICACHE_FLASH_ATTR
espconn_secure_accept(struct espconn *espconn)
{
if (espconn == NULL || espconn ->type != ESPCONN_TCP)
return ESPCONN_ARG;
return espconn_ssl_server(espconn);
}
/******************************************************************************
* FunctionName : espconn_secure_set_size
* Description : set the buffer size for client or server
* Parameters : level -- set for client or server
* 1: client,2:server,3:client and server
* size -- buffer size
* Returns : true or false
*******************************************************************************/
bool ICACHE_FLASH_ATTR espconn_secure_set_size(uint8 level, uint16 size)
{
size = (size < 4096) ? 4096 : size;
if (level >= ESPCONN_MAX || level <= ESPCONN_IDLE)
return false;
if (size > ESPCONN_SECURE_MAX_SIZE || size < ESPCONN_SECURE_DEFAULT_SIZE)
return false;
max_content_len = size;
return true;
}
/******************************************************************************
* FunctionName : espconn_secure_get_size
* Description : get buffer size for client or server
* Parameters : level -- set for client or server
* 1: client,2:server,3:client and server
* Returns : buffer size for client or server
*******************************************************************************/
sint16 ICACHE_FLASH_ATTR espconn_secure_get_size(uint8 level)
{
if (level >= ESPCONN_MAX || level <= ESPCONN_IDLE)
return ESPCONN_ARG;
return max_content_len;
}
/****************************************************************************** /******************************************************************************
* FunctionName : espconn_secure_ca_enable * FunctionName : espconn_secure_ca_enable
* Description : enable the certificate authenticate and set the flash sector * Description : enable the certificate authenticate and set the flash sector
...@@ -230,26 +181,16 @@ sint16 ICACHE_FLASH_ATTR espconn_secure_get_size(uint8 level) ...@@ -230,26 +181,16 @@ sint16 ICACHE_FLASH_ATTR espconn_secure_get_size(uint8 level)
*******************************************************************************/ *******************************************************************************/
bool ICACHE_FLASH_ATTR espconn_secure_ca_enable(uint8 level, uint32 flash_sector ) bool ICACHE_FLASH_ATTR espconn_secure_ca_enable(uint8 level, uint32 flash_sector )
{ {
if (level >= ESPCONN_MAX || level <= ESPCONN_IDLE || flash_sector <= 0) if (flash_sector <= 0)
return false; return false;
if (level == ESPCONN_CLIENT){ if (level == ESPCONN_CLIENT){
ssl_option.client.cert_ca_sector.sector = flash_sector; ssl_client_options.cert_ca_sector.sector = flash_sector;
ssl_option.client.cert_ca_sector.flag = true; ssl_client_options.cert_ca_sector.flag = true;
} return true;
if (level == ESPCONN_SERVER){
ssl_option.server.cert_ca_sector.sector = flash_sector;
ssl_option.server.cert_ca_sector.flag = true;
} }
if (level == ESPCONN_BOTH) { return false;
ssl_option.client.cert_ca_sector.sector = flash_sector;
ssl_option.server.cert_ca_sector.sector = flash_sector;
ssl_option.client.cert_ca_sector.flag = true;
ssl_option.server.cert_ca_sector.flag = true;
}
return true;
} }
/****************************************************************************** /******************************************************************************
...@@ -261,21 +202,12 @@ bool ICACHE_FLASH_ATTR espconn_secure_ca_enable(uint8 level, uint32 flash_sector ...@@ -261,21 +202,12 @@ bool ICACHE_FLASH_ATTR espconn_secure_ca_enable(uint8 level, uint32 flash_sector
*******************************************************************************/ *******************************************************************************/
bool ICACHE_FLASH_ATTR espconn_secure_ca_disable(uint8 level) bool ICACHE_FLASH_ATTR espconn_secure_ca_disable(uint8 level)
{ {
if (level >= ESPCONN_MAX || level <= ESPCONN_IDLE) if (level == ESPCONN_CLIENT) {
return false; ssl_client_options.cert_ca_sector.flag = false;
return true;
if (level == ESPCONN_CLIENT)
ssl_option.client.cert_ca_sector.flag = false;
if (level == ESPCONN_SERVER)
ssl_option.server.cert_ca_sector.flag = false;
if (level == ESPCONN_BOTH) {
ssl_option.client.cert_ca_sector.flag = false;
ssl_option.server.cert_ca_sector.flag = false;
} }
return true; return false;
} }
/****************************************************************************** /******************************************************************************
...@@ -289,26 +221,16 @@ bool ICACHE_FLASH_ATTR espconn_secure_ca_disable(uint8 level) ...@@ -289,26 +221,16 @@ bool ICACHE_FLASH_ATTR espconn_secure_ca_disable(uint8 level)
*******************************************************************************/ *******************************************************************************/
bool ICACHE_FLASH_ATTR espconn_secure_cert_req_enable(uint8 level, uint32 flash_sector ) bool ICACHE_FLASH_ATTR espconn_secure_cert_req_enable(uint8 level, uint32 flash_sector )
{ {
if (level >= ESPCONN_MAX || level <= ESPCONN_IDLE || flash_sector <= 0) if (flash_sector <= 0)
return false; return false;
if (level == ESPCONN_CLIENT){ if (level == ESPCONN_CLIENT){
ssl_option.client.cert_req_sector.sector = flash_sector; ssl_client_options.cert_req_sector.sector = flash_sector;
ssl_option.client.cert_req_sector.flag = true; ssl_client_options.cert_req_sector.flag = true;
} return true;
if (level == ESPCONN_SERVER){
ssl_option.server.cert_req_sector.sector = flash_sector;
ssl_option.server.cert_req_sector.flag = true;
} }
if (level == ESPCONN_BOTH) { return false;
ssl_option.client.cert_req_sector.sector = flash_sector;
ssl_option.server.cert_req_sector.sector = flash_sector;
ssl_option.client.cert_req_sector.flag = true;
ssl_option.server.cert_req_sector.flag = true;
}
return true;
} }
/****************************************************************************** /******************************************************************************
...@@ -320,78 +242,12 @@ bool ICACHE_FLASH_ATTR espconn_secure_cert_req_enable(uint8 level, uint32 flash_ ...@@ -320,78 +242,12 @@ bool ICACHE_FLASH_ATTR espconn_secure_cert_req_enable(uint8 level, uint32 flash_
*******************************************************************************/ *******************************************************************************/
bool ICACHE_FLASH_ATTR espconn_secure_cert_req_disable(uint8 level) bool ICACHE_FLASH_ATTR espconn_secure_cert_req_disable(uint8 level)
{ {
if (level >= ESPCONN_MAX || level <= ESPCONN_IDLE) if (level == ESPCONN_CLIENT) {
return false; ssl_client_options.cert_req_sector.flag = false;
return true;
if (level == ESPCONN_CLIENT)
ssl_option.client.cert_req_sector.flag = false;
if (level == ESPCONN_SERVER)
ssl_option.server.cert_req_sector.flag = false;
if (level == ESPCONN_BOTH) {
ssl_option.client.cert_req_sector.flag = false;
ssl_option.server.cert_req_sector.flag = false;
} }
return true; return false;
}
/******************************************************************************
* FunctionName : espconn_secure_set_default_certificate
* Description : Load the certificates in memory depending on compile-time
* and user options.
* Parameters : certificate -- Load the certificate
* length -- Load the certificate length
* Returns : result true or false
*******************************************************************************/
bool ICACHE_FLASH_ATTR espconn_secure_set_default_certificate(const uint8* certificate, uint16 length)
{
if (certificate == NULL || length > ESPCONN_SECURE_MAX_SIZE)
return false;
return mbedtls_load_default_obj(0, ESPCONN_CERT_OWN, certificate, length);
}
/******************************************************************************
* FunctionName : espconn_secure_set_default_private_key
* Description : Load the key in memory depending on compile-time
* and user options.
* Parameters : private_key -- Load the key
* length -- Load the key length
* Returns : result true or false
*******************************************************************************/
bool ICACHE_FLASH_ATTR espconn_secure_set_default_private_key(const uint8* private_key, uint16 length)
{
if (private_key == NULL || length > ESPCONN_SECURE_MAX_SIZE)
return false;
return mbedtls_load_default_obj(0, ESPCONN_PK, private_key, length);
}
/******************************************************************************
* FunctionName : espconn_secure_delete
* Description : delete the secure server host
* Parameters : espconn -- espconn to set for client or server
* Returns : result
*******************************************************************************/
sint8 ICACHE_FLASH_ATTR espconn_secure_delete(struct espconn *espconn)
{
sint8 error = ESPCONN_OK;
error = espconn_ssl_delete(espconn);
return error;
}
bool espconn_secure_obj_load(int obj_type, uint32 flash_sector, uint16 length)
{
if (length > ESPCONN_SECURE_MAX_SIZE || length == 0)
return false;
if (obj_type != ESPCONN_PK && obj_type != ESPCONN_CERT_OWN)
return false;
return mbedtls_load_default_obj(flash_sector, obj_type, NULL, length);
} }
#endif #endif
...@@ -255,39 +255,6 @@ exit: ...@@ -255,39 +255,6 @@ exit:
return; return;
} }
static err_t do_accepted(void *arg, struct tcp_pcb *newpcb, err_t err)
{
lwIP_netconn *newconn = NULL;
lwIP_netconn *conn = arg;
err = ERR_OK;
//Avoid two TCP connections coming in simultaneously
struct tcp_pcb *pactive_pcb;
int active_pcb_num=0;
for(pactive_pcb = tcp_active_pcbs; pactive_pcb != NULL; pactive_pcb = pactive_pcb->next){
if (pactive_pcb->state == ESTABLISHED ||pactive_pcb->state == SYN_RCVD){
active_pcb_num++;
if (active_pcb_num > MEMP_NUM_TCP_PCB){
ESP_LOG("%s %d active_pcb_number:%d\n",__FILE__, __LINE__,active_pcb_num);
return ERR_MEM;
}
}
}
lwIP_REQUIRE_ACTION(conn, exit, err = ESP_ARG);
/* We have to set the callback here even though
* the new socket is unknown. conn->socket is marked as -1. */
newconn = netconn_alloc(conn->type, newpcb);
lwIP_REQUIRE_ACTION(conn, exit, err = ERR_MEM);
newconn->tcp = newpcb;
setup_tcp(newconn);
newconn->state = NETCONN_STATE_ESTABLISHED;
conn->acceptmbox = newconn;
espconn_mbedtls_parse_internal(find_socket(conn), ERR_OK);
exit:
return err;
}
sint8 netconn_delete(lwIP_netconn *conn) sint8 netconn_delete(lwIP_netconn *conn)
{ {
sint8 error = ESP_OK; sint8 error = ESP_OK;
...@@ -335,44 +302,6 @@ exit: ...@@ -335,44 +302,6 @@ exit:
return error; return error;
} }
err_t netconn_accept(lwIP_netconn *conn, lwIP_netconn **new_conn)
{
err_t error = ESP_OK;
lwIP_netconn *newconn = NULL;
lwIP_REQUIRE_ACTION(conn, exit, error = ESP_ARG);
lwIP_REQUIRE_ACTION(new_conn, exit, error = ESP_ARG);
*new_conn = NULL;
newconn = (lwIP_netconn *)conn->acceptmbox;
conn->acceptmbox = NULL;
lwIP_REQUIRE_ACTION(newconn, exit, error = ERR_CLSD);
*new_conn = newconn;
exit:
return error;
}
sint8 netconn_listen(lwIP_netconn *conn)
{
sint8 error = ESP_OK;
struct tcp_pcb *lpcb = NULL;
lwIP_REQUIRE_ACTION(conn, exit, error = ESP_ARG);
lwIP_REQUIRE_ACTION(conn->tcp, exit, error = ESP_ARG);
setup_tcp(conn);
lpcb = conn->tcp;
conn->tcp = tcp_listen(conn->tcp);
if (conn->tcp != NULL)
{
tcp_accept(conn->tcp, do_accepted);
}
else
{
conn->tcp = lpcb;
}
exit:
return error;
}
static int alloc_socket(lwIP_netconn *newconn, int accepted) static int alloc_socket(lwIP_netconn *newconn, int accepted)
{ {
int i = 0; int i = 0;
...@@ -553,56 +482,6 @@ uint32_t lwip_getul(char *str) ...@@ -553,56 +482,6 @@ uint32_t lwip_getul(char *str)
return ret; return ret;
} }
int lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
{
lwIP_sock *sock = NULL;
err_t err = ERR_OK;
lwIP_netconn *newconn = NULL;
int newsock = -1;
sock = get_socket(s);
if (!sock)
{
return -1;
}
/* wait for a new connection */
err = netconn_accept(sock->conn, &newconn);
lwIP_REQUIRE_NOERROR(err, exit);
newsock = alloc_socket(newconn, 0);
if (newsock == -1)
{
goto exit;
}
newconn->socket = newsock;
exit:
if (newsock == -1)
{
netconn_delete(newconn);
}
return newsock;
}
int lwip_listen(int s, int backlog)
{
lwIP_sock *sock = NULL;
err_t err = ERR_OK;
sock = get_socket(s);
if (!sock)
{
return -1;
}
err = netconn_listen(sock->conn);
if (err != ERR_OK)
{
ESP_LOG("lwip_connect(%d) failed, err=%d\n", s, err);
return -1;
}
ESP_LOG("lwip_connect(%d) succeeded\n", s);
return ERR_OK;
}
int lwip_recvfrom(int s, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen) int lwip_recvfrom(int s, void *mem, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen)
{ {
lwIP_sock *sock = NULL; lwIP_sock *sock = NULL;
......
...@@ -104,6 +104,7 @@ mbedtls_error: ...@@ -104,6 +104,7 @@ mbedtls_error:
return( ret ); return( ret );
} }
#if 0 // NodeMCU does not support being a TLS server
/* /*
* Create a listening socket on bind_ip:port * Create a listening socket on bind_ip:port
*/ */
...@@ -155,6 +156,7 @@ int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char ...@@ -155,6 +156,7 @@ int mbedtls_net_bind( mbedtls_net_context *ctx, const char *bind_ip, const char
return( ret ); return( ret );
} }
#endif
/* /*
* Check if the requested operation would be blocking on a non-blocking socket * Check if the requested operation would be blocking on a non-blocking socket
...@@ -167,6 +169,7 @@ static int net_would_block( const mbedtls_net_context *ctx ) ...@@ -167,6 +169,7 @@ static int net_would_block( const mbedtls_net_context *ctx )
return( 0 ); return( 0 );
} }
#if 0 // NodeMCU does not support being a TLS server
/* /*
* Accept a connection from a remote client * Accept a connection from a remote client
*/ */
...@@ -208,6 +211,7 @@ int mbedtls_net_accept( mbedtls_net_context *bind_ctx, ...@@ -208,6 +211,7 @@ int mbedtls_net_accept( mbedtls_net_context *bind_ctx,
return( ret ); return( ret );
return( 0 ); return( 0 );
} }
#endif
/* /*
* Set the socket blocking or non-blocking * Set the socket blocking or non-blocking
......
This diff is collapsed.
...@@ -20,10 +20,6 @@ ...@@ -20,10 +20,6 @@
#include "lwip/udp.h" #include "lwip/udp.h"
#include "lwip/dhcp.h" #include "lwip/dhcp.h"
#if defined(CLIENT_SSL_ENABLE) && defined(LUA_USE_MODULES_NET) && defined(LUA_USE_MODULES_TLS)
#define TLS_MODULE_PRESENT
#endif
typedef enum net_type { typedef enum net_type {
TYPE_TCP_SERVER = 0, TYPE_TCP_SERVER = 0,
TYPE_TCP_CLIENT, TYPE_TCP_CLIENT,
...@@ -291,10 +287,6 @@ static err_t net_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err) { ...@@ -291,10 +287,6 @@ static err_t net_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err) {
#pragma mark - Lua API - create #pragma mark - Lua API - create
#ifdef TLS_MODULE_PRESENT
extern int tls_socket_create( lua_State *L );
#endif
// Lua: net.createUDPSocket() // Lua: net.createUDPSocket()
int net_createUDPSocket( lua_State *L ) { int net_createUDPSocket( lua_State *L ) {
net_create(L, TYPE_UDP_SOCKET); net_create(L, TYPE_UDP_SOCKET);
...@@ -305,14 +297,7 @@ int net_createUDPSocket( lua_State *L ) { ...@@ -305,14 +297,7 @@ int net_createUDPSocket( lua_State *L ) {
int net_createServer( lua_State *L ) { int net_createServer( lua_State *L ) {
int type, timeout; int type, timeout;
type = luaL_optlong(L, 1, TYPE_TCP); timeout = luaL_optlong(L, 1, 30);
timeout = luaL_optlong(L, 2, 30);
if (type == TYPE_UDP) {
platform_print_deprecation_note("net.createServer with net.UDP type", "in next version");
return net_createUDPSocket( L );
}
if (type != TYPE_TCP) return luaL_error(L, "invalid type");
lnet_userdata *u = net_create(L, TYPE_TCP_SERVER); lnet_userdata *u = net_create(L, TYPE_TCP_SERVER);
u->server.timeout = timeout; u->server.timeout = timeout;
...@@ -321,24 +306,7 @@ int net_createServer( lua_State *L ) { ...@@ -321,24 +306,7 @@ int net_createServer( lua_State *L ) {
// Lua: net.createConnection(type, secure) // Lua: net.createConnection(type, secure)
int net_createConnection( lua_State *L ) { int net_createConnection( lua_State *L ) {
int type, secure;
type = luaL_optlong(L, 1, TYPE_TCP);
secure = luaL_optlong(L, 2, 0);
if (type == TYPE_UDP) {
platform_print_deprecation_note("net.createConnection with net.UDP type", "in next version");
return net_createUDPSocket( L );
}
if (type != TYPE_TCP) return luaL_error(L, "invalid type");
if (secure) {
platform_print_deprecation_note("net.createConnection with secure flag", "in next version");
#ifdef TLS_MODULE_PRESENT
return tls_socket_create( L );
#else
return luaL_error(L, "secure connections not enabled");
#endif
}
net_create(L, TYPE_TCP_CLIENT); net_create(L, TYPE_TCP_CLIENT);
return 1; return 1;
} }
...@@ -1039,10 +1007,6 @@ static int net_ifinfo( lua_State* L ) { ...@@ -1039,10 +1007,6 @@ static int net_ifinfo( lua_State* L ) {
#pragma mark - Tables #pragma mark - Tables
#ifdef TLS_MODULE_PRESENT
LROT_EXTERN(tls_cert);
#endif
// Module function map // Module function map
LROT_BEGIN(net_tcpserver) LROT_BEGIN(net_tcpserver)
LROT_FUNCENTRY( listen, net_listen ) LROT_FUNCENTRY( listen, net_listen )
...@@ -1096,11 +1060,6 @@ LROT_BEGIN(net) ...@@ -1096,11 +1060,6 @@ LROT_BEGIN(net)
LROT_FUNCENTRY( multicastJoin, net_multicastJoin ) LROT_FUNCENTRY( multicastJoin, net_multicastJoin )
LROT_FUNCENTRY( multicastLeave, net_multicastLeave ) LROT_FUNCENTRY( multicastLeave, net_multicastLeave )
LROT_TABENTRY( dns, net_dns ) LROT_TABENTRY( dns, net_dns )
#ifdef TLS_MODULE_PRESENT
LROT_TABENTRY( cert, tls_cert )
#endif
LROT_NUMENTRY( TCP, TYPE_TCP )
LROT_NUMENTRY( UDP, TYPE_UDP )
LROT_TABENTRY( __metatable, net ) LROT_TABENTRY( __metatable, net )
LROT_END( net, net, 0 ) LROT_END( net, net, 0 )
......
...@@ -30,11 +30,8 @@ __attribute__((section(".servercert.flash"))) unsigned char tls_server_cert_area ...@@ -30,11 +30,8 @@ __attribute__((section(".servercert.flash"))) unsigned char tls_server_cert_area
__attribute__((section(".clientcert.flash"))) unsigned char tls_client_cert_area[INTERNAL_FLASH_SECTOR_SIZE]; __attribute__((section(".clientcert.flash"))) unsigned char tls_client_cert_area[INTERNAL_FLASH_SECTOR_SIZE];
extern int tls_socket_create( lua_State *L );
LROT_EXTERN(tls_cert);
typedef struct { typedef struct {
struct espconn *pesp_conn; struct espconn pesp_conn;
int self_ref; int self_ref;
int cb_connect_ref; int cb_connect_ref;
int cb_reconnect_ref; int cb_reconnect_ref;
...@@ -47,7 +44,8 @@ typedef struct { ...@@ -47,7 +44,8 @@ typedef struct {
int tls_socket_create( lua_State *L ) { int tls_socket_create( lua_State *L ) {
tls_socket_ud *ud = (tls_socket_ud*) lua_newuserdata(L, sizeof(tls_socket_ud)); tls_socket_ud *ud = (tls_socket_ud*) lua_newuserdata(L, sizeof(tls_socket_ud));
ud->pesp_conn = NULL; bzero(&ud->pesp_conn, sizeof(ud->pesp_conn));
ud->self_ref = ud->self_ref =
ud->cb_connect_ref = ud->cb_connect_ref =
ud->cb_reconnect_ref = ud->cb_reconnect_ref =
...@@ -63,7 +61,7 @@ int tls_socket_create( lua_State *L ) { ...@@ -63,7 +61,7 @@ int tls_socket_create( lua_State *L ) {
} }
static void tls_socket_onconnect( struct espconn *pesp_conn ) { static void tls_socket_onconnect( struct espconn *pesp_conn ) {
tls_socket_ud *ud = (tls_socket_ud *)pesp_conn->reverse; tls_socket_ud *ud = (tls_socket_ud *)pesp_conn;
if (!ud || ud->self_ref == LUA_NOREF) return; if (!ud || ud->self_ref == LUA_NOREF) return;
if (ud->cb_connect_ref != LUA_NOREF) { if (ud->cb_connect_ref != LUA_NOREF) {
lua_State *L = lua_getstate(); lua_State *L = lua_getstate();
...@@ -74,14 +72,10 @@ static void tls_socket_onconnect( struct espconn *pesp_conn ) { ...@@ -74,14 +72,10 @@ static void tls_socket_onconnect( struct espconn *pesp_conn ) {
} }
static void tls_socket_cleanup(tls_socket_ud *ud) { static void tls_socket_cleanup(tls_socket_ud *ud) {
if (ud->pesp_conn) { if (ud->pesp_conn.proto.tcp) {
espconn_secure_disconnect(ud->pesp_conn); espconn_secure_disconnect(&ud->pesp_conn);
if (ud->pesp_conn->proto.tcp) { free(ud->pesp_conn.proto.tcp);
free(ud->pesp_conn->proto.tcp); ud->pesp_conn.proto.tcp = NULL;
ud->pesp_conn->proto.tcp = NULL;
}
free(ud->pesp_conn);
ud->pesp_conn = NULL;
} }
lua_State *L = lua_getstate(); lua_State *L = lua_getstate();
lua_gc(L, LUA_GCSTOP, 0); lua_gc(L, LUA_GCSTOP, 0);
...@@ -91,7 +85,7 @@ static void tls_socket_cleanup(tls_socket_ud *ud) { ...@@ -91,7 +85,7 @@ static void tls_socket_cleanup(tls_socket_ud *ud) {
} }
static void tls_socket_ondisconnect( struct espconn *pesp_conn ) { static void tls_socket_ondisconnect( struct espconn *pesp_conn ) {
tls_socket_ud *ud = (tls_socket_ud *)pesp_conn->reverse; tls_socket_ud *ud = (tls_socket_ud *)pesp_conn;
if (!ud || ud->self_ref == LUA_NOREF) return; if (!ud || ud->self_ref == LUA_NOREF) return;
tls_socket_cleanup(ud); tls_socket_cleanup(ud);
if (ud->cb_disconnect_ref != LUA_NOREF) { if (ud->cb_disconnect_ref != LUA_NOREF) {
...@@ -104,7 +98,7 @@ static void tls_socket_ondisconnect( struct espconn *pesp_conn ) { ...@@ -104,7 +98,7 @@ static void tls_socket_ondisconnect( struct espconn *pesp_conn ) {
} }
static void tls_socket_onreconnect( struct espconn *pesp_conn, s8 err ) { static void tls_socket_onreconnect( struct espconn *pesp_conn, s8 err ) {
tls_socket_ud *ud = (tls_socket_ud *)pesp_conn->reverse; tls_socket_ud *ud = (tls_socket_ud *)pesp_conn;
if (!ud || ud->self_ref == LUA_NOREF) return; if (!ud || ud->self_ref == LUA_NOREF) return;
if (ud->cb_reconnect_ref != LUA_NOREF) { if (ud->cb_reconnect_ref != LUA_NOREF) {
const char* reason = NULL; const char* reason = NULL;
...@@ -132,7 +126,7 @@ static void tls_socket_onreconnect( struct espconn *pesp_conn, s8 err ) { ...@@ -132,7 +126,7 @@ static void tls_socket_onreconnect( struct espconn *pesp_conn, s8 err ) {
} }
static void tls_socket_onrecv( struct espconn *pesp_conn, char *buf, u16 length ) { static void tls_socket_onrecv( struct espconn *pesp_conn, char *buf, u16 length ) {
tls_socket_ud *ud = (tls_socket_ud *)pesp_conn->reverse; tls_socket_ud *ud = (tls_socket_ud *)pesp_conn;
if (!ud || ud->self_ref == LUA_NOREF) return; if (!ud || ud->self_ref == LUA_NOREF) return;
if (ud->cb_receive_ref != LUA_NOREF) { if (ud->cb_receive_ref != LUA_NOREF) {
lua_State *L = lua_getstate(); lua_State *L = lua_getstate();
...@@ -144,7 +138,7 @@ static void tls_socket_onrecv( struct espconn *pesp_conn, char *buf, u16 length ...@@ -144,7 +138,7 @@ static void tls_socket_onrecv( struct espconn *pesp_conn, char *buf, u16 length
} }
static void tls_socket_onsent( struct espconn *pesp_conn ) { static void tls_socket_onsent( struct espconn *pesp_conn ) {
tls_socket_ud *ud = (tls_socket_ud *)pesp_conn->reverse; tls_socket_ud *ud = (tls_socket_ud *)pesp_conn;
if (!ud || ud->self_ref == LUA_NOREF) return; if (!ud || ud->self_ref == LUA_NOREF) return;
if (ud->cb_sent_ref != LUA_NOREF) { if (ud->cb_sent_ref != LUA_NOREF) {
lua_State *L = lua_getstate(); lua_State *L = lua_getstate();
...@@ -178,8 +172,8 @@ static void tls_socket_dns_cb( const char* domain, const ip_addr_t *ip_addr, tls ...@@ -178,8 +172,8 @@ static void tls_socket_dns_cb( const char* domain, const ip_addr_t *ip_addr, tls
ud->self_ref = LUA_NOREF; ud->self_ref = LUA_NOREF;
lua_gc(L, LUA_GCRESTART, 0); lua_gc(L, LUA_GCRESTART, 0);
} else { } else {
os_memcpy(ud->pesp_conn->proto.tcp->remote_ip, &addr.addr, 4); os_memcpy(ud->pesp_conn.proto.tcp->remote_ip, &addr.addr, 4);
espconn_secure_connect(ud->pesp_conn); espconn_secure_connect(&ud->pesp_conn);
} }
} }
...@@ -191,7 +185,7 @@ static int tls_socket_connect( lua_State *L ) { ...@@ -191,7 +185,7 @@ static int tls_socket_connect( lua_State *L ) {
return 0; return 0;
} }
if (ud->pesp_conn) { if (ud->pesp_conn.proto.tcp) {
return luaL_error(L, "already connected"); return luaL_error(L, "already connected");
} }
...@@ -205,25 +199,19 @@ static int tls_socket_connect( lua_State *L ) { ...@@ -205,25 +199,19 @@ static int tls_socket_connect( lua_State *L ) {
if (domain == NULL) if (domain == NULL)
return luaL_error(L, "invalid domain"); return luaL_error(L, "invalid domain");
ud->pesp_conn = (struct espconn*)calloc(1,sizeof(struct espconn)); ud->pesp_conn.proto.udp = NULL;
if(!ud->pesp_conn) ud->pesp_conn.proto.tcp = (esp_tcp *)calloc(1,sizeof(esp_tcp));
return luaL_error(L, "not enough memory"); if(!ud->pesp_conn.proto.tcp){
ud->pesp_conn->proto.udp = NULL;
ud->pesp_conn->proto.tcp = (esp_tcp *)calloc(1,sizeof(esp_tcp));
if(!ud->pesp_conn->proto.tcp){
free(ud->pesp_conn);
ud->pesp_conn = NULL;
return luaL_error(L, "not enough memory"); return luaL_error(L, "not enough memory");
} }
ud->pesp_conn->type = ESPCONN_TCP; ud->pesp_conn.type = ESPCONN_TCP;
ud->pesp_conn->state = ESPCONN_NONE; ud->pesp_conn.state = ESPCONN_NONE;
ud->pesp_conn->reverse = ud; ud->pesp_conn.proto.tcp->remote_port = port;
ud->pesp_conn->proto.tcp->remote_port = port; espconn_regist_connectcb(&ud->pesp_conn, (espconn_connect_callback)tls_socket_onconnect);
espconn_regist_connectcb(ud->pesp_conn, (espconn_connect_callback)tls_socket_onconnect); espconn_regist_disconcb(&ud->pesp_conn, (espconn_connect_callback)tls_socket_ondisconnect);
espconn_regist_disconcb(ud->pesp_conn, (espconn_connect_callback)tls_socket_ondisconnect); espconn_regist_reconcb(&ud->pesp_conn, (espconn_reconnect_callback)tls_socket_onreconnect);
espconn_regist_reconcb(ud->pesp_conn, (espconn_reconnect_callback)tls_socket_onreconnect); espconn_regist_recvcb(&ud->pesp_conn, (espconn_recv_callback)tls_socket_onrecv);
espconn_regist_recvcb(ud->pesp_conn, (espconn_recv_callback)tls_socket_onrecv); espconn_regist_sentcb(&ud->pesp_conn, (espconn_sent_callback)tls_socket_onsent);
espconn_regist_sentcb(ud->pesp_conn, (espconn_sent_callback)tls_socket_onsent);
if (ud->self_ref == LUA_NOREF) { if (ud->self_ref == LUA_NOREF) {
lua_pushvalue(L, 1); // copy to the top of stack lua_pushvalue(L, 1); // copy to the top of stack
...@@ -254,30 +242,29 @@ static int tls_socket_on( lua_State *L ) { ...@@ -254,30 +242,29 @@ static int tls_socket_on( lua_State *L ) {
if (method == NULL) if (method == NULL)
return luaL_error( L, "wrong arg type" ); return luaL_error( L, "wrong arg type" );
luaL_checkanyfunction(L, 3); int *cbp;
lua_pushvalue(L, 3); // copy argument (func) to the top of stack
if (strcmp(method, "connection" ) == 0) { cbp = &ud->cb_connect_ref ; }
if (strcmp(method, "connection") == 0) { else if (strcmp(method, "disconnection") == 0) { cbp = &ud->cb_disconnect_ref; }
luaL_unref(L, LUA_REGISTRYINDEX, ud->cb_connect_ref); else if (strcmp(method, "reconnection" ) == 0) { cbp = &ud->cb_reconnect_ref ; }
ud->cb_connect_ref = luaL_ref(L, LUA_REGISTRYINDEX); else if (strcmp(method, "receive" ) == 0) { cbp = &ud->cb_receive_ref ; }
} else if (strcmp(method, "disconnection") == 0) { else if (strcmp(method, "sent" ) == 0) { cbp = &ud->cb_sent_ref ; }
luaL_unref(L, LUA_REGISTRYINDEX, ud->cb_disconnect_ref); else if (strcmp(method, "dns" ) == 0) { cbp = &ud->cb_dns_ref ; }
ud->cb_disconnect_ref = luaL_ref(L, LUA_REGISTRYINDEX); else {
} else if (strcmp(method, "reconnection") == 0) {
luaL_unref(L, LUA_REGISTRYINDEX, ud->cb_reconnect_ref);
ud->cb_reconnect_ref = luaL_ref(L, LUA_REGISTRYINDEX);
} else if (strcmp(method, "receive") == 0) {
luaL_unref(L, LUA_REGISTRYINDEX, ud->cb_receive_ref);
ud->cb_receive_ref = luaL_ref(L, LUA_REGISTRYINDEX);
} else if (strcmp(method, "sent") == 0) {
luaL_unref(L, LUA_REGISTRYINDEX, ud->cb_sent_ref);
ud->cb_sent_ref = luaL_ref(L, LUA_REGISTRYINDEX);
} else if (strcmp(method, "dns") == 0) {
luaL_unref(L, LUA_REGISTRYINDEX, ud->cb_dns_ref);
ud->cb_dns_ref = luaL_ref(L, LUA_REGISTRYINDEX);
} else {
return luaL_error(L, "invalid method"); return luaL_error(L, "invalid method");
} }
if (lua_isanyfunction(L, 3)) {
lua_pushvalue(L, 3); // copy argument (func) to the top of stack
luaL_unref(L, LUA_REGISTRYINDEX, *cbp);
*cbp = luaL_ref(L, LUA_REGISTRYINDEX);
} else if (lua_isnil(L, 3)) {
luaL_unref(L, LUA_REGISTRYINDEX, *cbp);
*cbp = LUA_NOREF;
} else {
return luaL_error(L, "invalid callback function");
}
return 0; return 0;
} }
...@@ -289,7 +276,7 @@ static int tls_socket_send( lua_State *L ) { ...@@ -289,7 +276,7 @@ static int tls_socket_send( lua_State *L ) {
return 0; return 0;
} }
if(ud->pesp_conn == NULL) { if(ud->pesp_conn.proto.tcp == NULL) {
NODE_DBG("not connected"); NODE_DBG("not connected");
return 0; return 0;
} }
...@@ -300,7 +287,7 @@ static int tls_socket_send( lua_State *L ) { ...@@ -300,7 +287,7 @@ static int tls_socket_send( lua_State *L ) {
return luaL_error(L, "wrong arg type"); return luaL_error(L, "wrong arg type");
} }
espconn_secure_send(ud->pesp_conn, (void*)buf, sl); espconn_secure_send(&ud->pesp_conn, (void*)buf, sl);
return 0; return 0;
} }
...@@ -312,12 +299,12 @@ static int tls_socket_hold( lua_State *L ) { ...@@ -312,12 +299,12 @@ static int tls_socket_hold( lua_State *L ) {
return 0; return 0;
} }
if(ud->pesp_conn == NULL) { if(ud->pesp_conn.proto.tcp == NULL) {
NODE_DBG("not connected"); NODE_DBG("not connected");
return 0; return 0;
} }
espconn_recv_hold(ud->pesp_conn); espconn_recv_hold(&ud->pesp_conn);
return 0; return 0;
} }
...@@ -329,12 +316,12 @@ static int tls_socket_unhold( lua_State *L ) { ...@@ -329,12 +316,12 @@ static int tls_socket_unhold( lua_State *L ) {
return 0; return 0;
} }
if(ud->pesp_conn == NULL) { if(ud->pesp_conn.proto.tcp == NULL) {
NODE_DBG("not connected"); NODE_DBG("not connected");
return 0; return 0;
} }
espconn_recv_unhold(ud->pesp_conn); espconn_recv_unhold(&ud->pesp_conn);
return 0; return 0;
} }
...@@ -347,11 +334,11 @@ static int tls_socket_getpeer( lua_State *L ) { ...@@ -347,11 +334,11 @@ static int tls_socket_getpeer( lua_State *L ) {
return 0; return 0;
} }
if(ud->pesp_conn && ud->pesp_conn->proto.tcp->remote_port != 0){ if(ud->pesp_conn.proto.tcp && ud->pesp_conn.proto.tcp->remote_port != 0){
char temp[20] = {0}; char temp[20] = {0};
sprintf(temp, IPSTR, IP2STR( &(ud->pesp_conn->proto.tcp->remote_ip) ) ); sprintf(temp, IPSTR, IP2STR( &(ud->pesp_conn.proto.tcp->remote_ip) ) );
lua_pushstring( L, temp ); lua_pushstring( L, temp );
lua_pushinteger( L, ud->pesp_conn->proto.tcp->remote_port ); lua_pushinteger( L, ud->pesp_conn.proto.tcp->remote_port );
} else { } else {
lua_pushnil( L ); lua_pushnil( L );
lua_pushnil( L ); lua_pushnil( L );
...@@ -366,8 +353,8 @@ static int tls_socket_close( lua_State *L ) { ...@@ -366,8 +353,8 @@ static int tls_socket_close( lua_State *L ) {
return 0; return 0;
} }
if (ud->pesp_conn) { if (ud->pesp_conn.proto.tcp) {
espconn_secure_disconnect(ud->pesp_conn); espconn_secure_disconnect(&ud->pesp_conn);
} }
return 0; return 0;
...@@ -379,14 +366,10 @@ static int tls_socket_delete( lua_State *L ) { ...@@ -379,14 +366,10 @@ static int tls_socket_delete( lua_State *L ) {
NODE_DBG("userdata is nil.\n"); NODE_DBG("userdata is nil.\n");
return 0; return 0;
} }
if (ud->pesp_conn) { if (ud->pesp_conn.proto.tcp) {
espconn_secure_disconnect(ud->pesp_conn); espconn_secure_disconnect(&ud->pesp_conn);
if (ud->pesp_conn->proto.tcp) { free(ud->pesp_conn.proto.tcp);
free(ud->pesp_conn->proto.tcp); ud->pesp_conn.proto.tcp = NULL;
ud->pesp_conn->proto.tcp = NULL;
}
free(ud->pesp_conn);
ud->pesp_conn = NULL;
} }
luaL_unref(L, LUA_REGISTRYINDEX, ud->cb_connect_ref); luaL_unref(L, LUA_REGISTRYINDEX, ud->cb_connect_ref);
...@@ -529,6 +512,20 @@ static const char *fill_page_with_pem(lua_State *L, const unsigned char *flash_m ...@@ -529,6 +512,20 @@ static const char *fill_page_with_pem(lua_State *L, const unsigned char *flash_m
// Lua: tls.cert.auth(true / false) // Lua: tls.cert.auth(true / false)
static int tls_cert_auth(lua_State *L) static int tls_cert_auth(lua_State *L)
{ {
if (ssl_client_options.cert_auth_callback != LUA_NOREF) {
lua_unref(L, ssl_client_options.cert_auth_callback);
ssl_client_options.cert_auth_callback = LUA_NOREF;
}
if ((lua_type(L, 1) == LUA_TFUNCTION)
|| (lua_type(L, 1) == LUA_TLIGHTFUNCTION)) {
ssl_client_options.cert_auth_callback = lua_ref(L, 1);
lua_pushboolean(L, true);
return 1;
}
if (lua_type(L, 1) != LUA_TNIL) {
platform_print_deprecation_note("tls.cert.auth's old interface", "soon");
}
int enable; int enable;
uint32_t flash_offset = platform_flash_mapped2phys((uint32_t) &tls_client_cert_area[0]); uint32_t flash_offset = platform_flash_mapped2phys((uint32_t) &tls_client_cert_area[0]);
...@@ -557,9 +554,9 @@ static int tls_cert_auth(lua_State *L) ...@@ -557,9 +554,9 @@ static int tls_cert_auth(lua_State *L)
if (tls_client_cert_area[0] == 0x00 || tls_client_cert_area[0] == 0xff) { if (tls_client_cert_area[0] == 0x00 || tls_client_cert_area[0] == 0xff) {
return luaL_error( L, "no certificates found" ); return luaL_error( L, "no certificates found" );
} }
rc = espconn_secure_cert_req_enable(1, flash_offset / INTERNAL_FLASH_SECTOR_SIZE); rc = espconn_secure_cert_req_enable(ESPCONN_CLIENT, flash_offset / INTERNAL_FLASH_SECTOR_SIZE);
} else { } else {
rc = espconn_secure_cert_req_disable(1); rc = espconn_secure_cert_req_disable(ESPCONN_CLIENT);
} }
lua_pushboolean(L, rc); lua_pushboolean(L, rc);
...@@ -570,6 +567,20 @@ static int tls_cert_auth(lua_State *L) ...@@ -570,6 +567,20 @@ static int tls_cert_auth(lua_State *L)
// Lua: tls.cert.verify(true / false) // Lua: tls.cert.verify(true / false)
static int tls_cert_verify(lua_State *L) static int tls_cert_verify(lua_State *L)
{ {
if (ssl_client_options.cert_verify_callback != LUA_NOREF) {
lua_unref(L, ssl_client_options.cert_verify_callback);
ssl_client_options.cert_verify_callback = LUA_NOREF;
}
if ((lua_type(L, 1) == LUA_TFUNCTION)
|| (lua_type(L, 1) == LUA_TLIGHTFUNCTION)) {
ssl_client_options.cert_verify_callback = lua_ref(L, 1);
lua_pushboolean(L, true);
return 1;
}
if (lua_type(L, 1) != LUA_TNIL) {
platform_print_deprecation_note("tls.cert.verify's old interface", "soon");
}
int enable; int enable;
uint32_t flash_offset = platform_flash_mapped2phys((uint32_t) &tls_server_cert_area[0]); uint32_t flash_offset = platform_flash_mapped2phys((uint32_t) &tls_server_cert_area[0]);
...@@ -598,9 +609,9 @@ static int tls_cert_verify(lua_State *L) ...@@ -598,9 +609,9 @@ static int tls_cert_verify(lua_State *L)
if (tls_server_cert_area[0] == 0x00 || tls_server_cert_area[0] == 0xff) { if (tls_server_cert_area[0] == 0x00 || tls_server_cert_area[0] == 0xff) {
return luaL_error( L, "no certificates found" ); return luaL_error( L, "no certificates found" );
} }
rc = espconn_secure_ca_enable(1, flash_offset / INTERNAL_FLASH_SECTOR_SIZE); rc = espconn_secure_ca_enable(ESPCONN_CLIENT, flash_offset / INTERNAL_FLASH_SECTOR_SIZE);
} else { } else {
rc = espconn_secure_ca_disable(1); rc = espconn_secure_ca_disable(ESPCONN_CLIENT);
} }
lua_pushboolean(L, rc); lua_pushboolean(L, rc);
......
...@@ -824,9 +824,9 @@ void ws_connect(ws_info *ws, const char *url) { ...@@ -824,9 +824,9 @@ void ws_connect(ws_info *ws, const char *url) {
// Attempt to resolve hostname address // Attempt to resolve hostname address
ip_addr_t addr; ip_addr_t addr;
err_t result = espconn_gethostbyname(conn, hostname, &addr, dns_callback); err_t result = dns_gethostbyname(hostname, &addr, dns_callback, conn);
if (result == ESPCONN_INPROGRESS) { if (result == ERR_INPROGRESS) {
NODE_DBG("DNS pending\n"); NODE_DBG("DNS pending\n");
} else { } else {
dns_callback(hostname, &addr, conn); dns_callback(hostname, &addr, conn);
......
...@@ -135,11 +135,11 @@ Connects to the broker specified by the given host, port, and secure options. ...@@ -135,11 +135,11 @@ Connects to the broker specified by the given host, port, and secure options.
!!! attention !!! attention
Secure (`https`) connections come with quite a few limitations. Please see Secure (`mqtts`) connections come with quite a few limitations. Please see
the warnings in the [tls module](tls.md)'s documentation. the warnings in the [tls module](tls.md)'s documentation.
#### Returns #### Returns
`true` on success, `false` otherwise `nil`; use callbacks to observe the outcome.
#### Notes #### Notes
...@@ -191,17 +191,24 @@ use the *boolean* `false` or `true` instead. ...@@ -191,17 +191,24 @@ use the *boolean* `false` or `true` instead.
## mqtt.client:lwt() ## mqtt.client:lwt()
Setup [Last Will and Testament](http://www.hivemq.com/blog/mqtt-essentials-part-9-last-will-and-testament) (optional). A broker will publish a message with qos = 0, retain = 0, data = "offline" to topic "/lwt" if client does not send keepalive packet. Setup [Last Will and Testament](http://www.hivemq.com/blog/mqtt-essentials-part-9-last-will-and-testament).
As the last will is sent to the broker when connecting, `lwt()` must be called BEFORE calling `connect()`.   As the last will is sent to the broker when connecting, `lwt()` must be called BEFORE calling `connect()`.  
The broker will publish a client's last will message once he NOTICES that the connection to the client is broken. The broker will notice this when: The broker will publish a client's last will message once it notices that the connection to the client is broken; that occurs when...
 - The client fails to send a keepalive packet for as long as specified in `mqtt.Client()`  - The client fails to send a keepalive packet for as long as specified in `mqtt.Client()`
 - The tcp-connection is properly closed (without closing the mqtt-connection before)  - The TCP connection is properly closed (without closing the mqtt-connection before)
- The broker tries to send data to the client and fails to do so, because the tcp-connection is not longer open. - The broker tries to send data to the client and the TCP connection breaks.
This means if you specified 120 as keepalive timer, just turn off the client device and the broker does not send any data to the client, the last will message will be published 120s after turning off the device. This means if you specified 120 as keepalive timer, just turn off the client device and the broker does not send any data to the client, the last will message will be published 120s after turning off the device.
!!! note
There is at present a bug in the NodeMCU MQTT library that results in all disconnections
appearing as unexpected disconnects -- the MQTT-level disconnection message is not set
before the TCP connection is torn down. As a result, LWT messages will almost always be
published. See https://github.com/nodemcu/nodemcu-firmware/issues/3031
#### Syntax #### Syntax
`mqtt:lwt(topic, message[, qos[, retain]])` `mqtt:lwt(topic, message[, qos[, retain]])`
......
...@@ -10,58 +10,36 @@ Constants to be used in other functions: `net.TCP`, `net.UDP` ...@@ -10,58 +10,36 @@ Constants to be used in other functions: `net.TCP`, `net.UDP`
## net.createConnection() ## net.createConnection()
Creates a client. Creates a TCP client.
#### Syntax #### Syntax
`net.createConnection([type[, secure]])` `net.createConnection()`
#### Parameters
- `type` `net.TCP` (default) or `net.UDP`
- `secure` 1 for encrypted, 0 for plain (default)
!!! attention
This will change in upcoming releases so that `net.createConnection` will always create an unencrypted TCP connection.
There's no such thing as a UDP _connection_ because UDP is connection*less*. Thus no connection `type` parameter should be required. For UDP use [net.createUDPSocket()](#netcreateudpsocket) instead. To create *secure* connections use [tls.createConnection()](tls.md#tlscreateconnection) instead.
#### Returns #### Returns
- for `net.TCP` - net.socket sub module - net.socket sub module
- for `net.UDP` - net.udpsocket sub module
- for `net.TCP` with `secure` - tls.socket sub module
#### Example
```lua
net.createConnection(net.TCP, 0)
```
#### See also #### See also
[`net.createServer()`](#netcreateserver), [`net.createUDPSocket()`](#netcreateudpsocket), [`tls.createConnection()`](tls.md#tlscreateconnection) [`net.createServer()`](#netcreateserver), [`net.createUDPSocket()`](#netcreateudpsocket), [`tls.createConnection()`](tls.md#tlscreateconnection)
## net.createServer() ## net.createServer()
Creates a server. Creates a TCP listening socket (a server).
#### Syntax #### Syntax
`net.createServer([type[, timeout]])` `net.createServer(timeout)`
#### Parameters #### Parameters
- `type` `net.TCP` (default) or `net.UDP` - `timeout`: seconds until disconnecting an inactive client; 1~28'800 seconds, 30 sec by default.
- `timeout` for a TCP server timeout is 1~28'800 seconds, 30 sec by default (for an inactive client to be disconnected)
!!! attention
The `type` parameter will be removed in upcoming releases so that `net.createServer` will always create a TCP-based server. For UDP use [net.createUDPSocket()](#netcreateudpsocket) instead.
#### Returns #### Returns
- for `net.TCP` - net.server sub module - net.server sub module
- for `net.UDP` - net.udpsocket sub module
#### Example #### Example
```lua ```lua
net.createServer(net.TCP, 30) -- 30s timeout net.createServer(30) -- 30s timeout
``` ```
#### See also #### See also
......
...@@ -263,10 +263,19 @@ Controls the certificate verification process when the NodeMCU makes a secure co ...@@ -263,10 +263,19 @@ Controls the certificate verification process when the NodeMCU makes a secure co
`tls.cert.verify(pemdata[, pemdata])` `tls.cert.verify(pemdata[, pemdata])`
`tls.cert.verify(callback)`
#### Parameters #### Parameters
- `enable` A boolean which indicates whether verification should be enabled or not. The default at boot is `false`. - `enable` A boolean which indicates whether verification should be enabled or not. The default at boot is `false`.
- `pemdata` A string containing the CA certificate to use for verification. There can be several of these. - `pemdata` A string containing the CA certificate to use for verification. There can be several of these.
- `callback` A Lua function which returns TLS keys and certificates for use
with connections. The callback should expect one, integer argument; for
value k, the callback should return the k-th CA certificate (in either DER or
PEM form) it wishes to use to validate the remote endpoint, or `nil` if no
such CA certificate exists. If no certificates are returned, the device will
not validate the remote endpoint.
#### Returns #### Returns
`true` if it worked. `true` if it worked.
...@@ -325,6 +334,9 @@ The alternative approach is easier for development, and that is to supply the PE ...@@ -325,6 +334,9 @@ The alternative approach is easier for development, and that is to supply the PE
will store the certificate into the flash chip and turn on verification for that certificate. Subsequent boots of the ESP can then will store the certificate into the flash chip and turn on verification for that certificate. Subsequent boots of the ESP can then
use `tls.cert.verify(true)` and use the stored certificate. use `tls.cert.verify(true)` and use the stored certificate.
The `callback`-based version will override the in-flash information until the callback
is unregistered *or* one of the other call forms is made.
## tls.cert.auth() ## tls.cert.auth()
Controls the client key and certificate used when the ESP creates a TLS connection (for example, Controls the client key and certificate used when the ESP creates a TLS connection (for example,
...@@ -335,10 +347,17 @@ through `tls.createConnection` or `https` or `MQTT` connections with `secure = t ...@@ -335,10 +347,17 @@ through `tls.createConnection` or `https` or `MQTT` connections with `secure = t
`tls.cert.auth(pemdata[, pemdata])` `tls.cert.auth(pemdata[, pemdata])`
`tls.cert.auth(callback)`
#### Parameters #### Parameters
- `enable` A boolean, specifying whether subsequent TLS connections will present a client certificate. The default at boot is `false`. - `enable` A boolean, specifying whether subsequent TLS connections will present a client certificate. The default at boot is `false`.
- `pemdata` Two strings, the first containing the PEM-encoded client's certificate and the second containing the PEM-encoded client's private key. - `pemdata` Two strings, the first containing the PEM-encoded client's certificate and the second containing the PEM-encoded client's private key.
- `callback` A Lua function which returns TLS keys and certificates for use with connections.
The callback should expect one, integer argument; if that is 0, the callback should return
the device's private key. Otherwise, for argument k, the callback should return the k-th
certificate (in either DER or PEM form) in the devices' certificate chain.
#### Returns #### Returns
`true` if it worked. `true` if it worked.
...@@ -379,7 +398,8 @@ It can be supplied by passing the PEM data as a string value to `tls.cert.auth`. ...@@ -379,7 +398,8 @@ It can be supplied by passing the PEM data as a string value to `tls.cert.auth`.
will store the certificate into the flash chip and turn on proofing with that certificate. will store the certificate into the flash chip and turn on proofing with that certificate.
Subsequent boots of the ESP can then use `tls.cert.auth(true)` and use the stored certificate. Subsequent boots of the ESP can then use `tls.cert.auth(true)` and use the stored certificate.
The `callback`-based version will override the in-flash information until the callback
is unregistered *or* one of the other call forms is made.
# tls.setDebug function # tls.setDebug function
......
...@@ -381,6 +381,7 @@ stds.nodemcu_libs = { ...@@ -381,6 +381,7 @@ stds.nodemcu_libs = {
setdnsserver = empty setdnsserver = empty
} }
}, },
ifinfo = empty,
multicastJoin = empty, multicastJoin = empty,
multicastLeave = empty multicastLeave = empty
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment