diff options
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 100 |
1 files changed, 50 insertions, 50 deletions
@@ -94,7 +94,7 @@ tunnel *tunnel_create(int sockfd, int connid, uint32_t friendnumber) | |||
94 | 94 | ||
95 | void tunnel_delete(tunnel *t) | 95 | void tunnel_delete(tunnel *t) |
96 | { | 96 | { |
97 | printf("Deleting tunnel #%d\n", t->connid); | 97 | fprintf(stderr, "Deleting tunnel #%d\n", t->connid); |
98 | if(t->sockfd) | 98 | if(t->sockfd) |
99 | { | 99 | { |
100 | close(t->sockfd); | 100 | close(t->sockfd); |
@@ -130,40 +130,21 @@ void set_tox_username(Tox *tox) | |||
130 | 130 | ||
131 | gethostname(hostname, 1024); | 131 | gethostname(hostname, 1024); |
132 | hostname[1023] = '\0'; | 132 | hostname[1023] = '\0'; |
133 | # if 0 | ||
134 | memset(&hints, 0, sizeof hints); | ||
135 | hints.ai_family = AF_UNSPEC; /*either IPV4 or IPV6*/ | ||
136 | hints.ai_socktype = SOCK_STREAM; | ||
137 | hints.ai_flags = AI_CANONNAME; | ||
138 | |||
139 | if ((gai_result = getaddrinfo(hostname, "ftp", &hints, &info)) != 0) | ||
140 | { | ||
141 | fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(gai_result)); | ||
142 | exit(1); | ||
143 | } | ||
144 | |||
145 | for(p = info; p != NULL; p = p->ai_next) | ||
146 | { | ||
147 | printf("hostname: %s\n", p->ai_canonname); | ||
148 | } | ||
149 | # endif | ||
150 | 133 | ||
151 | tox_set_name(tox, hostname, strlen(hostname)); | 134 | tox_set_name(tox, hostname, strlen(hostname)); |
152 | |||
153 | // freeaddrinfo(info); | ||
154 | } | 135 | } |
155 | // get sockaddr, IPv4 or IPv6: | 136 | |
156 | /* From Beej */ | 137 | /* Get sockaddr, IPv4 or IPv6 */ |
157 | void *get_in_addr(struct sockaddr *sa) | 138 | void *get_in_addr(struct sockaddr *sa) |
158 | { | 139 | { |
159 | if (sa->sa_family == AF_INET) { | 140 | if (sa->sa_family == AF_INET) |
141 | { | ||
160 | return &(((struct sockaddr_in*)sa)->sin_addr); | 142 | return &(((struct sockaddr_in*)sa)->sin_addr); |
161 | } | 143 | } |
162 | 144 | ||
163 | return &(((struct sockaddr_in6*)sa)->sin6_addr); | 145 | return &(((struct sockaddr_in6*)sa)->sin6_addr); |
164 | } | 146 | } |
165 | 147 | ||
166 | /* From Beej */ | ||
167 | int get_client_socket(char *hostname, int port) | 148 | int get_client_socket(char *hostname, int port) |
168 | { | 149 | { |
169 | int sockfd, numbytes; | 150 | int sockfd, numbytes; |
@@ -179,13 +160,27 @@ int get_client_socket(char *hostname, int port) | |||
179 | hints.ai_family = AF_INET; | 160 | hints.ai_family = AF_INET; |
180 | hints.ai_socktype = SOCK_STREAM; | 161 | hints.ai_socktype = SOCK_STREAM; |
181 | 162 | ||
182 | if ((rv = getaddrinfo(hostname, port_str, &hints, &servinfo)) != 0) { | 163 | if ((rv = getaddrinfo(hostname, port_str, &hints, &servinfo)) != 0) |
183 | fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); | 164 | { |
184 | return -1; | 165 | /* Add a special case for "localhost" when name resolution is broken */ |
166 | if(!strncmp("localhost", hostname, 256)) | ||
167 | { | ||
168 | const char localhostname[] = "127.0.0.1"; | ||
169 | 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 | return -1; | ||
172 | } | ||
173 | } | ||
174 | else | ||
175 | { | ||
176 | fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); | ||
177 | return -1; | ||
178 | } | ||
185 | } | 179 | } |
186 | 180 | ||
187 | // loop through all the results and connect to the first we can | 181 | // loop through all the results and connect to the first we can |
188 | for(p = servinfo; p != NULL; p = p->ai_next) { | 182 | for(p = servinfo; p != NULL; p = p->ai_next) |
183 | { | ||
189 | if (p->ai_family != AF_INET && p->ai_family != AF_INET6) | 184 | if (p->ai_family != AF_INET && p->ai_family != AF_INET6) |
190 | continue; | 185 | continue; |
191 | 186 | ||
@@ -209,8 +204,7 @@ int get_client_socket(char *hostname, int port) | |||
209 | return -1; | 204 | return -1; |
210 | } | 205 | } |
211 | 206 | ||
212 | inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), | 207 | inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s, sizeof s); |
213 | s, sizeof s); | ||
214 | fprintf(stderr, "connecting to %s\n", s); | 208 | fprintf(stderr, "connecting to %s\n", s); |
215 | 209 | ||
216 | freeaddrinfo(servinfo); // all done with this structure | 210 | freeaddrinfo(servinfo); // all done with this structure |
@@ -337,10 +331,10 @@ int handle_request_tunnel_frame(protocol_frame *rcvd_frame) | |||
337 | strncpy(hostname, rcvd_frame->data, rcvd_frame->data_length); | 331 | strncpy(hostname, rcvd_frame->data, rcvd_frame->data_length); |
338 | hostname[rcvd_frame->data_length] = '\0'; | 332 | hostname[rcvd_frame->data_length] = '\0'; |
339 | 333 | ||
340 | printf("Got a request to forward data from %s:%d\n", hostname, port); | 334 | fprintf(stderr, "Got a request to forward data from %s:%d\n", hostname, port); |
341 | 335 | ||
342 | tunnel_id = get_random_tunnel_id(); | 336 | tunnel_id = get_random_tunnel_id(); |
343 | printf("Tunnel ID: %d\n", tunnel_id); | 337 | fprintf(stderr, "Tunnel ID: %d\n", tunnel_id); |
344 | /* TODO make connection */ | 338 | /* TODO make connection */ |
345 | sockfd = get_client_socket(hostname, port); | 339 | sockfd = get_client_socket(hostname, port); |
346 | if(sockfd > 0) | 340 | if(sockfd > 0) |
@@ -380,6 +374,12 @@ int handle_client_tcp_frame(protocol_frame *rcvd_frame) | |||
380 | return -1; | 374 | return -1; |
381 | } | 375 | } |
382 | 376 | ||
377 | if(tun->friendnumber != rcvd_frame->friendnumber) | ||
378 | { | ||
379 | fprintf(stderr, "Friend #%d tried to send packet to a tunnel which belongs to #%d\n", rcvd_frame->friendnumber, tun->friendnumber); | ||
380 | return -1; | ||
381 | } | ||
382 | |||
383 | while(offset < rcvd_frame->data_length) | 383 | while(offset < rcvd_frame->data_length) |
384 | { | 384 | { |
385 | int sent_bytes; | 385 | int sent_bytes; |
@@ -388,7 +388,7 @@ int handle_client_tcp_frame(protocol_frame *rcvd_frame) | |||
388 | tun->sockfd, | 388 | tun->sockfd, |
389 | rcvd_frame->data + offset, | 389 | rcvd_frame->data + offset, |
390 | rcvd_frame->data_length - offset, | 390 | rcvd_frame->data_length - offset, |
391 | 0 | 391 | MSG_NOSIGNAL |
392 | ); | 392 | ); |
393 | 393 | ||
394 | if(sent_bytes < 0) | 394 | if(sent_bytes < 0) |
@@ -403,11 +403,6 @@ int handle_client_tcp_frame(protocol_frame *rcvd_frame) | |||
403 | return 0; | 403 | return 0; |
404 | } | 404 | } |
405 | 405 | ||
406 | int handle_server_tcp_fin_frame(protocol_frame *rcvd_frame) | ||
407 | { | ||
408 | |||
409 | } | ||
410 | |||
411 | /* Handle close-tunnel frame received from the client */ | 406 | /* Handle close-tunnel frame received from the client */ |
412 | int handle_client_tcp_fin_frame(protocol_frame *rcvd_frame) | 407 | int handle_client_tcp_fin_frame(protocol_frame *rcvd_frame) |
413 | { | 408 | { |
@@ -518,9 +513,9 @@ int parse_lossless_packet(void *sender_uc, const uint8_t *data, uint32_t len) | |||
518 | frame->packet_type = INT16_AT(data, 2); | 513 | frame->packet_type = INT16_AT(data, 2); |
519 | frame->connid = INT16_AT(data, 4); | 514 | frame->connid = INT16_AT(data, 4); |
520 | frame->data_length = INT16_AT(data, 6); | 515 | frame->data_length = INT16_AT(data, 6); |
521 | frame->data = data + PROTOCOL_BUFFER_OFFSET; | 516 | frame->data = (uint8_t *)(data + PROTOCOL_BUFFER_OFFSET); |
522 | frame->friendnumber = *((uint32_t*)sender_uc); | 517 | frame->friendnumber = *((uint32_t*)sender_uc); |
523 | printf("Got protocol frame magic 0x%x type 0x%x from friend %d\n", frame->magic, frame->packet_type, frame->friendnumber); | 518 | fprintf(stderr, "Got protocol frame magic 0x%x type 0x%x from friend %d\n", frame->magic, frame->packet_type, frame->friendnumber); |
524 | 519 | ||
525 | if(len < frame->data_length + PROTOCOL_BUFFER_OFFSET) | 520 | if(len < frame->data_length + PROTOCOL_BUFFER_OFFSET) |
526 | { | 521 | { |
@@ -650,12 +645,13 @@ void accept_friend_request(Tox *tox, const uint8_t *public_key, const uint8_t *d | |||
650 | int32_t friendnumber; | 645 | int32_t friendnumber; |
651 | int32_t *friendnumber_ptr = NULL; | 646 | int32_t *friendnumber_ptr = NULL; |
652 | 647 | ||
653 | printf("Got friend request\n"); | 648 | fprintf(stderr, "Got friend request\n"); |
654 | 649 | ||
655 | friendnumber = tox_add_friend_norequest(tox, public_key); | 650 | friendnumber = tox_add_friend_norequest(tox, public_key); |
656 | 651 | ||
652 | memset(tox_printable_id, '\0', sizeof(tox_printable_id)); | ||
657 | id_to_string(tox_printable_id, public_key); | 653 | id_to_string(tox_printable_id, public_key); |
658 | printf("Accepted friend request from %s as %d\n", tox_printable_id, friendnumber); | 654 | fprintf(stderr, "Accepted friend request from %s as %d\n", tox_printable_id, friendnumber); |
659 | 655 | ||
660 | /* TODO: this is not freed right now, we're leaking 4 bytes per contact (OMG!) */ | 656 | /* TODO: this is not freed right now, we're leaking 4 bytes per contact (OMG!) */ |
661 | friendnumber_ptr = malloc(sizeof(int32_t)); | 657 | friendnumber_ptr = malloc(sizeof(int32_t)); |
@@ -672,7 +668,7 @@ void accept_friend_request(Tox *tox, const uint8_t *public_key, const uint8_t *d | |||
672 | 668 | ||
673 | void cleanup(int status, void *tmp) | 669 | void cleanup(int status, void *tmp) |
674 | { | 670 | { |
675 | printf("kthxbye\n"); | 671 | fprintf(stderr, "kthxbye\n"); |
676 | fflush(stdout); | 672 | fflush(stdout); |
677 | tox_kill(tox); | 673 | tox_kill(tox); |
678 | if(client_socket) | 674 | if(client_socket) |
@@ -736,7 +732,7 @@ int do_server_loop() | |||
736 | char data[PROTOCOL_BUFFER_OFFSET]; | 732 | char data[PROTOCOL_BUFFER_OFFSET]; |
737 | protocol_frame frame_st, *frame; | 733 | protocol_frame frame_st, *frame; |
738 | 734 | ||
739 | printf("conn closed!\n"); | 735 | fprintf(stderr, "conn closed!\n"); |
740 | 736 | ||
741 | frame = &frame_st; | 737 | frame = &frame_st; |
742 | memset(frame, 0, sizeof(protocol_frame)); | 738 | memset(frame, 0, sizeof(protocol_frame)); |
@@ -748,7 +744,6 @@ int do_server_loop() | |||
748 | 744 | ||
749 | tunnel_delete(tun); | 745 | tunnel_delete(tun); |
750 | 746 | ||
751 | /* TODO remove tunnel? resume connection? */ | ||
752 | continue; | 747 | continue; |
753 | } | 748 | } |
754 | else | 749 | else |
@@ -785,7 +780,7 @@ int main(int argc, char *argv[]) | |||
785 | unsigned char tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1]; | 780 | unsigned char tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1]; |
786 | int oc; | 781 | int oc; |
787 | 782 | ||
788 | while ((oc = getopt(argc, argv, "L:pi:C:")) != -1) | 783 | while ((oc = getopt(argc, argv, "L:pi:C:P:")) != -1) |
789 | { | 784 | { |
790 | switch(oc) | 785 | switch(oc) |
791 | { | 786 | { |
@@ -804,8 +799,12 @@ int main(int argc, char *argv[]) | |||
804 | /* Pipe forwarding */ | 799 | /* Pipe forwarding */ |
805 | client_mode = 1; | 800 | client_mode = 1; |
806 | client_pipe_mode = 1; | 801 | client_pipe_mode = 1; |
807 | remote_port = atoi(optarg); | 802 | if(parse_pipe_port_forward(optarg, &remote_host, &remote_port) < 0) |
808 | fprintf(stderr, "Forwarding remote port %d\n", remote_port); | 803 | { |
804 | fprintf(stderr, "Invalid value for -P option - use something like -P 127.0.0.1:22\n"); | ||
805 | exit(1); | ||
806 | } | ||
807 | fprintf(stderr, "Forwarding remote port %d to stdin/out\n", remote_port); | ||
809 | break; | 808 | break; |
810 | case 'p': | 809 | case 'p': |
811 | /* Ping */ | 810 | /* Ping */ |
@@ -866,7 +865,7 @@ int main(int argc, char *argv[]) | |||
866 | tox_get_address(tox, tox_id); | 865 | tox_get_address(tox, tox_id); |
867 | id_to_string(tox_printable_id, tox_id); | 866 | id_to_string(tox_printable_id, tox_id); |
868 | tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2] = '\0'; | 867 | tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2] = '\0'; |
869 | printf("Generated Tox ID: %s\n", tox_printable_id); | 868 | fprintf(stderr, "Generated Tox ID: %s\n", tox_printable_id); |
870 | 869 | ||
871 | if(!remote_tox_id) | 870 | if(!remote_tox_id) |
872 | { | 871 | { |
@@ -886,9 +885,10 @@ int main(int argc, char *argv[]) | |||
886 | } | 885 | } |
887 | 886 | ||
888 | tox_get_address(tox, tox_id); | 887 | tox_get_address(tox, tox_id); |
888 | memset(tox_printable_id, '\0', sizeof(tox_printable_id)); | ||
889 | id_to_string(tox_printable_id, tox_id); | 889 | id_to_string(tox_printable_id, tox_id); |
890 | tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2] = '\0'; | 890 | tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2] = '\0'; |
891 | printf("Using Tox ID: %s\n", tox_printable_id); | 891 | fprintf(stderr, "Using Tox ID: %s\n", tox_printable_id); |
892 | 892 | ||
893 | tox_callback_friend_request(tox, accept_friend_request, NULL); | 893 | tox_callback_friend_request(tox, accept_friend_request, NULL); |
894 | do_server_loop(); | 894 | do_server_loop(); |