summaryrefslogtreecommitdiff
path: root/client.c
diff options
context:
space:
mode:
authorGDR! <gdr@gdr.name>2014-12-22 03:22:38 +0100
committerGDR! <gdr@gdr.name>2014-12-22 03:22:38 +0100
commitb8c1f1cf062ff681daf160411ad6c35ed7a49d42 (patch)
treeca3ccf0041c3c0542ef1e293f41a130f41d342ac /client.c
parent3f86c8bb935408fdfb056a412da23b6b181d0755 (diff)
Logging framework and command line options
Diffstat (limited to 'client.c')
-rw-r--r--client.c41
1 files changed, 21 insertions, 20 deletions
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));