diff options
author | Eniz Vukovic <eniz_vukovic@hotmail.com> | 2015-10-10 23:54:23 +0200 |
---|---|---|
committer | Eniz Vukovic <eniz_vukovic@hotmail.com> | 2015-10-10 23:54:23 +0200 |
commit | d6fdf16520b6f242935ca95eeb739ec9a8eaa14c (patch) | |
tree | 069f3355835aa0497fe494c36554ea24d0b222f1 /toxcore | |
parent | bf5e9b89d2a67c293aae503c03e193307ea7990b (diff) |
New Adaptive BR algorithm, cleanups and fixes
Diffstat (limited to 'toxcore')
-rw-r--r-- | toxcore/Messenger.c | 6 | ||||
-rw-r--r-- | toxcore/assoc.c | 8 | ||||
-rw-r--r-- | toxcore/assoc.h | 4 | ||||
-rw-r--r-- | toxcore/logger.c | 18 | ||||
-rw-r--r-- | toxcore/logger.h | 19 | ||||
-rw-r--r-- | toxcore/network.c | 12 | ||||
-rw-r--r-- | toxcore/util.c | 29 | ||||
-rw-r--r-- | toxcore/util.h | 6 |
8 files changed, 57 insertions, 45 deletions
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 4277f16a..4cd9e1d6 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -2239,7 +2239,7 @@ static void connection_status_cb(Messenger *m) | |||
2239 | } | 2239 | } |
2240 | 2240 | ||
2241 | 2241 | ||
2242 | #ifdef LOGGING | 2242 | #ifdef TOX_LOGGER |
2243 | #define DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS 60UL | 2243 | #define DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS 60UL |
2244 | static time_t lastdump = 0; | 2244 | static time_t lastdump = 0; |
2245 | static char IDString[crypto_box_PUBLICKEYBYTES * 2 + 1]; | 2245 | static char IDString[crypto_box_PUBLICKEYBYTES * 2 + 1]; |
@@ -2315,7 +2315,7 @@ void do_messenger(Messenger *m) | |||
2315 | do_friends(m); | 2315 | do_friends(m); |
2316 | connection_status_cb(m); | 2316 | connection_status_cb(m); |
2317 | 2317 | ||
2318 | #ifdef LOGGING | 2318 | #ifdef TOX_LOGGER |
2319 | 2319 | ||
2320 | if (unix_time() > lastdump + DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS) { | 2320 | if (unix_time() > lastdump + DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS) { |
2321 | 2321 | ||
@@ -2414,7 +2414,7 @@ void do_messenger(Messenger *m) | |||
2414 | } | 2414 | } |
2415 | } | 2415 | } |
2416 | 2416 | ||
2417 | #endif /* LOGGING */ | 2417 | #endif /* TOX_LOGGER */ |
2418 | } | 2418 | } |
2419 | 2419 | ||
2420 | /* new messenger format for load/save, more robust and forward compatible */ | 2420 | /* new messenger format for load/save, more robust and forward compatible */ |
diff --git a/toxcore/assoc.c b/toxcore/assoc.c index 44c4cc30..932adc76 100644 --- a/toxcore/assoc.c +++ b/toxcore/assoc.c | |||
@@ -878,9 +878,9 @@ void Assoc_self_client_id_changed(Assoc *assoc, const uint8_t *id) | |||
878 | } | 878 | } |
879 | } | 879 | } |
880 | 880 | ||
881 | #ifdef LOGGING | 881 | #ifdef TOX_LOGGER |
882 | static char *idpart2str(uint8_t *id, size_t len); | 882 | static char *idpart2str(uint8_t *id, size_t len); |
883 | #endif /* LOGGING */ | 883 | #endif /* TOX_LOGGER */ |
884 | 884 | ||
885 | /* refresh buckets */ | 885 | /* refresh buckets */ |
886 | void do_Assoc(Assoc *assoc, DHT *dht) | 886 | void do_Assoc(Assoc *assoc, DHT *dht) |
@@ -974,7 +974,7 @@ void kill_Assoc(Assoc *assoc) | |||
974 | } | 974 | } |
975 | } | 975 | } |
976 | 976 | ||
977 | #ifdef LOGGING | 977 | #ifdef TOX_LOGGER |
978 | 978 | ||
979 | static char buffer[crypto_box_PUBLICKEYBYTES * 2 + 1]; | 979 | static char buffer[crypto_box_PUBLICKEYBYTES * 2 + 1]; |
980 | static char *idpart2str(uint8_t *id, size_t len) | 980 | static char *idpart2str(uint8_t *id, size_t len) |
@@ -1028,4 +1028,4 @@ void Assoc_status(const Assoc *assoc) | |||
1028 | } | 1028 | } |
1029 | } | 1029 | } |
1030 | 1030 | ||
1031 | #endif /* LOGGING */ | 1031 | #endif /* TOX_LOGGER */ |
diff --git a/toxcore/assoc.h b/toxcore/assoc.h index 1b4e1ff9..65a2745d 100644 --- a/toxcore/assoc.h +++ b/toxcore/assoc.h | |||
@@ -97,8 +97,8 @@ void do_Assoc(Assoc *assoc, DHT *dht); | |||
97 | /* destroy */ | 97 | /* destroy */ |
98 | void kill_Assoc(Assoc *assoc); | 98 | void kill_Assoc(Assoc *assoc); |
99 | 99 | ||
100 | #ifdef LOGGING | 100 | #ifdef TOX_LOGGER |
101 | void Assoc_status(const Assoc *assoc); | 101 | void Assoc_status(const Assoc *assoc); |
102 | #endif /* LOGGING */ | 102 | #endif /* TOX_LOGGER */ |
103 | 103 | ||
104 | #endif /* !__ASSOC_H__ */ | 104 | #endif /* !__ASSOC_H__ */ |
diff --git a/toxcore/logger.c b/toxcore/logger.c index fc6a989a..f19f76b1 100644 --- a/toxcore/logger.c +++ b/toxcore/logger.c | |||
@@ -44,7 +44,7 @@ | |||
44 | #endif | 44 | #endif |
45 | 45 | ||
46 | 46 | ||
47 | struct logger { | 47 | struct Logger { |
48 | FILE *log_file; | 48 | FILE *log_file; |
49 | LOG_LEVEL level; | 49 | LOG_LEVEL level; |
50 | uint64_t start_time; /* Time when lib loaded */ | 50 | uint64_t start_time; /* Time when lib loaded */ |
@@ -87,7 +87,7 @@ char *strtime(char *dest, size_t max_len) | |||
87 | */ | 87 | */ |
88 | Logger *logger_new (const char *file_name, LOG_LEVEL level, const char *id) | 88 | Logger *logger_new (const char *file_name, LOG_LEVEL level, const char *id) |
89 | { | 89 | { |
90 | #ifndef LOGGING /* Disabled */ | 90 | #ifndef TOX_LOGGER /* Disabled */ |
91 | return NULL; | 91 | return NULL; |
92 | #endif | 92 | #endif |
93 | 93 | ||
@@ -96,7 +96,7 @@ Logger *logger_new (const char *file_name, LOG_LEVEL level, const char *id) | |||
96 | if (!retu) | 96 | if (!retu) |
97 | return NULL; | 97 | return NULL; |
98 | 98 | ||
99 | if ( pthread_mutex_init(retu->mutex, NULL) != 0 ) { | 99 | if (pthread_mutex_init(retu->mutex, NULL) != 0) { |
100 | free(retu); | 100 | free(retu); |
101 | return NULL; | 101 | return NULL; |
102 | } | 102 | } |
@@ -110,7 +110,7 @@ Logger *logger_new (const char *file_name, LOG_LEVEL level, const char *id) | |||
110 | 110 | ||
111 | if (!(retu->tstr = calloc(16, sizeof (char))) || | 111 | if (!(retu->tstr = calloc(16, sizeof (char))) || |
112 | !(retu->posstr = calloc(300, sizeof (char))) || | 112 | !(retu->posstr = calloc(300, sizeof (char))) || |
113 | !(retu->msg = calloc(4096, sizeof (char))) ) | 113 | !(retu->msg = calloc(4096, sizeof (char)))) |
114 | goto FAILURE; | 114 | goto FAILURE; |
115 | 115 | ||
116 | if (id) { | 116 | if (id) { |
@@ -147,7 +147,7 @@ FAILURE: | |||
147 | 147 | ||
148 | void logger_kill(Logger *log) | 148 | void logger_kill(Logger *log) |
149 | { | 149 | { |
150 | #ifndef LOGGING /* Disabled */ | 150 | #ifndef TOX_LOGGER /* Disabled */ |
151 | return; | 151 | return; |
152 | #endif | 152 | #endif |
153 | 153 | ||
@@ -160,7 +160,7 @@ void logger_kill(Logger *log) | |||
160 | free(log->posstr); | 160 | free(log->posstr); |
161 | free(log->msg); | 161 | free(log->msg); |
162 | 162 | ||
163 | if (fclose(log->log_file) != 0 ) | 163 | if (fclose(log->log_file) != 0) |
164 | perror("Could not close log file"); | 164 | perror("Could not close log file"); |
165 | 165 | ||
166 | pthread_mutex_unlock(log->mutex); | 166 | pthread_mutex_unlock(log->mutex); |
@@ -177,7 +177,7 @@ void logger_kill_global(void) | |||
177 | 177 | ||
178 | void logger_set_global(Logger *log) | 178 | void logger_set_global(Logger *log) |
179 | { | 179 | { |
180 | #ifndef LOGGING /* Disabled */ | 180 | #ifndef TOX_LOGGER /* Disabled */ |
181 | return; | 181 | return; |
182 | #endif | 182 | #endif |
183 | 183 | ||
@@ -186,7 +186,7 @@ void logger_set_global(Logger *log) | |||
186 | 186 | ||
187 | Logger *logger_get_global(void) | 187 | Logger *logger_get_global(void) |
188 | { | 188 | { |
189 | #ifndef LOGGING /* Disabled */ | 189 | #ifndef TOX_LOGGER /* Disabled */ |
190 | return NULL; | 190 | return NULL; |
191 | #endif | 191 | #endif |
192 | 192 | ||
@@ -195,7 +195,7 @@ Logger *logger_get_global(void) | |||
195 | 195 | ||
196 | void logger_write (Logger *log, LOG_LEVEL level, const char *file, int line, const char *format, ...) | 196 | void logger_write (Logger *log, LOG_LEVEL level, const char *file, int line, const char *format, ...) |
197 | { | 197 | { |
198 | #ifndef LOGGING /* Disabled */ | 198 | #ifndef TOX_LOGGER /* Disabled */ |
199 | return; | 199 | return; |
200 | #endif | 200 | #endif |
201 | 201 | ||
diff --git a/toxcore/logger.h b/toxcore/logger.h index 0513b32c..4d3e3b54 100644 --- a/toxcore/logger.h +++ b/toxcore/logger.h | |||
@@ -43,7 +43,7 @@ typedef enum { | |||
43 | LOG_ERROR | 43 | LOG_ERROR |
44 | } LOG_LEVEL; | 44 | } LOG_LEVEL; |
45 | 45 | ||
46 | typedef struct logger Logger; | 46 | typedef struct Logger Logger; |
47 | 47 | ||
48 | /** | 48 | /** |
49 | * Set 'level' as the lowest printable level. If id == NULL, random number is used. | 49 | * Set 'level' as the lowest printable level. If id == NULL, random number is used. |
@@ -66,21 +66,22 @@ void logger_write (Logger *log, LOG_LEVEL level, const char *file, int line, con | |||
66 | 66 | ||
67 | 67 | ||
68 | /* To do some checks or similar only when logging, use this */ | 68 | /* To do some checks or similar only when logging, use this */ |
69 | #ifdef LOGGING | 69 | #ifdef TOX_LOGGER |
70 | # define LOGGER_SCOPE(__SCOPE_DO__) do { __SCOPE_DO__ } while(0) | 70 | # define LOGGER_SCOPE(__SCOPE_DO__) do { __SCOPE_DO__ } while(0) |
71 | # define LOGGER_WRITE(log, level, format, ...) \ | 71 | # define LOGGER_WRITE(log, level, format, ...) \ |
72 | logger_write(log, level, __FILE__, __LINE__, format, ##__VA_ARGS__ ) | 72 | logger_write(log, level, __FILE__, __LINE__, format, ##__VA_ARGS__) |
73 | #else | 73 | #else |
74 | /* # warning "Logging disabled" */ | ||
74 | # define LOGGER_SCOPE(__SCOPE_DO__) do {} while(0) | 75 | # define LOGGER_SCOPE(__SCOPE_DO__) do {} while(0) |
75 | # define LOGGER_WRITE(log, level, format, ...) do {} while(0) | 76 | # define LOGGER_WRITE(log, level, format, ...) do {} while(0) |
76 | #endif /* LOGGING */ | 77 | #endif /* TOX_LOGGER */ |
77 | 78 | ||
78 | /* To log with an logger */ | 79 | /* To log with an logger */ |
79 | #define LOGGER_TRACE_(log, format, ...) LOGGER_WRITE(log, LOG_TRACE, format, ##__VA_ARGS__ ) | 80 | #define LOGGER_TRACE_(log, format, ...) LOGGER_WRITE(log, LOG_TRACE, format, ##__VA_ARGS__) |
80 | #define LOGGER_DEBUG_(log, format, ...) LOGGER_WRITE(log, LOG_DEBUG, format, ##__VA_ARGS__ ) | 81 | #define LOGGER_DEBUG_(log, format, ...) LOGGER_WRITE(log, LOG_DEBUG, format, ##__VA_ARGS__) |
81 | #define LOGGER_INFO_(log, format, ...) LOGGER_WRITE(log, LOG_INFO, format, ##__VA_ARGS__ ) | 82 | #define LOGGER_INFO_(log, format, ...) LOGGER_WRITE(log, LOG_INFO, format, ##__VA_ARGS__) |
82 | #define LOGGER_WARNING_(log, format, ...) LOGGER_WRITE(log, LOG_WARNING, format, ##__VA_ARGS__ ) | 83 | #define LOGGER_WARNING_(log, format, ...) LOGGER_WRITE(log, LOG_WARNING, format, ##__VA_ARGS__) |
83 | #define LOGGER_ERROR_(log, format, ...) LOGGER_WRITE(log, LOG_ERROR, format, ##__VA_ARGS__ ) | 84 | #define LOGGER_ERROR_(log, format, ...) LOGGER_WRITE(log, LOG_ERROR, format, ##__VA_ARGS__) |
84 | 85 | ||
85 | /* To log with the global logger */ | 86 | /* To log with the global logger */ |
86 | #define LOGGER_TRACE(format, ...) LOGGER_TRACE_(NULL, format, ##__VA_ARGS__) | 87 | #define LOGGER_TRACE(format, ...) LOGGER_TRACE_(NULL, format, ##__VA_ARGS__) |
diff --git a/toxcore/network.c b/toxcore/network.c index 22ee4202..965e65f9 100644 --- a/toxcore/network.c +++ b/toxcore/network.c | |||
@@ -266,7 +266,7 @@ uint64_t current_time_monotonic(void) | |||
266 | } | 266 | } |
267 | 267 | ||
268 | /* In case no logging */ | 268 | /* In case no logging */ |
269 | #ifndef LOGGING | 269 | #ifndef TOX_LOGGER |
270 | #define loglogdata(__message__, __buffer__, __buflen__, __ip_port__, __res__) | 270 | #define loglogdata(__message__, __buffer__, __buflen__, __ip_port__, __res__) |
271 | #else | 271 | #else |
272 | #define data_0(__buflen__, __buffer__) __buflen__ > 4 ? ntohl(*(uint32_t *)&__buffer__[1]) : 0 | 272 | #define data_0(__buflen__, __buffer__) __buflen__ > 4 ? ntohl(*(uint32_t *)&__buffer__[1]) : 0 |
@@ -287,7 +287,7 @@ uint64_t current_time_monotonic(void) | |||
287 | __buffer__[0], __message__, (size_t)__res__, (!__res__ ? '!' : '>'), __buflen__, \ | 287 | __buffer__[0], __message__, (size_t)__res__, (!__res__ ? '!' : '>'), __buflen__, \ |
288 | ip_ntoa(&((__ip_port__).ip)), ntohs((__ip_port__).port), 0, "OK", data_0(__buflen__, __buffer__), data_1(__buflen__, __buffer__)); | 288 | ip_ntoa(&((__ip_port__).ip)), ntohs((__ip_port__).port), 0, "OK", data_0(__buflen__, __buffer__), data_1(__buflen__, __buffer__)); |
289 | 289 | ||
290 | #endif /* LOGGING */ | 290 | #endif /* TOX_LOGGER */ |
291 | 291 | ||
292 | /* Basic network functions: | 292 | /* Basic network functions: |
293 | * Function to send packet(data) of length length to ip_port. | 293 | * Function to send packet(data) of length length to ip_port. |
@@ -615,9 +615,9 @@ Networking_Core *new_networking_ex(IP ip, uint16_t port_from, uint16_t port_to, | |||
615 | } | 615 | } |
616 | 616 | ||
617 | if (ip.family == AF_INET6) { | 617 | if (ip.family == AF_INET6) { |
618 | #ifdef LOGGING | 618 | #ifdef TOX_LOGGER |
619 | int is_dualstack = | 619 | int is_dualstack = |
620 | #endif /* LOGGING */ | 620 | #endif /* TOX_LOGGER */ |
621 | set_socket_dualstack(temp->sock); | 621 | set_socket_dualstack(temp->sock); |
622 | LOGGER_DEBUG( "Dual-stack socket: %s", | 622 | LOGGER_DEBUG( "Dual-stack socket: %s", |
623 | is_dualstack ? "enabled" : "Failed to enable, won't be able to receive from/send to IPv4 addresses" ); | 623 | is_dualstack ? "enabled" : "Failed to enable, won't be able to receive from/send to IPv4 addresses" ); |
@@ -628,9 +628,9 @@ Networking_Core *new_networking_ex(IP ip, uint16_t port_from, uint16_t port_to, | |||
628 | mreq.ipv6mr_multiaddr.s6_addr[ 1] = 0x02; | 628 | mreq.ipv6mr_multiaddr.s6_addr[ 1] = 0x02; |
629 | mreq.ipv6mr_multiaddr.s6_addr[15] = 0x01; | 629 | mreq.ipv6mr_multiaddr.s6_addr[15] = 0x01; |
630 | mreq.ipv6mr_interface = 0; | 630 | mreq.ipv6mr_interface = 0; |
631 | #ifdef LOGGING | 631 | #ifdef TOX_LOGGER |
632 | int res = | 632 | int res = |
633 | #endif /* LOGGING */ | 633 | #endif /* TOX_LOGGER */ |
634 | setsockopt(temp->sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq)); | 634 | setsockopt(temp->sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq)); |
635 | 635 | ||
636 | LOGGER_DEBUG(res < 0 ? "Failed to activate local multicast membership. (%u, %s)" : | 636 | LOGGER_DEBUG(res < 0 ? "Failed to activate local multicast membership. (%u, %s)" : |
diff --git a/toxcore/util.c b/toxcore/util.c index 5865a172..81fa84c6 100644 --- a/toxcore/util.c +++ b/toxcore/util.c | |||
@@ -234,14 +234,6 @@ bool rb_read(RingBuffer *b, void **p) | |||
234 | b->start = (b->start + 1) % b->size; | 234 | b->start = (b->start + 1) % b->size; |
235 | return true; | 235 | return true; |
236 | } | 236 | } |
237 | void rb_clear(RingBuffer *b) | ||
238 | { | ||
239 | while (!rb_empty(b)) { | ||
240 | void *p; | ||
241 | rb_read(b, &p); | ||
242 | free(p); | ||
243 | } | ||
244 | } | ||
245 | RingBuffer *rb_new(int size) | 237 | RingBuffer *rb_new(int size) |
246 | { | 238 | { |
247 | RingBuffer *buf = calloc(sizeof(RingBuffer), 1); | 239 | RingBuffer *buf = calloc(sizeof(RingBuffer), 1); |
@@ -257,11 +249,28 @@ RingBuffer *rb_new(int size) | |||
257 | 249 | ||
258 | return buf; | 250 | return buf; |
259 | } | 251 | } |
260 | void rb_free(RingBuffer *b) | 252 | void rb_kill(RingBuffer *b) |
261 | { | 253 | { |
262 | if (b) { | 254 | if (b) { |
263 | rb_clear(b); | ||
264 | free(b->data); | 255 | free(b->data); |
265 | free(b); | 256 | free(b); |
266 | } | 257 | } |
267 | } | 258 | } |
259 | uint16_t rb_size(const RingBuffer* b) | ||
260 | { | ||
261 | if (rb_empty(b)) | ||
262 | return 0; | ||
263 | |||
264 | return | ||
265 | b->end > b->start ? | ||
266 | b->end - b->start : | ||
267 | (b->size - b->start) + b->end; | ||
268 | } | ||
269 | uint16_t rb_data(const RingBuffer* b, void** dest) | ||
270 | { | ||
271 | uint16_t i = 0; | ||
272 | for (; i < rb_size(b); i++) | ||
273 | dest[i] = b->data[(b->start + i) % b->size]; | ||
274 | |||
275 | return i; | ||
276 | } | ||
diff --git a/toxcore/util.h b/toxcore/util.h index 7670a80f..7cf63178 100644 --- a/toxcore/util.h +++ b/toxcore/util.h | |||
@@ -64,7 +64,9 @@ bool rb_full(const RingBuffer *b); | |||
64 | bool rb_empty(const RingBuffer *b); | 64 | bool rb_empty(const RingBuffer *b); |
65 | void* rb_write(RingBuffer* b, void* p); | 65 | void* rb_write(RingBuffer* b, void* p); |
66 | bool rb_read(RingBuffer* b, void** p); | 66 | bool rb_read(RingBuffer* b, void** p); |
67 | void rb_clear(RingBuffer *b); | ||
68 | RingBuffer *rb_new(int size); | 67 | RingBuffer *rb_new(int size); |
69 | void rb_free(RingBuffer *b); | 68 | void rb_kill(RingBuffer *b); |
69 | uint16_t rb_size(const RingBuffer *b); | ||
70 | uint16_t rb_data(const RingBuffer* b, void** dest); | ||
71 | |||
70 | #endif /* __UTIL_H__ */ | 72 | #endif /* __UTIL_H__ */ |