Skip to content

Commit

Permalink
Networking rampage and accumulated fixes (#3060)
Browse files Browse the repository at this point in the history
* espconn: remove unused espconn code, take 1

This is the easiest part of #3004 .
It removes a bunch of functions that were never called in our tree.

* espconn: De-orbit espconn_gethostbyname

Further work on #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 #3032

* espconn_secure: introduce TLS cert/key callbacks

The new feature part of #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
  • Loading branch information
nwf authored Apr 7, 2020
1 parent cb2636d commit f5aa0af
Show file tree
Hide file tree
Showing 20 changed files with 421 additions and 2,082 deletions.
9 changes: 4 additions & 5 deletions app/http/httpclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

ip_addr_t addr;
err_t error = espconn_gethostbyname( (struct espconn *) req, /* It seems we don't need a real espconn pointer here. */
hostname, &addr, http_dns_callback );
err_t error = dns_gethostbyname( hostname, &addr, http_dns_callback, req );

if ( error == ESPCONN_INPROGRESS )
if ( error == ERR_INPROGRESS )
{
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. */
http_dns_callback( hostname, &addr, req );
}
else
{
if ( error == ESPCONN_ARG )
if ( error == ERR_ARG )
{
HTTPCLIENT_ERR( "DNS arg error %s", hostname );
}else {
Expand Down
306 changes: 0 additions & 306 deletions app/include/lwip/app/espconn.h

Large diffs are not rendered by default.

53 changes: 5 additions & 48 deletions app/include/sys/espconn_mbedtls.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ typedef struct{
int record_len;
pmbedtls_session psession;
mbedtls_net_context fd;
mbedtls_net_context listen_fd;
mbedtls_ctr_drbg_context ctr_drbg;
mbedtls_ssl_context ssl;
mbedtls_ssl_config conf;
Expand All @@ -65,14 +64,11 @@ typedef enum {
ESPCONN_CERT_OWN,
ESPCONN_CERT_AUTH,
ESPCONN_PK,
ESPCONN_PASSWORD
}mbedtls_auth_type;

typedef enum {
ESPCONN_IDLE = 0,
ESPCONN_CLIENT,
ESPCONN_SERVER,
ESPCONN_BOTH,
ESPCONN_MAX
}espconn_level;

Expand All @@ -91,23 +87,14 @@ typedef struct _ssl_sector{
bool flag;
}ssl_sector;

struct ssl_packet{
uint8* pbuffer;
struct ssl_options {
uint16 buffer_size;
ssl_sector cert_ca_sector;
ssl_sector cert_req_sector;
};

typedef struct _ssl_opt {
struct ssl_packet server;
struct ssl_packet client;
uint8 type;
}ssl_opt;

typedef struct{
mbedtls_auth_type auth_type;
espconn_level auth_level;
}mbedtls_auth_info;
int cert_verify_callback;
int cert_auth_callback;
};

#define SSL_KEEP_INTVL 1
#define SSL_KEEP_CNT 5
Expand All @@ -122,13 +109,12 @@ enum {

#define ESPCONN_SECURE_MAX_SIZE 8192
#define ESPCONN_SECURE_DEFAULT_HEAP 0x3800
#define ESPCONN_SECURE_DEFAULT_SIZE SSL_BUFFER_SIZE
#define ESPCONN_HANDSHAKE_TIMEOUT 0x3C
#define ESPCONN_INVALID_TYPE 0xFFFFFFFF
#define MBEDTLS_SSL_PLAIN_ADD TCP_MSS
#define FLASH_SECTOR_SIZE 4096

extern ssl_opt ssl_option;
extern struct ssl_options ssl_client_options;

typedef struct{
uint32 parame_sec;
Expand Down Expand Up @@ -199,25 +185,6 @@ typedef enum{
} \
} 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
* Description : Initialize the client: set up a connect PCB and bind it to
Expand Down Expand Up @@ -248,16 +215,6 @@ extern void espconn_ssl_sent(void *arg, uint8 *psent, uint16 length);

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


Expand Down
6 changes: 0 additions & 6 deletions app/include/user_mbedtls.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_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:
// 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
// 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 */
#endif

//#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) */
Expand Down
Loading

0 comments on commit f5aa0af

Please sign in to comment.