Merged changes made to HEAD branch. unlabeled-3.9.20
authorcls%seawood.org
Sun, 12 Sep 1999 01:09:35 +0000
branchunlabeled-3.9.20
changeset 806 d0d07fcf438cc720bb8092507bbe6c6c193bace6
parent 772 7c64be6a43645d8e6f938c937e25f1e141f17c1a
push idunknown
push userunknown
push dateunknown
Merged changes made to HEAD branch.
pr/src/md/mac/macsockotpt.c
--- a/pr/src/md/mac/macsockotpt.c
+++ b/pr/src/md/mac/macsockotpt.c
@@ -37,16 +37,17 @@ typedef enum SndRcvOpCode {
 
 static struct {
 	PRLock *    lock;
 	InetSvcRef  serviceRef;
 	PRThread *  thread;
 	void *      cookie;
 } dnsContext;
 
+static PRBool gOTInitialized;
 
 static pascal void  DNSNotifierRoutine(void * contextPtr, OTEventCode code, OTResult result, void * cookie);
 static pascal void  NotifierRoutine(void * contextPtr, OTEventCode code, 
             OTResult result, void * cookie);
 
 static PRBool GetState(PRFileDesc *fd, PRBool *readReady, PRBool *writeReady, PRBool *exceptReady);
 
 extern void WaitOnThisThread(PRThread *thread, PRIntervalTime timeout);
@@ -106,29 +107,38 @@ void _MD_InitNetAccess()
     PR_ASSERT(err == kOTNoError);
 
 	dnsContext.lock = PR_NewLock();
 	PR_ASSERT(dnsContext.lock != NULL);
 
 	dnsContext.thread = _PR_MD_CURRENT_THREAD();
 	dnsContext.cookie = NULL;
 	
+	gOTInitialized = PR_FALSE;
+	
+/* XXX Does not handle absence of open tpt and tcp yet! */
+}
+
+static void _MD_FinishInitNetAccess()
+{
+    OSStatus    errOT;
+
     dnsContext.serviceRef = OT_OPEN_INTERNET_SERVICES(kDefaultInternetServicesPath, NULL, &errOT);
     if (errOT != kOTNoError) return;    /* no network -- oh well */
     PR_ASSERT((dnsContext.serviceRef != NULL) && (errOT == kOTNoError));
 
     /* Install notify function for DNR Address To String completion */
     errOT = OTInstallNotifier(dnsContext.serviceRef, DNS_NOTIFIER_ROUTINE, &dnsContext);
     PR_ASSERT(errOT == kOTNoError);
 
     /* Put us into async mode */
     errOT = OTSetAsynchronous(dnsContext.serviceRef);
     PR_ASSERT(errOT == kOTNoError);
-
-/* XXX Does not handle absence of open tpt and tcp yet! */
+    
+    gOTInitialized = PR_TRUE;
 }
 
 
 pascal void  DNSNotifierRoutine(void * contextPtr, OTEventCode code, OTResult result, void * cookie)
 {
 #pragma unused(contextPtr)
     _PRCPU *    cpu    = _PR_MD_CURRENT_CPU(); 
 	
@@ -411,16 +421,19 @@ ErrorExit:
 // Errors returned:
 // kOTXXXX - OT returned error
 // EPROTONOSUPPORT - bad socket type/protocol
 // ENOBUFS - not enough space for another socket, or failure in socket creation routine
 PRInt32 _MD_socket(int domain, int type, int protocol)
 {
     OSStatus    err;
     EndpointRef endpoint;
+    
+    if (!gOTInitialized)
+    	_MD_FinishInitNetAccess();
 
     // We only deal with internet domain
     if (domain != AF_INET) {
         err = kEPROTONOSUPPORTErr;
         goto ErrorExit;
     }
     
     // We only know about tcp & udp
@@ -1581,16 +1594,19 @@ PR_IMPLEMENT(PRStatus) _MD_getpeername(P
 }                               
 
 
 PR_IMPLEMENT(unsigned long) inet_addr(const char *cp)
 {
     OSStatus err;
     InetHost host;    
 
+    if (!gOTInitialized)
+    	_MD_FinishInitNetAccess();
+
     err = OTInetStringToHost((char*) cp, &host);
     PR_ASSERT(err == kOTNoError);
     
     return host;
 }
 
 
 static char *sAliases[1] = {NULL};
@@ -1600,16 +1616,19 @@ static InetHost *sAddresses[kMaxHostAddr
 
 
 PR_IMPLEMENT(struct hostent *) gethostbyname(const char * name)
 {
     OSStatus err;
     PRUint32 index;
     PRThread *me = _PR_MD_CURRENT_THREAD();
 
+    if (!gOTInitialized)
+    	_MD_FinishInitNetAccess();
+
     me->io_pending       = PR_TRUE;
     me->io_fd            = NULL;
     me->md.osErrCode     = noErr;
 	
 	PR_Lock(dnsContext.lock);	// so we can safely store our thread ptr in dnsContext
 	dnsContext.thread = me;		// so we know what thread to wake up when OTInetStringToAddress completes
 
     err = OTInetStringToAddress(dnsContext.serviceRef, (char *)name, &sHostInfo);
@@ -1638,35 +1657,44 @@ ErrorExit:
 }
 
 
 PR_IMPLEMENT(struct hostent *) gethostbyaddr(const void *addr, int addrlen, int type)
 {
     PR_ASSERT(type == AF_INET);
     PR_ASSERT(addrlen == sizeof(struct in_addr));
 
+    if (!gOTInitialized)
+    	_MD_FinishInitNetAccess();
+
     OTInetHostToString((InetHost)addr, sHostInfo.name);
     
     return (gethostbyname(sHostInfo.name));
 }
 
 
 PR_IMPLEMENT(char *) inet_ntoa(struct in_addr addr)
 {
+    if (!gOTInitialized)
+    	_MD_FinishInitNetAccess();
+
     OTInetHostToString((InetHost)addr.s_addr, sHostInfo.name);
     
     return sHostInfo.name;
 }
 
 
 PRStatus _MD_gethostname(char *name, int namelen)
 {
     OSStatus err;
     InetInterfaceInfo info;
 
+    if (!gOTInitialized)
+    	_MD_FinishInitNetAccess();
+
     /*
      *    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.
      */