summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile5
-rw-r--r--client.c41
-rw-r--r--log.c78
-rw-r--r--log.h22
-rw-r--r--main.c165
-rw-r--r--util.c7
6 files changed, 224 insertions, 94 deletions
diff --git a/Makefile b/Makefile
index 32f6d87..09a266a 100644
--- a/Makefile
+++ b/Makefile
@@ -17,4 +17,7 @@ tuntox: $(OBJECTS) $(INCLUDES)
17cscope.out: 17cscope.out:
18 cscope -bv ./*.[ch] 18 cscope -bv ./*.[ch]
19 19
20all: cscope.out tuntox 20#gitversion.c: .git/HEAD .git/index
21# echo "const char *gitversion = \"$(shell git rev-parse HEAD)\";" > $@
22
23all: cscope.out tuntox gitversion.c
diff --git a/client.c b/client.c
index 3395757..58b31ab 100644
--- a/client.c
+++ b/client.c
@@ -1,3 +1,4 @@
1#include "log.h"
1#include "main.h" 2#include "main.h"
2#include "client.h" 3#include "client.h"
3 4
@@ -53,7 +54,7 @@ int local_bind()
53 bind_sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); 54 bind_sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
54 if(bind_sockfd < 0) 55 if(bind_sockfd < 0)
55 { 56 {
56 fprintf(stderr, "Could not create a socket for local listening: %s\n", strerror(errno)); 57 log_printf(L_ERROR, "Could not create a socket for local listening: %s\n", strerror(errno));
57 exit(1); 58 exit(1);
58 } 59 }
59 60
@@ -68,19 +69,19 @@ int local_bind()
68 69
69 if(bind(bind_sockfd, res->ai_addr, res->ai_addrlen) < 0) 70 if(bind(bind_sockfd, res->ai_addr, res->ai_addrlen) < 0)
70 { 71 {
71 fprintf(stderr, "Bind to port %d failed: %s\n", local_port, strerror(errno)); 72 log_printf(L_ERROR, "Bind to port %d failed: %s\n", local_port, strerror(errno));
72 close(bind_sockfd); 73 close(bind_sockfd);
73 exit(1); 74 exit(1);
74 } 75 }
75 76
76 if(listen(bind_sockfd, 1) < 0) 77 if(listen(bind_sockfd, 1) < 0)
77 { 78 {
78 fprintf(stderr, "Listening on port %d failed: %s\n", local_port, strerror(errno)); 79 log_printf(L_ERROR, "Listening on port %d failed: %s\n", local_port, strerror(errno));
79 close(bind_sockfd); 80 close(bind_sockfd);
80 exit(1); 81 exit(1);
81 } 82 }
82 83
83 fprintf(stderr, "Bound to local port %d\n", local_port); 84 log_printf(L_DEBUG, "Bound to local port %d\n", local_port);
84} 85}
85 86
86/* Bind the client.sockfd to a tunnel */ 87/* Bind the client.sockfd to a tunnel */
@@ -90,7 +91,7 @@ int handle_acktunnel_frame(protocol_frame *rcvd_frame)
90 91
91 if(!client_mode) 92 if(!client_mode)
92 { 93 {
93 fprintf(stderr, "Got ACK tunnel frame when not in client mode!?\n"); 94 log_printf(L_WARNING, "Got ACK tunnel frame when not in client mode!?\n");
94 return -1; 95 return -1;
95 } 96 }
96 97
@@ -111,12 +112,12 @@ int handle_acktunnel_frame(protocol_frame *rcvd_frame)
111 FD_SET(tun->sockfd, &client_master_fdset); 112 FD_SET(tun->sockfd, &client_master_fdset);
112 if(client_local_port_mode) 113 if(client_local_port_mode)
113 { 114 {
114 fprintf(stderr, "Accepted a new connection on port %d\n", local_port); 115 log_printf(L_INFO, "Accepted a new connection on port %d\n", local_port);
115 } 116 }
116 } 117 }
117 else 118 else
118 { 119 {
119 fprintf(stderr, "This tunnel mode is not supported yet\n"); 120 log_printf(L_ERROR, "This tunnel mode is not supported yet\n");
120 exit(1); 121 exit(1);
121 } 122 }
122} 123}
@@ -132,7 +133,7 @@ int handle_server_tcp_frame(protocol_frame *rcvd_frame)
132 133
133 if(!tun) 134 if(!tun)
134 { 135 {
135 fprintf(stderr, "Got TCP frame with unknown tunnel ID %d\n", rcvd_frame->connid); 136 log_printf(L_WARNING, "Got TCP frame with unknown tunnel ID %d\n", rcvd_frame->connid);
136 return -1; 137 return -1;
137 } 138 }
138 139
@@ -165,7 +166,7 @@ int handle_server_tcp_frame(protocol_frame *rcvd_frame)
165 char data[PROTOCOL_BUFFER_OFFSET]; 166 char data[PROTOCOL_BUFFER_OFFSET];
166 protocol_frame frame_st, *frame; 167 protocol_frame frame_st, *frame;
167 168
168 fprintf(stderr, "Could not write to socket %d: %s\n", write_sockfd, strerror(errno)); 169 log_printf(L_INFO, "Could not write to socket %d: %s\n", write_sockfd, strerror(errno));
169 170
170 frame = &frame_st; 171 frame = &frame_st;
171 memset(frame, 0, sizeof(protocol_frame)); 172 memset(frame, 0, sizeof(protocol_frame));
@@ -198,13 +199,13 @@ int handle_server_tcp_fin_frame(protocol_frame *rcvd_frame)
198 199
199 if(!tun) 200 if(!tun)
200 { 201 {
201 fprintf(stderr, "Got TCP FIN frame with unknown tunnel ID %d\n", rcvd_frame->connid); 202 log_printf(L_WARNING, "Got TCP FIN frame with unknown tunnel ID %d\n", rcvd_frame->connid);
202 return -1; 203 return -1;
203 } 204 }
204 205
205 if(tun->friendnumber != rcvd_frame->friendnumber) 206 if(tun->friendnumber != rcvd_frame->friendnumber)
206 { 207 {
207 fprintf(stderr, "Friend #%d tried to close tunnel while server is #%d\n", rcvd_frame->friendnumber, tun->friendnumber); 208 log_printf(L_WARNING, "Friend #%d tried to close tunnel while server is #%d\n", rcvd_frame->friendnumber, tun->friendnumber);
208 return -1; 209 return -1;
209 } 210 }
210 211
@@ -225,7 +226,7 @@ int do_client_loop(char *tox_id_str)
225 226
226 if(!string_to_id(tox_id, tox_id_str)) 227 if(!string_to_id(tox_id, tox_id_str))
227 { 228 {
228 fprintf(stderr, "Invalid Tox ID"); 229 log_printf(L_ERROR, "Invalid Tox ID");
229 exit(1); 230 exit(1);
230 } 231 }
231 232
@@ -235,7 +236,7 @@ int do_client_loop(char *tox_id_str)
235 signal(SIGPIPE, SIG_IGN); 236 signal(SIGPIPE, SIG_IGN);
236 } 237 }
237 238
238 fprintf(stderr, "Connecting to Tox...\n"); 239 log_printf(L_INFO, "Connecting to Tox...\n");
239 240
240 while(1) 241 while(1)
241 { 242 {
@@ -258,7 +259,7 @@ int do_client_loop(char *tox_id_str)
258 uint8_t data[] = "Hi, fellow tuntox instance!"; 259 uint8_t data[] = "Hi, fellow tuntox instance!";
259 uint16_t length = sizeof(data); 260 uint16_t length = sizeof(data);
260 261
261 fprintf(stderr, "Connected. Sending friend request.\n"); 262 log_printf(L_INFO, "Connected. Sending friend request.\n");
262 263
263 friendnumber = tox_add_friend( 264 friendnumber = tox_add_friend(
264 tox, 265 tox,
@@ -269,19 +270,19 @@ int do_client_loop(char *tox_id_str)
269 270
270 if(friendnumber < 0) 271 if(friendnumber < 0)
271 { 272 {
272 fprintf(stderr, "Error %d adding friend %s\n", friendnumber, tox_id); 273 log_printf(L_ERROR, "Error %d adding friend %s\n", friendnumber, tox_id);
273 exit(-1); 274 exit(-1);
274 } 275 }
275 276
276 tox_lossless_packet_registerhandler(tox, friendnumber, (PROTOCOL_MAGIC_V1)>>8, parse_lossless_packet, (void*)&friendnumber); 277 tox_lossless_packet_registerhandler(tox, friendnumber, (PROTOCOL_MAGIC_V1)>>8, parse_lossless_packet, (void*)&friendnumber);
277 state = CLIENT_STATE_SENTREQUEST; 278 state = CLIENT_STATE_SENTREQUEST;
278 fprintf(stderr, "Waiting for friend to accept us...\n"); 279 log_printf(L_INFO, "Waiting for friend to accept us...\n");
279 } 280 }
280 break; 281 break;
281 case CLIENT_STATE_SENTREQUEST: 282 case CLIENT_STATE_SENTREQUEST:
282 if(tox_get_friend_connection_status(tox, friendnumber) == 1) 283 if(tox_get_friend_connection_status(tox, friendnumber) == 1)
283 { 284 {
284 fprintf(stderr, "Friend request accepted!\n"); 285 log_printf(L_INFO, "Friend request accepted!\n");
285 state = CLIENT_STATE_REQUEST_ACCEPTED; 286 state = CLIENT_STATE_REQUEST_ACCEPTED;
286 } 287 }
287 else 288 else
@@ -327,7 +328,7 @@ int do_client_loop(char *tox_id_str)
327 case CLIENT_STATE_BIND_PORT: 328 case CLIENT_STATE_BIND_PORT:
328 if(bind_sockfd < 0) 329 if(bind_sockfd < 0)
329 { 330 {
330 fprintf(stderr, "Shutting down - could not bind to listening port\n"); 331 log_printf(L_ERROR, "Shutting down - could not bind to listening port\n");
331 state = CLIENT_STATE_SHUTDOWN; 332 state = CLIENT_STATE_SHUTDOWN;
332 } 333 }
333 else 334 else
@@ -376,7 +377,7 @@ int do_client_loop(char *tox_id_str)
376 accept_fd = accept(bind_sockfd, NULL, NULL); 377 accept_fd = accept(bind_sockfd, NULL, NULL);
377 if(accept_fd != -1) 378 if(accept_fd != -1)
378 { 379 {
379 fprintf(stderr, "Accepting a new connection - requesting tunnel...\n"); 380 log_printf(L_INFO, "Accepting a new connection - requesting tunnel...\n");
380 381
381 /* Open a new tunnel for this FD */ 382 /* Open a new tunnel for this FD */
382 client_tunnel.sockfd = accept_fd; 383 client_tunnel.sockfd = accept_fd;
@@ -415,7 +416,7 @@ int do_client_loop(char *tox_id_str)
415 char data[PROTOCOL_BUFFER_OFFSET]; 416 char data[PROTOCOL_BUFFER_OFFSET];
416 protocol_frame frame_st, *frame; 417 protocol_frame frame_st, *frame;
417 418
418 fprintf(stderr, "Connection closed\n"); 419 log_printf(L_INFO, "Connection closed\n");
419 420
420 frame = &frame_st; 421 frame = &frame_st;
421 memset(frame, 0, sizeof(protocol_frame)); 422 memset(frame, 0, sizeof(protocol_frame));
diff --git a/log.c b/log.c
new file mode 100644
index 0000000..c7627d9
--- /dev/null
+++ b/log.c
@@ -0,0 +1,78 @@
1#include <stdarg.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <time.h>
6
7#include "log.h"
8
9int min_log_level = 666;
10
11/* Turn log level number to a printable string */
12char *log_printable_level(int level)
13{
14 switch(level)
15 {
16 case L_ERROR:
17 return "ERROR";
18 case L_WARNING:
19 return "WARNING";
20 case L_NOTICE:
21 return "NOTICE";
22 case L_INFO:
23 return "INFO";
24 case L_DEBUG:
25 return "DEBUG";
26 }
27 return "UNKNOWN";
28}
29
30/* Output the log to the console */
31void log_printf(int level, const char *fmt, ...)
32{
33 va_list args;
34 char logfmt[2048];
35 char logtime[100];
36 char *level_str;
37 time_t rawtime;
38 struct tm *timeinfo;
39
40 if(level > min_log_level)
41 {
42 return;
43 }
44
45 time(&rawtime);
46 timeinfo = localtime(&rawtime);
47 strftime(logtime, 100, "%F %X", timeinfo);
48
49 level_str = log_printable_level(level);
50
51 if(fmt[strlen(fmt)-1] == '\n')
52 {
53 snprintf(logfmt, 2048, "%s: [%s]\t%s", logtime, level_str, fmt);
54 }
55 else
56 {
57 snprintf(logfmt, 2048, "%s: [%s]\t%s\n", logtime, level_str, fmt);
58 }
59
60 va_start(args, fmt);
61 vfprintf(stderr, logfmt, args);
62 va_end(args);
63}
64
65
66void log_test(void)
67{
68 int i = 112;
69 char *x = "test";
70
71 log_printf(L_WARNING, "Testing");
72 log_printf(L_ERROR, "Number stodwadziesciatrzy: %d", 123);
73 d(beenthere);
74 dd(i);
75
76 dp(&i);
77 ds(x);
78}
diff --git a/log.h b/log.h
new file mode 100644
index 0000000..25b19f7
--- /dev/null
+++ b/log.h
@@ -0,0 +1,22 @@
1#define L_ERROR 3
2#define L_WARNING 4
3#define L_NOTICE 5
4#define L_INFO 6
5#define L_DEBUG 7
6
7#define L_UNSET 0x29a
8
9void log_printf(int level, const char *fmt, ...);
10
11extern int min_log_level;
12
13#define d(x) log_printf(L_DEBUG, "%s:%d %s", __FILE__, __LINE__, #x);
14
15/* Debug-log the int variable x */
16#define dd(x) log_printf(L_DEBUG, "%s:%d %s=%d", __FILE__, __LINE__, #x, (x));
17
18/* Debug-log the pointer variable x */
19#define dp(x) log_printf(L_DEBUG, "%s:%d %s=%p", __FILE__, __LINE__, #x, (x));
20
21/* Debug-log the string variable x */
22#define ds(x) log_printf(L_DEBUG, "%s:%d %s=%s", __FILE__, __LINE__, #x, (x));
diff --git a/main.c b/main.c
index 10a7ba6..f57603e 100644
--- a/main.c
+++ b/main.c
@@ -1,6 +1,7 @@
1#include "main.h" 1#include "main.h"
2#include "client.h" 2#include "client.h"
3#include "tox_bootstrap.h" 3#include "tox_bootstrap.h"
4#include "log.h"
4 5
5static Tox_Options tox_options; 6static Tox_Options tox_options;
6Tox *tox; 7Tox *tox;
@@ -55,7 +56,7 @@ uint16_t get_random_tunnel_id()
55 { 56 {
56 return tunnel_id; 57 return tunnel_id;
57 } 58 }
58 fprintf(stderr, "[i] Found duplicated tunnel ID %d\n", key); 59 log_printf(L_WARNING, "[i] Found duplicated tunnel ID %d\n", key);
59 } 60 }
60} 61}
61 62
@@ -83,7 +84,7 @@ tunnel *tunnel_create(int sockfd, int connid, uint32_t friendnumber)
83 t->connid = connid; 84 t->connid = connid;
84 t->friendnumber = friendnumber; 85 t->friendnumber = friendnumber;
85 86
86 fprintf(stderr, "Created a new tunnel object connid=%d sockfd=%d\n", connid, sockfd); 87 log_printf(L_INFO, "Created a new tunnel object connid=%d sockfd=%d\n", connid, sockfd);
87 88
88 update_select_nfds(t->sockfd); 89 update_select_nfds(t->sockfd);
89 90
@@ -94,7 +95,7 @@ tunnel *tunnel_create(int sockfd, int connid, uint32_t friendnumber)
94 95
95void tunnel_delete(tunnel *t) 96void tunnel_delete(tunnel *t)
96{ 97{
97 fprintf(stderr, "Deleting tunnel #%d\n", t->connid); 98 log_printf(L_INFO, "Deleting tunnel #%d\n", t->connid);
98 if(t->sockfd) 99 if(t->sockfd)
99 { 100 {
100 close(t->sockfd); 101 close(t->sockfd);
@@ -167,13 +168,13 @@ int get_client_socket(char *hostname, int port)
167 { 168 {
168 const char localhostname[] = "127.0.0.1"; 169 const char localhostname[] = "127.0.0.1";
169 if ((rv = getaddrinfo(localhostname, port_str, &hints, &servinfo)) != 0) { 170 if ((rv = getaddrinfo(localhostname, port_str, &hints, &servinfo)) != 0) {
170 fprintf(stderr, "getaddrinfo failed for 127.0.0.1: %s\n", gai_strerror(rv)); 171 log_printf(L_WARNING, "getaddrinfo failed for 127.0.0.1: %s\n", gai_strerror(rv));
171 return -1; 172 return -1;
172 } 173 }
173 } 174 }
174 else 175 else
175 { 176 {
176 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); 177 log_printf(L_WARNING, "getaddrinfo: %s\n", gai_strerror(rv));
177 return -1; 178 return -1;
178 } 179 }
179 } 180 }
@@ -200,16 +201,16 @@ int get_client_socket(char *hostname, int port)
200 } 201 }
201 202
202 if (p == NULL) { 203 if (p == NULL) {
203 fprintf(stderr, "failed to connect to %s:%d\n", hostname, port); 204 log_printf(L_WARNING, "failed to connect to %s:%d\n", hostname, port);
204 return -1; 205 return -1;
205 } 206 }
206 207
207 inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s, sizeof s); 208 inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s, sizeof s);
208 fprintf(stderr, "connecting to %s\n", s); 209 log_printf(L_DEBUG, "connecting to %s\n", s);
209 210
210 freeaddrinfo(servinfo); // all done with this structure 211 freeaddrinfo(servinfo); // all done with this structure
211 212
212 fprintf(stderr, "Connected to %s:%d\n", hostname, port); 213 log_printf(L_DEBUG, "Connected to %s:%d\n", hostname, port);
213 214
214 return sockfd; 215 return sockfd;
215} 216}
@@ -251,7 +252,7 @@ int send_frame(protocol_frame *frame, uint8_t *data)
251 if(rv < 0) 252 if(rv < 0)
252 { 253 {
253 /* If this branch is ran, most likely we've hit congestion control. */ 254 /* If this branch is ran, most likely we've hit congestion control. */
254 fprintf(stderr, "[%d] Failed to send packet to friend %d\n", i, frame->friendnumber); 255 log_printf(L_DEBUG, "[%d] Failed to send packet to friend %d\n", i, frame->friendnumber);
255 } 256 }
256 else 257 else
257 { 258 {
@@ -270,7 +271,7 @@ int send_frame(protocol_frame *frame, uint8_t *data)
270 271
271 if(i > 0 && rv >= 0) 272 if(i > 0 && rv >= 0)
272 { 273 {
273 fprintf(stderr, "Packet succeeded at try %d\n", try); 274 log_printf(L_DEBUG, "Packet succeeded at try %d\n", try);
274 } 275 }
275 276
276 return rv; 277 return rv;
@@ -319,7 +320,7 @@ int handle_request_tunnel_frame(protocol_frame *rcvd_frame)
319 320
320 if(client_mode) 321 if(client_mode)
321 { 322 {
322 fprintf(stderr, "Got tunnel request frame from friend #%d when in client mode\n", rcvd_frame->friendnumber); 323 log_printf(L_WARNING, "Got tunnel request frame from friend #%d when in client mode\n", rcvd_frame->friendnumber);
323 return -1; 324 return -1;
324 } 325 }
325 326
@@ -327,17 +328,17 @@ int handle_request_tunnel_frame(protocol_frame *rcvd_frame)
327 hostname = calloc(1, rcvd_frame->data_length + 1); 328 hostname = calloc(1, rcvd_frame->data_length + 1);
328 if(!hostname) 329 if(!hostname)
329 { 330 {
330 fprintf(stderr, "Could not allocate memory for tunnel request hostname\n"); 331 log_printf(L_ERROR, "Could not allocate memory for tunnel request hostname\n");
331 return -1; 332 return -1;
332 } 333 }
333 334
334 strncpy(hostname, rcvd_frame->data, rcvd_frame->data_length); 335 strncpy(hostname, rcvd_frame->data, rcvd_frame->data_length);
335 hostname[rcvd_frame->data_length] = '\0'; 336 hostname[rcvd_frame->data_length] = '\0';
336 337
337 fprintf(stderr, "Got a request to forward data from %s:%d\n", hostname, port); 338 log_printf(L_INFO, "Got a request to forward data from %s:%d\n", hostname, port);
338 339
339 tunnel_id = get_random_tunnel_id(); 340 tunnel_id = get_random_tunnel_id();
340 fprintf(stderr, "Tunnel ID: %d\n", tunnel_id); 341 log_printf(L_DEBUG, "Tunnel ID: %d\n", tunnel_id);
341 /* TODO make connection */ 342 /* TODO make connection */
342 sockfd = get_client_socket(hostname, port); 343 sockfd = get_client_socket(hostname, port);
343 if(sockfd > 0) 344 if(sockfd > 0)
@@ -347,17 +348,17 @@ int handle_request_tunnel_frame(protocol_frame *rcvd_frame)
347 { 348 {
348 FD_SET(sockfd, &master_server_fds); 349 FD_SET(sockfd, &master_server_fds);
349 update_select_nfds(sockfd); 350 update_select_nfds(sockfd);
350 fprintf(stderr, "Created tunnel, yay!\n"); 351 log_printf(L_DEBUG, "Created tunnel, yay!\n");
351 send_tunnel_ack_frame(tun); 352 send_tunnel_ack_frame(tun);
352 } 353 }
353 else 354 else
354 { 355 {
355 fprintf(stderr, "Couldn't allocate memory for tunnel\n"); 356 log_printf(L_ERROR, "Couldn't allocate memory for tunnel\n");
356 } 357 }
357 } 358 }
358 else 359 else
359 { 360 {
360 fprintf(stderr, "Could not connect to %s:%d\n", hostname, port); 361 log_printf(L_WARNING, "Could not connect to %s:%d\n", hostname, port);
361 /* TODO send reject */ 362 /* TODO send reject */
362 } 363 }
363} 364}
@@ -373,13 +374,13 @@ int handle_client_tcp_frame(protocol_frame *rcvd_frame)
373 374
374 if(!tun) 375 if(!tun)
375 { 376 {
376 fprintf(stderr, "Got TCP frame with unknown tunnel ID %d\n", rcvd_frame->connid); 377 log_printf(L_WARNING, "Got TCP frame with unknown tunnel ID %d\n", rcvd_frame->connid);
377 return -1; 378 return -1;
378 } 379 }
379 380
380 if(tun->friendnumber != rcvd_frame->friendnumber) 381 if(tun->friendnumber != rcvd_frame->friendnumber)
381 { 382 {
382 fprintf(stderr, "Friend #%d tried to send packet to a tunnel which belongs to #%d\n", rcvd_frame->friendnumber, tun->friendnumber); 383 log_printf(L_WARNING, "Friend #%d tried to send packet to a tunnel which belongs to #%d\n", rcvd_frame->friendnumber, tun->friendnumber);
383 return -1; 384 return -1;
384 } 385 }
385 386
@@ -396,7 +397,7 @@ int handle_client_tcp_frame(protocol_frame *rcvd_frame)
396 397
397 if(sent_bytes < 0) 398 if(sent_bytes < 0)
398 { 399 {
399 fprintf(stderr, "Could not write to socket %d: %s\n", tun->sockfd, strerror(errno)); 400 log_printf(L_WARNING, "Could not write to socket %d: %s\n", tun->sockfd, strerror(errno));
400 return -1; 401 return -1;
401 } 402 }
402 403
@@ -417,13 +418,13 @@ int handle_client_tcp_fin_frame(protocol_frame *rcvd_frame)
417 418
418 if(!tun) 419 if(!tun)
419 { 420 {
420 fprintf(stderr, "Got TCP FIN frame with unknown tunnel ID %d\n", rcvd_frame->connid); 421 log_printf(L_WARNING, "Got TCP FIN frame with unknown tunnel ID %d\n", rcvd_frame->connid);
421 return -1; 422 return -1;
422 } 423 }
423 424
424 if(tun->friendnumber != rcvd_frame->friendnumber) 425 if(tun->friendnumber != rcvd_frame->friendnumber)
425 { 426 {
426 fprintf(stderr, "Friend #%d tried to close tunnel which belongs to #%d\n", rcvd_frame->friendnumber, tun->friendnumber); 427 log_printf(L_WARNING, "Friend #%d tried to close tunnel which belongs to #%d\n", rcvd_frame->friendnumber, tun->friendnumber);
427 return -1; 428 return -1;
428 } 429 }
429 430
@@ -468,7 +469,7 @@ int handle_frame(protocol_frame *frame)
468 } 469 }
469 break; 470 break;
470 default: 471 default:
471 fprintf(stderr, "Got unknown packet type 0x%x from friend %d\n", 472 log_printf(L_DEBUG, "Got unknown packet type 0x%x from friend %d\n",
472 frame->packet_type, 473 frame->packet_type,
473 frame->friendnumber 474 frame->friendnumber
474 ); 475 );
@@ -488,26 +489,26 @@ int parse_lossless_packet(void *sender_uc, const uint8_t *data, uint32_t len)
488 489
489 if(len < PROTOCOL_BUFFER_OFFSET) 490 if(len < PROTOCOL_BUFFER_OFFSET)
490 { 491 {
491 fprintf(stderr, "Received too short data frame - only %d bytes, at least %d expected\n", len, PROTOCOL_BUFFER_OFFSET); 492 log_printf(L_WARNING, "Received too short data frame - only %d bytes, at least %d expected\n", len, PROTOCOL_BUFFER_OFFSET);
492 return -1; 493 return -1;
493 } 494 }
494 495
495 if(!data) 496 if(!data)
496 { 497 {
497 fprintf(stderr, "Got NULL pointer from toxcore - WTF?\n"); 498 log_printf(L_ERROR, "Got NULL pointer from toxcore - WTF?\n");
498 return -1; 499 return -1;
499 } 500 }
500 501
501 if(data[0] != PROTOCOL_MAGIC_HIGH || data[1] != PROTOCOL_MAGIC_LOW) 502 if(data[0] != PROTOCOL_MAGIC_HIGH || data[1] != PROTOCOL_MAGIC_LOW)
502 { 503 {
503 fprintf(stderr, "Received data frame with invalid protocol magic number 0x%x%x\n", data[0], data[1]); 504 log_printf(L_WARNING, "Received data frame with invalid protocol magic number 0x%x%x\n", data[0], data[1]);
504 return -1; 505 return -1;
505 } 506 }
506 507
507 frame = calloc(1, sizeof(protocol_frame)); 508 frame = calloc(1, sizeof(protocol_frame));
508 if(!frame) 509 if(!frame)
509 { 510 {
510 fprintf(stderr, "Could not allocate memory for protocol_frame_t\n"); 511 log_printf(L_ERROR, "Could not allocate memory for protocol_frame_t\n");
511 return -1; 512 return -1;
512 } 513 }
513 514
@@ -518,17 +519,17 @@ int parse_lossless_packet(void *sender_uc, const uint8_t *data, uint32_t len)
518 frame->data_length = INT16_AT(data, 6); 519 frame->data_length = INT16_AT(data, 6);
519 frame->data = (uint8_t *)(data + PROTOCOL_BUFFER_OFFSET); 520 frame->data = (uint8_t *)(data + PROTOCOL_BUFFER_OFFSET);
520 frame->friendnumber = *((uint32_t*)sender_uc); 521 frame->friendnumber = *((uint32_t*)sender_uc);
521 fprintf(stderr, "Got protocol frame magic 0x%x type 0x%x from friend %d\n", frame->magic, frame->packet_type, frame->friendnumber); 522 log_printf(L_DEBUG, "Got protocol frame magic 0x%x type 0x%x from friend %d\n", frame->magic, frame->packet_type, frame->friendnumber);
522 523
523 if(len < frame->data_length + PROTOCOL_BUFFER_OFFSET) 524 if(len < frame->data_length + PROTOCOL_BUFFER_OFFSET)
524 { 525 {
525 fprintf(stderr, "Received frame too small (attempted buffer overflow?): %d bytes, excepted at least %d bytes\n", len, frame->data_length + PROTOCOL_BUFFER_OFFSET); 526 log_printf(L_WARNING, "Received frame too small (attempted buffer overflow?): %d bytes, excepted at least %d bytes\n", len, frame->data_length + PROTOCOL_BUFFER_OFFSET);
526 return -1; 527 return -1;
527 } 528 }
528 529
529 if(frame->data_length > (TOX_MAX_CUSTOM_PACKET_SIZE - PROTOCOL_BUFFER_OFFSET)) 530 if(frame->data_length > (TOX_MAX_CUSTOM_PACKET_SIZE - PROTOCOL_BUFFER_OFFSET))
530 { 531 {
531 fprintf(stderr, "Declared data length too big (attempted buffer overflow?): %d bytes, excepted at most %d bytes\n", frame->data_length, (TOX_MAX_CUSTOM_PACKET_SIZE - PROTOCOL_BUFFER_OFFSET)); 532 log_printf(L_WARNING, "Declared data length too big (attempted buffer overflow?): %d bytes, excepted at most %d bytes\n", frame->data_length, (TOX_MAX_CUSTOM_PACKET_SIZE - PROTOCOL_BUFFER_OFFSET));
532 return -1; 533 return -1;
533 } 534 }
534 535
@@ -541,14 +542,14 @@ int send_tunnel_request_packet(char *remote_host, int remote_port, int friend_nu
541 protocol_frame frame_i, *frame; 542 protocol_frame frame_i, *frame;
542 char *data = NULL; 543 char *data = NULL;
543 544
544 fprintf(stderr, "Sending packet to friend #%d to forward %s:%d\n", friend_number, remote_host, remote_port); 545 log_printf(L_INFO, "Sending packet to friend #%d to forward %s:%d\n", friend_number, remote_host, remote_port);
545 packet_length = PROTOCOL_BUFFER_OFFSET + strlen(remote_host); 546 packet_length = PROTOCOL_BUFFER_OFFSET + strlen(remote_host);
546 frame = &frame_i; 547 frame = &frame_i;
547 548
548 data = calloc(1, packet_length); 549 data = calloc(1, packet_length);
549 if(!data) 550 if(!data)
550 { 551 {
551 fprintf(stderr, "Could not allocate memory for tunnel request packet\n"); 552 log_printf(L_ERROR, "Could not allocate memory for tunnel request packet\n");
552 exit(1); 553 exit(1);
553 } 554 }
554 strcpy(data+PROTOCOL_BUFFER_OFFSET, remote_host); 555 strcpy(data+PROTOCOL_BUFFER_OFFSET, remote_host);
@@ -593,20 +594,20 @@ static void write_save(Tox *tox)
593 fflush(file); 594 fflush(file);
594 fclose(file); 595 fclose(file);
595 if (rename((char*)path_tmp, (char*)path_real) != 0) { 596 if (rename((char*)path_tmp, (char*)path_real) != 0) {
596 fprintf(stderr, "Failed to rename file. %s to %s deleting and trying again\n", path_tmp, path_real); 597 log_printf(L_WARNING, "Failed to rename file. %s to %s deleting and trying again\n", path_tmp, path_real);
597 remove((const char *)path_real); 598 remove((const char *)path_real);
598 if (rename((char*)path_tmp, (char*)path_real) != 0) { 599 if (rename((char*)path_tmp, (char*)path_real) != 0) {
599 fprintf(stderr, "Saving Failed\n"); 600 log_printf(L_WARNING, "Saving Failed\n");
600 } else { 601 } else {
601 fprintf(stderr, "Saved data\n"); 602 log_printf(L_DEBUG, "Saved data\n");
602 } 603 }
603 } else { 604 } else {
604 fprintf(stderr, "Saved data\n"); 605 log_printf(L_DEBUG, "Saved data\n");
605 } 606 }
606 } 607 }
607 else 608 else
608 { 609 {
609 fprintf(stderr, "Could not open save file\n"); 610 log_printf(L_WARNING, "Could not open save file\n");
610 } 611 }
611 612
612 free(data); 613 free(data);
@@ -637,7 +638,7 @@ static int load_save(Tox *tox)
637 } 638 }
638 else 639 else
639 { 640 {
640 fprintf(stderr, "Could not open save file\n"); 641 log_printf(L_WARNING, "Could not open save file\n");
641 return 0; 642 return 0;
642 } 643 }
643} 644}
@@ -648,19 +649,19 @@ void accept_friend_request(Tox *tox, const uint8_t *public_key, const uint8_t *d
648 int32_t friendnumber; 649 int32_t friendnumber;
649 int32_t *friendnumber_ptr = NULL; 650 int32_t *friendnumber_ptr = NULL;
650 651
651 fprintf(stderr, "Got friend request\n"); 652 log_printf(L_DEBUG, "Got friend request\n");
652 653
653 friendnumber = tox_add_friend_norequest(tox, public_key); 654 friendnumber = tox_add_friend_norequest(tox, public_key);
654 655
655 memset(tox_printable_id, '\0', sizeof(tox_printable_id)); 656 memset(tox_printable_id, '\0', sizeof(tox_printable_id));
656 id_to_string(tox_printable_id, public_key); 657 id_to_string(tox_printable_id, public_key);
657 fprintf(stderr, "Accepted friend request from %s as %d\n", tox_printable_id, friendnumber); 658 log_printf(L_INFO, "Accepted friend request from %s as %d\n", tox_printable_id, friendnumber);
658 659
659 /* TODO: this is not freed right now, we're leaking 4 bytes per contact (OMG!) */ 660 /* TODO: this is not freed right now, we're leaking 4 bytes per contact (OMG!) */
660 friendnumber_ptr = malloc(sizeof(int32_t)); 661 friendnumber_ptr = malloc(sizeof(int32_t));
661 if(!friendnumber_ptr) 662 if(!friendnumber_ptr)
662 { 663 {
663 fprintf(stderr, "Could not allocate memory for friendnumber_ptr\n"); 664 log_printf(L_ERROR, "Could not allocate memory for friendnumber_ptr\n");
664 return; 665 return;
665 } 666 }
666 667
@@ -671,7 +672,7 @@ void accept_friend_request(Tox *tox, const uint8_t *public_key, const uint8_t *d
671 672
672void cleanup(int status, void *tmp) 673void cleanup(int status, void *tmp)
673{ 674{
674 fprintf(stderr, "kthxbye\n"); 675 log_printf(L_DEBUG, "kthxbye\n");
675 fflush(stdout); 676 fflush(stdout);
676 tox_kill(tox); 677 tox_kill(tox);
677 if(client_socket) 678 if(client_socket)
@@ -709,11 +710,11 @@ int do_server_loop()
709 connected = tmp_isconnected; 710 connected = tmp_isconnected;
710 if(connected) 711 if(connected)
711 { 712 {
712 fprintf(stderr, "Connected to Tox network\n"); 713 log_printf(L_DEBUG, "Connected to Tox network\n");
713 } 714 }
714 else 715 else
715 { 716 {
716 fprintf(stderr, "Disconnected from Tox network\n"); 717 log_printf(L_DEBUG, "Disconnected from Tox network\n");
717 } 718 }
718 } 719 }
719 720
@@ -735,7 +736,7 @@ int do_server_loop()
735 char data[PROTOCOL_BUFFER_OFFSET]; 736 char data[PROTOCOL_BUFFER_OFFSET];
736 protocol_frame frame_st, *frame; 737 protocol_frame frame_st, *frame;
737 738
738 fprintf(stderr, "conn closed!\n"); 739 log_printf(L_WARNING, "conn closed!\n");
739 740
740 frame = &frame_st; 741 frame = &frame_st;
741 memset(frame, 0, sizeof(protocol_frame)); 742 memset(frame, 0, sizeof(protocol_frame));
@@ -775,6 +776,8 @@ void help()
775 fprintf(stderr, "-P <remotehostname>:<remoteport> - forward <remotehostname>:<remoteport> to stdin/stdout (SSH ProxyCommand mode)\n"); 776 fprintf(stderr, "-P <remotehostname>:<remoteport> - forward <remotehostname>:<remoteport> to stdin/stdout (SSH ProxyCommand mode)\n");
776 fprintf(stderr, "-p - ping the server from -i and exit\n"); 777 fprintf(stderr, "-p - ping the server from -i and exit\n");
777 fprintf(stderr, "-C <dir> - save private key in <dir> instead of /etc/tuntox in server mode\n"); 778 fprintf(stderr, "-C <dir> - save private key in <dir> instead of /etc/tuntox in server mode\n");
779 fprintf(stderr, "-d - debug mode\n");
780 fprintf(stderr, "-q - quiet mode\n");
778} 781}
779 782
780int main(int argc, char *argv[]) 783int main(int argc, char *argv[])
@@ -783,7 +786,7 @@ int main(int argc, char *argv[])
783 unsigned char tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1]; 786 unsigned char tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1];
784 int oc; 787 int oc;
785 788
786 while ((oc = getopt(argc, argv, "L:pi:C:P:")) != -1) 789 while ((oc = getopt(argc, argv, "L:pi:C:P:dq")) != -1)
787 { 790 {
788 switch(oc) 791 switch(oc)
789 { 792 {
@@ -793,10 +796,14 @@ int main(int argc, char *argv[])
793 client_local_port_mode = 1; 796 client_local_port_mode = 1;
794 if(parse_local_port_forward(optarg, &local_port, &remote_host, &remote_port) < 0) 797 if(parse_local_port_forward(optarg, &local_port, &remote_host, &remote_port) < 0)
795 { 798 {
796 fprintf(stderr, "Invalid value for -L option - use something like -L 22:127.0.0.1:22\n"); 799 log_printf(L_ERROR, "Invalid value for -L option - use something like -L 22:127.0.0.1:22\n");
797 exit(1); 800 exit(1);
798 } 801 }
799 fprintf(stderr, "Forwarding remote port %d to local port %d\n", remote_port, local_port); 802 if(min_log_level == L_UNSET)
803 {
804 min_log_level = L_INFO;
805 }
806 log_printf(L_DEBUG, "Forwarding remote port %d to local port %d\n", remote_port, local_port);
800 break; 807 break;
801 case 'P': 808 case 'P':
802 /* Pipe forwarding */ 809 /* Pipe forwarding */
@@ -804,15 +811,23 @@ int main(int argc, char *argv[])
804 client_pipe_mode = 1; 811 client_pipe_mode = 1;
805 if(parse_pipe_port_forward(optarg, &remote_host, &remote_port) < 0) 812 if(parse_pipe_port_forward(optarg, &remote_host, &remote_port) < 0)
806 { 813 {
807 fprintf(stderr, "Invalid value for -P option - use something like -P 127.0.0.1:22\n"); 814 log_printf(L_ERROR, "Invalid value for -P option - use something like -P 127.0.0.1:22\n");
808 exit(1); 815 exit(1);
809 } 816 }
810 fprintf(stderr, "Forwarding remote port %d to stdin/out\n", remote_port); 817 if(min_log_level == L_UNSET)
818 {
819 min_log_level = L_ERROR;
820 }
821 log_printf(L_INFO, "Forwarding remote port %d to stdin/out\n", remote_port);
811 break; 822 break;
812 case 'p': 823 case 'p':
813 /* Ping */ 824 /* Ping */
814 client_mode = 1; 825 client_mode = 1;
815 ping_mode = 1; 826 ping_mode = 1;
827 if(min_log_level == L_UNSET)
828 {
829 min_log_level = L_INFO;
830 }
816 break; 831 break;
817 case 'i': 832 case 'i':
818 /* Tox ID */ 833 /* Tox ID */
@@ -829,6 +844,12 @@ int main(int argc, char *argv[])
829 config_path[optarg_len + 1] = '\0'; 844 config_path[optarg_len + 1] = '\0';
830 } 845 }
831 break; 846 break;
847 case 'd':
848 min_log_level = L_DEBUG;
849 break;
850 case 'q':
851 min_log_level = L_ERROR;
852 break;
832 case '?': 853 case '?':
833 default: 854 default:
834 help(); 855 help();
@@ -844,19 +865,19 @@ int main(int argc, char *argv[])
844 tox_options.proxy_enabled = 0; 865 tox_options.proxy_enabled = 0;
845 866
846 tox = tox_new(&tox_options); 867 tox = tox_new(&tox_options);
847 if(tox == NULL) 868 if(tox == NULL)
848 { 869 {
849 fprintf(stderr, "tox_new() failed - trying without proxy\n"); 870 log_printf(L_DEBUG, "tox_new() failed - trying without proxy\n");
850 if(!tox_options.proxy_enabled || (tox_options.proxy_enabled = 0, (tox = tox_new(&tox_options)) == NULL)) 871 if(!tox_options.proxy_enabled || (tox_options.proxy_enabled = 0, (tox = tox_new(&tox_options)) == NULL))
851 { 872 {
852 fprintf(stderr, "tox_new() failed - trying without IPv6\n"); 873 log_printf(L_DEBUG, "tox_new() failed - trying without IPv6\n");
853 if(!tox_options.ipv6enabled || (tox_options.ipv6enabled = 0, (tox = tox_new(&tox_options)) == NULL)) 874 if(!tox_options.ipv6enabled || (tox_options.ipv6enabled = 0, (tox = tox_new(&tox_options)) == NULL))
854 { 875 {
855 fprintf(stderr, "tox_new() failed - exiting\n"); 876 log_printf(L_ERROR, "tox_new() failed - exiting\n");
856 exit(1); 877 exit(1);
857 } 878 }
858 } 879 }
859 } 880 }
860 881
861 set_tox_username(tox); 882 set_tox_username(tox);
862 883
@@ -868,17 +889,21 @@ int main(int argc, char *argv[])
868 tox_get_address(tox, tox_id); 889 tox_get_address(tox, tox_id);
869 id_to_string(tox_printable_id, tox_id); 890 id_to_string(tox_printable_id, tox_id);
870 tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2] = '\0'; 891 tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2] = '\0';
871 fprintf(stderr, "Generated Tox ID: %s\n", tox_printable_id); 892 log_printf(L_DEBUG, "Generated Tox ID: %s\n", tox_printable_id);
872 893
873 if(!remote_tox_id) 894 if(!remote_tox_id)
874 { 895 {
875 fprintf(stderr, "Tox id is required in client mode. Use -i 58435984ABCDEF475...\n"); 896 log_printf(L_ERROR, "Tox id is required in client mode. Use -i 58435984ABCDEF475...\n");
876 exit(1); 897 exit(1);
877 } 898 }
878 do_client_loop(remote_tox_id); 899 do_client_loop(remote_tox_id);
879 } 900 }
880 else 901 else
881 { 902 {
903 if(min_log_level == L_UNSET)
904 {
905 min_log_level = L_INFO;
906 }
882 /* Connect to the forwarded service */ 907 /* Connect to the forwarded service */
883// client_socket = get_client_socket(); 908// client_socket = get_client_socket();
884 if(!load_save(tox)) 909 if(!load_save(tox))
@@ -891,7 +916,7 @@ int main(int argc, char *argv[])
891 memset(tox_printable_id, '\0', sizeof(tox_printable_id)); 916 memset(tox_printable_id, '\0', sizeof(tox_printable_id));
892 id_to_string(tox_printable_id, tox_id); 917 id_to_string(tox_printable_id, tox_id);
893 tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2] = '\0'; 918 tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2] = '\0';
894 fprintf(stderr, "Using Tox ID: %s\n", tox_printable_id); 919 log_printf(L_INFO, "Using Tox ID: %s\n", tox_printable_id);
895 920
896 tox_callback_friend_request(tox, accept_friend_request, NULL); 921 tox_callback_friend_request(tox, accept_friend_request, NULL);
897 do_server_loop(); 922 do_server_loop();
diff --git a/util.c b/util.c
index b523236..4b7aa26 100644
--- a/util.c
+++ b/util.c
@@ -1,3 +1,4 @@
1#include "log.h"
1#include "util.h" 2#include "util.h"
2#include <string.h> 3#include <string.h>
3#include <tox/tox.h> 4#include <tox/tox.h>
@@ -134,7 +135,7 @@ void* file_raw(char *path, uint32_t *size)
134 135
135 file = fopen(path, "rb"); 136 file = fopen(path, "rb");
136 if(!file) { 137 if(!file) {
137 fprintf(stderr, "File not found (%s)\n", path); 138 log_printf(L_WARNING, "File not found (%s)\n", path);
138 return NULL; 139 return NULL;
139 } 140 }
140 141
@@ -149,7 +150,7 @@ void* file_raw(char *path, uint32_t *size)
149 fseek(file, 0, SEEK_SET); 150 fseek(file, 0, SEEK_SET);
150 151
151 if(fread(data, len, 1, file) != 1) { 152 if(fread(data, len, 1, file) != 1) {
152 fprintf(stderr, "Read error (%s)\n", path); 153 log_printf(L_WARNING, "Read error (%s)\n", path);
153 fclose(file); 154 fclose(file);
154 free(data); 155 free(data);
155 return NULL; 156 return NULL;
@@ -157,7 +158,7 @@ void* file_raw(char *path, uint32_t *size)
157 158
158 fclose(file); 159 fclose(file);
159 160
160 fprintf(stderr, "Read %u bytes (%s)\n", len, path); 161 log_printf(L_DEBUG, "Read %u bytes (%s)\n", len, path);
161 162
162 if(size) { 163 if(size) {
163 *size = len; 164 *size = len;