diff options
Diffstat (limited to 'include/packet.c')
-rw-r--r-- | include/packet.c | 65 |
1 files changed, 62 insertions, 3 deletions
diff --git a/include/packet.c b/include/packet.c index 5f551de..a523285 100644 --- a/include/packet.c +++ b/include/packet.c @@ -1,7 +1,66 @@ #include "packet.h" -unsigned int system_packet_checksum(struct kv_system_packet *packet) { - return (packet->user_id << 8) ^ - (packet->operation_id | (177013 << 10)); +i64 const commd_size_lookup[64] = {[CMD_CREATE] = sizeof(struct commd_create), + [CMD_DELETE] = sizeof(struct commd_delete), + [CMD_JOIN] = sizeof(struct commd_join), + [CMD_LEAVE] = sizeof(struct commd_leave), + [CMD_REGISTER] = sizeof(struct commd_register), + [CMD_UNREGISTER] = sizeof(struct commd_unregister), + [CMD_GET_PORT] = sizeof(struct commd_get_port), + [CMD_GET_CHANNELS] = 0, // You can not get a sizeof(void) + 0}; + +u32 system_packet_checksum(struct kv_system_packet *packet) +{ + return ((packet->user_id << 8) ^ (ntoh64(packet->operation_id) | (177013 << 10)) ^ packet->ackid) & + packet->magic_bytes; +} +u8 is_system_packet(struct kv_packet *p) +{ + struct kv_system_packet *sysp = (struct kv_system_packet *)p; + if (sysp->magic_bytes == SYS_PACKET_MAGIC_BYTES && (signed int)ntohl(sysp->operation_id) < 0) return 1; + return 0; +} + +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +u64 hton64(u64 a) +{ + #if __SIZEOF_SIZE_T__ == 8 + return ((u64)htonl((u32)(a & 0xFFFFFFFF)) << 32) | (u64)htonl((u32)(a >> 32)); + #else + return htonl((u32)a); + #endif +} +u64 ntoh64(u64 a) +{ + #if __SIZEOF_SIZE_T__ == 8 + return ((u64)ntohl((u32)(a & 0xFFFFFFFF)) << 32) | (u64)ntohl((u32)(a >> 32)); + #else + return ntohl((u32)a); + #endif } +#else +u64 htonzu(u64 a) { return a; } +u64 ntohzu(u64 a) { return a; } +#endif +const char *kv_strerror(enum commd_error e) +{ + switch (e) { + case ERR_SUCCESS: + return "Success"; + case ERR_SERV: + return "Internal server error"; + case ERR_ACCESS: + return "No access"; + case ERR_INVAL: + return "Invalid/insufficient parameters"; + case ERR_PARAM: + return "Incorrect parameters"; + case ERR_NOIMPL: + return "Not implemented"; + case ERR_DO_IT_YOURSELF: + return "You should not have recieved this error. This is either server or network fault"; + } + return "Unknown error"; +} |