]>
Commit | Line | Data |
---|---|---|
1 | /* ------------------------------------------------------------------------- | |
2 | * Project: GSocket (Generic Socket) for WX | |
3 | * Name: gsocket.c | |
4 | * Authors: Guilhem Lavaux, | |
5 | * Guillermo Rodriguez Garcia <guille@iies.es> (maintainer) | |
6 | * Stefan CSomor | |
7 | * Purpose: GSocket main mac file | |
8 | * CVSID: $Id$ | |
9 | * ------------------------------------------------------------------------- | |
10 | */ | |
11 | ||
12 | /* | |
13 | * PLEASE don't put C++ comments here - this is a C source file. | |
14 | */ | |
15 | ||
16 | #ifndef __GSOCKET_STANDALONE__ | |
17 | #include "wx/setup.h" | |
18 | #endif | |
19 | ||
20 | #if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) | |
21 | ||
22 | #ifdef __DARWIN__ | |
23 | #include <CoreServices/CoreServices.h> | |
24 | ||
25 | #ifndef FALSE | |
26 | #define FALSE 0 | |
27 | #endif | |
28 | #ifndef TRUE | |
29 | #define TRUE 1 | |
30 | #endif | |
31 | #else | |
32 | #include <MacHeaders.c> | |
33 | #define OTUNIXERRORS 1 | |
34 | #include <OpenTransport.h> | |
35 | #include <OpenTransportProviders.h> | |
36 | #include <OpenTptInternet.h> | |
37 | #endif | |
38 | #if TARGET_CARBON | |
39 | #define OTAssert( str , cond ) /* does not exists in Carbon */ | |
40 | #endif | |
41 | ||
42 | #include <assert.h> | |
43 | #include <errno.h> | |
44 | #include <string.h> | |
45 | #include <unistd.h> | |
46 | #include <stdio.h> | |
47 | #include <stdlib.h> | |
48 | #include <stddef.h> | |
49 | #include <ctype.h> | |
50 | #include <utime.h> | |
51 | ||
52 | /* | |
53 | * INADDR_BROADCAST is identical to INADDR_NONE which is not defined | |
54 | * on all unices. INADDR_BROADCAST should be fine to indicate an error. | |
55 | */ | |
56 | #ifndef INADDR_BROADCAST | |
57 | #define INADDR_BROADCAST 0xFFFFFFFFUL | |
58 | #endif | |
59 | #ifndef INADDR_NONE | |
60 | #define INADDR_NONE INADDR_BROADCAST | |
61 | #endif | |
62 | #ifndef INADDR_ANY | |
63 | #define INADDR_ANY 0x0UL | |
64 | #endif | |
65 | #ifndef __GSOCKET_STANDALONE__ | |
66 | ||
67 | #include "wx/mac/macnotfy.h" | |
68 | #include "wx/mac/gsockmac.h" | |
69 | #include "wx/gsocket.h" | |
70 | ||
71 | #else | |
72 | ||
73 | #include "gsockmac.h" | |
74 | #include "gsocket.h" | |
75 | ||
76 | #endif /* __GSOCKET_STANDALONE__ */ | |
77 | ||
78 | void wxCYield() ; | |
79 | #ifdef __WXDEBUG__ | |
80 | #define qDebug 1 | |
81 | #define qDebug2 1 | |
82 | extern pascal void OTDebugStr(const char* str); | |
83 | #endif | |
84 | #ifndef __DARWIN__ | |
85 | #include <OTDebug.h> | |
86 | #endif | |
87 | InetSvcRef gInetSvcRef = 0 ; | |
88 | int gOTInited = 0 ; | |
89 | ||
90 | OSStatus DoNegotiateIPReuseAddrOption(EndpointRef ep, Boolean enableReuseIPMode); | |
91 | ||
92 | /* Input: ep - endpointref on which to negotiate the option | |
93 | enableReuseIPMode - desired option setting - true/false | |
94 | Return: kOTNoError indicates that the option was successfully negotiated | |
95 | OSStatus is an error if < 0, otherwise, the status field is | |
96 | returned and is > 0. | |
97 | ||
98 | IMPORTANT NOTE: The endpoint is assumed to be in synchronous more, otherwise | |
99 | this code will not function as desired | |
100 | */ | |
101 | ||
102 | ||
103 | OSStatus DoNegotiateIPReuseAddrOption(EndpointRef ep, Boolean enableReuseIPMode) | |
104 | ||
105 | { | |
106 | UInt8 buf[kOTFourByteOptionSize]; // define buffer for fourByte Option size | |
107 | TOption* opt; // option ptr to make items easier to access | |
108 | TOptMgmt req; | |
109 | TOptMgmt ret; | |
110 | OSStatus err; | |
111 | ||
112 | if (!OTIsSynchronous(ep)) | |
113 | { | |
114 | return (-1); | |
115 | } | |
116 | opt = (TOption*)buf; // set option ptr to buffer | |
117 | req.opt.buf = buf; | |
118 | req.opt.len = sizeof(buf); | |
119 | req.flags = T_NEGOTIATE; // negotiate for option | |
120 | ||
121 | ret.opt.buf = buf; | |
122 | ret.opt.maxlen = kOTFourByteOptionSize; | |
123 | ||
124 | opt->level = INET_IP; // dealing with an IP Level function | |
125 | #ifdef __DARWIN__ | |
126 | opt->name = kIP_REUSEADDR; | |
127 | #else | |
128 | opt->name = IP_REUSEADDR; | |
129 | #endif | |
130 | opt->len = kOTFourByteOptionSize; | |
131 | opt->status = 0; | |
132 | *(UInt32*)opt->value = enableReuseIPMode; // set the desired option level, true or false | |
133 | ||
134 | err = OTOptionManagement(ep, &req, &ret); | |
135 | ||
136 | // if no error then return the option status value | |
137 | if (err == kOTNoError) | |
138 | { | |
139 | if (opt->status != T_SUCCESS) | |
140 | err = opt->status; | |
141 | else | |
142 | err = kOTNoError; | |
143 | } | |
144 | ||
145 | return err; | |
146 | } | |
147 | ||
148 | ||
149 | pascal void OTInetEventHandler(void*s, OTEventCode event, OTResult, void *cookie) ; | |
150 | pascal void OTInetEventHandler(void*s, OTEventCode event, OTResult result, void *cookie) | |
151 | { | |
152 | int wakeUp = true ; | |
153 | GSocket* sock = (GSocket*) s ; | |
154 | ||
155 | if ( event == kOTSyncIdleEvent ) | |
156 | { | |
157 | YieldToAnyThread() ; | |
158 | return ; | |
159 | } | |
160 | ||
161 | if ( s ) | |
162 | { | |
163 | wxMacAddEvent( sock->m_mac_events , _GSocket_Internal_Proc , event , s , wakeUp ) ; | |
164 | } | |
165 | ||
166 | return; | |
167 | } | |
168 | ||
169 | static void SetDefaultEndpointModes(EndpointRef ep , void *data ) | |
170 | // This routine sets the supplied endpoint into the default | |
171 | // mode used in this application. The specifics are: | |
172 | // blocking, synchronous, and using synch idle events with | |
173 | // the standard YieldingNotifier. | |
174 | { | |
175 | OSStatus junk = kOTNoError ; | |
176 | OTAssert ("SetDefaultEndpointModes:invalid ref", ep != kOTInvalidEndpointRef ) ; | |
177 | junk = OTSetAsynchronous(ep); | |
178 | OTAssert("SetDefaultEndpointModes: Could not set asynchronous", junk == noErr); | |
179 | /* | |
180 | junk = OTSetBlocking(ep); | |
181 | OTAssert("SetDefaultEndpointModes: Could not set blocking", junk == noErr); | |
182 | junk = OTSetSynchronous(ep); | |
183 | OTAssert("SetDefaultEndpointModes: Could not set synchronous", junk == noErr); | |
184 | junk = OTSetBlocking(ep); | |
185 | OTAssert("SetDefaultEndpointModes: Could not set blocking", junk == noErr); | |
186 | */ | |
187 | junk = OTInstallNotifier(ep, OTInetEventHandler, data); | |
188 | OTAssert("SetDefaultEndpointModes: Could not install notifier", junk == noErr); | |
189 | /* | |
190 | junk = OTUseSyncIdleEvents(ep, true); | |
191 | OTAssert("SetDefaultEndpointModes: Could not use sync idle events", junk == noErr); | |
192 | */ | |
193 | } | |
194 | ||
195 | /* Global initialisers */ | |
196 | ||
197 | int GSocket_Init() | |
198 | { | |
199 | return TRUE; | |
200 | } | |
201 | ||
202 | int GSocket_Verify_Inited() ; | |
203 | int GSocket_Verify_Inited() | |
204 | { | |
205 | OSStatus err ; | |
206 | #if TARGET_CARBON | |
207 | // Marc Newsam: added the clientcontext variable | |
208 | // however, documentation is unclear how this works | |
209 | OTClientContextPtr clientcontext; | |
210 | ||
211 | if ( gInetSvcRef ) | |
212 | return TRUE ; | |
213 | ||
214 | InitOpenTransportInContext(kInitOTForApplicationMask, &clientcontext); | |
215 | gOTInited = 1 ; | |
216 | gInetSvcRef = OTOpenInternetServicesInContext(kDefaultInternetServicesPath, | |
217 | NULL, &err, clientcontext); | |
218 | #else | |
219 | if ( gInetSvcRef ) | |
220 | return TRUE ; | |
221 | ||
222 | InitOpenTransport() ; | |
223 | gOTInited = 1 ; | |
224 | gInetSvcRef = OTOpenInternetServices(kDefaultInternetServicesPath, NULL, &err); | |
225 | #endif | |
226 | if ( gInetSvcRef == NULL || err != kOTNoError ) | |
227 | { | |
228 | OTAssert("Could not open Inet Services", err == noErr); | |
229 | return FALSE ; | |
230 | } | |
231 | return TRUE ; | |
232 | } | |
233 | ||
234 | void GSocket_Cleanup() | |
235 | { | |
236 | if ( gOTInited != 0 ) | |
237 | { | |
238 | if ( gInetSvcRef != NULL ) | |
239 | OTCloseProvider( gInetSvcRef ); | |
240 | #if TARGET_CARBON | |
241 | CloseOpenTransportInContext( NULL ) ; | |
242 | #else | |
243 | CloseOpenTransport() ; | |
244 | #endif | |
245 | } | |
246 | } | |
247 | ||
248 | /* Constructors / Destructors for GSocket */ | |
249 | ||
250 | GSocket *GSocket_new() | |
251 | { | |
252 | ||
253 | int i; | |
254 | GSocket *socket; | |
255 | ||
256 | if ( GSocket_Verify_Inited() == FALSE ) | |
257 | return NULL ; | |
258 | ||
259 | socket = (GSocket *)malloc(sizeof(GSocket)); | |
260 | ||
261 | if (socket == NULL) | |
262 | return NULL; | |
263 | ||
264 | socket->m_endpoint = NULL ; | |
265 | for (i=0;i<GSOCK_MAX_EVENT;i++) | |
266 | { | |
267 | socket->m_cbacks[i] = NULL; | |
268 | } | |
269 | socket->m_detected = 0; | |
270 | socket->m_local = NULL; | |
271 | socket->m_peer = NULL; | |
272 | socket->m_error = GSOCK_NOERROR; | |
273 | socket->m_server = FALSE; | |
274 | socket->m_stream = TRUE; | |
275 | socket->m_non_blocking = FALSE; | |
276 | socket->m_timeout = 1*1000; | |
277 | /* 10 sec * 1000 millisec */ | |
278 | socket->m_takesEvents = TRUE ; | |
279 | socket->m_mac_events = wxMacGetNotifierTable() ; | |
280 | return socket; | |
281 | } | |
282 | ||
283 | void GSocket_destroy(GSocket *socket) | |
284 | { | |
285 | assert(socket != NULL); | |
286 | ||
287 | /* Check that the socket is really shutdowned */ | |
288 | if (socket->m_endpoint != kOTInvalidEndpointRef) | |
289 | GSocket_Shutdown(socket); | |
290 | ||
291 | ||
292 | /* Destroy private addresses */ | |
293 | if (socket->m_local) | |
294 | GAddress_destroy(socket->m_local); | |
295 | ||
296 | if (socket->m_peer) | |
297 | GAddress_destroy(socket->m_peer); | |
298 | ||
299 | /* Destroy the socket itself */ | |
300 | free(socket); | |
301 | } | |
302 | ||
303 | /* GSocket_Shutdown: | |
304 | * Disallow further read/write operations on this socket, close | |
305 | * the fd and disable all callbacks. | |
306 | */ | |
307 | void GSocket_Shutdown(GSocket *socket) | |
308 | { | |
309 | OSStatus err ; | |
310 | int evt; | |
311 | ||
312 | assert(socket != NULL); | |
313 | ||
314 | /* If socket has been created, shutdown it */ | |
315 | if (socket->m_endpoint != kOTInvalidEndpointRef ) | |
316 | { | |
317 | err = OTSndOrderlyDisconnect( socket->m_endpoint ) ; | |
318 | if ( err != kOTNoError ) | |
319 | { | |
320 | ||
321 | } | |
322 | err = OTRcvOrderlyDisconnect( socket->m_endpoint ) ; | |
323 | err = OTUnbind( socket->m_endpoint ) ; | |
324 | err = OTCloseProvider( socket->m_endpoint ) ; | |
325 | socket->m_endpoint = kOTInvalidEndpointRef ; | |
326 | } | |
327 | ||
328 | /* Disable GUI callbacks */ | |
329 | for (evt = 0; evt < GSOCK_MAX_EVENT; evt++) | |
330 | socket->m_cbacks[evt] = NULL; | |
331 | ||
332 | socket->m_detected = 0; | |
333 | _GSocket_Disable_Events(socket); | |
334 | wxMacRemoveAllNotifiersForData( wxMacGetNotifierTable() , socket ) ; | |
335 | } | |
336 | ||
337 | ||
338 | /* Address handling */ | |
339 | ||
340 | /* GSocket_SetLocal: | |
341 | * GSocket_GetLocal: | |
342 | * GSocket_SetPeer: | |
343 | * GSocket_GetPeer: | |
344 | * Set or get the local or peer address for this socket. The 'set' | |
345 | * functions return GSOCK_NOERROR on success, an error code otherwise. | |
346 | * The 'get' functions return a pointer to a GAddress object on success, | |
347 | * or NULL otherwise, in which case they set the error code of the | |
348 | * corresponding GSocket. | |
349 | * | |
350 | * Error codes: | |
351 | * GSOCK_INVSOCK - the socket is not valid. | |
352 | * GSOCK_INVADDR - the address is not valid. | |
353 | */ | |
354 | GSocketError GSocket_SetLocal(GSocket *socket, GAddress *address) | |
355 | { | |
356 | assert(socket != NULL); | |
357 | ||
358 | /* the socket must be initialized, or it must be a server */ | |
359 | if ((socket->m_endpoint != kOTInvalidEndpointRef && !socket->m_server)) | |
360 | { | |
361 | socket->m_error = GSOCK_INVSOCK; | |
362 | return GSOCK_INVSOCK; | |
363 | } | |
364 | ||
365 | /* check address */ | |
366 | if (address == NULL || address->m_family == GSOCK_NOFAMILY) | |
367 | { | |
368 | socket->m_error = GSOCK_INVADDR; | |
369 | return GSOCK_INVADDR; | |
370 | } | |
371 | ||
372 | if (socket->m_local) | |
373 | GAddress_destroy(socket->m_local); | |
374 | ||
375 | socket->m_local = GAddress_copy(address); | |
376 | ||
377 | return GSOCK_NOERROR; | |
378 | } | |
379 | ||
380 | GSocketError GSocket_SetPeer(GSocket *socket, GAddress *address) | |
381 | { | |
382 | assert(socket != NULL); | |
383 | ||
384 | /* check address */ | |
385 | if (address == NULL || address->m_family == GSOCK_NOFAMILY) | |
386 | { | |
387 | socket->m_error = GSOCK_INVADDR; | |
388 | return GSOCK_INVADDR; | |
389 | } | |
390 | ||
391 | if (socket->m_peer) | |
392 | GAddress_destroy(socket->m_peer); | |
393 | ||
394 | socket->m_peer = GAddress_copy(address); | |
395 | ||
396 | return GSOCK_NOERROR; | |
397 | } | |
398 | ||
399 | GAddress *GSocket_GetLocal(GSocket *socket) | |
400 | { | |
401 | GAddress *address = NULL ; | |
402 | GSocketError err; | |
403 | InetAddress loc ; | |
404 | ||
405 | assert(socket != NULL); | |
406 | ||
407 | /* try to get it from the m_local var first */ | |
408 | if (socket->m_local) | |
409 | return GAddress_copy(socket->m_local); | |
410 | ||
411 | /* else, if the socket is initialized, try getsockname */ | |
412 | if (socket->m_endpoint == kOTInvalidEndpointRef) | |
413 | { | |
414 | socket->m_error = GSOCK_INVSOCK; | |
415 | return NULL; | |
416 | } | |
417 | ||
418 | ||
419 | /* we do not support multihoming with this code at the moment | |
420 | OTGetProtAddress will have to be used then - but we don't have a handy | |
421 | method to use right now | |
422 | */ | |
423 | { | |
424 | InetInterfaceInfo info; | |
425 | OTInetGetInterfaceInfo(&info, kDefaultInetInterface); | |
426 | loc.fHost = info.fAddress ; | |
427 | loc.fPort = 0 ; | |
428 | loc.fAddressType = AF_INET ; | |
429 | } | |
430 | ||
431 | /* got a valid address from getsockname, create a GAddress object */ | |
432 | address = GAddress_new(); | |
433 | if (address == NULL) | |
434 | { | |
435 | socket->m_error = GSOCK_MEMERR; | |
436 | return NULL; | |
437 | } | |
438 | ||
439 | err = _GAddress_translate_from(address, &loc); | |
440 | if (err != GSOCK_NOERROR) | |
441 | { | |
442 | GAddress_destroy(address); | |
443 | socket->m_error = err; | |
444 | return NULL; | |
445 | } | |
446 | ||
447 | return address; | |
448 | } | |
449 | ||
450 | GAddress *GSocket_GetPeer(GSocket *socket) | |
451 | { | |
452 | assert(socket != NULL); | |
453 | ||
454 | /* try to get it from the m_peer var */ | |
455 | if (socket->m_peer) | |
456 | return GAddress_copy(socket->m_peer); | |
457 | ||
458 | return NULL; | |
459 | } | |
460 | ||
461 | /* Server specific parts */ | |
462 | ||
463 | /* GSocket_SetServer: | |
464 | * Sets up this socket as a server. The local address must have been | |
465 | * set with GSocket_SetLocal() before GSocket_SetServer() is called. | |
466 | * Returns GSOCK_NOERROR on success, one of the following otherwise: | |
467 | * | |
468 | * Error codes: | |
469 | * GSOCK_INVSOCK - the socket is in use. | |
470 | * GSOCK_INVADDR - the local address has not been set. | |
471 | * GSOCK_IOERR - low-level error. | |
472 | */ | |
473 | GSocketError GSocket_SetServer(GSocket *sck) | |
474 | { | |
475 | int type; | |
476 | int arg = 1; | |
477 | ||
478 | assert(sck != NULL); | |
479 | ||
480 | /* must not be in use */ | |
481 | if (sck->m_endpoint != kOTInvalidEndpointRef ) | |
482 | { | |
483 | sck->m_error = GSOCK_INVSOCK; | |
484 | return GSOCK_INVSOCK; | |
485 | } | |
486 | ||
487 | /* the local addr must have been set */ | |
488 | if (!sck->m_local) | |
489 | { | |
490 | sck->m_error = GSOCK_INVADDR; | |
491 | return GSOCK_INVADDR; | |
492 | } | |
493 | ||
494 | /* Initialize all fields */ | |
495 | sck->m_stream = TRUE; | |
496 | sck->m_server = TRUE; | |
497 | sck->m_oriented = TRUE; | |
498 | ||
499 | // TODO | |
500 | #if 0 | |
501 | /* Create the socket */ | |
502 | sck->m_endpoint = socket(sck->m_local->m_realfamily, SOCK_STREAM, 0); | |
503 | socket_set_ref( sck->m_endpoint , (unsigned long) &gMacNetEvents , (unsigned long) sck ) ; | |
504 | if (sck->m_endpoint == kOTInvalidEndpointRef) | |
505 | { | |
506 | sck->m_error = GSOCK_IOERR; | |
507 | return GSOCK_IOERR; | |
508 | } | |
509 | ||
510 | ioctl(sck->m_endpoint, FIONBIO, &arg); | |
511 | _GSocket_Enable_Events(sck); | |
512 | ||
513 | /* Bind to the local address, | |
514 | * retrieve the actual address bound, | |
515 | * and listen up to 5 connections. | |
516 | */ | |
517 | if ((bind(sck->m_endpoint, sck->m_local->m_addr, sck->m_local->m_len) != 0) || | |
518 | (getsockname(sck->m_endpoint, | |
519 | sck->m_local->m_addr, | |
520 | (SOCKLEN_T *) &sck->m_local->m_len) != 0) || | |
521 | (listen(sck->m_endpoint, 5) != 0)) | |
522 | { | |
523 | close(sck->m_endpoint); | |
524 | sck->m_endpoint = -1; | |
525 | sck->m_error = GSOCK_IOERR; | |
526 | return GSOCK_IOERR; | |
527 | } | |
528 | #endif | |
529 | return GSOCK_NOERROR; | |
530 | } | |
531 | ||
532 | /* GSocket_WaitConnection: | |
533 | * Waits for an incoming client connection. Returns a pointer to | |
534 | * a GSocket object, or NULL if there was an error, in which case | |
535 | * the last error field will be updated for the calling GSocket. | |
536 | * | |
537 | * Error codes (set in the calling GSocket) | |
538 | * GSOCK_INVSOCK - the socket is not valid or not a server. | |
539 | * GSOCK_TIMEDOUT - timeout, no incoming connections. | |
540 | * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking. | |
541 | * GSOCK_MEMERR - couldn't allocate memory. | |
542 | * GSOCK_IOERR - low-level error. | |
543 | */ | |
544 | GSocket *GSocket_WaitConnection(GSocket *socket) | |
545 | { | |
546 | GSocket *connection = NULL ; | |
547 | GSocketError err; | |
548 | ||
549 | int arg = 1; | |
550 | ||
551 | assert(socket != NULL); | |
552 | ||
553 | /* Reenable CONNECTION events */ | |
554 | socket->m_detected &= ~GSOCK_CONNECTION_FLAG; | |
555 | ||
556 | /* If the socket has already been created, we exit immediately */ | |
557 | if (socket->m_endpoint == kOTInvalidEndpointRef || !socket->m_server) | |
558 | { | |
559 | socket->m_error = GSOCK_INVSOCK; | |
560 | return NULL; | |
561 | } | |
562 | ||
563 | /* Create a GSocket object for the new connection */ | |
564 | connection = GSocket_new(); | |
565 | ||
566 | if (!connection) | |
567 | { | |
568 | socket->m_error = GSOCK_MEMERR; | |
569 | return NULL; | |
570 | } | |
571 | ||
572 | /* Wait for a connection (with timeout) */ | |
573 | if (_GSocket_Input_Timeout(socket) == GSOCK_TIMEDOUT) | |
574 | { | |
575 | GSocket_destroy(connection); | |
576 | /* socket->m_error set by _GSocket_Input_Timeout */ | |
577 | return NULL; | |
578 | } | |
579 | ||
580 | // TODO | |
581 | #if 0 | |
582 | connection->m_endpoint = accept(socket->m_endpoint, &from, (SOCKLEN_T *) &fromlen); | |
583 | #endif | |
584 | ||
585 | if (connection->m_endpoint == kOTInvalidEndpointRef ) | |
586 | { | |
587 | if (errno == EWOULDBLOCK) | |
588 | socket->m_error = GSOCK_WOULDBLOCK; | |
589 | else | |
590 | socket->m_error = GSOCK_IOERR; | |
591 | ||
592 | GSocket_destroy(connection); | |
593 | return NULL; | |
594 | } | |
595 | ||
596 | /* Initialize all fields */ | |
597 | connection->m_server = FALSE; | |
598 | connection->m_stream = TRUE; | |
599 | connection->m_oriented = TRUE; | |
600 | ||
601 | /* Setup the peer address field */ | |
602 | connection->m_peer = GAddress_new(); | |
603 | if (!connection->m_peer) | |
604 | { | |
605 | GSocket_destroy(connection); | |
606 | socket->m_error = GSOCK_MEMERR; | |
607 | return NULL; | |
608 | } | |
609 | // TODO | |
610 | #if 0 | |
611 | err = _GAddress_translate_from(connection->m_peer, &from, fromlen); | |
612 | if (err != GSOCK_NOERROR) | |
613 | { | |
614 | GAddress_destroy(connection->m_peer); | |
615 | GSocket_destroy(connection); | |
616 | socket->m_error = err; | |
617 | return NULL; | |
618 | } | |
619 | ||
620 | ioctl(connection->m_endpoint, FIONBIO, &arg); | |
621 | #endif | |
622 | _GSocket_Enable_Events(connection); | |
623 | ||
624 | return connection; | |
625 | } | |
626 | ||
627 | /* Datagram sockets */ | |
628 | ||
629 | /* GSocket_SetNonOriented: | |
630 | * Sets up this socket as a non-connection oriented (datagram) socket. | |
631 | * Before using this function, the local address must have been set | |
632 | * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR | |
633 | * on success, or one of the following otherwise. | |
634 | * | |
635 | * Error codes: | |
636 | * GSOCK_INVSOCK - the socket is in use. | |
637 | * GSOCK_INVADDR - the local address has not been set. | |
638 | * GSOCK_IOERR - low-level error. | |
639 | */ | |
640 | GSocketError GSocket_SetNonOriented(GSocket *sck) | |
641 | { | |
642 | int arg = 1; | |
643 | ||
644 | assert(sck != NULL); | |
645 | ||
646 | if (sck->m_endpoint != kOTInvalidEndpointRef ) | |
647 | { | |
648 | sck->m_error = GSOCK_INVSOCK; | |
649 | return GSOCK_INVSOCK; | |
650 | } | |
651 | ||
652 | if (!sck->m_local) | |
653 | { | |
654 | sck->m_error = GSOCK_INVADDR; | |
655 | return GSOCK_INVADDR; | |
656 | } | |
657 | ||
658 | /* Initialize all fields */ | |
659 | sck->m_stream = FALSE; | |
660 | sck->m_server = FALSE; | |
661 | sck->m_oriented = FALSE; | |
662 | ||
663 | /* Create the socket */ | |
664 | ||
665 | // TODO | |
666 | #if 0 | |
667 | sck->m_endpoint = socket(sck->m_local->m_realfamily, SOCK_DGRAM, 0); | |
668 | socket_set_ref( sck->m_endpoint , (unsigned long) &gMacNetEvents , (unsigned long) sck ) ; | |
669 | #endif | |
670 | if (sck->m_endpoint == kOTInvalidEndpointRef ) | |
671 | { | |
672 | sck->m_error = GSOCK_IOERR; | |
673 | return GSOCK_IOERR; | |
674 | } | |
675 | ||
676 | // TODO | |
677 | #if 0 | |
678 | ioctl(sck->m_endpoint, FIONBIO, &arg); | |
679 | #endif | |
680 | _GSocket_Enable_Events(sck); | |
681 | ||
682 | /* Bind to the local address, | |
683 | * and retrieve the actual address bound. | |
684 | */ | |
685 | // TODO | |
686 | #if 0 | |
687 | if ((bind(sck->m_endpoint, sck->m_local->m_addr, sck->m_local->m_len) != 0) || | |
688 | (getsockname(sck->m_endpoint, | |
689 | sck->m_local->m_addr, | |
690 | (SOCKLEN_T *) &sck->m_local->m_len) != 0)) | |
691 | { | |
692 | close(sck->m_endpoint); | |
693 | sck->m_endpoint = -1; | |
694 | sck->m_error = GSOCK_IOERR; | |
695 | return GSOCK_IOERR; | |
696 | } | |
697 | #endif | |
698 | return GSOCK_NOERROR; | |
699 | } | |
700 | ||
701 | /* Client specific parts */ | |
702 | ||
703 | /* GSocket_Connect: | |
704 | * For stream (connection oriented) sockets, GSocket_Connect() tries | |
705 | * to establish a client connection to a server using the peer address | |
706 | * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the | |
707 | * connection has been succesfully established, or one of the error | |
708 | * codes listed below. Note that for nonblocking sockets, a return | |
709 | * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection | |
710 | * request can be completed later; you should use GSocket_Select() | |
711 | * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the | |
712 | * corresponding asynchronous events. | |
713 | * | |
714 | * For datagram (non connection oriented) sockets, GSocket_Connect() | |
715 | * just sets the peer address established with GSocket_SetPeer() as | |
716 | * default destination. | |
717 | * | |
718 | * Error codes: | |
719 | * GSOCK_INVSOCK - the socket is in use or not valid. | |
720 | * GSOCK_INVADDR - the peer address has not been established. | |
721 | * GSOCK_TIMEDOUT - timeout, the connection failed. | |
722 | * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only) | |
723 | * GSOCK_MEMERR - couldn't allocate memory. | |
724 | * GSOCK_IOERR - low-level error. | |
725 | */ | |
726 | GSocketError GSocket_Connect(GSocket *sck, GSocketStream stream) | |
727 | { | |
728 | int ret; | |
729 | int arg = 1; | |
730 | InetAddress addr ; | |
731 | TEndpointInfo info; | |
732 | OTFlags flags = 0; | |
733 | OSStatus err = kOTNoError; | |
734 | TCall peer ; | |
735 | ||
736 | assert(sck != NULL); | |
737 | ||
738 | /* Enable CONNECTION events (needed for nonblocking connections) */ | |
739 | sck->m_detected &= ~GSOCK_CONNECTION_FLAG; | |
740 | ||
741 | if (sck->m_endpoint != kOTInvalidEndpointRef ) | |
742 | { | |
743 | sck->m_error = GSOCK_INVSOCK; | |
744 | return GSOCK_INVSOCK; | |
745 | } | |
746 | ||
747 | if (!sck->m_peer) | |
748 | { | |
749 | sck->m_error = GSOCK_INVADDR; | |
750 | return GSOCK_INVADDR; | |
751 | } | |
752 | ||
753 | /* Streamed or dgram socket? */ | |
754 | sck->m_stream = (stream == GSOCK_STREAMED); | |
755 | sck->m_oriented = TRUE; | |
756 | sck->m_server = FALSE; | |
757 | ||
758 | /* Create the socket */ | |
759 | #if TARGET_CARBON | |
760 | sck->m_endpoint = | |
761 | OTOpenEndpointInContext( OTCreateConfiguration( kTCPName) , 0 , &info , &err , NULL ) ; | |
762 | #else | |
763 | sck->m_endpoint = | |
764 | OTOpenEndpoint( OTCreateConfiguration( kTCPName) , 0 , &info , &err ) ; | |
765 | #endif | |
766 | if ( sck->m_endpoint == kOTInvalidEndpointRef || err != kOTNoError ) | |
767 | { | |
768 | sck->m_endpoint = kOTInvalidEndpointRef ; | |
769 | sck->m_error = GSOCK_IOERR; | |
770 | return GSOCK_IOERR; | |
771 | } | |
772 | err = OTBind( sck->m_endpoint , nil , nil ) ; | |
773 | if ( err != kOTNoError ) | |
774 | { | |
775 | return GSOCK_IOERR; | |
776 | } | |
777 | SetDefaultEndpointModes( sck->m_endpoint , sck ) ; | |
778 | // TODO | |
779 | #if 0 | |
780 | ioctl(sck->m_endpoint, FIONBIO, &arg); | |
781 | #endif | |
782 | _GSocket_Enable_Events(sck); | |
783 | ||
784 | _GAddress_translate_to( sck->m_peer , &addr ) ; | |
785 | memset( &peer , 0 , sizeof( TCall ) ) ; | |
786 | peer.addr.len = sizeof( InetAddress ) ; | |
787 | peer.addr.buf = (unsigned char*) &addr ; | |
788 | err = OTConnect( sck->m_endpoint , &peer , nil ) ; | |
789 | if ( err != noErr ) | |
790 | { | |
791 | /* If connect failed with EINPROGRESS and the GSocket object | |
792 | * is in blocking mode, we select() for the specified timeout | |
793 | * checking for writability to see if the connection request | |
794 | * completes. | |
795 | */ | |
796 | ||
797 | if ((err == kOTNoDataErr ) && (!sck->m_non_blocking)) | |
798 | { | |
799 | if (_GSocket_Output_Timeout(sck) == GSOCK_TIMEDOUT) | |
800 | { | |
801 | OTSndOrderlyDisconnect( sck->m_endpoint ) ; | |
802 | sck->m_endpoint = kOTInvalidEndpointRef ; | |
803 | /* sck->m_error is set in _GSocket_Output_Timeout */ | |
804 | return GSOCK_TIMEDOUT; | |
805 | } | |
806 | else | |
807 | { | |
808 | /* | |
809 | int error; | |
810 | SOCKLEN_T len = sizeof(error); | |
811 | ||
812 | getsockopt(sck->m_endpoint, SOL_SOCKET, SO_ERROR, (void*) &error, &len); | |
813 | ||
814 | if (!error) | |
815 | */ | |
816 | return GSOCK_NOERROR; | |
817 | } | |
818 | } | |
819 | ||
820 | /* If connect failed with EINPROGRESS and the GSocket object | |
821 | * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK | |
822 | * (and return GSOCK_WOULDBLOCK) but we don't close the socket; | |
823 | * this way if the connection completes, a GSOCK_CONNECTION | |
824 | * event will be generated, if enabled. | |
825 | */ | |
826 | if ((err == kOTNoDataErr) && (sck->m_non_blocking)) | |
827 | { | |
828 | sck->m_error = GSOCK_WOULDBLOCK; | |
829 | return GSOCK_WOULDBLOCK; | |
830 | } | |
831 | ||
832 | /* If connect failed with an error other than EINPROGRESS, | |
833 | * then the call to GSocket_Connect has failed. | |
834 | */ | |
835 | OTSndOrderlyDisconnect( sck->m_endpoint ) ; | |
836 | ||
837 | sck->m_endpoint = kOTInvalidEndpointRef ; | |
838 | sck->m_error = GSOCK_IOERR; | |
839 | return GSOCK_IOERR; | |
840 | } | |
841 | // OTInetEventHandler(sck, T_CONNECT , kOTNoError , NULL ) ; | |
842 | return GSOCK_NOERROR; | |
843 | } | |
844 | ||
845 | /* Generic IO */ | |
846 | ||
847 | /* Like recv(), send(), ... */ | |
848 | int GSocket_Read(GSocket *socket, char *buffer, int size) | |
849 | { | |
850 | int ret = 0 ; | |
851 | ||
852 | assert(socket != NULL); | |
853 | ||
854 | /* Reenable INPUT events */ | |
855 | socket->m_detected &= ~GSOCK_INPUT_FLAG; | |
856 | ||
857 | if (socket->m_endpoint == kOTInvalidEndpointRef || socket->m_server) | |
858 | { | |
859 | socket->m_error = GSOCK_INVSOCK; | |
860 | return -1; | |
861 | } | |
862 | ||
863 | /* If the socket is blocking, wait for data (with a timeout) */ | |
864 | if (_GSocket_Input_Timeout(socket) == GSOCK_TIMEDOUT) | |
865 | return -1; | |
866 | ||
867 | /* Read the data */ | |
868 | if (socket->m_stream) | |
869 | ret = _GSocket_Recv_Stream(socket, buffer, size); | |
870 | else | |
871 | ret = _GSocket_Recv_Dgram(socket, buffer, size); | |
872 | ||
873 | if (ret == -1) | |
874 | { | |
875 | if (errno == EWOULDBLOCK) | |
876 | socket->m_error = GSOCK_WOULDBLOCK; | |
877 | else | |
878 | socket->m_error = GSOCK_IOERR; | |
879 | } | |
880 | ||
881 | return ret; | |
882 | } | |
883 | ||
884 | int GSocket_Write(GSocket *socket, const char *buffer, int size) | |
885 | { | |
886 | int ret; | |
887 | ||
888 | assert(socket != NULL); | |
889 | ||
890 | if (socket->m_endpoint == kOTInvalidEndpointRef || socket->m_server) | |
891 | { | |
892 | socket->m_error = GSOCK_INVSOCK; | |
893 | return -1; | |
894 | } | |
895 | ||
896 | /* If the socket is blocking, wait for writability (with a timeout) */ | |
897 | if (_GSocket_Output_Timeout(socket) == GSOCK_TIMEDOUT) | |
898 | return -1; | |
899 | ||
900 | /* Write the data */ | |
901 | if (socket->m_stream) | |
902 | ret = _GSocket_Send_Stream(socket, buffer, size); | |
903 | else | |
904 | ret = _GSocket_Send_Dgram(socket, buffer, size); | |
905 | ||
906 | if (ret == -1) | |
907 | { | |
908 | if (errno == EWOULDBLOCK) | |
909 | socket->m_error = GSOCK_WOULDBLOCK; | |
910 | else | |
911 | socket->m_error = GSOCK_IOERR; | |
912 | ||
913 | /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect | |
914 | * in MSW). Once the first OUTPUT event is received, users can assume | |
915 | * that the socket is writable until a read operation fails. Only then | |
916 | * will further OUTPUT events be posted. | |
917 | */ | |
918 | socket->m_detected &= ~GSOCK_OUTPUT_FLAG; | |
919 | return -1; | |
920 | } | |
921 | ||
922 | return ret; | |
923 | } | |
924 | ||
925 | /* GSocket_Select: | |
926 | * Polls the socket to determine its status. This function will | |
927 | * check for the events specified in the 'flags' parameter, and | |
928 | * it will return a mask indicating which operations can be | |
929 | * performed. This function won't block, regardless of the | |
930 | * mode (blocking | nonblocking) of the socket. | |
931 | */ | |
932 | GSocketEventFlags GSocket_Select(GSocket *socket, GSocketEventFlags flags) | |
933 | { | |
934 | OTResult state ; | |
935 | assert(socket != NULL); | |
936 | wxMacProcessNotifierEvents() ; | |
937 | /* | |
938 | state = OTGetEndpointState(socket->m_endpoint); | |
939 | ||
940 | if ( ( flags & GSOCK_INPUT_FLAG ) && ! ( socket->m_detected & GSOCK_INPUT_FLAG ) ) | |
941 | { | |
942 | size_t sz = 0 ; | |
943 | OTCountDataBytes( socket->m_endpoint , &sz ) ; | |
944 | if ( state == T_INCON || sz > 0 ) | |
945 | { | |
946 | socket->m_detected |= GSOCK_INPUT_FLAG ; | |
947 | (socket->m_cbacks[GSOCK_INPUT])(socket, GSOCK_INPUT, socket->m_data[GSOCK_INPUT]); | |
948 | } | |
949 | } | |
950 | if ( ( flags & GSOCK_INPUT_FLAG ) && ! ( socket->m_detected & GSOCK_OUTPUT_FLAG ) ) | |
951 | { | |
952 | if ( state == T_DATAXFER || state == T_INREL ) | |
953 | { | |
954 | socket->m_detected |=GSOCK_OUTPUT_FLAG ; | |
955 | (socket->m_cbacks[GSOCK_OUTPUT])(socket, GSOCK_OUTPUT, socket->m_data[GSOCK_OUTPUT]); | |
956 | } | |
957 | } | |
958 | */ | |
959 | return ( flags & socket->m_detected ) ; | |
960 | } | |
961 | ||
962 | /* Flags */ | |
963 | ||
964 | /* GSocket_SetNonBlocking: | |
965 | * Sets the socket to non-blocking mode. All IO calls will return | |
966 | * immediately. | |
967 | */ | |
968 | void GSocket_SetNonBlocking(GSocket *socket, int non_block) | |
969 | { | |
970 | assert(socket != NULL); | |
971 | ||
972 | socket->m_non_blocking = non_block; | |
973 | } | |
974 | ||
975 | /* GSocket_SetTimeout: | |
976 | * Sets the timeout for blocking calls. Time is expressed in | |
977 | * milliseconds. | |
978 | */ | |
979 | void GSocket_SetTimeout(GSocket *socket, unsigned long millisec) | |
980 | { | |
981 | assert(socket != NULL); | |
982 | ||
983 | // this is usually set too high and we have not yet been able to detect a closed | |
984 | // stream, thus we leave the 10 sec timeout | |
985 | // socket->m_timeout = millisec; | |
986 | } | |
987 | ||
988 | /* GSocket_GetError: | |
989 | * Returns the last error occured for this socket. Note that successful | |
990 | * operations do not clear this back to GSOCK_NOERROR, so use it only | |
991 | * after an error. | |
992 | */ | |
993 | GSocketError GSocket_GetError(GSocket *socket) | |
994 | { | |
995 | assert(socket != NULL); | |
996 | ||
997 | return socket->m_error; | |
998 | } | |
999 | ||
1000 | /* Callbacks */ | |
1001 | ||
1002 | /* GSOCK_INPUT: | |
1003 | * There is data to be read in the input buffer. If, after a read | |
1004 | * operation, there is still data available, the callback function will | |
1005 | * be called again. | |
1006 | * GSOCK_OUTPUT: | |
1007 | * The socket is available for writing. That is, the next write call | |
1008 | * won't block. This event is generated only once, when the connection is | |
1009 | * first established, and then only if a call failed with GSOCK_WOULDBLOCK, | |
1010 | * when the output buffer empties again. This means that the app should | |
1011 | * assume that it can write since the first OUTPUT event, and no more | |
1012 | * OUTPUT events will be generated unless an error occurs. | |
1013 | * GSOCK_CONNECTION: | |
1014 | * Connection succesfully established, for client sockets, or incoming | |
1015 | * client connection, for server sockets. Wait for this event (also watch | |
1016 | * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call. | |
1017 | * GSOCK_LOST: | |
1018 | * The connection is lost (or a connection request failed); this could | |
1019 | * be due to a failure, or due to the peer closing it gracefully. | |
1020 | */ | |
1021 | ||
1022 | /* GSocket_SetCallback: | |
1023 | * Enables the callbacks specified by 'flags'. Note that 'flags' | |
1024 | * may be a combination of flags OR'ed toghether, so the same | |
1025 | * callback function can be made to accept different events. | |
1026 | * The callback function must have the following prototype: | |
1027 | * | |
1028 | * void function(GSocket *socket, GSocketEvent event, char *cdata) | |
1029 | */ | |
1030 | void GSocket_SetCallback(GSocket *socket, GSocketEventFlags flags, | |
1031 | GSocketCallback callback, char *cdata) | |
1032 | { | |
1033 | int count; | |
1034 | ||
1035 | assert(socket != NULL); | |
1036 | ||
1037 | for (count = 0; count < GSOCK_MAX_EVENT; count++) | |
1038 | { | |
1039 | if ((flags & (1 << count)) != 0) | |
1040 | { | |
1041 | socket->m_cbacks[count] = callback; | |
1042 | socket->m_data[count] = cdata; | |
1043 | } | |
1044 | } | |
1045 | } | |
1046 | ||
1047 | /* GSocket_UnsetCallback: | |
1048 | * Disables all callbacks specified by 'flags', which may be a | |
1049 | * combination of flags OR'ed toghether. | |
1050 | */ | |
1051 | void GSocket_UnsetCallback(GSocket *socket, GSocketEventFlags flags) | |
1052 | { | |
1053 | int count; | |
1054 | ||
1055 | assert(socket != NULL); | |
1056 | ||
1057 | for (count = 0; count < GSOCK_MAX_EVENT; count++) | |
1058 | { | |
1059 | if ((flags & (1 << count)) != 0) | |
1060 | { | |
1061 | socket->m_cbacks[count] = NULL; | |
1062 | socket->m_data[count] = NULL; | |
1063 | } | |
1064 | } | |
1065 | } | |
1066 | ||
1067 | ||
1068 | #define CALL_CALLBACK(socket, event) { \ | |
1069 | _GSocket_Disable(socket, event); \ | |
1070 | if (socket->m_cbacks[event]) \ | |
1071 | socket->m_cbacks[event](socket, event, socket->m_data[event]); \ | |
1072 | } | |
1073 | ||
1074 | int _GSocket_Recv_Stream(GSocket *socket, char *buffer, int size) | |
1075 | { | |
1076 | OTFlags flags ; | |
1077 | OTResult res ; | |
1078 | OTByteCount sz = 0 ; | |
1079 | ||
1080 | OTCountDataBytes( socket->m_endpoint , &sz ) ; | |
1081 | if ( size > sz ) | |
1082 | size = sz ; | |
1083 | res = OTRcv( socket->m_endpoint , buffer , size , &flags ) ; | |
1084 | if ( res < 0 ) | |
1085 | { | |
1086 | return -1 ; | |
1087 | } | |
1088 | ||
1089 | // we simulate another read event if there are still bytes | |
1090 | if ( socket->m_takesEvents ) | |
1091 | { | |
1092 | OTByteCount sz = 0 ; | |
1093 | OTCountDataBytes( socket->m_endpoint , &sz ) ; | |
1094 | if ( sz > 0 ) | |
1095 | { | |
1096 | socket->m_detected |= GSOCK_INPUT_FLAG ; | |
1097 | (socket->m_cbacks[GSOCK_INPUT])(socket, GSOCK_INPUT, socket->m_data[GSOCK_INPUT]); | |
1098 | } | |
1099 | } | |
1100 | return res ; | |
1101 | } | |
1102 | ||
1103 | int _GSocket_Recv_Dgram(GSocket *socket, char *buffer, int size) | |
1104 | { | |
1105 | // TODO | |
1106 | int ret = -1; | |
1107 | #if 0 | |
1108 | struct sockaddr from; | |
1109 | SOCKLEN_T fromlen = sizeof(from); | |
1110 | GSocketError err; | |
1111 | ||
1112 | fromlen = sizeof(from); | |
1113 | ||
1114 | ret = recvfrom(socket->m_endpoint, buffer, size, 0, &from, (SOCKLEN_T *) &fromlen); | |
1115 | ||
1116 | if (ret == -1) | |
1117 | return -1; | |
1118 | ||
1119 | /* Translate a system address into a GSocket address */ | |
1120 | if (!socket->m_peer) | |
1121 | { | |
1122 | socket->m_peer = GAddress_new(); | |
1123 | if (!socket->m_peer) | |
1124 | { | |
1125 | socket->m_error = GSOCK_MEMERR; | |
1126 | return -1; | |
1127 | } | |
1128 | } | |
1129 | err = _GAddress_translate_from(socket->m_peer, &from, fromlen); | |
1130 | if (err != GSOCK_NOERROR) | |
1131 | { | |
1132 | GAddress_destroy(socket->m_peer); | |
1133 | socket->m_peer = NULL; | |
1134 | socket->m_error = err; | |
1135 | return -1; | |
1136 | } | |
1137 | #endif | |
1138 | return ret; | |
1139 | } | |
1140 | ||
1141 | int _GSocket_Send_Stream(GSocket *socket, const char *buffer, int size) | |
1142 | { | |
1143 | OTFlags flags = 0 ; | |
1144 | OTResult res ; | |
1145 | ||
1146 | res = OTSnd( socket->m_endpoint , (void*) buffer , size , flags ) ; | |
1147 | return res ; | |
1148 | } | |
1149 | ||
1150 | int _GSocket_Send_Dgram(GSocket *socket, const char *buffer, int size) | |
1151 | { | |
1152 | int ret = -1 ; | |
1153 | // TODO | |
1154 | #if 0 | |
1155 | struct sockaddr *addr; | |
1156 | int len ; | |
1157 | GSocketError err; | |
1158 | ||
1159 | if (!socket->m_peer) | |
1160 | { | |
1161 | socket->m_error = GSOCK_INVADDR; | |
1162 | return -1; | |
1163 | } | |
1164 | ||
1165 | err = _GAddress_translate_to(socket->m_peer, &addr, &len); | |
1166 | if (err != GSOCK_NOERROR) | |
1167 | { | |
1168 | socket->m_error = err; | |
1169 | return -1; | |
1170 | } | |
1171 | ||
1172 | ret = sendto(socket->m_endpoint, buffer, size, 0, addr, len); | |
1173 | ||
1174 | /* Frees memory allocated from _GAddress_translate_to */ | |
1175 | free(addr); | |
1176 | #endif | |
1177 | return ret; | |
1178 | } | |
1179 | ||
1180 | ||
1181 | /* | |
1182 | * ------------------------------------------------------------------------- | |
1183 | * GAddress | |
1184 | * ------------------------------------------------------------------------- | |
1185 | */ | |
1186 | ||
1187 | /* CHECK_ADDRESS verifies that the current family is either GSOCK_NOFAMILY | |
1188 | * or GSOCK_*family*, and if it is GSOCK_NOFAMILY, it initalizes address | |
1189 | * to be a GSOCK_*family*. In other cases, it returns GSOCK_INVADDR. | |
1190 | */ | |
1191 | #define CHECK_ADDRESS(address, family, retval) \ | |
1192 | { \ | |
1193 | if (address->m_family == GSOCK_NOFAMILY) \ | |
1194 | if (_GAddress_Init_##family(address) != GSOCK_NOERROR) \ | |
1195 | return address->m_error; \ | |
1196 | if (address->m_family != GSOCK_##family) \ | |
1197 | { \ | |
1198 | address->m_error = GSOCK_INVADDR; \ | |
1199 | return retval; \ | |
1200 | } \ | |
1201 | } | |
1202 | ||
1203 | GAddress *GAddress_new() | |
1204 | { | |
1205 | GAddress *address; | |
1206 | ||
1207 | if ((address = (GAddress *) malloc(sizeof(GAddress))) == NULL) | |
1208 | return NULL; | |
1209 | ||
1210 | address->m_family = GSOCK_NOFAMILY; | |
1211 | address->m_host = INADDR_NONE ; | |
1212 | address->m_port = 0 ; | |
1213 | ||
1214 | return address; | |
1215 | } | |
1216 | ||
1217 | GAddress *GAddress_copy(GAddress *address) | |
1218 | { | |
1219 | GAddress *addr2; | |
1220 | ||
1221 | assert(address != NULL); | |
1222 | ||
1223 | if ((addr2 = (GAddress *) malloc(sizeof(GAddress))) == NULL) | |
1224 | return NULL; | |
1225 | ||
1226 | memcpy(addr2, address, sizeof(GAddress)); | |
1227 | return addr2; | |
1228 | } | |
1229 | ||
1230 | void GAddress_destroy(GAddress *address) | |
1231 | { | |
1232 | assert(address != NULL); | |
1233 | ||
1234 | free(address); | |
1235 | } | |
1236 | ||
1237 | void GAddress_SetFamily(GAddress *address, GAddressType type) | |
1238 | { | |
1239 | assert(address != NULL); | |
1240 | ||
1241 | address->m_family = type; | |
1242 | } | |
1243 | ||
1244 | GAddressType GAddress_GetFamily(GAddress *address) | |
1245 | { | |
1246 | assert(address != NULL); | |
1247 | ||
1248 | return address->m_family; | |
1249 | } | |
1250 | ||
1251 | GSocketError _GAddress_translate_from(GAddress *address, | |
1252 | InetAddress *addr) | |
1253 | { | |
1254 | switch (addr->fAddressType) | |
1255 | { | |
1256 | case AF_INET: | |
1257 | address->m_family = GSOCK_INET; | |
1258 | break; | |
1259 | #ifdef AF_INET6 | |
1260 | case AF_INET6: | |
1261 | address->m_family = GSOCK_INET6; | |
1262 | break; | |
1263 | #endif | |
1264 | default: | |
1265 | { | |
1266 | address->m_error = GSOCK_INVOP; | |
1267 | return GSOCK_INVOP; | |
1268 | } | |
1269 | } | |
1270 | address->m_host = addr->fHost ; | |
1271 | address->m_port = addr->fPort ; | |
1272 | return GSOCK_NOERROR; | |
1273 | } | |
1274 | ||
1275 | GSocketError _GAddress_translate_to(GAddress *address, | |
1276 | InetAddress *addr) | |
1277 | { | |
1278 | if ( GSocket_Verify_Inited() == FALSE ) | |
1279 | return GSOCK_IOERR ; | |
1280 | memset(addr, 0 , sizeof(struct InetAddress)); | |
1281 | OTInitInetAddress( addr , address->m_port , address->m_host ) ; | |
1282 | return GSOCK_NOERROR; | |
1283 | } | |
1284 | ||
1285 | /* | |
1286 | * ------------------------------------------------------------------------- | |
1287 | * Internet address family | |
1288 | * ------------------------------------------------------------------------- | |
1289 | */ | |
1290 | ||
1291 | GSocketError _GAddress_Init_INET(GAddress *address) | |
1292 | { | |
1293 | address->m_family = GSOCK_INET; | |
1294 | address->m_host = kOTAnyInetAddress ; | |
1295 | ||
1296 | return GSOCK_NOERROR; | |
1297 | } | |
1298 | ||
1299 | GSocketError GAddress_INET_SetHostName(GAddress *address, const char *hostname) | |
1300 | { | |
1301 | InetHostInfo hinfo ; | |
1302 | OSStatus ret ; | |
1303 | ||
1304 | if ( GSocket_Verify_Inited() == FALSE ) | |
1305 | return GSOCK_IOERR ; | |
1306 | ||
1307 | assert(address != NULL); | |
1308 | ||
1309 | CHECK_ADDRESS(address, INET, GSOCK_INVADDR); | |
1310 | ret = OTInetStringToAddress( gInetSvcRef , (char*) hostname , &hinfo ) ; | |
1311 | if ( ret != kOTNoError ) | |
1312 | { | |
1313 | address->m_host = INADDR_NONE ; | |
1314 | address->m_error = GSOCK_NOHOST; | |
1315 | return GSOCK_NOHOST; | |
1316 | } | |
1317 | address->m_host = hinfo.addrs[0] ; | |
1318 | return GSOCK_NOERROR; | |
1319 | } | |
1320 | ||
1321 | GSocketError GAddress_INET_SetAnyAddress(GAddress *address) | |
1322 | { | |
1323 | return GAddress_INET_SetHostAddress(address, INADDR_ANY); | |
1324 | } | |
1325 | ||
1326 | GSocketError GAddress_INET_SetHostAddress(GAddress *address, | |
1327 | unsigned long hostaddr) | |
1328 | { | |
1329 | struct in_addr *addr; | |
1330 | ||
1331 | assert(address != NULL); | |
1332 | ||
1333 | CHECK_ADDRESS(address, INET, GSOCK_INVADDR); | |
1334 | ||
1335 | address->m_host = hostaddr ; | |
1336 | ||
1337 | return GSOCK_NOERROR; | |
1338 | } | |
1339 | ||
1340 | struct service_entry | |
1341 | { | |
1342 | char * name ; | |
1343 | unsigned short port ; | |
1344 | char * protocol ; | |
1345 | } ; | |
1346 | typedef struct service_entry service_entry ; | |
1347 | ||
1348 | service_entry gServices[] = | |
1349 | { | |
1350 | { "http" , 80 , "tcp" } | |
1351 | } ; | |
1352 | ||
1353 | GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port, | |
1354 | const char *protocol) | |
1355 | { | |
1356 | InetAddress *addr; | |
1357 | int i ; | |
1358 | ||
1359 | assert(address != NULL); | |
1360 | CHECK_ADDRESS(address, INET, GSOCK_INVADDR); | |
1361 | ||
1362 | if (!port) | |
1363 | { | |
1364 | address->m_error = GSOCK_INVPORT; | |
1365 | return GSOCK_INVPORT; | |
1366 | } | |
1367 | for ( i = 0 ; i < sizeof( gServices) / sizeof( service_entry ) ; ++i ) | |
1368 | { | |
1369 | if ( strcmp( port , gServices[i].name ) == 0 ) | |
1370 | { | |
1371 | if ( protocol == NULL || strcmp( protocol , gServices[i].protocol ) ) | |
1372 | { | |
1373 | address->m_port = gServices[i].port ; | |
1374 | return GSOCK_NOERROR; | |
1375 | } | |
1376 | } | |
1377 | } | |
1378 | ||
1379 | if (isdigit(port[0])) | |
1380 | { | |
1381 | address->m_port = atoi(port); | |
1382 | return GSOCK_NOERROR; | |
1383 | } | |
1384 | ||
1385 | address->m_error = GSOCK_INVPORT; | |
1386 | return GSOCK_INVPORT; | |
1387 | } | |
1388 | ||
1389 | GSocketError GAddress_INET_SetPort(GAddress *address, unsigned short port) | |
1390 | { | |
1391 | InetAddress *addr; | |
1392 | ||
1393 | assert(address != NULL); | |
1394 | CHECK_ADDRESS(address, INET, GSOCK_INVADDR); | |
1395 | address->m_port = port ; | |
1396 | ||
1397 | return GSOCK_NOERROR; | |
1398 | } | |
1399 | ||
1400 | GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf) | |
1401 | { | |
1402 | InetDomainName name ; | |
1403 | if ( GSocket_Verify_Inited() == FALSE ) | |
1404 | return GSOCK_IOERR ; | |
1405 | ||
1406 | assert(address != NULL); | |
1407 | CHECK_ADDRESS(address, INET, GSOCK_INVADDR); | |
1408 | ||
1409 | OTInetAddressToName( gInetSvcRef , address->m_host , name ) ; | |
1410 | strncpy( hostname , name , sbuf ) ; | |
1411 | return GSOCK_NOERROR; | |
1412 | } | |
1413 | ||
1414 | unsigned long GAddress_INET_GetHostAddress(GAddress *address) | |
1415 | { | |
1416 | assert(address != NULL); | |
1417 | CHECK_ADDRESS(address, INET, 0); | |
1418 | ||
1419 | return address->m_host; | |
1420 | } | |
1421 | ||
1422 | unsigned short GAddress_INET_GetPort(GAddress *address) | |
1423 | { | |
1424 | assert(address != NULL); | |
1425 | CHECK_ADDRESS(address, INET, 0); | |
1426 | ||
1427 | return address->m_port; | |
1428 | } | |
1429 | ||
1430 | void _GSocket_Enable_Events(GSocket *socket) | |
1431 | { | |
1432 | if ( socket->m_takesEvents ) | |
1433 | return ; | |
1434 | ||
1435 | { | |
1436 | OTResult state ; | |
1437 | socket->m_takesEvents = TRUE ; | |
1438 | state = OTGetEndpointState(socket->m_endpoint); | |
1439 | ||
1440 | { | |
1441 | OTByteCount sz = 0 ; | |
1442 | OTCountDataBytes( socket->m_endpoint , &sz ) ; | |
1443 | if ( state == T_INCON || sz > 0 ) | |
1444 | { | |
1445 | socket->m_detected |= GSOCK_INPUT_FLAG ; | |
1446 | (socket->m_cbacks[GSOCK_INPUT])(socket, GSOCK_INPUT, socket->m_data[GSOCK_INPUT]); | |
1447 | } | |
1448 | } | |
1449 | { | |
1450 | if ( state == T_DATAXFER || state == T_INREL ) | |
1451 | { | |
1452 | socket->m_detected |=GSOCK_OUTPUT_FLAG ; | |
1453 | (socket->m_cbacks[GSOCK_OUTPUT])(socket, GSOCK_OUTPUT, socket->m_data[GSOCK_OUTPUT]); | |
1454 | } | |
1455 | } | |
1456 | } | |
1457 | } | |
1458 | ||
1459 | void _GSocket_Disable_Events(GSocket *socket) | |
1460 | { | |
1461 | socket->m_takesEvents = FALSE ; | |
1462 | } | |
1463 | ||
1464 | /* _GSocket_Input_Timeout: | |
1465 | * For blocking sockets, wait until data is available or | |
1466 | * until timeout ellapses. | |
1467 | */ | |
1468 | GSocketError _GSocket_Input_Timeout(GSocket *socket) | |
1469 | { | |
1470 | if ( !socket->m_non_blocking ) | |
1471 | { | |
1472 | UnsignedWide now , start ; | |
1473 | short formerTakesEvents = socket->m_takesEvents ; | |
1474 | Microseconds(&start); | |
1475 | now = start ; | |
1476 | socket->m_takesEvents = FALSE ; | |
1477 | ||
1478 | while( (now.hi * 4294967296.0 + now.lo) - (start.hi * 4294967296.0 + start.lo) < socket->m_timeout * 1000.0 ) | |
1479 | { | |
1480 | OTResult state ; | |
1481 | OTByteCount sz = 0 ; | |
1482 | state = OTGetEndpointState(socket->m_endpoint); | |
1483 | ||
1484 | OTCountDataBytes( socket->m_endpoint , &sz ) ; | |
1485 | if ( state == T_INCON || sz > 0 ) | |
1486 | { | |
1487 | socket->m_takesEvents = formerTakesEvents ; | |
1488 | return GSOCK_NOERROR; | |
1489 | } | |
1490 | Microseconds(&now); | |
1491 | } | |
1492 | socket->m_takesEvents = formerTakesEvents ; | |
1493 | socket->m_error = GSOCK_TIMEDOUT; | |
1494 | return GSOCK_TIMEDOUT; | |
1495 | } | |
1496 | return GSOCK_NOERROR; | |
1497 | } | |
1498 | ||
1499 | /* _GSocket_Output_Timeout: | |
1500 | * For blocking sockets, wait until data can be sent without | |
1501 | * blocking or until timeout ellapses. | |
1502 | */ | |
1503 | GSocketError _GSocket_Output_Timeout(GSocket *socket) | |
1504 | { | |
1505 | if ( !socket->m_non_blocking ) | |
1506 | { | |
1507 | UnsignedWide now , start ; | |
1508 | short formerTakesEvents = socket->m_takesEvents ; | |
1509 | Microseconds(&start); | |
1510 | now = start ; | |
1511 | socket->m_takesEvents = FALSE ; | |
1512 | ||
1513 | while( (now.hi * 4294967296.0 + now.lo) - (start.hi * 4294967296.0 + start.lo) < socket->m_timeout * 1000.0 ) | |
1514 | { | |
1515 | OTResult state ; | |
1516 | state = OTGetEndpointState(socket->m_endpoint); | |
1517 | ||
1518 | if ( state == T_DATAXFER || state == T_INREL ) | |
1519 | { | |
1520 | socket->m_takesEvents = formerTakesEvents ; | |
1521 | return GSOCK_NOERROR; | |
1522 | } | |
1523 | Microseconds(&now); | |
1524 | } | |
1525 | socket->m_takesEvents = formerTakesEvents ; | |
1526 | socket->m_error = GSOCK_TIMEDOUT; | |
1527 | return GSOCK_TIMEDOUT; | |
1528 | } | |
1529 | return GSOCK_NOERROR; | |
1530 | } | |
1531 | ||
1532 | /* GSOCK_INPUT: | |
1533 | * There is data to be read in the input buffer. If, after a read | |
1534 | * operation, there is still data available, the callback function will | |
1535 | * be called again. | |
1536 | * GSOCK_OUTPUT: | |
1537 | * The socket is available for writing. That is, the next write call | |
1538 | * won't block. This event is generated only once, when the connection is | |
1539 | * first established, and then only if a call failed with GSOCK_WOULDBLOCK, | |
1540 | * when the output buffer empties again. This means that the app should | |
1541 | * assume that it can write since the first OUTPUT event, and no more | |
1542 | * OUTPUT events will be generated unless an error occurs. | |
1543 | * GSOCK_CONNECTION: | |
1544 | * Connection succesfully established, for client sockets, or incoming | |
1545 | * client connection, for server sockets. Wait for this event (also watch | |
1546 | * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call. | |
1547 | * GSOCK_LOST: | |
1548 | * The connection is lost (or a connection request failed); this could | |
1549 | * be due to a failure, or due to the peer closing it gracefully. | |
1550 | */ | |
1551 | ||
1552 | void _GSocket_Internal_Proc(unsigned long e , void* d ) | |
1553 | { | |
1554 | ||
1555 | GSocket * socket = (GSocket*) d ; | |
1556 | OTEventCode ev = (OTEventCode) e ; | |
1557 | GSocketEvent event; | |
1558 | GSocketEvent event2; | |
1559 | GSocketCallback cback; | |
1560 | char *data; | |
1561 | GSocketCallback cback2; | |
1562 | char *data2; | |
1563 | ||
1564 | if ( !socket ) | |
1565 | return ; | |
1566 | event = GSOCK_MAX_EVENT ; | |
1567 | event2 = GSOCK_MAX_EVENT ; | |
1568 | cback = NULL; | |
1569 | data = NULL; | |
1570 | cback2 = NULL; | |
1571 | data2 = NULL; | |
1572 | ||
1573 | /* Check that the socket still exists (it has not been | |
1574 | * destroyed) and for safety, check that the m_endpoint field | |
1575 | * is what we expect it to be. | |
1576 | */ | |
1577 | if ((socket != NULL) && (socket->m_takesEvents)) | |
1578 | { | |
1579 | switch (ev) | |
1580 | { | |
1581 | case T_LISTEN : | |
1582 | event = GSOCK_CONNECTION ; | |
1583 | break ; | |
1584 | case T_CONNECT : | |
1585 | event = GSOCK_CONNECTION ; | |
1586 | event2 = GSOCK_OUTPUT ; | |
1587 | { | |
1588 | TCall retCall; | |
1589 | ||
1590 | retCall.addr.buf = NULL; | |
1591 | retCall.addr.maxlen = 0; | |
1592 | retCall.opt.buf = NULL; | |
1593 | retCall.opt.maxlen = 0; | |
1594 | retCall.udata.buf = NULL; | |
1595 | retCall.udata.maxlen= 0; | |
1596 | OTRcvConnect( socket->m_endpoint , &retCall ) ; | |
1597 | } | |
1598 | break ; | |
1599 | case T_DISCONNECT : | |
1600 | event = GSOCK_LOST ; | |
1601 | break ; | |
1602 | case T_GODATA : | |
1603 | case T_GOEXDATA : | |
1604 | event = GSOCK_OUTPUT ; | |
1605 | break ; | |
1606 | case T_DATA : | |
1607 | event = GSOCK_INPUT ; | |
1608 | break ; | |
1609 | case T_EXDATA : | |
1610 | event = GSOCK_INPUT ; | |
1611 | break ; | |
1612 | } | |
1613 | if (event != GSOCK_MAX_EVENT) | |
1614 | { | |
1615 | cback = socket->m_cbacks[event]; | |
1616 | data = socket->m_data[event]; | |
1617 | ||
1618 | if (event == GSOCK_LOST) | |
1619 | socket->m_detected = GSOCK_LOST_FLAG; | |
1620 | else | |
1621 | socket->m_detected |= (1 << event); | |
1622 | } | |
1623 | if (event2 != GSOCK_MAX_EVENT) | |
1624 | { | |
1625 | cback2 = socket->m_cbacks[event2]; | |
1626 | data2 = socket->m_data[event2]; | |
1627 | ||
1628 | if (event2 == GSOCK_LOST) | |
1629 | socket->m_detected = GSOCK_LOST_FLAG; | |
1630 | else | |
1631 | socket->m_detected |= (1 << event2); | |
1632 | } | |
1633 | } | |
1634 | ||
1635 | /* OK, we can now leave the critical section because we have | |
1636 | * already obtained the callback address (we make no further | |
1637 | * accesses to socket->whatever). However, the app should | |
1638 | * be prepared to handle events from a socket that has just | |
1639 | * been closed! | |
1640 | */ | |
1641 | ||
1642 | if (cback != NULL) | |
1643 | (cback)(socket, event, data); | |
1644 | if (cback2 != NULL) | |
1645 | (cback2)(socket, event2, data2); | |
1646 | ||
1647 | } | |
1648 | ||
1649 | /* Hack added for Mac OS X */ | |
1650 | GSocketError GAddress_UNIX_GetPath(GAddress *addr, char *path, size_t buf) | |
1651 | { | |
1652 | } | |
1653 | ||
1654 | GSocketError GAddress_UNIX_SetPath(GAddress *addr, const char *path) | |
1655 | { | |
1656 | } | |
1657 | ||
1658 | #endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */ |