]> icculus.org git repositories - taylor/freespace2.git/blob - src/network/multi_kick.cpp
byte-swapping changes for bigendian systems
[taylor/freespace2.git] / src / network / multi_kick.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/Network/multi_kick.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * $Log$
16  * Revision 1.5  2004/06/11 01:18:40  tigital
17  * byte-swapping changes for bigendian systems
18  *
19  * Revision 1.4  2002/06/09 04:41:23  relnev
20  * added copyright header
21  *
22  * Revision 1.3  2002/05/27 00:40:47  theoddone33
23  * Fix net_addr vs net_addr_t
24  *
25  * Revision 1.2  2002/05/07 03:16:47  theoddone33
26  * The Great Newline Fix
27  *
28  * Revision 1.1.1.1  2002/05/03 03:28:10  root
29  * Initial import.
30  *  
31  * 
32  * 8     10/13/99 3:51p Jefff
33  * fixed unnumbered XSTRs
34  * 
35  * 7     3/10/99 6:50p Dave
36  * Changed the way we buffer packets for all clients. Optimized turret
37  * fired packets. Did some weapon firing optimizations.
38  * 
39  * 6     3/09/99 6:24p Dave
40  * More work on object update revamping. Identified several sources of
41  * unnecessary bandwidth.
42  * 
43  * 5     11/19/98 8:03a Dave
44  * Full support for D3-style reliable sockets. Revamped packet lag/loss
45  * system, made it receiver side and at the lowest possible level.
46  * 
47  * 4     11/17/98 11:12a Dave
48  * Removed player identification by address. Now assign explicit id #'s.
49  * 
50  * 3     11/05/98 5:55p Dave
51  * Big pass at reducing #includes
52  * 
53  * 2     10/07/98 10:53a Dave
54  * Initial checkin.
55  * 
56  * 1     10/07/98 10:50a Dave
57  * 
58  * 16    9/15/98 7:24p Dave
59  * Minor UI changes. Localized bunch of new text.
60  * 
61  * 15    9/11/98 5:53p Dave
62  * Final revisions to kick system changes.
63  * 
64  * 14    9/11/98 5:08p Dave
65  * More tweaks to kick notification system.
66  * 
67  * 13    9/11/98 4:14p Dave
68  * Fixed file checksumming of < file_size. Put in more verbose kicking and
69  * PXO stats store reporting.
70  * 
71  * 12    6/13/98 9:32p Mike
72  * Kill last character in file which caused "Find in Files" to report the
73  * file as "not a text file."
74  * 
75  * 11    6/13/98 6:01p Hoffoss
76  * Externalized all new (or forgot to be added) strings to all the code.
77  * 
78  * 10    4/23/98 6:18p Dave
79  * Store ETS values between respawns. Put kick feature in the text
80  * messaging system. Fixed text messaging system so that it doesn't
81  * process or trigger ship controls. Other UI fixes.
82  * 
83  * 9     4/04/98 4:22p Dave
84  * First rev of UDP reliable sockets is done. Seems to work well if not
85  * overly burdened.
86  * 
87  * 8     4/03/98 1:03a Dave
88  * First pass at unreliable guaranteed delivery packets.
89  * 
90  * 7     4/01/98 11:19p Dave
91  * Put in auto-loading of xferred pilot pic files. Grey out background
92  * behind pinfo popup. Put a chatbox message in when players are kicked.
93  * Moved mission title down in briefing. Other ui fixes.
94  * 
95  * 6     3/30/98 6:27p Dave
96  * Put in a more official set of multiplayer options, including a system
97  * for distributing netplayer and netgame settings.
98  * 
99  * 5     3/24/98 5:00p Dave
100  * Fixed several ui bugs. Put in pre and post voice stream playback sound
101  * fx. Put in error specific popups for clients getting dropped from games
102  * through actions other than their own.
103  * 
104  * 4     3/18/98 5:52p Dave
105  * Put in netgame password popup. Numerous ui changes. Laid groundwork for
106  * streamed multi_voice data.
107  * 
108  * 3     3/17/98 5:29p Dave
109  * Minor bug fixes in player select menu. Solidified mp joining process.
110  * Made furball mode support ingame joiners and dropped players correctly.
111  * 
112  * 2     3/15/98 4:17p Dave
113  * Fixed oberver hud problems. Put in handy netplayer macros. Reduced size
114  * of network orientation matrices.
115  * 
116  * 1     2/12/98 4:38p Dave
117  * Multiplayer kick functionality
118  * 
119  * $NoKeywords: $
120  */
121 #include "multi.h"
122 #include "multi_kick.h"
123 #include "multimsgs.h"
124 #include "multiutil.h"
125 #include "freespace.h"
126 #include "chatbox.h"
127 #include "timer.h"
128
129 // ----------------------------------------------------------------------------------
130 // KICK DEFINES/VARS
131 //
132
133 #define MULTI_KICK_RESPONSE_TIME                                                4000            // if someone who has been kicked has not responded in this time, disconnect him hard
134
135 #define MAX_BAN_SLOTS           30
136 net_addr_t Multi_kick_ban_slots[MAX_BAN_SLOTS];                         // banned addresses
137 int Multi_kick_num_ban_slots;                                                                           // the # of banned addresses
138                                                          
139 // ----------------------------------------------------------------------------------
140 // KICK FORWARD DECLARATIONS
141 //
142
143 // send a player kick packet
144 void send_player_kick_packet(int player_index, int ban = 1, int reason = KICK_REASON_NORM);
145
146 // process a player kick packet
147 void process_player_kick_packet(ubyte *data, header *hinfo);
148
149 // add a net address to the banned list
150 void multi_kick_add_ban(net_addr_t *addr);
151
152 // can the given player perform a kick
153 int multi_kick_can_kick(net_player *player);
154
155
156 // ----------------------------------------------------------------------------------
157 // KICK FUNCTIONS
158 //
159
160 // initialize all kicking details (ban lists, etc). it is safe to call this function at any time
161 void multi_kick_init()
162 {
163         // blast all the ban slots
164         memset(Multi_kick_ban_slots,0,sizeof(net_addr_t)*MAX_BAN_SLOTS);
165         Multi_kick_num_ban_slots = 0;
166 }
167
168 // process all kick details (disconnecting players who have been kicked but haven't closed their socket)
169 void multi_kick_process()
170 {
171         int idx;
172
173         // if i'm not the server, don't do anything
174         if(!(Net_player->flags & NETINFO_FLAG_AM_MASTER)){
175                 return;
176         }
177
178         // disconnect any kicked players who have timed out on leaving
179         for(idx=0;idx<MAX_PLAYERS;idx++){
180                 if(MULTI_CONNECTED(Net_players[idx]) && (Net_players[idx].s_info.kick_timestamp != -1) && timestamp_elapsed(Net_players[idx].s_info.kick_timestamp) ){
181                         delete_player(idx, Net_players[idx].s_info.kick_reason);
182                 }
183         }
184 }
185
186 // attempt to kick a player. return success or fail
187 void multi_kick_player(int player_index, int ban, int reason)
188 {       
189         // only the standalone should be able to kick the host of the game
190         if(!(Game_mode & GM_STANDALONE_SERVER) && ((Net_players[player_index].flags & NETINFO_FLAG_GAME_HOST) || (Net_players[player_index].flags & NETINFO_FLAG_AM_MASTER))){
191                 nprintf(("Network","Cannot kick the host or server of a game!\n"));
192         } else {
193                 // if we're the master, then delete the guy
194                 if(Net_player->flags & NETINFO_FLAG_AM_MASTER){
195                         // if we're supposed to ban him, add his address to the banned list
196                         if(ban){
197                                 multi_kick_add_ban(&Net_players[player_index].p_info.addr);
198                         }
199
200                         // mark him as having been kicked
201                         Net_players[player_index].flags |= NETINFO_FLAG_KICKED;
202
203                         // set his kick timestamp and send him a leave game packet
204                         Net_players[player_index].s_info.kick_timestamp = timestamp(MULTI_KICK_RESPONSE_TIME);
205                         Net_players[player_index].s_info.kick_reason = reason;
206                         send_leave_game_packet(Net_players[player_index].player_id, reason, &Net_players[player_index]);                                                        
207
208                         // tell everyone else that he was kicked
209                         send_leave_game_packet(Net_players[player_index].player_id, reason);
210                         
211                         // wait until he either shuts his connection down or he times out)
212                         // add the string to the chatbox and the hud (always safe - if it is not inited, nothing bad will happen)                       
213                         char str[512];
214                         memset(str, 0, 512);
215                         sprintf(str, XSTR("<kicking %s ...>", 1501), Net_players[player_index].player->callsign);
216                         multi_display_chat_msg(str, player_index, 0);                                                    
217                 }
218                 // otherwise, we should send the packet indicating that this guy should be kicked
219                 else {
220                         send_player_kick_packet(player_index, ban, reason);
221                 }
222         }
223 }
224
225 // is this net address currently kicked and banded
226 int multi_kick_is_banned(net_addr_t *addr)
227 {
228         int idx;
229         
230         // traverse the banned list
231         for(idx=0;idx<Multi_kick_num_ban_slots;idx++){
232                 // if we found a duplicate
233                 if(psnet_same(&Multi_kick_ban_slots[idx],addr)){
234                         return 1;
235                 }
236         }
237
238         // he's not banned
239         return 0;
240 }
241
242 // debug console function called to determine which player to kick
243 void multi_dcf_kick()
244 {
245         int player_num,idx;
246
247         // get the callsign of the player to kick
248         dc_get_arg(ARG_STRING);
249
250         if(strlen(Dc_arg) == 0){
251                 dc_printf("Invalid player callsign!\n");
252                 return ;
253         }
254
255         player_num = -1;
256         for(idx=0;idx<MAX_PLAYERS;idx++){
257                 if(MULTI_CONNECTED(Net_players[idx]) && (stricmp(Net_players[idx].player->callsign,Dc_arg)==0)){
258                         player_num = idx;
259                         break;
260                 }
261         }
262
263         // if we didn't find the player, notify of the results
264         if(player_num == -1){
265                 dc_printf("Could not find player %s to kick!",Dc_arg);
266         } 
267         // if we found the guy, then try and kick him
268         else {
269                 multi_kick_player(player_num);
270         }
271 }
272
273 // fill in the passed string with the appropriate "kicked" string
274 void multi_kick_get_text(net_player *pl, int reason, char *str)
275 {
276         // safety net
277         if((pl == NULL) || (pl->player == NULL)){
278                 strcpy(str, NOX(""));
279         }
280
281         switch(reason){
282         case KICK_REASON_BAD_XFER:
283                 sprintf(str, XSTR("<%s was kicked because of mission file xfer failure>", 1003), pl->player->callsign);
284                 break;
285         case KICK_REASON_CANT_XFER:
286                 sprintf(str, XSTR("<%s was kicked for not having builtin mission %s>", 1004), pl->player->callsign, Game_current_mission_filename);
287                 break;
288         case KICK_REASON_INGAME_ENDED:
289                 sprintf(str, XSTR("<%s was kicked for ingame joining an ended game>",1005), pl->player->callsign);
290                 break;
291         default:
292                 sprintf(str, XSTR("<%s was kicked>",687), pl->player->callsign);
293                 break;
294         }               
295 }
296
297
298 // ----------------------------------------------------------------------------------
299 // KICK FORWARD DEFINITIONS
300 //
301
302 // add a net address to the banned list
303 void multi_kick_add_ban(net_addr_t *addr)
304 {
305         // if we still have any slots left
306         if(Multi_kick_num_ban_slots < (MAX_BAN_SLOTS - 1)){
307                 memcpy(&Multi_kick_ban_slots[Multi_kick_num_ban_slots++],addr,sizeof(net_addr_t));
308         }
309 }
310
311
312 // ----------------------------------------------------------------------------------
313 // KICK PACKET HANDLERS
314 //
315
316 // send a player kick packet
317 void send_player_kick_packet(int player_index, int ban, int reason)
318 {               
319         ubyte data[MAX_PACKET_SIZE];
320         int packet_size = 0;
321
322         BUILD_HEADER(KICK_PLAYER);
323
324         // add the address of the player to be kicked
325         ADD_DATA_S16(Net_players[player_index].player_id);
326         
327         // indicate if he should be banned
328         ADD_DATA_S32(ban);
329         ADD_DATA_S32(reason);
330
331         // send the request to the server       
332         multi_io_send_reliable(Net_player, data, packet_size);
333 }
334
335 // process a player kick packet
336 void process_player_kick_packet(ubyte *data, header *hinfo)
337 {
338         int player_num,from_player,ban,reason;  
339         short player_id;
340         int offset = HEADER_LENGTH;
341         
342         // get the address of the guy who is to be kicked
343         GET_DATA_S16(player_id);
344         GET_DATA_S32(ban);
345         GET_DATA_S32(reason);
346         player_num = find_player_id(player_id);
347         PACKET_SET_SIZE();
348
349         // only the server should ever receive a request to kick a guy
350         Assert(Net_player->flags & NETINFO_FLAG_AM_MASTER);
351         
352         // determine who sent the packet        
353         from_player = find_player_id(hinfo->id);
354
355         // check to see if this guy is allowed to make such a request
356         if((from_player == -1) || !multi_kick_can_kick(&Net_players[from_player]) ){
357                 nprintf(("Network","Received a kick request from an invalid player!!\n"));
358         } 
359         // otherwise, process the request fully
360         else {
361                 // make sure we have a valid player to kick
362                 if(player_num == -1){
363                         nprintf(("Network","Received request to kick an unknown player!\n"));
364                 } else {
365                         // will handle all the rest of the details
366                         multi_kick_player(player_num,ban,reason);
367                 }
368         }
369 }
370
371 // can the given player perform a kick
372 int multi_kick_can_kick(net_player *player)
373 {
374         // only host or server can kick
375         if((player->flags & NETINFO_FLAG_AM_MASTER) || (player->flags & NETINFO_FLAG_GAME_HOST)){
376                 return 1;
377         }
378         
379         // this guy cannot kick
380         return 0;
381 }
382