comparison src/packets.c @ 4:aa38447a4b21

First entry of Paradise Server 2.9 patch 10 Beta
author darius
date Sat, 06 Dec 1997 04:37:03 +0000
parents
children
comparison
equal deleted inserted replaced
3:cafa94d86546 4:aa38447a4b21
1 /*--------------------------------------------------------------------------
2 NETREK II -- Paradise
3
4 Permission to use, copy, modify, and distribute this software and its
5 documentation, or any derivative works thereof, for any NON-COMMERCIAL
6 purpose and without fee is hereby granted, provided that this copyright
7 notice appear in all copies. No representations are made about the
8 suitability of this software for any purpose. This software is provided
9 "as is" without express or implied warranty.
10
11 Xtrek Copyright 1986 Chris Guthrie
12 Netrek (Xtrek II) Copyright 1989 Kevin P. Smith
13 Scott Silvey
14 Paradise II (Netrek II) Copyright 1993 Larry Denys
15 Kurt Olsen
16 Brandon Gillespie
17 --------------------------------------------------------------------------*/
18
19 #include "config.h"
20 #include <stddef.h>
21 #include <stdio.h>
22
23 #include "defs.h"
24 #include "packets.h"
25 #include "gppackets.h"
26 #include "wtext.h"
27
28 size_t client_packet_sizes[] = {
29 0,
30 sizeof(struct mesg_cpacket),
31 sizeof(struct speed_cpacket),
32 sizeof(struct dir_cpacket),
33 sizeof(struct phaser_cpacket),
34 sizeof(struct plasma_cpacket),
35 sizeof(struct torp_cpacket),
36 sizeof(struct quit_cpacket),
37 sizeof(struct login_cpacket),
38 sizeof(struct outfit_cpacket),
39 /* 10 v */
40 sizeof(struct war_cpacket),
41 sizeof(struct practr_cpacket),
42 sizeof(struct shield_cpacket),
43 sizeof(struct repair_cpacket),
44 sizeof(struct orbit_cpacket),
45 sizeof(struct planlock_cpacket),
46 sizeof(struct playlock_cpacket),
47 sizeof(struct bomb_cpacket),
48 sizeof(struct beam_cpacket),
49 sizeof(struct cloak_cpacket),
50 /* 20 v */
51 sizeof(struct det_torps_cpacket),
52 sizeof(struct det_mytorp_cpacket),
53 sizeof(struct copilot_cpacket),
54 sizeof(struct refit_cpacket),
55 sizeof(struct tractor_cpacket),
56 sizeof(struct repress_cpacket),
57 sizeof(struct coup_cpacket),
58 sizeof(struct socket_cpacket),
59 sizeof(struct options_cpacket),
60 sizeof(struct bye_cpacket),
61 /* 30 v */
62 sizeof(struct dockperm_cpacket),
63 sizeof(struct updates_cpacket),
64 sizeof(struct resetstats_cpacket),
65 sizeof(struct reserved_cpacket),
66 sizeof(struct scan_cpacket),
67 sizeof(struct udp_req_cpacket),
68 sizeof(struct sequence_cpacket),
69 sizeof(struct rsa_key_cpacket),
70 sizeof(struct obvious_packet),
71 0,
72 /* 40 v */
73 0,
74 0,
75 sizeof(struct ping_cpacket),
76 #ifdef SHORT_PACKETS
77 sizeof(struct shortreq_cpacket),
78 sizeof(struct threshold_cpacket),
79 0, /* CP_S_MESSAGE */
80 0, /* CP_S_RESERVED */
81 0, /* CP_S_DUMMY */
82 #else
83 0,
84 0,
85 0,
86 0,
87 0,
88 #endif
89 0,
90 0,
91 /* 50 v */
92 0,
93 0,
94 0,
95 0,
96 0,
97 0,
98 0,
99 0,
100 0,
101 0,
102 /* 60 v */
103 #ifdef FEATURE
104 sizeof(struct feature_cpacket)/* CP_FEATURE */
105 #else
106 0
107 #endif
108 };
109
110 #define num_cpacket_sizes sizeof(client_packet_sizes)/sizeof(*client_packet_sizes)
111
112 int
113 size_of_cpacket(pkt)
114 void *pkt;
115 {
116 CARD8 type;
117 CARD8 subtype;
118
119 type = ((CARD8 *) pkt)[0];
120 subtype = ((CARD8 *) pkt)[1];
121
122 if (type < num_cpacket_sizes && client_packet_sizes[type] > 0)
123 return client_packet_sizes[type];
124
125 switch (type)
126 {
127 #ifdef CP_FIRE_WEAPON
128 case CP_FIRE_WEAPON:
129 {
130 struct fire_weapon_cpacket *fwp = pkt;
131 return (fwp->mech == TM_POSITION) ? 12 : 4;
132 }
133 #endif
134
135 #ifdef SHORT_PACKETS
136 case CP_S_MESSAGE:
137 return ((unsigned char *) pkt)[3];
138 case CP_S_RESERVED:
139 case CP_S_DUMMY:
140 /* hmm? good question */
141 return 0;
142 #endif /* SHORT_PACKETS */
143
144 default:
145 return 0;
146 }
147 }
148
149
150 int server_packet_sizes[] = {
151 0, /* record 0 */
152 sizeof(struct mesg_spacket), /* SP_MESSAGE */
153 sizeof(struct plyr_info_spacket), /* SP_PLAYER_INFO */
154 sizeof(struct kills_spacket), /* SP_KILLS */
155 sizeof(struct player_spacket),/* SP_PLAYER */
156 sizeof(struct torp_info_spacket), /* SP_TORP_INFO */
157 sizeof(struct torp_spacket), /* SP_TORP */
158 sizeof(struct phaser_spacket),/* SP_PHASER */
159 sizeof(struct plasma_info_spacket), /* SP_PLASMA_INFO */
160 sizeof(struct plasma_spacket),/* SP_PLASMA */
161 /* 10 v */
162 sizeof(struct warning_spacket), /* SP_WARNING */
163 sizeof(struct motd_spacket), /* SP_MOTD */
164 sizeof(struct you_spacket), /* SP_YOU */
165 sizeof(struct queue_spacket), /* SP_QUEUE */
166 sizeof(struct status_spacket),/* SP_STATUS */
167 sizeof(struct planet_spacket),/* SP_PLANET */
168 sizeof(struct pickok_spacket),/* SP_PICKOK */
169 sizeof(struct login_spacket), /* SP_LOGIN */
170 sizeof(struct flags_spacket), /* SP_FLAGS */
171 sizeof(struct mask_spacket), /* SP_MASK */
172 /* 20 v */
173 sizeof(struct pstatus_spacket), /* SP_PSTATUS */
174 sizeof(struct badversion_spacket), /* SP_BADVERSION */
175 sizeof(struct hostile_spacket), /* SP_HOSTILE */
176 sizeof(struct stats_spacket), /* SP_STATS */
177 sizeof(struct plyr_login_spacket), /* SP_PL_LOGIN */
178 sizeof(struct reserved_spacket), /* SP_RESERVED */
179 sizeof(struct planet_loc_spacket), /* SP_PLANET_LOC */
180 sizeof(struct scan_spacket), /* SP_SCAN (ATM) */
181 sizeof(struct udp_reply_spacket), /* SP_UDP_REPLY */
182 sizeof(struct sequence_spacket), /* SP_SEQUENCE */
183 /* 30 v */
184 sizeof(struct sc_sequence_spacket), /* SP_SC_SEQUENCE */
185 sizeof(struct rsa_key_spacket), /* SP_RSA_KEY */
186 sizeof(struct motd_pic_spacket), /* SP_MOTD_PIC */
187 sizeof(struct stats_spacket2),/* SP_STATS2 */
188 sizeof(struct status_spacket2), /* SP_STATUS2 */
189 sizeof(struct planet_spacket2), /* SP_PLANET2 */
190 sizeof(struct obvious_packet),/* SP_NEW_MOTD */
191 sizeof(struct thingy_spacket),/* SP_THINGY */
192 sizeof(struct thingy_info_spacket), /* SP_THINGY_INFO */
193 sizeof(struct ship_cap_spacket), /* SP_SHIP_CAP */
194 /* 40 v */
195 #ifdef SHORT_PACKETS
196 sizeof(struct shortreply_spacket), /* SP_S_REPLY */
197 -1, /* SP_S_MESSAGE */
198 -1, /* SP_S_WARNING */
199 sizeof(struct youshort_spacket), /* SP_S_YOU */
200 sizeof(struct youss_spacket), /* SP_S_YOU_SS */
201 -1, /* SP_S_PLAYER */
202 #else
203 -1,
204 -1,
205 -1,
206 -1,
207 -1,
208 -1,
209 #endif
210 sizeof(struct ping_spacket), /* SP_PING */
211 -1, /* SP_S_TORP */
212 -1, /* SP_S_TORP_INFO */
213 20, /* SP_S_8_TORP */
214 /* 50 v */
215 -1, /* SP_S_PLANET */
216 -1, /* SP_GPARAM */
217 -1, /* SP_PARADISE_EXT1 */
218 sizeof(struct terrain_packet2), /* SP_TERRAIN2 */
219 sizeof(struct terrain_info_packet2), /* SP_TERRAIN_INFO2 */
220 -1,
221 -1,
222 -1,
223 -1,
224 -1,
225 /* 60 v */
226 #ifdef FEATURE
227 sizeof(struct feature_spacket), /* SP_FEATURE */
228 #else
229 -1,
230 #endif
231 -1 /* The fools! I'm pretty sure there is an */
232 /* off-by-one in the test in the last fnc */
233 /* but I'm not sure and this WILL fix it. */
234 };
235
236 #define num_spacket_sizes (sizeof(server_packet_sizes) / sizeof(server_packet_sizes[0]) - 1)
237
238 #ifdef SHORT_PACKETS
239 unsigned char numofbits[256] =
240 {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1,
241 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1,
242 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2,
243 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1,
244 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2,
245 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2,
246 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3,
247 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 1,
248 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2,
249 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2,
250 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3,
251 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2,
252 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3,
253 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 3,
254 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4,
255 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8};
256
257 static int vtsize[9] =
258 {4, 8, 8, 12, 12, 16, 20, 20, 24}; /* How big is the torppacket */
259 int vtisize[9] =
260 {4, 7, 9, 11, 13, 16, 18, 20, 22}; /* 4 byte Header + torpdata */
261
262
263 static int
264 padto4(sz)
265 int sz;
266 {
267 return (sz % 4) ? (sz / 4 + 1) * 4 : sz;
268
269 }
270 #endif
271
272 int
273 size_of_spacket(pkt)
274 unsigned char *pkt;
275 {
276 switch (pkt[0])
277 {
278 case SP_GPARAM:
279 switch (pkt[1])
280 {
281 case 0:
282 return sizeof(struct gp_sizes_spacket);
283 case 1:
284 return sizeof(struct gp_team_spacket);
285 case 2:
286 return sizeof(struct gp_teamlogo_spacket);
287 case 3:
288 return sizeof(struct gp_shipshape_spacket);
289 case 4:
290 return sizeof(struct gp_shipbitmap_spacket);
291 case 5:
292 return sizeof(struct gp_rank_spacket);
293 case 6:
294 return sizeof(struct gp_royal_spacket);
295 case 7:
296 return sizeof(struct gp_teamplanet_spacket);
297 default:
298 return 0;
299 }
300 #ifdef SHORT_PACKETS
301 case SP_S_MESSAGE:
302 return padto4(pkt[4]); /* IMPORTANT Changed */
303 case SP_S_WARNING:
304 if (pkt[1] == STEXTE_STRING ||
305 pkt[1] == SHORT_WARNING)
306 {
307 return padto4(pkt[3]);
308 }
309 else
310 return 4; /* Normal Packet */
311 case SP_S_PLAYER:
312 if (pkt[1] & 128)
313 { /* Small +extended Header */
314 return padto4(((pkt[1] & 0x3f) * 4) + 4);
315 }
316 else if (pkt[1] & 64)
317 { /* Small Header */
318 return padto4(((pkt[1] & 0x3f) * 4) + 4);
319 }
320 else
321 { /* Big Header */
322 return padto4((pkt[1] * 4 + 12));
323 }
324 case SP_S_TORP:
325 return padto4(vtsize[numofbits[pkt[1]]]);
326 case SP_S_TORP_INFO:
327 return padto4((vtisize[numofbits[pkt[1]]] + numofbits[pkt[3]]));
328 case SP_S_PLANET:
329 return padto4((pkt[1] * VPLANET_SIZE) + 2);
330 #endif
331 case SP_PARADISE_EXT1:
332 switch (pkt[1])
333 {
334 case SP_PE1_MISSING_BITMAP:
335 return sizeof(struct pe1_missing_bitmap_spacket);
336 case SP_PE1_NUM_MISSILES:
337 return sizeof(struct pe1_num_missiles_spacket);
338 default:
339 return 0;
340 }
341 default:
342 #ifdef FEATURE_DIAG
343 if (*pkt >= num_spacket_sizes)
344 {
345 fprintf(stderr, "Packet type %d out of range (%d)\n", (*pkt), num_spacket_sizes);
346 return (0);
347 }
348 if (server_packet_sizes[*pkt] < 0)
349 {
350 fprintf(stderr, "Packet type %d has no size\n", (*pkt));
351 return (0);
352 }
353 return (server_packet_sizes[*pkt]);
354 #else
355 return (*pkt < num_spacket_sizes && server_packet_sizes[*pkt] >= 0)
356 ? server_packet_sizes[*pkt] : 0;
357 #endif
358 }
359 }