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 {
buffer buf[DTLS_POOL_SZ];
int used;
byte pool[MAX_MTU*DTLS_POOL_SZ];
} DtlsPool;
@ -1503,7 +1502,7 @@ CYASSL_LOCAL void FreeArrays(CYASSL* ssl, int keep);
#ifdef CYASSL_DTLS
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 DtlsPoolSend(CYASSL*);
CYASSL_LOCAL void DtlsPoolReset(CYASSL*);

View File

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