ffi-tox 0.1.1 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ProjectTox-Core/AUTHORS +0 -0
- data/ProjectTox-Core/ChangeLog +0 -0
- data/ProjectTox-Core/INSTALL +370 -0
- data/ProjectTox-Core/INSTALL.md +455 -56
- data/ProjectTox-Core/Makefile.am +35 -0
- data/ProjectTox-Core/NEWS +0 -0
- data/ProjectTox-Core/README +43 -0
- data/ProjectTox-Core/README.md +34 -44
- data/ProjectTox-Core/auto_tests/Makefile.inc +110 -0
- data/ProjectTox-Core/auto_tests/TCP_test.c +519 -0
- data/ProjectTox-Core/auto_tests/assoc_test.c +160 -0
- data/ProjectTox-Core/auto_tests/crypto_test.c +302 -0
- data/ProjectTox-Core/auto_tests/dht_test.c +362 -0
- data/ProjectTox-Core/auto_tests/encryptsave_test.c +104 -0
- data/ProjectTox-Core/auto_tests/friends_test.c +238 -0
- data/ProjectTox-Core/auto_tests/helpers.h +15 -0
- data/ProjectTox-Core/auto_tests/messenger_test.c +365 -0
- data/ProjectTox-Core/auto_tests/network_test.c +171 -0
- data/ProjectTox-Core/auto_tests/onion_test.c +363 -0
- data/ProjectTox-Core/auto_tests/skeleton_test.c +49 -0
- data/ProjectTox-Core/auto_tests/tox_test.c +454 -0
- data/ProjectTox-Core/auto_tests/toxav_basic_test.c +597 -0
- data/ProjectTox-Core/auto_tests/toxav_many_test.c +402 -0
- data/ProjectTox-Core/autogen.sh +6 -0
- data/ProjectTox-Core/build/Makefile.am +14 -0
- data/ProjectTox-Core/configure.ac +694 -0
- data/ProjectTox-Core/dist-build/android-arm.sh +3 -0
- data/ProjectTox-Core/dist-build/android-armv7.sh +3 -0
- data/ProjectTox-Core/dist-build/android-build.sh +59 -0
- data/ProjectTox-Core/dist-build/android-mips.sh +3 -0
- data/ProjectTox-Core/dist-build/android-x86.sh +3 -0
- data/ProjectTox-Core/docs/Group-Chats.md +71 -0
- data/ProjectTox-Core/docs/Hardening.txt +60 -0
- data/ProjectTox-Core/docs/Hardening_docs.txt +30 -0
- data/ProjectTox-Core/docs/Prevent_Tracking.txt +160 -0
- data/ProjectTox-Core/docs/TCP_Network.txt +154 -0
- data/ProjectTox-Core/docs/TODO +62 -0
- data/ProjectTox-Core/docs/Tox_middle_level_network_protocol.txt +120 -0
- data/ProjectTox-Core/docs/av_api.md +194 -0
- data/ProjectTox-Core/libtoxav.pc.in +11 -0
- data/ProjectTox-Core/libtoxcore.pc.in +11 -0
- data/ProjectTox-Core/m4/ax_have_epoll.m4 +104 -0
- data/ProjectTox-Core/m4/ax_pthread.m4 +317 -0
- data/ProjectTox-Core/m4/pkg.m4 +199 -0
- data/ProjectTox-Core/other/DHT_bootstrap.c +121 -58
- data/ProjectTox-Core/other/DHTnodes +3 -0
- data/ProjectTox-Core/other/Makefile.inc +20 -0
- data/ProjectTox-Core/other/bootstrap_node_packets.c +65 -0
- data/ProjectTox-Core/other/tox.png +0 -0
- data/ProjectTox-Core/testing/DHT_test.c +170 -98
- data/ProjectTox-Core/testing/Makefile.inc +112 -0
- data/ProjectTox-Core/testing/Messenger_test.c +133 -69
- data/ProjectTox-Core/testing/dns3_test.c +115 -0
- data/ProjectTox-Core/testing/misc_tools.c +59 -13
- data/ProjectTox-Core/testing/nTox.c +1127 -264
- data/ProjectTox-Core/testing/nTox.h +10 -19
- data/ProjectTox-Core/testing/tox_shell.c +159 -0
- data/ProjectTox-Core/testing/tox_sync.c +299 -0
- data/ProjectTox-Core/tools/README +11 -0
- data/ProjectTox-Core/tools/astylerc +11 -0
- data/ProjectTox-Core/tools/pre-commit +17 -0
- data/ProjectTox-Core/toxav/Makefile.inc +36 -0
- data/ProjectTox-Core/toxav/codec.c +357 -0
- data/ProjectTox-Core/toxav/codec.h +116 -0
- data/ProjectTox-Core/toxav/msi.c +1949 -0
- data/ProjectTox-Core/toxav/msi.h +267 -0
- data/ProjectTox-Core/toxav/rtp.c +600 -0
- data/ProjectTox-Core/toxav/rtp.h +196 -0
- data/ProjectTox-Core/toxav/toxav.c +1148 -0
- data/ProjectTox-Core/toxav/toxav.h +389 -0
- data/ProjectTox-Core/toxcore/DHT.c +2521 -0
- data/ProjectTox-Core/toxcore/DHT.h +412 -0
- data/ProjectTox-Core/toxcore/LAN_discovery.c +322 -0
- data/ProjectTox-Core/{core → toxcore}/LAN_discovery.h +17 -12
- data/ProjectTox-Core/toxcore/Makefile.inc +67 -0
- data/ProjectTox-Core/toxcore/Messenger.c +3006 -0
- data/ProjectTox-Core/toxcore/Messenger.h +818 -0
- data/ProjectTox-Core/toxcore/TCP_client.c +858 -0
- data/ProjectTox-Core/toxcore/TCP_client.h +156 -0
- data/ProjectTox-Core/toxcore/TCP_server.c +1332 -0
- data/ProjectTox-Core/toxcore/TCP_server.h +181 -0
- data/ProjectTox-Core/toxcore/assoc.c +1033 -0
- data/ProjectTox-Core/toxcore/assoc.h +104 -0
- data/ProjectTox-Core/toxcore/crypto_core.c +278 -0
- data/ProjectTox-Core/toxcore/crypto_core.h +151 -0
- data/ProjectTox-Core/toxcore/friend_requests.c +175 -0
- data/ProjectTox-Core/toxcore/friend_requests.h +83 -0
- data/ProjectTox-Core/toxcore/group_chats.c +837 -0
- data/ProjectTox-Core/toxcore/group_chats.h +199 -0
- data/ProjectTox-Core/toxcore/list.c +256 -0
- data/ProjectTox-Core/toxcore/list.h +85 -0
- data/ProjectTox-Core/toxcore/logger.c +153 -0
- data/ProjectTox-Core/toxcore/logger.h +84 -0
- data/ProjectTox-Core/toxcore/misc_tools.h +70 -0
- data/ProjectTox-Core/toxcore/net_crypto.c +2753 -0
- data/ProjectTox-Core/toxcore/net_crypto.h +410 -0
- data/ProjectTox-Core/toxcore/network.c +979 -0
- data/ProjectTox-Core/toxcore/network.h +367 -0
- data/ProjectTox-Core/toxcore/onion.c +540 -0
- data/ProjectTox-Core/toxcore/onion.h +150 -0
- data/ProjectTox-Core/toxcore/onion_announce.c +433 -0
- data/ProjectTox-Core/toxcore/onion_announce.h +139 -0
- data/ProjectTox-Core/toxcore/onion_client.c +1347 -0
- data/ProjectTox-Core/toxcore/onion_client.h +253 -0
- data/ProjectTox-Core/toxcore/ping.c +346 -0
- data/ProjectTox-Core/toxcore/ping.h +47 -0
- data/ProjectTox-Core/toxcore/ping_array.c +162 -0
- data/ProjectTox-Core/toxcore/ping_array.h +75 -0
- data/ProjectTox-Core/toxcore/tox.c +940 -0
- data/ProjectTox-Core/toxcore/tox.h +734 -0
- data/ProjectTox-Core/toxcore/util.c +193 -0
- data/ProjectTox-Core/toxcore/util.h +63 -0
- data/ProjectTox-Core/toxdns/Makefile.inc +29 -0
- data/ProjectTox-Core/toxdns/toxdns.c +238 -0
- data/ProjectTox-Core/toxdns/toxdns.h +88 -0
- data/ProjectTox-Core/toxencryptsave/Makefile.inc +45 -0
- data/ProjectTox-Core/toxencryptsave/toxencryptsave.c +179 -0
- data/ProjectTox-Core/toxencryptsave/toxencryptsave.h +74 -0
- data/interfaces/libtox.i +2 -6
- data/lib/ffi-tox/libtox.rb +406 -28
- metadata +124 -46
- data/ProjectTox-Core/CMakeLists.txt +0 -50
- data/ProjectTox-Core/cmake/FindLIBCONFIG.cmake +0 -15
- data/ProjectTox-Core/cmake/FindNaCl.cmake +0 -17
- data/ProjectTox-Core/cmake/FindSODIUM.cmake +0 -15
- data/ProjectTox-Core/core/CMakeLists.txt +0 -19
- data/ProjectTox-Core/core/DHT.c +0 -1104
- data/ProjectTox-Core/core/DHT.h +0 -111
- data/ProjectTox-Core/core/LAN_discovery.c +0 -79
- data/ProjectTox-Core/core/Lossless_UDP.c +0 -755
- data/ProjectTox-Core/core/Lossless_UDP.h +0 -106
- data/ProjectTox-Core/core/Messenger.c +0 -596
- data/ProjectTox-Core/core/Messenger.h +0 -165
- data/ProjectTox-Core/core/friend_requests.c +0 -131
- data/ProjectTox-Core/core/friend_requests.h +0 -51
- data/ProjectTox-Core/core/net_crypto.c +0 -575
- data/ProjectTox-Core/core/net_crypto.h +0 -134
- data/ProjectTox-Core/core/network.c +0 -205
- data/ProjectTox-Core/core/network.h +0 -134
- data/ProjectTox-Core/docs/commands.md +0 -25
- data/ProjectTox-Core/docs/start_guide.de.md +0 -40
- data/ProjectTox-Core/docs/start_guide.md +0 -38
- data/ProjectTox-Core/other/CMakeLists.txt +0 -9
- data/ProjectTox-Core/testing/CMakeLists.txt +0 -18
- data/ProjectTox-Core/testing/DHT_cryptosendfiletest.c +0 -228
- data/ProjectTox-Core/testing/DHT_sendfiletest.c +0 -176
- data/ProjectTox-Core/testing/Lossless_UDP_testclient.c +0 -214
- data/ProjectTox-Core/testing/Lossless_UDP_testserver.c +0 -201
- data/ProjectTox-Core/testing/misc_tools.h +0 -29
- data/ProjectTox-Core/testing/nTox_win32.c +0 -387
- data/ProjectTox-Core/testing/nTox_win32.h +0 -40
- data/ProjectTox-Core/testing/rect.py +0 -45
@@ -1,134 +0,0 @@
|
|
1
|
-
/* net_crypto.h
|
2
|
-
*
|
3
|
-
* Functions for the core network crypto.
|
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 NET_CRYPTO_H
|
25
|
-
#define NET_CRYPTO_H
|
26
|
-
|
27
|
-
#include "Lossless_UDP.h"
|
28
|
-
|
29
|
-
#ifdef __cplusplus
|
30
|
-
extern "C" {
|
31
|
-
#endif
|
32
|
-
|
33
|
-
/* Our public key. */
|
34
|
-
extern uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
|
35
|
-
extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
|
36
|
-
|
37
|
-
#define ENCRYPTION_PADDING (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES)
|
38
|
-
|
39
|
-
/* encrypts plain of length length to encrypted of length + 16 using the
|
40
|
-
public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce
|
41
|
-
return -1 if there was a problem.
|
42
|
-
return length of encrypted data if everything was fine. */
|
43
|
-
int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
|
44
|
-
uint8_t *plain, uint32_t length, uint8_t *encrypted);
|
45
|
-
|
46
|
-
|
47
|
-
/* decrypts encrypted of length length to plain of length length - 16 using the
|
48
|
-
public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce
|
49
|
-
return -1 if there was a problem(decryption failed)
|
50
|
-
return length of plain data if everything was fine. */
|
51
|
-
int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
|
52
|
-
uint8_t *encrypted, uint32_t length, uint8_t *plain);
|
53
|
-
|
54
|
-
|
55
|
-
/* fill the given nonce with random bytes. */
|
56
|
-
void random_nonce(uint8_t *nonce);
|
57
|
-
|
58
|
-
/* return 0 if there is no received data in the buffer
|
59
|
-
return -1 if the packet was discarded.
|
60
|
-
return length of received data if successful */
|
61
|
-
int read_cryptpacket(int crypt_connection_id, uint8_t *data);
|
62
|
-
|
63
|
-
/* return 0 if data could not be put in packet queue
|
64
|
-
return 1 if data was put into the queue */
|
65
|
-
int write_cryptpacket(int crypt_connection_id, uint8_t *data, uint32_t length);
|
66
|
-
|
67
|
-
/* create a request to peer with public_key.
|
68
|
-
packet must be an array of MAX_DATA_SIZE big.
|
69
|
-
Data represents the data we send with the request with length being the length of the data.
|
70
|
-
request_id is the id of the request (32 = friend request, 254 = ping request)
|
71
|
-
returns -1 on failure
|
72
|
-
returns the length of the created packet on success */
|
73
|
-
int create_request(uint8_t *packet, uint8_t * public_key, uint8_t *data, uint32_t length, uint8_t request_id);
|
74
|
-
|
75
|
-
/* puts the senders public key in the request in public_key, the data from the request
|
76
|
-
in data if a friend or ping request was sent to us and returns the length of the data.
|
77
|
-
packet is the request packet and length is its length
|
78
|
-
return -1 if not valid request. */
|
79
|
-
int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *packet, uint16_t length);
|
80
|
-
|
81
|
-
/* Start a secure connection with other peer who has public_key and ip_port
|
82
|
-
returns -1 if failure
|
83
|
-
returns crypt_connection_id of the initialized connection if everything went well. */
|
84
|
-
int crypto_connect(uint8_t *public_key, IP_Port ip_port);
|
85
|
-
|
86
|
-
/* kill a crypto connection
|
87
|
-
return 0 if killed successfully
|
88
|
-
return 1 if there was a problem. */
|
89
|
-
int crypto_kill(int crypt_connection_id);
|
90
|
-
|
91
|
-
/* handle an incoming connection
|
92
|
-
return -1 if no crypto inbound connection
|
93
|
-
return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection
|
94
|
-
Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce
|
95
|
-
and the session public key for the connection in session_key
|
96
|
-
to accept it see: accept_crypto_inbound(...)
|
97
|
-
to refuse it just call kill_connection(...) on the connection id */
|
98
|
-
int crypto_inbound(uint8_t *public_key, uint8_t * secret_nonce, uint8_t *session_key);
|
99
|
-
|
100
|
-
/* accept an incoming connection using the parameters provided by crypto_inbound
|
101
|
-
return -1 if not successful
|
102
|
-
returns the crypt_connection_id if successful */
|
103
|
-
int accept_crypto_inbound(int connection_id, uint8_t *public_key, uint8_t * secret_nonce, uint8_t *session_key);
|
104
|
-
|
105
|
-
/* return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet
|
106
|
-
(we have received a handshake but no empty data packet), 3 if the connection is established.
|
107
|
-
4 if the connection is timed out and waiting to be killed */
|
108
|
-
int is_cryptoconnected(int crypt_connection_id);
|
109
|
-
|
110
|
-
|
111
|
-
/* Generate our public and private keys
|
112
|
-
Only call this function the first time the program starts. */
|
113
|
-
void new_keys();
|
114
|
-
|
115
|
-
/* save the public and private keys to the keys array
|
116
|
-
Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */
|
117
|
-
void save_keys(uint8_t * keys);
|
118
|
-
|
119
|
-
/* load the public and private keys from the keys array
|
120
|
-
Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */
|
121
|
-
void load_keys(uint8_t * keys);
|
122
|
-
|
123
|
-
/* run this to (re)initialize net_crypto
|
124
|
-
sets all the global connection variables to their default values. */
|
125
|
-
void initNetCrypto();
|
126
|
-
|
127
|
-
/* main loop */
|
128
|
-
void doNetCrypto();
|
129
|
-
|
130
|
-
#ifdef __cplusplus
|
131
|
-
}
|
132
|
-
#endif
|
133
|
-
|
134
|
-
#endif
|
@@ -1,205 +0,0 @@
|
|
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
|
-
/*
|
168
|
-
resolve_addr():
|
169
|
-
address should represent IPv4 or a hostname with A record
|
170
|
-
|
171
|
-
returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
|
172
|
-
returns 0 on failure
|
173
|
-
|
174
|
-
TODO: Fix ipv6 support
|
175
|
-
*/
|
176
|
-
uint32_t resolve_addr(const char *address)
|
177
|
-
{
|
178
|
-
struct addrinfo *server = NULL;
|
179
|
-
struct addrinfo hints;
|
180
|
-
int rc;
|
181
|
-
uint32_t addr;
|
182
|
-
|
183
|
-
memset(&hints, 0, sizeof(hints));
|
184
|
-
hints.ai_family = AF_INET; // IPv4 only right now.
|
185
|
-
hints.ai_socktype = SOCK_DGRAM; // type of socket Tox uses.
|
186
|
-
|
187
|
-
rc = getaddrinfo(address, "echo", &hints, &server);
|
188
|
-
|
189
|
-
// Lookup failed.
|
190
|
-
if(rc != 0) {
|
191
|
-
return 0;
|
192
|
-
}
|
193
|
-
|
194
|
-
// IPv4 records only..
|
195
|
-
if(server->ai_family != AF_INET) {
|
196
|
-
freeaddrinfo(server);
|
197
|
-
return 0;
|
198
|
-
}
|
199
|
-
|
200
|
-
|
201
|
-
addr = ((struct sockaddr_in*)server->ai_addr)->sin_addr.s_addr;
|
202
|
-
|
203
|
-
freeaddrinfo(server);
|
204
|
-
return addr;
|
205
|
-
}
|
@@ -1,134 +0,0 @@
|
|
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
|
-
#include <crypto_box.h>
|
60
|
-
#endif
|
61
|
-
|
62
|
-
#ifdef __cplusplus
|
63
|
-
extern "C" {
|
64
|
-
#endif
|
65
|
-
|
66
|
-
#define MAX_UDP_PACKET_SIZE 65507
|
67
|
-
|
68
|
-
typedef union {
|
69
|
-
uint8_t c[4];
|
70
|
-
uint16_t s[2];
|
71
|
-
uint32_t i;
|
72
|
-
} IP;
|
73
|
-
|
74
|
-
typedef struct {
|
75
|
-
IP ip;
|
76
|
-
uint16_t port;
|
77
|
-
/* not used for anything right now */
|
78
|
-
uint16_t padding;
|
79
|
-
} IP_Port;
|
80
|
-
|
81
|
-
typedef struct {
|
82
|
-
int16_t family;
|
83
|
-
uint16_t port;
|
84
|
-
IP ip;
|
85
|
-
uint8_t zeroes[8];
|
86
|
-
#ifdef ENABLE_IPV6
|
87
|
-
uint8_t zeroes2[12];
|
88
|
-
#endif
|
89
|
-
} ADDR;
|
90
|
-
|
91
|
-
/* returns current time in milleseconds since the epoch. */
|
92
|
-
uint64_t current_time();
|
93
|
-
|
94
|
-
/* return a random number
|
95
|
-
NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary */
|
96
|
-
uint32_t random_int();
|
97
|
-
|
98
|
-
/* Basic network functions: */
|
99
|
-
|
100
|
-
/* Function to send packet(data) of length length to ip_port */
|
101
|
-
int sendpacket(IP_Port ip_port, uint8_t *data, uint32_t length);
|
102
|
-
|
103
|
-
/* Function to receive data, ip and port of sender is put into ip_port
|
104
|
-
the packet data into data
|
105
|
-
the packet length into length. */
|
106
|
-
int receivepacket(IP_Port *ip_port, uint8_t *data, uint32_t *length);
|
107
|
-
|
108
|
-
/* initialize networking
|
109
|
-
bind to ip and port
|
110
|
-
ip must be in network order EX: 127.0.0.1 = (7F000001)
|
111
|
-
port is in host byte order (this means don't worry about it)
|
112
|
-
returns 0 if no problems
|
113
|
-
returns -1 if there were problems */
|
114
|
-
int init_networking(IP ip, uint16_t port);
|
115
|
-
|
116
|
-
/* function to cleanup networking stuff(doesn't do much right now) */
|
117
|
-
void shutdown_networking();
|
118
|
-
|
119
|
-
/*
|
120
|
-
resolve_addr():
|
121
|
-
address should represent IPv4 or a hostname with A record
|
122
|
-
|
123
|
-
returns a data in network byte order that can be used to set IP.i or IP_Port.ip.i
|
124
|
-
returns 0 on failure
|
125
|
-
|
126
|
-
TODO: Fix ipv6 support
|
127
|
-
*/
|
128
|
-
uint32_t resolve_addr(const char *address);
|
129
|
-
|
130
|
-
#ifdef __cplusplus
|
131
|
-
}
|
132
|
-
#endif
|
133
|
-
|
134
|
-
#endif
|