diff options
Diffstat (limited to 'toxcore/network.h')
-rw-r--r-- | toxcore/network.h | 64 |
1 files changed, 34 insertions, 30 deletions
diff --git a/toxcore/network.h b/toxcore/network.h index 3547f79b..7d699762 100644 --- a/toxcore/network.h +++ b/toxcore/network.h | |||
@@ -39,9 +39,9 @@ | |||
39 | #include <windows.h> | 39 | #include <windows.h> |
40 | #include <ws2tcpip.h> | 40 | #include <ws2tcpip.h> |
41 | 41 | ||
42 | #undef VANILLA_NACL /* make sure on windows we use libsodium */ | 42 | #undef VANILLA_NACL /* Make sure on Windows we use libsodium. */ |
43 | 43 | ||
44 | #else //Linux includes | 44 | #else // Linux includes |
45 | 45 | ||
46 | #include <fcntl.h> | 46 | #include <fcntl.h> |
47 | #include <sys/socket.h> | 47 | #include <sys/socket.h> |
@@ -55,7 +55,7 @@ | |||
55 | #endif | 55 | #endif |
56 | 56 | ||
57 | #ifndef VANILLA_NACL | 57 | #ifndef VANILLA_NACL |
58 | /* we use libsodium by default */ | 58 | /* We use libsodium by default. */ |
59 | #include <sodium.h> | 59 | #include <sodium.h> |
60 | #else | 60 | #else |
61 | #include <crypto_box.h> | 61 | #include <crypto_box.h> |
@@ -68,15 +68,15 @@ extern "C" { | |||
68 | 68 | ||
69 | #define MAX_UDP_PACKET_SIZE 65507 | 69 | #define MAX_UDP_PACKET_SIZE 65507 |
70 | 70 | ||
71 | #define NET_PACKET_PING_REQUEST 0 /* Ping request packet ID */ | 71 | #define NET_PACKET_PING_REQUEST 0 /* Ping request packet ID. */ |
72 | #define NET_PACKET_PING_RESPONSE 1 /* Ping response 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 */ | 73 | #define NET_PACKET_GET_NODES 2 /* Get nodes request packet ID. */ |
74 | #define NET_PACKET_SEND_NODES 3 /* Send nodes response packet ID */ | 74 | #define NET_PACKET_SEND_NODES 3 /* Send nodes response packet ID. */ |
75 | #define NET_PACKET_HANDSHAKE 16 /* Handshake packet ID */ | 75 | #define NET_PACKET_HANDSHAKE 16 /* Handshake packet ID. */ |
76 | #define NET_PACKET_SYNC 17 /* SYNC packet ID */ | 76 | #define NET_PACKET_SYNC 17 /* SYNC packet ID. */ |
77 | #define NET_PACKET_DATA 18 /* Data packet ID */ | 77 | #define NET_PACKET_DATA 18 /* Data packet ID. */ |
78 | #define NET_PACKET_CRYPTO 32 /* Encrypted data packet ID */ | 78 | #define NET_PACKET_CRYPTO 32 /* Encrypted data packet ID. */ |
79 | #define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID */ | 79 | #define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID. */ |
80 | 80 | ||
81 | 81 | ||
82 | /* Current time, unix format */ | 82 | /* Current time, unix format */ |
@@ -92,7 +92,7 @@ typedef union { | |||
92 | typedef struct { | 92 | typedef struct { |
93 | IP ip; | 93 | IP ip; |
94 | uint16_t port; | 94 | uint16_t port; |
95 | /* not used for anything right now */ | 95 | /* Not used for anything right now. */ |
96 | uint16_t padding; | 96 | uint16_t padding; |
97 | } IP_Port; | 97 | } IP_Port; |
98 | 98 | ||
@@ -106,9 +106,10 @@ typedef struct { | |||
106 | #endif | 106 | #endif |
107 | } ADDR; | 107 | } ADDR; |
108 | 108 | ||
109 | /* Function to receive data, ip and port of sender is put into ip_port | 109 | /* Function to receive data, ip and port of sender is put into ip_port. |
110 | the packet data into data | 110 | * Packet data is put into data. |
111 | the packet length into length. */ | 111 | * Packet length is put into length. |
112 | */ | ||
112 | typedef int (*packet_handler_callback)(void *object, IP_Port ip_port, uint8_t *data, uint32_t len); | 113 | typedef int (*packet_handler_callback)(void *object, IP_Port ip_port, uint8_t *data, uint32_t len); |
113 | 114 | ||
114 | typedef struct { | 115 | typedef struct { |
@@ -118,37 +119,40 @@ typedef struct { | |||
118 | 119 | ||
119 | typedef struct { | 120 | typedef struct { |
120 | Packet_Handles packethandlers[256]; | 121 | Packet_Handles packethandlers[256]; |
121 | /* our UDP socket */ | 122 | /* Our UDP socket. */ |
122 | int sock; | 123 | int sock; |
123 | } Networking_Core; | 124 | } Networking_Core; |
124 | 125 | ||
125 | /* returns current time in milleseconds since the epoch. */ | 126 | /* return current time in milleseconds since the epoch. */ |
126 | uint64_t current_time(void); | 127 | uint64_t current_time(void); |
127 | 128 | ||
128 | /* return a random number | 129 | /* return a random number. |
129 | NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary */ | 130 | * NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary. |
131 | */ | ||
130 | uint32_t random_int(void); | 132 | uint32_t random_int(void); |
131 | 133 | ||
132 | /* Basic network functions: */ | 134 | /* Basic network functions: */ |
133 | 135 | ||
134 | /* Function to send packet(data) of length length to ip_port */ | 136 | /* Function to send packet(data) of length length to ip_port. */ |
135 | int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length); | 137 | int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length); |
136 | 138 | ||
137 | /* Function to call when packet beginning with byte is received */ | 139 | /* Function to call when packet beginning with byte is received. */ |
138 | void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_callback cb, void *object); | 140 | void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_callback cb, void *object); |
139 | 141 | ||
140 | /* call this several times a second */ | 142 | /* Call this several times a second. */ |
141 | void networking_poll(Networking_Core *net); | 143 | void networking_poll(Networking_Core *net); |
142 | 144 | ||
143 | /* initialize networking | 145 | /* Initialize networking. |
144 | bind to ip and port | 146 | * bind to ip and port. |
145 | ip must be in network order EX: 127.0.0.1 = (7F000001) | 147 | * 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) | 148 | * port is in host byte order (this means don't worry about it). |
147 | returns 0 if no problems | 149 | * |
148 | returns -1 if there were problems */ | 150 | * returns 0 if no problems. |
151 | * returns -1 if there were problems. | ||
152 | */ | ||
149 | Networking_Core *new_networking(IP ip, uint16_t port); | 153 | Networking_Core *new_networking(IP ip, uint16_t port); |
150 | 154 | ||
151 | /* function to cleanup networking stuff(doesn't do much right now) */ | 155 | /* Function to cleanup networking stuff (doesn't do much right now). */ |
152 | void kill_networking(Networking_Core *net); | 156 | void kill_networking(Networking_Core *net); |
153 | 157 | ||
154 | 158 | ||