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