ffi-tox 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/ProjectTox-Core/CMakeLists.txt +28 -0
  2. data/ProjectTox-Core/COPYING +674 -0
  3. data/ProjectTox-Core/INSTALL.md +91 -0
  4. data/ProjectTox-Core/README.md +54 -0
  5. data/ProjectTox-Core/core/CMakeLists.txt +17 -0
  6. data/ProjectTox-Core/core/DHT.c +1104 -0
  7. data/ProjectTox-Core/core/DHT.h +111 -0
  8. data/ProjectTox-Core/core/LAN_discovery.c +79 -0
  9. data/ProjectTox-Core/core/LAN_discovery.h +50 -0
  10. data/ProjectTox-Core/core/Lossless_UDP.c +749 -0
  11. data/ProjectTox-Core/core/Lossless_UDP.h +106 -0
  12. data/ProjectTox-Core/core/Messenger.c +581 -0
  13. data/ProjectTox-Core/core/Messenger.h +157 -0
  14. data/ProjectTox-Core/core/friend_requests.c +131 -0
  15. data/ProjectTox-Core/core/friend_requests.h +51 -0
  16. data/ProjectTox-Core/core/net_crypto.c +564 -0
  17. data/ProjectTox-Core/core/net_crypto.h +134 -0
  18. data/ProjectTox-Core/core/network.c +188 -0
  19. data/ProjectTox-Core/core/network.h +134 -0
  20. data/ProjectTox-Core/other/CMakeLists.txt +9 -0
  21. data/ProjectTox-Core/other/DHT_bootstrap.c +139 -0
  22. data/ProjectTox-Core/testing/CMakeLists.txt +18 -0
  23. data/ProjectTox-Core/testing/DHT_cryptosendfiletest.c +228 -0
  24. data/ProjectTox-Core/testing/DHT_sendfiletest.c +176 -0
  25. data/ProjectTox-Core/testing/DHT_test.c +182 -0
  26. data/ProjectTox-Core/testing/Lossless_UDP_testclient.c +214 -0
  27. data/ProjectTox-Core/testing/Lossless_UDP_testserver.c +201 -0
  28. data/ProjectTox-Core/testing/Messenger_test.c +145 -0
  29. data/ProjectTox-Core/testing/misc_tools.c +40 -0
  30. data/ProjectTox-Core/testing/misc_tools.h +29 -0
  31. data/ProjectTox-Core/testing/nTox.c +381 -0
  32. data/ProjectTox-Core/testing/nTox.h +50 -0
  33. data/ProjectTox-Core/testing/nTox_win32.c +323 -0
  34. data/ProjectTox-Core/testing/nTox_win32.h +31 -0
  35. data/ProjectTox-Core/testing/rect.py +45 -0
  36. data/ext/ffi-tox/extconf.rb +16 -0
  37. data/interfaces/libtox.i +18 -0
  38. data/lib/ffi-tox/libtox.rb +37 -0
  39. data/lib/ffi-tox.rb +1 -0
  40. metadata +116 -0
@@ -0,0 +1,188 @@
1
+ /* network.h
2
+ *
3
+ * Functions for the core networking.
4
+ *
5
+ * Copyright (C) 2013 Tox project All Rights Reserved.
6
+ *
7
+ * This file is part of Tox.
8
+ *
9
+ * Tox is free software: you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation, either version 3 of the License, or
12
+ * (at your option) any later version.
13
+ *
14
+ * Tox is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ * GNU General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU General Public License
20
+ * along with Tox. If not, see <http://www.gnu.org/licenses/>.
21
+ *
22
+ */
23
+
24
+ #include "network.h"
25
+
26
+ /* returns current UNIX time in microseconds (us). */
27
+ uint64_t current_time()
28
+ {
29
+ uint64_t time;
30
+ #ifdef WIN32
31
+ /* This probably works fine */
32
+ FILETIME ft;
33
+ GetSystemTimeAsFileTime(&ft);
34
+ time = ft.dwHighDateTime;
35
+ time <<=32;
36
+ time |= ft.dwLowDateTime;
37
+ time -= 116444736000000000UL;
38
+ return time/10;
39
+ #else
40
+ struct timeval a;
41
+ gettimeofday(&a, NULL);
42
+ time = 1000000UL*a.tv_sec + a.tv_usec;
43
+ return time;
44
+ #endif
45
+ }
46
+
47
+ /* return a random number
48
+ NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary */
49
+ uint32_t random_int()
50
+ {
51
+ #ifndef VANILLA_NACL
52
+ //NOTE: this function comes from libsodium
53
+ return randombytes_random();
54
+ #else
55
+ return random();
56
+ #endif
57
+ }
58
+
59
+ /* our UDP socket, a global variable. */
60
+ static int sock;
61
+
62
+ /* Basic network functions:
63
+ Function to send packet(data) of length length to ip_port */
64
+ int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length)
65
+ {
66
+ ADDR addr = {AF_INET, ip_port.port, ip_port.ip};
67
+ return sendto(sock,(char *) data, length, 0, (struct sockaddr *)&addr, sizeof(addr));
68
+ }
69
+
70
+ /* Function to receive data, ip and port of sender is put into ip_port
71
+ the packet data into data
72
+ the packet length into length.
73
+ dump all empty packets. */
74
+ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
75
+ {
76
+ ADDR addr;
77
+ #ifdef WIN32
78
+ int addrlen = sizeof(addr);
79
+ #else
80
+ uint32_t addrlen = sizeof(addr);
81
+ #endif
82
+ (*(int32_t*)length) = recvfrom(sock,(char*) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr*)&addr, &addrlen);
83
+ if (*(int32_t*)length <= 0)
84
+ return -1; /* nothing received or empty packet */
85
+
86
+ ip_port->ip = addr.ip;
87
+ ip_port->port = addr.port;
88
+ return 0;
89
+ }
90
+
91
+ /* initialize networking
92
+ bind to ip and port
93
+ ip must be in network order EX: 127.0.0.1 = (7F000001)
94
+ port is in host byte order (this means don't worry about it)
95
+ returns 0 if no problems
96
+ returns -1 if there are problems */
97
+ int init_networking(IP ip, uint16_t port)
98
+ {
99
+ #ifdef WIN32
100
+ WSADATA wsaData;
101
+ if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
102
+ return -1;
103
+ #else
104
+ srandom((uint32_t)current_time());
105
+ #endif
106
+ srand((uint32_t)current_time());
107
+
108
+ /* initialize our socket */
109
+ sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
110
+
111
+ /* Check for socket error */
112
+ #ifdef WIN32
113
+ if (sock == INVALID_SOCKET) /* MSDN recommends this */
114
+ return -1;
115
+ #else
116
+ if (sock < 0)
117
+ return -1;
118
+ #endif
119
+
120
+ /* Functions to increase the size of the send and receive UDP buffers
121
+ NOTE: uncomment if necessary */
122
+ /*
123
+ int n = 1024 * 1024 * 2;
124
+ if(setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char*)&n, sizeof(n)) == -1)
125
+ {
126
+ return -1;
127
+ }
128
+
129
+ if(setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char*)&n, sizeof(n)) == -1)
130
+ return -1;
131
+ */
132
+
133
+ /* Enable broadcast on socket */
134
+ int broadcast = 1;
135
+ setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&broadcast, sizeof(broadcast));
136
+
137
+ /* Set socket nonblocking */
138
+ #ifdef WIN32
139
+ /* I think this works for windows */
140
+ u_long mode = 1;
141
+ /* ioctl(sock, FIONBIO, &mode); */
142
+ ioctlsocket(sock, FIONBIO, &mode);
143
+ #else
144
+ fcntl(sock, F_SETFL, O_NONBLOCK, 1);
145
+ #endif
146
+
147
+ /* Bind our socket to port PORT and address 0.0.0.0 */
148
+ ADDR addr = {AF_INET, htons(port), ip};
149
+ bind(sock, (struct sockaddr*)&addr, sizeof(addr));
150
+
151
+ return 0;
152
+
153
+ }
154
+
155
+ /* function to cleanup networking stuff */
156
+ void shutdown_networking()
157
+ {
158
+ #ifdef WIN32
159
+ closesocket(sock);
160
+ WSACleanup();
161
+ #else
162
+ close(sock);
163
+ #endif
164
+ return;
165
+ }
166
+
167
+ /* resolves provided address to a binary data in network byte order
168
+ address is ASCII null terminated string
169
+ address should represent IPv4, IPv6 or a hostname
170
+ on success returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
171
+ on failure returns -1 */
172
+ int resolve_addr(const char *address)
173
+ {
174
+ struct addrinfo hints;
175
+ memset(&hints, 0, sizeof(hints));
176
+ hints.ai_family = AF_UNSPEC; //support both IPv4 and IPv6
177
+ hints.ai_socktype = SOCK_DGRAM; //type of socket Tox uses
178
+
179
+ struct addrinfo *server = NULL;
180
+
181
+ int success = getaddrinfo(address, "echo", &hints, &server);
182
+ if(success != 0)
183
+ return -1;
184
+
185
+ int resolved = ((struct sockaddr_in*)server->ai_addr)->sin_addr.s_addr;
186
+ freeaddrinfo(server);
187
+ return resolved;
188
+ }
@@ -0,0 +1,134 @@
1
+ /* network.h
2
+ *
3
+ * Datatypes, functions and includes for the core networking.
4
+ *
5
+ * Copyright (C) 2013 Tox project All Rights Reserved.
6
+ *
7
+ * This file is part of Tox.
8
+ *
9
+ * Tox is free software: you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation, either version 3 of the License, or
12
+ * (at your option) any later version.
13
+ *
14
+ * Tox is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ * GNU General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU General Public License
20
+ * along with Tox. If not, see <http://www.gnu.org/licenses/>.
21
+ *
22
+ */
23
+
24
+ #ifndef NETWORK_H
25
+ #define NETWORK_H
26
+
27
+ #include <stdlib.h>
28
+ #include <stdio.h>
29
+ #include <stdint.h>
30
+ #include <string.h>
31
+ #include <time.h>
32
+
33
+ #ifdef WIN32 /* Put win32 includes here */
34
+ //Windows XP
35
+ #define WINVER 0x0501
36
+ #include <winsock2.h>
37
+ #include <windows.h>
38
+ #include <ws2tcpip.h>
39
+
40
+ #undef VANILLA_NACL /* make sure on windows we use libsodium */
41
+
42
+ #else //Linux includes
43
+
44
+ #include <fcntl.h>
45
+ #include <sys/socket.h>
46
+ #include <netinet/in.h>
47
+ #include <errno.h>
48
+ #include <sys/time.h>
49
+ #include <sys/types.h>
50
+ #include <netdb.h>
51
+ #include <unistd.h>
52
+
53
+ #endif
54
+
55
+ #ifndef VANILLA_NACL
56
+ /* we use libsodium by default */
57
+ #include <sodium.h>
58
+ #else
59
+
60
+ /* TODO: Including stuff like this is bad. This needs fixing.
61
+ We keep support for the original NaCl for now. */
62
+ #include "../nacl/build/Linux/include/amd64/crypto_box.h"
63
+
64
+ #endif
65
+
66
+ #ifdef __cplusplus
67
+ extern "C" {
68
+ #endif
69
+
70
+ #define MAX_UDP_PACKET_SIZE 65507
71
+
72
+ typedef union {
73
+ uint8_t c[4];
74
+ uint16_t s[2];
75
+ uint32_t i;
76
+ } IP;
77
+
78
+ typedef struct {
79
+ IP ip;
80
+ uint16_t port;
81
+ /* not used for anything right now */
82
+ uint16_t padding;
83
+ } IP_Port;
84
+
85
+ typedef struct {
86
+ int16_t family;
87
+ uint16_t port;
88
+ IP ip;
89
+ uint8_t zeroes[8];
90
+ #ifdef ENABLE_IPV6
91
+ uint8_t zeroes2[12];
92
+ #endif
93
+ } ADDR;
94
+
95
+ /* returns current time in milleseconds since the epoch. */
96
+ uint64_t current_time();
97
+
98
+ /* return a random number
99
+ NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary */
100
+ uint32_t random_int();
101
+
102
+ /* Basic network functions: */
103
+
104
+ /* Function to send packet(data) of length length to ip_port */
105
+ int sendpacket(IP_Port ip_port, uint8_t *data, uint32_t length);
106
+
107
+ /* Function to receive data, ip and port of sender is put into ip_port
108
+ the packet data into data
109
+ the packet length into length. */
110
+ int receivepacket(IP_Port *ip_port, uint8_t *data, uint32_t *length);
111
+
112
+ /* initialize networking
113
+ bind to ip and port
114
+ ip must be in network order EX: 127.0.0.1 = (7F000001)
115
+ port is in host byte order (this means don't worry about it)
116
+ returns 0 if no problems
117
+ returns -1 if there were problems */
118
+ int init_networking(IP ip, uint16_t port);
119
+
120
+ /* function to cleanup networking stuff(doesn't do much right now) */
121
+ void shutdown_networking();
122
+
123
+ /* resolves provided address to a binary data in network byte order
124
+ address is ASCII null terminated string
125
+ address should represent IPv4, IPv6 or a hostname
126
+ on success returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
127
+ on failure returns -1 */
128
+ int resolve_addr(const char *address);
129
+
130
+ #ifdef __cplusplus
131
+ }
132
+ #endif
133
+
134
+ #endif
@@ -0,0 +1,9 @@
1
+ cmake_minimum_required(VERSION 2.6.0)
2
+
3
+ cmake_policy(SET CMP0011 NEW)
4
+
5
+ include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/DHT_bootstrap.cmake)
6
+
7
+ if(NOT WIN32)
8
+ add_subdirectory(bootstrap_serverdaemon)
9
+ endif()
@@ -0,0 +1,139 @@
1
+ /* DHT boostrap
2
+ *
3
+ * A simple DHT boostrap server for tox.
4
+ *
5
+ * Build commands (use one or the other):
6
+ * gcc -O2 -Wall -D VANILLA_NACL -o bootstrap_server ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/Messenger.c ../core/DHT.c ../core/friend_requests.c ../nacl/build/${HOSTNAME%.*}/lib/amd64/{cpucycles.o,libnacl.a,randombytes.o} DHT_bootstrap.c
7
+ *
8
+ * gcc -O2 -Wall -o bootstrap_server ../core/Lossless_UDP.c ../core/network.c ../core/net_crypto.c ../core/Messenger.c ../core/DHT.c ../core/friend_requests.c -lsodium DHT_bootstrap.c
9
+ *
10
+ *
11
+ * Copyright (C) 2013 Tox project All Rights Reserved.
12
+ *
13
+ * This file is part of Tox.
14
+ *
15
+ * Tox is free software: you can redistribute it and/or modify
16
+ * it under the terms of the GNU General Public License as published by
17
+ * the Free Software Foundation, either version 3 of the License, or
18
+ * (at your option) any later version.
19
+ *
20
+ * Tox is distributed in the hope that it will be useful,
21
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
+ * GNU General Public License for more details.
24
+ *
25
+ * You should have received a copy of the GNU General Public License
26
+ * along with Tox. If not, see <http://www.gnu.org/licenses/>.
27
+ *
28
+ */
29
+
30
+ #include "../core/DHT.h"
31
+ #include "../core/friend_requests.h"
32
+ #include "../testing/misc_tools.h"
33
+
34
+ //Sleep function (x = milliseconds)
35
+ #ifdef WIN32
36
+ #define c_sleep(x) Sleep(1*x)
37
+ #else
38
+ #include <unistd.h>
39
+ #include <arpa/inet.h>
40
+ #define c_sleep(x) usleep(1000*x)
41
+ #endif
42
+
43
+ #define PORT 33445
44
+
45
+
46
+
47
+ void manage_keys()
48
+ {
49
+ const uint32_t KEYS_SIZE = crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES;
50
+ uint8_t keys[KEYS_SIZE];
51
+
52
+ FILE *keys_file = fopen("key", "r");
53
+ if (keys_file != NULL) {
54
+ //if file was opened successfully -- load keys
55
+ size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
56
+ if (read_size != KEYS_SIZE) {
57
+ printf("Error while reading the key file\nExiting.\n");
58
+ exit(1);
59
+ }
60
+ load_keys(keys);
61
+ printf("Keys loaded successfully\n");
62
+ } else {
63
+ //otherwise save new keys
64
+ new_keys();
65
+ save_keys(keys);
66
+ keys_file = fopen("key", "w");
67
+ if (fwrite(keys, sizeof(uint8_t), KEYS_SIZE, keys_file) != KEYS_SIZE) {
68
+ printf("Error while writing the key file.\nExiting.\n");
69
+ exit(1);
70
+ }
71
+ printf("Keys saved successfully\n");
72
+ }
73
+
74
+ fclose(keys_file);
75
+ }
76
+
77
+ int main(int argc, char *argv[])
78
+ {
79
+ manage_keys();
80
+ printf("Public key: ");
81
+ uint32_t i;
82
+
83
+ FILE *file;
84
+ file = fopen("PUBLIC_ID.txt", "w");
85
+
86
+ for(i = 0; i < 32; i++)
87
+ {
88
+ if(self_public_key[i] < 16)
89
+ printf("0");
90
+ printf("%hhX",self_public_key[i]);
91
+ fprintf(file, "%hhX",self_public_key[i]);
92
+ }
93
+
94
+ fclose(file);
95
+
96
+ printf("\n");
97
+ printf("Port: %u\n", PORT);
98
+ //initialize networking
99
+ //bind to ip 0.0.0.0:PORT
100
+ IP ip;
101
+ ip.i = 0;
102
+ init_networking(ip, PORT);
103
+
104
+ perror("Initialization");
105
+
106
+ if (argc > 3) {
107
+ printf("Trying to bootstrap into the network...\n");
108
+ IP_Port bootstrap_info;
109
+ bootstrap_info.ip.i = inet_addr(argv[1]);
110
+ bootstrap_info.port = htons(atoi(argv[2]));
111
+ uint8_t *bootstrap_key = hex_string_to_bin(argv[3]);
112
+ DHT_bootstrap(bootstrap_info, bootstrap_key);
113
+ free(bootstrap_key);
114
+ }
115
+
116
+ IP_Port ip_port;
117
+ uint8_t data[MAX_UDP_PACKET_SIZE];
118
+ uint32_t length;
119
+
120
+ int is_waiting_for_dht_connection = 1;
121
+ while(1)
122
+ {
123
+ if (is_waiting_for_dht_connection && DHT_isconnected())
124
+ {
125
+ printf("Connected to other bootstrap server successfully.\n");
126
+ is_waiting_for_dht_connection = 0;
127
+ }
128
+ doDHT();
129
+
130
+ while(receivepacket(&ip_port, data, &length) != -1)
131
+ {
132
+ DHT_handlepacket(data, length, ip_port);
133
+ friendreq_handlepacket(data, length, ip_port);
134
+ }
135
+ c_sleep(1);
136
+ }
137
+ shutdown_networking();
138
+ return 0;
139
+ }
@@ -0,0 +1,18 @@
1
+ cmake_minimum_required(VERSION 2.6.0)
2
+
3
+ cmake_policy(SET CMP0011 NEW)
4
+
5
+ #include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/DHT_cryptosendfiletest.cmake)
6
+ #include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/DHT_sendfiletest.cmake)
7
+ include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/DHT_test.cmake)
8
+ include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Lossless_UDP_testclient.cmake)
9
+ include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Lossless_UDP_testserver.cmake)
10
+ include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Messenger_test.cmake)
11
+ if(WIN32)
12
+ include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/nTox_win32.cmake)
13
+ endif()
14
+
15
+ if(NOT WIN32)
16
+ include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/nTox.cmake)
17
+ include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/toxic.cmake)
18
+ endif()