diff options
Diffstat (limited to 'testing/nTox.c')
-rw-r--r-- | testing/nTox.c | 84 |
1 files changed, 81 insertions, 3 deletions
diff --git a/testing/nTox.c b/testing/nTox.c index 9df1e78b..128e729d 100644 --- a/testing/nTox.c +++ b/testing/nTox.c | |||
@@ -20,13 +20,30 @@ | |||
20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | 20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. |
21 | * | 21 | * |
22 | */ | 22 | */ |
23 | #ifdef HAVE_CONFIG_H | ||
24 | #include "config.h" | ||
25 | #endif | ||
26 | |||
27 | #ifdef __WIN32__ | ||
28 | #define _WIN32_WINNT 0x501 | ||
29 | #include <winsock2.h> | ||
30 | #include <ws2tcpip.h> | ||
31 | #else | ||
32 | #include <sys/socket.h> | ||
33 | #include <netinet/in.h> | ||
34 | #include <arpa/inet.h> | ||
35 | #include <sys/types.h> | ||
36 | #include <netdb.h> | ||
37 | #endif | ||
38 | |||
39 | |||
23 | #include "nTox.h" | 40 | #include "nTox.h" |
24 | #include "misc_tools.h" | 41 | #include "misc_tools.h" |
25 | 42 | ||
26 | #include <stdio.h> | 43 | #include <stdio.h> |
27 | #include <time.h> | 44 | #include <time.h> |
28 | 45 | ||
29 | #ifdef WIN32 | 46 | #ifdef __WIN32__ |
30 | #define c_sleep(x) Sleep(1*x) | 47 | #define c_sleep(x) Sleep(1*x) |
31 | #else | 48 | #else |
32 | #include <unistd.h> | 49 | #include <unistd.h> |
@@ -50,6 +67,67 @@ typedef struct { | |||
50 | Friend_request pending_requests[256]; | 67 | Friend_request pending_requests[256]; |
51 | uint8_t num_requests = 0; | 68 | uint8_t num_requests = 0; |
52 | 69 | ||
70 | /* | ||
71 | resolve_addr(): | ||
72 | address should represent IPv4 or a hostname with A record | ||
73 | |||
74 | returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i | ||
75 | returns 0 on failure | ||
76 | |||
77 | TODO: Fix ipv6 support | ||
78 | */ | ||
79 | |||
80 | uint32_t resolve_addr(const char *address) | ||
81 | { | ||
82 | struct addrinfo *server = NULL; | ||
83 | struct addrinfo hints; | ||
84 | int rc; | ||
85 | uint32_t addr; | ||
86 | |||
87 | memset(&hints, 0, sizeof(hints)); | ||
88 | hints.ai_family = AF_INET; // IPv4 only right now. | ||
89 | hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses. | ||
90 | |||
91 | #ifdef __WIN32__ | ||
92 | int res; | ||
93 | WSADATA wsa_data; | ||
94 | |||
95 | res = WSAStartup(MAKEWORD(2, 2), &wsa_data); | ||
96 | if (res != 0) | ||
97 | { | ||
98 | return 0; | ||
99 | } | ||
100 | #endif | ||
101 | |||
102 | rc = getaddrinfo(address, "echo", &hints, &server); | ||
103 | |||
104 | // Lookup failed. | ||
105 | if (rc != 0) { | ||
106 | #ifdef __WIN32__ | ||
107 | WSACleanup(); | ||
108 | #endif | ||
109 | return 0; | ||
110 | } | ||
111 | |||
112 | // IPv4 records only.. | ||
113 | if (server->ai_family != AF_INET) { | ||
114 | freeaddrinfo(server); | ||
115 | #ifdef __WIN32__ | ||
116 | WSACleanup(); | ||
117 | #endif | ||
118 | return 0; | ||
119 | } | ||
120 | |||
121 | |||
122 | addr = ((struct sockaddr_in *)server->ai_addr)->sin_addr.s_addr; | ||
123 | |||
124 | freeaddrinfo(server); | ||
125 | #ifdef __WIN32__ | ||
126 | WSACleanup(); | ||
127 | #endif | ||
128 | return addr; | ||
129 | } | ||
130 | |||
53 | void get_id(Tox *m, char *data) | 131 | void get_id(Tox *m, char *data) |
54 | { | 132 | { |
55 | sprintf(data, "[i] ID: "); | 133 | sprintf(data, "[i] ID: "); |
@@ -525,7 +603,7 @@ int main(int argc, char *argv[]) | |||
525 | free(binary_string); | 603 | free(binary_string); |
526 | nodelay(stdscr, TRUE); | 604 | nodelay(stdscr, TRUE); |
527 | 605 | ||
528 | while (true) { | 606 | while (1) { |
529 | if (on == 0 && tox_isconnected(m)) { | 607 | if (on == 0 && tox_isconnected(m)) { |
530 | new_lines("[i] connected to DHT\n[i] define username with /n"); | 608 | new_lines("[i] connected to DHT\n[i] define username with /n"); |
531 | on = 1; | 609 | on = 1; |
@@ -542,7 +620,7 @@ int main(int argc, char *argv[]) | |||
542 | 620 | ||
543 | getmaxyx(stdscr, y, x); | 621 | getmaxyx(stdscr, y, x); |
544 | 622 | ||
545 | if (c == '\n') { | 623 | if ((c == 0x0d) || (c == 0x0a)) { |
546 | line_eval(m, line); | 624 | line_eval(m, line); |
547 | strcpy(line, ""); | 625 | strcpy(line, ""); |
548 | } else if (c == 8 || c == 127) { | 626 | } else if (c == 8 || c == 127) { |