Skip to content

Commit

Permalink
Handle cases where there can be multiple calls to axl_init in an appl…
Browse files Browse the repository at this point in the history
…ication.

Fixes #1.
This fix also takes care for axl_init calls that are done outside TcpConnection.
  • Loading branch information
Slavey Karadzhov committed Oct 10, 2016
1 parent 259625e commit 25dbf7b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
38 changes: 34 additions & 4 deletions Sming/axtls-8266/compat/lwipr_compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@ AxlTcpDataArray axlFdArray;

/**
* Function that should be called once we are ready to use the axTLS - LWIP raw compatibility
* @param int capacity - the desired capacity
* @return int - the actual capacity
* - 0 - not enough memory
* > 0 && capacity != actual capacity - the axl_init was already called and this is the current capacity
*/
void axl_init(int capacity) {
ax_fd_init(&axlFdArray, capacity);
int axl_init(int capacity) {
if(axlFdArray.capacity > 0 && axlFdArray.data != NULL) {
// we are already initialized.
return axlFdArray.capacity;
}
return ax_fd_init(&axlFdArray, capacity);
}

/**
Expand All @@ -42,7 +50,7 @@ int axl_free(struct tcp_pcb *tcp) {
for (i = 0; i < vector->size; i++) {
if (vector->data[i].tcp == tcp) {
if(vector->data[i].tcp_pbuf != NULL) {
// pbuf_free(vector->data[i].tcp_pbuf);
// Don't free tcp_pbuf here. It should be freed manually outside of this code.
vector->data[i].tcp_pbuf = NULL;
}
vector->data[i].tcp = NULL;
Expand All @@ -54,6 +62,19 @@ int axl_free(struct tcp_pcb *tcp) {
return occurances;
}


/**
* Frees completely the FD mapping.
*/
void axl_destroy()
{
if(axlFdArray.data != NULL) {
free(axlFdArray.data);
}
axlFdArray.size = 0;
axlFdArray.capacity = 0;
}

/**
* Reads data from the SSL over TCP stream. Returns decrypted data.
* @param SSL *sslObj
Expand Down Expand Up @@ -264,11 +285,20 @@ int ax_get_file(const char *filename, uint8_t **buf) {

/*
* Utility functions
*
* @param AxlTcpDataArray the tcp data vector
* @param int capacity - the desired capacity.
* @return int - the actual capacity
*/
void ax_fd_init(AxlTcpDataArray *vector, int capacity) {
int ax_fd_init(AxlTcpDataArray *vector, int capacity) {
vector->size = 0;
vector->capacity = capacity;
vector->data = (AxlTcpData*) malloc(sizeof(AxlTcpData) * vector->capacity);
if(vector->data == NULL) {
return 0;
}

return capacity;
}

int ax_fd_append(AxlTcpDataArray *vector, struct tcp_pcb *tcp) {
Expand Down
5 changes: 3 additions & 2 deletions Sming/axtls-8266/compat/lwipr_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ typedef struct {
* High Level Functions - these are the ones that should be used directly
*/

void axl_init(int capacity);
int axl_init(int capacity);
int axl_append(struct tcp_pcb *tcp);
int axl_free(struct tcp_pcb *tcp);
void axl_destroy();

#define axl_ssl_write(A, B, C) ssl_write(A, B, C)
int axl_ssl_read(SSL *sslObj, struct tcp_pcb *tcp, struct pbuf *pin, struct pbuf **pout);
Expand All @@ -71,7 +72,7 @@ int ax_port_read(int clientfd, uint8_t *buf, int bytes_needed);
/*
* Lower Level Utility functions
*/
void ax_fd_init(AxlTcpDataArray *vector, int capacity);
int ax_fd_init(AxlTcpDataArray *vector, int capacity);
int ax_fd_append(AxlTcpDataArray *vector, struct tcp_pcb *tcp);
AxlTcpData* ax_fd_get(AxlTcpDataArray *vector, int index);
int ax_fd_getfd(AxlTcpDataArray *vector, struct tcp_pcb *tcp);
Expand Down

0 comments on commit 25dbf7b

Please sign in to comment.