1 // Emacs style mode select   -*- C++ -*-
2 //-----------------------------------------------------------------------------
3 //
4 // Copyright (C) 2000 by DooM Legacy Team.
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 //-----------------------------------------------------------------------------
17 
18 #ifndef _IPCS_H_
19 #define _IPCS_H_
20 
21 #include "common.h"
22 
23 #if defined (_WIN32) || defined ( __OS2__)
24 #include <io.h>
25 #include <sys/types.h>
26 typedef int socklen_t;
27 #if defined (__OS2__)
28 #include <netinet/in.h>
29 #endif
30 #endif
31 #ifdef _WIN32
32 #include <winsock.h>
33 #define close closesocket
34 #else
35 #include <arpa/inet.h>   // inet_addr(),...
36 #endif
37 
38 #ifndef SOCKET
39 #define SOCKET u_int
40 #endif
41 
42 // ================================ DEFINITIONS ===============================
43 
44 #define PACKET_SIZE 1024
45 #define MAX_CLIENT    512
46 
47 #ifndef _WIN32
48 #define NO_ERROR                      0
49 #define SOCKET_ERROR               -201
50 #endif
51 #define BIND_ERROR                 -202
52 #define CONNECT_ERROR              -203
53 #define LISTEN_ERROR               -204
54 #define ACCEPT_ERROR               -205
55 #define WRITE_ERROR                -210
56 #define READ_ERROR                 -211
57 #define CLOSE_ERROR                -212
58 #define GETHOSTBYNAME_ERROR        -220
59 #define SELECT_ERROR               -230
60 #define TIMEOUT_ERROR              -231
61 #define MALLOC_ERROR               -301
62 
63 #define INVALID_MSG                   -1
64 #define ACCEPT_MSG                   100
65 #define ADD_SERVER_MSG               101
66 #define ADD_CLIENT_MSG               102
67 #define REMOVE_SERVER_MSG            103
68 #define ADD_SERVERv2_MSG             104
69 #define GET_SERVER_MSG               200
70 #define SEND_SERVER_MSG              201
71 #define GET_LOGFILE_MSG              202
72 #define SEND_FILE_MSG                203
73 #define ERASE_LOGFILE_MSG            204
74 #define GET_SHORT_SERVER_MSG         205
75 #define SEND_SHORT_SERVER_MSG        206
76 #define ASK_SERVER_MSG               206
77 #define ANSWER_ASK_SERVER_MSG        207
78 #define GET_MOTD_MSG                 208
79 #define SEND_MOTD_MSG                209
80 #define GET_ROOMS_MSG				 210
81 #define SEND_ROOMS_MSG				 211
82 #define GET_ROOMS_HOST_MSG			 212
83 #define GET_VERSION_MSG				 213
84 #define SEND_VERSION_MSG			 214
85 #define GET_BANNED_MSG				 215
86 #define PING_SERVER_MSG				 216
87 
88 #define UDP_RECV_MSG                 300
89 #define TIMEOUT_MSG                  301
90 #define HTTP_REQUEST_MSG       875770417    // "4321"
91 #define SEND_HTTP_REQUEST_MSG  875770418    // "4322"
92 #define TEXT_REQUEST_MSG       825373494    // "1236"
93 #define SEND_TEXT_REQUEST_MSG  825373495    // "1237"
94 #define RSS92_REQUEST_MSG      825373496    // "1238"
95 #define SEND_RSS92_REQUEST_MSG 825373497    // "1239"
96 #define RSS10_REQUEST_MSG      825373744    // "1240"
97 #define SEND_RSS10_REQUEST_MSG 825373745    // "1241"
98 #define ADD_PSERVER_MSG        0xabacab81    // this number just need to be different than the others
99 #define REMOVE_PSERVER_MSG     0xabacab82
100 
101 // Sent FROM Client
102 #define LIVE_AUTH_USER				 600
103 #define LIVE_AUTH_KEY				 601
104 #define LIVE_GET_USER				 602
105 #define LIVE_UPDATE_LOCATION		 603
106 #define LIVE_UPDATE_PUBLIC_KEY		 604
107 #define LIVE_AUTH_PUBLIC_KEY		 605
108 
109 // Sent TO Client
110 #define LIVE_INVALID_KEY			 800
111 #define LIVE_INVALID_USER			 801
112 #define LIVE_AUTHORISED_KEY			 802
113 #define	LIVE_SEND_USER				 803
114 #define LIVE_VALIDATED_USER			 804
115 
116 // Location Types
117 #define LIVE_LOCATION_SP			 100
118 #define LIVE_LOCATION_MENU			 101
119 #define LIVE_LOCATION_MP_JOIN		 102
120 #define LIVE_LOCATION_MP_HOST		 103
121 #define LIVE_LOCATION_MP_LOCAL		 104
122 #define LIVE_LOCATION_MP_PRIVATE	 105
123 
124 #define HEADER_SIZE ((UINT32)sizeof (UINT32)*4)
125 
126 #define HEADER_MSG_POS      0
127 #define IP_MSG_POS         16
128 #define PORT_MSG_POS       32
129 #define HOSTNAME_MSG_POS   40
130 
131 #if defined(_MSC_VER)
132 #pragma pack(1)
133 #endif
134 
135 // Keep this structure 8 bytes aligned (current size is 80)
136 typedef struct
137 {
138 	char header[16]; // information such as password
139 	char ip[16];
140 	char port[8];
141 	char name[32];
142 	INT32 room;
143 	char version[8]; // format is: x.yy.z (like 1.30.2 or 1.31)
144 	char key[32]; // Secret key for linking dedicated servers to accounts
145 } ATTRPACK msg_server_t;
146 
147 typedef struct
148 {
149 	char header[16];
150 	UINT32 id;
151 	char name[32];
152 	char motd[256];
153 } ATTRPACK msg_rooms_t;
154 
155 typedef struct
156 {
157 	char header[16];
158 	char ipstart[16];
159 	char ipend[16];
160 	char endstamp[32];
161 	char reason[256];
162 	UINT8 hostonly;
163 } ATTRPACK msg_ban_t;
164 
165 typedef struct
166 {
167 	char header[16];
168 	INT32 id;
169 	char username[100];
170 	char password[32];
171 } ATTRPACK msg_live_auth_t;
172 
173 typedef struct
174 {
175 	char header[16];
176 	INT32 uid;
177 	char username[100];
178 	INT32 location_type;
179 	char location_ip[32];
180 	INT32 location_port;
181 	INT32 lastseen_type;
182 	char lastseen_data1[256];
183 	char lastseen_data2[256];
184 	char lastseen_data3[256];
185 } ATTRPACK msg_live_user_t;
186 
187 typedef struct
188 {
189 	char header[16];
190 	UINT8 location_type;
191 	char location_ip[32];
192 	INT32 location_port;
193 	char location_data1[256];
194 	char location_data2[256];
195 	char location_data3[256];
196 } ATTRPACK msg_live_updatelocation_t;
197 
198 typedef struct
199 {
200 	char header[16];
201 	char publickey[256];
202 	char username[256];
203 } ATTRPACK msg_live_validateuser_t;
204 
205 typedef struct
206 {
207 	char header[16];
208 	char username[256];
209 	UINT8 keytype;
210 	char keydata[256];
211 } ATTRPACK msg_live_update_key_t;
212 
213 
214 typedef struct
215 {
216 	UINT32 id;
217 	INT32 type;
218 	INT32 room;
219 	INT32 length;
220 	char buffer[PACKET_SIZE];
221 } ATTRPACK msg_t;
222 
223 #if defined(_MSC_VER)
224 #pragma pack()
225 #endif
226 
227 class CSocket
228 {
229 protected:
230 	sockaddr_in addr;
231 	msg_t msg;
232 	fd_set rset;
233 public:
234 	int getIP(const char *);
235 	CSocket();
236 	~CSocket();
237 };
238 
239 class CServerSocket : public CSocket
240 {
241 private:
242 	sockaddr_in udp_addr;
243 	sockaddr_in udp_in_addr;
244 	SOCKET udp_fd;
245 	SOCKET accept_fd;
246 	size_t num_clients;
247 	SOCKET client_fd[MAX_CLIENT];
248 	sockaddr_in client_addr[MAX_CLIENT];
249 
250 public:
251 	int deleteClient(size_t id);
252 	int listen(const char *str_port);
253 	int accept(void);
254 	int read(msg_t *msg);
255 	const char *getUdpIP(void);
256 	const char *getUdpPort(bool);
257 	int write(msg_t *msg);
258 	int writeUDP(const char *data, size_t length, const char *ip, UINT16 port);
259 	const char *getClientIP(size_t id);
260 	const char *getClientPort(size_t id);
261 	CServerSocket(void);
262 	~CServerSocket(void);
263 };
264 
265 class CClientSocket : public CSocket
266 {
267 private:
268 	SOCKET socket_fd;
269 public:
270 	int connect(const char *ip_addr, const char *str_port);
271 	int read(msg_t *msg);
272 	int write(msg_t *msg);
273 	CClientSocket(void);
274 	~CClientSocket(void);
275 };
276 
277 // ================================== PROTOS ==================================
278 
279 // ================================== EXTERNS =================================
280 
281 #endif
282