PR_IMPLEMENT(PRStatus) _MD_gethostname(char *name, int namelen) { OSStatus err; InetInterfaceInfo info; /* * On a Macintosh, we donÕt have the concept of a local host name. * We do though have an IP address & everyone should be happy with * a string version of that for a name. * The alternative here is to ping a local DNS for our name, they * will often know it. This is the cheap, easiest, and safest way out. */ /* Make sure the string is as long as the longest possible address */ if (namelen < strlen("123.123.123.123")) { err = kEINVALErr; goto ErrorExit; } err = OTInetGetInterfaceInfo(&info, kDefaultInetInterface); if (err != kOTNoError) goto ErrorExit; OTInetHostToString(info.fAddress, name); return PR_SUCCESS; ErrorExit: macsock_map_error(err); return PR_FAILURE; }
/* Local functions for initializing and cleaning up the DNS resolver */ static int OpenDNS(void) { int retval; OSStatus status; retval = 0; status = OTAsyncOpenInternetServices( kDefaultInternetServicesPath, 0, OpenDNSNotifier, NULL); if ( status == noErr ) { InetInterfaceInfo info; dnsStatus.stat = dnsNotReady; while( dnsStatus.stat != dnsError && dnsStatus.dns == NULL) { // what's to be done ? Yield ? WaitNextEvent ? or what ? // ( 010311 masahiro minami<*****@*****.**> ) //YieldToAnyThread(); } /* Get the address of the local system - What should it be if ethernet is off? */ OTInetGetInterfaceInfo(&info, kDefaultInetInterface); OTlocalhost = info.fAddress; } else { SDLNet_SetError("Unable to open DNS handle"); retval = status; } return(retval); }
void OTIPEndpoint::MakeEnumerationResponse(void) { DEBUG_ENTRY_EXIT("OTIPEndpoint::MakeEnumerationResponse"); mEnumerationResponseLen = sizeof(IPEnumerationResponsePacket) + mConfig.customEnumDataLen; // in case we created one from a previous listen... if (mEnumerationResponseData) delete[] mEnumerationResponseData; mEnumerationResponseData = (NMUInt8 *)new char[mEnumerationResponseLen]; IPEnumerationResponsePacket *theResponse = (IPEnumerationResponsePacket *) mEnumerationResponseData; InetInterfaceInfo info; NMErr status; TNetbuf addr; NMSInt16 len; addr = mStreamEndpoint->mLocalAddress; status = OTInetGetInterfaceInfo(&info, kDefaultInetInterface); if (status == kNMNoError) ((InetAddress *)addr.buf)->fHost = info.fAddress; len = build_ip_enumeration_response_packet((char *) theResponse, mConfig.gameID, kVersion, info.fAddress, ((InetAddress *)addr.buf)->fPort, mConfig.name, mConfig.customEnumDataLen, mConfig.customEnumData); op_assert(len <= mEnumerationResponseLen); }
void TCPLow_Create(TCPINSTANCE* pTcp, u_short* port, TCPPEER* peer, int* status) { TBind boundAddr; InetAddress boundInetAddr; InetInterfaceInfo inetInfo; OSStatus err; pTcp->isValid = false; // Initialize OpenTransport err = InitOpenTransport(); if (err != noErr) { *status = comm_STATUS_BAD; return; } // initialize Internet Services pTcp->inetService = OTOpenInternetServices(kDefaultInternetServicesPath, 0, &err); if (err != noErr) { *status = comm_STATUS_BAD; return; } // open an endpoint for sending and recieving data err = CreateAndConfigUDP(&pTcp->udpEndpoint); if (err != noErr) { ShutDownUDP(pTcp->udpEndpoint); *status = comm_STATUS_BAD; return; } pTcp->isValid = true; // Get information about Internet err = OTInetGetInterfaceInfo(&inetInfo, kDefaultInetInterface); if (err != noErr) { //DebugStr("\pCannot Get Information About Default Interface"); *status = comm_STATUS_BAD; ShutDownUDP(pTcp->udpEndpoint); return; } boundAddr.addr.maxlen = sizeof(boundInetAddr); boundAddr.addr.buf = (unsigned char*) &boundInetAddr;; err = OTGetProtAddress(pTcp->udpEndpoint,&boundAddr,NULL); if (err != noErr) { *status = comm_STATUS_BAD; ShutDownUDP(pTcp->udpEndpoint); return; } peer->addr = inetInfo.fAddress; peer->port = *port = boundInetAddr.fPort; pTcp->myHandle = dcstReplace(pTcp->handles,pTcp->myHandle,peer); }
/* ifAddr * * Returns the internet IP address of the default endpoint * which is configured for this system. In most all cases * there will be only one physical interface, and most hosts will * only be assigned a single IP address, but if that is not the case * then it is possible that this call could return the information * for the wrong interface. */ UInt32 ifAddr(void) { OSStatus err; InetInterfaceInfo info; err = OTInetGetInterfaceInfo ( &info, kDefaultInetInterface); if (err != noErr){ return(0); } return( info.fAddress); }
GAddress *GSocket_GetLocal(GSocket *socket) { GAddress *address = NULL ; GSocketError err; InetAddress loc ; assert(socket != NULL); /* try to get it from the m_local var first */ if (socket->m_local) return GAddress_copy(socket->m_local); /* else, if the socket is initialized, try getsockname */ if (socket->m_endpoint == kOTInvalidEndpointRef) { socket->m_error = GSOCK_INVSOCK; return NULL; } /* 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 method to use right now */ { InetInterfaceInfo info; OTInetGetInterfaceInfo(&info, kDefaultInetInterface); loc.fHost = info.fAddress ; loc.fPort = 0 ; loc.fAddressType = AF_INET ; } /* got a valid address from getsockname, create a GAddress object */ address = GAddress_new(); if (address == NULL) { socket->m_error = GSOCK_MEMERR; return NULL; } err = _GAddress_translate_from(address, &loc); if (err != GSOCK_NOERROR) { GAddress_destroy(address); socket->m_error = err; return NULL; } return address; }
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; }
unsigned long gethostid( void) { OSStatus theErr = kOTNoError; InetInterfaceInfo info; extern EndpointRef gDNRep; // open or get the current resolver reference if( gDNRep == kOTInvalidEndpointRef){ theErr = ot_DNRInit(); if( theErr != kOTNoError){ ncbi_SetErrno( theErr); return 0; } } theErr = OTInetGetInterfaceInfo( &info, kDefaultInetInterface); if(theErr != kOTNoError){ ncbi_SetErrno( theErr); return 0; } return info.fAddress; }
/* 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); }
int commInit( commInitReq_t * req, // Request (or NULL) commInitResp_t * resp) // Response (or NULL) { commInitReq_t reqDummy; commInitResp_t respDummy; ip_adr_t adr_broadcast; ip_adr_t *padr; OSStatus err; extern Str255 gSavedSelection; // DebugStr ( "\pcommInit()" ); DPRINT(("@TRUMP commInit(): ")); if (req == NULL) req = (commInitReq_t *)memset(&reqDummy, 0, sizeof(*req)); if (resp == NULL) resp = &respDummy; scratch_flat = malloc(MAX_RAW_PKTLEN); //scratch_flat = my_dos_malloc(MAX_RAW_PKTLEN, &scratch_seg, &scratch_off, &scratch_selector); if (!scratch_flat) { DPRINT(("commInit: couldn't allocate DOS memory!\n")); resp->status = comm_STATUS_BAD; return FALSE; } // check for the existanct of OpenTransport if(!OpenTransportExists() || !OpenTransportInetExists()) { //DebugStr("\pOpen Transport Does Not Exist"); DPRINT(("commInit: UDP ABI not found\n")); resp->status = comm_STATUS_NETWORK_NOT_PRESENT; free(scratch_flat); return FALSE; } // Initialize OpenTransport err = InitOpenTransport(); if (err != noErr) { resp->status = comm_STATUS_BAD; free(scratch_flat); return FALSE; } // initialize Internet Services gInetService = OTOpenInternetServices(kDefaultInternetServicesPath, 0, &err); if (err != noErr) { resp->status = comm_STATUS_BAD; free(scratch_flat); return FALSE; } // open an endpoint for sending and recieving data err = CreateAndConfigUDP(&gUDPEndpoint); if (err != noErr) { resp->status = comm_STATUS_BAD; free(scratch_flat); ShutDownUDP(); return FALSE; } // create the peer table peertab = assoctab_create(sizeof(ip_adr_t)); if (!peertab) { // ABORT! Out of Memory resp->status = comm_STATUS_BAD; free(scratch_flat); ShutDownUDP(); return FALSE; } // Get information about the Internet err = OTInetGetInterfaceInfo(&gInetInfo, kDefaultInetInterface); if (err != noErr) { //DebugStr("\pCannot Get Information About Default Interface"); resp->status = comm_STATUS_BAD; free(scratch_flat); ShutDownUDP(); return FALSE; } // Store our address in the peer table under the bogus ME handle padr = (ip_adr_t *)assoctab_subscript_grow(peertab, trump_HDL_ME); if (!padr) { DPRINT(("commInit: couldn't grow peer table\n")); resp->status = comm_STATUS_BAD; free(scratch_flat); assoctab_destroy(peertab); ShutDownUDP(); return FALSE; } dprint_peertab(peertab); DPRINT(("commInit: saving handle %d adr %x at %p\n", trump_HDL_ME, gInetInfo.fAddress, padr)); memcpy(padr, &gInetInfo.fAddress, sizeof(ip_adr_t)); dprint_peertab(peertab); // Open a handle good for receiving packets on the standard port // SRC = (*,*) // DEST = (localhost, pt->port) adr_broadcast = 0xffffffff; hdl_rx = trump_adr2hdl(adr_broadcast, SOCKET_MW2, SOCKET_MW2, TRUE); if (hdl_rx == trump_HDL_NONE) { DPRINT(("commInit: couldn't open handle for listening\n")); resp->status = comm_STATUS_BUSY; free(scratch_flat); assoctab_destroy(peertab); ShutDownUDP(); return FALSE; } /* if ((req->flags & comm_INIT_FLAGS_RESUME) == 0) { if (!DoHostListDialog()) { resp->status = comm_STATUS_EMPTY; free(scratch_flat); assoctab_destroy(peertab); ShutDownUDP(); return FALSE; } } else { // we need to load the last string selected when we are resuming OpenPrefsFile(); p2cstr(gSavedSelection); // the apps like C-strings ClosePrefsFile(); } */ // initialize our address list to nothing InitAddressList(); // add our own address to the beginning of the list (it MUST Be the first // address in our list - we broadcast to all _other_ addresses in our list) AddAddressToList(gInetInfo.fAddress); // add the address from the dialog to our broadcast list if the user chose one if (gSavedSelection[0] != 0) { InetHostInfo theHostInfo; OTSetSynchronous(gInetService); err = OTInetStringToAddress(gInetService, gSavedSelection, &theHostInfo); OTSetAsynchronous(gInetService); if (err == noErr) { AddAddressToList(theHostInfo.addrs[0]); } } resp->status = comm_STATUS_OK; DPRINT(("commInit: success\n")); return TRUE; }
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; }