git-svn-id: http://bladebattles.com/kurok/SVN@11 20cd92bb-ff49-0410-b73e-96a06e42c3b9
[kurok.git] / net.h
blobdf078e4e3dbe1503004df0315fea89ff0f484c14
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
20 // net.h -- quake's interface to the networking layer
22 struct qsockaddr
24 #ifdef PSP
25 unsigned char sa_len;
26 unsigned char sa_family;
27 #else
28 short sa_family;
29 #endif
30 unsigned char sa_data[14];
34 #define NET_NAMELEN 64
36 #define NET_MAXMESSAGE 8192
37 #define NET_HEADERSIZE (2 * sizeof(unsigned int))
38 #define NET_DATAGRAMSIZE (MAX_DATAGRAM + NET_HEADERSIZE)
40 // NetHeader flags
41 #define NETFLAG_LENGTH_MASK 0x0000ffff
42 #define NETFLAG_DATA 0x00010000
43 #define NETFLAG_ACK 0x00020000
44 #define NETFLAG_NAK 0x00040000
45 #define NETFLAG_EOM 0x00080000
46 #define NETFLAG_UNRELIABLE 0x00100000
47 #define NETFLAG_CTL 0x80000000
50 #define NET_PROTOCOL_VERSION 3
52 // This is the network info/connection protocol. It is used to find Quake
53 // servers, get info about them, and connect to them. Once connected, the
54 // Quake game protocol (documented elsewhere) is used.
57 // General notes:
58 // game_name is currently always "QUAKE", but is there so this same protocol
59 // can be used for future games as well; can you say Quake2?
61 // CCREQ_CONNECT
62 // string game_name "QUAKE"
63 // byte net_protocol_version NET_PROTOCOL_VERSION
65 // CCREQ_SERVER_INFO
66 // string game_name "QUAKE"
67 // byte net_protocol_version NET_PROTOCOL_VERSION
69 // CCREQ_PLAYER_INFO
70 // byte player_number
72 // CCREQ_RULE_INFO
73 // string rule
77 // CCREP_ACCEPT
78 // long port
80 // CCREP_REJECT
81 // string reason
83 // CCREP_SERVER_INFO
84 // string server_address
85 // string host_name
86 // string level_name
87 // byte current_players
88 // byte max_players
89 // byte protocol_version NET_PROTOCOL_VERSION
91 // CCREP_PLAYER_INFO
92 // byte player_number
93 // string name
94 // long colors
95 // long frags
96 // long connect_time
97 // string address
99 // CCREP_RULE_INFO
100 // string rule
101 // string value
103 // note:
104 // There are two address forms used above. The short form is just a
105 // port number. The address that goes along with the port is defined as
106 // "whatever address you receive this reponse from". This lets us use
107 // the host OS to solve the problem of multiple host addresses (possibly
108 // with no routing between them); the host will use the right address
109 // when we reply to the inbound connection request. The long from is
110 // a full address and port in a string. It is used for returning the
111 // address of a server that is not running locally.
113 #define CCREQ_CONNECT 0x01
114 #define CCREQ_SERVER_INFO 0x02
115 #define CCREQ_PLAYER_INFO 0x03
116 #define CCREQ_RULE_INFO 0x04
118 #define CCREP_ACCEPT 0x81
119 #define CCREP_REJECT 0x82
120 #define CCREP_SERVER_INFO 0x83
121 #define CCREP_PLAYER_INFO 0x84
122 #define CCREP_RULE_INFO 0x85
124 typedef struct qsocket_s
126 struct qsocket_s *next;
127 double connecttime;
128 double lastMessageTime;
129 double lastSendTime;
131 qboolean disconnected;
132 qboolean canSend;
133 qboolean sendNext;
135 int driver;
136 int landriver;
137 int socket;
138 void *driverdata;
140 unsigned int ackSequence;
141 unsigned int sendSequence;
142 unsigned int unreliableSendSequence;
143 int sendMessageLength;
144 byte sendMessage [NET_MAXMESSAGE];
146 unsigned int receiveSequence;
147 unsigned int unreliableReceiveSequence;
148 int receiveMessageLength;
149 byte receiveMessage [NET_MAXMESSAGE];
151 struct qsockaddr addr;
152 char address[NET_NAMELEN];
154 } qsocket_t;
156 extern qsocket_t *net_activeSockets;
157 extern qsocket_t *net_freeSockets;
158 extern int net_numsockets;
160 typedef struct
162 char *name;
163 qboolean initialized;
164 int controlSock;
165 int (*Init) (void);
166 void (*Shutdown) (void);
167 void (*Listen) (qboolean state);
168 int (*OpenSocket) (int port);
169 int (*CloseSocket) (int socket);
170 int (*Connect) (int socket, struct qsockaddr *addr);
171 int (*CheckNewConnections) (void);
172 int (*Read) (int socket, byte *buf, int len, struct qsockaddr *addr);
173 int (*Write) (int socket, byte *buf, int len, struct qsockaddr *addr);
174 int (*Broadcast) (int socket, byte *buf, int len);
175 char * (*AddrToString) (struct qsockaddr *addr);
176 int (*StringToAddr) (char *string, struct qsockaddr *addr);
177 int (*GetSocketAddr) (int socket, struct qsockaddr *addr);
178 int (*GetNameFromAddr) (struct qsockaddr *addr, char *name);
179 int (*GetAddrFromName) (char *name, struct qsockaddr *addr);
180 int (*AddrCompare) (struct qsockaddr *addr1, struct qsockaddr *addr2);
181 int (*GetSocketPort) (struct qsockaddr *addr);
182 int (*SetSocketPort) (struct qsockaddr *addr, int port);
183 } net_landriver_t;
185 #define MAX_NET_DRIVERS 8
186 extern int net_numlandrivers;
187 extern net_landriver_t net_landrivers[MAX_NET_DRIVERS];
189 typedef struct
191 char *name;
192 qboolean initialized;
193 int (*Init) (void);
194 void (*Listen) (qboolean state);
195 void (*SearchForHosts) (qboolean xmit);
196 qsocket_t *(*Connect) (char *host);
197 qsocket_t *(*CheckNewConnections) (void);
198 int (*QGetMessage) (qsocket_t *sock);
199 int (*QSendMessage) (qsocket_t *sock, sizebuf_t *data);
200 int (*SendUnreliableMessage) (qsocket_t *sock, sizebuf_t *data);
201 qboolean (*CanSendMessage) (qsocket_t *sock);
202 qboolean (*CanSendUnreliableMessage) (qsocket_t *sock);
203 void (*Close) (qsocket_t *sock);
204 void (*Shutdown) (void);
205 int controlSock;
206 } net_driver_t;
208 extern int net_numdrivers;
209 extern net_driver_t net_drivers[MAX_NET_DRIVERS];
211 extern int DEFAULTnet_hostport;
212 extern int net_hostport;
214 extern int net_driverlevel;
215 extern cvar_t hostname;
216 extern char playername[];
217 extern int playercolor;
219 extern int messagesSent;
220 extern int messagesReceived;
221 extern int unreliableMessagesSent;
222 extern int unreliableMessagesReceived;
224 qsocket_t *NET_NewQSocket (void);
225 void NET_FreeQSocket(qsocket_t *);
226 double SetNetTime(void);
229 #define HOSTCACHESIZE 8
231 typedef struct
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;
243 extern int hostCacheCount;
244 extern hostcache_t hostcache[HOSTCACHESIZE];
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
261 #ifdef IDGODS
262 qboolean IsID(struct qsockaddr *addr);
263 #endif
265 //============================================================================
267 // public network functions
269 //============================================================================
271 extern double net_time;
272 extern sizebuf_t net_message;
273 extern int net_activeconnections;
275 void NET_Init (void);
276 void NET_Shutdown (void);
278 struct qsocket_s *NET_CheckNewConnections (void);
279 // returns a new connection number if there is one pending, else -1
281 struct qsocket_s *NET_Connect (char *host);
282 // called by client to connect to a host. Returns -1 if not able to
284 qboolean NET_CanSendMessage (qsocket_t *sock);
285 // Returns true or false if the given qsocket can currently accept a
286 // message to be transmitted.
288 int NET_GetMessage (struct qsocket_s *sock);
289 // returns data in net_message sizebuf
290 // returns 0 if no data is waiting
291 // returns 1 if a message was received
292 // returns 2 if an unreliable message was received
293 // returns -1 if the connection died
295 int NET_SendMessage (struct qsocket_s *sock, sizebuf_t *data);
296 int NET_SendUnreliableMessage (struct qsocket_s *sock, sizebuf_t *data);
297 // returns 0 if the message connot be delivered reliably, but the connection
298 // is still considered valid
299 // returns 1 if the message was sent properly
300 // returns -1 if the connection died
302 int NET_SendToAll(sizebuf_t *data, int blocktime);
303 // This is a reliable *blocking* send to all attached clients.
306 void NET_Close (struct qsocket_s *sock);
307 // if a dead connection is returned by a get or send function, this function
308 // should be called when it is convenient
310 // Server calls when a client is kicked off for a game related misbehavior
311 // like an illegal protocal conversation. Client calls when disconnecting
312 // from a server.
313 // A netcon_t number will not be reused until this function is called for it
315 void NET_Poll(void);
318 typedef struct _PollProcedure
320 struct _PollProcedure *next;
321 double nextTime;
322 void (*procedure)();
323 void *arg;
324 } PollProcedure;
326 void SchedulePollProcedure(PollProcedure *pp, double timeOffset);
328 extern qboolean serialAvailable;
329 extern qboolean ipxAvailable;
330 extern qboolean tcpipAvailable;
331 extern qboolean tcpipAdhoc;
333 extern char my_ipx_address[NET_NAMELEN];
334 extern char my_tcpip_address[NET_NAMELEN];
335 extern void (*GetComPortConfig) (int portNumber, int *port, int *irq, int *baud, qboolean *useModem);
336 extern void (*SetComPortConfig) (int portNumber, int port, int irq, int baud, qboolean useModem);
337 extern void (*GetModemConfig) (int portNumber, char *dialType, char *clear, char *init, char *hangup);
338 extern void (*SetModemConfig) (int portNumber, char *dialType, char *clear, char *init, char *hangup);
340 extern qboolean slistInProgress;
341 extern qboolean slistSilent;
342 extern qboolean slistLocal;
344 void NET_Slist_f (void);