libchat6
lc6_common.h
1 #ifndef LC6_COMMON_H
2 #define LC6_COMMON_H
3 
4 #include <pthread.h>
5 #include <sys/param.h>
6 #include <arpa/inet.h>
7 #include <sodium.h>
8 
9 #include "../inc/libchat6.h"
10 #include "../inc/lc6_helpers.h"
11 
12 #ifdef LC6_DEBUG
13 #include "../inc/lc6_debug.h"
14 #endif
15 
16 #define LC6_CRYPTO_HASHLEN crypto_generichash_BYTES
17 #define LC6_CRYPTO_HASHSIGNLEN (crypto_secretbox_NONCEBYTES+crypto_box_MACBYTES+crypto_generichash_BYTES)
18 #define LC6_CRYPTO_PUBLEN crypto_kx_PUBLICKEYBYTES
19 #define LC6_CRYPTO_PRIVLEN crypto_kx_SECRETKEYBYTES
20 #define LC6_CRYPTO_RXLEN crypto_kx_SESSIONKEYBYTES
21 #define LC6_CRYPTO_TXLEN crypto_kx_SESSIONKEYBYTES
22 
23 #define LC6_EVENT_MAXPEERS_OUT 5
24 #define LC6_EVENT_MAXPEERS_IN 5
25 
26 typedef struct LC6_NODE {
27  unsigned char *pub_key;
28  unsigned char *priv_key;
29 } LC6_NODE;
30 
31 typedef struct LC6_PEER {
32  int sock;
33  LC6_NODE keys;
34  unsigned char bufi[65536];
35  unsigned char bufo[65536];
36 } LC6_PEER;
37 
38 typedef struct LC6_USER {
39  struct LC6_USER *prev;
40  struct LC6_USER *next;
41  unsigned char nickname[256];
42  unsigned char pub_key[LC6_CRYPTO_PUBLEN];
43  unsigned char priv_key[LC6_CRYPTO_PRIVLEN];
44  uint8_t status;
45 } LC6_USER;
46 
47 typedef struct LC6_FRIEND {
48  struct LC6_FRIEND *prev;
49  struct LC6_FRIEND *next;
50  unsigned char nickname[256];
51  unsigned char pub_key[LC6_CRYPTO_PUBLEN];
52  unsigned char key_rx[LC6_CRYPTO_RXLEN];
53  unsigned char key_tx[LC6_CRYPTO_TXLEN];
54  uint8_t status;
55 } LC6_FRIEND;
56 
57 typedef struct LC6_MSG_TLV {
58  int type;
59  int length;
60  unsigned char *value;
61 } LC6_MSG_TLV;
62 
63 typedef struct LC6_MSG {
64  uint64_t timestamp;
65  int type;
66  LC6_MSG_TLV **tlv;
67 } LC6_MSG;
68 
69 enum LC6_MSG_TYPE_ENUM {
70  LC6_MSG_TYPE_CLIENT_ONLINE,
71  LC6_MSG_TYPE_CLIENT_UPDATE,
72  LC6_MSG_TYPE_CLIENT_OFFLINE,
73  LC6_MSG_TYPE_KEEPALIVE_REQUEST,
74  LC6_MSG_TYPE_KEEPALIVE_RESPONSE,
75  LC6_MSG_TYPE_FRIEND_REQUEST,
76  LC6_MSG_TYPE_FRIEND_RESPONSE,
77  LC6_MSG_TYPE_MESSAGE_SEND,
78  LC6_MSG_TYPE_MESSAGE_RECV,
79  LC6_MSG_TYPE_MESSAGE_READ,
80  LC6_MSG_TYPE_NEIGHBOURS_REQUEST,
81  LC6_MSG_TYPE_NEIGHBOURS_RESPONSE
82 };
83 
84 enum LC6_MSG_TLV_ENUM {
85  LC6_MSG_TLV_NICKNAME,
86  LC6_MSG_TLV_KEY,
87  LC6_MSG_TLV_NONCE,
88  LC6_MSG_TLV_ICON,
89  LC6_MSG_TLV_METRIC,
90  LC6_MSG_TLV_MSGID,
91  LC6_MSG_TLV_CHATID,
92  LC6_MSG_TLV_NEIGHBOUR
93 };
94 
95 enum LC6_MSG_TLV_TYPE_ENUM {
96  LC6_MSG_TLV_TYPE_UTF8,
97  LC6_MSG_TLV_TYPE_BINARY,
98  LC6_MSG_TLV_TYPE_INTEGER,
99  LC6_MSG_TLV_TYPE_UUID,
100  LC6_MSG_TLV_TYPE_IPPORT
101 };
102 
103 
104 #define LC6_CONFIG_BOOTSTRAP "bootstrap.bin"
105 #define LC6_CONFIG_USER "user.bin"
106 #define LC6_CONFIG_FRIEND "friends.bin"
107 #define LC6_CONFIG_MAXLEN 50
108 
109 typedef struct LC6_IPADDR {
110  uint8_t af;
111  union addr {
112  uint32_t inet;
113  unsigned char inet6[16];
114  } addr;
115 } LC6_IPADDR;
116 
117 typedef struct LC6_BOOTSTRAP {
118  struct LC6_BOOTSTRAP *prev;
119  struct LC6_BOOTSTRAP *next;
120  uint64_t last_contact;
121  uint8_t hardcoded;
122  LC6_IPADDR ip;
123 } LC6_BOOTSTRAP;
124 
125 typedef struct LC6_CTX {
126  char path[MAXPATHLEN-LC6_CONFIG_MAXLEN];
127  LIBCHAT_CB *callback;
128  LC6_USER *user;
129  LC6_NODE *node;
130  LC6_FRIEND *friend;
131  LC6_BOOTSTRAP *bootstrap;
132  pthread_t ev_thread;
133 } LC6_CTX;
134 
135 extern int LC6_MSG_TLV_TYPE_INT[8];
136 extern unsigned char LC6_MSG_TYPE_STR[13][30];
137 extern unsigned char LC6_MSG_TLV_STR[8][20];
138 
139 #endif
Definition: lc6_common.h:109
Definition: lc6_common.h:57
Definition: lc6_common.h:31
Definition: lc6_common.h:125
Definition: lc6_common.h:117
Definition: lc6_common.h:111
Definition: lc6_common.h:63
Definition: lc6_common.h:38
Definition: lc6_common.h:47
Definition: lc6_common.h:26