From 1885184932474907f67208e12902fc7a64818112 Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Tue, 22 Aug 2017 22:08:56 +0100 Subject: update netbyte lib, add rpc lib --- extlibs/nbrpc.c | 232 +++++++++++++++++++++++++++++++++++++++++++++++++++++ extlibs/nbrpc.h | 32 ++++++++ extlibs/netbytes.c | 70 +++++++++++----- extlibs/netbytes.h | 42 +++++----- 4 files changed, 336 insertions(+), 40 deletions(-) create mode 100644 extlibs/nbrpc.c create mode 100644 extlibs/nbrpc.h (limited to 'extlibs') diff --git a/extlibs/nbrpc.c b/extlibs/nbrpc.c new file mode 100644 index 0000000..2e8d6c3 --- /dev/null +++ b/extlibs/nbrpc.c @@ -0,0 +1,232 @@ +#include "nbrpc.h" + +extern char* alloc_new_str(char *); +extern char *alloc_new_str_s(char *, size_t); + +rpc_request* rpc_req_new(char *method, char *params, int id) +{ + rpc_request *ret = NULL; + + ret = malloc(sizeof(rpc_request)); + if (!ret) + { + return NULL; + } + ret->id = id; + ret->method = alloc_new_str(method); + ret->params = alloc_new_str(params); + return ret; +} + + +rpc_response* rpc_resp_new(char *result, char *error, int id) +{ + rpc_response *ret = NULL; + + ret = malloc(sizeof(rpc_response)); + if (!ret) + { + return NULL; + } + + PRINT("resp->id %d\n", id); + ret->id = id; + ret->result = alloc_new_str(result); + ret->error = alloc_new_str(error); + + return ret; +} + + +int rpc_req_free(rpc_request *req) +{ + if (req) + { + free(req->method); + free(req->params); + free(req); + req = NULL; + } + +} + + +int rpc_resp_free(rpc_response *resp) +{ + if (resp) + { + free(resp->result); + free(resp->error); + free(resp); + resp = NULL; + } +} + + +int rpc_req_marsh( rpc_request *req, netbyte_store **nb_req) +{ + netbyte_store *nb = NULL; + nb_u32 nb_id; + nb_u8arr nb_method, nb_params; + + nb = calloc(1,sizeof(netbyte_store)); + nb_init(nb); + + nb_u8arr_create(&nb_method, strlen(req->method), req->method); + nb_add_u8arr(nb, &nb_method); + + nb_u8arr_create(&nb_params, strlen(req->params), req->params); + nb_add_u8arr(nb, &nb_params); + + nb_u32_create(&nb_id, req->id); + PRINT("%d\n", req->id); + PRINT("%d\n", nb_id.val); + nb_add_u32(nb, &nb_id); + + nb_print(nb); + nb_print(nb); + + *nb_req = nb; + return 0; +} + + +int rpc_resp_marsh( rpc_response *resp, netbyte_store **nb_resp) +{ + int eret; + netbyte_store *nb = NULL; + nb_u32 nb_id; + nb_u8arr nb_result, nb_error; + + nb = calloc(1,sizeof(netbyte_store)); + nb_init(nb); + + eret = nb_u8arr_create(&nb_result, strlen(resp->result), resp->result); + eret |= nb_add_u8arr(nb, &nb_result); + + eret = nb_u8arr_create(&nb_error, strlen(resp->error), resp->error); + eret |= nb_add_u8arr(nb, &nb_error); + + eret = nb_u32_create(&nb_id, resp->id); + PRINT("%d\n", resp->id); + PRINT("%d\n", nb_id.val); + eret |= nb_add_u32(nb, &nb_id); + if (!eret) + { + ENL(); + } + + //nb_print(nb); + + *nb_resp = nb; + return 0; +} + + +int rpc_req_unmarsh( netbyte_store *nb_req, rpc_request **req) +{ + __nb_type *type=NULL; + rpc_request *__req=NULL; + + if (!nb_req) + { + return -1; + } + + if (!req) + { + return -1; + } + + __req = calloc(1,sizeof(rpc_request)); + + if (0 == nb_val(nb_req, 0, &type)) + if (type->type == NBT_U8ARRAY) + { + nb_u8arr *u8arr = (nb_u8arr *)type->nb_val; + __req->method = alloc_new_str_s(u8arr->val, u8arr->len); + } else + { + ENL(); + } + + if (0 == nb_val(nb_req, 1, &type)) + if (type->type == NBT_U8ARRAY) + { + nb_u8arr *u8arr = (nb_u8arr *)type->nb_val; + __req->params = alloc_new_str_s(u8arr->val, u8arr->len); + } else + { + ENL(); + } + + if (0 == nb_val(nb_req, 2, &type)) + if (type->type == NBT_U32) + { + PNL(); + nb_u32 *u32 = (nb_u32 *)type->nb_val; + __req->id = u32->val; + } else + { + ENL(); + } + + *req = __req; + + return 0; +} + + +int rpc_resp_unmarsh(netbyte_store *nb_resp, rpc_response **resp) +{ + __nb_type *type=NULL; + rpc_response *__resp=NULL; + + if (!nb_resp) + { + return -1; + } + + if (!resp) + { + return -1; + } + + __resp = calloc(1,sizeof(rpc_response)); + + if (0 == nb_val(nb_resp, 0, &type)) + if (type->type == NBT_U8ARRAY) + { + nb_u8arr *u8arr = (nb_u8arr *)type->nb_val; + __resp->result = alloc_new_str_s(u8arr->val, u8arr->len); + } else + { + ENL(); + } + + if (0 == nb_val(nb_resp, 1, &type)) + if (type->type == NBT_U8ARRAY) + { + nb_u8arr *u8arr = (nb_u8arr *)type->nb_val; + __resp->error = alloc_new_str_s(u8arr->val, u8arr->len); + } else + { + ENL(); + } + + if (0 == nb_val(nb_resp, 2, &type)) + if (type->type == NBT_U32) + { + PNL(); + nb_u32 *u32 = (nb_u32 *)type->nb_val; + __resp->id = u32->val; + PRINT("%d\n",u32->val) + } else + { + ENL(); + } + + *resp = __resp; + + return 0; +} \ No newline at end of file diff --git a/extlibs/nbrpc.h b/extlibs/nbrpc.h new file mode 100644 index 0000000..3108702 --- /dev/null +++ b/extlibs/nbrpc.h @@ -0,0 +1,32 @@ +#ifndef __NBRPC_H +#define __NBRPC_H + +#include + +#include "mq_cmd.h" +#include "mq_ntf.h" + +typedef struct rpc_request +{ + char *method; + char *params; + int id; +} rpc_request; + +typedef struct rpc_response +{ + char *result; + char *error; + int id; +} rpc_response; + +rpc_request* rpc_req_new(char *method, char *params, int id); +rpc_response* rpc_resp_new(char *result, char *error, int id); +int rpc_req_free(rpc_request *req); +int rpc_resp_free(rpc_response *resp); +int rpc_req_marsh( rpc_request *req, netbyte_store **nb_req); +int rpc_resp_marsh( rpc_response *resp, netbyte_store **nb_resp); +int rpc_req_unmarsh( netbyte_store *nb_req, rpc_request **req); +int rpc_resp_unmarsh(netbyte_store *nb_resp, rpc_response **resp); + +#endif \ No newline at end of file diff --git a/extlibs/netbytes.c b/extlibs/netbytes.c index ecbb87e..81ba4d0 100644 --- a/extlibs/netbytes.c +++ b/extlibs/netbytes.c @@ -165,6 +165,8 @@ int nb_add_u8arr( netbyte_store *store, nb_u8arr *u8arr ) int nb_add_u16( netbyte_store *store, nb_u16 *u16 ) { + nb_u16 *new_u16 = NULL; + if ( store->count >= __NBT_MAX_TYPES - 1 ) return -1; @@ -172,7 +174,11 @@ int nb_add_u16( netbyte_store *store, nb_u16 *u16 ) return -2; store->types[store->count].type = NBT_U16; - store->types[store->count].nb_val = (uint8_t *)u16; + + new_u16 = malloc(sizeof(nb_u16)); + memcpy(new_u16, u16, sizeof(nb_u16)); + store->types[store->count].nb_val = (uint8_t *)new_u16; + store->count += 1; return 0; } @@ -195,6 +201,8 @@ int nb_add_u16arr( netbyte_store *store, nb_u16arr *u16arr ) int nb_add_u32( netbyte_store *store, nb_u32 *u32 ) { + nb_u32 *new_u32 = NULL; + if ( store->count >= __NBT_MAX_TYPES - 1 ) return -1; @@ -202,7 +210,11 @@ int nb_add_u32( netbyte_store *store, nb_u32 *u32 ) return -2; store->types[store->count].type = NBT_U32; - store->types[store->count].nb_val = (uint8_t *)u32; + + new_u32 = malloc(sizeof(nb_u32)); + memcpy(new_u32, u32, sizeof(nb_u32)); + store->types[store->count].nb_val = (uint8_t *)new_u32; + store->count += 1; return 0; } @@ -225,6 +237,8 @@ int nb_add_u32arr( netbyte_store *store, nb_u32arr *u32arr ) int nb_add_u64( netbyte_store *store, nb_u64 *u64 ) { + nb_u64 *new_u64 = NULL; + if ( store->count >= __NBT_MAX_TYPES - 1 ) return -1; @@ -232,7 +246,11 @@ int nb_add_u64( netbyte_store *store, nb_u64 *u64 ) return -2; store->types[store->count].type = NBT_U64; - store->types[store->count].nb_val = (uint8_t *)u64; + + new_u64 = malloc(sizeof(nb_u64)); + memcpy(new_u64, u64, sizeof(nb_u64)); + store->types[store->count].nb_val = (uint8_t *)new_u64; + store->count += 1; return 0; } @@ -463,6 +481,15 @@ int nb_free(netbyte_store *store) store->types[i].type=0; break; } + case NBT_U32: + { + nb_u32 *u32 = (nb_u32 *)val; + //free(u32); + //u32 = NULL; + store->types[i].nb_val=NULL; + store->types[i].type=0; + break; + } default: printf("Unknown type\n"); } @@ -492,7 +519,7 @@ int nb_load( netbyte_store *store, uint8_t *data ) __NBT_SIZE size; memcpy( &size, c, sizeof(__NBT_SIZE) ); - printf("size: %d\n", size); + printf("NB_LOAD size: %d\n", size); size -= sizeof(__NBT_SIZE); c += sizeof(__NBT_SIZE); @@ -512,14 +539,12 @@ int nb_load( netbyte_store *store, uint8_t *data ) { case NBT_U8: { - nb_u8 *u8 = malloc( sizeof(nb_u8) ); + nb_u8 u8; - memcpy( u8, c, sizeof(nb_u8) ); + memcpy( &u8, c, sizeof(nb_u8) ); c += sizeof(nb_u8); - nb_add_u8( store, u8 ); - - free(u8); + nb_add_u8( store, &u8 ); break; } @@ -545,12 +570,12 @@ int nb_load( netbyte_store *store, uint8_t *data ) } case NBT_U16: { - nb_u16 *u16 = malloc( sizeof(nb_u16) ); + nb_u16 u16; - memcpy( u16, c, sizeof(nb_u16) ); + memcpy( &u16, c, sizeof(nb_u16) ); c += sizeof(nb_u16); - nb_add_u16( store, u16 ); + nb_add_u16( store, &u16 ); break; } @@ -573,12 +598,12 @@ int nb_load( netbyte_store *store, uint8_t *data ) } case NBT_U32: { - nb_u32 *u32 = malloc( sizeof(nb_u32) ); - - memcpy( u32, c, sizeof(nb_u32) ); + nb_u32 u32; + + memcpy( &u32, c, sizeof(nb_u32) ); c += sizeof(nb_u32); - nb_add_u32( store, u32 ); + nb_add_u32( store, &u32 ); break; } case NBT_U32ARRAY: @@ -600,12 +625,12 @@ int nb_load( netbyte_store *store, uint8_t *data ) } case NBT_U64: { - nb_u64 *u64 = malloc( sizeof(nb_u64) ); + nb_u64 u64; - memcpy( u64, c, sizeof(nb_u64) ); + memcpy( &u64, c, sizeof(nb_u64) ); c += sizeof(nb_u64); - nb_add_u64( store, u64 ); + nb_add_u64( store, &u64 ); break; } case NBT_U64ARRAY: @@ -766,6 +791,13 @@ int nb_print(netbyte_store *store) u8arr->type, u8arr->len, u8arr->val); break; } + case NBT_U32: + { + nb_u32 *u32 = (nb_u32 *)store->types[i].nb_val; + printf("u32: t=0x%x v=0x%x", + u32->type, u32->val); + break; + } default: printf("Unknown"); } diff --git a/extlibs/netbytes.h b/extlibs/netbytes.h index 3960142..6e008bc 100644 --- a/extlibs/netbytes.h +++ b/extlibs/netbytes.h @@ -47,25 +47,25 @@ #define __NBT_MAX_TYPES (256) //creating netbyte structure -typedef struct __nb_type { +typedef struct __attribute__((packed)) __nb_type { __NBT_TYPED type; uint8_t *nb_val; -} __nb_type; +} __nb_type ; -typedef struct netbyte_store +typedef struct __attribute__((packed)) netbyte_store { __NBT_SIZE size; int count; __nb_type types[__NBT_MAX_TYPES]; } netbyte_store; -typedef struct nb_u8 +typedef struct __attribute__((packed)) nb_u8 { __NBT_TYPED type; uint8_t val; } nb_u8; -typedef struct nb_u8arr +typedef struct __attribute__((packed)) nb_u8arr { __NBT_TYPED type; __NBT_U8ARR_LEN len; @@ -74,13 +74,13 @@ typedef struct nb_u8arr /*-----------------*/ -typedef struct nb_u16 +typedef struct __attribute__((packed)) nb_u16 { __NBT_TYPED type; uint16_t val; } nb_u16; -typedef struct nb_u16arr +typedef struct __attribute__((packed)) nb_u16arr { __NBT_TYPED type; __NBT_U16ARR_LEN len; @@ -89,13 +89,13 @@ typedef struct nb_u16arr /*-----------------*/ -typedef struct nb_u32 +typedef struct __attribute__((packed)) nb_u32 { __NBT_TYPED type; uint32_t val; } nb_u32; -typedef struct nb_u32arr +typedef struct __attribute__((packed)) nb_u32arr { __NBT_TYPED type; __NBT_U32ARR_LEN len; @@ -104,13 +104,13 @@ typedef struct nb_u32arr /*------------------*/ -typedef struct nb_u64 +typedef struct __attribute__((packed)) nb_u64 { __NBT_TYPED type; uint64_t val; } nb_u64; -typedef struct nb_u64arr +typedef struct __attribute__((packed)) nb_u64arr { __NBT_TYPED type; __NBT_U64ARR_LEN len; @@ -118,7 +118,7 @@ typedef struct nb_u64arr } nb_u64arr; //loading/parsing netbyte structure -typedef struct netbyte_load +typedef struct __attribute__((packed)) netbyte_load { __NBT_SIZE size; uint8_t *buf; @@ -148,24 +148,24 @@ int nb_u32arr_create( nb_u32arr *s, __NBT_U32ARR_LEN len, uint32_t *val ); int nb_u64_create( nb_u64 *s, uint64_t val ); int nb_u64arr_create( nb_u64arr *s, __NBT_U64ARR_LEN len, uint64_t *val ); -int nb_init( netbyte_store *store ); -int nb_add_u8( netbyte_store *store, nb_u8 *u8 ); -int nb_add_u8arr( netbyte_store *store, nb_u8arr *u8arr ); -int nb_add_u16( netbyte_store *store, nb_u16 *u16 ); +int nb_init( netbyte_store *store ); +int nb_add_u8( netbyte_store *store, nb_u8 *u8 ); +int nb_add_u8arr( netbyte_store *store, nb_u8arr *u8arr ); +int nb_add_u16( netbyte_store *store, nb_u16 *u16 ); int nb_add_u16arr( netbyte_store *store, nb_u16arr *u16arr ); -int nb_add_u32( netbyte_store *store, nb_u32 *u32 ); +int nb_add_u32( netbyte_store *store, nb_u32 *u32 ); int nb_add_u32arr( netbyte_store *store, nb_u32arr *u32arr ); -int nb_add_u64( netbyte_store *store, nb_u64 *u64 ); +int nb_add_u64( netbyte_store *store, nb_u64 *u64 ); int nb_add_u64arr( netbyte_store *store, nb_u64arr *u64arr ); uint8_t *nb_create( netbyte_store *store ); int nb_free(netbyte_store *store); -int nb_load( netbyte_store *store, uint8_t *data ); +int nb_load( netbyte_store *store, uint8_t *data ); int nb_count( netbyte_store *store ); -int nb_type( netbyte_store *store, int count, __NBT_TYPED **type ); -int nb_val( netbyte_store *store, int count, __nb_type **type ); +int nb_type( netbyte_store *store, int count, __NBT_TYPED **type ); +int nb_val( netbyte_store *store, int count, __nb_type **type ); int nb_fread( netbyte_store *store, int fd); //print all all values in netbyte string -- cgit v1.2.3