net.h 9.5 KB

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