Andrew Boyson / crypto

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Wed Aug 28 07:10:59 2019 +0000
Revision:
5:ee5489ee1117
Child:
6:819c17738dc2
Added connection status

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 5:ee5489ee1117 1 #include <stdlib.h>
andrewboyson 5:ee5489ee1117 2
andrewboyson 5:ee5489ee1117 3 #include "tls-connection.h"
andrewboyson 5:ee5489ee1117 4 #include "mstimer.h"
andrewboyson 5:ee5489ee1117 5
andrewboyson 5:ee5489ee1117 6 #define MAX_CONNECTIONS 4
andrewboyson 5:ee5489ee1117 7
andrewboyson 5:ee5489ee1117 8 static struct TlsConnection connections[MAX_CONNECTIONS];
andrewboyson 5:ee5489ee1117 9
andrewboyson 5:ee5489ee1117 10 static void zeroConnection(struct TlsConnection* p)
andrewboyson 5:ee5489ee1117 11 {
andrewboyson 5:ee5489ee1117 12 p->id = 0;
andrewboyson 5:ee5489ee1117 13 p->lastUsed = 0;
andrewboyson 5:ee5489ee1117 14 p->toDo = 0;
andrewboyson 5:ee5489ee1117 15 p->session = 0;
andrewboyson 5:ee5489ee1117 16 Sha256Start(&p->handshakeHash); //This just clears any information previously calculated
andrewboyson 5:ee5489ee1117 17 }
andrewboyson 5:ee5489ee1117 18
andrewboyson 5:ee5489ee1117 19 struct TlsConnection* TlsConnectionNew(int connectionId) //Never fails so never returns NULL
andrewboyson 5:ee5489ee1117 20 {
andrewboyson 5:ee5489ee1117 21 struct TlsConnection* p;
andrewboyson 5:ee5489ee1117 22
andrewboyson 5:ee5489ee1117 23 //Look for an existing connection
andrewboyson 5:ee5489ee1117 24 for (p = connections; p < connections + MAX_CONNECTIONS; p++)
andrewboyson 5:ee5489ee1117 25 {
andrewboyson 5:ee5489ee1117 26 if (p->id == connectionId) goto end;
andrewboyson 5:ee5489ee1117 27 }
andrewboyson 5:ee5489ee1117 28
andrewboyson 5:ee5489ee1117 29 //look for an empty connection
andrewboyson 5:ee5489ee1117 30 {
andrewboyson 5:ee5489ee1117 31 struct TlsConnection* pOldest = 0;
andrewboyson 5:ee5489ee1117 32 uint32_t ageOldest = 0;
andrewboyson 5:ee5489ee1117 33 for (p = connections; p < connections + MAX_CONNECTIONS; p++)
andrewboyson 5:ee5489ee1117 34 {
andrewboyson 5:ee5489ee1117 35 if (!p->id) goto end;
andrewboyson 5:ee5489ee1117 36
andrewboyson 5:ee5489ee1117 37 //Otherwise record the oldest and keep going
andrewboyson 5:ee5489ee1117 38 uint32_t age = MsTimerCount - p->lastUsed;
andrewboyson 5:ee5489ee1117 39 if (age >= ageOldest)
andrewboyson 5:ee5489ee1117 40 {
andrewboyson 5:ee5489ee1117 41 ageOldest = age;
andrewboyson 5:ee5489ee1117 42 pOldest = p;
andrewboyson 5:ee5489ee1117 43 }
andrewboyson 5:ee5489ee1117 44 }
andrewboyson 5:ee5489ee1117 45
andrewboyson 5:ee5489ee1117 46 //No empty ones found so use the oldest
andrewboyson 5:ee5489ee1117 47 p = pOldest;
andrewboyson 5:ee5489ee1117 48 }
andrewboyson 5:ee5489ee1117 49
andrewboyson 5:ee5489ee1117 50 end:
andrewboyson 5:ee5489ee1117 51 zeroConnection(p);
andrewboyson 5:ee5489ee1117 52 p->id = connectionId;
andrewboyson 5:ee5489ee1117 53 p->lastUsed = MsTimerCount;
andrewboyson 5:ee5489ee1117 54 return p;
andrewboyson 5:ee5489ee1117 55 }
andrewboyson 5:ee5489ee1117 56 struct TlsConnection* TlsConnectionOrNull(int connectionId)
andrewboyson 5:ee5489ee1117 57 {
andrewboyson 5:ee5489ee1117 58 for (struct TlsConnection* p = connections; p < connections + MAX_CONNECTIONS; p++)
andrewboyson 5:ee5489ee1117 59 {
andrewboyson 5:ee5489ee1117 60 if (p->id == connectionId)
andrewboyson 5:ee5489ee1117 61 {
andrewboyson 5:ee5489ee1117 62 p->lastUsed = MsTimerCount;
andrewboyson 5:ee5489ee1117 63 return p;
andrewboyson 5:ee5489ee1117 64 }
andrewboyson 5:ee5489ee1117 65 }
andrewboyson 5:ee5489ee1117 66 return NULL;
andrewboyson 5:ee5489ee1117 67 }
andrewboyson 5:ee5489ee1117 68 void TlsConnectionReset(int connectionId)
andrewboyson 5:ee5489ee1117 69 {
andrewboyson 5:ee5489ee1117 70 for (struct TlsConnection* p = connections; p < connections + MAX_CONNECTIONS; p++)
andrewboyson 5:ee5489ee1117 71 {
andrewboyson 5:ee5489ee1117 72 if (p->id == connectionId) zeroConnection(p);
andrewboyson 5:ee5489ee1117 73 }
andrewboyson 5:ee5489ee1117 74 }