diff options
Diffstat (limited to 'toxcore/network.h')
-rw-r--r-- | toxcore/network.h | 100 |
1 files changed, 77 insertions, 23 deletions
diff --git a/toxcore/network.h b/toxcore/network.h index 2d08e88d..e804379d 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 | ||
42 | typedef 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 | ||
56 | typedef int sock_t; | ||
57 | |||
53 | #endif | 58 | #endif |
54 | 59 | ||
55 | #ifndef VANILLA_NACL | 60 | #ifndef VANILLA_NACL |
@@ -66,7 +71,8 @@ | |||
66 | #define NET_PACKET_PING_REQUEST 0 /* Ping request packet ID. */ | 71 | #define NET_PACKET_PING_REQUEST 0 /* Ping request packet ID. */ |
67 | #define NET_PACKET_PING_RESPONSE 1 /* Ping response packet ID. */ | 72 | #define NET_PACKET_PING_RESPONSE 1 /* Ping response packet ID. */ |
68 | #define NET_PACKET_GET_NODES 2 /* Get nodes request packet ID. */ | 73 | #define NET_PACKET_GET_NODES 2 /* Get nodes request packet ID. */ |
69 | #define NET_PACKET_SEND_NODES 3 /* Send nodes response packet ID. */ | 74 | #define NET_PACKET_SEND_NODES 3 /* Send nodes response packet ID for IPv4 addresses. */ |
75 | #define NET_PACKET_SEND_NODES_EX 4 /* Send nodes response packet ID for other addresses. */ | ||
70 | #define NET_PACKET_HANDSHAKE 16 /* Handshake packet ID. */ | 76 | #define NET_PACKET_HANDSHAKE 16 /* Handshake packet ID. */ |
71 | #define NET_PACKET_SYNC 17 /* SYNC packet ID. */ | 77 | #define NET_PACKET_SYNC 17 /* SYNC packet ID. */ |
72 | #define NET_PACKET_DATA 18 /* Data packet ID. */ | 78 | #define NET_PACKET_DATA 18 /* Data packet ID. */ |
@@ -82,27 +88,83 @@ typedef union { | |||
82 | uint8_t uint8[4]; | 88 | uint8_t uint8[4]; |
83 | uint16_t uint16[2]; | 89 | uint16_t uint16[2]; |
84 | uint32_t uint32; | 90 | uint32_t uint32; |
85 | } IP; | 91 | struct in_addr in_addr; |
92 | } IP4; | ||
93 | |||
94 | typedef struct in6_addr IP6; | ||
95 | |||
96 | typedef struct { | ||
97 | sa_family_t family; | ||
98 | union { | ||
99 | IP4 ip4; | ||
100 | IP6 ip6; | ||
101 | }; | ||
102 | } IPAny; | ||
86 | 103 | ||
87 | typedef union { | 104 | typedef union { |
88 | struct { | 105 | struct { |
89 | IP ip; | 106 | IP4 ip; |
90 | uint16_t port; | 107 | uint16_t port; |
91 | /* Not used for anything right now. */ | 108 | /* Not used for anything right now. */ |
92 | uint16_t padding; | 109 | uint16_t padding; |
93 | }; | 110 | }; |
94 | uint8_t uint8[8]; | 111 | uint8_t uint8[8]; |
95 | } IP_Port; | 112 | } IP4_Port; |
96 | 113 | ||
114 | /* will replace IP_Port as soon as the complete infrastructure is in place | ||
115 | * removed the unused union and padding also */ | ||
97 | typedef struct { | 116 | typedef struct { |
98 | int16_t family; | 117 | IPAny ip; |
99 | uint16_t port; | 118 | uint16_t port; |
100 | IP ip; | 119 | } IPAny_Port; |
101 | uint8_t zeroes[8]; | 120 | |
102 | #ifdef ENABLE_IPV6 | 121 | #undef TOX_ENABLE_IPV6 |
103 | uint8_t zeroes2[12]; | 122 | /* #define TOX_ENABLE_IPV6 */ |
123 | #ifdef TOX_ENABLE_IPV6 | ||
124 | #define TOX_ENABLE_IPV6_DEFAULT 1 | ||
125 | typedef IPAny IP; | ||
126 | typedef IPAny_Port IP_Port; | ||
127 | #else | ||
128 | #define TOX_ENABLE_IPV6_DEFAULT 0 | ||
129 | typedef IP4 IP; | ||
130 | typedef IP4_Port IP_Port; | ||
104 | #endif | 131 | #endif |
105 | } ADDR; | 132 | |
133 | /* ip_ntoa | ||
134 | * converts ip into a string | ||
135 | * uses a static buffer, so mustn't used multiple times in the same output | ||
136 | */ | ||
137 | const char *ip_ntoa(IP *ip); | ||
138 | |||
139 | /* ipport_equal | ||
140 | * compares two IPAny_Port structures | ||
141 | * unset means unequal | ||
142 | * | ||
143 | * returns 0 when not equal or when uninitialized | ||
144 | */ | ||
145 | int ipport_equal(IP_Port *a, IP_Port *b); | ||
146 | |||
147 | /* nulls out ip */ | ||
148 | void ip_reset(IP *ip); | ||
149 | /* nulls out ip, sets family according to flag */ | ||
150 | void ip_init(IP *ip, uint8_t ipv6enabled); | ||
151 | /* checks if ip is valid */ | ||
152 | int ip_isset(IP *ip); | ||
153 | /* checks if ip is valid */ | ||
154 | int ipport_isset(IP_Port *ipport); | ||
155 | /* copies an ip structure */ | ||
156 | void ip_copy(IP *target, IP *source); | ||
157 | /* copies an ip_port structure */ | ||
158 | void ipport_copy(IP_Port *target, IP_Port *source); | ||
159 | |||
160 | /* | ||
161 | * addr_resolve_or_parse_ip | ||
162 | * resolves string into an IP address | ||
163 | * | ||
164 | * to->family MUST be set (AF_UNSPEC, AF_INET, AF_INET6) | ||
165 | * returns 1 on success, 0 on failure | ||
166 | */ | ||
167 | int addr_resolve_or_parse_ip(const char *address, IP *to); | ||
106 | 168 | ||
107 | /* Function to receive data, ip and port of sender is put into ip_port. | 169 | /* Function to receive data, ip and port of sender is put into ip_port. |
108 | * Packet data is put into data. | 170 | * Packet data is put into data. |
@@ -117,13 +179,11 @@ typedef struct { | |||
117 | 179 | ||
118 | typedef struct { | 180 | typedef struct { |
119 | Packet_Handles packethandlers[256]; | 181 | Packet_Handles packethandlers[256]; |
120 | /* Our UDP socket. */ | ||
121 | #ifdef WIN32 | ||
122 | unsigned int sock; | ||
123 | #else | ||
124 | int sock; | ||
125 | #endif | ||
126 | 182 | ||
183 | /* Our UDP socket. */ | ||
184 | sa_family_t family; | ||
185 | uint16_t port; | ||
186 | sock_t sock; | ||
127 | } Networking_Core; | 187 | } Networking_Core; |
128 | 188 | ||
129 | /* return current time in milleseconds since the epoch. */ | 189 | /* return current time in milleseconds since the epoch. */ |
@@ -137,12 +197,7 @@ uint32_t random_int(void); | |||
137 | /* Basic network functions: */ | 197 | /* Basic network functions: */ |
138 | 198 | ||
139 | /* Function to send packet(data) of length length to ip_port. */ | 199 | /* Function to send packet(data) of length length to ip_port. */ |
140 | #ifdef WIN32 | 200 | int sendpacket(Networking_Core *net, IP_Port ip_port, uint8_t *data, uint32_t length); |
141 | int sendpacket(unsigned int sock, IP_Port ip_port, uint8_t *data, uint32_t length); | ||
142 | #else | ||
143 | int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length); | ||
144 | #endif | ||
145 | |||
146 | 201 | ||
147 | /* Function to call when packet beginning with byte is received. */ | 202 | /* Function to call when packet beginning with byte is received. */ |
148 | void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_callback cb, void *object); | 203 | void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_callback cb, void *object); |
@@ -163,5 +218,4 @@ Networking_Core *new_networking(IP ip, uint16_t port); | |||
163 | /* Function to cleanup networking stuff (doesn't do much right now). */ | 218 | /* Function to cleanup networking stuff (doesn't do much right now). */ |
164 | void kill_networking(Networking_Core *net); | 219 | void kill_networking(Networking_Core *net); |
165 | 220 | ||
166 | |||
167 | #endif | 221 | #endif |