initial-darwin-support-20010327
[openafs.git] / src / rx / rx_kcommon.c
index 005664d..a07dad1 100644 (file)
@@ -423,7 +423,7 @@ register struct rx_peer *pp;
  */
 
 
-#if ! defined(AFS_AIX_ENV) && ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(AFS_LINUX20_ENV)
+#if ! defined(AFS_AIX_ENV) && ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(AFS_LINUX20_ENV) && !defined (AFS_DARWIN_ENV)
 /* Routine called during the afsd "-shutdown" process to put things back to
  * the initial state.
  */
@@ -548,7 +548,7 @@ afs_int32 rxi_Findcbi(addr)
 
 #else /* AFS_USERSPACE_IP_ADDR */
 
-#if !defined(AFS_AIX41_ENV) && !defined(AFS_DUX40_ENV)
+#if !defined(AFS_AIX41_ENV) && !defined(AFS_DUX40_ENV) && !defined(AFS_DARWIN_ENV)
 #define IFADDR2SA(f) (&((f)->ifa_addr))
 #else /* AFS_AIX41_ENV */
 #define IFADDR2SA(f) ((f)->ifa_addr)
@@ -569,10 +569,20 @@ int rxi_GetIFInfo()
     bzero(addrs, sizeof(addrs));
     bzero(mtus, sizeof(mtus));
 
+#ifdef AFS_DARWIN_ENV
+    TAILQ_FOREACH(ifn, &ifnet, if_link) {
+      if (i >= ADDRSPERSITE) break;
+#else 
     for (ifn = ifnet; ifn != NULL && i < ADDRSPERSITE; ifn = ifn->if_next) {
+#endif
       rxmtu = (ifn->if_mtu - RX_IPUDP_SIZE);
+#ifdef AFS_DARWIN_ENV
+      TAILQ_FOREACH(ifad, &ifn->if_addrhead, ifa_link) {
+      if (i >= ADDRSPERSITE) break;
+#else
       for (ifad = ifn->if_addrlist; ifad != NULL && i < ADDRSPERSITE;
           ifad = ifad->ifa_next){
+#endif
        if (IFADDR2SA(ifad)->sa_family == AF_INET) {
          ifinaddr = ntohl(((struct sockaddr_in *) IFADDR2SA(ifad))->sin_addr.s_addr);
          if (myNetAddrs[i] != ifinaddr) { 
@@ -626,14 +636,26 @@ rxi_FindIfnet(addr, pifad)
 
   /* if we're given an address, skip everything until we find it */
   if (!*pifad)
+#ifdef AFS_DARWIN_ENV
+    *pifad = TAILQ_FIRST(&in_ifaddrhead);
+#else 
     *pifad = in_ifaddr;
+#endif
   else {
     if (((ppaddr & (*pifad)->ia_subnetmask) == (*pifad)->ia_subnet))
       match_value = 2; /* don't find matching nets, just subnets */
+#ifdef AFS_DARWIN_ENV
+    *pifad = TAILQ_NEXT(*pifad, ia_link);
+#else   
     *pifad = (*pifad)->ia_next;
+#endif
   }
     
+#ifdef AFS_DARWIN_ENV
+  for (ifa = *pifad; ifa; ifa = TAILQ_NEXT(ifa, ia_link) ) {
+#else
   for (ifa = *pifad; ifa; ifa = ifa->ia_next ) {
+#endif
     if ((ppaddr & ifa->ia_netmask) == ifa->ia_net) {
       if ((ppaddr & ifa->ia_subnetmask) == ifa->ia_subnet) {
        sin=IA_SIN(ifa);
@@ -691,6 +713,9 @@ struct osi_socket *rxk_NewSocket(short aport)
 #endif
 
     AFS_STATCNT(osi_NewSocket);
+#if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
+    thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
+#endif
 #if    defined(AFS_HPUX102_ENV)
 #if     defined(AFS_HPUX110_ENV)
     /* blocking socket */
@@ -705,7 +730,7 @@ struct osi_socket *rxk_NewSocket(short aport)
     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0);
 #endif /* AFS_SGI65_ENV */
 #endif /* AFS_HPUX102_ENV */
-    if (code) return (struct osi_socket *) 0;
+    if (code) goto bad;
 
     myaddr.sin_family = AF_INET;
     myaddr.sin_port = aport;
@@ -715,7 +740,7 @@ struct osi_socket *rxk_NewSocket(short aport)
     bindnam = allocb_wait((addrsize+SO_MSGOFFSET+1), BPRI_MED);
     if (!bindnam) {
        setuerror(ENOBUFS);
-       return(struct osi_socket *) 0;
+       goto bad;
     }
     bcopy((caddr_t)&myaddr, (caddr_t)bindnam->b_rptr+SO_MSGOFFSET, addrsize);
     bindnam->b_wptr = bindnam->b_rptr + (addrsize+SO_MSGOFFSET+1);
@@ -724,7 +749,7 @@ struct osi_socket *rxk_NewSocket(short aport)
     if (code) {
        soclose(newSocket);
        m_freem(nam);
-       return(struct osi_socket *) 0;
+       goto bad;
     }
 
     freeb(bindnam);
@@ -735,6 +760,15 @@ struct osi_socket *rxk_NewSocket(short aport)
        if (code)
            osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
     }
+#ifdef AFS_DARWIN_ENV
+    myaddr.sin_len = sizeof(myaddr);
+    code = sobind(newSocket, (struct sockaddr *)&myaddr);
+    if (code) {
+        printf("sobind fails\n");
+        soclose(newSocket);
+        goto bad;
+    }
+#else
 #ifdef  AFS_OSF_ENV
     nam = m_getclr(M_WAIT, MT_SONAME);
 #else   /* AFS_OSF_ENV */
@@ -744,7 +778,7 @@ struct osi_socket *rxk_NewSocket(short aport)
 #if !defined(AFS_SUN5_ENV) && !defined(AFS_OSF_ENV) && !defined(AFS_SGI64_ENV)
        setuerror(ENOBUFS);
 #endif
-       return (struct osi_socket *) 0;
+       goto bad;
     }
     nam->m_len = sizeof(myaddr);
 #ifdef  AFS_OSF_ENV
@@ -763,11 +797,21 @@ struct osi_socket *rxk_NewSocket(short aport)
 #ifndef AFS_SGI65_ENV
        m_freem(nam);
 #endif
-       return (struct osi_socket *) 0;
+       goto bad;
     }
+#endif /* else AFS_DARWIN_ENV */
 #endif /* else AFS_HPUX110_ENV */
 
+#if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
+    thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
+#endif
     return (struct osi_socket *) newSocket;
+
+bad:
+#if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
+    thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
+#endif
+    return (struct osi_socket *) 0;
 }
 
 
@@ -776,7 +820,13 @@ int rxk_FreeSocket(asocket)
     register struct socket *asocket;
 {
     AFS_STATCNT(osi_FreeSocket);
+#if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
+    thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
+#endif
     soclose(asocket);
+#if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
+    thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
+#endif
     return 0;
 }
 #endif /* !SUN5 && !LINUX20 */
@@ -938,6 +988,9 @@ void rxk_Listener(void)
 #ifdef AFS_SUN5_ENV
     rxk_ListenerPid = ttoproc(curthread)->p_pidp->pid_id;
 #endif /* AFS_SUN5_ENV */
+#ifdef AFS_DARWIN_ENV
+    rxk_ListenerPid = current_proc()->p_pid;
+#endif
 #if defined(RX_ENABLE_LOCKS) && !defined(AFS_SUN5_ENV)
     AFS_GUNLOCK();
 #endif /* RX_ENABLE_LOCKS && !AFS_SUN5_ENV */
@@ -977,7 +1030,7 @@ void rxk_Listener(void)
 #endif /* AFS_SUN5_ENV */
 }
 
-#if !defined(AFS_LINUX20_ENV) && !defined(AFS_SUN5_ENV)
+#if !defined(AFS_LINUX20_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_DARWIN_ENV)
 /* The manner of stopping the rx listener thread may vary. Most unix's should
  * be able to call soclose.
  */