]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - net.h
update file list
[xonotic/darkplaces.git] / net.h
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // net.h -- quake's interface to the networking layer
21
22 #ifndef NET_H
23 #define NET_H
24
25 struct qsockaddr
26 {
27         short sa_family;
28         unsigned char sa_data[14];
29 };
30
31
32 #define NET_NAMELEN                     64
33
34 #define NET_MAXMESSAGE          16384
35 #define NET_HEADERSIZE          (2 * sizeof(unsigned int))
36 #define NET_DATAGRAMSIZE        (MAX_DATAGRAM + NET_HEADERSIZE)
37
38 // NetHeader flags
39 #define NETFLAG_LENGTH_MASK     0x0000ffff
40 #define NETFLAG_DATA            0x00010000
41 #define NETFLAG_ACK                     0x00020000
42 #define NETFLAG_NAK                     0x00040000
43 #define NETFLAG_EOM                     0x00080000
44 #define NETFLAG_UNRELIABLE      0x00100000
45 #define NETFLAG_CTL                     0x80000000
46
47
48 #define NET_PROTOCOL_VERSION    3
49
50 // This is the network info/connection protocol.  It is used to find Quake
51 // servers, get info about them, and connect to them.  Once connected, the
52 // Quake game protocol (documented elsewhere) is used.
53 //
54 //
55 // General notes:
56 //      game_name is currently always "QUAKE", but is there so this same protocol
57 //              can be used for future games as well; can you say Quake2?
58 //
59 // CCREQ_CONNECT
60 //              string  game_name                               "QUAKE"
61 //              byte    net_protocol_version    NET_PROTOCOL_VERSION
62 //
63 // CCREQ_SERVER_INFO
64 //              string  game_name                               "QUAKE"
65 //              byte    net_protocol_version    NET_PROTOCOL_VERSION
66 //
67 // CCREQ_PLAYER_INFO
68 //              byte    player_number
69 //
70 // CCREQ_RULE_INFO
71 //              string  rule
72 //
73 //
74 //
75 // CCREP_ACCEPT
76 //              long    port
77 //
78 // CCREP_REJECT
79 //              string  reason
80 //
81 // CCREP_SERVER_INFO
82 //              string  server_address
83 //              string  host_name
84 //              string  level_name
85 //              byte    current_players
86 //              byte    max_players
87 //              byte    protocol_version        NET_PROTOCOL_VERSION
88 //
89 // CCREP_PLAYER_INFO
90 //              byte    player_number
91 //              string  name
92 //              long    colors
93 //              long    frags
94 //              long    connect_time
95 //              string  address
96 //
97 // CCREP_RULE_INFO
98 //              string  rule
99 //              string  value
100
101 //      note:
102 //              There are two address forms used above.  The short form is just a
103 //              port number.  The address that goes along with the port is defined as
104 //              "whatever address you receive this reponse from".  This lets us use
105 //              the host OS to solve the problem of multiple host addresses (possibly
106 //              with no routing between them); the host will use the right address
107 //              when we reply to the inbound connection request.  The long from is
108 //              a full address and port in a string.  It is used for returning the
109 //              address of a server that is not running locally.
110
111 #define CCREQ_CONNECT           0x01
112 #define CCREQ_SERVER_INFO       0x02
113 #define CCREQ_PLAYER_INFO       0x03
114 #define CCREQ_RULE_INFO         0x04
115
116 #define CCREP_ACCEPT            0x81
117 #define CCREP_REJECT            0x82
118 #define CCREP_SERVER_INFO       0x83
119 #define CCREP_PLAYER_INFO       0x84
120 #define CCREP_RULE_INFO         0x85
121
122 typedef struct qsocket_s
123 {
124         struct qsocket_s        *next;
125         double                  connecttime;
126         double                  lastMessageTime;
127         double                  lastSendTime;
128
129         qboolean                disconnected;
130         qboolean                canSend;
131         qboolean                sendNext;
132
133         int                             driver;
134         int                             landriver;
135         int                             socket;
136         void                    *driverdata;
137
138         unsigned int    ackSequence;
139         unsigned int    sendSequence;
140         unsigned int    unreliableSendSequence;
141         int                             sendMessageLength;
142         qbyte                   sendMessage [NET_MAXMESSAGE];
143
144         unsigned int    receiveSequence;
145         unsigned int    unreliableReceiveSequence;
146         int                             receiveMessageLength;
147         qbyte                   receiveMessage [NET_MAXMESSAGE];
148
149         struct qsockaddr        addr;
150         char                            address[NET_NAMELEN];
151 } qsocket_t;
152
153 extern qsocket_t        *net_activeSockets;
154 extern mempool_t *net_mempool;
155
156 typedef struct
157 {
158         char            *name;
159         qboolean        initialized;
160         int                     controlSock;
161         int                     (*Init) (void);
162         void            (*Shutdown) (void);
163         void            (*Listen) (qboolean state);
164         int             (*OpenSocket) (int port);
165         int             (*CloseSocket) (int socket);
166         int             (*Connect) (int socket, struct qsockaddr *addr);
167         int             (*CheckNewConnections) (void);
168         int             (*Recv) (qbyte *buf, int len, struct qsockaddr *addr);
169         int                     (*Send) (qbyte *buf, int len, struct qsockaddr *addr);
170         int             (*Read) (int socket, qbyte *buf, int len, struct qsockaddr *addr);
171         int             (*Write) (int socket, qbyte *buf, int len, struct qsockaddr *addr);
172         int             (*Broadcast) (int socket, qbyte *buf, int len);
173         char *          (*AddrToString) (struct qsockaddr *addr);
174         int             (*StringToAddr) (const char *string, struct qsockaddr *addr);
175         int             (*GetSocketAddr) (int socket, struct qsockaddr *addr);
176         int             (*GetNameFromAddr) (struct qsockaddr *addr, char *name);
177         int             (*GetAddrFromName) (const char *name, struct qsockaddr *addr);
178         int                     (*AddrCompare) (struct qsockaddr *addr1, struct qsockaddr *addr2);
179         int                     (*GetSocketPort) (struct qsockaddr *addr);
180         int                     (*SetSocketPort) (struct qsockaddr *addr, int port);
181 } net_landriver_t;
182
183 #define MAX_NET_DRIVERS         8
184 extern int                              net_numlandrivers;
185 extern net_landriver_t  net_landrivers[MAX_NET_DRIVERS];
186
187 typedef struct
188 {
189         char            *name;
190         qboolean        initialized;
191         int                     (*Init) (void);
192         void            (*Listen) (qboolean state);
193         void            (*SearchForHosts) (qboolean xmit);
194         qboolean        (*SearchForInetHosts) (char *master);
195         qsocket_t       *(*Connect) (char *host);
196         qsocket_t       *(*CheckNewConnections) (void);
197         int                     (*QGetMessage) (qsocket_t *sock);
198         int                     (*QSendMessage) (qsocket_t *sock, sizebuf_t *data);
199         int                     (*SendUnreliableMessage) (qsocket_t *sock, sizebuf_t *data);
200         qboolean        (*CanSendMessage) (qsocket_t *sock);
201         qboolean        (*CanSendUnreliableMessage) (qsocket_t *sock);
202         void            (*Close) (qsocket_t *sock);
203         void            (*Shutdown) (void);
204         void            (*Heartbeat) (char *host);
205         int                     controlSock;
206 } net_driver_t;
207
208 extern int                      net_numdrivers;
209 extern net_driver_t     net_drivers[MAX_NET_DRIVERS];
210
211 extern int                      DEFAULTnet_hostport;
212 extern int                      net_hostport;
213
214 extern int net_driverlevel;
215 extern cvar_t           hostname;
216 extern char                     playername[];
217 extern int                      playercolor;
218
219 extern int              messagesSent;
220 extern int              messagesReceived;
221 extern int              unreliableMessagesSent;
222 extern int              unreliableMessagesReceived;
223
224 qsocket_t *NET_NewQSocket (void);
225 void NET_FreeQSocket(qsocket_t *);
226 double SetNetTime(void);
227
228
229 #define HOSTCACHESIZE   8
230
231 typedef struct
232 {
233         char    name[16];
234         char    map[16];
235         char    cname[32];
236         int             users;
237         int             maxusers;
238         int             driver;
239         int             ldriver;
240         struct qsockaddr addr;
241 } hostcache_t;
242
243 extern int hostCacheCount;
244 extern hostcache_t hostcache[HOSTCACHESIZE];
245
246 #if !defined(_WIN32 ) && !defined (__linux__) && !defined (__sun__)
247 #ifndef htonl
248 extern unsigned long htonl (unsigned long hostlong);
249 #endif
250 #ifndef htons
251 extern unsigned short htons (unsigned short hostshort);
252 #endif
253 #ifndef ntohl
254 extern unsigned long ntohl (unsigned long netlong);
255 #endif
256 #ifndef ntohs
257 extern unsigned short ntohs (unsigned short netshort);
258 #endif
259 #endif
260
261 //============================================================================
262 //
263 // public network functions
264 //
265 //============================================================================
266
267 extern  double          net_time;
268 extern  sizebuf_t       net_message;
269 extern  int                     net_activeconnections;
270
271 void            NET_Init (void);
272 void            NET_Shutdown (void);
273
274 struct qsocket_s        *NET_CheckNewConnections (void);
275 // returns a new connection number if there is one pending, else -1
276
277 struct qsocket_s        *NET_Connect (char *host);
278 // called by client to connect to a host.  Returns -1 if not able to
279
280 void            NET_Heartbeat (void);
281 // Send an heartbeat to the master server(s)
282
283 qboolean NET_CanSendMessage (qsocket_t *sock);
284 // Returns true or false if the given qsocket can currently accept a
285 // message to be transmitted.
286
287 int                     NET_GetMessage (struct qsocket_s *sock);
288 // returns data in net_message sizebuf
289 // returns 0 if no data is waiting
290 // returns 1 if a message was received
291 // returns 2 if an unreliable message was received
292 // returns -1 if the connection died
293
294 int                     NET_SendMessage (struct qsocket_s *sock, sizebuf_t *data);
295 int                     NET_SendUnreliableMessage (struct qsocket_s *sock, sizebuf_t *data);
296 // returns 0 if the message connot be delivered reliably, but the connection
297 //              is still considered valid
298 // returns 1 if the message was sent properly
299 // returns -1 if the connection died
300
301 int                     NET_SendToAll(sizebuf_t *data, int blocktime);
302 // This is a reliable *blocking* send to all attached clients.
303
304
305 void            NET_Close (struct qsocket_s *sock);
306 // if a dead connection is returned by a get or send function, this function
307 // should be called when it is convenient
308
309 // Server calls when a client is kicked off for a game related misbehavior
310 // like an illegal protocal conversation.  Client calls when disconnecting
311 // from a server.
312 // A netcon_t number will not be reused until this function is called for it
313
314 void NET_Poll(void);
315
316
317 typedef struct _PollProcedure
318 {
319         struct _PollProcedure   *next;
320         double                                  nextTime;
321         void                                    (*procedure)();
322         void                                    *arg;
323 } PollProcedure;
324
325 void SchedulePollProcedure(PollProcedure *pp, double timeOffset);
326
327 extern  qboolean        ipxAvailable;
328 extern  qboolean        tcpipAvailable;
329 extern  char            my_ipx_address[NET_NAMELEN];
330 extern  char            my_tcpip_address[NET_NAMELEN];
331
332 extern  qboolean        slistInProgress;
333 extern  qboolean        slistSilent;
334 extern  qboolean        slistLocal;
335
336 extern cvar_t hostname;
337
338 void NET_Slist_f (void);
339 void NET_InetSlist_f (void);
340
341 #endif
342