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