summaryrefslogtreecommitdiff
path: root/toxcore/network.h
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/network.h')
-rw-r--r--toxcore/network.h159
1 files changed, 159 insertions, 0 deletions
diff --git a/toxcore/network.h b/toxcore/network.h
new file mode 100644
index 00000000..3547f79b
--- /dev/null
+++ b/toxcore/network.h
@@ -0,0 +1,159 @@
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#ifndef WINVER
35//Windows XP
36#define WINVER 0x0501
37#endif
38#include <winsock2.h>
39#include <windows.h>
40#include <ws2tcpip.h>
41
42#undef VANILLA_NACL /* make sure on windows we use libsodium */
43
44#else //Linux includes
45
46#include <fcntl.h>
47#include <sys/socket.h>
48#include <netinet/in.h>
49#include <errno.h>
50#include <sys/time.h>
51#include <sys/types.h>
52#include <netdb.h>
53#include <unistd.h>
54
55#endif
56
57#ifndef VANILLA_NACL
58/* we use libsodium by default */
59#include <sodium.h>
60#else
61#include <crypto_box.h>
62#define crypto_box_MACBYTES (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES)
63#endif
64
65#ifdef __cplusplus
66extern "C" {
67#endif
68
69#define MAX_UDP_PACKET_SIZE 65507
70
71#define NET_PACKET_PING_REQUEST 0 /* Ping request packet ID */
72#define NET_PACKET_PING_RESPONSE 1 /* Ping response packet ID */
73#define NET_PACKET_GET_NODES 2 /* Get nodes request packet ID */
74#define NET_PACKET_SEND_NODES 3 /* Send nodes response packet ID */
75#define NET_PACKET_HANDSHAKE 16 /* Handshake packet ID */
76#define NET_PACKET_SYNC 17 /* SYNC packet ID */
77#define NET_PACKET_DATA 18 /* Data packet ID */
78#define NET_PACKET_CRYPTO 32 /* Encrypted data packet ID */
79#define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID */
80
81
82/* Current time, unix format */
83#define unix_time() ((uint64_t)time(NULL))
84
85
86typedef union {
87 uint8_t c[4];
88 uint16_t s[2];
89 uint32_t i;
90} IP;
91
92typedef struct {
93 IP ip;
94 uint16_t port;
95 /* not used for anything right now */
96 uint16_t padding;
97} IP_Port;
98
99typedef struct {
100 int16_t family;
101 uint16_t port;
102 IP ip;
103 uint8_t zeroes[8];
104#ifdef ENABLE_IPV6
105 uint8_t zeroes2[12];
106#endif
107} ADDR;
108
109/* Function to receive data, ip and port of sender is put into ip_port
110 the packet data into data
111 the packet length into length. */
112typedef int (*packet_handler_callback)(void *object, IP_Port ip_port, uint8_t *data, uint32_t len);
113
114typedef struct {
115 packet_handler_callback function;
116 void *object;
117} Packet_Handles;
118
119typedef struct {
120 Packet_Handles packethandlers[256];
121 /* our UDP socket */
122 int sock;
123} Networking_Core;
124
125/* returns current time in milleseconds since the epoch. */
126uint64_t current_time(void);
127
128/* return a random number
129 NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary */
130uint32_t random_int(void);
131
132/* Basic network functions: */
133
134/* Function to send packet(data) of length length to ip_port */
135int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length);
136
137/* Function to call when packet beginning with byte is received */
138void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_callback cb, void *object);
139
140/* call this several times a second */
141void networking_poll(Networking_Core *net);
142
143/* initialize networking
144 bind to ip and port
145 ip must be in network order EX: 127.0.0.1 = (7F000001)
146 port is in host byte order (this means don't worry about it)
147 returns 0 if no problems
148 returns -1 if there were problems */
149Networking_Core *new_networking(IP ip, uint16_t port);
150
151/* function to cleanup networking stuff(doesn't do much right now) */
152void kill_networking(Networking_Core *net);
153
154
155#ifdef __cplusplus
156}
157#endif
158
159#endif