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 926e6c08..0a0122be 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> |
@@ -65,15 +65,15 @@ | |||
65 | 65 | ||
66 | #define MAX_UDP_PACKET_SIZE 65507 | 66 | #define MAX_UDP_PACKET_SIZE 65507 |
67 | 67 | ||
68 | #define NET_PACKET_PING_REQUEST 0 /* Ping request packet ID */ | 68 | #define NET_PACKET_PING_REQUEST 0 /* Ping request packet ID. */ |
69 | #define NET_PACKET_PING_RESPONSE 1 /* Ping response packet ID */ | 69 | #define NET_PACKET_PING_RESPONSE 1 /* Ping response packet ID. */ |
70 | #define NET_PACKET_GET_NODES 2 /* Get nodes request packet ID */ | 70 | #define NET_PACKET_GET_NODES 2 /* Get nodes request packet ID. */ |
71 | #define NET_PACKET_SEND_NODES 3 /* Send nodes response packet ID */ | 71 | #define NET_PACKET_SEND_NODES 3 /* Send nodes response packet ID. */ |
72 | #define NET_PACKET_HANDSHAKE 16 /* Handshake packet ID */ | 72 | #define NET_PACKET_HANDSHAKE 16 /* Handshake packet ID. */ |
73 | #define NET_PACKET_SYNC 17 /* SYNC packet ID */ | 73 | #define NET_PACKET_SYNC 17 /* SYNC packet ID. */ |
74 | #define NET_PACKET_DATA 18 /* Data packet ID */ | 74 | #define NET_PACKET_DATA 18 /* Data packet ID. */ |
75 | #define NET_PACKET_CRYPTO 32 /* Encrypted data packet ID */ | 75 | #define NET_PACKET_CRYPTO 32 /* Encrypted data packet ID. */ |
76 | #define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID */ | 76 | #define NET_PACKET_LAN_DISCOVERY 33 /* LAN discovery packet ID. */ |
77 | 77 | ||
78 | 78 | ||
79 | /* Current time, unix format */ | 79 | /* Current time, unix format */ |
@@ -89,7 +89,7 @@ typedef union { | |||
89 | typedef struct { | 89 | typedef struct { |
90 | IP ip; | 90 | IP ip; |
91 | uint16_t port; | 91 | uint16_t port; |
92 | /* not used for anything right now */ | 92 | /* Not used for anything right now. */ |
93 | uint16_t padding; | 93 | uint16_t padding; |
94 | } IP_Port; | 94 | } IP_Port; |
95 | 95 | ||
@@ -103,9 +103,10 @@ typedef struct { | |||
103 | #endif | 103 | #endif |
104 | } ADDR; | 104 | } ADDR; |
105 | 105 | ||
106 | /* Function to receive data, ip and port of sender is put into ip_port | 106 | /* Function to receive data, ip and port of sender is put into ip_port. |
107 | the packet data into data | 107 | * Packet data is put into data. |
108 | the packet length into length. */ | 108 | * Packet length is put into length. |
109 | */ | ||
109 | typedef int (*packet_handler_callback)(void *object, IP_Port ip_port, uint8_t *data, uint32_t len); | 110 | typedef int (*packet_handler_callback)(void *object, IP_Port ip_port, uint8_t *data, uint32_t len); |
110 | 111 | ||
111 | typedef struct { | 112 | typedef struct { |
@@ -115,37 +116,40 @@ typedef struct { | |||
115 | 116 | ||
116 | typedef struct { | 117 | typedef struct { |
117 | Packet_Handles packethandlers[256]; | 118 | Packet_Handles packethandlers[256]; |
118 | /* our UDP socket */ | 119 | /* Our UDP socket. */ |
119 | int sock; | 120 | int sock; |
120 | } Networking_Core; | 121 | } Networking_Core; |
121 | 122 | ||
122 | /* returns current time in milleseconds since the epoch. */ | 123 | /* return current time in milleseconds since the epoch. */ |
123 | uint64_t current_time(void); | 124 | uint64_t current_time(void); |
124 | 125 | ||
125 | /* return a random number | 126 | /* return a random number. |
126 | NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary */ | 127 | * NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary. |
128 | */ | ||
127 | uint32_t random_int(void); | 129 | uint32_t random_int(void); |
128 | 130 | ||
129 | /* Basic network functions: */ | 131 | /* Basic network functions: */ |
130 | 132 | ||
131 | /* Function to send packet(data) of length length to ip_port */ | 133 | /* Function to send packet(data) of length length to ip_port. */ |
132 | int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length); | 134 | int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length); |
133 | 135 | ||
134 | /* Function to call when packet beginning with byte is received */ | 136 | /* Function to call when packet beginning with byte is received. */ |
135 | void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_callback cb, void *object); | 137 | void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_callback cb, void *object); |
136 | 138 | ||
137 | /* call this several times a second */ | 139 | /* Call this several times a second. */ |
138 | void networking_poll(Networking_Core *net); | 140 | void networking_poll(Networking_Core *net); |
139 | 141 | ||
140 | /* initialize networking | 142 | /* Initialize networking. |
141 | bind to ip and port | 143 | * bind to ip and port. |
142 | ip must be in network order EX: 127.0.0.1 = (7F000001) | 144 | * ip must be in network order EX: 127.0.0.1 = (7F000001). |
143 | port is in host byte order (this means don't worry about it) | 145 | * port is in host byte order (this means don't worry about it). |
144 | returns 0 if no problems | 146 | * |
145 | returns -1 if there were problems */ | 147 | * returns 0 if no problems. |
148 | * returns -1 if there were problems. | ||
149 | */ | ||
146 | Networking_Core *new_networking(IP ip, uint16_t port); | 150 | Networking_Core *new_networking(IP ip, uint16_t port); |
147 | 151 | ||
148 | /* function to cleanup networking stuff(doesn't do much right now) */ | 152 | /* Function to cleanup networking stuff (doesn't do much right now). */ |
149 | void kill_networking(Networking_Core *net); | 153 | void kill_networking(Networking_Core *net); |
150 | 154 | ||
151 | 155 | ||