… | |
… | |
71 | |
71 | |
72 | struct NsCmdMapping |
72 | struct NsCmdMapping |
73 | { |
73 | { |
74 | const char *cmdname; |
74 | const char *cmdname; |
75 | func_uint8_int_ns cmdproc; |
75 | func_uint8_int_ns cmdproc; |
|
|
76 | uint8 flag; |
76 | }; |
77 | }; |
77 | |
78 | |
78 | typedef void (*func_uint8_int_pl) (char *, int, player *); |
79 | typedef void (*func_uint8_int_pl) (char *, int, player *); |
79 | struct PlCmdMapping |
80 | struct PlCmdMapping |
80 | { |
81 | { |
… | |
… | |
107 | {"lock", (func_uint8_int_pl) LockItem, 1}, |
108 | {"lock", (func_uint8_int_pl) LockItem, 1}, |
108 | {"mark", (func_uint8_int_pl) MarkItem, 1}, |
109 | {"mark", (func_uint8_int_pl) MarkItem, 1}, |
109 | {"mapredraw", MapRedrawCmd, 0}, /* Added: phil */ |
110 | {"mapredraw", MapRedrawCmd, 0}, /* Added: phil */ |
110 | {"mapinfo", MapInfoCmd, 2}, /* CF+ */ |
111 | {"mapinfo", MapInfoCmd, 2}, /* CF+ */ |
111 | {"ext", ExtCmd, 2}, /* CF+ */ |
112 | {"ext", ExtCmd, 2}, /* CF+ */ |
112 | {NULL, NULL, 0} /* terminator */ |
113 | { 0 } /* terminator */ |
113 | }; |
114 | }; |
114 | |
115 | |
115 | /** Face-related commands */ |
116 | /** Face-related commands */ |
116 | static struct NsCmdMapping nscommands[] = { |
117 | static struct NsCmdMapping nscommands[] = { |
117 | {"addme", AddMeCmd}, |
118 | {"addme", AddMeCmd, 2}, |
118 | {"askface", SendFaceCmd}, /* Added: phil */ |
119 | {"askface", SendFaceCmd}, /* Added: phil */ |
119 | {"requestinfo", RequestInfo}, |
120 | {"requestinfo", RequestInfo}, |
120 | {"setfacemode", SetFaceMode}, |
121 | {"setfacemode", SetFaceMode, 2}, |
121 | {"setsound", SetSound}, |
122 | {"setsound", SetSound, 2}, |
122 | {"setup", SetUp}, |
123 | {"setup", SetUp, 2}, |
123 | {"version", VersionCmd}, |
124 | {"version", VersionCmd, 2}, |
124 | {"toggleextendedinfos", ToggleExtendedInfos}, /*Added: tchize */ |
125 | {"toggleextendedinfos", ToggleExtendedInfos, 2}, /*Added: tchize */ |
125 | {"toggleextendedtext", ToggleExtendedText}, /*Added: tchize */ |
126 | {"toggleextendedtext", ToggleExtendedText, 2}, /*Added: tchize */ |
126 | {"asksmooth", AskSmooth}, /*Added: tchize (smoothing technologies) */ |
127 | {"asksmooth", AskSmooth}, /*Added: tchize (smoothing technologies) */ |
127 | {NULL, NULL} /* terminator (I, II & III) */ |
128 | { 0 } /* terminator (I, II & III) */ |
128 | }; |
129 | }; |
129 | |
130 | |
130 | /** |
131 | /** |
131 | * RequestInfo is sort of a meta command. There is some specific |
132 | * RequestInfo is sort of a meta command. There is some specific |
132 | * request of information, but we call other functions to provide |
133 | * request of information, but we call other functions to provide |
… | |
… | |
166 | else if (!strcmp (buf, "skill_info")) |
167 | else if (!strcmp (buf, "skill_info")) |
167 | send_skill_info (ns, params); |
168 | send_skill_info (ns, params); |
168 | else if (!strcmp (buf, "spell_paths")) |
169 | else if (!strcmp (buf, "spell_paths")) |
169 | send_spell_paths (ns, params); |
170 | send_spell_paths (ns, params); |
170 | else |
171 | else |
171 | Write_String_To_Socket (ns, bigbuf, len); |
172 | ns->send_packet (bigbuf, len); |
172 | } |
173 | } |
173 | |
174 | |
174 | /** |
175 | /** |
175 | * Handle client input. |
176 | * Handle client input. |
176 | * |
177 | * |
… | |
… | |
180 | * with this socket, null if no player (one of the init_sockets for just |
181 | * with this socket, null if no player (one of the init_sockets for just |
181 | * starting a connection) |
182 | * starting a connection) |
182 | */ |
183 | */ |
183 | |
184 | |
184 | void |
185 | void |
185 | HandleClient (NewSocket * ns, player *pl) |
186 | HandleClient (NewSocket *ns, player *pl) |
186 | { |
187 | { |
187 | int len = 0, i, cnt; |
|
|
188 | char *data; |
|
|
189 | |
|
|
190 | /* Loop through this - maybe we have several complete packets here. */ |
188 | /* Loop through this - maybe we have several complete packets here. */ |
191 | // limit to a few commands only, though, as to not monopolise the server |
189 | // limit to a few commands only, though, as to not monopolise the server |
192 | for (cnt = 16; cnt--;) |
190 | for (int repeat = 16; repeat--;) |
193 | { |
191 | { |
194 | /* If it is a player, and they don't have any speed left, we |
192 | /* If it is a player, and they don't have any speed left, we |
195 | * return, and will read in the data when they do have time. |
193 | * return, and will read in the data when they do have time. |
196 | */ |
194 | */ |
197 | if (pl && pl->state == ST_PLAYING && pl->ob != NULL && pl->ob->speed_left < 0) |
195 | if (pl && pl->state == ST_PLAYING && pl->ob && pl->ob->speed_left < 0) |
198 | { |
|
|
199 | return; |
196 | return; |
|
|
197 | |
|
|
198 | int pkt_len = ns->read_packet (); |
|
|
199 | |
|
|
200 | if (pkt_len < 0) |
200 | } |
201 | { |
201 | |
202 | LOG (llevError, "read error on player %s\n", &pl->ob->name); |
202 | i = SockList_ReadPacket (ns->fd, &ns->inbuf, MAXSOCKBUF - 1); |
|
|
203 | |
|
|
204 | if (i < 0) |
|
|
205 | { |
|
|
206 | #ifdef ESRV_DEBUG |
|
|
207 | LOG (llevDebug, "HandleClient: Read error on connection player %s\n", (pl ? pl->ob->name : "None")); |
|
|
208 | #endif |
|
|
209 | /* Caller will take care of cleaning this up */ |
203 | /* Caller will take care of cleaning this up */ |
210 | ns->status = Ns_Dead; |
204 | ns->status = Ns_Dead; |
211 | return; |
205 | return; |
212 | } |
206 | } |
|
|
207 | else if (pkt_len == 0) |
213 | /* Still dont have a full packet */ |
208 | /* Still dont have a full packet */ |
214 | if (i == 0) |
|
|
215 | return; |
209 | return; |
216 | |
|
|
217 | // //D//TODO//temporarily log long commands |
|
|
218 | // if (ns->inbuf.len >= 40 && pl && pl->ob) |
|
|
219 | // LOG (llevDebug, "HandleClient: long comamnd from <%s,%s> %d<%s>\n", pl->ob->name, ns->host, ns->inbuf.len, ns->inbuf.buf + 2); |
|
|
220 | |
210 | |
221 | /* First, break out beginning word. There are at least |
211 | /* First, break out beginning word. There are at least |
222 | * a few commands that do not have any paremeters. If |
212 | * a few commands that do not have any paremeters. If |
223 | * we get such a command, don't worry about trying |
213 | * we get such a command, don't worry about trying |
224 | * to break it up. |
214 | * to break it up. |
225 | */ |
215 | */ |
|
|
216 | int datalen; |
226 | data = (char *) strchr ((char *) ns->inbuf.buf + 2, ' '); |
217 | char *data = strchr ((char *)ns->inbuf + 2, ' '); |
|
|
218 | |
227 | if (data) |
219 | if (data) |
228 | { |
220 | { |
229 | *data = '\0'; |
221 | *data++ = 0; |
230 | data++; |
222 | datalen = pkt_len - (data - (char *)ns->inbuf); |
231 | len = ns->inbuf.len - (data - (char *) ns->inbuf.buf); |
|
|
232 | } |
223 | } |
233 | else |
224 | else |
|
|
225 | { |
|
|
226 | data = (char *)ns->inbuf + 2; // better read garbage than segfault |
234 | len = 0; |
227 | datalen = 0; |
235 | |
|
|
236 | ns->inbuf.buf[ns->inbuf.len] = '\0'; /* Terminate buffer - useful for string data */ |
|
|
237 | for (i = 0; nscommands[i].cmdname != NULL; i++) |
|
|
238 | { |
228 | } |
|
|
229 | |
|
|
230 | ns->inbuf [pkt_len] = 0; /* Terminate buffer - useful for string data */ |
|
|
231 | |
|
|
232 | for (int i = 0; nscommands[i].cmdname; i++) |
|
|
233 | { |
239 | if (strcmp ((char *) ns->inbuf.buf + 2, nscommands[i].cmdname) == 0) |
234 | if (!strcmp ((char *)ns->inbuf + 2, nscommands[i].cmdname)) |
240 | { |
235 | { |
241 | nscommands[i].cmdproc ((char *) data, len, ns); |
236 | nscommands[i].cmdproc (data, datalen, ns); |
242 | ns->inbuf.len = 0; |
237 | ns->skip_packet (pkt_len); |
243 | return; //D// not doing this causes random memory corruption |
238 | //D// not doing this causes random memory corruption |
|
|
239 | if (nscommands[i].flag & 2) |
244 | goto next_packet; |
240 | goto next_packet; |
245 | } |
241 | return; |
246 | } |
242 | } |
|
|
243 | } |
|
|
244 | |
247 | /* Player must be in the playing state or the flag on the |
245 | /* Player must be in the playing state or the flag on the |
248 | * the command must be zero for the user to use the command - |
246 | * the command must be zero for the user to use the command - |
249 | * otherwise, a player cam save, be in the play_again state, and |
247 | * otherwise, a player cam save, be in the play_again state, and |
250 | * the map they were on gets swapped out, yet things that try to look |
248 | * the map they were on gets swapped out, yet things that try to look |
251 | * at the map causes a crash. If the command is valid, but |
249 | * at the map causes a crash. If the command is valid, but |
252 | * one they can't use, we still swallow it up. |
250 | * one they can't use, we still swallow it up. |
253 | */ |
251 | */ |
254 | if (pl) |
252 | if (pl) |
255 | for (i = 0; plcommands[i].cmdname != NULL; i++) |
253 | for (int i = 0; plcommands[i].cmdname; i++) |
256 | { |
254 | { |
257 | if (strcmp ((char *) ns->inbuf.buf + 2, plcommands[i].cmdname) == 0) |
255 | if (!strcmp ((char *)ns->inbuf + 2, plcommands[i].cmdname)) |
258 | { |
256 | { |
259 | if (pl->state == ST_PLAYING || !(plcommands[i].flag & 1)) |
257 | if (pl->state == ST_PLAYING || !(plcommands[i].flag & 1)) |
260 | plcommands[i].cmdproc ((char *) data, len, pl); |
258 | plcommands[i].cmdproc ((char *) data, datalen, pl); |
261 | ns->inbuf.len = 0; |
259 | |
|
|
260 | ns->skip_packet (pkt_len); |
262 | //D// not doing this causes random memory corruption |
261 | //D// not doing this causes random memory corruption |
263 | if (plcommands[i].flag & 2) |
262 | if (plcommands[i].flag & 2) |
264 | goto next_packet; |
263 | goto next_packet; |
|
|
264 | |
265 | return; |
265 | return; |
266 | } |
266 | } |
267 | } |
267 | } |
|
|
268 | |
|
|
269 | printf ("BP34n");//D |
268 | /* If we get here, we didn't find a valid command. Logging |
270 | /* If we get here, we didn't find a valid command. Logging |
269 | * this might be questionable, because a broken client/malicious |
271 | * this might be questionable, because a broken client/malicious |
270 | * user could certainly send a whole bunch of invalid commands. |
272 | * user could certainly send a whole bunch of invalid commands. |
271 | */ |
273 | */ |
272 | LOG (llevDebug, "Bad command from client (%s)\n", ns->inbuf.buf + 2); |
274 | LOG (llevDebug, "Bad command from client (%s)\n", ns->inbuf + 2); |
|
|
275 | ns->skip_packet (pkt_len); |
273 | next_packet: |
276 | next_packet: |
274 | ; |
277 | ; |
275 | } |
278 | } |
276 | } |
279 | } |
277 | |
280 | |
… | |
… | |
480 | for (i = 1; i < socket_info.allocated_sockets; i++) |
483 | for (i = 1; i < socket_info.allocated_sockets; i++) |
481 | { |
484 | { |
482 | if (init_sockets[i].status == Ns_Avail) |
485 | if (init_sockets[i].status == Ns_Avail) |
483 | continue; |
486 | continue; |
484 | |
487 | |
|
|
488 | //TODO: disassociate handleclient from socket readin |
485 | if (FD_ISSET (init_sockets[i].fd, &tmp_read)) |
489 | if (init_sockets[i].inbuf_len || FD_ISSET (init_sockets[i].fd, &tmp_read)) |
486 | HandleClient (&init_sockets[i], NULL); |
490 | HandleClient (&init_sockets[i], NULL); |
487 | |
491 | |
488 | if (FD_ISSET (init_sockets[i].fd, &tmp_write)) |
492 | if (FD_ISSET (init_sockets[i].fd, &tmp_write)) |
489 | init_sockets[i].can_write = 1; |
493 | init_sockets[i].can_write = 1; |
490 | } |
494 | } |