DTLS resend allocates only enough buffer when needed

This commit is contained in:
John Safranek
2012-09-17 09:52:20 -07:00
parent e9c7cbf803
commit 40eb5b3cc5
2 changed files with 29 additions and 11 deletions

View File

@@ -1225,7 +1225,6 @@ typedef struct DtlsRecordLayerHeader {
typedef struct DtlsPool { typedef struct DtlsPool {
buffer buf[DTLS_POOL_SZ]; buffer buf[DTLS_POOL_SZ];
int used; int used;
byte pool[MAX_MTU*DTLS_POOL_SZ];
} DtlsPool; } DtlsPool;
@@ -1503,7 +1502,7 @@ CYASSL_LOCAL void FreeArrays(CYASSL* ssl, int keep);
#ifdef CYASSL_DTLS #ifdef CYASSL_DTLS
CYASSL_LOCAL int DtlsPoolInit(CYASSL*); CYASSL_LOCAL int DtlsPoolInit(CYASSL*);
CYASSL_LOCAL void DtlsPoolSave(CYASSL*, const byte*, int); CYASSL_LOCAL int DtlsPoolSave(CYASSL*, const byte*, int);
CYASSL_LOCAL int DtlsPoolTimeout(CYASSL*); CYASSL_LOCAL int DtlsPoolTimeout(CYASSL*);
CYASSL_LOCAL int DtlsPoolSend(CYASSL*); CYASSL_LOCAL int DtlsPoolSend(CYASSL*);
CYASSL_LOCAL void DtlsPoolReset(CYASSL*); CYASSL_LOCAL void DtlsPoolReset(CYASSL*);

View File

@@ -1177,8 +1177,10 @@ void SSL_ResourceFree(CYASSL* ssl)
#ifdef CYASSL_DTLS #ifdef CYASSL_DTLS
if (ssl->buffers.dtlsHandshake.buffer != NULL) if (ssl->buffers.dtlsHandshake.buffer != NULL)
XFREE(ssl->buffers.dtlsHandshake.buffer, ssl->heap, DYNAMIC_TYPE_NONE); XFREE(ssl->buffers.dtlsHandshake.buffer, ssl->heap, DYNAMIC_TYPE_NONE);
if (ssl->dtls_pool != NULL) if (ssl->dtls_pool != NULL) {
DtlsPoolReset(ssl);
XFREE(ssl->dtls_pool, ssl->heap, DYNAMIC_TYPE_NONE); XFREE(ssl->dtls_pool, ssl->heap, DYNAMIC_TYPE_NONE);
}
#endif #endif
#if defined(OPENSSL_EXTRA) || defined(GOAHEAD_WS) #if defined(OPENSSL_EXTRA) || defined(GOAHEAD_WS)
XFREE(ssl->peerCert.derCert.buffer, ssl->heap, DYNAMIC_TYPE_CERT); XFREE(ssl->peerCert.derCert.buffer, ssl->heap, DYNAMIC_TYPE_CERT);
@@ -1220,6 +1222,7 @@ void FreeHandshakeResources(CYASSL* ssl)
#ifdef CYASSL_DTLS #ifdef CYASSL_DTLS
/* DTLS_POOL */ /* DTLS_POOL */
if (ssl->options.dtls && ssl->dtls_pool != NULL) { if (ssl->options.dtls && ssl->dtls_pool != NULL) {
DtlsPoolReset(ssl);
XFREE(ssl->dtls_pool, ssl->heap, DYNAMIC_TYPE_DTLS_POOL); XFREE(ssl->dtls_pool, ssl->heap, DYNAMIC_TYPE_DTLS_POOL);
ssl->dtls_pool = NULL; ssl->dtls_pool = NULL;
} }
@@ -1262,7 +1265,7 @@ int DtlsPoolInit(CYASSL* ssl)
for (i = 0; i < DTLS_POOL_SZ; i++) { for (i = 0; i < DTLS_POOL_SZ; i++) {
pool->buf[i].length = 0; pool->buf[i].length = 0;
pool->buf[i].buffer = pool->pool + (MAX_MTU * i); pool->buf[i].buffer = NULL;
} }
pool->used = 0; pool->used = 0;
ssl->dtls_pool = pool; ssl->dtls_pool = pool;
@@ -1272,24 +1275,40 @@ int DtlsPoolInit(CYASSL* ssl)
} }
void DtlsPoolSave(CYASSL* ssl, const byte *src, int sz) int DtlsPoolSave(CYASSL* ssl, const byte *src, int sz)
{ {
DtlsPool *pool = ssl->dtls_pool; DtlsPool *pool = ssl->dtls_pool;
if (pool != NULL && pool->used < DTLS_POOL_SZ) { if (pool != NULL && pool->used < DTLS_POOL_SZ) {
buffer *buf = &pool->buf[pool->used]; buffer *pBuf = &pool->buf[pool->used];
XMEMCPY(buf->buffer, src, sz); pBuf->buffer = (byte*)XMALLOC(sz, ssl->heap, DYNAMIC_TYPE_OUT_BUFFER);
buf->length = (word32)sz; if (pBuf->buffer == NULL) {
CYASSL_MSG("DTLS Buffer Memory error");
return MEMORY_E;
}
XMEMCPY(pBuf->buffer, src, sz);
pBuf->length = (word32)sz;
pool->used++; pool->used++;
} }
return 0;
} }
void DtlsPoolReset(CYASSL* ssl) void DtlsPoolReset(CYASSL* ssl)
{ {
if (ssl->dtls_pool != NULL) { DtlsPool *pool = ssl->dtls_pool;
ssl->dtls_pool->used = 0; if (pool != NULL) {
ssl->dtls_timeout = DTLS_DEFAULT_TIMEOUT; buffer *pBuf;
int i, used;
used = pool->used;
for (i = 0, pBuf = &pool->buf[0]; i < used; i++, pBuf++) {
XFREE(pBuf->buffer, ssl->heap, DYNAMIC_TYPE_OUT_BUFFER);
pBuf->buffer = NULL;
pBuf->length = 0;
} }
pool->used = 0;
}
ssl->dtls_timeout = DTLS_DEFAULT_TIMEOUT;
} }