summaryrefslogtreecommitdiff
path: root/cleopatre/application/libmme/src/mme.c
diff options
context:
space:
mode:
Diffstat (limited to 'cleopatre/application/libmme/src/mme.c')
-rw-r--r--cleopatre/application/libmme/src/mme.c66
1 files changed, 58 insertions, 8 deletions
diff --git a/cleopatre/application/libmme/src/mme.c b/cleopatre/application/libmme/src/mme.c
index e025beefe0..db43ff15f1 100644
--- a/cleopatre/application/libmme/src/mme.c
+++ b/cleopatre/application/libmme/src/mme.c
@@ -46,6 +46,10 @@
mme_error_t mme_init (mme_ctx_t *ctx, const mme_type_t mmtype, unsigned char *buffer, const unsigned int length)
{
+ /* protect from null pointers */
+ if (ctx == NULL || buffer == NULL)
+ return MME_ERROR_GEN;
+
ctx->buffer = buffer;
ctx->mmtype = mmtype;
ctx->length = length;
@@ -67,11 +71,14 @@ mme_error_t mme_init (mme_ctx_t *ctx, const mme_type_t mmtype, unsigned char *bu
mme_error_t mme_get_length (mme_ctx_t *ctx, unsigned int *length)
{
+ /* protect from null pointers */
+ if (ctx == NULL || length == NULL)
+ return MME_ERROR_GEN;
/* check if ctx has been inititalized */
if (ctx->status == MME_STATUS_INIT)
return MME_ERROR_NOT_INIT;
- *length = ctx->length;
+ *length = ctx->tail - ctx->head;
return MME_SUCCESS;
}
@@ -85,11 +92,14 @@ mme_error_t mme_get_length (mme_ctx_t *ctx, unsigned int *length)
mme_error_t mme_get_free_space (mme_ctx_t *ctx, unsigned int *length)
{
+ /* protect from null pointers */
+ if (ctx == NULL || length == NULL)
+ return MME_ERROR_GEN;
/* check if ctx has been inititalized */
if (ctx->status == MME_STATUS_INIT)
return MME_ERROR_NOT_INIT;
- *length = ctx->tail - ctx->head;
+ *length = ctx->length - (ctx->tail - ctx->head);
return MME_SUCCESS;
}
@@ -112,13 +122,19 @@ mme_error_t mme_push (mme_ctx_t *ctx, const void *data, unsigned int *length)
int free = 0;
int delta = 0;
+ /* protect from null pointers */
+ if (ctx == NULL || data == NULL || length == NULL)
+ return MME_ERROR_GEN;
/* check if ctx has been inititalized */
if (ctx->status == MME_STATUS_INIT)
return MME_ERROR_NOT_INIT;
free = ctx->length - (ctx->tail - ctx->head);
if (*length > free)
+ {
+ *length = free;
return MME_ERROR_SPACE;
+ }
/* make place in front, if needed */
if (*length > ctx->head)
@@ -166,16 +182,22 @@ mme_error_t mme_put (mme_ctx_t *ctx, void *data, unsigned int *length)
int free = 0;
int delta = 0;
+ /* protect from null pointers */
+ if (ctx == NULL || data == NULL || length == NULL)
+ return MME_ERROR_GEN;
/* check if ctx has been inititalized */
if (ctx->status == MME_STATUS_INIT)
return MME_ERROR_NOT_INIT;
free = ctx->length - (ctx->tail - ctx->head);
if (*length > free)
+ {
+ *length = free;
return MME_ERROR_SPACE;
+ }
/* make place after payload, if needed */
- if (*length > ctx->head)
+ if (*length > ctx->length - ctx->tail)
{
/*
* *length
@@ -216,13 +238,19 @@ mme_error_t mme_put (mme_ctx_t *ctx, void *data, unsigned int *length)
mme_error_t mme_pull (mme_ctx_t *ctx, void *data, unsigned int *length)
{
+ /* protect from null pointers */
+ if (ctx == NULL || data == NULL || length == NULL)
+ return MME_ERROR_GEN;
/* check if ctx has been inititalized */
if (ctx->status == MME_STATUS_INIT)
return MME_ERROR_NOT_INIT;
/* check if it is demanded more data than we have in payload */
if (*length > ctx->tail - ctx->head)
+ {
+ *length = ctx->tail - ctx->head;
return MME_ERROR_ENOUGH;
+ }
memcpy(data, (unsigned char *)(ctx->buffer + ctx->head), *length);
ctx->head += *length;
@@ -244,13 +272,19 @@ mme_error_t mme_pull (mme_ctx_t *ctx, void *data, unsigned int *length)
mme_error_t mme_pop (mme_ctx_t *ctx, void *data, unsigned int *length)
{
+ /* protect from null pointers */
+ if (ctx == NULL || data == NULL || length == NULL)
+ return MME_ERROR_GEN;
/* check if ctx has been inititalized */
if (ctx->status == MME_STATUS_INIT)
return MME_ERROR_NOT_INIT;
/* check if it is demanded more data than we have in payload */
if (*length > ctx->tail - ctx->head)
+ {
+ *length = ctx->tail - ctx->head;
return MME_ERROR_ENOUGH;
+ }
memcpy(data, (unsigned char *)(ctx->buffer + ctx->tail - *length), *length);
ctx->tail -= *length;
@@ -306,7 +340,11 @@ mme_error_t mme_send (mme_ctx_t *ctx, mme_send_type_t type, unsigned char *iface
int res, len;
int i;
int pkt_nfo_sz = sizeof(pkt_info);
+ mme_error_t ret;
+ /* protect from null pointers */
+ if (ctx == NULL || confirm_ctx == NULL ||iface == NULL || dest == NULL)
+ return MME_ERROR_GEN;
/* check if ctx has been inititalized */
if (ctx->status == MME_STATUS_INIT)
return MME_ERROR_NOT_INIT;
@@ -354,7 +392,6 @@ mme_error_t mme_send (mme_ctx_t *ctx, mme_send_type_t type, unsigned char *iface
* --- Create MME header ---
*/
/* Find src mac addr (can be eth0 or plc interface) */
- strcpy(ifr.ifr_name, (char *)iface);
ioctl(raw, SIOCGIFHWADDR, &ifr);
for( i = 0; i < 6; i++ )
{
@@ -496,7 +533,7 @@ mme_error_t mme_send (mme_ctx_t *ctx, mme_send_type_t type, unsigned char *iface
* by HP AV standard difference is in LSB, i.e. if REQ LSBs are ...00,
* CNF will be ...01, IND ...10 and RSP ...11 */
if ( memcmp(pkt_info.sll_addr, dest, 6) == 0 && /* we received packet from correct source */
- ((MME_t *)pkt)->mmtype == type + 1 ) /* correct response to our request */
+ ((MME_t *)pkt)->mmtype == ctx->mmtype + 1 ) /* correct response to our request */
{
break; /* good answer - go out and process the package */
}
@@ -514,7 +551,9 @@ mme_error_t mme_send (mme_ctx_t *ctx, mme_send_type_t type, unsigned char *iface
len -= sizeof(MME_t);
/* copy the packet into the receive buffer */
- mme_put( confirm_ctx, pkt, (unsigned int *)&len );
+ ret = mme_put( confirm_ctx, pkt, (unsigned int *)&len );
+ if (ret != MME_SUCCESS)
+ return ret;
} while (!last_pkt);
@@ -553,7 +592,11 @@ mme_error_t mme_listen (mme_ctx_t *ctx, unsigned char *iface, unsigned char *sou
struct timeval waittime;
int res, len;
int pkt_nfo_sz = sizeof(pkt_info);
+ mme_error_t ret;
+ /* protect from null pointers */
+ if (ctx == NULL || iface == NULL || source == NULL)
+ return MME_ERROR_GEN;
/* check if ctx has been inititalized */
if (ctx->status == MME_STATUS_INIT)
return MME_ERROR_NOT_INIT;
@@ -659,12 +702,19 @@ mme_error_t mme_listen (mme_ctx_t *ctx, unsigned char *iface, unsigned char *sou
len -= sizeof(MME_t);
/* copy the packet into the receive buffer */
- mme_put( ctx, pkt, (unsigned int *)&len );
+ ret = mme_put( ctx, pkt, (unsigned int *)&len );
+ if (ret != MME_SUCCESS)
+ return ret;
} while (!last_pkt);
/* and now whe we assembled the message, call the processing function */
- listen_cb(ctx, iface, source);
+ if (listen_cb != NULL)
+ {
+ ret = listen_cb(ctx, iface, source);
+ if (ret != MME_SUCCESS)
+ return ret;
+ }
return MME_SUCCESS;
}