summaryrefslogtreecommitdiff
path: root/testing
diff options
context:
space:
mode:
Diffstat (limited to 'testing')
-rw-r--r--testing/DHT_test.c10
-rw-r--r--testing/Makefile.inc4
-rw-r--r--testing/misc_tools.h94
-rw-r--r--testing/nTox.c84
-rw-r--r--testing/nTox.h49
5 files changed, 135 insertions, 106 deletions
diff --git a/testing/DHT_test.c b/testing/DHT_test.c
index d14f9781..09e6dfe0 100644
--- a/testing/DHT_test.c
+++ b/testing/DHT_test.c
@@ -54,10 +54,10 @@ void print_clientlist(DHT *dht)
54 IP_Port p_ip; 54 IP_Port p_ip;
55 printf("___________________CLOSE________________________________\n"); 55 printf("___________________CLOSE________________________________\n");
56 56
57 for (i = 0; i < 32; i++) { 57 for (i = 0; i < LCLIENT_LIST; i++) {
58 printf("ClientID: "); 58 printf("ClientID: ");
59 59
60 for (j = 0; j < 32; j++) { 60 for (j = 0; j < CLIENT_ID_SIZE; j++) {
61 printf("%02hhX", dht->close_clientlist[i].client_id[j]); 61 printf("%02hhX", dht->close_clientlist[i].client_id[j]);
62 } 62 }
63 63
@@ -81,7 +81,7 @@ void print_friendlist(DHT *dht)
81 printf("FRIEND %u\n", k); 81 printf("FRIEND %u\n", k);
82 printf("ID: "); 82 printf("ID: ");
83 83
84 for (j = 0; j < 32; j++) { 84 for (j = 0; j < CLIENT_ID_SIZE; j++) {
85 printf("%c", dht->friends_list[k].client_id[j]); 85 printf("%c", dht->friends_list[k].client_id[j]);
86 } 86 }
87 87
@@ -90,10 +90,10 @@ void print_friendlist(DHT *dht)
90 90
91 printf("\nCLIENTS IN LIST:\n\n"); 91 printf("\nCLIENTS IN LIST:\n\n");
92 92
93 for (i = 0; i < 4; i++) { 93 for (i = 0; i < MAX_FRIEND_CLIENTS; i++) {
94 printf("ClientID: "); 94 printf("ClientID: ");
95 95
96 for (j = 0; j < 32; j++) { 96 for (j = 0; j < CLIENT_ID_SIZE; j++) {
97 if (dht->friends_list[k].client_list[i].client_id[j] < 16) 97 if (dht->friends_list[k].client_list[i].client_id[j] < 16)
98 printf("0"); 98 printf("0");
99 99
diff --git a/testing/Makefile.inc b/testing/Makefile.inc
index 067b6ead..bd499ab6 100644
--- a/testing/Makefile.inc
+++ b/testing/Makefile.inc
@@ -13,8 +13,8 @@ nTox_CFLAGS = $(LIBSODIUM_CFLAGS) \
13nTox_LDADD = $(LIBSODIUM_LDFLAGS) \ 13nTox_LDADD = $(LIBSODIUM_LDFLAGS) \
14 libtoxcore.la \ 14 libtoxcore.la \
15 $(LIBSODIUM_LIBS) \ 15 $(LIBSODIUM_LIBS) \
16 $(NCURSES_LIBS) 16 $(NCURSES_LIBS) \
17 17 $(WINSOCK2_LIBS)
18endif 18endif
19 19
20 20
diff --git a/testing/misc_tools.h b/testing/misc_tools.h
index 1379b31a..0ffd51e7 100644
--- a/testing/misc_tools.h
+++ b/testing/misc_tools.h
@@ -26,6 +26,7 @@
26 26
27#include <stdlib.h> 27#include <stdlib.h>
28#include <stdint.h> 28#include <stdint.h>
29#include <string.h> /* for memcpy() */
29 30
30unsigned char *hex_string_to_bin(char hex_string[]); 31unsigned char *hex_string_to_bin(char hex_string[]);
31 32
@@ -33,40 +34,41 @@ unsigned char *hex_string_to_bin(char hex_string[]);
33 * wiki.tox.im/index.php/Internal_functions_and_data_structures#Debugging 34 * wiki.tox.im/index.php/Internal_functions_and_data_structures#Debugging
34 *********************************************************/ 35 *********************************************************/
35#ifdef DEBUG 36#ifdef DEBUG
36#include <assert.h> 37 #include <assert.h>
37#include <stdio.h> 38 #include <stdio.h>
38#include <string.h> 39 #include <string.h>
39 40
40#define DEBUG_PRINT(str, ...) do { \ 41 #define DEBUG_PRINT(str, ...) do { \
41 char msg[1000]; \ 42 char msg[1000]; \
42 sprintf(msg, "%s(): line %d (file %s): %s%%c\n", __FUNCTION__, __LINE__, __FILE__, str); \ 43 sprintf(msg, "%s(): line %d (file %s): %s%%c\n", __FUNCTION__, __LINE__, __FILE__, str); \
43 fprintf(stderr, msg, __VA_ARGS__); \ 44 fprintf(stderr, msg, __VA_ARGS__); \
44 } while (0) 45 } while (0)
45 46
46#define WARNING(...) do { \ 47 #define WARNING(...) do { \
47 fprintf(stderr, "warning in "); \ 48 fprintf(stderr, "warning in "); \
48 DEBUG_PRINT(__VA_ARGS__, ' '); \ 49 DEBUG_PRINT(__VA_ARGS__, ' '); \
49 } while (0) 50 } while (0)
50 51
51#define INFO(...) do { \ 52 #define INFO(...) do { \
52 DEBUG_PRINT(__VA_ARGS__, ' '); \ 53 DEBUG_PRINT(__VA_ARGS__, ' '); \
53 } while (0) 54 } while (0)
54 55
55#undef ERROR 56 #undef ERROR
56#define ERROR(exit_status, ...) do { \ 57 #define ERROR(exit_status, ...) do { \
57 fprintf(stderr, "error in "); \ 58 fprintf(stderr, "error in "); \
58 DEBUG_PRINT(__VA_ARGS__, ' '); \ 59 DEBUG_PRINT(__VA_ARGS__, ' '); \
59 exit(exit_status); \ 60 exit(exit_status); \
60 } while (0) 61 } while (0)
61#else 62#else
62#define WARNING(...) 63 #define WARNING(...)
63#define INFO(...) 64 #define INFO(...)
64#undef ERROR 65 #undef ERROR
65#define ERROR(...) 66 #define ERROR(...)
66#endif // DEBUG 67#endif // DEBUG
67 68
68/************************Linked List*********************** 69/************************Linked List***********************
69 * http://wiki.tox.im/index.php/Internal_functions_and_data_structures#Linked_List 70 * http://wiki.tox.im/index.php/Internal_functions_and_data_structures#Linked_List
71 * TODO: Update wiki.
70 **********************************************************/ 72 **********************************************************/
71 73
72#define MEMBER_OFFSET(var_name_in_parent, parent_type) \ 74#define MEMBER_OFFSET(var_name_in_parent, parent_type) \
@@ -76,22 +78,22 @@ unsigned char *hex_string_to_bin(char hex_string[]);
76 ((parent_type*)((uint64_t)(&(var)) - (uint64_t)(MEMBER_OFFSET(var_name_in_parent, parent_type)))) 78 ((parent_type*)((uint64_t)(&(var)) - (uint64_t)(MEMBER_OFFSET(var_name_in_parent, parent_type))))
77 79
78#define TOX_LIST_FOR_EACH(lst, tmp_name) \ 80#define TOX_LIST_FOR_EACH(lst, tmp_name) \
79 for (tox_list_t* tmp_name = lst.next; tmp_name != &lst; tmp_name = tmp_name->next) 81 for (tox_list* tmp_name = lst.next; tmp_name != &lst; tmp_name = tmp_name->next)
80 82
81#define TOX_LIST_GET_VALUE(tmp_name, name_in_parent, parent_type) GET_PARENT(tmp_name, name_in_parent, parent_type) 83#define TOX_LIST_GET_VALUE(tmp_name, name_in_parent, parent_type) GET_PARENT(tmp_name, name_in_parent, parent_type)
82 84
83typedef struct tox_list { 85typedef struct tox_list {
84 struct tox_list *prev, *next; 86 struct tox_list *prev, *next;
85} tox_list_t; 87} tox_list;
86 88
87/* Returns a new tox_list_t. */ 89/* Returns a new tox_list_t. */
88static inline void tox_list_new(tox_list_t *lst) 90static inline void tox_list_new(tox_list *lst)
89{ 91{
90 lst->prev = lst->next = lst; 92 lst->prev = lst->next = lst;
91} 93}
92 94
93/* Inserts a new tox_lst after lst and returns it. */ 95/* Inserts a new tox_lst after lst and returns it. */
94static inline void tox_list_add(tox_list_t *lst, tox_list_t *new_lst) 96static inline void tox_list_add(tox_list *lst, tox_list *new_lst)
95{ 97{
96 tox_list_new(new_lst); 98 tox_list_new(new_lst);
97 99
@@ -102,61 +104,55 @@ static inline void tox_list_add(tox_list_t *lst, tox_list_t *new_lst)
102 new_lst->prev = lst; 104 new_lst->prev = lst;
103} 105}
104 106
105static inline void tox_list_remove(tox_list_t *lst) 107static inline void tox_list_remove(tox_list *lst)
106{ 108{
107 lst->prev->next = lst->next; 109 lst->prev->next = lst->next;
108 lst->next->prev = lst->prev; 110 lst->next->prev = lst->prev;
109} 111}
110 112
111/****************************Array*************************** 113/****************************Array***************************
112 * Array to store pointers which tracks it's own size. 114 * Array which manages its own memory allocation.
113 * TODO: Figure out if it shold store values instead of 115 * It stores copy of data (not pointers).
114 * pointers?
115 * TODO: Add wiki info usage. 116 * TODO: Add wiki info usage.
116 ************************************************************/ 117 ************************************************************/
117 118
118struct tox_array { 119typedef struct tox_array {
119 void **data; 120 void *data;
120 uint32_t size, length; 121 uint32_t len;
121}; 122 size_t elem_size; /* in bytes */
123} tox_array;
122 124
123static inline void tox_array_init(struct tox_array *arr) 125static inline void tox_array_init(tox_array *arr, size_t elem_size)
124{ 126{
125 arr->size = 1; 127 arr->len = 0;
126 arr->length = 0; 128 arr->elem_size = elem_size;
127 arr->data = malloc(sizeof(void *)); 129 arr->data = NULL;
128} 130}
129 131
130static inline void tox_array_delete(struct tox_array *arr) 132static inline void tox_array_delete(tox_array *arr)
131{ 133{
132 free(arr->data); 134 free(arr->data);
133 arr->size = arr->length = 0; 135 arr->len = arr->elem_size = 0;
134} 136}
135 137
136/* shrinks arr so it will not have unused space. If you want to have 138static inline void _tox_array_push(tox_array *arr, uint8_t *item)
137 * addtional space, extra species the amount of extra space.
138 */
139static inline void tox_array_shrink_to_fit(struct tox_array *arr, int32_t extra)
140{
141 arr->size = arr->length + extra;
142 arr->data = realloc(arr->data, arr->size * sizeof(void *));
143}
144
145static inline void _tox_array_push(struct tox_array *arr, void *new)
146{ 139{
147 if (arr->length + 1 >= arr->size) 140 arr->data = realloc(arr->data, arr->elem_size * (arr->len+1));
148 tox_array_shrink_to_fit(arr, arr->size);
149 141
150 arr->data[arr->length++] = new; 142 memcpy(arr->data + arr->elem_size*arr->len, item, arr->elem_size);
143 arr->len++;
151} 144}
152#define tox_array_push(arr, new) _tox_array_push(arr, (void*)new) 145#define tox_array_push(arr, item) _tox_array_push(arr, (void*)(&(item)))
153 146
154static inline void *tox_array_pop(struct tox_array *arr) 147/* Deletes num items from array.
148 * Not same as pop in stacks, because to access elements you use data.
149 */
150static inline void tox_array_pop(tox_array *arr, uint32_t num)
155{ 151{
156 if (arr->length - 1 < arr->size / 4) 152 arr->len--;
157 tox_array_shrink_to_fit(arr, arr->length * 2); 153 arr->data = realloc(arr->data, arr->elem_size*arr->len);
158
159 return arr->data[arr->length--];
160} 154}
161 155
156#define tox_array_get(arr, i, type) ((type*)(arr)->data)[i]
157
162#endif // MISC_TOOLS_H 158#endif // MISC_TOOLS_H
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 {
50Friend_request pending_requests[256]; 67Friend_request pending_requests[256];
51uint8_t num_requests = 0; 68uint8_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
80uint32_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
53void get_id(Tox *m, char *data) 131void 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) {
diff --git a/testing/nTox.h b/testing/nTox.h
index df9d404a..a72ce0c2 100644
--- a/testing/nTox.h
+++ b/testing/nTox.h
@@ -27,61 +27,16 @@
27#include <stdio.h> 27#include <stdio.h>
28#include <stdlib.h> 28#include <stdlib.h>
29#include <string.h> 29#include <string.h>
30#include <ncurses.h>
31#include <curses.h> 30#include <curses.h>
32#include <ctype.h> 31#include <ctype.h>
33#include <sys/socket.h> 32
34#include <netinet/in.h>
35#include <arpa/inet.h>
36#include <sys/types.h>
37#include <netdb.h>
38#include "../toxcore/tox.h" 33#include "../toxcore/tox.h"
39 34
40#define STRING_LENGTH 256 35#define STRING_LENGTH 256
41#define HISTORY 50 36#define HISTORY 50
42#define PUB_KEY_BYTES 32 37#define PUB_KEY_BYTES 32
43 38
44/* 39uint32_t resolve_addr(const char *address);
45 resolve_addr():
46 address should represent IPv4 or a hostname with A record
47
48 returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
49 returns 0 on failure
50
51 TODO: Fix ipv6 support
52*/
53
54uint32_t resolve_addr(const char *address)
55{
56 struct addrinfo *server = NULL;
57 struct addrinfo hints;
58 int rc;
59 uint32_t addr;
60
61 memset(&hints, 0, sizeof(hints));
62 hints.ai_family = AF_INET; // IPv4 only right now.
63 hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses.
64
65 rc = getaddrinfo(address, "echo", &hints, &server);
66
67 // Lookup failed.
68 if (rc != 0) {
69 return 0;
70 }
71
72 // IPv4 records only..
73 if (server->ai_family != AF_INET) {
74 freeaddrinfo(server);
75 return 0;
76 }
77
78
79 addr = ((struct sockaddr_in *)server->ai_addr)->sin_addr.s_addr;
80
81 freeaddrinfo(server);
82 return addr;
83}
84
85void new_lines(char *line); 40void new_lines(char *line);
86void line_eval(Tox *m, char *line); 41void line_eval(Tox *m, char *line);
87void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) ; 42void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) ;