1 /* 2 netchan.h 3 4 quake's interface to the networking layer 5 6 Copyright (C) 1996-1997 Id Software, Inc. 7 8 This program is free software; you can redistribute it and/or 9 modify it under the terms of the GNU General Public License 10 as published by the Free Software Foundation; either version 2 11 of the License, or (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 16 17 See the GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with this program; if not, write to: 21 22 Free Software Foundation, Inc. 23 59 Temple Place - Suite 330 24 Boston, MA 02111-1307, USA 25 26 */ 27 28 #ifndef _NET_H 29 #define _NET_H 30 31 #include "QF/cvar.h" 32 #include "QF/msg.h" 33 #include "QF/qdefs.h" 34 #include "QF/sizebuf.h" 35 36 /** \defgroup network Network support 37 */ 38 39 /** \defgroup qw-net QuakeWorld network support. 40 \ingroup network 41 */ 42 //{ 43 #define MAX_MSGLEN 1450 ///< max length of a reliable message 44 #define MAX_DATAGRAM 1450 ///< max length of unreliable message 45 46 #define PORT_ANY -1 47 48 typedef struct 49 { 50 #ifdef HAVE_IPV6 51 byte ip[16]; 52 #else 53 byte ip[4]; 54 #endif 55 unsigned short port; 56 unsigned short family; 57 } netadr_t; 58 59 extern int net_socket; 60 extern netadr_t net_local_adr; 61 extern netadr_t net_loopback_adr; 62 extern netadr_t net_from; // address of who sent the packet 63 extern struct msg_s *net_message; 64 65 extern struct cvar_s *qport; 66 67 int Net_Log_Init (const char **sound_precache); 68 void Net_LogPrintf (const char *fmt, ...) __attribute__ ((format (printf, 1, 2))); 69 void Log_Incoming_Packet (const byte *p, int len, int has_sequence, 70 int is_server); 71 void Log_Outgoing_Packet (const byte *p, int len, int has_sequence, 72 int is_server); 73 void Net_LogStop (void); 74 void Analyze_Client_Packet (const byte * data, int len, int has_sequence); 75 void Analyze_Server_Packet (const byte * data, int len, int has_sequence); 76 77 extern struct cvar_s *net_packetlog; 78 //@} 79 80 /** \defgroup qw-udp QuakeWorld udp support. 81 \ingroup qw-net 82 */ 83 //@{ 84 85 /** Initialize the UDP network interface. 86 87 Opens a single socket to be used for all communications. 88 89 \param port The port to which the socket will be bound. 90 */ 91 void NET_Init (int port); 92 93 /** Shutdown the UDP network interface. 94 */ 95 void NET_Shutdown (void); 96 97 /** Read a single packet from the network into net_message. 98 99 \return True if successfully read, otherwise false. 100 */ 101 qboolean NET_GetPacket (void); 102 103 /** Send a data packet out to the network. 104 105 \param length The length of the data to be sent. 106 \param data The data to be sent. 107 \param to The address to which the data will be sent. 108 */ 109 void NET_SendPacket (int length, const void *data, netadr_t to); 110 111 /** Compare two network addresses. 112 113 Compare the port number too. 114 115 \param a The first address to compare. 116 \param b The second address to compare. 117 \return True of the addresses match, otherwise false. 118 */ 119 qboolean NET_CompareAdr (netadr_t a, netadr_t b); 120 121 /** Compare two network addresses. 122 123 Ignore the port number. 124 125 \param a The first address to compare. 126 \param b The second address to compare. 127 \return True of the addresses match, otherwise false. 128 */ 129 qboolean NET_CompareBaseAdr (netadr_t a, netadr_t b); 130 131 /** Convert an address to a string. 132 133 Include the port number in the string. 134 135 \warning The return value is a pointer to a static buffer. The returned 136 string must be saved if mixing calls with different addresses. 137 138 \param a The address to convert 139 \return The address in human readable form. 140 */ 141 const char *NET_AdrToString (netadr_t a); 142 143 /** Convert an address to a string. 144 145 Do not include the port number in the string. 146 147 \warning The return value is a pointer to a static buffer. The returned 148 string must be saved if mixing calls with different addresses. 149 150 \param a The address to convert 151 \return The address in human readable form. 152 */ 153 const char *NET_BaseAdrToString (netadr_t a); 154 155 /** Convert a human readable address to a quake address. 156 157 Accepts both host names (full or partial) and numeric form. 158 159 If a port is specified, the port value in the address will be set to that 160 value, otherwise it will be set to 0. 161 162 \param[in] s The human readable address to be converted. 163 \param[out] a The resulting address of the conversion. 164 \return True if the conversion is successful, otherwise false. 165 */ 166 qboolean NET_StringToAdr (const char *s, netadr_t *a); 167 168 //@} 169 170 /** \defgroup netchan Netchan 171 \ingroup qw-net 172 173 <table> 174 <tr><td colspan=2>Packet Header</td></tr> 175 <tr><td>Bits</td> <td>Meaning</td></tr> 176 <tr><td>31</td> <td>sequence</td></tr> 177 <tr><td>1</td> <td>this message contains a reliable payload</td></tr> 178 <tr><td>31</td> <td>acknowledge sequence</td></tr> 179 <tr><td>1</td> <td>acknowledge receipt of even/odd message</td></tr> 180 <tr><td>16</td> <td>qport</td></tr> 181 </table> 182 183 The remote connection never knows if it missed a reliable message, the 184 local side detects that it has been dropped by seeing a sequence 185 acknowledge higher than the last reliable sequence, but without the 186 correct even/odd bit for the reliable set. 187 188 If the sender notices that a reliable message has been dropped, it will 189 be retransmitted. It will not be retransmitted again until a message 190 after the retransmit has been acknowledged and the reliable still failed 191 to get there. 192 193 If the sequence number and reliable payload bits are all 1 (32bit -1), 194 the packet is an out-of-band packet and should be handled without a 195 netcon. 196 197 The reliable message can be added to at any time by doing 198 MSG_Write* (&netchan->message, data). 199 200 If the message buffer is overflowed, either by a single message, or by 201 multiple frames worth piling up while the last reliable transmit goes 202 unacknowledged, the netchan signals a fatal error. 203 204 Reliable messages are always placed first in a packet, then the unreliable 205 message is included if there is sufficient room. 206 207 To the receiver, there is no distinction between the reliable and 208 unreliable parts of the message, they are just processed out as a single 209 larger message. 210 211 Illogical packet sequence numbers cause the packet to be dropped, but do 212 not kill the connection. This, combined with the tight window of valid 213 reliable acknowledgement numbers provides protection against malicious 214 address spoofing. 215 216 The qport field is a workaround for bad address translating routers that 217 sometimes remap the client's source port on a packet during gameplay. 218 219 If the base part of the net address matches and the qport matches, then 220 the channel matches even if the IP port differs. The IP port should be 221 updated to the new value before sending out any replies. 222 */ 223 //@{ 224 #define OLD_AVG 0.99 // total = oldtotal*OLD_AVG + new*(1-OLD_AVG) 225 226 #define MAX_LATENT 32 227 228 typedef enum { 229 NC_QPORT_SEND = 0x01, 230 NC_QPORT_READ = 0x02, 231 } ncqport_e; 232 233 typedef struct netchan_s { 234 qboolean fatal_error; ///< True if the message overflowed 235 236 float last_received; ///< Time the last packet was received. 237 238 /// \name statistics 239 /// the statistics are cleared at each client begin, because 240 /// the server connection process gives a bogus picture of the data 241 ///@{ 242 float frame_latency; ///< rolling average 243 float frame_rate; 244 245 int drop_count; ///< dropped packets, cleared each level 246 int net_drop; ///< packets dropped before this one 247 int good_count; ///< cleared each level 248 ///@} 249 250 netadr_t remote_address; 251 int qport; 252 ncqport_e flags; 253 254 /// \name bandwidth estimator 255 ///@{ 256 double cleartime; ///< if realtime > nc->cleartime, 257 ///< free to go 258 double rate; ///< seconds / byte 259 ///@} 260 261 /// \name sequencing variables 262 ///@{ 263 int incoming_sequence; 264 int incoming_acknowledged; 265 int incoming_reliable_acknowledged; ///< single bit 266 267 int incoming_reliable_sequence; ///< single bit, maintained local 268 269 int outgoing_sequence; 270 int reliable_sequence; ///< single bit 271 int last_reliable_sequence; ///< sequence number of last send 272 ///@} 273 274 /// \name reliable staging and holding areas 275 ///@{ 276 sizebuf_t message; ///< writing buffer to send to server 277 byte message_buf[MAX_MSGLEN]; 278 279 int reliable_length; 280 byte reliable_buf[MAX_MSGLEN]; ///< unacked reliable message 281 ///@} 282 283 /// \name time and size data to calculate bandwidth 284 ///@{ 285 int outgoing_size[MAX_LATENT]; 286 double outgoing_time[MAX_LATENT]; 287 ///@} 288 } netchan_t; 289 290 /** Disable packet choking. 291 */ 292 extern int net_nochoke; 293 294 /** Disable packet sending. 295 296 Used by clients in demo playback mode. 297 */ 298 extern int net_blocksend; 299 300 /** Pointer to variable holding the current time in seconds. 301 */ 302 extern double *net_realtime; 303 304 /** Initialize the netchan system. 305 306 Currently only sets the qport cvar default to a random value. 307 */ 308 void Netchan_Init (void); 309 310 /** Initialize the netchan cvars. 311 */ 312 void Netchan_Init_Cvars (void); 313 314 /** Try to send an unreliable packet to a connection. 315 316 Handles transmission or retransmission of the reliable packet. 317 318 0 length will still generate a packet and deal with the reliable messages. 319 320 \param chan The netchan representing the connection. 321 \param length The size of the unreliable packet. 322 \param data The data of the unreliable packet. 323 */ 324 void Netchan_Transmit (netchan_t *chan, int length, byte *data); 325 326 /** Send an out-of-band packet. 327 328 \param adr The address to which the data will be sent. 329 \param length The length of the data to be sent. 330 \param data The data to be sent. 331 */ 332 void Netchan_OutOfBand (netadr_t adr, int length, byte *data); 333 /** Send a formatted string as an out-of-band packet. 334 335 \param adr The address to which the data will be sent. 336 \param format The printf style format string. 337 */ 338 void Netchan_OutOfBandPrint (netadr_t adr, const char *format, ...) 339 __attribute__ ((format (printf,2,3))); 340 341 /** Process a packet for the specifiied connection. 342 343 Called when the current net_message is from remote_address. 344 Modifies net_message so that it points to the packet payload. 345 346 \param chan The netchan representing the connection. 347 */ 348 qboolean Netchan_Process (netchan_t *chan); 349 350 /** Initialize a new connection. 351 352 \param chan The netchan representing the connection. 353 \param adr The address of the remote end of the connection. 354 \param qport The qport associated with the connection. 355 \param flags Control of the sending/reading of the qport on this 356 connection. 357 */ 358 void Netchan_Setup (netchan_t *chan, netadr_t adr, int qport, ncqport_e flags); 359 360 /** Check if a packet can be sent to the connection. 361 362 \param chan The netchan representing the connection. 363 \return True if the connection isn't chocked. 364 */ 365 qboolean Netchan_CanPacket (netchan_t *chan); 366 367 /** Check if a reliable packet can be sent to the connection. 368 369 \param chan The netchan representing the connection. 370 \return True if there is no outstanding reliable packet and the 371 connection isn't chocked. 372 */ 373 qboolean Netchan_CanReliable (netchan_t *chan); 374 375 /** Send a packet. 376 377 Very raw. Just calls NET_SendPacket(). 378 \param length The length of the data to be sent. 379 \param data The data to be sent. 380 \param to The address to which the data will be sent. 381 */ 382 void Netchan_SendPacket (int length, const void *data, netadr_t to); 383 384 //@} 385 386 #endif // _NET_H 387