diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/DHT.h | 2 | ||||
-rw-r--r-- | core/Messenger.c | 38 | ||||
-rw-r--r-- | core/Messenger.h | 4 | ||||
-rw-r--r-- | core/network.h | 5 |
4 files changed, 40 insertions, 9 deletions
@@ -30,8 +30,6 @@ | |||
30 | extern "C" { | 30 | extern "C" { |
31 | #endif | 31 | #endif |
32 | 32 | ||
33 | /* Current time, unix format */ | ||
34 | #define unix_time() ((uint64_t)time(NULL)) | ||
35 | 33 | ||
36 | /* size of the client_id in bytes */ | 34 | /* size of the client_id in bytes */ |
37 | #define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES | 35 | #define CLIENT_ID_SIZE crypto_box_PUBLICKEYBYTES |
diff --git a/core/Messenger.c b/core/Messenger.c index 3d991e63..1df49d2a 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -51,16 +51,31 @@ static uint8_t *self_statusmessage; | |||
51 | static uint16_t self_statusmessage_len; | 51 | static uint16_t self_statusmessage_len; |
52 | static USERSTATUS self_userstatus; | 52 | static USERSTATUS self_userstatus; |
53 | 53 | ||
54 | #define MAX_NUM_FRIENDS 256 | 54 | static Friend *friendlist; |
55 | 55 | static uint32_t numallocated; | |
56 | static Friend friendlist[MAX_NUM_FRIENDS]; | ||
57 | |||
58 | static uint32_t numfriends; | 56 | static uint32_t numfriends; |
59 | 57 | ||
58 | |||
60 | /* 1 if we are online | 59 | /* 1 if we are online |
61 | 0 if we are offline | 60 | 0 if we are offline |
62 | static uint8_t online; */ | 61 | static uint8_t online; */ |
63 | 62 | ||
63 | /* double the size of the friend list | ||
64 | return -1 if realloc fails */ | ||
65 | int realloc_friendlist(void) { | ||
66 | if (numallocated == 0) | ||
67 | numallocated = 1; /* initial size */ | ||
68 | else | ||
69 | numallocated *= 2; /* double each time */ | ||
70 | |||
71 | Friend *newfriendlist = realloc(friendlist, numallocated*sizeof(Friend)); | ||
72 | if (newfriendlist == NULL) | ||
73 | return -1; | ||
74 | |||
75 | friendlist = newfriendlist; | ||
76 | return 0; | ||
77 | } | ||
78 | |||
64 | /* return the friend id associated to that public key. | 79 | /* return the friend id associated to that public key. |
65 | return -1 if no such friend */ | 80 | return -1 if no such friend */ |
66 | int getfriend_id(uint8_t *client_id) | 81 | int getfriend_id(uint8_t *client_id) |
@@ -118,8 +133,12 @@ int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) | |||
118 | if (getfriend_id(client_id) != -1) | 133 | if (getfriend_id(client_id) != -1) |
119 | return FAERR_ALREADYSENT; | 134 | return FAERR_ALREADYSENT; |
120 | 135 | ||
136 | /* resize the friend list if necessary */ | ||
137 | if (numfriends + 1 > numallocated) | ||
138 | realloc_friendlist(); | ||
139 | |||
121 | uint32_t i; | 140 | uint32_t i; |
122 | for (i = 0; i <= numfriends && i <= MAX_NUM_FRIENDS; ++i) { /*TODO: dynamic memory allocation to allow for more than MAX_NUM_FRIENDS friends */ | 141 | for (i = 0; i <= numfriends; ++i) { |
123 | if (friendlist[i].status == NOFRIEND) { | 142 | if (friendlist[i].status == NOFRIEND) { |
124 | DHT_addfriend(client_id); | 143 | DHT_addfriend(client_id); |
125 | friendlist[i].status = FRIEND_ADDED; | 144 | friendlist[i].status = FRIEND_ADDED; |
@@ -145,8 +164,13 @@ int m_addfriend_norequest(uint8_t * client_id) | |||
145 | { | 164 | { |
146 | if (getfriend_id(client_id) != -1) | 165 | if (getfriend_id(client_id) != -1) |
147 | return -1; | 166 | return -1; |
167 | |||
168 | /* resize the friend list if necessary */ | ||
169 | if (numfriends + 1 > numallocated) | ||
170 | realloc_friendlist(); | ||
171 | |||
148 | uint32_t i; | 172 | uint32_t i; |
149 | for (i = 0; i <= numfriends && i <= MAX_NUM_FRIENDS; ++i) { /*TODO: dynamic memory allocation to allow for more than MAX_NUM_FRIENDS friends */ | 173 | for (i = 0; i <= numfriends; ++i) { |
150 | if(friendlist[i].status == NOFRIEND) { | 174 | if(friendlist[i].status == NOFRIEND) { |
151 | DHT_addfriend(client_id); | 175 | DHT_addfriend(client_id); |
152 | friendlist[i].status = FRIEND_REQUESTED; | 176 | friendlist[i].status = FRIEND_REQUESTED; |
@@ -158,7 +182,7 @@ int m_addfriend_norequest(uint8_t * client_id) | |||
158 | friendlist[i].userstatus = USERSTATUS_NONE; | 182 | friendlist[i].userstatus = USERSTATUS_NONE; |
159 | friendlist[i].message_id = 0; | 183 | friendlist[i].message_id = 0; |
160 | friendlist[i].receives_read_receipts = 1; /* default: YES */ | 184 | friendlist[i].receives_read_receipts = 1; /* default: YES */ |
161 | numfriends++; | 185 | ++numfriends; |
162 | return i; | 186 | return i; |
163 | } | 187 | } |
164 | } | 188 | } |
diff --git a/core/Messenger.h b/core/Messenger.h index d2fa8945..3a9a56df 100644 --- a/core/Messenger.h +++ b/core/Messenger.h | |||
@@ -188,6 +188,10 @@ void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t)); | |||
188 | you are not responsible for freeing newstatus */ | 188 | you are not responsible for freeing newstatus */ |
189 | void m_callback_statusmessage(void (*function)(int, uint8_t *, uint16_t)); | 189 | void m_callback_statusmessage(void (*function)(int, uint8_t *, uint16_t)); |
190 | 190 | ||
191 | /* set the callback for status type changes | ||
192 | function(int friendnumber, USERSTATUS kind) */ | ||
193 | void m_callback_userstatus(void (*function)(int, USERSTATUS)); | ||
194 | |||
191 | /* set the callback for read receipts | 195 | /* set the callback for read receipts |
192 | function(int friendnumber, uint32_t receipt) | 196 | function(int friendnumber, uint32_t receipt) |
193 | if you are keeping a record of returns from m_sendmessage, | 197 | if you are keeping a record of returns from m_sendmessage, |
diff --git a/core/network.h b/core/network.h index d246a9d1..d3c39333 100644 --- a/core/network.h +++ b/core/network.h | |||
@@ -66,6 +66,11 @@ extern "C" { | |||
66 | 66 | ||
67 | #define MAX_UDP_PACKET_SIZE 65507 | 67 | #define MAX_UDP_PACKET_SIZE 65507 |
68 | 68 | ||
69 | |||
70 | /* Current time, unix format */ | ||
71 | #define unix_time() ((uint64_t)time(NULL)) | ||
72 | |||
73 | |||
69 | typedef union { | 74 | typedef union { |
70 | uint8_t c[4]; | 75 | uint8_t c[4]; |
71 | uint16_t s[2]; | 76 | uint16_t s[2]; |