summaryrefslogtreecommitdiff
path: root/toxcore/network.h
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/network.h')
-rw-r--r--toxcore/network.h139
1 files changed, 116 insertions, 23 deletions
diff --git a/toxcore/network.h b/toxcore/network.h
index e1f9b212..7dea8c16 100644
--- a/toxcore/network.h
+++ b/toxcore/network.h
@@ -39,17 +39,22 @@
39#include <windows.h> 39#include <windows.h>
40#include <ws2tcpip.h> 40#include <ws2tcpip.h>
41 41
42typedef unsigned int sock_t;
43
42#else // Linux includes 44#else // Linux includes
43 45
44#include <fcntl.h> 46#include <fcntl.h>
45#include <sys/socket.h> 47#include <sys/socket.h>
46#include <netinet/in.h> 48#include <netinet/in.h>
49#include <arpa/inet.h>
47#include <errno.h> 50#include <errno.h>
48#include <sys/time.h> 51#include <sys/time.h>
49#include <sys/types.h> 52#include <sys/types.h>
50#include <netdb.h> 53#include <netdb.h>
51#include <unistd.h> 54#include <unistd.h>
52 55
56typedef int sock_t;
57
53#endif 58#endif
54 59
55#ifndef VANILLA_NACL 60#ifndef VANILLA_NACL
@@ -67,7 +72,8 @@
67#define NET_PACKET_PING_REQUEST 0 /* Ping request packet ID. */ 72#define NET_PACKET_PING_REQUEST 0 /* Ping request packet ID. */
68#define NET_PACKET_PING_RESPONSE 1 /* Ping response packet ID. */ 73#define NET_PACKET_PING_RESPONSE 1 /* Ping response packet ID. */
69#define NET_PACKET_GET_NODES 2 /* Get nodes request packet ID. */ 74#define NET_PACKET_GET_NODES 2 /* Get nodes request packet ID. */
70#define NET_PACKET_SEND_NODES 3 /* Send nodes response packet ID. */ 75#define NET_PACKET_SEND_NODES 3 /* Send nodes response packet ID for IPv4 addresses. */
76#define NET_PACKET_SEND_NODES_IPV6 4 /* Send nodes response packet ID for other addresses. */
71#define NET_PACKET_HANDSHAKE 16 /* Handshake packet ID. */ 77#define NET_PACKET_HANDSHAKE 16 /* Handshake packet ID. */
72#define NET_PACKET_SYNC 17 /* SYNC packet ID. */ 78#define NET_PACKET_SYNC 17 /* SYNC packet ID. */
73#define NET_PACKET_DATA 18 /* Data packet ID. */ 79#define NET_PACKET_DATA 18 /* Data packet ID. */
@@ -75,6 +81,10 @@
75#define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID. */ 81#define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID. */
76#define NET_PACKET_GROUP_CHATS 48 /* Group chats packet ID. */ 82#define NET_PACKET_GROUP_CHATS 48 /* Group chats packet ID. */
77 83
84#define TOX_PORTRANGE_FROM 33445
85#define TOX_PORTRANGE_TO 33455
86#define TOX_PORT_DEFAULT TOX_PORTRANGE_FROM
87
78/* Current time, unix format */ 88/* Current time, unix format */
79#define unix_time() ((uint64_t)time(NULL)) 89#define unix_time() ((uint64_t)time(NULL))
80 90
@@ -83,27 +93,118 @@ typedef union {
83 uint8_t uint8[4]; 93 uint8_t uint8[4];
84 uint16_t uint16[2]; 94 uint16_t uint16[2];
85 uint32_t uint32; 95 uint32_t uint32;
86} IP; 96 struct in_addr in_addr;
97} IP4;
98
99typedef struct in6_addr IP6;
100
101typedef struct {
102 sa_family_t family;
103 union {
104 IP4 ip4;
105 IP6 ip6;
106 };
107} IPAny;
87 108
88typedef union { 109typedef union {
89 struct { 110 struct {
90 IP ip; 111 IP4 ip;
91 uint16_t port; 112 uint16_t port;
92 /* Not used for anything right now. */ 113 /* Not used for anything right now. */
93 uint16_t padding; 114 uint16_t padding;
94 }; 115 };
95 uint8_t uint8[8]; 116 uint8_t uint8[8];
96} IP_Port; 117} IP4_Port;
97 118
119/* will replace IP_Port as soon as the complete infrastructure is in place
120 * removed the unused union and padding also */
98typedef struct { 121typedef struct {
99 int16_t family; 122 IPAny ip;
100 uint16_t port; 123 uint16_t port;
101 IP ip; 124} IPAny_Port;
102 uint8_t zeroes[8]; 125
103#ifdef ENABLE_IPV6 126/* #undef TOX_ENABLE_IPV6 */
104 uint8_t zeroes2[12]; 127#define TOX_ENABLE_IPV6
128#ifdef TOX_ENABLE_IPV6
129#define TOX_ENABLE_IPV6_DEFAULT 1
130typedef IPAny IP;
131typedef IPAny_Port IP_Port;
132#else
133#define TOX_ENABLE_IPV6_DEFAULT 0
134typedef IP4 IP;
135typedef IP4_Port IP_Port;
105#endif 136#endif
106} ADDR; 137
138/* ip_ntoa
139 * converts ip into a string
140 * uses a static buffer, so mustn't used multiple times in the same output
141 */
142const char *ip_ntoa(IP *ip);
143
144/* ip_equal
145 * compares two IPAny structures
146 * unset means unequal
147 *
148 * returns 0 when not equal or when uninitialized
149 */
150int ip_equal(IP *a, IP *b);
151
152/* ipport_equal
153 * compares two IPAny_Port structures
154 * unset means unequal
155 *
156 * returns 0 when not equal or when uninitialized
157 */
158int ipport_equal(IP_Port *a, IP_Port *b);
159
160/* nulls out ip */
161void ip_reset(IP *ip);
162/* nulls out ip, sets family according to flag */
163void ip_init(IP *ip, uint8_t ipv6enabled);
164/* checks if ip is valid */
165int ip_isset(IP *ip);
166/* checks if ip is valid */
167int ipport_isset(IP_Port *ipport);
168/* copies an ip structure */
169void ip_copy(IP *target, IP *source);
170/* copies an ip_port structure */
171void ipport_copy(IP_Port *target, IP_Port *source);
172
173/*
174 * addr_resolve():
175 * uses getaddrinfo to resolve an address into an IP address
176 * uses the first IPv4/IPv6 addresses returned by getaddrinfo
177 *
178 * input
179 * address: a hostname (or something parseable to an IP address)
180 * to: to.family MUST be initialized, either set to a specific IP version
181 * (AF_INET/AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both
182 * IP versions are acceptable
183 * extra can be NULL and is only set in special circumstances, see returns
184 *
185 * returns in *to a valid IPAny (v4/v6),
186 * prefers v6 if ip.family was AF_UNSPEC and both available
187 * returns in *extra an IPv4 address, if family was AF_UNSPEC and *to is AF_INET6
188 * returns 0 on failure
189 */
190int addr_resolve(const char *address, IP *to, IP *extra);
191
192/*
193 * addr_resolve_or_parse_ip
194 * resolves string into an IP address
195 *
196 * address: a hostname (or something parseable to an IP address)
197 * to: to.family MUST be initialized, either set to a specific IP version
198 * (AF_INET/AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both
199 * IP versions are acceptable
200 * extra can be NULL and is only set in special circumstances, see returns
201 *
202 * returns in *tro a matching address (IPv6 or IPv4)
203 * returns in *extra, if not NULL, an IPv4 address, if to->family was AF_UNSPEC
204 * returns 1 on success
205 * returns 0 on failure
206 */
207int addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra);
107 208
108/* Function to receive data, ip and port of sender is put into ip_port. 209/* Function to receive data, ip and port of sender is put into ip_port.
109 * Packet data is put into data. 210 * Packet data is put into data.
@@ -118,13 +219,11 @@ typedef struct {
118 219
119typedef struct { 220typedef struct {
120 Packet_Handles packethandlers[256]; 221 Packet_Handles packethandlers[256];
121 /* Our UDP socket. */
122#ifdef WIN32
123 unsigned int sock;
124#else
125 int sock;
126#endif
127 222
223 /* Our UDP socket. */
224 sa_family_t family;
225 uint16_t port;
226 sock_t sock;
128} Networking_Core; 227} Networking_Core;
129 228
130/* return current time in milleseconds since the epoch. */ 229/* return current time in milleseconds since the epoch. */
@@ -137,12 +236,7 @@ uint32_t random_int(void);
137/* Basic network functions: */ 236/* Basic network functions: */
138 237
139/* Function to send packet(data) of length length to ip_port. */ 238/* Function to send packet(data) of length length to ip_port. */
140#ifdef WIN32 239int sendpacket(Networking_Core *net, IP_Port ip_port, uint8_t *data, uint32_t length);
141int sendpacket(unsigned int sock, IP_Port ip_port, uint8_t *data, uint32_t length);
142#else
143int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length);
144#endif
145
146 240
147/* Function to call when packet beginning with byte is received. */ 241/* Function to call when packet beginning with byte is received. */
148void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_callback cb, void *object); 242void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_callback cb, void *object);
@@ -163,5 +257,4 @@ Networking_Core *new_networking(IP ip, uint16_t port);
163/* Function to cleanup networking stuff (doesn't do much right now). */ 257/* Function to cleanup networking stuff (doesn't do much right now). */
164void kill_networking(Networking_Core *net); 258void kill_networking(Networking_Core *net);
165 259
166
167#endif 260#endif