Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: oldheating gps motorhome heating
Revision 145:206bf0d073c7, committed 2019-05-14
- Comitter:
- andrewboyson
- Date:
- Tue May 14 15:09:39 2019 +0000
- Parent:
- 144:6bd5c54efc7d
- Child:
- 146:0fc66d610fd6
- Commit message:
- Added a shim module between the web server and TCP to switch between http (direct) and https (tls).
Changed in this revision
--- a/tcp/http/http.c Sun May 12 17:17:49 2019 +0000
+++ b/tcp/http/http.c Tue May 14 15:09:39 2019 +0000
@@ -1,46 +1,4 @@
#include <stdbool.h>
-#include "http.h"
-#include "tcpbuf.h"
-#include "action.h"
-#include "net.h"
-#include "log.h"
-#include "led.h"
-#include "restart.h"
-#include "mstimer.h"
-
bool HttpTrace = false;
-
-//Plumb into these from your html server
-void (*HttpRequestFunction)(int size, char* pRequestStream, uint32_t positionInRequestStream, char* pState);
-int (*HttpReplyFunction )(char* pState);
-
-void HttpHandleRequest(int size, char* pRequestStream, uint32_t positionInRequestStream, char* pState)
-{
- int lastRestartPoint = RestartPoint;
- RestartPoint = FAULT_POINT_HttpHandleRequest;
-
- if (HttpTrace)
- {
- LogF("HTTP <<< %d (%u)\r\n", size, positionInRequestStream);
- }
-
- HttpRequestFunction(size, pRequestStream, positionInRequestStream, pState);
-
- RestartPoint = lastRestartPoint;
-}
-int HttpSendReply(int* pSize, char* pReplyStream, uint32_t positionInReplyStream, uint16_t mss, char* pState)
-{
- TcpBufStart(positionInReplyStream, mss, pReplyStream);
- int status = HttpReplyFunction(pState); //0: not started; +1: started; -1: wait
- if (status == 0) return TCP_APP_NOT_STARTED;
- if (status == -1) return TCP_APP_STARTED;
-
- *pSize = TcpBufLength();
-
- if (HttpTrace)
- {
- LogF("HTTP >>> %d (%d)\r\n", *pSize, positionInReplyStream);
- }
- return *pSize < mss ? TCP_APP_FINISHED : TCP_APP_STARTED;
-}
+bool HttpsTrace = false;
--- a/tcp/http/http.h Sun May 12 17:17:49 2019 +0000 +++ b/tcp/http/http.h Tue May 14 15:09:39 2019 +0000 @@ -17,7 +17,7 @@ extern void HttpAddInt32AsHex (int value); extern void HttpAddInt64AsHex (int64_t value); extern void HttpAddTm (struct tm* ptm); -extern int HttpGetLength (void); +extern bool HttpBufFilled (void); extern void HttpOk(const char* contentType, const char* cacheControl, const char* lastModifiedDate, const char* lastModifiedTime); extern char* HttpOkCookieName; @@ -34,12 +34,8 @@ extern int HttpQueryValueAsInt(char* pValue); extern void HttpQueryUnencode (char* pValue); -extern void (*HttpRequestFunction)(int size, char* pRequestStream, uint32_t positionInRequestStream, char* pState); -extern int (*HttpReplyFunction )(char* pState); - extern bool HttpTrace; -extern void HttpHandleRequest(int size, char* pRequestStream, uint32_t positionInRequestStream, char* pState); -extern int HttpSendReply (int* pSize, char* pReplyStream, uint32_t positionInReplyStream, uint16_t mss, char* pState); +extern bool HttpsTrace; extern void HttpDateFromDateTime(const char* date, const char *ptime, char* ptext); extern void HttpDateFromNow(char* pText); @@ -50,3 +46,10 @@ #define HTTP_DATE_LENGTH 30 +extern void (*HttpRequestFunction)(int size, char* pRequestStream, uint32_t positionInRequestStream, char* pState); +extern void HttpRequest (int size, char* pRequestStream, uint32_t positionInRequestStream, char* pState); +extern void HttpsRequest (int size, char* pRequestStream, uint32_t positionInRequestStream, char* pState); +extern bool (*HttpReplyPollFunction)(char* pState, bool clientFinished); +extern bool HttpReplyPoll (char* pState, bool clientFinished); +extern bool HttpsReplyPoll (char* pState, bool clientFinished); +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tcp/http/httpadd.c Tue May 14 15:09:39 2019 +0000
@@ -0,0 +1,133 @@
+#include <time.h>
+#include <stdio.h>
+
+#include "http.h"
+
+void HttpFillChar (char c, int length)
+{
+ while (length > 0)
+ {
+ HttpAddChar(c);
+ length--;
+ }
+}
+int HttpAddText (const char* text)
+{
+ const char* start = text;
+ while (*text)
+ {
+ HttpAddChar(*text);
+ text++;
+ }
+ return text - start;
+}
+int HttpAddV (char *fmt, va_list argptr)
+{
+ int size = vsnprintf(NULL, 0, fmt, argptr); //Find the size required
+ char text[size + 1]; //Allocate enough memory for the size required with an extra byte for the terminating null
+ vsprintf(text, fmt, argptr); //Fill the new buffer
+ return HttpAddText(text); //Add the text
+}
+int HttpAddF (char *fmt, ...)
+{
+ va_list argptr;
+ va_start(argptr, fmt);
+ int size = HttpAddV(fmt, argptr);
+ va_end(argptr);
+ return size;
+}
+void HttpAddData (const char* data, int length)
+{
+ while (length > 0)
+ {
+ HttpAddChar(*data);
+ data++;
+ length--;
+ }
+}
+void HttpAddStream(void (*startFunction)(void), int (*enumerateFunction)(void))
+{
+ startFunction();
+ while (true)
+ {
+ int c = enumerateFunction();
+ if (c == EOF) break;
+ HttpAddChar(c);
+ }
+}
+void HttpAddNibbleAsHex(int nibble)
+{
+ nibble &= 0x0F;
+ char c;
+ if (nibble < 0x0A) c = nibble + '0';
+ else if (nibble < 0x10) c = nibble - 0xA + 'A';
+ else c = '0';
+ HttpAddChar(c);
+}
+void HttpAddByteAsHex(int value)
+{
+ HttpAddNibbleAsHex(value >> 4);
+ HttpAddNibbleAsHex(value >> 0);
+}
+void HttpAddInt12AsHex(int value)
+{
+ HttpAddNibbleAsHex(value >> 8);
+ HttpAddNibbleAsHex(value >> 4);
+ HttpAddNibbleAsHex(value >> 0);
+}
+void HttpAddInt16AsHex(int value)
+{
+ HttpAddNibbleAsHex(value >> 12);
+ HttpAddNibbleAsHex(value >> 8);
+ HttpAddNibbleAsHex(value >> 4);
+ HttpAddNibbleAsHex(value >> 0);
+}
+void HttpAddInt32AsHex(int value)
+{
+ HttpAddNibbleAsHex(value >> 28);
+ HttpAddNibbleAsHex(value >> 24);
+ HttpAddNibbleAsHex(value >> 20);
+ HttpAddNibbleAsHex(value >> 16);
+ HttpAddNibbleAsHex(value >> 12);
+ HttpAddNibbleAsHex(value >> 8);
+ HttpAddNibbleAsHex(value >> 4);
+ HttpAddNibbleAsHex(value >> 0);
+}
+void HttpAddInt64AsHex(int64_t value)
+{
+ HttpAddNibbleAsHex(value >> 60);
+ HttpAddNibbleAsHex(value >> 56);
+ HttpAddNibbleAsHex(value >> 52);
+ HttpAddNibbleAsHex(value >> 48);
+ HttpAddNibbleAsHex(value >> 44);
+ HttpAddNibbleAsHex(value >> 40);
+ HttpAddNibbleAsHex(value >> 36);
+ HttpAddNibbleAsHex(value >> 32);
+ HttpAddNibbleAsHex(value >> 28);
+ HttpAddNibbleAsHex(value >> 24);
+ HttpAddNibbleAsHex(value >> 20);
+ HttpAddNibbleAsHex(value >> 16);
+ HttpAddNibbleAsHex(value >> 12);
+ HttpAddNibbleAsHex(value >> 8);
+ HttpAddNibbleAsHex(value >> 4);
+ HttpAddNibbleAsHex(value >> 0);
+}
+void HttpAddTm(struct tm* ptm)
+{
+ HttpAddF("%d-%02d-%02d ", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday);
+ switch(ptm->tm_wday)
+ {
+ case 0: HttpAddText("Sun"); break;
+ case 1: HttpAddText("Mon"); break;
+ case 2: HttpAddText("Tue"); break;
+ case 3: HttpAddText("Wed"); break;
+ case 4: HttpAddText("Thu"); break;
+ case 5: HttpAddText("Fri"); break;
+ case 6: HttpAddText("Sat"); break;
+ default: HttpAddText("???"); break;
+ }
+ HttpAddF(" %02d:%02d:%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
+ if (ptm->tm_isdst > 0) HttpAddText(" BST");
+ else if (ptm->tm_isdst == 0) HttpAddText(" GMT");
+ else HttpAddText(" UTC");
+}
--- a/tcp/http/httpsend.c Sun May 12 17:17:49 2019 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,94 +0,0 @@
-#include <time.h>
-#include "tcpbuf.h"
-
-int HttpGetLength() { return TcpBufLength(); }
-void HttpAddChar (char c) { TcpBufAddChar(c); }
-void HttpFillChar (char c, int length) { TcpBufFillChar(c, length); }
-int HttpAddText (const char* text) { return TcpBufAddText(text); }
-int HttpAddV (char *fmt, va_list argptr) { return TcpBufAddV(fmt, argptr); }
-int HttpAddF (char *fmt, ...)
-{
- va_list argptr;
- va_start(argptr, fmt);
- int size = TcpBufAddV(fmt, argptr);
- va_end(argptr);
- return size;
-}
-void HttpAddData (const char* data, int length) { TcpBufAddData(data, length); }
-void HttpAddStream(void (*startFunction)(void), int (*enumerateFunction)(void)) { TcpBufAddStream(startFunction, enumerateFunction);}
-void HttpAddNibbleAsHex(int nibble)
-{
- nibble &= 0x0F;
- char c;
- if (nibble < 0x0A) c = nibble + '0';
- else if (nibble < 0x10) c = nibble - 0xA + 'A';
- else c = '0';
- HttpAddChar(c);
-}
-void HttpAddByteAsHex(int value)
-{
- HttpAddNibbleAsHex(value >> 4);
- HttpAddNibbleAsHex(value >> 0);
-}
-void HttpAddInt12AsHex(int value)
-{
- HttpAddNibbleAsHex(value >> 8);
- HttpAddNibbleAsHex(value >> 4);
- HttpAddNibbleAsHex(value >> 0);
-}
-void HttpAddInt16AsHex(int value)
-{
- HttpAddNibbleAsHex(value >> 12);
- HttpAddNibbleAsHex(value >> 8);
- HttpAddNibbleAsHex(value >> 4);
- HttpAddNibbleAsHex(value >> 0);
-}
-void HttpAddInt32AsHex(int value)
-{
- HttpAddNibbleAsHex(value >> 28);
- HttpAddNibbleAsHex(value >> 24);
- HttpAddNibbleAsHex(value >> 20);
- HttpAddNibbleAsHex(value >> 16);
- HttpAddNibbleAsHex(value >> 12);
- HttpAddNibbleAsHex(value >> 8);
- HttpAddNibbleAsHex(value >> 4);
- HttpAddNibbleAsHex(value >> 0);
-}
-void HttpAddInt64AsHex(int64_t value)
-{
- HttpAddNibbleAsHex(value >> 60);
- HttpAddNibbleAsHex(value >> 56);
- HttpAddNibbleAsHex(value >> 52);
- HttpAddNibbleAsHex(value >> 48);
- HttpAddNibbleAsHex(value >> 44);
- HttpAddNibbleAsHex(value >> 40);
- HttpAddNibbleAsHex(value >> 36);
- HttpAddNibbleAsHex(value >> 32);
- HttpAddNibbleAsHex(value >> 28);
- HttpAddNibbleAsHex(value >> 24);
- HttpAddNibbleAsHex(value >> 20);
- HttpAddNibbleAsHex(value >> 16);
- HttpAddNibbleAsHex(value >> 12);
- HttpAddNibbleAsHex(value >> 8);
- HttpAddNibbleAsHex(value >> 4);
- HttpAddNibbleAsHex(value >> 0);
-}
-void HttpAddTm(struct tm* ptm)
-{
- HttpAddF("%d-%02d-%02d ", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday);
- switch(ptm->tm_wday)
- {
- case 0: HttpAddText("Sun"); break;
- case 1: HttpAddText("Mon"); break;
- case 2: HttpAddText("Tue"); break;
- case 3: HttpAddText("Wed"); break;
- case 4: HttpAddText("Thu"); break;
- case 5: HttpAddText("Fri"); break;
- case 6: HttpAddText("Sat"); break;
- default: HttpAddText("???"); break;
- }
- HttpAddF(" %02d:%02d:%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
- if (ptm->tm_isdst > 0) HttpAddText(" BST");
- else if (ptm->tm_isdst == 0) HttpAddText(" GMT");
- else HttpAddText(" UTC");
-}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tcp/http/httpshim.c Tue May 14 15:09:39 2019 +0000
@@ -0,0 +1,39 @@
+#include <stdbool.h>
+
+#include "tcp.h"
+#include "tcpbuf.h"
+#include "tls.h"
+
+//Plumb into these from your html server
+void (*HttpRequestFunction)(int size, char* pRequestStream, uint32_t positionInRequestStream, char* pState);
+bool (*HttpReplyPollFunction)(char* pState, bool clientFinished);
+
+void HttpRequest(int size, char* pRequestStream, uint32_t positionInRequestStream, char* pState)
+{
+ HttpRequestFunction(size, pRequestStream, positionInRequestStream, pState);
+}
+void HttpsRequest(int size, char* pRequestStream, uint32_t positionInRequestStream, char* pState)
+{
+ TlsRequest (size, pRequestStream, positionInRequestStream, pState);
+}
+bool HttpBufFilled(void)
+{
+ return TcpBufFilled();
+}
+
+static bool tlsRequired;
+bool HttpReplyPoll (char* pState, bool clientFinished)
+{
+ tlsRequired = false;
+ return HttpReplyPollFunction(pState, clientFinished);
+}
+bool HttpsReplyPoll(char* pState, bool clientFinished)
+{
+ tlsRequired = true;
+ return TlsReplyPoll(pState, clientFinished);
+}
+void HttpAddChar (char c)
+{
+ if (tlsRequired) TlsAddChar(c);
+ else TcpBufAddChar(c);
+}
\ No newline at end of file
--- a/tcp/https/https.c Sun May 12 17:17:49 2019 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,114 +0,0 @@
-#include <stdbool.h>
-
-#include "http.h"
-#include "tcpbuf.h"
-#include "action.h"
-#include "net.h"
-#include "log.h"
-#include "led.h"
-#include "restart.h"
-#include "mstimer.h"
-
-#define TLS_CONTENT_TYPE_ChangeCipher 20
-#define TLS_CONTENT_TYPE_Alert 21
-#define TLS_CONTENT_TYPE_Handshake 22
-#define TLS_CONTENT_TYPE_Application 23
-#define TLS_CONTENT_TYPE_Heartbeat 24
-
-#define TLS_HANDSHAKE_HelloRequest 0
-#define TLS_HANDSHAKE_ClientHello 1
-#define TLS_HANDSHAKE_ServerHello 2
-#define TLS_HANDSHAKE_NewSessionTicket 4
-#define TLS_HANDSHAKE_EncryptedExtensions 8
-#define TLS_HANDSHAKE_Certificate 11
-#define TLS_HANDSHAKE_ServerKeyExchange 12
-#define TLS_HANDSHAKE_CertificateRequest 13
-#define TLS_HANDSHAKE_ServerHelloDone 14
-#define TLS_HANDSHAKE_CertificateVerify 15
-#define TLS_HANDSHAKE_ClientKeyExchange 16
-#define TLS_HANDSHAKE_Finished 20
-
-#define DO_SERVER_HELLO 100
-
-/*static void logHandshakeType(char handshakeType)
-{
- switch (handshakeType)
- {
- case TLS_HANDSHAKE_HelloRequest: Log ("Hello request"); break;
- case TLS_HANDSHAKE_ClientHello: Log ("Client hello"); break;
- case TLS_HANDSHAKE_ServerHello: Log ("Server hello"); break;
- case TLS_HANDSHAKE_NewSessionTicket: Log ("New session ticket"); break;
- case TLS_HANDSHAKE_EncryptedExtensions: Log ("Encrypted extensions"); break;
- case TLS_HANDSHAKE_Certificate: Log ("Certificate"); break;
- case TLS_HANDSHAKE_ServerKeyExchange: Log ("Server key exchange"); break;
- case TLS_HANDSHAKE_CertificateRequest: Log ("Certificate request"); break;
- case TLS_HANDSHAKE_ServerHelloDone: Log ("Server hello done"); break;
- case TLS_HANDSHAKE_CertificateVerify: Log ("Certificate verify"); break;
- case TLS_HANDSHAKE_ClientKeyExchange: Log ("Client key exchange"); break;
- case TLS_HANDSHAKE_Finished: Log ("Finished"); break;
- default: LogF("%02hX", handshakeType); break;
- }
-}
-static void logContentType(char contentType)
-{
- switch (contentType)
- {
- case TLS_CONTENT_TYPE_ChangeCipher: Log ("Change cipher"); break;
- case TLS_CONTENT_TYPE_Alert: Log ("Alert"); break;
- case TLS_CONTENT_TYPE_Handshake: Log ("Handshake"); break;
- case TLS_CONTENT_TYPE_Application: Log ("Application"); break;
- case TLS_CONTENT_TYPE_Heartbeat: Log ("Heartbeat"); break;
- default: LogF("%02hX", contentType); break;
- }
-}
-*/
-bool HttpsTrace = true;
-
-void HttpsHandleRequest(int size, char* pRequestStream, uint32_t positionInRequestStream, void* pData)
-{
- /*
- if (HttpsTrace)
- {
- LogF("HTTPS <<< %d (%u)\r\n", size, positionInRequestStream);
- }
- //Handle request for the first packet of data received but leave todo the same after that.
- if (size == 0) return;
- if (positionInRequestStream != 0) return;
- char contentType = pRequestStream[0];
- if (HttpsTrace) Log(" content type: "); logContentType(contentType); Log("\r\n");
- switch (contentType)
- {
- case TLS_CONTENT_TYPE_Handshake:
- {
- char handshakeType = pRequestStream[5];
- if (HttpsTrace) Log(" handshake type: "); logHandshakeType(handshakeType); Log("\r\n");
- *pToDo = DO_SERVER_HELLO;
- return;
- }
- default:
- Log("HTTPS - ignoring untreated content type\r\n");
- *pToDo = 0;
- return;
- }
- //ECDHE-RSA-AES128-GCM-SHA256
- */
-}
-/*static void sendServerHello()
-{
- Log(" sending server hello\r\n");
-}
-*/
-bool HttpsSendReply(int* pSize, char* pReplyStream, uint32_t positionInReplyStream, uint16_t mss, void* pData)
-{
- /*
- TcpBufStart(positionInReplyStream, mss, pReplyStream);
- if (todo == DO_SERVER_HELLO) sendServerHello();
- *pSize = TcpBufLength();
-
- if (HttpsTrace)
- {
- LogF("HTTPS >>> %d (%d)\r\n", *pSize, positionInReplyStream);
- }
- */
- return TCP_APP_FINISHED; //0: not started; +1: started; -1: finished
-}
--- a/tcp/https/https.h Sun May 12 17:17:49 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,5 +0,0 @@ -#include <stdint.h> - -extern bool HttpsTrace; -extern void HttpsHandleRequest(int size, char* pRequestStream, uint32_t positionInRequestStream, void* pAppData); -extern int HttpsSendReply (int* pSize, char* pReplyStream, uint32_t positionInReplyStream, uint16_t mss, void* pAppData);
--- a/tcp/tcp.c Sun May 12 17:17:49 2019 +0000 +++ b/tcp/tcp.c Tue May 14 15:09:39 2019 +0000 @@ -1,4 +1,6 @@ #include <stdbool.h> +#include <stdint.h> bool TcpTrace = false; +
--- a/tcp/tcp.h Sun May 12 17:17:49 2019 +0000 +++ b/tcp/tcp.h Tue May 14 15:09:39 2019 +0000 @@ -1,3 +1,4 @@ #include <stdbool.h> +#include <stdint.h> -extern bool TcpTrace; +extern bool TcpTrace; \ No newline at end of file
--- a/tcp/tcpbuf.c Sun May 12 17:17:49 2019 +0000
+++ b/tcp/tcpbuf.c Tue May 14 15:09:39 2019 +0000
@@ -9,11 +9,6 @@
static char* pBuffer;
static char* p;
-static bool currentPositionIsInBuffer()
-{
- return currentPositionInMessage >= bufferPositionInMessage && currentPositionInMessage < bufferPositionInMessage + bufferLength;
-}
-
void TcpBufStart(uint32_t position, int mss, char *pData)
{
currentPositionInMessage = 0;
@@ -26,65 +21,16 @@
{
return p - pBuffer;
}
+bool TcpBufFilled()
+{
+ return p - pBuffer >= bufferLength;
+}
void TcpBufAddChar(char c)
{
- if (currentPositionIsInBuffer()) *p++ = c;
+ //Add character if the current position is within the buffer
+ if (currentPositionInMessage >= bufferPositionInMessage &&
+ currentPositionInMessage < bufferPositionInMessage + bufferLength) *p++ = c;
+
currentPositionInMessage++;
}
-void TcpBufFillChar(char c, int length)
-{
- while (length > 0)
- {
- if (currentPositionIsInBuffer()) *p++ = c;
- currentPositionInMessage++;
- length--;
- }
-}
-int TcpBufAddText(const char* text)
-{
- const char* start = text;
- while (*text)
- {
- if (currentPositionIsInBuffer()) *p++ = *text;
- text++;
- currentPositionInMessage++;
- }
- return text - start;
-}
-int TcpBufAddV(char *fmt, va_list argptr)
-{
- int size = vsnprintf(NULL, 0, fmt, argptr); //Find the size required
- char text[size + 1]; //Allocate enough memory for the size required with an extra byte for the terminating null
- vsprintf(text, fmt, argptr); //Fill the new buffer
- return TcpBufAddText(text); //Add the text
-}
-int TcpBufAddF(char *fmt, ...)
-{
- va_list argptr;
- va_start(argptr, fmt);
- int size = TcpBufAddV(fmt, argptr);
- va_end(argptr);
- return size;
-}
-void TcpBufAddData(const char* data, int length)
-{
- while (length > 0)
- {
- if (currentPositionIsInBuffer()) *p++ = *data;
- data++;
- currentPositionInMessage++;
- length--;
- }
-}
-void TcpBufAddStream(void (*startFunction)(void), int (*enumerateFunction)(void))
-{
- startFunction();
- while (true)
- {
- int c = enumerateFunction();
- if (c == EOF) break;
- if (currentPositionIsInBuffer()) *p++ = c;
- currentPositionInMessage++;
- }
-}
--- a/tcp/tcpbuf.h Sun May 12 17:17:49 2019 +0000 +++ b/tcp/tcpbuf.h Tue May 14 15:09:39 2019 +0000 @@ -1,18 +1,8 @@ #include <stdarg.h> #include <stdint.h> - -extern void TcpBufAddChar (char c); -extern void TcpBufFillChar (char c, int length); -extern int TcpBufAddText (const char* text); -extern int TcpBufAddV (char *fmt, va_list argptr); -extern int TcpBufAddF (char *fmt, ...); -extern void TcpBufAddData (const char* data, int length); -extern void TcpBufAddStream(void (*startFunction)(void), int (*enumerateFunction)(void)); +#include <stdbool.h> extern void TcpBufStart (uint32_t position, int mss, char *pData); +extern void TcpBufAddChar (char c); extern int TcpBufLength (void); - - -#define TCP_APP_NOT_STARTED 0 -#define TCP_APP_STARTED 1 -#define TCP_APP_FINISHED -1 \ No newline at end of file +extern bool TcpBufFilled (void); \ No newline at end of file
--- a/tcp/tcprecv.c Sun May 12 17:17:49 2019 +0000
+++ b/tcp/tcprecv.c Tue May 14 15:09:39 2019 +0000
@@ -12,7 +12,6 @@
#include "ip4.h"
#include "dhcp.h"
#include "http.h"
-#include "https.h"
#include "led.h"
#include "mstimer.h"
#include "restart.h"
@@ -58,14 +57,9 @@
char* pData = (char*)pPacket + TcpHdrSizeGet();
switch (pTcb->locPort)
{
- case 80:
- HttpHandleRequest (dataLength, pData, position, pTcb->appData);
- break;
- case 443:
- HttpsHandleRequest(dataLength, pData, position, pTcb->appData);
- break;
- default:
- break;
+ case 80: HttpRequest (dataLength, pData, position, pTcb->appData); break;
+ case 443: HttpsRequest(dataLength, pData, position, pTcb->appData); break;
+ default: break;
}
}
static int sendResetFromPacket(int* pSizeTx, void* pPacketTx, int ipType, int remArIndex, int locIpScope, int seqLengthRcvd)
--- a/tcp/tcpsend.c Sun May 12 17:17:49 2019 +0000
+++ b/tcp/tcpsend.c Tue May 14 15:09:39 2019 +0000
@@ -12,7 +12,6 @@
#include "ip4.h"
#include "dhcp.h"
#include "http.h"
-#include "https.h"
#include "led.h"
#include "tcpsend.h"
#include "mstimer.h"
@@ -46,15 +45,18 @@
return false;
}
}
-static int addApplicationData(int *pDataLength, void* pPacket, uint16_t port, uint32_t start, int mss, char* pAppData)
+static bool addApplicationData(int *pDataLength, void* pPacket, uint16_t port, uint32_t start, int mss, char* pAppState, bool clientFinished)
{
char* pData = (char*)pPacket + TcpHdrSizeGet();
+ TcpBufStart(start, mss, pData);
+ bool finished = false;
switch (port)
{
- case 80: return HttpSendReply (pDataLength, pData, start, mss, pAppData);
- case 443: return HttpsSendReply(pDataLength, pData, start, mss, pAppData);
- default: return 0; //0: not started; +1: started; -1: finished
+ case 80: finished = HttpReplyPoll (pAppState, clientFinished); break;
+ case 443: finished = HttpsReplyPoll(pAppState, clientFinished); break;
}
+ *pDataLength = TcpBufLength();
+ return finished;
}
static int preparePacket(void* pPacket, struct tcb* pTcb, int dataLength, int* pSize)
{
@@ -96,9 +98,8 @@
{
if (pTcb->bytesSentToRem - pTcb->bytesAckdByRem < pTcb->window)
{
- //0: not started; +1: started; -1: finished
- int state = addApplicationData(&dataLength, pPacket, pTcb->locPort, pTcb->bytesSentToRem - 1, pTcb->remMss, pTcb->appData);
- if (state == TCP_APP_FINISHED || state == TCP_APP_NOT_STARTED && pTcb->rcvdFin)
+ bool finished = addApplicationData(&dataLength, pPacket, pTcb->locPort, pTcb->bytesSentToRem - 1, pTcb->remMss, pTcb->appData, pTcb->rcvdFin);
+ if (finished)
{
TcpHdrFIN = true;
pTcb->sentFin = true;
@@ -145,16 +146,15 @@
case TCB_ESTABLISHED:
case TCB_CLOSE_FIN_WAIT:
- if (pTcb->appStarted)
{
- bool added = addApplicationData(&dataLength, pPacket, pTcb->locPort, seqNum - 1, pTcb->remMss, pTcb->appData);
- if (added && dataLength < pTcb->remMss)
+ bool finished = addApplicationData(&dataLength, pPacket, pTcb->locPort, seqNum - 1, pTcb->remMss, pTcb->appData, pTcb->rcvdFin);
+ if (finished)
{
TcpHdrFIN = true;
pTcb->sentFin = true;
}
+ break;
}
- break;
}
TcpHdrAckNum = pTcb->bytesAckdToRem + pTcb->remIsn; //Set up the acknowledgement field ready to send
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tcp/tls/tls.c Tue May 14 15:09:39 2019 +0000
@@ -0,0 +1,118 @@
+#include <stdbool.h>
+
+#include "http.h"
+#include "tcp.h"
+#include "tcpbuf.h"
+#include "action.h"
+#include "net.h"
+#include "log.h"
+#include "led.h"
+#include "restart.h"
+#include "mstimer.h"
+
+#define TLS_CONTENT_TYPE_ChangeCipher 20
+#define TLS_CONTENT_TYPE_Alert 21
+#define TLS_CONTENT_TYPE_Handshake 22
+#define TLS_CONTENT_TYPE_Application 23
+#define TLS_CONTENT_TYPE_Heartbeat 24
+
+#define TLS_HANDSHAKE_HelloRequest 0
+#define TLS_HANDSHAKE_ClientHello 1
+#define TLS_HANDSHAKE_ServerHello 2
+#define TLS_HANDSHAKE_NewSessionTicket 4
+#define TLS_HANDSHAKE_EncryptedExtensions 8
+#define TLS_HANDSHAKE_Certificate 11
+#define TLS_HANDSHAKE_ServerKeyExchange 12
+#define TLS_HANDSHAKE_CertificateRequest 13
+#define TLS_HANDSHAKE_ServerHelloDone 14
+#define TLS_HANDSHAKE_CertificateVerify 15
+#define TLS_HANDSHAKE_ClientKeyExchange 16
+#define TLS_HANDSHAKE_Finished 20
+
+#define DO_SERVER_HELLO 100
+
+/*static void logHandshakeType(char handshakeType)
+{
+ switch (handshakeType)
+ {
+ case TLS_HANDSHAKE_HelloRequest: Log ("Hello request"); break;
+ case TLS_HANDSHAKE_ClientHello: Log ("Client hello"); break;
+ case TLS_HANDSHAKE_ServerHello: Log ("Server hello"); break;
+ case TLS_HANDSHAKE_NewSessionTicket: Log ("New session ticket"); break;
+ case TLS_HANDSHAKE_EncryptedExtensions: Log ("Encrypted extensions"); break;
+ case TLS_HANDSHAKE_Certificate: Log ("Certificate"); break;
+ case TLS_HANDSHAKE_ServerKeyExchange: Log ("Server key exchange"); break;
+ case TLS_HANDSHAKE_CertificateRequest: Log ("Certificate request"); break;
+ case TLS_HANDSHAKE_ServerHelloDone: Log ("Server hello done"); break;
+ case TLS_HANDSHAKE_CertificateVerify: Log ("Certificate verify"); break;
+ case TLS_HANDSHAKE_ClientKeyExchange: Log ("Client key exchange"); break;
+ case TLS_HANDSHAKE_Finished: Log ("Finished"); break;
+ default: LogF("%02hX", handshakeType); break;
+ }
+}
+static void logContentType(char contentType)
+{
+ switch (contentType)
+ {
+ case TLS_CONTENT_TYPE_ChangeCipher: Log ("Change cipher"); break;
+ case TLS_CONTENT_TYPE_Alert: Log ("Alert"); break;
+ case TLS_CONTENT_TYPE_Handshake: Log ("Handshake"); break;
+ case TLS_CONTENT_TYPE_Application: Log ("Application"); break;
+ case TLS_CONTENT_TYPE_Heartbeat: Log ("Heartbeat"); break;
+ default: LogF("%02hX", contentType); break;
+ }
+}
+*/
+
+void TlsRequest(int size, char* pRequestStream, uint32_t positionInRequestStream, void* pData)
+{
+ /*
+ if (HttpsTrace)
+ {
+ LogF("HTTPS <<< %d (%u)\r\n", size, positionInRequestStream);
+ }
+ //Handle request for the first packet of data received but leave todo the same after that.
+ if (size == 0) return;
+ if (positionInRequestStream != 0) return;
+ char contentType = pRequestStream[0];
+ if (HttpsTrace) Log(" content type: "); logContentType(contentType); Log("\r\n");
+ switch (contentType)
+ {
+ case TLS_CONTENT_TYPE_Handshake:
+ {
+ char handshakeType = pRequestStream[5];
+ if (HttpsTrace) Log(" handshake type: "); logHandshakeType(handshakeType); Log("\r\n");
+ *pToDo = DO_SERVER_HELLO;
+ return;
+ }
+ default:
+ Log("HTTPS - ignoring untreated content type\r\n");
+ *pToDo = 0;
+ return;
+ }
+ //ECDHE-RSA-AES128-GCM-SHA256
+ */
+}
+/*static void sendServerHello()
+{
+ Log(" sending server hello\r\n");
+}
+*/
+bool TlsReplyPoll(char* pState, bool clientFinished)
+{
+ /*
+ TcpBufStart(positionInReplyStream, mss, pReplyStream);
+ if (todo == DO_SERVER_HELLO) sendServerHello();
+ *pSize = TcpBufLength();
+
+ if (HttpsTrace)
+ {
+ LogF("HTTPS >>> %d (%d)\r\n", *pSize, positionInReplyStream);
+ }
+ */
+ return true; //Finished
+}
+void TlsAddChar(char c)
+{
+ TcpBufAddChar(c);
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tcp/tls/tls.h Tue May 14 15:09:39 2019 +0000 @@ -0,0 +1,6 @@ +#include <stdint.h> +#include <stdbool.h> + +extern void TlsRequest (int size, char* pRequestStream, uint32_t positionInRequestStream, char* pState); +extern bool TlsReplyPoll(char* pState, bool clientFinished); +extern void TlsAddChar (char c); \ No newline at end of file