]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/gsocket.cpp
fixing mask color correction
[wxWidgets.git] / src / mac / carbon / gsocket.cpp
CommitLineData
e6f2ec14 1/* -------------------------------------------------------------------------
99d80019
JS
2 * Project: GSocket (Generic Socket) for WX
3 * Name: gsocket.cpp
4 * Copyright: (c) Guilhem Lavaux
5 * Licence: wxWindows Licence
6 * Authors: Guilhem Lavaux,
7 * Guillermo Rodriguez Garcia <guille@iies.es> (maintainer)
8 * Stefan CSomor
9 * Purpose: GSocket main mac file
10 * CVSID: $Id$
e6f2ec14
DE
11 * -------------------------------------------------------------------------
12 */
13
14/*
15 * PLEASE don't put C++ comments here - this is a C source file.
16 */
17
18#ifndef __GSOCKET_STANDALONE__
19#include "wx/setup.h"
20#include "wx/platform.h"
21#endif
22
23#if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
24
25#ifdef __DARWIN__
26 #include <CoreServices/CoreServices.h>
27
e6f2ec14
DE
28#else
29 #include <MacHeaders.c>
30 #define OTUNIXERRORS 1
31 #include <OpenTransport.h>
32 #include <OpenTransportProviders.h>
33 #include <OpenTptInternet.h>
34#endif
35#if TARGET_CARBON && !defined(OTAssert)
36 #define OTAssert( str , cond ) /* does not exists in Carbon */
37#endif
38
39#include <assert.h>
40#include <errno.h>
41#include <string.h>
42#include <unistd.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <stddef.h>
46#include <ctype.h>
47#include <utime.h>
48
49/*
50 * INADDR_BROADCAST is identical to INADDR_NONE which is not defined
51 * on all unices. INADDR_BROADCAST should be fine to indicate an error.
52 */
53#ifndef INADDR_BROADCAST
54#define INADDR_BROADCAST 0xFFFFFFFFUL
55#endif
56#ifndef INADDR_NONE
57#define INADDR_NONE INADDR_BROADCAST
58#endif
59#ifndef INADDR_ANY
60#define INADDR_ANY 0x0UL
61#endif
62#ifndef __GSOCKET_STANDALONE__
63
64#include "wx/mac/macnotfy.h"
65#include "wx/mac/gsockmac.h"
66#include "wx/gsocket.h"
67
68#else
69
70#include "gsockmac.h"
71#include "gsocket.h"
72
73#endif /* __GSOCKET_STANDALONE__ */
74
75#ifndef ntohl
76#define ntohl(x) (x)
77#define ntohs(x) (x)
78#define htonl(x) (x)
79#define htons(x) (x)
80#endif
81
82void wxCYield() ;
83#ifdef __WXDEBUG__
84#define qDebug 1
85#define qDebug2 1
86extern pascal void OTDebugStr(const char* str);
87#endif
88#ifndef __DARWIN__
89 #include <OTDebug.h>
90#endif
91InetSvcRef gInetSvcRef = 0 ;
92int gOTInited = 0 ;
93OTNotifyUPP gOTNotifierUPP = NULL ;
94
95OSStatus DoNegotiateIPReuseAddrOption(EndpointRef ep, Boolean enableReuseIPMode);
96
97/* Input: ep - endpointref on which to negotiate the option
98 enableReuseIPMode - desired option setting - true/false
99 Return: kOTNoError indicates that the option was successfully negotiated
100 OSStatus is an error if < 0, otherwise, the status field is
101 returned and is > 0.
102
103 IMPORTANT NOTE: The endpoint is assumed to be in synchronous more, otherwise
104 this code will not function as desired
105*/
106
107
108OSStatus DoNegotiateIPReuseAddrOption(EndpointRef ep, Boolean enableReuseIPMode)
109
110{
111 UInt8 buf[kOTFourByteOptionSize]; // define buffer for fourByte Option size
112 TOption* opt; // option ptr to make items easier to access
113 TOptMgmt req;
114 TOptMgmt ret;
115 OSStatus err;
116
117 if (!OTIsSynchronous(ep))
118 {
119 return (-1);
120 }
121 opt = (TOption*)buf; // set option ptr to buffer
122 req.opt.buf = buf;
123 req.opt.len = sizeof(buf);
124 req.flags = T_NEGOTIATE; // negotiate for option
125
126 ret.opt.buf = buf;
127 ret.opt.maxlen = kOTFourByteOptionSize;
128
129 opt->level = INET_IP; // dealing with an IP Level function
130#ifdef __DARWIN__
131 opt->name = kIP_REUSEADDR;
132#else
133 opt->name = IP_REUSEADDR;
134#endif
135 opt->len = kOTFourByteOptionSize;
136 opt->status = 0;
137 *(UInt32*)opt->value = enableReuseIPMode; // set the desired option level, true or false
138
139 err = OTOptionManagement(ep, &req, &ret);
140
141 // if no error then return the option status value
142 if (err == kOTNoError)
143 {
144 if (opt->status != T_SUCCESS)
145 err = opt->status;
146 else
147 err = kOTNoError;
148 }
149
150 return err;
151}
152
153
154pascal void OTInetEventHandler(void*s, OTEventCode event, OTResult, void *cookie) ;
155pascal void OTInetEventHandler(void*s, OTEventCode event, OTResult result, void *cookie)
156{
157 int wakeUp = true ;
158 GSocket* sock = (GSocket*) s ;
159
160 if ( event == kOTSyncIdleEvent )
161 {
162 return ;
163 }
164
165 if ( s )
166 {
167 wxMacAddEvent( sock->m_mac_events , _GSocket_Internal_Proc , event , s , wakeUp ) ;
168 }
169
170 return;
171}
172
173static void SetDefaultEndpointModes(EndpointRef ep , void *data )
174 // This routine sets the supplied endpoint into the default
175 // mode used in this application. The specifics are:
176 // blocking, synchronous, and using synch idle events with
177 // the standard YieldingNotifier.
178{
179 OSStatus junk = kOTNoError ;
180 OTAssert ("SetDefaultEndpointModes:invalid ref", ep != kOTInvalidEndpointRef ) ;
181 junk = OTSetAsynchronous(ep);
182 OTAssert("SetDefaultEndpointModes: Could not set asynchronous", junk == noErr);
183/*
184 junk = OTSetBlocking(ep);
185 OTAssert("SetDefaultEndpointModes: Could not set blocking", junk == noErr);
186 junk = OTSetSynchronous(ep);
187 OTAssert("SetDefaultEndpointModes: Could not set synchronous", junk == noErr);
188 junk = OTSetBlocking(ep);
189 OTAssert("SetDefaultEndpointModes: Could not set blocking", junk == noErr);
190*/
191 junk = OTInstallNotifier(ep, gOTNotifierUPP, data);
192 OTAssert("SetDefaultEndpointModes: Could not install notifier", junk == noErr);
193/*
194 junk = OTUseSyncIdleEvents(ep, true);
195 OTAssert("SetDefaultEndpointModes: Could not use sync idle events", junk == noErr);
196*/
197}
198
199/* Global initialisers */
200
3e1400ac 201void GSocket_SetGUIFunctions(GSocketGUIFunctionsTable *table)
e6f2ec14
DE
202{
203 // do nothing, wxMac doesn't have wxBase-GUI separation yet
204}
205
206int GSocket_Init()
207{
948c96ef 208 return 1;
e6f2ec14
DE
209}
210
948c96ef
DE
211bool GSocket_Verify_Inited() ;
212bool GSocket_Verify_Inited()
e6f2ec14
DE
213{
214 OSStatus err ;
215#if TARGET_CARBON
216 // Marc Newsam: added the clientcontext variable
217 // however, documentation is unclear how this works
218 OTClientContextPtr clientcontext;
219
220 if ( gInetSvcRef )
948c96ef 221 return true ;
e6f2ec14
DE
222
223 InitOpenTransportInContext(kInitOTForApplicationMask, &clientcontext);
224 gOTInited = 1 ;
225 gInetSvcRef = OTOpenInternetServicesInContext(kDefaultInternetServicesPath,
226 NULL, &err, clientcontext);
227#else
228 if ( gInetSvcRef )
948c96ef 229 return true ;
e6f2ec14
DE
230
231 InitOpenTransport() ;
232 gOTInited = 1 ;
233 gInetSvcRef = OTOpenInternetServices(kDefaultInternetServicesPath, NULL, &err);
234#endif
235 if ( gInetSvcRef == NULL || err != kOTNoError )
236 {
237 OTAssert("Could not open Inet Services", err == noErr);
948c96ef 238 return false ;
e6f2ec14
DE
239 }
240 gOTNotifierUPP = NewOTNotifyUPP( OTInetEventHandler ) ;
948c96ef 241 return true ;
e6f2ec14
DE
242}
243
244void GSocket_Cleanup()
245{
246 if ( gOTInited != 0 )
247 {
248 if ( gInetSvcRef != NULL )
249 OTCloseProvider( gInetSvcRef );
250 #if TARGET_CARBON
251 CloseOpenTransportInContext( NULL ) ;
252 #else
253 CloseOpenTransport() ;
254 #endif
255 if ( gOTNotifierUPP )
256 DisposeOTNotifyUPP( gOTNotifierUPP ) ;
257 }
258}
259
260/* Constructors / Destructors for GSocket */
261
c5602b4a 262GSocket::GSocket()
e6f2ec14 263{
e6f2ec14 264 int i;
e6f2ec14 265
948c96ef 266 m_ok = GSocket_Verify_Inited();
e6f2ec14 267
c5602b4a 268 m_endpoint = NULL ;
e6f2ec14
DE
269 for (i=0;i<GSOCK_MAX_EVENT;i++)
270 {
c5602b4a 271 m_cbacks[i] = NULL;
e6f2ec14 272 }
c5602b4a
DE
273 m_detected = 0;
274 m_local = NULL;
275 m_peer = NULL;
276 m_error = GSOCK_NOERROR;
948c96ef
DE
277 m_server = false;
278 m_stream = true;
279 m_non_blocking = false;
c5602b4a 280 m_timeout = 1*1000;
e6f2ec14 281 /* 10 sec * 1000 millisec */
948c96ef 282 m_takesEvents = true ;
c5602b4a 283 m_mac_events = wxMacGetNotifierTable() ;
e6f2ec14
DE
284}
285
c5602b4a 286GSocket::~GSocket()
e6f2ec14 287{
c5602b4a 288 assert(this);
e6f2ec14
DE
289
290 /* Check that the socket is really shutdowned */
c5602b4a
DE
291 if (m_endpoint != kOTInvalidEndpointRef)
292 Shutdown();
e6f2ec14
DE
293
294
295 /* Destroy private addresses */
c5602b4a
DE
296 if (m_local)
297 GAddress_destroy(m_local);
e6f2ec14 298
c5602b4a
DE
299 if (m_peer)
300 GAddress_destroy(m_peer);
e6f2ec14
DE
301}
302
303/* GSocket_Shutdown:
304 * Disallow further read/write operations on this socket, close
305 * the fd and disable all callbacks.
306 */
c5602b4a 307void GSocket::Shutdown()
e6f2ec14
DE
308{
309 OSStatus err ;
310 int evt;
311
c5602b4a 312 assert(this);
e6f2ec14
DE
313
314 /* If socket has been created, shutdown it */
c5602b4a 315 if (m_endpoint != kOTInvalidEndpointRef )
e6f2ec14 316 {
c5602b4a 317 err = OTSndOrderlyDisconnect( m_endpoint ) ;
e6f2ec14
DE
318 if ( err != kOTNoError )
319 {
320
321 }
c5602b4a
DE
322 err = OTRcvOrderlyDisconnect( m_endpoint ) ;
323 err = OTUnbind( m_endpoint ) ;
324 err = OTCloseProvider( m_endpoint ) ;
325 m_endpoint = kOTInvalidEndpointRef ;
e6f2ec14
DE
326 }
327
328 /* Disable GUI callbacks */
329 for (evt = 0; evt < GSOCK_MAX_EVENT; evt++)
c5602b4a 330 m_cbacks[evt] = NULL;
e6f2ec14 331
c5602b4a
DE
332 m_detected = 0;
333 Disable_Events();
334 wxMacRemoveAllNotifiersForData( wxMacGetNotifierTable() , this ) ;
e6f2ec14
DE
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 */
c5602b4a 354GSocketError GSocket::SetLocal(GAddress *address)
e6f2ec14 355{
c5602b4a 356 assert(this);
e6f2ec14
DE
357
358 /* the socket must be initialized, or it must be a server */
c5602b4a 359 if ((m_endpoint != kOTInvalidEndpointRef && !m_server))
e6f2ec14 360 {
c5602b4a 361 m_error = GSOCK_INVSOCK;
e6f2ec14
DE
362 return GSOCK_INVSOCK;
363 }
364
365 /* check address */
366 if (address == NULL || address->m_family == GSOCK_NOFAMILY)
367 {
c5602b4a 368 m_error = GSOCK_INVADDR;
e6f2ec14
DE
369 return GSOCK_INVADDR;
370 }
371
c5602b4a
DE
372 if (m_local)
373 GAddress_destroy(m_local);
e6f2ec14 374
c5602b4a 375 m_local = GAddress_copy(address);
e6f2ec14
DE
376
377 return GSOCK_NOERROR;
378}
379
c5602b4a 380GSocketError GSocket::SetPeer(GAddress *address)
e6f2ec14 381{
c5602b4a 382 assert(this);
e6f2ec14
DE
383
384 /* check address */
385 if (address == NULL || address->m_family == GSOCK_NOFAMILY)
386 {
c5602b4a 387 m_error = GSOCK_INVADDR;
e6f2ec14
DE
388 return GSOCK_INVADDR;
389 }
390
c5602b4a
DE
391 if (m_peer)
392 GAddress_destroy(m_peer);
e6f2ec14 393
c5602b4a 394 m_peer = GAddress_copy(address);
e6f2ec14
DE
395
396 return GSOCK_NOERROR;
397}
398
c5602b4a 399GAddress *GSocket::GetLocal()
e6f2ec14
DE
400{
401 GAddress *address = NULL ;
402 GSocketError err;
403 InetAddress loc ;
404
c5602b4a 405 assert(this);
e6f2ec14
DE
406
407 /* try to get it from the m_local var first */
c5602b4a
DE
408 if (m_local)
409 return GAddress_copy(m_local);
e6f2ec14
DE
410
411 /* else, if the socket is initialized, try getsockname */
c5602b4a 412 if (m_endpoint == kOTInvalidEndpointRef)
e6f2ec14 413 {
c5602b4a 414 m_error = GSOCK_INVSOCK;
e6f2ec14
DE
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 {
c5602b4a 435 m_error = GSOCK_MEMERR;
e6f2ec14
DE
436 return NULL;
437 }
438
439 err = _GAddress_translate_from(address, &loc);
440 if (err != GSOCK_NOERROR)
441 {
442 GAddress_destroy(address);
c5602b4a 443 m_error = err;
e6f2ec14
DE
444 return NULL;
445 }
446
447 return address;
448}
449
c5602b4a 450GAddress *GSocket::GetPeer()
e6f2ec14 451{
c5602b4a 452 assert(this);
e6f2ec14
DE
453
454 /* try to get it from the m_peer var */
c5602b4a
DE
455 if (m_peer)
456 return GAddress_copy(m_peer);
e6f2ec14
DE
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 */
c5602b4a 473GSocketError GSocket::SetServer()
e6f2ec14 474{
c5602b4a 475 assert(this);
e6f2ec14
DE
476
477 /* must not be in use */
c5602b4a 478 if (m_endpoint != kOTInvalidEndpointRef )
e6f2ec14 479 {
c5602b4a 480 m_error = GSOCK_INVSOCK;
e6f2ec14
DE
481 return GSOCK_INVSOCK;
482 }
483
484 /* the local addr must have been set */
c5602b4a 485 if (!m_local)
e6f2ec14 486 {
c5602b4a 487 m_error = GSOCK_INVADDR;
e6f2ec14
DE
488 return GSOCK_INVADDR;
489 }
490
491 /* Initialize all fields */
948c96ef
DE
492 m_stream = true;
493 m_server = true;
494 m_oriented = true;
e6f2ec14
DE
495
496// TODO
497#if 0
498 /* Create the socket */
c5602b4a
DE
499 m_endpoint = socket(m_local->m_realfamily, SOCK_STREAM, 0);
500 socket_set_ref( m_endpoint , (unsigned long) &gMacNetEvents , (unsigned long) this ) ;
501 if (m_endpoint == kOTInvalidEndpointRef)
e6f2ec14 502 {
c5602b4a 503 m_error = GSOCK_IOERR;
e6f2ec14
DE
504 return GSOCK_IOERR;
505 }
506
c5602b4a
DE
507 ioctl(m_endpoint, FIONBIO, &arg);
508 Enable_Events();
e6f2ec14
DE
509
510 /* Bind to the local address,
511 * retrieve the actual address bound,
512 * and listen up to 5 connections.
513 */
c5602b4a
DE
514 if ((bind(m_endpoint, m_local->m_addr, m_local->m_len) != 0) ||
515 (getsockname(m_endpoint,
516 m_local->m_addr,
517 (SOCKLEN_T *) &m_local->m_len) != 0) ||
518 (listen(m_endpoint, 5) != 0))
e6f2ec14 519 {
c5602b4a
DE
520 close(m_endpoint);
521 m_endpoint = -1;
522 m_error = GSOCK_IOERR;
e6f2ec14
DE
523 return GSOCK_IOERR;
524 }
525#endif
526 return GSOCK_NOERROR;
527}
528
529/* GSocket_WaitConnection:
530 * Waits for an incoming client connection. Returns a pointer to
531 * a GSocket object, or NULL if there was an error, in which case
532 * the last error field will be updated for the calling GSocket.
533 *
534 * Error codes (set in the calling GSocket)
535 * GSOCK_INVSOCK - the socket is not valid or not a server.
536 * GSOCK_TIMEDOUT - timeout, no incoming connections.
537 * GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
538 * GSOCK_MEMERR - couldn't allocate memory.
539 * GSOCK_IOERR - low-level error.
540 */
c5602b4a 541GSocket *GSocket::WaitConnection()
e6f2ec14
DE
542{
543 GSocket *connection = NULL ;
544
c5602b4a 545 assert(this);
e6f2ec14
DE
546
547 /* Reenable CONNECTION events */
c5602b4a 548 m_detected &= ~GSOCK_CONNECTION_FLAG;
e6f2ec14
DE
549
550 /* If the socket has already been created, we exit immediately */
c5602b4a 551 if (m_endpoint == kOTInvalidEndpointRef || !m_server)
e6f2ec14 552 {
c5602b4a 553 m_error = GSOCK_INVSOCK;
e6f2ec14
DE
554 return NULL;
555 }
556
557 /* Create a GSocket object for the new connection */
558 connection = GSocket_new();
559
560 if (!connection)
561 {
c5602b4a 562 m_error = GSOCK_MEMERR;
e6f2ec14
DE
563 return NULL;
564 }
565
566 /* Wait for a connection (with timeout) */
c5602b4a 567 if (Input_Timeout() == GSOCK_TIMEDOUT)
e6f2ec14 568 {
c5602b4a
DE
569 delete connection;
570 /* m_error set by _GSocket_Input_Timeout */
e6f2ec14
DE
571 return NULL;
572 }
573
574// TODO
575#if 0
c5602b4a 576 connection->m_endpoint = accept(m_endpoint, &from, (SOCKLEN_T *) &fromlen);
e6f2ec14
DE
577#endif
578
579 if (connection->m_endpoint == kOTInvalidEndpointRef )
580 {
581 if (errno == EWOULDBLOCK)
c5602b4a 582 m_error = GSOCK_WOULDBLOCK;
e6f2ec14 583 else
c5602b4a 584 m_error = GSOCK_IOERR;
e6f2ec14 585
c5602b4a 586 delete connection;
e6f2ec14
DE
587 return NULL;
588 }
589
590 /* Initialize all fields */
948c96ef
DE
591 connection->m_server = false;
592 connection->m_stream = true;
593 connection->m_oriented = true;
e6f2ec14
DE
594
595 /* Setup the peer address field */
596 connection->m_peer = GAddress_new();
597 if (!connection->m_peer)
598 {
c5602b4a
DE
599 delete connection;
600 m_error = GSOCK_MEMERR;
e6f2ec14
DE
601 return NULL;
602 }
603 // TODO
604 #if 0
605 err = _GAddress_translate_from(connection->m_peer, &from, fromlen);
606 if (err != GSOCK_NOERROR)
607 {
608 GAddress_destroy(connection->m_peer);
609 GSocket_destroy(connection);
c5602b4a 610 m_error = err;
e6f2ec14
DE
611 return NULL;
612 }
613
614 ioctl(connection->m_endpoint, FIONBIO, &arg);
615#endif
c5602b4a 616 connection->Enable_Events();
e6f2ec14
DE
617
618 return connection;
619}
620
621/* Datagram sockets */
622
623/* GSocket_SetNonOriented:
624 * Sets up this socket as a non-connection oriented (datagram) socket.
625 * Before using this function, the local address must have been set
626 * with GSocket_SetLocal(), or the call will fail. Returns GSOCK_NOERROR
627 * on success, or one of the following otherwise.
628 *
629 * Error codes:
630 * GSOCK_INVSOCK - the socket is in use.
631 * GSOCK_INVADDR - the local address has not been set.
632 * GSOCK_IOERR - low-level error.
633 */
c5602b4a 634GSocketError GSocket::SetNonOriented()
e6f2ec14 635{
c5602b4a 636 assert(this);
e6f2ec14 637
c5602b4a 638 if (m_endpoint != kOTInvalidEndpointRef )
e6f2ec14 639 {
c5602b4a 640 m_error = GSOCK_INVSOCK;
e6f2ec14
DE
641 return GSOCK_INVSOCK;
642 }
643
c5602b4a 644 if (!m_local)
e6f2ec14 645 {
c5602b4a 646 m_error = GSOCK_INVADDR;
e6f2ec14
DE
647 return GSOCK_INVADDR;
648 }
649
650 /* Initialize all fields */
948c96ef
DE
651 m_stream = false;
652 m_server = false;
653 m_oriented = false;
e6f2ec14
DE
654
655 /* Create the socket */
656
657// TODO
658#if 0
c5602b4a
DE
659 m_endpoint = socket(m_local->m_realfamily, SOCK_DGRAM, 0);
660 socket_set_ref( m_endpoint , (unsigned long) &gMacNetEvents , (unsigned long) this ) ;
e6f2ec14 661#endif
c5602b4a 662 if (m_endpoint == kOTInvalidEndpointRef )
e6f2ec14 663 {
c5602b4a 664 m_error = GSOCK_IOERR;
e6f2ec14
DE
665 return GSOCK_IOERR;
666 }
667
668// TODO
669#if 0
c5602b4a 670 ioctl(m_endpoint, FIONBIO, &arg);
e6f2ec14 671#endif
c5602b4a 672 Enable_Events();
e6f2ec14
DE
673
674 /* Bind to the local address,
675 * and retrieve the actual address bound.
676 */
677// TODO
678#if 0
c5602b4a
DE
679 if ((bind(m_endpoint, m_local->m_addr, m_local->m_len) != 0) ||
680 (getsockname(m_endpoint,
681 m_local->m_addr,
682 (SOCKLEN_T *) &m_local->m_len) != 0))
e6f2ec14 683 {
c5602b4a
DE
684 close(m_endpoint);
685 m_endpoint = -1;
686 m_error = GSOCK_IOERR;
e6f2ec14
DE
687 return GSOCK_IOERR;
688 }
689#endif
690 return GSOCK_NOERROR;
691}
692
693/* Client specific parts */
694
695/* GSocket_Connect:
696 * For stream (connection oriented) sockets, GSocket_Connect() tries
697 * to establish a client connection to a server using the peer address
698 * as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
699 * connection has been succesfully established, or one of the error
700 * codes listed below. Note that for nonblocking sockets, a return
701 * value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
702 * request can be completed later; you should use GSocket_Select()
703 * to poll for GSOCK_CONNECTION | GSOCK_LOST, or wait for the
704 * corresponding asynchronous events.
705 *
706 * For datagram (non connection oriented) sockets, GSocket_Connect()
707 * just sets the peer address established with GSocket_SetPeer() as
708 * default destination.
709 *
710 * Error codes:
711 * GSOCK_INVSOCK - the socket is in use or not valid.
712 * GSOCK_INVADDR - the peer address has not been established.
713 * GSOCK_TIMEDOUT - timeout, the connection failed.
714 * GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
715 * GSOCK_MEMERR - couldn't allocate memory.
716 * GSOCK_IOERR - low-level error.
717 */
c5602b4a 718GSocketError GSocket::Connect(GSocketStream stream)
e6f2ec14
DE
719{
720 InetAddress addr ;
721 TEndpointInfo info;
722 OSStatus err = kOTNoError;
723 TCall peer ;
724
c5602b4a 725 assert(this);
e6f2ec14
DE
726
727 /* Enable CONNECTION events (needed for nonblocking connections) */
c5602b4a 728 m_detected &= ~GSOCK_CONNECTION_FLAG;
e6f2ec14 729
c5602b4a 730 if (m_endpoint != kOTInvalidEndpointRef )
e6f2ec14 731 {
c5602b4a 732 m_error = GSOCK_INVSOCK;
e6f2ec14
DE
733 return GSOCK_INVSOCK;
734 }
735
c5602b4a 736 if (!m_peer)
e6f2ec14 737 {
c5602b4a 738 m_error = GSOCK_INVADDR;
e6f2ec14
DE
739 return GSOCK_INVADDR;
740 }
741
742 /* Streamed or dgram socket? */
c5602b4a 743 m_stream = (stream == GSOCK_STREAMED);
948c96ef
DE
744 m_oriented = true;
745 m_server = false;
e6f2ec14
DE
746
747 /* Create the socket */
748#if TARGET_CARBON
c5602b4a 749 m_endpoint =
e6f2ec14
DE
750 OTOpenEndpointInContext( OTCreateConfiguration( kTCPName) , 0 , &info , &err , NULL ) ;
751#else
c5602b4a 752 m_endpoint =
e6f2ec14
DE
753 OTOpenEndpoint( OTCreateConfiguration( kTCPName) , 0 , &info , &err ) ;
754#endif
c5602b4a 755 if ( m_endpoint == kOTInvalidEndpointRef || err != kOTNoError )
e6f2ec14 756 {
c5602b4a
DE
757 m_endpoint = kOTInvalidEndpointRef ;
758 m_error = GSOCK_IOERR;
e6f2ec14
DE
759 return GSOCK_IOERR;
760 }
c5602b4a 761 err = OTBind( m_endpoint , nil , nil ) ;
e6f2ec14
DE
762 if ( err != kOTNoError )
763 {
764 return GSOCK_IOERR;
765 }
c5602b4a 766 SetDefaultEndpointModes( m_endpoint , this ) ;
e6f2ec14
DE
767// TODO
768#if 0
c5602b4a 769 ioctl(m_endpoint, FIONBIO, &arg);
e6f2ec14 770#endif
c5602b4a 771 Enable_Events();
e6f2ec14 772
c5602b4a 773 _GAddress_translate_to( m_peer , &addr ) ;
e6f2ec14
DE
774 memset( &peer , 0 , sizeof( TCall ) ) ;
775 peer.addr.len = sizeof( InetAddress ) ;
776 peer.addr.buf = (unsigned char*) &addr ;
c5602b4a 777 err = OTConnect( m_endpoint , &peer , nil ) ;
e6f2ec14
DE
778 if ( err != noErr )
779 {
780 /* If connect failed with EINPROGRESS and the GSocket object
781 * is in blocking mode, we select() for the specified timeout
782 * checking for writability to see if the connection request
783 * completes.
784 */
785
c5602b4a 786 if ((err == kOTNoDataErr ) && (!m_non_blocking))
e6f2ec14 787 {
c5602b4a 788 if (Output_Timeout() == GSOCK_TIMEDOUT)
e6f2ec14 789 {
c5602b4a
DE
790 OTSndOrderlyDisconnect( m_endpoint ) ;
791 m_endpoint = kOTInvalidEndpointRef ;
792 /* m_error is set in _GSocket_Output_Timeout */
e6f2ec14
DE
793 return GSOCK_TIMEDOUT;
794 }
795 else
796 {
797/*
798 int error;
799 SOCKLEN_T len = sizeof(error);
800
c5602b4a 801 getsockopt(m_endpoint, SOL_SOCKET, SO_ERROR, (void*) &error, &len);
e6f2ec14
DE
802
803 if (!error)
804*/
805 return GSOCK_NOERROR;
806 }
807 }
808
809 /* If connect failed with EINPROGRESS and the GSocket object
810 * is set to nonblocking, we set m_error to GSOCK_WOULDBLOCK
811 * (and return GSOCK_WOULDBLOCK) but we don't close the socket;
812 * this way if the connection completes, a GSOCK_CONNECTION
813 * event will be generated, if enabled.
814 */
c5602b4a 815 if ((err == kOTNoDataErr) && (m_non_blocking))
e6f2ec14 816 {
c5602b4a 817 m_error = GSOCK_WOULDBLOCK;
e6f2ec14
DE
818 return GSOCK_WOULDBLOCK;
819 }
820
821 /* If connect failed with an error other than EINPROGRESS,
822 * then the call to GSocket_Connect has failed.
823 */
c5602b4a 824 OTSndOrderlyDisconnect( m_endpoint ) ;
e6f2ec14 825
c5602b4a
DE
826 m_endpoint = kOTInvalidEndpointRef ;
827 m_error = GSOCK_IOERR;
e6f2ec14
DE
828 return GSOCK_IOERR;
829 }
c5602b4a 830// OTInetEventHandler(this, T_CONNECT , kOTNoError , NULL ) ;
e6f2ec14
DE
831 return GSOCK_NOERROR;
832}
833
834/* Generic IO */
835
836/* Like recv(), send(), ... */
c5602b4a 837int GSocket::Read(char *buffer, int size)
e6f2ec14
DE
838{
839 int ret = 0 ;
840
c5602b4a 841 assert(this);
e6f2ec14
DE
842
843 /* Reenable INPUT events */
c5602b4a 844 m_detected &= ~GSOCK_INPUT_FLAG;
e6f2ec14 845
c5602b4a 846 if (m_endpoint == kOTInvalidEndpointRef || m_server)
e6f2ec14 847 {
c5602b4a 848 m_error = GSOCK_INVSOCK;
e6f2ec14
DE
849 return -1;
850 }
851
852 /* If the socket is blocking, wait for data (with a timeout) */
c5602b4a 853 if (Input_Timeout() == GSOCK_TIMEDOUT)
e6f2ec14
DE
854 return -1;
855
856 /* Read the data */
c5602b4a
DE
857 if (m_stream)
858 ret = Recv_Stream(buffer, size);
e6f2ec14 859 else
c5602b4a 860 ret = Recv_Dgram(buffer, size);
e6f2ec14
DE
861
862 if (ret == -1)
863 {
864 if (errno == EWOULDBLOCK)
c5602b4a 865 m_error = GSOCK_WOULDBLOCK;
e6f2ec14 866 else
c5602b4a 867 m_error = GSOCK_IOERR;
e6f2ec14
DE
868 }
869
870 return ret;
871}
872
c5602b4a 873int GSocket::Write(const char *buffer, int size)
e6f2ec14
DE
874{
875 int ret;
876
c5602b4a 877 assert(this);
e6f2ec14 878
c5602b4a 879 if (m_endpoint == kOTInvalidEndpointRef || m_server)
e6f2ec14 880 {
c5602b4a 881 m_error = GSOCK_INVSOCK;
e6f2ec14
DE
882 return -1;
883 }
884
885 /* If the socket is blocking, wait for writability (with a timeout) */
c5602b4a 886 if (Output_Timeout() == GSOCK_TIMEDOUT)
e6f2ec14
DE
887 return -1;
888
889 /* Write the data */
c5602b4a
DE
890 if (m_stream)
891 ret = Send_Stream(buffer, size);
e6f2ec14 892 else
c5602b4a 893 ret = Send_Dgram(buffer, size);
e6f2ec14
DE
894
895 if (ret == -1)
896 {
897 if (errno == EWOULDBLOCK)
c5602b4a 898 m_error = GSOCK_WOULDBLOCK;
e6f2ec14 899 else
c5602b4a 900 m_error = GSOCK_IOERR;
e6f2ec14
DE
901
902 /* Only reenable OUTPUT events after an error (just like WSAAsyncSelect
903 * in MSW). Once the first OUTPUT event is received, users can assume
904 * that the socket is writable until a read operation fails. Only then
905 * will further OUTPUT events be posted.
906 */
c5602b4a 907 m_detected &= ~GSOCK_OUTPUT_FLAG;
e6f2ec14
DE
908 return -1;
909 }
910
911 return ret;
912}
913
914/* GSocket_Select:
915 * Polls the socket to determine its status. This function will
916 * check for the events specified in the 'flags' parameter, and
917 * it will return a mask indicating which operations can be
918 * performed. This function won't block, regardless of the
919 * mode (blocking | nonblocking) of the socket.
920 */
c5602b4a 921GSocketEventFlags GSocket::Select(GSocketEventFlags flags)
e6f2ec14 922{
c5602b4a 923 assert(this);
e6f2ec14
DE
924 wxMacProcessNotifierEvents() ;
925 /*
c5602b4a 926 state = OTGetEndpointState(m_endpoint);
e6f2ec14 927
c5602b4a 928 if ( ( flags & GSOCK_INPUT_FLAG ) && ! ( m_detected & GSOCK_INPUT_FLAG ) )
e6f2ec14
DE
929 {
930 size_t sz = 0 ;
c5602b4a 931 OTCountDataBytes( m_endpoint , &sz ) ;
e6f2ec14
DE
932 if ( state == T_INCON || sz > 0 )
933 {
c5602b4a
DE
934 m_detected |= GSOCK_INPUT_FLAG ;
935 (m_cbacks[GSOCK_INPUT])(this, GSOCK_INPUT, m_data[GSOCK_INPUT]);
e6f2ec14
DE
936 }
937 }
c5602b4a 938 if ( ( flags & GSOCK_INPUT_FLAG ) && ! ( m_detected & GSOCK_OUTPUT_FLAG ) )
e6f2ec14
DE
939 {
940 if ( state == T_DATAXFER || state == T_INREL )
941 {
c5602b4a
DE
942 m_detected |=GSOCK_OUTPUT_FLAG ;
943 (m_cbacks[GSOCK_OUTPUT])(this, GSOCK_OUTPUT, m_data[GSOCK_OUTPUT]);
e6f2ec14
DE
944 }
945 }
946 */
c5602b4a 947 return ( flags & m_detected ) ;
e6f2ec14
DE
948}
949
950/* Flags */
951
952/* GSocket_SetNonBlocking:
953 * Sets the socket to non-blocking mode. All IO calls will return
954 * immediately.
955 */
948c96ef 956void GSocket::SetNonBlocking(bool non_block)
e6f2ec14 957{
c5602b4a 958 assert(this);
e6f2ec14 959
c5602b4a 960 m_non_blocking = non_block;
e6f2ec14
DE
961}
962
963/* GSocket_SetTimeout:
964 * Sets the timeout for blocking calls. Time is expressed in
965 * milliseconds.
966 */
c5602b4a 967void GSocket::SetTimeout(unsigned long millisec)
e6f2ec14 968{
c5602b4a 969 assert(this);
e6f2ec14
DE
970
971// this is usually set too high and we have not yet been able to detect a closed
972// stream, thus we leave the 10 sec timeout
c5602b4a 973// m_timeout = millisec;
e6f2ec14
DE
974}
975
976/* GSocket_GetError:
977 * Returns the last error occured for this socket. Note that successful
978 * operations do not clear this back to GSOCK_NOERROR, so use it only
979 * after an error.
980 */
c5602b4a 981GSocketError WXDLLIMPEXP_NET GSocket::GetError()
e6f2ec14 982{
c5602b4a 983 assert(this);
e6f2ec14 984
c5602b4a 985 return m_error;
e6f2ec14
DE
986}
987
988/* Callbacks */
989
990/* GSOCK_INPUT:
991 * There is data to be read in the input buffer. If, after a read
992 * operation, there is still data available, the callback function will
993 * be called again.
994 * GSOCK_OUTPUT:
995 * The socket is available for writing. That is, the next write call
996 * won't block. This event is generated only once, when the connection is
997 * first established, and then only if a call failed with GSOCK_WOULDBLOCK,
998 * when the output buffer empties again. This means that the app should
999 * assume that it can write since the first OUTPUT event, and no more
1000 * OUTPUT events will be generated unless an error occurs.
1001 * GSOCK_CONNECTION:
1002 * Connection succesfully established, for client sockets, or incoming
1003 * client connection, for server sockets. Wait for this event (also watch
1004 * out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
1005 * GSOCK_LOST:
1006 * The connection is lost (or a connection request failed); this could
1007 * be due to a failure, or due to the peer closing it gracefully.
1008 */
1009
1010/* GSocket_SetCallback:
1011 * Enables the callbacks specified by 'flags'. Note that 'flags'
1012 * may be a combination of flags OR'ed toghether, so the same
1013 * callback function can be made to accept different events.
1014 * The callback function must have the following prototype:
1015 *
1016 * void function(GSocket *socket, GSocketEvent event, char *cdata)
1017 */
c5602b4a 1018void GSocket::SetCallback(GSocketEventFlags flags,
e6f2ec14
DE
1019 GSocketCallback callback, char *cdata)
1020{
1021 int count;
1022
c5602b4a 1023 assert(this);
e6f2ec14
DE
1024
1025 for (count = 0; count < GSOCK_MAX_EVENT; count++)
1026 {
1027 if ((flags & (1 << count)) != 0)
1028 {
c5602b4a
DE
1029 m_cbacks[count] = callback;
1030 m_data[count] = cdata;
e6f2ec14
DE
1031 }
1032 }
1033}
1034
1035/* GSocket_UnsetCallback:
1036 * Disables all callbacks specified by 'flags', which may be a
1037 * combination of flags OR'ed toghether.
1038 */
c5602b4a 1039void GSocket::UnsetCallback(GSocketEventFlags flags)
e6f2ec14
DE
1040{
1041 int count;
1042
c5602b4a 1043 assert(this);
e6f2ec14
DE
1044
1045 for (count = 0; count < GSOCK_MAX_EVENT; count++)
1046 {
1047 if ((flags & (1 << count)) != 0)
1048 {
c5602b4a
DE
1049 m_cbacks[count] = NULL;
1050 m_data[count] = NULL;
e6f2ec14
DE
1051 }
1052 }
1053}
1054
1055
1056#define CALL_CALLBACK(socket, event) { \
1057 _GSocket_Disable(socket, event); \
1058 if (socket->m_cbacks[event]) \
1059 socket->m_cbacks[event](socket, event, socket->m_data[event]); \
1060}
1061
c5602b4a 1062int GSocket::Recv_Stream(char *buffer, int size)
e6f2ec14
DE
1063{
1064 OTFlags flags ;
1065 OTResult res ;
1066 OTByteCount sz = 0 ;
1067
c5602b4a 1068 OTCountDataBytes( m_endpoint , &sz ) ;
e6f2ec14
DE
1069 if ( size > (int)sz )
1070 size = sz ;
c5602b4a 1071 res = OTRcv( m_endpoint , buffer , size , &flags ) ;
e6f2ec14
DE
1072 if ( res < 0 )
1073 {
1074 return -1 ;
1075 }
1076
1077 // we simulate another read event if there are still bytes
c5602b4a 1078 if ( m_takesEvents )
e6f2ec14
DE
1079 {
1080 OTByteCount sz = 0 ;
c5602b4a 1081 OTCountDataBytes( m_endpoint , &sz ) ;
e6f2ec14
DE
1082 if ( sz > 0 )
1083 {
c5602b4a
DE
1084 m_detected |= GSOCK_INPUT_FLAG ;
1085 (m_cbacks[GSOCK_INPUT])(this, GSOCK_INPUT, m_data[GSOCK_INPUT]);
e6f2ec14
DE
1086 }
1087 }
1088 return res ;
1089}
1090
c5602b4a 1091int GSocket::Recv_Dgram(char *buffer, int size)
e6f2ec14
DE
1092{
1093// TODO
1094 int ret = -1;
1095#if 0
1096 struct sockaddr from;
1097 SOCKLEN_T fromlen = sizeof(from);
1098 GSocketError err;
1099
1100 fromlen = sizeof(from);
1101
c5602b4a 1102 ret = recvfrom(m_endpoint, buffer, size, 0, &from, (SOCKLEN_T *) &fromlen);
e6f2ec14
DE
1103
1104 if (ret == -1)
1105 return -1;
1106
1107 /* Translate a system address into a GSocket address */
c5602b4a 1108 if (!m_peer)
e6f2ec14 1109 {
c5602b4a
DE
1110 m_peer = GAddress_new();
1111 if (!m_peer)
e6f2ec14 1112 {
c5602b4a 1113 m_error = GSOCK_MEMERR;
e6f2ec14
DE
1114 return -1;
1115 }
1116 }
c5602b4a 1117 err = _GAddress_translate_from(m_peer, &from, fromlen);
e6f2ec14
DE
1118 if (err != GSOCK_NOERROR)
1119 {
c5602b4a
DE
1120 GAddress_destroy(m_peer);
1121 m_peer = NULL;
1122 m_error = err;
e6f2ec14
DE
1123 return -1;
1124 }
1125#endif
1126 return ret;
1127}
1128
c5602b4a 1129int GSocket::Send_Stream(const char *buffer, int size)
e6f2ec14
DE
1130{
1131 OTFlags flags = 0 ;
1132 OTResult res ;
1133
c5602b4a 1134 res = OTSnd( m_endpoint , (void*) buffer , size , flags ) ;
e6f2ec14
DE
1135 return res ;
1136}
1137
c5602b4a 1138int GSocket::Send_Dgram(const char *buffer, int size)
e6f2ec14
DE
1139{
1140 int ret = -1 ;
1141// TODO
1142#if 0
1143 struct sockaddr *addr;
1144 int len ;
1145 GSocketError err;
1146
c5602b4a 1147 if (!m_peer)
e6f2ec14 1148 {
c5602b4a 1149 m_error = GSOCK_INVADDR;
e6f2ec14
DE
1150 return -1;
1151 }
1152
c5602b4a 1153 err = _GAddress_translate_to(m_peer, &addr, &len);
e6f2ec14
DE
1154 if (err != GSOCK_NOERROR)
1155 {
c5602b4a 1156 m_error = err;
e6f2ec14
DE
1157 return -1;
1158 }
1159
c5602b4a 1160 ret = sendto(m_endpoint, buffer, size, 0, addr, len);
e6f2ec14
DE
1161
1162 /* Frees memory allocated from _GAddress_translate_to */
1163 free(addr);
1164#endif
1165 return ret;
1166}
1167
f5d5bd1d
DE
1168/* Compatibility functions for GSocket */
1169GSocket *GSocket_new(void)
1170{
1171 GSocket *newsocket = new GSocket();
1172 if(newsocket->IsOk())
1173 return newsocket;
1174 delete newsocket;
1175 return NULL;
1176}
1177
e6f2ec14
DE
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{
948c96ef 1276 if ( !GSocket_Verify_Inited() )
e6f2ec14
DE
1277 return GSOCK_IOERR ;
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
948c96ef 1302 if ( !GSocket_Verify_Inited() )
e6f2ec14
DE
1303 return GSOCK_IOERR ;
1304
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{
1327 assert(address != NULL);
1328
1329 CHECK_ADDRESS(address, INET, GSOCK_INVADDR);
1330
1331 address->m_host = htonl(hostaddr) ;
1332
1333 return GSOCK_NOERROR;
1334}
1335
1336struct service_entry
1337{
3e637936 1338 const char * name ;
e6f2ec14 1339 unsigned short port ;
3e637936 1340 const char * protocol ;
e6f2ec14
DE
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{
1352 size_t i ;
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{
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 ;
948c96ef 1396 if ( !GSocket_Verify_Inited() )
e6f2ec14
DE
1397 return GSOCK_IOERR ;
1398
1399 assert(address != NULL);
1400 CHECK_ADDRESS(address, INET, GSOCK_INVADDR);
1401
1402 OTInetAddressToName( gInetSvcRef , address->m_host , name ) ;
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 ntohl(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
c5602b4a 1423void GSocket::Enable_Events()
e6f2ec14 1424{
c5602b4a 1425 if ( m_takesEvents )
e6f2ec14
DE
1426 return ;
1427
1428 {
1429 OTResult state ;
948c96ef 1430 m_takesEvents = true ;
c5602b4a 1431 state = OTGetEndpointState(m_endpoint);
e6f2ec14
DE
1432
1433 {
1434 OTByteCount sz = 0 ;
c5602b4a 1435 OTCountDataBytes( m_endpoint , &sz ) ;
e6f2ec14
DE
1436 if ( state == T_INCON || sz > 0 )
1437 {
c5602b4a
DE
1438 m_detected |= GSOCK_INPUT_FLAG ;
1439 (m_cbacks[GSOCK_INPUT])(this, GSOCK_INPUT, m_data[GSOCK_INPUT]);
e6f2ec14
DE
1440 }
1441 }
1442 {
1443 if ( state == T_DATAXFER || state == T_INREL )
1444 {
c5602b4a
DE
1445 m_detected |=GSOCK_OUTPUT_FLAG ;
1446 (m_cbacks[GSOCK_OUTPUT])(this, GSOCK_OUTPUT, m_data[GSOCK_OUTPUT]);
e6f2ec14
DE
1447 }
1448 }
1449 }
1450}
1451
c5602b4a 1452void GSocket::Disable_Events()
e6f2ec14 1453{
948c96ef 1454 m_takesEvents = false ;
e6f2ec14
DE
1455}
1456
1457/* _GSocket_Input_Timeout:
1458 * For blocking sockets, wait until data is available or
1459 * until timeout ellapses.
1460 */
c5602b4a 1461GSocketError GSocket::Input_Timeout()
e6f2ec14 1462{
c5602b4a 1463 if ( !m_non_blocking )
e6f2ec14
DE
1464 {
1465 UnsignedWide now , start ;
948c96ef 1466 bool formerTakesEvents = m_takesEvents ;
e6f2ec14
DE
1467 Microseconds(&start);
1468 now = start ;
948c96ef 1469 m_takesEvents = false ;
e6f2ec14 1470
c5602b4a 1471 while( (now.hi * 4294967296.0 + now.lo) - (start.hi * 4294967296.0 + start.lo) < m_timeout * 1000.0 )
e6f2ec14
DE
1472 {
1473 OTResult state ;
1474 OTByteCount sz = 0 ;
c5602b4a 1475 state = OTGetEndpointState(m_endpoint);
e6f2ec14 1476
c5602b4a 1477 OTCountDataBytes( m_endpoint , &sz ) ;
e6f2ec14
DE
1478 if ( state == T_INCON || sz > 0 )
1479 {
c5602b4a 1480 m_takesEvents = formerTakesEvents ;
e6f2ec14
DE
1481 return GSOCK_NOERROR;
1482 }
1483 Microseconds(&now);
1484 }
c5602b4a
DE
1485 m_takesEvents = formerTakesEvents ;
1486 m_error = GSOCK_TIMEDOUT;
e6f2ec14
DE
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 */
c5602b4a 1496GSocketError GSocket::Output_Timeout()
e6f2ec14 1497{
c5602b4a 1498 if ( !m_non_blocking )
e6f2ec14
DE
1499 {
1500 UnsignedWide now , start ;
948c96ef 1501 bool formerTakesEvents = m_takesEvents ;
e6f2ec14
DE
1502 Microseconds(&start);
1503 now = start ;
948c96ef 1504 m_takesEvents = false ;
e6f2ec14 1505
c5602b4a 1506 while( (now.hi * 4294967296.0 + now.lo) - (start.hi * 4294967296.0 + start.lo) < m_timeout * 1000.0 )
e6f2ec14
DE
1507 {
1508 OTResult state ;
c5602b4a 1509 state = OTGetEndpointState(m_endpoint);
e6f2ec14
DE
1510
1511 if ( state == T_DATAXFER || state == T_INREL )
1512 {
c5602b4a 1513 m_takesEvents = formerTakesEvents ;
e6f2ec14
DE
1514 return GSOCK_NOERROR;
1515 }
1516 Microseconds(&now);
1517 }
c5602b4a
DE
1518 m_takesEvents = formerTakesEvents ;
1519 m_error = GSOCK_TIMEDOUT;
e6f2ec14
DE
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
1642/* Hack added for Mac OS X */
1643GSocketError GAddress_UNIX_GetPath(GAddress *addr, char *path, size_t buf)
1644{
1645 return GSOCK_INVADDR;
1646}
1647
1648GSocketError GAddress_UNIX_SetPath(GAddress *addr, const char *path)
1649{
1650 return GSOCK_INVADDR;
1651}
1652
1653#endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */