Commit Graph

46 Commits

Author SHA1 Message Date
Alex Villacís Lasso
37974a7f79 Use TLS context on AsyncTCP if enabled
Compile tested only.
2021-10-13 15:10:59 -05:00
Alex Villacís Lasso
a042d8e8ab API change to reduce number of NULL parameters 2021-10-13 13:08:42 -05:00
Alex Villacís Lasso
58d12224c3 Add macro definition for end-of-stream condition 2021-10-13 12:13:28 -05:00
Alex Villacís Lasso
354f535a7a Trivial code formatting fix 2021-10-13 12:03:03 -05:00
Alex Villacís Lasso
d7ea0eae72 Add methods for reading and writing data to TLS connection 2021-10-11 18:36:55 -05:00
Alex Villacís Lasso
80fc7eb489 More missing variables 2021-10-11 18:32:34 -05:00
Alex Villacís Lasso
6e5567c6f5 Remove reference to ssl_client struct 2021-10-11 18:29:27 -05:00
Alex Villacís Lasso
10eac704da Add verbose tracing to cleanup and destructor 2021-10-11 18:11:12 -05:00
Alex Villacís Lasso
081e750165 WIP: start of TLS negotiation on nonblocking socket 2021-10-11 17:57:42 -05:00
Alex Villacís Lasso
d62f9c9961 Fix typo 2021-10-07 16:29:14 -05:00
Alex Villacís Lasso
5ca0ba2d15 Renamed file for TLS context class implementation 2021-10-07 16:28:41 -05:00
Alex Villacís Lasso
b3a01893fe API skeleton for SSL/TLS client support
Completely non-functional. API sketch only.
2021-10-06 18:28:23 -05:00
Alex Villacís Lasso
8ee20adb2b Protect against AsyncClient being destroyed in the middle of processing
- Take the _asyncsock_mutex in AsyncClient destructor. This should
  handle scenarios where a previously-valid client is destroyed by
  another task while being examined by the asyncTcpSock task.
- Since the _asyncsock_mutex is released before being taken again by the
  AsyncSocketBase destructor, examined objects may be still "half-destroyed"
  (AsyncClient destructor terminated and _write_mutex invalid, but still
   waiting for lock in AsyncSocketBase destructor). However, _socket may
   now be guaranteed to  remain valid if it was valid when the
   _asyncsock_mutex was taken.
2021-09-08 12:53:22 -05:00
Alex Villacís Lasso
8607ac169b Stop accept()-ing connections if already at max connection limit
The constant CONFIG_LWIP_MAX_SOCKETS, if available, tells how many
sockets can be opened at once. If already at this limit, and an attempt
is made to accept() an additional incoming connection from a listening
socket, the accept() call will fail. Fix this by checking the socket
list and refusing to add the listening socket to the set of readable
sockets to check in the select() call. This will cause connections to
remain in the listening backlog until some other socket is closed.
2021-09-07 15:02:21 -05:00
Alex Villacís Lasso
2895bb8b84 Check for readable socket right before signaling a read timeout
In the current architecture, all socket reads proceed before all socket
polls, and the socket poll is where the read timeout check is made. If
the sum of the execution time of all write and read handlers exceeds
that of the next timeout-enabled polled socket, the code would
incorrectly close due to a "timeout", even if some readable data became
available in the meantime. Fix by checking (via select()) one last time
and resetting the timer if the socket has some readable data.
2021-09-07 14:06:12 -05:00
Alex Villacís Lasso
6f8bcef490 Fix failure to outbound-connect introduced by previous commit
Commit a5ae72c0f6 forgot to include
sockets with in-progress connections as writable, which broke all
outgoing connections. Fixed.
2021-09-06 18:02:25 -05:00
Alex Villacís Lasso
a5ae72c0f6 Properly use nonzero select() timeout for writable sockets
A socket that is "at rest" (no pending reads or writes) is by definition
"writable", and using select() to check for writability will return TRUE
 for the socket without any delay. Therefore each active connection must
 be checked for pending data to write before being added to the write
 check set. This fixes failure to yield to other tasks due to a busy
 loop.
2021-09-06 16:03:31 -05:00
Alex Villacís Lasso
511459ca3b Extract poll interval duration into macro 2021-09-06 12:54:22 -05:00
Alex Villacís Lasso
5c2fef785e Only process ACK timeout if buffer has not been fully written
Otherwise a buffer queued past _ack that has been fully written yet not
removed from the queue risks triggering an incorrect timeout event. This
has caused incomplete file downloads with ESPAsyncWebServer.
2021-09-06 12:19:28 -05:00
Alex Villacís Lasso
2234057978 First much-needed example exercising the raw client API
This is a bare-bones program that opens a basic TCP/IP socket to a
particular non-encrypted web server, on port 80, sends a hardcoded
HTTP/1.1 request, dumps the response, and should disconnect when the
remote side closes the connection. All necessary callbacks for this are
installed. The main loop() starts a connection attempt every 20 seconds,
and otherwise just outputs stars to show it is still running while the
AsyncClient does its thing.
2021-08-15 14:49:43 -05:00
Alex Villacís Lasso
fbc6d97bc1 Return false on send() if socket not connected 2021-08-15 14:32:39 -05:00
Alex Villacís Lasso
2f49e04979 Use state 1 for DNS resolution wait period
This prevents free() from being TRUE while DNS is being waited on.
2021-08-15 14:29:27 -05:00
Alex Villacís Lasso
78953656ef Eagerly flush as much data as possible from write queue
This commit implements a policy of writing as many queued buffers as the
writable socket allows, until non-writable, or the write queue is empty.
This has two objectives: 1) any allocated buffers for data copies will
be freed sooner, 2) the write output will be more responsive, fixing at
least one known delay in WiFi scanlist output in YUBOX Now.
2021-08-03 18:16:32 -05:00
Alex Villacís Lasso
01f436bbcb Display actual core assignment for asyncTcpSock task
For ease of debugging.
2021-08-02 18:57:38 -05:00
Alex Villacís Lasso
16292c981e Update RX timestamp right after assigning socket
Otherwise the socket poll callback races with the RX timeout (if
nonzero) and will immediately disconnect the socket without waiting for
it to actually finish connecting.
2021-08-02 16:47:07 -05:00
Alex Villacís Lasso
fea600a56c Do not remove callbacks on socket error either
The general rule is that at no time must callbacks be removed from the
AsyncTCP object while the object is still live. Clients expect this from
the AsyncTCP library.
2021-07-29 18:01:10 -05:00
Alex Villacís Lasso
14dad4679c Merge branch 'master' of github.com:yubox-node-org/AsyncTCPSock 2021-07-16 14:19:41 -05:00
Alex Villacís Lasso
20460d62ba Do not remove callbacks on socket close
Users of AsyncTCP expect callbacks installed on the object to remain
active on close event and do not reinstall them if the just-closed
object is reopened. Fixes failure to react to events after reopening.
2021-07-16 14:19:04 -05:00
Alex Villacís Lasso
ca0d753698 Fix for absence of lwip_*_r functions in Arduino ESP 2.0.0-alpha 2021-06-19 18:28:41 -05:00
Alex Villacís Lasso
ddbeed4375 Revert "User ESP_IDF_VERSION_MAJOR for LwIP function conditional"
This reverts commit 93063fdb86.
2021-06-19 18:07:38 -05:00
Alex Villacís Lasso
93063fdb86 User ESP_IDF_VERSION_MAJOR for LwIP function conditional
The same way it is used in the standard ESP32 Arduino WiFi library.
2021-06-18 18:54:33 -05:00
Andrii
7168825854 Fixes #2 (#3)
* fixed reorder "error-as-warning"

* reverted code reformatting
2021-03-14 11:26:03 -05:00
Alex Villacís Lasso
0ad4a10c88 Flush some writable data on AsyncClient::send() 2021-02-27 20:55:02 -05:00
Alex Villacís Lasso
f69bcbd5ad Use recursive mutex, call callbacks with mutex held
Prior to this commit, the mutex that protected the global client socket
list was non-recursive. This forced the release of the mutex before
iterating through sockets on which to call callbacks. However, releasing
the mutex allowed destructors to proceed, therefore possibly
invalidating the object pointers copied to the local list. This in turn
risked calling callbacks on already destroyed objects. Fixed by the use
of a recursive mutex that allows holding the mutex during the callback
invocation.
2021-02-17 11:42:44 -05:00
Alex Villacís Lasso
d5cf17ff65 Clear write queue BEFORE calling discard callback
The discard callback (installed via onDisconnect()) can legitimately
decide to destroy the AsyncTCP client, calling the destructors which in
turn invalidate the write mutex object. Prior to this commit, calling
_clearWriteQueue() (which takes said write mutex) risked accessing
uninitialized memory and causing memory corruption. Fixed.
2021-02-16 19:54:43 -05:00
Alex Villacís Lasso
caefb971a4 Scrub write mutex ptr immediately after destroying it
In order to detect possible memory corruption.
2021-02-16 18:27:43 -05:00
Alex Villacís Lasso
36ceb07da4 Insert forgotten return statement 2021-02-16 17:31:19 -05:00
Alex Villacís Lasso
f9cce2dd8d Fix variable shadowing 2020-12-23 16:42:00 -05:00
Alex Villacís Lasso
ddd0bc9290 Clear all callbacks on close or error 2020-12-23 16:41:29 -05:00
Alex Villacís Lasso
c35a1f2079 Plug mutex semaphore leak
Introduced by previous commit. This prevents a leak of one mutex
semaphore structure per destroyed client socket.
2020-12-23 11:08:14 -05:00
Alex Villacís Lasso
2bb8788ec1 Protect write buffer queue with a per-client mutex
This prevents a race condicion of the ::add() method modifying the queue
and being preempted by the high-priority asyncTcpSock task that in turn
flushes the same queue to the socket, therefore modifying the queue.
2020-12-21 18:21:27 -05:00
Alex Villacís Lasso
9ee3bda5ae Add abort() support if enabled in LWIP 2020-12-21 14:04:00 -05:00
Alex Villacís Lasso
de2a0da915 Implement write() and free() methods
Also comment out one prototype not yet implemented.
2020-12-21 13:48:24 -05:00
Alex Villacís Lasso
7a70bda9ce Place code under the same license as original AsyncTCP 2020-12-21 13:16:21 -05:00
Alex Villacís Lasso
9bde555be8 Arduino library files 2020-12-21 12:59:04 -05:00
Alex Villacís Lasso
5f24debb27 Initial commit 2020-12-19 17:28:53 -05:00