static OSStatus TransferTest(void) { OSStatus err; OSStatus junk; EndpointRef ep; EndpointRef ep2; OTClient me; ep2 = NULL; ep = OTOpenEndpointInContext(OTCreateConfiguration("tcp"), 0, NULL, &err, NULL); if (err == noErr) { me = OTWhoAmIInContext(NULL); ep2 = OTTransferProviderOwnershipInContext(ep, me, &err, NULL); if (err == noErr) { ep = NULL; } } if (ep != NULL) { junk = OTCloseProvider(ep); assert(junk == noErr); } if (ep2 != NULL) { junk = OTCloseProvider(ep2); assert(junk == noErr); } return err; }
// CreateProxyRegistrationForRealService() checks to see if the given port is currently // in use, and if so, advertises the specified service as present on that port. // This is useful for advertising existing real services (Personal Web Sharing, Personal // File Sharing, etc.) that currently don't register with mDNS Service Discovery themselves. static DNSServiceErrorType CreateProxyRegistrationForRealService(RegisteredService *rs, const char *servicetype, UInt16 PortAsNumber, const char txtinfo[]) { mDNSOpaque16 OpaquePort = mDNSOpaque16fromIntVal(PortAsNumber); InetAddress ia; TBind bindReq; OSStatus err; TEndpointInfo endpointinfo; EndpointRef ep = OTOpenEndpoint(OTCreateConfiguration(kTCPName), 0, &endpointinfo, &err); if (!ep || err) { printf("OTOpenEndpoint (CreateProxyRegistrationForRealService) failed %d", err); return(err); } ia.fAddressType = AF_INET; ia.fPort = OpaquePort.NotAnInteger; ia.fHost = 0; bindReq.addr.maxlen = sizeof(ia); bindReq.addr.len = sizeof(ia); bindReq.addr.buf = (UInt8*)&ia; bindReq.qlen = 0; err = OTBind(ep, &bindReq, NULL); if (err == kOTBadAddressErr) err = RegisterService(rs, OpaquePort, "", servicetype, "local.", txtinfo); else if (err) printf("OTBind failed %d", err); OTCloseProvider(ep); return(err); }
/* GSocket_Shutdown: * Disallow further read/write operations on this socket, close * the fd and disable all callbacks. */ void GSocket_Shutdown(GSocket *socket) { OSStatus err ; int evt; assert(socket != NULL); /* If socket has been created, shutdown it */ if (socket->m_endpoint != kOTInvalidEndpointRef ) { err = OTSndOrderlyDisconnect( socket->m_endpoint ) ; if ( err != kOTNoError ) { } err = OTRcvOrderlyDisconnect( socket->m_endpoint ) ; err = OTUnbind( socket->m_endpoint ) ; err = OTCloseProvider( socket->m_endpoint ) ; socket->m_endpoint = kOTInvalidEndpointRef ; } /* Disable GUI callbacks */ for (evt = 0; evt < GSOCK_MAX_EVENT; evt++) socket->m_cbacks[evt] = NULL; socket->m_detected = 0; _GSocket_Disable_Events(socket); wxMacRemoveAllNotifiersForData( wxMacGetNotifierTable() , socket ) ; }
static int LookUpInetHost(char *name, InetHost *result) { OSStatus err; // If the hostname is dots and numbers, like "128.32.122.13", we don't neet DNR err = OTInetStringToHost(name, result); if (err != noErr) { // We do need DNR. InetSvcRef tcpipProvider; InetHostInfo hinfo; tcpipProvider = OTOpenInternetServicesInContext(kDefaultInternetServicesPath, 0, &err, OTContext); if (err != noErr) { error("¥ otudp: Error %d from OTOpenInternetServices().", err); return 0; } err = OTInetStringToAddress(tcpipProvider, name, &hinfo); OTCloseProvider(tcpipProvider); if (err == kOTBadNameErr) { error("¥ otudp: Bad host name \"%s\"; not changing.", name); } else if (err != noErr) { error("¥ otudp: Error %d from OTInetStringToAddress; not changing host.", err); return 0; } *result = hinfo.addrs[0]; } return 1; }
/* Open Transport helper procedures */ static int LookUpInetHost(char *name, InetHost *result) { OSStatus err; // If the hostname is dots and numbers, like "128.32.122.13", we don't neet DNR err = OTInetStringToHost(name, result); if (err != noErr) { // We do need DNR. InetSvcRef tcpipProvider; InetHostInfo hinfo; tcpipProvider = OTOpenInternetServices(kDefaultInternetServicesPath, 0, &err); if (err != noErr) { post("е otudp: Error %d from OTOpenInternetServices().", err); return 0; } err = OTInetStringToAddress(tcpipProvider, name, &hinfo); OTCloseProvider(tcpipProvider); if (err != noErr) { post("е otudp: Error %d from OTInetStringToAddress", err); return 0; } *result = hinfo.addrs[0]; } return 1; }
// CreateProxyRegistrationForRealService() checks to see if the given port is currently // in use, and if so, advertises the specified service as present on that port. // This is useful for advertising existing real services (Personal Web Sharing, Personal // File Sharing, etc.) that currently don't register with mDNS Service Discovery themselves. mDNSlocal OSStatus CreateProxyRegistrationForRealService(mDNS *m, UInt16 PortAsNumber, const char txtinfo[], const char *servicetype, ServiceRecordSet *recordset) { InetAddress ia; TBind bindReq; OSStatus err; TEndpointInfo endpointinfo; EndpointRef ep = OTOpenEndpoint(OTCreateConfiguration(kTCPName), 0, &endpointinfo, &err); if (!ep || err) { printf("OTOpenEndpoint (CreateProxyRegistrationForRealService) failed %d", err); return(err); } ia.fAddressType = AF_INET; ia.fPort = mDNSOpaque16fromIntVal(PortAsNumber).NotAnInteger; ia.fHost = 0; bindReq.addr.maxlen = sizeof(ia); bindReq.addr.len = sizeof(ia); bindReq.addr.buf = (UInt8*)&ia; bindReq.qlen = 0; err = OTBind(ep, &bindReq, NULL); if (err == kOTBadAddressErr) RegisterService(m, recordset, PortAsNumber, txtinfo, &m->nicelabel, servicetype, "local."); else if (err) debugf("OTBind failed %d", err); OTCloseProvider(ep); return(noErr); }
static OSStatus MemTest(void) { OSStatus err; OSStatus junk; EndpointRef ep; void *p1; void *p2; p1 = NULL; p2 = NULL; ep = OTOpenEndpointInContext(OTCreateConfiguration("tcp"), 0, NULL, &err, NULL); if (err == noErr) { p1 = OTAllocInContext(ep, T_BIND, T_ALL, &err, NULL); } if (err == noErr) { p2 = OTAllocMemInContext(100, NULL); } if (p2 != NULL) { OTFreeMem(p2); } if (p1 != NULL) { junk = OTFree(p1, T_BIND); assert(junk == noErr); } if (ep != NULL) { junk = OTCloseProvider(ep); assert(junk == noErr); } return err; }
/*---------------------------------------------------------------------------*/ static AGSocket *AGNetSocketNew(AGNetCtx *ctx) { AGSocket *soc; OSStatus err = noErr; /* Note that in this implementation we don't actually need the AGNetCtx pointer. Left in for source-code compatibility. */ soc = (AGSocket *)malloc(sizeof(AGSocket)); if (NULL == soc) return NULL; bzero(soc, sizeof(AGSocket)); soc->ep = OTOpenEndpoint(OTCreateConfiguration(kTCPName), 0, NULL, &err); if (noErr == err) { err = OTBind(soc->ep, NULL, NULL); if (noErr == err) { /* Success. Return newly created socket. */ soc->bound = TRUE; return soc; } OTCloseProvider(soc->ep); } /* Didn't successfully bind, so return error. */ free(soc); return NULL; }
NMErr OTIPEnumerator::EndEnumeration(void) { OSStatus status; if (! bActive) return kNMNoError; if (mEP == kOTInvalidEndpointRef) return kNMNoError; status = OTUnbind(mEP); if (status == kNMNoError) { for (NMSInt32 i = 0; i < 1000; i++) { if (OTGetEndpointState(mEP) == T_UNBND) break; } } status = OTCloseProvider(mEP); InterruptSafe_free(mIncomingData.udata.buf); return kNMNoError; }
static void CloseDNS(void) { if ( dnsStatus.dns ) { OTCloseProvider(dnsStatus.dns); dnsStatus.dns = 0; dnsStatus.stat = dnsNotReady; } OTlocalhost = 0; }
void ottcp_free(OTTCP *x) { freeobject(x->o_clock); freeobject(x->o_connectedclock); if (x->o_tcp_ep != 0) { OTCloseProvider(x->o_tcp_ep); } DisposePtr(x->o_ReadBufA); DisposePtr(x->o_ReadBufB); DisposePtr(x->o_WriteBuf); }
/* Must call this before quitting app */ void ResolverCleanup(void) { if (slNetChecked && slNetSvcOpen) { if (slNetPresent==1) OTCloseProvider(sSvcRef.ref); #if !TARGET_API_MAC_CARBON else if (slNetPresent==2) CloseResolver(); #endif } } /* end ResolverCleanup() */
void ot_cleanup(void) { Actual_Socket s; for (s = ot.socklist; s !=NULL; s = s->next) { OTUnbind(s->ep); OTCloseProvider(s->ep); } CloseOpenTransport(); }
PRInt32 _MD_closesocket(PRInt32 osfd) { OSStatus err; EndpointRef endpoint = (EndpointRef) osfd; PRThread *me = _PR_MD_CURRENT_THREAD(); if (endpoint == NULL) { err = kEBADFErr; goto ErrorExit; } if (me->io_pending && me->io_fd == osfd) me->io_pending = PR_FALSE; #if 0 { OTResult state; state = OTGetEndpointState(endpoint); err = OTSndOrderlyDisconnect(endpoint); if (err != kOTNoError && err != kOTOutStateErr) goto ErrorExit; state = OTGetEndpointState(endpoint); err = OTUnbind(endpoint); if (err != kOTNoError && err != kOTOutStateErr) goto ErrorExit; state = OTGetEndpointState(endpoint); err = OTSetSynchronous(endpoint); if (err != kOTNoError) goto ErrorExit; err = OTSetBlocking(endpoint); if (err != kOTNoError) goto ErrorExit; } #endif (void) OTSndOrderlyDisconnect(endpoint); err = OTCloseProvider(endpoint); if (err != kOTNoError) goto ErrorExit; return kOTNoError; ErrorExit: macsock_map_error(err); return -1; }
/*---------------------------------------------------------------------------*/ static sword AGNetSocketClose(AGNetCtx *ctx, AGSocket *soc) { /* Note that in this implementation we don't actually need the AGNetCtx pointer. Left in for source-code compatibility. */ if (NULL != soc) { if (soc->bound) OTUnbind(soc->ep); if (kOTInvalidEndpointRef != soc->ep) OTCloseProvider(soc->ep); } return 0; }
/*---------------------------------------------------------------------------*/ ExportFunc sword AGNetClose(AGNetCtx *ctx) { OSStatus rc = 0; if (NULL == ctx) return AG_NET_ERROR; if (AGNetIsInited) { OTCloseProvider(ctx->inet_services); CloseOpenTransport(); } return rc; }
void GSocket_Cleanup() { if ( gOTInited != 0 ) { if ( gInetSvcRef != NULL ) OTCloseProvider( gInetSvcRef ); #if TARGET_CARBON CloseOpenTransportInContext( NULL ) ; #else CloseOpenTransport() ; #endif if ( gOTNotifierUPP ) DisposeOTNotifyUPP( gOTNotifierUPP ) ; } }
/* Close a UDP network socket */ extern void SDLNet_UDP_Close(UDPsocket sock) { if ( sock != NULL ) { if ( sock->channel != INVALID_SOCKET ) { #ifdef MACOS_OPENTRANSPORT OTUnbind(sock->channel); OTCloseProvider(sock->channel); #else closesocket(sock->channel); #endif /* MACOS_OPENTRANSPORT */ } free(sock); } }
static OSStatus RunAllHTTPServers(void) // Run HTTP servers for all of the IP addresses on the machine. // This routine iterates through the active Internet interfaces, // starting server threads for each active IP address on each // interface. { OSStatus err; OSStatus junk; EndpointRef dummyEP; InetInterfaceInfo info; SInt32 interfaceIndex; Boolean done; TEndpointInfo epInfo; // Force TCP to load by creating a dummy endpoint. Otherwise, // if we're the first TCP application to run, OTInetGetInterfaceInfo // will not return any active interfaces )-: // Note that we do this with OTOpenEndpoint rather than OTMPXOpenEndpoint // because we know we're running at system task time so we might as // well give OT time to dial the modem etc. dummyEP = OTOpenEndpointInContext(OTCreateConfiguration("tcp"), 0, &epInfo, &err, NULL); if (err == noErr) { // Iterate through the interfaces, starting HTTP servers on each. done = false; interfaceIndex = 0; do { done = ( OTInetGetInterfaceInfo(&info, interfaceIndex) != noErr ); if ( ! done ) { err = RunServersForInterface(&info, interfaceIndex); interfaceIndex += 1; } } while (err == noErr && !done); } if (dummyEP != NULL) { junk = OTCloseProvider(dummyEP); assert(junk == noErr); } return err; }
void otudp_free(OTUDP *x) { debug_close_post("* entered otudp_free"); freeobject(x->o_clock); if (x->o_udp_ep != 0) { debug_close_post("* about to close Open Transport endpoint..."); OTCloseProvider(x->o_udp_ep); debug_close_post("* ... closed Open Transport endpoint"); } else { debug_close_post("* This OTUDP object didn't have an Open Transport endpoint"); } if (x->allBuffers != 0) { DestroyPackets(x->allBuffers, x->nbufs); } qelem_free(x->UnbindQelem); }
static pascal void DNRYieldNotifier( void* contextPtr, OTEventCode code, OTResult result, void* cookie) { #pragma unused(contextPtr) #pragma unused(result) #pragma unused(cookie) OSStatus status; switch (code) { case kOTSyncIdleEvent: status = Idle(); break; case kOTProviderWillClose: // if the dnr service is going away case kOTProviderIsClosed: // or already gone... (void) OTCloseProvider(gDNRep); // then close our provider gDNRep = kOTInvalidProviderRef; // and note that. break; default: /* do nothing */ break; } }
int connect_chuukei_server(char *prf_name) { #ifndef MACINTOSH #ifdef WINDOWS WSADATA wsaData; WORD wVersionRequested = (WORD) (( 1) | ( 1 << 8)); #endif struct sockaddr_in ask; struct hostent *hp; if (read_chuukei_prf(prf_name) < 0) { printf("Wrong prf file\n"); return (-1); } if (init_buffer() < 0) { printf("Malloc error\n"); return (-1); } #ifdef WINDOWS if (WSAStartup(wVersionRequested, &wsaData)) { msg_print("Report: WSAStartup failed."); return (-1); } #endif printf("server = %s\nport = %d\n", server_name, server_port); if ((hp = gethostbyname(server_name)) != NULL) { memset(&ask, 0, sizeof(ask)); memcpy(&ask.sin_addr, hp->h_addr_list[0], hp->h_length); } else { if ((ask.sin_addr.s_addr=inet_addr(server_name)) == 0) { printf("Bad hostname\n"); return (-1); } } ask.sin_family = AF_INET; ask.sin_port = htons((unsigned short)server_port); #ifndef WINDOWS if ((sd=socket(PF_INET,SOCK_STREAM, 0)) < 0) #else if ((sd=socket(PF_INET,SOCK_STREAM, 0)) == INVALID_SOCKET) #endif { printf("Can't create socket\n"); return (-1); } if (connect(sd, (struct sockaddr *)&ask, sizeof(ask)) < 0) { close(sd); printf("Can't connect %s port %d\n", server_name, server_port); return (-1); } return (0); #else /* MACINTOSH */ OSStatus err; InetHostInfo response; InetHost host_addr; InetAddress inAddr; TCall sndCall; Boolean bind = false; OSStatus junk; if (read_chuukei_prf(prf_name) < 0){ printf("Wrong prf file\n"); return (-1); } init_buffer(); printf("server = %s\nport = %d\n", server_name, server_port); #if TARGET_API_MAC_CARBON err = InitOpenTransportInContext(kInitOTForApplicationMask, NULL); #else err = InitOpenTransport(); #endif memset(&response, 0, sizeof(response)); #if TARGET_API_MAC_CARBON inet_services = OTOpenInternetServicesInContext(kDefaultInternetServicesPath, 0, &err, NULL); #else inet_services = OTOpenInternetServices(kDefaultInternetServicesPath, 0, &err); #endif if (err == noErr) { err = OTInetStringToAddress(inet_services, (char *)server_name, &response); if (err == noErr) { host_addr = response.addrs[0]; } else { printf("Bad hostname\n"); } #if TARGET_API_MAC_CARBON ep = (void *)OTOpenEndpointInContext(OTCreateConfiguration(kTCPName), 0, nil, &err, NULL); #else ep = (void *)OTOpenEndpoint(OTCreateConfiguration(kTCPName), 0, nil, &err); #endif if (err == noErr) { err = OTBind(ep, nil, nil); bind = (err == noErr); } if (err == noErr){ OTInitInetAddress(&inAddr, server_port, host_addr); sndCall.addr.len = sizeof(InetAddress); sndCall.addr.buf = (unsigned char*) &inAddr; sndCall.opt.buf = nil; /* no connection options */ sndCall.opt.len = 0; sndCall.udata.buf = nil; /* no connection data */ sndCall.udata.len = 0; sndCall.sequence = 0; /* ignored by OTConnect */ err = OTConnect(ep, &sndCall, NULL); if( err != noErr ){ printf("Can't connect %s port %d\n", server_name, server_port); } } err = OTSetSynchronous(ep); if (err == noErr) err = OTSetBlocking(ep); } if( err != noErr ){ if( bind ){ OTUnbind(ep); } /* Clean up. */ if (ep != kOTInvalidEndpointRef) { OTCloseProvider(ep); ep = nil; } if (inet_services != nil) { OTCloseProvider(inet_services); inet_services = nil; } return -1; } return 0; #endif }
/* Open a UDP network socket If 'port' is non-zero, the UDP socket is bound to a fixed local port. */ extern UDPsocket SDLNet_UDP_Open(Uint16 port) { UDPsocket sock; #ifdef MACOS_OPENTRANSPORT EndpointRef dummy = NULL; #endif /* Allocate a UDP socket structure */ sock = (UDPsocket)malloc(sizeof(*sock)); if ( sock == NULL ) { SDLNet_SetError("Out of memory"); goto error_return; } memset(sock, 0, sizeof(*sock)); /* Open the socket */ #ifdef MACOS_OPENTRANSPORT { sock->error = OTAsyncOpenEndpoint( OTCreateConfiguration(kUDPName),0, &(sock->info), (OTNotifyProcPtr)AsyncUDPNotifier, sock ); AsyncUDPPopEvent( sock ); while( !sock->error && !( sock->completion & CompleteMask(T_OPENCOMPLETE))) { AsyncUDPPopEvent( sock ); } if( sock->error ) { SDLNet_SetError("Could not open UDP socket"); goto error_return; } // Should we ?? // (01/05/03 minami<*****@*****.**> OTSetBlocking( sock->channel ); } #else sock->channel = socket(AF_INET, SOCK_DGRAM, 0); #endif /* MACOS_OPENTRANSPORT */ if ( sock->channel == INVALID_SOCKET ) { SDLNet_SetError("Couldn't create socket"); goto error_return; } #ifdef MACOS_OPENTRANSPORT { InetAddress required, assigned; TBind req_addr, assigned_addr; OSStatus status; InetInterfaceInfo info; memset(&assigned_addr, 0, sizeof(assigned_addr)); assigned_addr.addr.maxlen = sizeof(assigned); assigned_addr.addr.len = sizeof(assigned); assigned_addr.addr.buf = (UInt8 *) &assigned; if ( port ) { status = OTInetGetInterfaceInfo( &info, kDefaultInetInterface ); if( status != kOTNoError ) goto error_return; OTInitInetAddress(&required, port, info.fAddress ); req_addr.addr.maxlen = sizeof( required ); req_addr.addr.len = sizeof( required ); req_addr.addr.buf = (UInt8 *) &required; sock->error = OTBind(sock->channel, &req_addr, &assigned_addr); } else { sock->error = OTBind(sock->channel, nil, &assigned_addr ); } AsyncUDPPopEvent(sock); while( !sock->error && !(sock->completion & CompleteMask(T_BINDCOMPLETE))) { AsyncUDPPopEvent(sock); } if (sock->error != noErr) { SDLNet_SetError("Couldn't bind to local port, OTBind() = %d",(int) status); goto error_return; } sock->address.host = assigned.fHost; sock->address.port = assigned.fPort; #ifdef DEBUG_NET printf("UDP open host = %d, port = %d\n", assigned.fHost, assigned.fPort ); #endif } #else /* Bind locally, if appropriate */ if ( port ) { struct sockaddr_in sock_addr; memset(&sock_addr, 0, sizeof(sock_addr)); sock_addr.sin_family = AF_INET; sock_addr.sin_addr.s_addr = INADDR_ANY; sock_addr.sin_port = SDL_SwapBE16(port); /* Bind the socket for listening */ if ( bind(sock->channel, (struct sockaddr *)&sock_addr, sizeof(sock_addr)) == SOCKET_ERROR ) { SDLNet_SetError("Couldn't bind to local port"); goto error_return; } /* Fill in the channel host address */ sock->address.host = sock_addr.sin_addr.s_addr; sock->address.port = sock_addr.sin_port; } /* Allow LAN broadcasts with the socket */ { int yes = 1; setsockopt(sock->channel, SOL_SOCKET, SO_BROADCAST, (char*)&yes, sizeof(yes)); } #endif /* MACOS_OPENTRANSPORT */ /* The socket is ready */ return(sock); error_return: #ifdef MACOS_OPENTRANSPORT if( dummy ) OTCloseProvider( dummy ); #endif SDLNet_UDP_Close(sock); return(NULL); }
NMErr OTIPEnumerator::StartEnumeration(void) { TEndpointInfo info; NMErr status = kNMNoError; TBind request; TOption optBuf; //NMUInt8 optBuf[64]; //NMUInt8 fooBuf[32]; TOptMgmt cmd; //NMUInt8 *foo = fooBuf; // If they don't want us to actively get the enumeration, there is nothing to do if (! bActive) return kNMNoError; // first clear out any current items (mCallback)(mContext, kNMEnumClear, NULL); // [Edmark/PBE] 11/16/99 added bFirstIdle = true; // Create an OT endpoint mEP = OTOpenEndpoint(OTCreateConfiguration(kUDPName), 0, &info, &status); if (status) goto error; // fill in the option request cmd.flags = T_NEGOTIATE; cmd.opt.len = kOTFourByteOptionSize; cmd.opt.maxlen = kOTFourByteOptionSize; cmd.opt.buf = (NMUInt8*)&optBuf; // fill in the toption struct optBuf.len = sizeof(TOption); optBuf.level = INET_IP; optBuf.name = kIP_BROADCAST; optBuf.status = 0; optBuf.value[0] = 1; /* cmd.opt.len = 0; cmd.opt.maxlen = 64; cmd.opt.buf = (NMUInt8*)optBuf; cmd.flags = T_NEGOTIATE; // Option management kinda sucks strcpy((char *) fooBuf, "Broadcast = 1"); status = OTCreateOptions(kRawIPName, (char **)&foo, &cmd.opt); */ status = OTOptionManagement(mEP, &cmd, &cmd); if (status) goto error; // Allocate the buffer for receiving the endpoint mIncomingData.udata.buf = (NMUInt8 *) InterruptSafe_alloc(info.tsdu); if (mIncomingData.udata.buf == NULL){ status = kNSpMemAllocationErr; goto error; } mIncomingData.udata.maxlen = info.tsdu; // Bind it request.addr.buf = NULL; request.addr.len = 0; request.addr.maxlen = 0; request.qlen = 0; status = OTBind(mEP, &request, NULL); if (status) goto error; OTSetNonBlocking(mEP); // Get our interface info (for the broadcast address) // Do this after we bind so that we know the interface is live if (! bGotInterfaceInfo) { status = OTInetGetInterfaceInfo(&mInterfaceInfo, kDefaultInetInterface); if (status) goto error; bGotInterfaceInfo = true; } // Install notifier status = OTInstallNotifier(mEP, mNotifier.fUPP, this); if (status) goto error; // Make is asynchronous status = OTSetAsynchronous(mEP); if (status) goto error; // Send out the query mEnumPeriod = 250; status = SendQuery(); error: if (status) { if (mEP) { OTCloseProvider(mEP); // ignore errors mEP = kOTInvalidEndpointRef; } } return status; }
PCSBSocket::PCSBSocket( unsigned short inPort, bool inServer) : mRemoteIP(0), mRemotePort(0), mLocalIP(0), mLocalPort(0), mMacSocket(NULL), mDone(false), mErr(false) { OSStatus err; mIncoming.addr.buf = (unsigned char *) &mIncomingIP; mIncoming.addr.len = mIncoming.addr.maxlen = sizeof(mIncomingIP); mIncoming.udata.buf = mIncoming.opt.buf = nil; mIncoming.udata.maxlen = mIncoming.udata.len = mIncoming.opt.len = mIncoming.opt.maxlen = 0; mIncomingIP.fAddressType = AF_INET; mIncomingIP.fPort = 0; mIncomingIP.fHost = 0; mIncoming.sequence = 0; mHasIncoming = false; mWorker = NULL; mMacSocket = ::OTOpenEndpointInContext( ::OTCreateConfiguration(kTCPName), 0, // Options must be 0 NULL, // Don't care about endpoint info &err, NULL); if (err != noErr) throw err; // If we're a server, reuse the port address... TOptMgmt optreq; TOption* Opt; UInt8 OptionBuf[kOTFourByteOptionSize]; Opt = (TOption*)OptionBuf; optreq.opt.buf = OptionBuf; optreq.opt.len = sizeof(OptionBuf); optreq.opt.maxlen = sizeof(OptionBuf); optreq.flags = T_NEGOTIATE; Opt->level = INET_IP; Opt->name = kIP_REUSEADDR; Opt->len = kOTFourByteOptionSize; *(UInt32*)Opt->value = true; ::OTOptionManagement(mMacSocket, &optreq, &optreq); // bind to a port immediately. InetAddress localAddress, realAddress; TBind req, ret; localAddress.fAddressType = AF_INET; localAddress.fPort = inPort; localAddress.fHost = kOTAnyInetAddress; req.addr.maxlen = req.addr.len = sizeof(localAddress); req.addr.buf = (unsigned char *) &localAddress; req.qlen = inServer ? 1 : 0; ret.addr.maxlen = sizeof(realAddress); ret.addr.buf = (unsigned char *) &realAddress; err = ::OTBind(mMacSocket, &req, &ret); if (err != noErr) { OTCloseProvider(mMacSocket); throw err; } mRemoteIP = realAddress.fHost; mRemotePort = realAddress.fPort; // Now that we are bound, go to async I/O and we will poll. ::OTSetAsynchronous(mMacSocket); }
/* Open a TCP network socket If 'remote' is NULL, this creates a local server socket on the given port, otherwise a TCP connection to the remote host and port is attempted. The newly created socket is returned, or NULL if there was an error. ( re-written by masahiro minami<*****@*****.**> Now endpoint is created in Async mode. 01/02/20 ) */ TCPsocket SDLNet_TCP_Open(IPaddress *ip) { EndpointRef dummy = NULL; TCPsocket sock = AsyncTCPNewSocket(); if( ! sock) return NULL; // Determin whether bind locally, or connect to remote if ( (ip->host != INADDR_NONE) && (ip->host != INADDR_ANY) ) { // ######## Connect to remote OTResult stat; InetAddress inAddr; TBind bindReq; // Open endpoint sock->error = OTAsyncOpenEndpoint( OTCreateConfiguration(kTCPName), NULL, &(sock->info), (OTNotifyProcPtr)(AsyncTCPNotifier), sock ); AsyncTCPPopEvent( sock ); while( !sock->error && !( sock->completion & CompleteMask(T_OPENCOMPLETE))) { //SetThreadState( kCurrentThreadID, kReadyThreadState, kNoThreadID ); //YieldToAnyThread(); //WaitNextEvent(everyEvent, &macEvent, 1, NULL); AsyncTCPPopEvent( sock ); } if( !sock->channel ) { SDLNet_SetError("OTAsyncOpenEndpoint failed --- client socket could not be opened"); goto error_return; } // Set blocking mode // I'm not sure if this is a good solution.... // Check out Apple's sample code, OT Virtual Server // ( 010314 masahiro minami<*****@*****.**>) sock->error = OTSetBlocking( sock->channel ); if( sock->error != kOTNoError ) { SDLNet_SetError("OTSetBlocking() returned an error"); goto error_return; } // Bind the socket OTInitInetAddress(&inAddr, 0, 0 ); bindReq.addr.len = sizeof( InetAddress ); bindReq.addr.buf = (unsigned char*)&inAddr; bindReq.qlen = 0; sock->error = OTBind( sock->channel, &bindReq, NULL ); AsyncTCPPopEvent(sock); while( !sock->error && !( sock->completion & CompleteMask(T_BINDCOMPLETE))) { //YieldToAnyThread(); //WaitNextEvent(everyEvent, &macEvent, 1, NULL); AsyncTCPPopEvent(sock); } switch( stat = OTGetEndpointState( sock->channel )) { InetAddress inAddr; TCall sndCall; OTResult res; case T_OUTCON: SDLNet_SetError("SDLNet_Open() failed -- T_OUTCON"); goto error_return; break; case T_IDLE: sock->readShutdown = false; sock->writeShutdown = false; sock->event &=~T_CONNECT; OTMemzero(&sndCall, sizeof(TCall)); OTInitInetAddress(&inAddr, ip->port, ip->host ); sndCall.addr.len = sizeof(InetAddress); sndCall.addr.buf = (unsigned char*)&inAddr; sock->connected = 0; res = OTConnect( sock->channel, &sndCall, NULL ); AsyncTCPPopEvent(sock); while( sock->error == kOTNoDataErr || !sock->connected ) AsyncTCPPopEvent(sock); break; default: // What's to be done ? (TODO) SDLNet_SetError("SDLNet_TCP_Open() failed -- EndpointState not good"); goto error_return; } if( !(sock->event & (T_CONNECT|T_DISCONNECT))) goto error_return; AsyncTCPPopEvent( sock ); while( !(sock->event & (T_CONNECT|T_DISCONNECT))) { AsyncTCPPopEvent( sock ); } // OTConnect successfull if( sock->event & T_CONNECT) { sock->remoteAddress.host = inAddr.fHost; sock->remoteAddress.port = inAddr.fPort; sock->sflag = false; } else { // OTConnect failed sock->event &= ~T_DISCONNECT; goto error_return; } } else { // ######## Bind locally TBind bindReq; InetAddress inAddr; // First, get InetInterfaceInfo. // I don't search for all of them. // Does that matter ? sock->error = OTAsyncOpenEndpoint( OTCreateConfiguration("tilisten, tcp"), NULL, &(sock->info), (OTNotifyProcPtr)(AsyncTCPNotifier), sock); AsyncTCPPopEvent( sock ); while( !sock->error && !( sock->completion & CompleteMask( T_OPENCOMPLETE))) { AsyncTCPPopEvent( sock ); } if( ! sock->channel ) { SDLNet_SetError("OTAsyncOpenEndpoint failed --- server socket could not be opened"); goto error_return; } // Create a master OTConfiguration sock->config = OTCreateConfiguration(kTCPName); if( ! sock->config ) { SDLNet_SetError("Could not create master OTConfiguration"); goto error_return; } // Bind the socket OTInitInetAddress(&inAddr, ip->port, 0 ); inAddr.fAddressType = AF_INET; bindReq.addr.len = sizeof( InetAddress ); bindReq.addr.buf = (unsigned char*)&inAddr; bindReq.qlen = 35; // This number is NOT well considered. (TODO) sock->localAddress.host = inAddr.fHost; sock->localAddress.port = inAddr.fPort; sock->sflag = true; sock->error = OTBind( sock->channel, &bindReq, NULL ); AsyncTCPPopEvent(sock); while( !sock->error && !( sock->completion & CompleteMask(T_BINDCOMPLETE))) { AsyncTCPPopEvent(sock); } if( sock->error != kOTNoError ) { SDLNet_SetError("Could not bind server socket"); goto error_return; } if( dummy ) OTCloseProvider( dummy ); } sock->ready = 0; return sock; error_return: if( dummy ) OTCloseProvider( dummy ); SDLNet_TCP_Close( sock ); return NULL; }
static OSStatus OpenTest(void) { OSStatus err; OSStatus junk; EndpointRef ep1; EndpointRef ep2; MapperRef map1; MapperRef map2; ATSvcRef at1; ATSvcRef at2; InetSvcRef inet1; InetSvcRef inet2; ProviderRef prov1; ProviderRef prov2; ep1 = NULL; ep2 = NULL; map1 = NULL; map2 = NULL; at1 = NULL; at2 = NULL; inet1 = NULL; inet2 = NULL; prov1 = NULL; prov2 = NULL; ep1 = OTOpenEndpointInContext(OTCreateConfiguration("tcp"), 0, NULL, &err, NULL); if (err == noErr) { err = OTAsyncOpenEndpointInContext(OTCreateConfiguration("tcp"), 0, NULL, DummyNotifier, &ep2, NULL); if (err == noErr) { while (ep2 == NULL) { SystemTask(); } if (ep2 == (void *) -1) { err = -1; } } } if (err == noErr) { map1 = OTOpenMapperInContext(OTCreateConfiguration("nbp"), 0, &err, NULL); } if (err == noErr) { err = OTAsyncOpenMapperInContext(OTCreateConfiguration("nbp"), 0, DummyNotifier, &map2, NULL); if (err == noErr) { while (map2 == NULL) { SystemTask(); } if (map2 == (void *) -1) { err = -1; } } } if (err == noErr) { inet1 = OTOpenInternetServicesInContext(kDefaultInternetServicesPath, 0, &err, NULL); } if (err == noErr) { err = OTAsyncOpenInternetServicesInContext(kDefaultInternetServicesPath, 0, DummyNotifier, &inet2, NULL); if (err == noErr) { while (inet2 == NULL) { SystemTask(); } if (inet2 == (void *) -1) { err = -1; } } } if (err == noErr) { at1 = OTOpenAppleTalkServicesInContext(kDefaultAppleTalkServicesPath, 0, &err, NULL); } if (err == noErr) { err = OTAsyncOpenAppleTalkServicesInContext(kDefaultAppleTalkServicesPath, 0, DummyNotifier, &at2, NULL); if (err == noErr) { while (at2 == NULL) { SystemTask(); } if (at2 == (void *) -1) { err = -1; } } } if (err == noErr) { prov1 = OTOpenProviderInContext(OTCreateConfiguration("enet"), 0, &err, NULL); } if (err == noErr) { err = OTAsyncOpenProviderInContext(OTCreateConfiguration("enet"), 0, DummyNotifier, &prov2, NULL); if (err == noErr) { while (prov2 == NULL) { SystemTask(); } if (prov2 == (void *) -1) { err = -1; } } } if (ep1 != NULL) { junk = OTCloseProvider(ep1); assert(junk == noErr); } if (ep2 != NULL) { junk = OTCloseProvider(ep2); assert(junk == noErr); } if (map1 != NULL) { junk = OTCloseProvider(map1); assert(junk == noErr); } if (map2 != NULL) { junk = OTCloseProvider(map2); assert(junk == noErr); } if (at1 != NULL) { junk = OTCloseProvider(at1); assert(junk == noErr); } if (at2 != NULL) { junk = OTCloseProvider(at2); assert(junk == noErr); } if (inet1 != NULL) { junk = OTCloseProvider(inet1); assert(junk == noErr); } if (inet2 != NULL) { junk = OTCloseProvider(inet2); assert(junk == noErr); } if (prov1 != NULL) { junk = OTCloseProvider(prov1); assert(junk == noErr); } if (prov2 != NULL) { junk = OTCloseProvider(prov2); assert(junk == noErr); } return err; }