1 | |
---|
2 | /* |
---|
3 | * sockGen.c -- Posix Socket support module for general posix use |
---|
4 | * |
---|
5 | * Copyright (c) GoAhead Software Inc., 1995-2000. All Rights Reserved. |
---|
6 | */ |
---|
7 | |
---|
8 | /******************************** Description *********************************/ |
---|
9 | |
---|
10 | /* |
---|
11 | * Posix Socket Module. This supports blocking and non-blocking buffered |
---|
12 | * socket I/O. |
---|
13 | */ |
---|
14 | |
---|
15 | #if (!WIN) || LITTLEFOOT || WEBS |
---|
16 | |
---|
17 | /********************************** Includes **********************************/ |
---|
18 | |
---|
19 | #include <string.h> |
---|
20 | #include <stdlib.h> |
---|
21 | |
---|
22 | #if UEMF |
---|
23 | #include "uemf.h" |
---|
24 | #else |
---|
25 | #include <socket.h> |
---|
26 | #include <types.h> |
---|
27 | #include <unistd.h> |
---|
28 | #include "emfInternal.h" |
---|
29 | #endif |
---|
30 | |
---|
31 | #if VXWORKS |
---|
32 | #include <hostLib.h> |
---|
33 | #endif |
---|
34 | |
---|
35 | #if __rtems__ |
---|
36 | #include <sys/select.h> |
---|
37 | #endif |
---|
38 | |
---|
39 | /************************************ Locals **********************************/ |
---|
40 | |
---|
41 | extern socket_t **socketList; /* List of open sockets */ |
---|
42 | extern int socketMax; /* Maximum size of socket */ |
---|
43 | extern int socketHighestFd; /* Highest socket fd opened */ |
---|
44 | static int socketOpenCount = 0; /* Number of task using sockets */ |
---|
45 | |
---|
46 | /***************************** Forward Declarations ***************************/ |
---|
47 | |
---|
48 | static void socketAccept(socket_t *sp); |
---|
49 | static int socketDoEvent(socket_t *sp); |
---|
50 | static int tryAlternateConnect(int sock, struct sockaddr *sockaddr); |
---|
51 | |
---|
52 | /*********************************** Code *************************************/ |
---|
53 | /* |
---|
54 | * Open socket module |
---|
55 | */ |
---|
56 | |
---|
57 | int socketOpen() |
---|
58 | { |
---|
59 | #if CE || WIN |
---|
60 | WSADATA wsaData; |
---|
61 | #endif |
---|
62 | |
---|
63 | if (++socketOpenCount > 1) { |
---|
64 | return 0; |
---|
65 | } |
---|
66 | |
---|
67 | #if CE || WIN |
---|
68 | if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0) { |
---|
69 | return -1; |
---|
70 | } |
---|
71 | if (wsaData.wVersion != MAKEWORD(1,1)) { |
---|
72 | WSACleanup(); |
---|
73 | return -1; |
---|
74 | } |
---|
75 | #endif |
---|
76 | socketList = NULL; |
---|
77 | socketMax = 0; |
---|
78 | socketHighestFd = -1; |
---|
79 | |
---|
80 | return 0; |
---|
81 | } |
---|
82 | |
---|
83 | /******************************************************************************/ |
---|
84 | /* |
---|
85 | * Close the socket module, by closing all open connections |
---|
86 | */ |
---|
87 | |
---|
88 | void socketClose() |
---|
89 | { |
---|
90 | int i; |
---|
91 | |
---|
92 | if (--socketOpenCount <= 0) { |
---|
93 | for (i = socketMax; i >= 0; i--) { |
---|
94 | if (socketList && socketList[i]) { |
---|
95 | socketCloseConnection(i); |
---|
96 | } |
---|
97 | } |
---|
98 | socketOpenCount = 0; |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | /******************************************************************************/ |
---|
103 | /* |
---|
104 | * Open a client or server socket. Host is NULL if we want server capability. |
---|
105 | */ |
---|
106 | |
---|
107 | int socketOpenConnection(char *host, int port, socketAccept_t accept, int flags) |
---|
108 | { |
---|
109 | #if ! (NO_GETHOSTBYNAME || VXWORKS) |
---|
110 | struct hostent *hostent; /* Host database entry */ |
---|
111 | #endif /* ! (NO_GETHOSTBYNAME || VXWORKS) */ |
---|
112 | socket_t *sp; |
---|
113 | struct sockaddr_in sockaddr; |
---|
114 | int sid, bcast, dgram, rc; |
---|
115 | |
---|
116 | if (port > SOCKET_PORT_MAX) { |
---|
117 | return -1; |
---|
118 | } |
---|
119 | /* |
---|
120 | * Allocate a socket structure |
---|
121 | */ |
---|
122 | if ((sid = socketAlloc(host, port, accept, flags)) < 0) { |
---|
123 | return -1; |
---|
124 | } |
---|
125 | sp = socketList[sid]; |
---|
126 | a_assert(sp); |
---|
127 | |
---|
128 | /* |
---|
129 | * Create the socket address structure |
---|
130 | */ |
---|
131 | memset((char *) &sockaddr, '\0', sizeof(struct sockaddr_in)); |
---|
132 | sockaddr.sin_family = AF_INET; |
---|
133 | sockaddr.sin_port = htons((short) (port & 0xFFFF)); |
---|
134 | |
---|
135 | if (host == NULL) { |
---|
136 | sockaddr.sin_addr.s_addr = INADDR_ANY; |
---|
137 | } else { |
---|
138 | sockaddr.sin_addr.s_addr = inet_addr(host); |
---|
139 | if (sockaddr.sin_addr.s_addr == INADDR_NONE) { |
---|
140 | /* |
---|
141 | * If the OS does not support gethostbyname functionality, the macro: |
---|
142 | * NO_GETHOSTBYNAME should be defined to skip the use of gethostbyname. |
---|
143 | * Unfortunatly there is no easy way to recover, the following code |
---|
144 | * simply uses the basicGetHost IP for the sockaddr. |
---|
145 | */ |
---|
146 | |
---|
147 | #if NO_GETHOSTBYNAME |
---|
148 | if (strcmp(host, basicGetHost()) == 0) { |
---|
149 | sockaddr.sin_addr.s_addr = inet_addr(basicGetAddress()); |
---|
150 | } |
---|
151 | if (sockaddr.sin_addr.s_addr == INADDR_NONE) { |
---|
152 | socketFree(sid); |
---|
153 | return -1; |
---|
154 | } |
---|
155 | #elif VXWORKS |
---|
156 | sockaddr.sin_addr.s_addr = (unsigned long) hostGetByName(host); |
---|
157 | if (sockaddr.sin_addr.s_addr == NULL) { |
---|
158 | errno = ENXIO; |
---|
159 | socketFree(sid); |
---|
160 | return -1; |
---|
161 | } |
---|
162 | #else |
---|
163 | hostent = gethostbyname(host); |
---|
164 | if (hostent != NULL) { |
---|
165 | memcpy((char *) &sockaddr.sin_addr, |
---|
166 | (char *) hostent->h_addr_list[0], |
---|
167 | (size_t) hostent->h_length); |
---|
168 | } else { |
---|
169 | char *asciiAddress; |
---|
170 | char_t *address; |
---|
171 | |
---|
172 | address = basicGetAddress(); |
---|
173 | asciiAddress = ballocUniToAsc(address, gstrlen(address)); |
---|
174 | sockaddr.sin_addr.s_addr = inet_addr(asciiAddress); |
---|
175 | bfree(B_L, asciiAddress); |
---|
176 | if (sockaddr.sin_addr.s_addr == INADDR_NONE) { |
---|
177 | errno = ENXIO; |
---|
178 | socketFree(sid); |
---|
179 | return -1; |
---|
180 | } |
---|
181 | } |
---|
182 | #endif /* (NO_GETHOSTBYNAME || VXWORKS) */ |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | bcast = sp->flags & SOCKET_BROADCAST; |
---|
187 | if (bcast) { |
---|
188 | sp->flags |= SOCKET_DATAGRAM; |
---|
189 | } |
---|
190 | dgram = sp->flags & SOCKET_DATAGRAM; |
---|
191 | |
---|
192 | /* |
---|
193 | * Create the socket. Support for datagram sockets. Set the close on |
---|
194 | * exec flag so children don't inherit the socket. |
---|
195 | */ |
---|
196 | sp->sock = socket(AF_INET, dgram ? SOCK_DGRAM: SOCK_STREAM, 0); |
---|
197 | if (sp->sock < 0) { |
---|
198 | socketFree(sid); |
---|
199 | return -1; |
---|
200 | } |
---|
201 | #ifndef __NO_FCNTL |
---|
202 | fcntl(sp->sock, F_SETFD, FD_CLOEXEC); |
---|
203 | #endif |
---|
204 | socketHighestFd = max(socketHighestFd, sp->sock); |
---|
205 | |
---|
206 | /* |
---|
207 | * If broadcast, we need to turn on broadcast capability. |
---|
208 | */ |
---|
209 | if (bcast) { |
---|
210 | int broadcastFlag = 1; |
---|
211 | if (setsockopt(sp->sock, SOL_SOCKET, SO_BROADCAST, |
---|
212 | (char *) &broadcastFlag, sizeof(broadcastFlag)) < 0) { |
---|
213 | socketFree(sid); |
---|
214 | return -1; |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | /* |
---|
219 | * Host is set if we are the client |
---|
220 | */ |
---|
221 | if (host) { |
---|
222 | /* |
---|
223 | * Connect to the remote server in blocking mode, then go into |
---|
224 | * non-blocking mode if desired. |
---|
225 | */ |
---|
226 | if (!dgram) { |
---|
227 | if (! (sp->flags & SOCKET_BLOCK)) { |
---|
228 | /* |
---|
229 | * sockGen.c is only used for Windows products when blocking |
---|
230 | * connects are expected. This applies to FieldUpgrader |
---|
231 | * agents and open source webserver connectws. Therefore the |
---|
232 | * asynchronous connect code here is not compiled. |
---|
233 | */ |
---|
234 | #if (WIN || CE) && !(LITTLEFOOT || WEBS) |
---|
235 | int flag; |
---|
236 | |
---|
237 | sp->flags |= SOCKET_ASYNC; |
---|
238 | /* |
---|
239 | * Set to non-blocking for an async connect |
---|
240 | */ |
---|
241 | flag = 1; |
---|
242 | if (ioctlsocket(sp->sock, FIONBIO, &flag) == SOCKET_ERROR) { |
---|
243 | socketFree(sid); |
---|
244 | return -1; |
---|
245 | } |
---|
246 | #else |
---|
247 | socketSetBlock(sid, 1); |
---|
248 | #endif /* #if (WIN || CE) && !(LITTLEFOOT || WEBS) */ |
---|
249 | |
---|
250 | } |
---|
251 | if ((rc = connect(sp->sock, (struct sockaddr *) &sockaddr, |
---|
252 | sizeof(sockaddr))) < 0 && |
---|
253 | (rc = tryAlternateConnect(sp->sock, |
---|
254 | (struct sockaddr *) &sockaddr)) < 0) { |
---|
255 | #if WIN || CE |
---|
256 | if (socketGetError() != EWOULDBLOCK) { |
---|
257 | socketFree(sid); |
---|
258 | return -1; |
---|
259 | } |
---|
260 | #else |
---|
261 | socketFree(sid); |
---|
262 | return -1; |
---|
263 | |
---|
264 | #endif /* WIN || CE */ |
---|
265 | |
---|
266 | } |
---|
267 | } |
---|
268 | } else { |
---|
269 | /* |
---|
270 | * Bind to the socket endpoint and the call listen() to start listening |
---|
271 | */ |
---|
272 | rc = 1; |
---|
273 | setsockopt(sp->sock, SOL_SOCKET, SO_REUSEADDR, (char *)&rc, sizeof(rc)); |
---|
274 | if (bind(sp->sock, (struct sockaddr *) &sockaddr, |
---|
275 | sizeof(sockaddr)) < 0) { |
---|
276 | socketFree(sid); |
---|
277 | return -1; |
---|
278 | } |
---|
279 | |
---|
280 | if (! dgram) { |
---|
281 | if (listen(sp->sock, SOMAXCONN) < 0) { |
---|
282 | socketFree(sid); |
---|
283 | return -1; |
---|
284 | } |
---|
285 | #if !UEMF |
---|
286 | sp->fileHandle = emfCreateFileHandler(sp->sock, SOCKET_READABLE, |
---|
287 | (emfFileProc *) socketAccept, (void *) sp); |
---|
288 | #else |
---|
289 | sp->flags |= SOCKET_LISTENING; |
---|
290 | #endif |
---|
291 | } |
---|
292 | sp->handlerMask |= SOCKET_READABLE; |
---|
293 | } |
---|
294 | |
---|
295 | /* |
---|
296 | * Set the blocking mode |
---|
297 | */ |
---|
298 | |
---|
299 | if (flags & SOCKET_BLOCK) { |
---|
300 | socketSetBlock(sid, 1); |
---|
301 | } else { |
---|
302 | socketSetBlock(sid, 0); |
---|
303 | } |
---|
304 | return sid; |
---|
305 | } |
---|
306 | |
---|
307 | /******************************************************************************/ |
---|
308 | /* |
---|
309 | * If the connection failed, swap the first two bytes in the |
---|
310 | * sockaddr structure. This is a kludge due to a change in |
---|
311 | * VxWorks between versions 5.3 and 5.4, but we want the |
---|
312 | * product to run on either. |
---|
313 | */ |
---|
314 | |
---|
315 | static int tryAlternateConnect(int sock, struct sockaddr *sockaddr) |
---|
316 | { |
---|
317 | #if VXWORKS |
---|
318 | char *ptr; |
---|
319 | |
---|
320 | ptr = (char *)sockaddr; |
---|
321 | *ptr = *(ptr+1); |
---|
322 | *(ptr+1) = 0; |
---|
323 | return connect(sock, sockaddr, sizeof(struct sockaddr)); |
---|
324 | #else |
---|
325 | return -1; |
---|
326 | #endif /* VXWORKS */ |
---|
327 | } |
---|
328 | |
---|
329 | /******************************************************************************/ |
---|
330 | /* |
---|
331 | * Close a socket |
---|
332 | */ |
---|
333 | |
---|
334 | void socketCloseConnection(int sid) |
---|
335 | { |
---|
336 | socket_t *sp; |
---|
337 | |
---|
338 | if ((sp = socketPtr(sid)) == NULL) { |
---|
339 | return; |
---|
340 | } |
---|
341 | socketFree(sid); |
---|
342 | } |
---|
343 | |
---|
344 | /******************************************************************************/ |
---|
345 | /* |
---|
346 | * Accept a connection. Called as a callback on incoming connection. |
---|
347 | */ |
---|
348 | |
---|
349 | static void socketAccept(socket_t *sp) |
---|
350 | { |
---|
351 | union { |
---|
352 | struct sockaddr addr; |
---|
353 | struct sockaddr_in addr_in; |
---|
354 | } overlay; |
---|
355 | struct sockaddr_in *addr; |
---|
356 | socket_t *nsp; |
---|
357 | socklen_t len; |
---|
358 | char *pString; |
---|
359 | int newSock, nid; |
---|
360 | |
---|
361 | a_assert(sp); |
---|
362 | addr = &overlay.addr_in; |
---|
363 | |
---|
364 | /* |
---|
365 | * Accept the connection and prevent inheriting by children (F_SETFD) |
---|
366 | */ |
---|
367 | len = sizeof(struct sockaddr_in); |
---|
368 | if ((newSock = accept(sp->sock, &overlay.addr, &len)) < 0) { |
---|
369 | return; |
---|
370 | } |
---|
371 | #ifndef __NO_FCNTL |
---|
372 | fcntl(newSock, F_SETFD, FD_CLOEXEC); |
---|
373 | #endif |
---|
374 | socketHighestFd = max(socketHighestFd, newSock); |
---|
375 | |
---|
376 | /* |
---|
377 | * Create a socket structure and insert into the socket list |
---|
378 | */ |
---|
379 | nid = socketAlloc(sp->host, sp->port, sp->accept, sp->flags); |
---|
380 | nsp = socketList[nid]; |
---|
381 | a_assert(nsp); |
---|
382 | nsp->sock = newSock; |
---|
383 | nsp->flags &= ~SOCKET_LISTENING; |
---|
384 | |
---|
385 | if (nsp == NULL) { |
---|
386 | return; |
---|
387 | } |
---|
388 | /* |
---|
389 | * Set the blocking mode before calling the accept callback. |
---|
390 | */ |
---|
391 | |
---|
392 | socketSetBlock(nid, (nsp->flags & SOCKET_BLOCK) ? 1: 0); |
---|
393 | /* |
---|
394 | * Call the user accept callback. The user must call socketCreateHandler |
---|
395 | * to register for further events of interest. |
---|
396 | */ |
---|
397 | if (sp->accept != NULL) { |
---|
398 | pString = inet_ntoa(addr->sin_addr); |
---|
399 | if ((sp->accept)(nid, pString, ntohs(addr->sin_port), sp->sid) < 0) { |
---|
400 | socketFree(nid); |
---|
401 | } |
---|
402 | #if VXWORKS |
---|
403 | free(pString); |
---|
404 | #endif |
---|
405 | } |
---|
406 | } |
---|
407 | |
---|
408 | /******************************************************************************/ |
---|
409 | /* |
---|
410 | * Get more input from the socket and return in buf. |
---|
411 | * Returns 0 for EOF, -1 for errors and otherwise the number of bytes read. |
---|
412 | */ |
---|
413 | |
---|
414 | int socketGetInput(int sid, char *buf, int toRead, int *errCode) |
---|
415 | { |
---|
416 | struct sockaddr_in server; |
---|
417 | socket_t *sp; |
---|
418 | socklen_t len; |
---|
419 | int bytesRead; |
---|
420 | |
---|
421 | a_assert(buf); |
---|
422 | a_assert(errCode); |
---|
423 | |
---|
424 | *errCode = 0; |
---|
425 | |
---|
426 | if ((sp = socketPtr(sid)) == NULL) { |
---|
427 | return -1; |
---|
428 | } |
---|
429 | |
---|
430 | /* |
---|
431 | * If we have previously seen an EOF condition, then just return |
---|
432 | */ |
---|
433 | if (sp->flags & SOCKET_EOF) { |
---|
434 | return 0; |
---|
435 | } |
---|
436 | #if (WIN || CE) && !(LITTLEFOOT || WEBS) |
---|
437 | if ( !(sp->flags & SOCKET_BLOCK) |
---|
438 | && ! socketWaitForEvent(sp, FD_CONNECT, errCode)) { |
---|
439 | return -1; |
---|
440 | } |
---|
441 | #endif |
---|
442 | |
---|
443 | /* |
---|
444 | * Read the data |
---|
445 | */ |
---|
446 | if (sp->flags & SOCKET_DATAGRAM) { |
---|
447 | len = sizeof(server); |
---|
448 | bytesRead = recvfrom(sp->sock, buf, toRead, 0, |
---|
449 | (struct sockaddr *) &server, &len); |
---|
450 | } else { |
---|
451 | bytesRead = recv(sp->sock, buf, toRead, 0); |
---|
452 | } |
---|
453 | if (bytesRead < 0) { |
---|
454 | if (errno == ECONNRESET) { |
---|
455 | return 0; |
---|
456 | } |
---|
457 | *errCode = socketGetError(); |
---|
458 | return -1; |
---|
459 | } |
---|
460 | return bytesRead; |
---|
461 | } |
---|
462 | |
---|
463 | /******************************************************************************/ |
---|
464 | /* |
---|
465 | * Process an event on the event queue |
---|
466 | */ |
---|
467 | |
---|
468 | #ifndef UEMF |
---|
469 | |
---|
470 | static int socketEventProc(void *data, int mask) |
---|
471 | { |
---|
472 | socket_t *sp; |
---|
473 | ringq_t *rq; |
---|
474 | int sid; |
---|
475 | |
---|
476 | sid = (int) data; |
---|
477 | |
---|
478 | a_assert(sid >= 0 && sid < socketMax); |
---|
479 | a_assert(socketList[sid]); |
---|
480 | |
---|
481 | if ((sp = socketPtr(sid)) == NULL) { |
---|
482 | return 1; |
---|
483 | } |
---|
484 | |
---|
485 | /* |
---|
486 | * If now writable and flushing in the background, continue flushing |
---|
487 | */ |
---|
488 | if (mask & SOCKET_WRITABLE) { |
---|
489 | if (sp->flags & SOCKET_FLUSHING) { |
---|
490 | rq = &sp->outBuf; |
---|
491 | if (ringqLen(rq) > 0) { |
---|
492 | socketFlush(sp->sid); |
---|
493 | } else { |
---|
494 | sp->flags &= ~SOCKET_FLUSHING; |
---|
495 | } |
---|
496 | } |
---|
497 | } |
---|
498 | |
---|
499 | /* |
---|
500 | * Now invoke the users socket handler. NOTE: the handler may delete the |
---|
501 | * socket, so we must be very careful after calling the handler. |
---|
502 | */ |
---|
503 | if (sp->handler && (sp->handlerMask & mask)) { |
---|
504 | (sp->handler)(sid, mask & sp->handlerMask, sp->handler_data); |
---|
505 | } |
---|
506 | if (socketList && sid < socketMax && socketList[sid] == sp) { |
---|
507 | socketRegisterInterest(sp, sp->handlerMask); |
---|
508 | } |
---|
509 | return 1; |
---|
510 | } |
---|
511 | #endif /* ! UEMF */ |
---|
512 | |
---|
513 | /******************************************************************************/ |
---|
514 | /* |
---|
515 | * Define the events of interest |
---|
516 | */ |
---|
517 | |
---|
518 | void socketRegisterInterest(socket_t *sp, int handlerMask) |
---|
519 | { |
---|
520 | a_assert(sp); |
---|
521 | |
---|
522 | sp->handlerMask = handlerMask; |
---|
523 | #if !UEMF |
---|
524 | if (handlerMask) { |
---|
525 | sp->fileHandle = emfCreateFileHandler(sp->sock, handlerMask, |
---|
526 | (emfFileProc *) socketEventProc, (void *) sp->sid); |
---|
527 | } else { |
---|
528 | emfDeleteFileHandler(sp->fileHandle); |
---|
529 | } |
---|
530 | #endif /* ! UEMF */ |
---|
531 | } |
---|
532 | |
---|
533 | /******************************************************************************/ |
---|
534 | /* |
---|
535 | * Wait until an event occurs on a socket. Return 1 on success, 0 on failure. |
---|
536 | * or -1 on exception (UEMF only) |
---|
537 | */ |
---|
538 | |
---|
539 | int socketWaitForEvent(socket_t *sp, int handlerMask, int *errCode) |
---|
540 | { |
---|
541 | int mask; |
---|
542 | |
---|
543 | a_assert(sp); |
---|
544 | |
---|
545 | mask = sp->handlerMask; |
---|
546 | sp->handlerMask |= handlerMask; |
---|
547 | while (socketSelect(sp->sid, 1000)) { |
---|
548 | if (sp->currentEvents & (handlerMask | SOCKET_EXCEPTION)) { |
---|
549 | break; |
---|
550 | } |
---|
551 | } |
---|
552 | sp->handlerMask = mask; |
---|
553 | if (sp->currentEvents & SOCKET_EXCEPTION) { |
---|
554 | return -1; |
---|
555 | } else if (sp->currentEvents & handlerMask) { |
---|
556 | return 1; |
---|
557 | } |
---|
558 | if (errCode) { |
---|
559 | *errCode = errno = EWOULDBLOCK; |
---|
560 | } |
---|
561 | return 0; |
---|
562 | } |
---|
563 | |
---|
564 | /******************************************************************************/ |
---|
565 | /* |
---|
566 | * Return TRUE if there is a socket with an event ready to process, |
---|
567 | */ |
---|
568 | |
---|
569 | int socketReady(int sid) |
---|
570 | { |
---|
571 | socket_t *sp; |
---|
572 | int all; |
---|
573 | |
---|
574 | all = 0; |
---|
575 | if (sid < 0) { |
---|
576 | sid = 0; |
---|
577 | all = 1; |
---|
578 | } |
---|
579 | |
---|
580 | for (; sid < socketMax; sid++) { |
---|
581 | if ((sp = socketList[sid]) == NULL) { |
---|
582 | if (! all) { |
---|
583 | break; |
---|
584 | } else { |
---|
585 | continue; |
---|
586 | } |
---|
587 | } |
---|
588 | if (sp->currentEvents & sp->handlerMask) { |
---|
589 | return 1; |
---|
590 | } |
---|
591 | /* |
---|
592 | * If there is input data, also call select to test for new events |
---|
593 | */ |
---|
594 | if (sp->handlerMask & SOCKET_READABLE && socketInputBuffered(sid)) { |
---|
595 | socketSelect(sid, 0); |
---|
596 | return 1; |
---|
597 | } |
---|
598 | if (! all) { |
---|
599 | break; |
---|
600 | } |
---|
601 | } |
---|
602 | return 0; |
---|
603 | } |
---|
604 | |
---|
605 | /******************************************************************************/ |
---|
606 | /* |
---|
607 | * Wait for a handle to become readable or writable and return a number of |
---|
608 | * noticed events. Timeout is in milliseconds. |
---|
609 | */ |
---|
610 | |
---|
611 | #if WIN || CE |
---|
612 | |
---|
613 | int socketSelect(int sid, int timeout) |
---|
614 | { |
---|
615 | struct timeval tv; |
---|
616 | socket_t *sp; |
---|
617 | fd_set readFds, writeFds, exceptFds; |
---|
618 | int nEvents; |
---|
619 | int all, socketHighestFd; /* Highest socket fd opened */ |
---|
620 | |
---|
621 | FD_ZERO(&readFds); |
---|
622 | FD_ZERO(&writeFds); |
---|
623 | FD_ZERO(&exceptFds); |
---|
624 | socketHighestFd = -1; |
---|
625 | |
---|
626 | tv.tv_sec = timeout / 1000; |
---|
627 | tv.tv_usec = (timeout % 1000) * 1000; |
---|
628 | |
---|
629 | /* |
---|
630 | * Set the select event masks for events to watch |
---|
631 | */ |
---|
632 | all = nEvents = 0; |
---|
633 | |
---|
634 | if (sid < 0) { |
---|
635 | all++; |
---|
636 | sid = 0; |
---|
637 | } |
---|
638 | |
---|
639 | for (; sid < socketMax; sid++) { |
---|
640 | if ((sp = socketList[sid]) == NULL) { |
---|
641 | continue; |
---|
642 | } |
---|
643 | a_assert(sp); |
---|
644 | /* |
---|
645 | * Set the appropriate bit in the ready masks for the sp->sock. |
---|
646 | */ |
---|
647 | if (sp->handlerMask & SOCKET_READABLE) { |
---|
648 | FD_SET(sp->sock, &readFds); |
---|
649 | nEvents++; |
---|
650 | if (socketInputBuffered(sid) > 0) { |
---|
651 | tv.tv_sec = 0; |
---|
652 | tv.tv_usec = 0; |
---|
653 | } |
---|
654 | } |
---|
655 | if (sp->handlerMask & SOCKET_WRITABLE) { |
---|
656 | FD_SET(sp->sock, &writeFds); |
---|
657 | nEvents++; |
---|
658 | } |
---|
659 | if (sp->handlerMask & SOCKET_EXCEPTION) { |
---|
660 | FD_SET(sp->sock, &exceptFds); |
---|
661 | nEvents++; |
---|
662 | } |
---|
663 | if (! all) { |
---|
664 | break; |
---|
665 | } |
---|
666 | } |
---|
667 | |
---|
668 | /* |
---|
669 | * Windows select() fails if no descriptors are set, instead of just sleeping |
---|
670 | * like other, nice select() calls. So, if WIN, sleep. |
---|
671 | */ |
---|
672 | if (nEvents == 0) { |
---|
673 | Sleep(timeout); |
---|
674 | return 0; |
---|
675 | } |
---|
676 | |
---|
677 | /* |
---|
678 | * Wait for the event or a timeout. |
---|
679 | */ |
---|
680 | nEvents = select(socketHighestFd+1, &readFds, &writeFds, &exceptFds, &tv); |
---|
681 | |
---|
682 | if (all) { |
---|
683 | sid = 0; |
---|
684 | } |
---|
685 | for (; sid < socketMax; sid++) { |
---|
686 | if ((sp = socketList[sid]) == NULL) { |
---|
687 | continue; |
---|
688 | } |
---|
689 | |
---|
690 | if (FD_ISSET(sp->sock, &readFds) || socketInputBuffered(sid) > 0) { |
---|
691 | sp->currentEvents |= SOCKET_READABLE; |
---|
692 | } |
---|
693 | if (FD_ISSET(sp->sock, &writeFds)) { |
---|
694 | sp->currentEvents |= SOCKET_WRITABLE; |
---|
695 | } |
---|
696 | if (FD_ISSET(sp->sock, &exceptFds)) { |
---|
697 | sp->currentEvents |= SOCKET_EXCEPTION; |
---|
698 | } |
---|
699 | if (! all) { |
---|
700 | break; |
---|
701 | } |
---|
702 | } |
---|
703 | |
---|
704 | return nEvents; |
---|
705 | } |
---|
706 | |
---|
707 | #else /* not WIN || CE */ |
---|
708 | |
---|
709 | int socketSelect(int sid, int timeout) |
---|
710 | { |
---|
711 | socket_t *sp; |
---|
712 | struct timeval tv; |
---|
713 | fd_mask *readFds, *writeFds, *exceptFds; |
---|
714 | int all, len, nwords, index, bit, nEvents; |
---|
715 | |
---|
716 | /* |
---|
717 | * Allocate and zero the select masks |
---|
718 | */ |
---|
719 | nwords = (socketHighestFd + NFDBITS) / NFDBITS; |
---|
720 | len = nwords * sizeof(int); |
---|
721 | |
---|
722 | readFds = balloc(B_L, len); |
---|
723 | memset(readFds, 0, len); |
---|
724 | writeFds = balloc(B_L, len); |
---|
725 | memset(writeFds, 0, len); |
---|
726 | exceptFds = balloc(B_L, len); |
---|
727 | memset(exceptFds, 0, len); |
---|
728 | |
---|
729 | tv.tv_sec = timeout / 1000; |
---|
730 | tv.tv_usec = (timeout % 1000) * 1000; |
---|
731 | |
---|
732 | /* |
---|
733 | * Set the select event masks for events to watch |
---|
734 | */ |
---|
735 | all = nEvents = 0; |
---|
736 | |
---|
737 | if (sid < 0) { |
---|
738 | all++; |
---|
739 | sid = 0; |
---|
740 | } |
---|
741 | |
---|
742 | for (; sid < socketMax; sid++) { |
---|
743 | if ((sp = socketList[sid]) == NULL) { |
---|
744 | if (all == 0) { |
---|
745 | break; |
---|
746 | } else { |
---|
747 | continue; |
---|
748 | } |
---|
749 | } |
---|
750 | a_assert(sp); |
---|
751 | |
---|
752 | /* |
---|
753 | * Initialize the ready masks and compute the mask offsets. |
---|
754 | */ |
---|
755 | index = sp->sock / (NBBY * sizeof(fd_mask)); |
---|
756 | bit = 1 << (sp->sock % (NBBY * sizeof(fd_mask))); |
---|
757 | |
---|
758 | /* |
---|
759 | * Set the appropriate bit in the ready masks for the sp->sock. |
---|
760 | */ |
---|
761 | if (sp->handlerMask & SOCKET_READABLE) { |
---|
762 | readFds[index] |= bit; |
---|
763 | nEvents++; |
---|
764 | if (socketInputBuffered(sid) > 0) { |
---|
765 | tv.tv_sec = 0; |
---|
766 | tv.tv_usec = 0; |
---|
767 | } |
---|
768 | } |
---|
769 | if (sp->handlerMask & SOCKET_WRITABLE) { |
---|
770 | writeFds[index] |= bit; |
---|
771 | nEvents++; |
---|
772 | } |
---|
773 | if (sp->handlerMask & SOCKET_EXCEPTION) { |
---|
774 | exceptFds[index] |= bit; |
---|
775 | nEvents++; |
---|
776 | } |
---|
777 | if (! all) { |
---|
778 | break; |
---|
779 | } |
---|
780 | } |
---|
781 | |
---|
782 | /* |
---|
783 | * Wait for the event or a timeout. Reset nEvents to be the number of actual |
---|
784 | * events now. |
---|
785 | */ |
---|
786 | nEvents = select(socketHighestFd + 1, (fd_set *) readFds, |
---|
787 | (fd_set *) writeFds, (fd_set *) exceptFds, &tv); |
---|
788 | |
---|
789 | if (nEvents > 0) { |
---|
790 | if (all) { |
---|
791 | sid = 0; |
---|
792 | } |
---|
793 | for (; sid < socketMax; sid++) { |
---|
794 | if ((sp = socketList[sid]) == NULL) { |
---|
795 | if (all == 0) { |
---|
796 | break; |
---|
797 | } else { |
---|
798 | continue; |
---|
799 | } |
---|
800 | } |
---|
801 | |
---|
802 | index = sp->sock / (NBBY * sizeof(fd_mask)); |
---|
803 | bit = 1 << (sp->sock % (NBBY * sizeof(fd_mask))); |
---|
804 | |
---|
805 | if (readFds[index] & bit || socketInputBuffered(sid) > 0) { |
---|
806 | sp->currentEvents |= SOCKET_READABLE; |
---|
807 | } |
---|
808 | if (writeFds[index] & bit) { |
---|
809 | sp->currentEvents |= SOCKET_WRITABLE; |
---|
810 | } |
---|
811 | if (exceptFds[index] & bit) { |
---|
812 | sp->currentEvents |= SOCKET_EXCEPTION; |
---|
813 | } |
---|
814 | if (! all) { |
---|
815 | break; |
---|
816 | } |
---|
817 | } |
---|
818 | } |
---|
819 | |
---|
820 | bfree(B_L, readFds); |
---|
821 | bfree(B_L, writeFds); |
---|
822 | bfree(B_L, exceptFds); |
---|
823 | |
---|
824 | return nEvents; |
---|
825 | } |
---|
826 | #endif /* WIN || CE */ |
---|
827 | |
---|
828 | /******************************************************************************/ |
---|
829 | /* |
---|
830 | * Process socket events |
---|
831 | */ |
---|
832 | |
---|
833 | void socketProcess(int sid) |
---|
834 | { |
---|
835 | socket_t *sp; |
---|
836 | int all; |
---|
837 | |
---|
838 | all = 0; |
---|
839 | if (sid < 0) { |
---|
840 | all = 1; |
---|
841 | sid = 0; |
---|
842 | } |
---|
843 | /* |
---|
844 | * Process each socket |
---|
845 | */ |
---|
846 | for (; sid < socketMax; sid++) { |
---|
847 | if ((sp = socketList[sid]) == NULL) { |
---|
848 | if (! all) { |
---|
849 | break; |
---|
850 | } else { |
---|
851 | continue; |
---|
852 | } |
---|
853 | } |
---|
854 | if (socketReady(sid)) { |
---|
855 | socketDoEvent(sp); |
---|
856 | } |
---|
857 | if (! all) { |
---|
858 | break; |
---|
859 | } |
---|
860 | } |
---|
861 | } |
---|
862 | |
---|
863 | /******************************************************************************/ |
---|
864 | /* |
---|
865 | * Process an event on the event queue |
---|
866 | */ |
---|
867 | |
---|
868 | static int socketDoEvent(socket_t *sp) |
---|
869 | { |
---|
870 | ringq_t *rq; |
---|
871 | int sid; |
---|
872 | |
---|
873 | a_assert(sp); |
---|
874 | |
---|
875 | sid = sp->sid; |
---|
876 | if (sp->currentEvents & SOCKET_READABLE) { |
---|
877 | if (sp->flags & SOCKET_LISTENING) { |
---|
878 | socketAccept(sp); |
---|
879 | sp->currentEvents = 0; |
---|
880 | return 1; |
---|
881 | } |
---|
882 | |
---|
883 | } else { |
---|
884 | /* |
---|
885 | * If there is still read data in the buffers, trigger the read handler |
---|
886 | * NOTE: this may busy spin if the read handler doesn't read the data |
---|
887 | */ |
---|
888 | if (sp->handlerMask & SOCKET_READABLE && socketInputBuffered(sid)) { |
---|
889 | sp->currentEvents |= SOCKET_READABLE; |
---|
890 | } |
---|
891 | } |
---|
892 | |
---|
893 | |
---|
894 | /* |
---|
895 | * If now writable and flushing in the background, continue flushing |
---|
896 | */ |
---|
897 | if (sp->currentEvents & SOCKET_WRITABLE) { |
---|
898 | if (sp->flags & SOCKET_FLUSHING) { |
---|
899 | rq = &sp->outBuf; |
---|
900 | if (ringqLen(rq) > 0) { |
---|
901 | socketFlush(sp->sid); |
---|
902 | } else { |
---|
903 | sp->flags &= ~SOCKET_FLUSHING; |
---|
904 | } |
---|
905 | } |
---|
906 | } |
---|
907 | |
---|
908 | /* |
---|
909 | * Now invoke the users socket handler. NOTE: the handler may delete the |
---|
910 | * socket, so we must be very careful after calling the handler. |
---|
911 | */ |
---|
912 | if (sp->handler && (sp->handlerMask & sp->currentEvents)) { |
---|
913 | (sp->handler)(sid, sp->handlerMask & sp->currentEvents, |
---|
914 | sp->handler_data); |
---|
915 | /* |
---|
916 | * Make sure socket pointer is still valid, then reset the currentEvents. |
---|
917 | */ |
---|
918 | if (socketList && sid < socketMax && socketList[sid] == sp) { |
---|
919 | sp->currentEvents = 0; |
---|
920 | } |
---|
921 | } |
---|
922 | return 1; |
---|
923 | } |
---|
924 | |
---|
925 | /******************************************************************************/ |
---|
926 | /* |
---|
927 | * Set the socket blocking mode |
---|
928 | */ |
---|
929 | |
---|
930 | int socketSetBlock(int sid, int on) |
---|
931 | { |
---|
932 | socket_t *sp; |
---|
933 | unsigned long flag; |
---|
934 | int iflag; |
---|
935 | int oldBlock; |
---|
936 | |
---|
937 | flag = iflag = !on; |
---|
938 | |
---|
939 | if ((sp = socketPtr(sid)) == NULL) { |
---|
940 | a_assert(0); |
---|
941 | return 0; |
---|
942 | } |
---|
943 | oldBlock = (sp->flags & SOCKET_BLOCK); |
---|
944 | sp->flags &= ~(SOCKET_BLOCK); |
---|
945 | if (on) { |
---|
946 | sp->flags |= SOCKET_BLOCK; |
---|
947 | } |
---|
948 | |
---|
949 | /* |
---|
950 | * Put the socket into block / non-blocking mode |
---|
951 | */ |
---|
952 | if (sp->flags & SOCKET_BLOCK) { |
---|
953 | #if CE || WIN |
---|
954 | ioctlsocket(sp->sock, FIONBIO, &flag); |
---|
955 | #elif ECOS |
---|
956 | int off; |
---|
957 | off = 0; |
---|
958 | ioctl(sp->sock, FIONBIO, &off); |
---|
959 | #elif VXWORKS |
---|
960 | ioctl(sp->sock, FIONBIO, (int)&iflag); |
---|
961 | #else |
---|
962 | fcntl(sp->sock, F_SETFL, fcntl(sp->sock, F_GETFL) & ~O_NONBLOCK); |
---|
963 | #endif |
---|
964 | |
---|
965 | } else { |
---|
966 | #if CE || WIN |
---|
967 | ioctlsocket(sp->sock, FIONBIO, &flag); |
---|
968 | #elif ECOS |
---|
969 | int on; |
---|
970 | on = 1; |
---|
971 | ioctl(sp->sock, FIONBIO, &on); |
---|
972 | #elif VXWORKS |
---|
973 | ioctl(sp->sock, FIONBIO, (int)&iflag); |
---|
974 | #else |
---|
975 | fcntl(sp->sock, F_SETFL, fcntl(sp->sock, F_GETFL) | O_NONBLOCK); |
---|
976 | #endif |
---|
977 | } |
---|
978 | return oldBlock; |
---|
979 | } |
---|
980 | |
---|
981 | /******************************************************************************/ |
---|
982 | /* |
---|
983 | * Return true if a readable socket has buffered data. - not public |
---|
984 | */ |
---|
985 | |
---|
986 | int socketDontBlock() |
---|
987 | { |
---|
988 | socket_t *sp; |
---|
989 | int i; |
---|
990 | |
---|
991 | for (i = 0; i < socketMax; i++) { |
---|
992 | if ((sp = socketList[i]) == NULL || |
---|
993 | (sp->handlerMask & SOCKET_READABLE) == 0) { |
---|
994 | continue; |
---|
995 | } |
---|
996 | if (socketInputBuffered(i) > 0) { |
---|
997 | return 1; |
---|
998 | } |
---|
999 | } |
---|
1000 | return 0; |
---|
1001 | } |
---|
1002 | |
---|
1003 | /******************************************************************************/ |
---|
1004 | /* |
---|
1005 | * Return true if a particular socket buffered data. - not public |
---|
1006 | */ |
---|
1007 | |
---|
1008 | int socketSockBuffered(int sock) |
---|
1009 | { |
---|
1010 | socket_t *sp; |
---|
1011 | int i; |
---|
1012 | |
---|
1013 | for (i = 0; i < socketMax; i++) { |
---|
1014 | if ((sp = socketList[i]) == NULL || sp->sock != sock) { |
---|
1015 | continue; |
---|
1016 | } |
---|
1017 | return socketInputBuffered(i); |
---|
1018 | } |
---|
1019 | return 0; |
---|
1020 | } |
---|
1021 | |
---|
1022 | #endif /* (!WIN) | LITTLEFOOT | WEBS */ |
---|
1023 | |
---|
1024 | /******************************************************************************/ |
---|