/* -------------------------------------------------------------------------
* Project: GSocket (Generic Socket) for WX
- * Name: gsocket.c
+ * Name: src/mac/classic/gsocket.c
* Authors: Guilhem Lavaux,
* Guillermo Rodriguez Garcia <guille@iies.es> (maintainer)
* Stefan CSomor
*/
#ifndef __GSOCKET_STANDALONE__
-#include "wx/setup.h"
#include "wx/platform.h"
#endif
OSStatus DoNegotiateIPReuseAddrOption(EndpointRef ep, Boolean enableReuseIPMode);
/* Input: ep - endpointref on which to negotiate the option
- enableReuseIPMode - desired option setting - true/false
+ enableReuseIPMode - desired option setting - true/false
Return: kOTNoError indicates that the option was successfully negotiated
- OSStatus is an error if < 0, otherwise, the status field is
- returned and is > 0.
-
- IMPORTANT NOTE: The endpoint is assumed to be in synchronous more, otherwise
- this code will not function as desired
+ OSStatus is an error if < 0, otherwise, the status field is
+ returned and is > 0.
+
+ IMPORTANT NOTE: The endpoint is assumed to be in synchronous more, otherwise
+ this code will not function as desired
*/
OSStatus DoNegotiateIPReuseAddrOption(EndpointRef ep, Boolean enableReuseIPMode)
{
- UInt8 buf[kOTFourByteOptionSize]; // define buffer for fourByte Option size
- TOption* opt; // option ptr to make items easier to access
- TOptMgmt req;
- TOptMgmt ret;
- OSStatus err;
-
- if (!OTIsSynchronous(ep))
- {
- return (-1);
- }
+ UInt8 buf[kOTFourByteOptionSize]; // define buffer for fourByte Option size
+ TOption* opt; // option ptr to make items easier to access
+ TOptMgmt req;
+ TOptMgmt ret;
+ OSStatus err;
+
+ if (!OTIsSynchronous(ep))
+ {
+ return (-1);
+ }
opt = (TOption*)buf; // set option ptr to buffer
req.opt.buf = buf;
req.opt.len = sizeof(buf);
#else
if ( gInetSvcRef )
return TRUE ;
-
+
InitOpenTransport() ;
gOTInited = 1 ;
gInetSvcRef = OTOpenInternetServices(kDefaultInternetServicesPath, NULL, &err);
GSocket *GSocket_new()
{
-
+
int i;
GSocket *socket;
/* we do not support multihoming with this code at the moment
- OTGetProtAddress will have to be used then - but we don't have a handy
+ OTGetProtAddress will have to be used then - but we don't have a handy
method to use right now
*/
{
* Sets up this socket as a server. The local address must have been
* set with GSocket_SetLocal() before GSocket_SetServer() is called.
* Returns GSOCK_NOERROR on success, one of the following otherwise:
- *
+ *
* Error codes:
* GSOCK_INVSOCK - the socket is in use.
* GSOCK_INVADDR - the local address has not been set.
- * GSOCK_IOERR - low-level error.
+ * GSOCK_IOERR - low-level error.
*/
GSocketError GSocket_SetServer(GSocket *sck)
{
if ((bind(sck->m_endpoint, sck->m_local->m_addr, sck->m_local->m_len) != 0) ||
(getsockname(sck->m_endpoint,
sck->m_local->m_addr,
- (SOCKLEN_T *) &sck->m_local->m_len) != 0) ||
+ (WX_SOCKLEN_T *) &sck->m_local->m_len) != 0) ||
(listen(sck->m_endpoint, 5) != 0))
{
close(sck->m_endpoint);
* GSOCK_TIMEDOUT - timeout, no incoming connections.
* GSOCK_WOULDBLOCK - the call would block and the socket is nonblocking.
* GSOCK_MEMERR - couldn't allocate memory.
- * GSOCK_IOERR - low-level error.
+ * GSOCK_IOERR - low-level error.
*/
GSocket *GSocket_WaitConnection(GSocket *socket)
{
// TODO
#if 0
- connection->m_endpoint = accept(socket->m_endpoint, &from, (SOCKLEN_T *) &fromlen);
+ connection->m_endpoint = accept(socket->m_endpoint, &from, (WX_SOCKLEN_T *) &fromlen);
#endif
if (connection->m_endpoint == kOTInvalidEndpointRef )
sck->m_oriented = FALSE;
/* Create the socket */
-
+
// TODO
#if 0
sck->m_endpoint = socket(sck->m_local->m_realfamily, SOCK_DGRAM, 0);
if ((bind(sck->m_endpoint, sck->m_local->m_addr, sck->m_local->m_len) != 0) ||
(getsockname(sck->m_endpoint,
sck->m_local->m_addr,
- (SOCKLEN_T *) &sck->m_local->m_len) != 0))
+ (WX_SOCKLEN_T *) &sck->m_local->m_len) != 0))
{
close(sck->m_endpoint);
sck->m_endpoint = -1;
* For stream (connection oriented) sockets, GSocket_Connect() tries
* to establish a client connection to a server using the peer address
* as established with GSocket_SetPeer(). Returns GSOCK_NOERROR if the
- * connection has been succesfully established, or one of the error
+ * connection has been successfully established, or one of the error
* codes listed below. Note that for nonblocking sockets, a return
* value of GSOCK_WOULDBLOCK doesn't mean a failure. The connection
* request can be completed later; you should use GSocket_Select()
* GSOCK_TIMEDOUT - timeout, the connection failed.
* GSOCK_WOULDBLOCK - connection in progress (nonblocking sockets only)
* GSOCK_MEMERR - couldn't allocate memory.
- * GSOCK_IOERR - low-level error.
+ * GSOCK_IOERR - low-level error.
*/
GSocketError GSocket_Connect(GSocket *sck, GSocketStream stream)
{
/* Create the socket */
#if TARGET_CARBON
- sck->m_endpoint =
+ sck->m_endpoint =
OTOpenEndpointInContext( OTCreateConfiguration( kTCPName) , 0 , &info , &err , NULL ) ;
#else
- sck->m_endpoint =
+ sck->m_endpoint =
OTOpenEndpoint( OTCreateConfiguration( kTCPName) , 0 , &info , &err ) ;
#endif
if ( sck->m_endpoint == kOTInvalidEndpointRef || err != kOTNoError )
* is in blocking mode, we select() for the specified timeout
* checking for writability to see if the connection request
* completes.
- */
+ */
if ((err == kOTNoDataErr ) && (!sck->m_non_blocking))
{
{
/*
int error;
- SOCKLEN_T len = sizeof(error);
+ WX_SOCKLEN_T len = sizeof(error);
getsockopt(sck->m_endpoint, SOL_SOCKET, SO_ERROR, (void*) &error, &len);
ret = _GSocket_Recv_Stream(socket, buffer, size);
else
ret = _GSocket_Recv_Dgram(socket, buffer, size);
-
+
if (ret == -1)
{
if (errno == EWOULDBLOCK)
else
socket->m_error = GSOCK_IOERR;
}
-
+
return ret;
}
int GSocket_Write(GSocket *socket, const char *buffer, int size)
-{
+{
int ret;
assert(socket != NULL);
ret = _GSocket_Send_Stream(socket, buffer, size);
else
ret = _GSocket_Send_Dgram(socket, buffer, size);
-
+
if (ret == -1)
{
if (errno == EWOULDBLOCK)
socket->m_detected &= ~GSOCK_OUTPUT_FLAG;
return -1;
}
-
+
return ret;
}
wxMacProcessNotifierEvents() ;
/*
state = OTGetEndpointState(socket->m_endpoint);
-
+
if ( ( flags & GSOCK_INPUT_FLAG ) && ! ( socket->m_detected & GSOCK_INPUT_FLAG ) )
{
size_t sz = 0 ;
}
/* GSocket_GetError:
- * Returns the last error occured for this socket. Note that successful
+ * Returns the last error which occurred for this socket. Note that successful
* operations do not clear this back to GSOCK_NOERROR, so use it only
* after an error.
*/
* operation, there is still data available, the callback function will
* be called again.
* GSOCK_OUTPUT:
- * The socket is available for writing. That is, the next write call
+ * The socket is available for writing. That is, the next write call
* won't block. This event is generated only once, when the connection is
* first established, and then only if a call failed with GSOCK_WOULDBLOCK,
* when the output buffer empties again. This means that the app should
* assume that it can write since the first OUTPUT event, and no more
* OUTPUT events will be generated unless an error occurs.
* GSOCK_CONNECTION:
- * Connection succesfully established, for client sockets, or incoming
+ * Connection successfully established, for client sockets, or incoming
* client connection, for server sockets. Wait for this event (also watch
* out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
* GSOCK_LOST:
int ret = -1;
#if 0
struct sockaddr from;
- SOCKLEN_T fromlen = sizeof(from);
+ WX_SOCKLEN_T fromlen = sizeof(from);
GSocketError err;
fromlen = sizeof(from);
- ret = recvfrom(socket->m_endpoint, buffer, size, 0, &from, (SOCKLEN_T *) &fromlen);
+ ret = recvfrom(socket->m_endpoint, buffer, size, 0, &from, (WX_SOCKLEN_T *) &fromlen);
if (ret == -1)
return -1;
{
address->m_family = GSOCK_INET;
address->m_host = kOTAnyInetAddress ;
-
+
return GSOCK_NOERROR;
}
address->m_host = INADDR_NONE ;
address->m_error = GSOCK_NOHOST;
return GSOCK_NOHOST;
- }
+ }
address->m_host = hinfo.addrs[0] ;
return GSOCK_NOERROR;
}
return GSOCK_NOERROR;
}
-struct service_entry
+struct service_entry
{
char * name ;
unsigned short port ;
- char * protocol ;
+ char * protocol ;
} ;
typedef struct service_entry service_entry ;
service_entry gServices[] =
{
- { "http" , 80 , "tcp" }
+ { "http" , 80 , "tcp" }
} ;
GSocketError GAddress_INET_SetPortName(GAddress *address, const char *port,
assert(address != NULL);
CHECK_ADDRESS(address, INET, GSOCK_INVADDR);
address->m_port = port ;
-
+
return GSOCK_NOERROR;
}
GSocketError GAddress_INET_GetHostName(GAddress *address, char *hostname, size_t sbuf)
{
InetDomainName name ;
- if ( GSocket_Verify_Inited() == FALSE )
+ if ( GSocket_Verify_Inited() == FALSE )
return GSOCK_IOERR ;
-
- assert(address != NULL);
+
+ assert(address != NULL);
CHECK_ADDRESS(address, INET, GSOCK_INVADDR);
OTInetAddressToName( gInetSvcRef , address->m_host , name ) ;
unsigned long GAddress_INET_GetHostAddress(GAddress *address)
{
- assert(address != NULL);
- CHECK_ADDRESS(address, INET, 0);
+ assert(address != NULL);
+ CHECK_ADDRESS(address, INET, 0);
return address->m_host;
}
unsigned short GAddress_INET_GetPort(GAddress *address)
{
- assert(address != NULL);
- CHECK_ADDRESS(address, INET, 0);
+ assert(address != NULL);
+ CHECK_ADDRESS(address, INET, 0);
return address->m_port;
}
{
if ( socket->m_takesEvents )
return ;
-
+
{
OTResult state ;
socket->m_takesEvents = TRUE ;
state = OTGetEndpointState(socket->m_endpoint);
-
+
{
OTByteCount sz = 0 ;
OTCountDataBytes( socket->m_endpoint , &sz ) ;
Microseconds(&start);
now = start ;
socket->m_takesEvents = FALSE ;
-
+
while( (now.hi * 4294967296.0 + now.lo) - (start.hi * 4294967296.0 + start.lo) < socket->m_timeout * 1000.0 )
{
OTResult state ;
OTByteCount sz = 0 ;
state = OTGetEndpointState(socket->m_endpoint);
-
+
OTCountDataBytes( socket->m_endpoint , &sz ) ;
if ( state == T_INCON || sz > 0 )
{
socket->m_takesEvents = formerTakesEvents ;
return GSOCK_NOERROR;
}
- Microseconds(&now);
+ Microseconds(&now);
}
socket->m_takesEvents = formerTakesEvents ;
socket->m_error = GSOCK_TIMEDOUT;
Microseconds(&start);
now = start ;
socket->m_takesEvents = FALSE ;
-
+
while( (now.hi * 4294967296.0 + now.lo) - (start.hi * 4294967296.0 + start.lo) < socket->m_timeout * 1000.0 )
{
OTResult state ;
state = OTGetEndpointState(socket->m_endpoint);
-
+
if ( state == T_DATAXFER || state == T_INREL )
{
socket->m_takesEvents = formerTakesEvents ;
return GSOCK_NOERROR;
}
- Microseconds(&now);
+ Microseconds(&now);
}
socket->m_takesEvents = formerTakesEvents ;
socket->m_error = GSOCK_TIMEDOUT;
* operation, there is still data available, the callback function will
* be called again.
* GSOCK_OUTPUT:
- * The socket is available for writing. That is, the next write call
+ * The socket is available for writing. That is, the next write call
* won't block. This event is generated only once, when the connection is
* first established, and then only if a call failed with GSOCK_WOULDBLOCK,
* when the output buffer empties again. This means that the app should
* assume that it can write since the first OUTPUT event, and no more
* OUTPUT events will be generated unless an error occurs.
* GSOCK_CONNECTION:
- * Connection succesfully established, for client sockets, or incoming
+ * Connection successfully established, for client sockets, or incoming
* client connection, for server sockets. Wait for this event (also watch
* out for GSOCK_LOST) after you issue a nonblocking GSocket_Connect() call.
* GSOCK_LOST: