aboutsummaryrefslogtreecommitdiffstats
path: root/include/packet.c
blob: 45ebcd19ddd306d0a600dfa4c46c4ea1248a3bcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "packet.h"

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)
	[CMD_LAST] = -1,
	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";
	case ERR_TYPE:
		return "No such command";
	}
	return "Unknown error";
}
/* vim: set ts=8 noet: */