1 | |
|
|
2 | /* |
|
|
3 | * static char *rcsid_sockets_c = |
|
|
4 | * "$Id: lowlevel.C,v 1.5 2006/09/10 13:43:33 root Exp $"; |
|
|
5 | */ |
|
|
6 | |
|
|
7 | /* |
1 | /* |
8 | CrossFire, A Multiplayer game for X-windows |
2 | CrossFire, A Multiplayer game for X-windows |
9 | |
3 | |
10 | Copyright (C) 1992 Frank Tore Johansen |
4 | Copyright (C) 1992 Frank Tore Johansen |
11 | |
5 | |
… | |
… | |
78 | } |
72 | } |
79 | } |
73 | } |
80 | |
74 | |
81 | int val; |
75 | int val; |
82 | |
76 | |
83 | val = 0; |
|
|
84 | setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val)); |
77 | val = 0; setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val)); |
85 | val = 1; |
|
|
86 | setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val)); |
78 | val = 1; setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val)); |
87 | #endif |
79 | #endif |
88 | } |
80 | } |
89 | |
81 | |
90 | /*********************************************************************** |
82 | /*********************************************************************** |
91 | * |
83 | * |
92 | * SockList functions/utilities |
84 | * SockList functions/utilities |
93 | * |
85 | * |
94 | **********************************************************************/ |
86 | **********************************************************************/ |
95 | |
87 | |
96 | void |
88 | SockList &SockList::operator <<(const data8 &v) |
97 | SockList_Init (SockList * sl) |
|
|
98 | { |
89 | { |
99 | sl->len = 0; |
90 | *this << uint8 (v.len); |
100 | sl->buf = NULL; |
|
|
101 | } |
|
|
102 | |
91 | |
103 | void |
92 | memcpy (buf + len, v.data, v.len); |
104 | SockList_AddInt (SockList * sl, uint32 data) |
93 | len += v.len; |
105 | { |
|
|
106 | sl->buf[sl->len++] = (data >> 24) & 0xff; |
|
|
107 | sl->buf[sl->len++] = (data >> 16) & 0xff; |
|
|
108 | sl->buf[sl->len++] = (data >> 8) & 0xff; |
|
|
109 | sl->buf[sl->len++] = data & 0xff; |
|
|
110 | } |
|
|
111 | |
94 | |
112 | void |
95 | return *this; |
113 | SockList_AddInt64 (SockList * sl, uint64 data) |
96 | } |
114 | { |
|
|
115 | sl->buf[sl->len++] = (char) ((data >> 56) & 0xff); |
|
|
116 | sl->buf[sl->len++] = (char) ((data >> 48) & 0xff); |
|
|
117 | sl->buf[sl->len++] = (char) ((data >> 40) & 0xff); |
|
|
118 | sl->buf[sl->len++] = (char) ((data >> 32) & 0xff); |
|
|
119 | |
97 | |
120 | sl->buf[sl->len++] = (char) ((data >> 24) & 0xff); |
98 | SockList &SockList::operator <<(const data16 &v) |
121 | sl->buf[sl->len++] = (char) ((data >> 16) & 0xff); |
99 | { |
122 | sl->buf[sl->len++] = (char) ((data >> 8) & 0xff); |
100 | *this << uint16 (v.len); |
123 | sl->buf[sl->len++] = (char) (data & 0xff); |
101 | |
|
|
102 | memcpy (buf + len, v.data, v.len); |
|
|
103 | len += v.len; |
|
|
104 | |
|
|
105 | return *this; |
124 | } |
106 | } |
125 | |
107 | |
126 | /* Basically does the reverse of SockList_AddInt, but on |
108 | /* Basically does the reverse of SockList_AddInt, but on |
127 | * strings instead. Same for the GetShort, but for 16 bits. |
109 | * strings instead. Same for the GetShort, but for 16 bits. |
128 | */ |
110 | */ |
… | |
… | |
163 | abort (); |
145 | abort (); |
164 | } |
146 | } |
165 | /* We already have a partial packet */ |
147 | /* We already have a partial packet */ |
166 | if (sl->len < 2) |
148 | if (sl->len < 2) |
167 | { |
149 | { |
168 | #ifdef WIN32 /* ***WIN32 SockList_ReadPacket: change read() to recv() */ |
|
|
169 | |
|
|
170 | stat = recv (fd, sl->buf + sl->len, 2 - sl->len, 0); |
|
|
171 | |
|
|
172 | #else |
|
|
173 | do |
150 | do |
174 | { |
151 | { |
175 | stat = read (fd, sl->buf + sl->len, 2 - sl->len); |
152 | stat = read (fd, sl->buf + sl->len, 2 - sl->len); |
176 | } |
153 | } |
177 | while ((stat == -1) && (errno == EINTR)); |
154 | while ((stat == -1) && (errno == EINTR)); |
178 | #endif |
155 | |
179 | if (stat < 0) |
156 | if (stat < 0) |
180 | { |
157 | { |
181 | /* In non blocking mode, EAGAIN is set when there is no |
158 | /* In non blocking mode, EAGAIN is set when there is no |
182 | * data available. |
159 | * data available. |
183 | */ |
160 | */ |
184 | #ifdef WIN32 /* ***WIN32 SockList_ReadPacket: error handling for win32 */ |
|
|
185 | if ((stat == -1) && WSAGetLastError () != WSAEWOULDBLOCK) |
|
|
186 | { |
|
|
187 | if (WSAGetLastError () == WSAECONNRESET) |
|
|
188 | LOG (llevDebug, "Connection closed by client\n"); |
|
|
189 | else |
|
|
190 | { |
|
|
191 | LOG (llevDebug, "ReadPacket got error %d, returning 0\n", WSAGetLastError ()); |
|
|
192 | } |
|
|
193 | return -1; /* kick this user! */ |
|
|
194 | } |
|
|
195 | #else |
|
|
196 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
161 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
197 | { |
162 | { |
198 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
163 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
199 | } |
164 | } |
200 | #endif |
|
|
201 | return 0; /*Error */ |
165 | return 0; /*Error */ |
202 | } |
166 | } |
203 | if (stat == 0) |
167 | if (stat == 0) |
204 | return -1; |
168 | return -1; |
205 | sl->len += stat; |
169 | sl->len += stat; |
… | |
… | |
219 | LOG (llevError, "SockList_ReadPacket: Want to read more bytes than will fit in buffer (%d>=%d).\n", toread + sl->len, len); |
183 | LOG (llevError, "SockList_ReadPacket: Want to read more bytes than will fit in buffer (%d>=%d).\n", toread + sl->len, len); |
220 | /* Quick hack in case for 'oldsocketmode' input. If we are |
184 | /* Quick hack in case for 'oldsocketmode' input. If we are |
221 | * closing the socket anyways, then reading this extra 100 bytes |
185 | * closing the socket anyways, then reading this extra 100 bytes |
222 | * shouldn't hurt. |
186 | * shouldn't hurt. |
223 | */ |
187 | */ |
224 | #ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */ |
|
|
225 | recv (fd, sl->buf + 2, 100, 0); |
|
|
226 | #else |
|
|
227 | read (fd, sl->buf + 2, 100); |
188 | read (fd, sl->buf + 2, 100); |
228 | #endif /* end win32 */ |
|
|
229 | |
189 | |
230 | /* return error so the socket is closed */ |
190 | /* return error so the socket is closed */ |
231 | return -1; |
191 | return -1; |
232 | } |
192 | } |
233 | do |
193 | do |
234 | { |
194 | { |
235 | #ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */ |
|
|
236 | stat = recv (fd, sl->buf + sl->len, toread, 0); |
|
|
237 | #else |
|
|
238 | do |
195 | do |
239 | { |
196 | { |
240 | stat = read (fd, sl->buf + sl->len, toread); |
197 | stat = read (fd, sl->buf + sl->len, toread); |
241 | } |
198 | } |
242 | while ((stat < 0) && (errno == EINTR)); |
199 | while ((stat < 0) && (errno == EINTR)); |
243 | #endif |
|
|
244 | if (stat < 0) |
200 | if (stat < 0) |
245 | { |
201 | { |
246 | |
202 | |
247 | #ifdef WIN32 /* ***win32 SockList_ReadPacket: change error handling for win32 */ |
|
|
248 | if ((stat == -1) && WSAGetLastError () != WSAEWOULDBLOCK) |
|
|
249 | { |
|
|
250 | if (WSAGetLastError () == WSAECONNRESET) |
|
|
251 | LOG (llevDebug, "Connection closed by client\n"); |
|
|
252 | else |
|
|
253 | { |
|
|
254 | LOG (llevDebug, "ReadPacket got error %d, returning 0\n", WSAGetLastError ()); |
|
|
255 | } |
|
|
256 | return -1; /* kick this user! */ |
|
|
257 | } |
|
|
258 | #else |
|
|
259 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
203 | if (errno != EAGAIN && errno != EWOULDBLOCK) |
260 | { |
204 | { |
261 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
205 | LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); |
262 | } |
206 | } |
263 | #endif |
|
|
264 | return 0; /*Error */ |
207 | return 0; /*Error */ |
265 | } |
208 | } |
266 | if (stat == 0) |
209 | if (stat == 0) |
267 | return -1; |
210 | return -1; |
268 | sl->len += stat; |
211 | sl->len += stat; |
… | |
… | |
314 | |
257 | |
315 | end = ns->outputbuffer.start + ns->outputbuffer.len; |
258 | end = ns->outputbuffer.start + ns->outputbuffer.len; |
316 | /* The buffer is already in a wrapped state, so adjust end */ |
259 | /* The buffer is already in a wrapped state, so adjust end */ |
317 | if (end >= SOCKETBUFSIZE) |
260 | if (end >= SOCKETBUFSIZE) |
318 | end -= SOCKETBUFSIZE; |
261 | end -= SOCKETBUFSIZE; |
|
|
262 | |
319 | avail = SOCKETBUFSIZE - end; |
263 | avail = SOCKETBUFSIZE - end; |
320 | |
264 | |
321 | /* We can all fit it behind the current data without wrapping */ |
265 | /* We can all fit it behind the current data without wrapping */ |
322 | if (avail >= len) |
266 | if (avail >= len) |
323 | { |
|
|
324 | memcpy (ns->outputbuffer.data + end, buf, len); |
267 | memcpy (ns->outputbuffer.data + end, buf, len); |
325 | } |
|
|
326 | else |
268 | else |
327 | { |
269 | { |
328 | memcpy (ns->outputbuffer.data + end, buf, avail); |
270 | memcpy (ns->outputbuffer.data + end, buf, avail); |
329 | memcpy (ns->outputbuffer.data, buf + avail, len - avail); |
271 | memcpy (ns->outputbuffer.data, buf + avail, len - avail); |
330 | } |
272 | } |
|
|
273 | |
331 | ns->outputbuffer.len += len; |
274 | ns->outputbuffer.len += len; |
332 | #if 0 |
275 | #if 0 |
333 | LOG (llevDebug, "Added %d to output buffer, total length now %d, start=%d\n", len, ns->outputbuffer.len, ns->outputbuffer.start); |
276 | LOG (llevDebug, "Added %d to output buffer, total length now %d, start=%d\n", len, ns->outputbuffer.len, ns->outputbuffer.start); |
334 | #endif |
277 | #endif |
335 | } |
278 | } |
… | |
… | |
355 | { |
298 | { |
356 | max = SOCKETBUFSIZE - ns->outputbuffer.start; |
299 | max = SOCKETBUFSIZE - ns->outputbuffer.start; |
357 | if (ns->outputbuffer.len < max) |
300 | if (ns->outputbuffer.len < max) |
358 | max = ns->outputbuffer.len; |
301 | max = ns->outputbuffer.len; |
359 | |
302 | |
360 | #ifdef WIN32 /* ***win32 write_socket_buffer: change write() to send() */ |
|
|
361 | amt = send (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max, 0); |
|
|
362 | #else |
|
|
363 | do |
303 | do |
364 | { |
304 | { |
365 | amt = write (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max); |
305 | amt = write (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max); |
366 | } |
306 | } |
367 | while ((amt < 0) && (errno == EINTR)); |
307 | while ((amt < 0) && (errno == EINTR)); |
368 | #endif |
|
|
369 | |
308 | |
370 | if (amt < 0) |
309 | if (amt < 0) |
371 | { /* We got an error */ |
310 | { /* We got an error */ |
372 | |
311 | |
373 | #ifdef WIN32 /* ***win32 write_socket_buffer: change error handling */ |
|
|
374 | if (amt == -1 && WSAGetLastError () != WSAEWOULDBLOCK) |
|
|
375 | { |
|
|
376 | LOG (llevError, "New socket write failed (wsb) (%d).\n", WSAGetLastError ()); |
|
|
377 | #else |
|
|
378 | if (errno != EWOULDBLOCK) |
312 | if (errno != EWOULDBLOCK) |
379 | { |
313 | { |
380 | LOG (llevError, "New socket write failed (wsb) (%d: %s).\n", errno, strerror (errno)); |
314 | LOG (llevError, "New socket write failed (wsb) (%d: %s).\n", errno, strerror (errno)); |
381 | #endif |
|
|
382 | ns->status = Ns_Dead; |
315 | ns->status = Ns_Dead; |
383 | return; |
316 | return; |
384 | } |
317 | } |
385 | else |
318 | else |
386 | { /* EWOULDBLOCK */ |
319 | { /* EWOULDBLOCK */ |
… | |
… | |
430 | #endif |
363 | #endif |
431 | /* If we manage to write more than we wanted, take it as a bonus */ |
364 | /* If we manage to write more than we wanted, take it as a bonus */ |
432 | while (len > 0) |
365 | while (len > 0) |
433 | { |
366 | { |
434 | |
367 | |
435 | #ifdef WIN32 /* ***win32 Write_To_Socket: change write() to send() */ |
|
|
436 | amt = send (ns->fd, pos, len, 0); |
|
|
437 | #else |
|
|
438 | do |
368 | do |
439 | { |
369 | { |
440 | amt = write (ns->fd, pos, len); |
370 | amt = write (ns->fd, pos, len); |
441 | } |
371 | } |
442 | while ((amt < 0) && (errno == EINTR)); |
372 | while ((amt < 0) && (errno == EINTR)); |
443 | #endif |
|
|
444 | |
373 | |
445 | if (amt < 0) |
374 | if (amt < 0) |
446 | { /* We got an error */ |
375 | { /* We got an error */ |
447 | #ifdef WIN32 /* ***win32 Write_To_Socket: change error handling */ |
|
|
448 | if (amt == -1 && WSAGetLastError () != WSAEWOULDBLOCK) |
|
|
449 | { |
|
|
450 | LOG (llevError, "New socket write failed WTS (%d).\n", WSAGetLastError ()); |
|
|
451 | #else |
|
|
452 | if (errno != EWOULDBLOCK) |
376 | if (errno != EWOULDBLOCK) |
453 | { |
377 | { |
454 | LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ |
378 | LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ |
455 | errno, strerror (errno)); |
379 | errno, strerror (errno)); |
456 | #endif |
|
|
457 | ns->status = Ns_Dead; |
380 | ns->status = Ns_Dead; |
458 | return; |
381 | return; |
459 | } |
382 | } |
460 | else |
383 | else |
461 | { /* EWOULDBLOCK */ |
384 | { /* EWOULDBLOCK */ |
… | |
… | |
574 | cst_lst.obytes = 0; |
497 | cst_lst.obytes = 0; |
575 | cst_lst.max_conn = socket_info.nconns; |
498 | cst_lst.max_conn = socket_info.nconns; |
576 | cst_lst.time_start = now; |
499 | cst_lst.time_start = now; |
577 | } |
500 | } |
578 | #endif |
501 | #endif |
|
|
502 | |