diff --git a/protocol b/protocol index 389f492..5e03f95 100644 --- a/protocol +++ b/protocol @@ -3,8 +3,9 @@ PROTOCOL all numbers are packed unless stated otherwise packed values are msb -strings are predefined sizes and are terminated at either -the boundary of the region or the first NUL character (0x0) +strings are predefined sizes and are terminated at the +first NUL character (0x0). max string length is the region +length minus one, for the required NUL. byte n is b.n region n is r.n @@ -23,11 +24,11 @@ byte the bytes after the header octet bytes bytes: client -> server id 0: registration attempt - r.0 - username - string (max 8b) + r.0 - username - string (max 9b) r.1 - pin - ushort (max 9999) id 1: login attempt - r.0 - username - string (max 8b) + r.0 - username - string (max 9b) r.1 - pin - ushort (max 9999) id 2: ctx change response @@ -39,12 +40,12 @@ client -> server server -> client id 0: registration response r.0 - succeeded - bool - r.1 - status - string (max 16b) + r.1 - status - string (17b) id 1: login response - r.0 - succeeded - bool + r.0 - succeeded - bool r.1 - user id - ushort - r.2 - status - string (max 16b) + r.2 - status - string (17b) id 2: ctx change request r.0 - ctx id - ushort \ No newline at end of file diff --git a/src/main.c b/src/main.c index c705ebb..75c86bd 100644 --- a/src/main.c +++ b/src/main.c @@ -6,12 +6,12 @@ int main(int argc, char **argv) { packet_context_init(2); - packet_context_register(PCK_CTX_C2S, PCK_ID_REGISTER, 0, 2, 8, 2); - packet_context_register(PCK_CTX_C2S, PCK_ID_LOGIN, 0, 2, 8, 2); + packet_context_register(PCK_CTX_C2S, PCK_ID_REGISTER, 0, 2, 9, 2); + packet_context_register(PCK_CTX_C2S, PCK_ID_LOGIN, 0, 2, 9, 2); packet_context_register(PCK_CTX_C2S, PCK_ID_CHANGE_CTX, 0, 2, 1, 2); - packet_context_register(PCK_CTX_S2C, PCK_ID_REGISTER, 0, 2, 1, 16); - packet_context_register(PCK_CTX_S2C, PCK_ID_LOGIN, 0, 3, 1, 2, 16); + packet_context_register(PCK_CTX_S2C, PCK_ID_REGISTER, 0, 2, 1, 17); + packet_context_register(PCK_CTX_S2C, PCK_ID_LOGIN, 0, 3, 1, 2, 17); packet_context_register(PCK_CTX_S2C, PCK_ID_CHANGE_CTX, 0, 1, 2); if(argc == 2 && strcmp(argv[1], "server") == 0) { diff --git a/src/packet.c b/src/packet.c index 0dd95b3..dd7b8bc 100644 --- a/src/packet.c +++ b/src/packet.c @@ -4,8 +4,12 @@ typedef struct packet_ctx_t packet_ctx_t; struct packet_ctx_t { uint8_t iter_start; uint32_t iter_length; + uint32_t pre_iter_length; uint8_t region_count; + uint16_t iter_count; + uint16_t pre_iter_count; + uint16_t *region_lengths; uint32_t length; }; @@ -39,7 +43,11 @@ void packet_context_register(uint8_t direction, uint8_t id, uint8_t iter_pt, uin direction == PCK_CTX_C2S ? ctx.c2s : ctx.s2c; ptr[id].length = 0; + ptr[id].iter_length = 0; + ptr[id].pre_iter_length = 0; ptr[id].iter_start = iter_pt; + ptr[id].iter_count = count - iter_pt; + ptr[id].pre_iter_count = count - ptr[id].iter_count; ptr[id].region_count = count; ptr[id].region_lengths = (uint16_t*)malloc(count * sizeof(uint16_t)); @@ -48,6 +56,10 @@ void packet_context_register(uint8_t direction, uint8_t id, uint8_t iter_pt, uin uint16_t length = va_arg(args, int); ptr[id].region_lengths[i] = length; ptr[id].length += length; + if(iter_pt == 0 || i < iter_pt) + ptr[id].pre_iter_length += length; + else + ptr[id].iter_length += length; } va_end(args); } @@ -64,13 +76,18 @@ void packet_context_free() { free(ctx.s2c); } -static void packet_fill_regions(packet_t *packet) { - uint8_t *ptr = packet->raw; +static void packet_fill_regions(packet_t *packet, packet_ctx_t *ctx) { + uint8_t *ptr = packet->raw + 7; int i; packet->regions = (uint8_t**)malloc(packet->region_count * sizeof(uint8_t*)); for(i = 0; i < packet->region_count; ++i) { - packet->regions // TOD Ofisnibh THIS + packet->regions[i] = ptr; + if(ctx->iter_start == 0 || i < ctx->iter_start) + ptr += packet->region_lengths[i]; + else if(ctx->iter_start != 0) + ptr += packet->region_lengths[ctx->iter_start + ((i - ctx->iter_start) % ctx->iter_count)]; + } } packet_t* packet_init_in(uint8_t *raw) { @@ -81,14 +98,16 @@ packet_t* packet_init_in(uint8_t *raw) { packet_ctx_t *in = &(ctx.in[raw[2]]); - packet_t *packet = (packet_t*)malloc(sizeof(packet_t)); - packet->id = raw[2]; - packet->raw = raw; - packet->length = ntohl(*((uint32_t*)raw[3])); + packet_t *packet = (packet_t*)malloc(sizeof(packet_t)); + packet->ctx = in; + packet->id = raw[2]; + packet->iterator = 0; + packet->next_append = 0xFFFF; + packet->raw = raw; + packet->length = ntohl(*((uint32_t*)&raw[3])); packet->region_lengths = in->region_lengths; - packet->iterator = 0; - packet->next_append = 0xFFFF; - packet_fill_regions(packet); + packet->region_count = in->pre_iter_count + (packet->length - in->pre_iter_length) / in->iter_length; + packet_fill_regions(packet, in); return packet; } @@ -99,20 +118,72 @@ packet_t* packet_init_out(uint8_t id, uint16_t iterations) { packet_ctx_t *out = &(ctx.out[id]); int regions = out->region_count; - int iterable_regions = out->region_count - out->iter_start - 1; if(out->iter_start != 0) - regions = out->iter_start + (iterations * iterable_regions); + regions = out->iter_start + (iterations * out->iter_count); - packet_t *packet = (packet_t*)malloc(sizeof(packet_t)); - - packet->id = id; - packet->iterator = 0xFFFF; - packet->next_append = 0; - packet->region_count = regions; + packet_t *packet = (packet_t*)malloc(sizeof(packet_t)); + packet->ctx = out; + packet->id = id; + packet->iterator = 0xFFFF; + packet->next_append = 0; + packet->region_count = regions; packet->region_lengths = out->region_lengths; - packet->length = (length - iter_length) + iter_length * iterations; - packet->raw = (uint8_t*)malloc(packet->length * sizeof(uint8_t)); - packet_fill_regions(packet); + packet->length = out->pre_iter_length + out->iter_length * iterations; + packet->raw = (uint8_t*)malloc(packet->length * sizeof(uint8_t)); + packet_fill_regions(packet, out); return packet; -} \ No newline at end of file +} + +uint8_t* packet_get(packet_t *packet, uint16_t region, int *size) { + if(region >= packet->region_count) + return NULL; + + packet_ctx_t *ctx; + if(size != NULL) { + ctx = (packet_ctx_t*)packet->ctx; + if(region >= ctx->iter_start && ctx->iter_start != 0) + *size = packet->region_lengths[(region - ctx->iter_start) % ctx->iter_count]; + else + *size = packet->region_lengths[region]; + } + + return packet->regions[region]; +} + +uint8_t packet_get_uint8(packet_t *packet, uint16_t region) { + if(region >= packet->region_count) + return NULL; + + return *((uint8_t*)packet->regions[region]); +} + +uint16_t packet_get_uint16(packet_t *packet, uint16_t region) { + if(region >= packet->region_count) + return NULL; + + return *((uint16_t*)packet->regions[region]); +} + +uint32_t packet_get_uint32(packet_t *packet, uint16_t region) { + if(region >= packet->region_count) + return NULL; + + return *((uint32_t*)packet->regions[region]); +} + +uint64_t packet_get_uint64(packet_t *packet, uint64_t region) { + if(region >= packet->region_count) + return NULL; + + return *((uint64_t*)packet->regions[region]); +} + +uint8_t* packet_get_raw(packet_t *packet, int *size) { + if(size != NULL) + *size = packet->length; + + return packet->raw; +} + +uint16_t packet_region_length(uint64_t todo_finish_this, void *DO_IT \ No newline at end of file diff --git a/src/packet.h b/src/packet.h index 2fe8e53..6a37614 100644 --- a/src/packet.h +++ b/src/packet.h @@ -24,6 +24,7 @@ struct packet_t { uint16_t region_count; uint16_t iterator; uint16_t next_append; + void *ctx; }; void packet_context_init(uint8_t); @@ -33,13 +34,15 @@ void packet_context_free(); packet_t* packet_init_in(uint8_t*); packet_t* packet_init_out(uint8_t, uint16_t); -uint8_t* packet_get (packet_t*, uint8_t, int*); -uint8_t packet_get_uint8 (packet_t*, uint8_t); -uint16_t packet_get_uint16 (packet_t*, uint8_t); -uint32_t packet_get_uint32 (packet_t*, uint8_t); -uint64_t packet_get_uint64 (packet_t*, uint8_t); +uint8_t* packet_get (packet_t*, uint16_t, int*); +uint8_t packet_get_uint8 (packet_t*, uint16_t); +uint16_t packet_get_uint16 (packet_t*, uint16_t); +uint32_t packet_get_uint32 (packet_t*, uint16_t); +uint64_t packet_get_uint64 (packet_t*, uint16_t); uint8_t* packet_get_raw (packet_t*, int*); +BOOL packet_iter_next (packet_t*); +BOOL packet_iter_prev (packet_t*); uint8_t* packet_iter_get (packet_t*, int*); uint8_t packet_iter_get_uint8 (packet_t*); uint16_t packet_iter_get_uint16 (packet_t*);