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