diff --git a/src/common/protocol.c b/src/common/protocol.c --- a/src/common/protocol.c +++ b/src/common/protocol.c @@ -20,6 +20,7 @@ #include "protocol.h" #include +#include #define DISTREN_REQUEST_MAGIC (0x32423434) @@ -36,13 +37,37 @@ int distren_request_new(struct distren_r newreq->magic = DISTREN_REQUEST_MAGIC; newreq->len = len; - newreq->enumsize = sizeof(enum distren_request_type); newreq->type = type; (*req) = newreq; return 0; } +int distren_request_new_fromdata(struct distren_request **req, void *data, size_t len) +{ + struct distren_request *newreq; + + if(len < sizeof(struct distren_request)) + return 1; + + if( ((struct distren_request *)data)->magic != DISTREN_REQUEST_MAGIC ) + { + fprintf(stderr, "packet doesn't match magic stuffs\n"); + return 1; + } + + newreq = malloc(sizeof(struct distren_request)); + if(!newreq) + { + fprintf(stderr, "OOM\n"); + return 1; + } + + memcpy(newreq, data, sizeof(struct distren_request)); + (*req) = newreq; + return 0; +} + int distren_request_free(struct distren_request *req) { free(req);