Remove some more warnings from rx
[openafs.git] / src / rx / rx_kcommon.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * rx_kcommon.c - Common kernel RX code for all system types.
12  */
13
14 #include <afsconfig.h>
15 #include "afs/param.h"
16
17
18 #include "rx/rx_kcommon.h"
19
20 #ifdef AFS_HPUX110_ENV
21 #include "h/tihdr.h"
22 #include <xti.h>
23 #endif
24 #include "afsint.h"
25
26 #ifndef RXK_LISTENER_ENV
27 int (*rxk_PacketArrivalProc) (struct rx_packet * ahandle, struct sockaddr_in * afrom, struct socket *arock, afs_int32 asize);   /* set to packet allocation procedure */
28 int (*rxk_GetPacketProc) (struct rx_packet **ahandle, int asize);
29 #endif
30
31 osi_socket *rxk_NewSocketHost(afs_uint32 ahost, short aport);
32 extern struct interfaceAddr afs_cb_interface;
33
34 rxk_ports_t rxk_ports;
35 rxk_portRocks_t rxk_portRocks;
36
37 int rxk_initDone = 0;
38
39 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
40 #define ADDRSPERSITE 16
41 static afs_uint32 myNetAddrs[ADDRSPERSITE];
42 static int myNetMTUs[ADDRSPERSITE];
43 static int numMyNetAddrs = 0;
44 #endif
45
46 #if defined(AFS_DARWIN80_ENV)
47 #define sobind sock_bind
48 #define soclose sock_close
49 #endif
50
51 /* add a port to the monitored list, port # is in network order */
52 static int
53 rxk_AddPort(u_short aport, char *arock)
54 {
55     int i;
56     unsigned short *tsp, ts;
57     int zslot;
58
59     zslot = -1;                 /* look for an empty slot simultaneously */
60     for (i = 0, tsp = rxk_ports; i < MAXRXPORTS; i++, tsp++) {
61         if (((ts = *tsp) == 0) && (zslot == -1))
62             zslot = i;
63         if (ts == aport) {
64             return 0;
65         }
66     }
67     /* otherwise allocate a new port slot */
68     if (zslot < 0)
69         return E2BIG;           /* all full */
70     rxk_ports[zslot] = aport;
71     rxk_portRocks[zslot] = arock;
72     return 0;
73 }
74
75 /* remove as port from the monitored list, port # is in network order */
76 int
77 rxk_DelPort(u_short aport)
78 {
79     int i;
80     unsigned short *tsp;
81
82     for (i = 0, tsp = rxk_ports; i < MAXRXPORTS; i++, tsp++) {
83         if (*tsp == aport) {
84             /* found it, adjust ref count and free the port reference if all gone */
85             *tsp = 0;
86             return 0;
87         }
88     }
89     /* otherwise port not found */
90     return ENOENT;
91 }
92
93 void
94 rxk_shutdownPorts(void)
95 {
96     int i;
97     for (i = 0; i < MAXRXPORTS; i++) {
98         if (rxk_ports[i]) {
99             rxk_ports[i] = 0;
100 #if ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(RXK_LISTENER_ENV)
101             soclose((struct socket *)rxk_portRocks[i]);
102 #endif
103             rxk_portRocks[i] = NULL;
104         }
105     }
106 }
107
108 osi_socket
109 rxi_GetHostUDPSocket(u_int host, u_short port)
110 {
111     osi_socket *sockp;
112     sockp = (osi_socket *)rxk_NewSocketHost(host, port);
113     if (sockp == (osi_socket *)0)
114         return OSI_NULLSOCKET;
115     rxk_AddPort(port, (char *)sockp);
116     return (osi_socket) sockp;
117 }
118
119 osi_socket
120 rxi_GetUDPSocket(u_short port)
121 {
122     return rxi_GetHostUDPSocket(htonl(INADDR_ANY), port);
123 }
124
125 #if !defined(AFS_LINUX26_ENV)
126 void
127 #ifdef AFS_AIX_ENV
128 osi_Panic(char *msg, void *a1, void *a2, void *a3)
129 #else
130 osi_Panic(char *msg, ...)
131 #endif
132 {
133 #ifdef AFS_AIX_ENV
134     if (!msg)
135         msg = "Unknown AFS panic";
136     printf(msg, a1, a2, a3);
137     panic(msg);
138 #elif (defined(AFS_DARWIN80_ENV) && !defined(AFS_DARWIN90_ENV)) || (defined(AFS_LINUX22_ENV) && !defined(AFS_LINUX_26_ENV))
139     char buf[256];
140     va_list ap;
141     if (!msg)
142         msg = "Unknown AFS panic";
143
144     va_start(ap, msg);
145     vsnprintf(buf, sizeof(buf), msg, ap);
146     va_end(ap);
147     printf(buf);
148     panic(buf);
149 #else
150     va_list ap;
151     if (!msg)
152         msg = "Unknown AFS panic";
153
154     va_start(ap, msg);
155     vprintf(msg, ap);
156     va_end(ap);
157 # ifdef AFS_LINUX20_ENV
158     * ((char *) 0) = 0; 
159 # else
160     panic(msg);
161 # endif
162 #endif
163 }
164
165 /*
166  * osi_utoa() - write the NUL-terminated ASCII decimal form of the given
167  * unsigned long value into the given buffer.  Returns 0 on success,
168  * and a value less than 0 on failure.  The contents of the buffer is
169  * defined only on success.
170  */
171
172 int
173 osi_utoa(char *buf, size_t len, unsigned long val)
174 {
175     long k;                     /* index of first byte of string value */
176
177     /* we definitely need room for at least one digit and NUL */
178
179     if (len < 2) {
180         return -1;
181     }
182
183     /* compute the string form from the high end of the buffer */
184
185     buf[len - 1] = '\0';
186     for (k = len - 2; k >= 0; k--) {
187         buf[k] = val % 10 + '0';
188         val /= 10;
189
190         if (val == 0)
191             break;
192     }
193
194     /* did we finish converting val to string form? */
195
196     if (val != 0) {
197         return -2;
198     }
199
200     /* this should never happen */
201
202     if (k < 0) {
203         return -3;
204     }
205
206     /* this should never happen */
207
208     if (k >= len) {
209         return -4;
210     }
211
212     /* if necessary, relocate string to beginning of buf[] */
213
214     if (k > 0) {
215
216         /*
217          * We need to achieve the effect of calling
218          *
219          * memmove(buf, &buf[k], len - k);
220          *
221          * However, since memmove() is not available in all
222          * kernels, we explicitly do an appropriate copy.
223          */
224
225         char *dst = buf;
226         char *src = buf + k;
227
228         while ((*dst++ = *src++) != '\0')
229             continue;
230     }
231
232     return 0;
233 }
234
235 /*
236  * osi_AssertFailK() -- used by the osi_Assert() macro.
237  *
238  * It essentially does
239  *
240  * osi_Panic("assertion failed: %s, file: %s, line: %d", expr, file, line);
241  *
242  * Since the kernel version of osi_Panic() only passes its first
243  * argument to the native panic(), we construct a single string and hand
244  * that to osi_Panic().
245  */
246 void
247 osi_AssertFailK(const char *expr, const char *file, int line)
248 {
249     static const char msg0[] = "assertion failed: ";
250     static const char msg1[] = ", file: ";
251     static const char msg2[] = ", line: ";
252     static const char msg3[] = "\n";
253
254     /*
255      * These buffers add up to 1K, which is a pleasantly nice round
256      * value, but probably not vital.
257      */
258     char buf[1008];
259     char linebuf[16];
260
261     /* check line number conversion */
262
263     if (osi_utoa(linebuf, sizeof linebuf, line) < 0) {
264         osi_Panic("osi_AssertFailK: error in osi_utoa()\n");
265     }
266
267     /* okay, panic */
268
269 #define ADDBUF(BUF, STR)                                        \
270         if (strlen(BUF) + strlen((char *)(STR)) + 1 <= sizeof BUF) {    \
271                 strcat(BUF, (char *)(STR));                             \
272         }
273
274     buf[0] = '\0';
275     ADDBUF(buf, msg0);
276     ADDBUF(buf, expr);
277     ADDBUF(buf, msg1);
278     ADDBUF(buf, file);
279     ADDBUF(buf, msg2);
280     ADDBUF(buf, linebuf);
281     ADDBUF(buf, msg3);
282
283 #undef ADDBUF
284
285     osi_Panic(buf);
286 }
287 #else
288 void
289 osi_AssertFailK(const char *expr, const char *file, int line)
290 {
291     printk(KERN_CRIT "assertion failed: %s, file: %s, line: %d\n", expr, file, line);
292 }
293 #endif
294
295 #ifndef UKERNEL
296 /* This is the server process request loop. Kernel server
297  * processes never become listener threads */
298 void *
299 rx_ServerProc(void *unused)
300 {
301     int threadID;
302
303 /* jaltman - rxi_dataQuota is protected by a mutex everywhere else */
304     rxi_MorePackets(rx_maxReceiveWindow + 2);   /* alloc more packets */
305     rxi_dataQuota += rx_initSendWindow; /* Reserve some pkts for hard times */
306     /* threadID is used for making decisions in GetCall.  Get it by bumping
307      * number of threads handling incoming calls */
308     threadID = rxi_availProcs++;
309
310 #ifdef RX_ENABLE_LOCKS
311     AFS_GUNLOCK();
312 #endif /* RX_ENABLE_LOCKS */
313     rxi_ServerProc(threadID, NULL, NULL);
314 #ifdef RX_ENABLE_LOCKS
315     AFS_GLOCK();
316 #endif /* RX_ENABLE_LOCKS */
317
318     return NULL;
319 }
320 #endif /* !UKERNEL */
321
322 #ifndef RXK_LISTENER_ENV
323 /* asize includes the Rx header */
324 static int
325 MyPacketProc(struct rx_packet **ahandle, int asize)
326 {
327     struct rx_packet *tp;
328
329     /* If this is larger than we expected, increase rx_maxReceiveDataSize */
330     /* If we can't scrounge enough cbufs, then we have to drop the packet,
331      * but we should set a flag so we magic up some more at our leisure.
332      */
333
334     if ((asize >= 0) && (asize <= RX_MAX_PACKET_SIZE)) {
335         tp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
336         if (tp && (tp->length + RX_HEADER_SIZE) < asize) {
337             if (0 <
338                 rxi_AllocDataBuf(tp, asize - (tp->length + RX_HEADER_SIZE),
339                                  RX_PACKET_CLASS_RECV_CBUF)) {
340                 rxi_FreePacket(tp);
341                 tp = NULL;
342                 if (rx_stats_active) {
343                     MUTEX_ENTER(&rx_stats_mutex);
344                     rx_stats.noPacketBuffersOnRead++;
345                     MUTEX_EXIT(&rx_stats_mutex);
346                 }
347             }
348         }
349     } else {
350         /*
351          * XXX if packet is too long for our buffer,
352          * should do this at a higher layer and let other
353          * end know we're losing.
354          */
355         if (rx_stats_active) {
356             MUTEX_ENTER(&rx_stats_mutex);
357             rx_stats.bogusPacketOnRead++;
358             MUTEX_EXIT(&rx_stats_mutex);
359         }
360         /* I DON"T LIKE THIS PRINTF -- PRINTFS MAKE THINGS VERY VERY SLOOWWW */
361         dpf(("rx: packet dropped: bad ulen=%d\n", asize));
362         tp = NULL;
363     }
364
365     if (!tp)
366         return -1;
367     /* otherwise we have a packet, set appropriate values */
368     *ahandle = tp;
369     return 0;
370 }
371
372 static int
373 MyArrivalProc(struct rx_packet *ahandle,
374               struct sockaddr_in *afrom,
375               struct socket *arock,
376               afs_int32 asize)
377 {
378     /* handle basic rx packet */
379     ahandle->length = asize - RX_HEADER_SIZE;
380     rxi_DecodePacketHeader(ahandle);
381     ahandle =
382         rxi_ReceivePacket(ahandle, arock,
383                           afrom->sin_addr.s_addr, afrom->sin_port, NULL,
384                           NULL);
385
386     /* free the packet if it has been returned */
387     if (ahandle)
388         rxi_FreePacket(ahandle);
389     return 0;
390 }
391 #endif /* !RXK_LISTENER_ENV */
392
393 void
394 rxi_StartListener(void)
395 {
396     /* if kernel, give name of appropriate procedures */
397 #ifndef RXK_LISTENER_ENV
398     rxk_GetPacketProc = MyPacketProc;
399     rxk_PacketArrivalProc = MyArrivalProc;
400     rxk_init();
401 #endif
402 }
403
404 /* Called from rxi_FindPeer, when initializing a clear rx_peer structure,
405   to get interesting information. */
406 void
407 rxi_InitPeerParams(struct rx_peer *pp)
408 {
409     u_short rxmtu;
410
411 #ifdef  ADAPT_MTU
412 # ifndef AFS_SUN5_ENV
413 #  ifdef AFS_USERSPACE_IP_ADDR
414     afs_int32 i;
415     afs_int32 mtu;
416
417     i = rxi_Findcbi(pp->host);
418     if (i == -1) {
419         pp->timeout.sec = 3;
420         /* pp->timeout.usec = 0; */
421         pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
422     } else {
423         pp->timeout.sec = 2;
424         /* pp->timeout.usec = 0; */
425         pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
426     }
427     if (i != -1) {
428         mtu = ntohl(afs_cb_interface.mtu[i]);
429         /* Diminish the packet size to one based on the MTU given by
430          * the interface. */
431         if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
432             rxmtu = mtu - RX_IPUDP_SIZE;
433             if (rxmtu < pp->ifMTU)
434                 pp->ifMTU = rxmtu;
435         }
436     } else {                    /* couldn't find the interface, so assume the worst */
437         pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
438     }
439 #  else /* AFS_USERSPACE_IP_ADDR */
440     AFS_IFNET_T ifn;
441
442 #   if !defined(AFS_SGI62_ENV)
443     if (numMyNetAddrs == 0)
444         (void)rxi_GetIFInfo();
445 #   endif
446
447     ifn = rxi_FindIfnet(pp->host, NULL);
448     if (ifn) {
449         pp->timeout.sec = 2;
450         /* pp->timeout.usec = 0; */
451         pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
452 #   ifdef IFF_POINTOPOINT
453         if (ifnet_flags(ifn) & IFF_POINTOPOINT) {
454             /* wish we knew the bit rate and the chunk size, sigh. */
455             pp->timeout.sec = 4;
456             pp->ifMTU = RX_PP_PACKET_SIZE;
457         }
458 #   endif /* IFF_POINTOPOINT */
459         /* Diminish the packet size to one based on the MTU given by
460          * the interface. */
461         if (ifnet_mtu(ifn) > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
462             rxmtu = ifnet_mtu(ifn) - RX_IPUDP_SIZE;
463             if (rxmtu < pp->ifMTU)
464                 pp->ifMTU = rxmtu;
465         }
466     } else {                    /* couldn't find the interface, so assume the worst */
467         pp->timeout.sec = 3;
468         /* pp->timeout.usec = 0; */
469         pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
470     }
471 #  endif /* else AFS_USERSPACE_IP_ADDR */
472 # else /* AFS_SUN5_ENV */
473     afs_int32 mtu;
474
475     mtu = rxi_FindIfMTU(pp->host);
476
477     if (mtu <= 0) {
478         pp->timeout.sec = 3;
479         /* pp->timeout.usec = 0; */
480         pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE, rx_MyMaxSendSize);
481     } else {
482         pp->timeout.sec = 2;
483         /* pp->timeout.usec = 0; */
484         pp->ifMTU = MIN(RX_MAX_PACKET_SIZE, rx_MyMaxSendSize);
485     }
486
487     if (mtu > 0) {
488         /* Diminish the packet size to one based on the MTU given by
489          * the interface. */
490         if (mtu > (RX_IPUDP_SIZE + RX_HEADER_SIZE)) {
491             rxmtu = mtu - RX_IPUDP_SIZE;
492             if (rxmtu < pp->ifMTU)
493                 pp->ifMTU = rxmtu;
494         }
495     } else {                    /* couldn't find the interface, so assume the worst */
496         pp->ifMTU = MIN(RX_REMOTE_PACKET_SIZE,rx_MyMaxSendSize);
497     }
498 # endif /* AFS_SUN5_ENV */
499 #else /* ADAPT_MTU */
500     pp->rateFlag = 2;           /* start timing after two full packets */
501     pp->timeout.sec = 2;
502     pp->ifMTU = OLD_MAX_PACKET_SIZE;
503 #endif /* else ADAPT_MTU */
504     pp->ifMTU = rxi_AdjustIfMTU(pp->ifMTU);
505     pp->maxMTU = OLD_MAX_PACKET_SIZE;   /* for compatibility with old guys */
506     pp->natMTU = MIN(pp->ifMTU, OLD_MAX_PACKET_SIZE);
507     pp->ifDgramPackets =
508         MIN(rxi_nDgramPackets,
509             rxi_AdjustDgramPackets(rxi_nSendFrags, pp->ifMTU));
510     pp->maxDgramPackets = 1;
511
512     /* Initialize slow start parameters */
513     pp->MTU = MIN(pp->natMTU, pp->maxMTU);
514     pp->cwind = 1;
515     pp->nDgramPackets = 1;
516     pp->congestSeq = 0;
517 }
518
519
520 /* The following code is common to several system types, but not all. The
521  * separate ones are found in the system specific subdirectories.
522  */
523
524
525 #if ! defined(AFS_AIX_ENV) && ! defined(AFS_SUN5_ENV) && ! defined(UKERNEL) && ! defined(AFS_LINUX20_ENV) && !defined (AFS_DARWIN_ENV) && !defined (AFS_XBSD_ENV)
526 /* Routine called during the afsd "-shutdown" process to put things back to
527  * the initial state.
528  */
529 static struct protosw parent_proto;     /* udp proto switch */
530
531 void
532 shutdown_rxkernel(void)
533 {
534     struct protosw *tpro, *last;
535     last = inetdomain.dom_protoswNPROTOSW;
536     for (tpro = inetdomain.dom_protosw; tpro < last; tpro++)
537         if (tpro->pr_protocol == IPPROTO_UDP) {
538             /* restore original udp protocol switch */
539             memcpy((void *)tpro, (void *)&parent_proto, sizeof(parent_proto));
540             memset((void *)&parent_proto, 0, sizeof(parent_proto));
541             rxk_initDone = 0;
542             rxk_shutdownPorts();
543             return;
544         }
545     dpf(("shutdown_rxkernel: no udp proto"));
546 }
547 #endif /* !AIX && !SUN && !NCR  && !UKERNEL */
548
549 #if !defined(AFS_SUN5_ENV) && !defined(AFS_SGI62_ENV)
550 /* Determine what the network interfaces are for this machine. */
551
552 #ifdef AFS_USERSPACE_IP_ADDR
553 int
554 rxi_GetcbiInfo(void)
555 {
556     int i, j, different = 0, num = ADDRSPERSITE;
557     int rxmtu, maxmtu;
558     afs_uint32 ifinaddr;
559     afs_uint32 addrs[ADDRSPERSITE];
560     int mtus[ADDRSPERSITE];
561
562     memset((void *)addrs, 0, sizeof(addrs));
563     memset((void *)mtus, 0, sizeof(mtus));
564
565     if (afs_cb_interface.numberOfInterfaces < num)
566         num = afs_cb_interface.numberOfInterfaces;
567     for (i = 0; i < num; i++) {
568         if (!afs_cb_interface.mtu[i])
569             afs_cb_interface.mtu[i] = htonl(1500);
570         rxmtu = (ntohl(afs_cb_interface.mtu[i]) - RX_IPUDP_SIZE);
571         ifinaddr = ntohl(afs_cb_interface.addr_in[i]);
572         if (myNetAddrs[i] != ifinaddr)
573             different++;
574
575         mtus[i] = rxmtu;
576         rxmtu = rxi_AdjustIfMTU(rxmtu);
577         maxmtu =
578             rxmtu * rxi_nRecvFrags + ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
579         maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
580         addrs[i++] = ifinaddr;
581         if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
582             rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
583             rx_maxReceiveSize = MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
584         }
585     }
586
587     rx_maxJumboRecvSize =
588         RX_HEADER_SIZE + (rxi_nDgramPackets * RX_JUMBOBUFFERSIZE) +
589         ((rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE);
590     rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
591
592     if (different) {
593         for (j = 0; j < i; j++) {
594             myNetMTUs[j] = mtus[j];
595             myNetAddrs[j] = addrs[j];
596         }
597     }
598     return different;
599 }
600
601
602 /* Returns the afs_cb_interface inxex which best matches address.
603  * If none is found, we return -1.
604  */
605 afs_int32
606 rxi_Findcbi(afs_uint32 addr)
607 {
608     int j;
609     afs_uint32 myAddr, thisAddr, netMask, subnetMask;
610     afs_int32 rvalue = -1;
611     int match_value = 0;
612
613     if (numMyNetAddrs == 0)
614         (void)rxi_GetcbiInfo();
615
616     myAddr = ntohl(addr);
617
618     if (IN_CLASSA(myAddr))
619         netMask = IN_CLASSA_NET;
620     else if (IN_CLASSB(myAddr))
621         netMask = IN_CLASSB_NET;
622     else if (IN_CLASSC(myAddr))
623         netMask = IN_CLASSC_NET;
624     else
625         netMask = 0;
626
627     for (j = 0; j < afs_cb_interface.numberOfInterfaces; j++) {
628         thisAddr = ntohl(afs_cb_interface.addr_in[j]);
629         subnetMask = ntohl(afs_cb_interface.subnetmask[j]);
630         if ((myAddr & netMask) == (thisAddr & netMask)) {
631             if ((myAddr & subnetMask) == (thisAddr & subnetMask)) {
632                 if (myAddr == thisAddr) {
633                     match_value = 4;
634                     rvalue = j;
635                     break;
636                 }
637                 if (match_value < 3) {
638                     match_value = 3;
639                     rvalue = j;
640                 }
641             } else {
642                 if (match_value < 2) {
643                     match_value = 2;
644                     rvalue = j;
645                 }
646             }
647         }
648     }
649
650     return (rvalue);
651 }
652
653 #else /* AFS_USERSPACE_IP_ADDR */
654
655 #if !defined(AFS_AIX41_ENV) && !defined(AFS_DUX40_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
656 #define IFADDR2SA(f) (&((f)->ifa_addr))
657 #else /* AFS_AIX41_ENV */
658 #define IFADDR2SA(f) ((f)->ifa_addr)
659 #endif
660
661 int
662 rxi_GetIFInfo(void)
663 {
664     int i = 0;
665     int different = 0;
666
667     int rxmtu, maxmtu;
668     afs_uint32 addrs[ADDRSPERSITE];
669     int mtus[ADDRSPERSITE];
670     afs_uint32 ifinaddr;
671 #if defined(AFS_DARWIN80_ENV)
672     errno_t t;
673     unsigned int count;
674     int cnt=0, m, j;
675     ifaddr_t *ifads;
676     ifnet_t *ifn;
677     struct sockaddr sout;
678     struct sockaddr_in *sin;
679     struct in_addr pin;
680 #else
681     struct ifaddr *ifad;        /* ifnet points to a if_addrlist of ifaddrs */
682     struct ifnet *ifn;
683 #endif
684
685     memset(addrs, 0, sizeof(addrs));
686     memset(mtus, 0, sizeof(mtus));
687
688 #if defined(AFS_DARWIN80_ENV)
689     if (!ifnet_list_get(AF_INET, &ifn, &count)) {
690         for (m = 0; m < count; m++) {
691             if (!ifnet_get_address_list(ifn[m], &ifads)) {
692                 for (j = 0; ifads[j] != NULL && cnt < ADDRSPERSITE; j++) {
693                     if ((t = ifaddr_address(ifads[j], &sout, sizeof(struct sockaddr))) == 0) {
694                         sin = (struct sockaddr_in *)&sout;
695                         rxmtu = ifnet_mtu(ifaddr_ifnet(ifads[j])) - RX_IPUDP_SIZE;
696                         ifinaddr = ntohl(sin->sin_addr.s_addr);
697                         if (myNetAddrs[i] != ifinaddr) {
698                             different++;
699                         }
700                         mtus[i] = rxmtu;
701                         rxmtu = rxi_AdjustIfMTU(rxmtu);
702                         maxmtu =
703                             rxmtu * rxi_nRecvFrags +
704                             ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
705                         maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
706                         addrs[i++] = ifinaddr;
707                         if ((ifinaddr != 0x7f000001) && 
708                             (maxmtu > rx_maxReceiveSize)) {
709                             rx_maxReceiveSize = 
710                                 MIN(RX_MAX_PACKET_SIZE, maxmtu);
711                             rx_maxReceiveSize =
712                                 MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
713                         }
714                         cnt++;
715                     }
716                 }
717                 ifnet_free_address_list(ifads);
718             }
719         }
720         ifnet_list_free(ifn);
721     }
722 #else
723 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
724     TAILQ_FOREACH(ifn, &ifnet, if_link) {
725         if (i >= ADDRSPERSITE)
726             break;
727 #elif defined(AFS_OBSD_ENV)
728     for (ifn = ifnet.tqh_first; i < ADDRSPERSITE && ifn != NULL;
729          ifn = ifn->if_list.tqe_next) {
730 #else
731     for (ifn = ifnet; ifn != NULL && i < ADDRSPERSITE; ifn = ifn->if_next) {
732 #endif
733         rxmtu = (ifn->if_mtu - RX_IPUDP_SIZE);
734 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
735         TAILQ_FOREACH(ifad, &ifn->if_addrhead, ifa_link) {
736             if (i >= ADDRSPERSITE)
737                 break;
738 #elif defined(AFS_OBSD_ENV)
739         for (ifad = ifn->if_addrlist.tqh_first;
740              ifad != NULL && i < ADDRSPERSITE;
741              ifad = ifad->ifa_list.tqe_next) {
742 #else
743         for (ifad = ifn->if_addrlist; ifad != NULL && i < ADDRSPERSITE;
744              ifad = ifad->ifa_next) {
745 #endif
746             if (IFADDR2SA(ifad)->sa_family == AF_INET) {
747                 ifinaddr =
748                     ntohl(((struct sockaddr_in *)IFADDR2SA(ifad))->sin_addr.
749                           s_addr);
750                 if (myNetAddrs[i] != ifinaddr) {
751                     different++;
752                 }
753                 mtus[i] = rxmtu;
754                 rxmtu = rxi_AdjustIfMTU(rxmtu);
755                 maxmtu =
756                     rxmtu * rxi_nRecvFrags +
757                     ((rxi_nRecvFrags - 1) * UDP_HDR_SIZE);
758                 maxmtu = rxi_AdjustMaxMTU(rxmtu, maxmtu);
759                 addrs[i++] = ifinaddr;
760                 if ((ifinaddr != 0x7f000001) && (maxmtu > rx_maxReceiveSize)) {
761                     rx_maxReceiveSize = MIN(RX_MAX_PACKET_SIZE, maxmtu);
762                     rx_maxReceiveSize =
763                         MIN(rx_maxReceiveSize, rx_maxReceiveSizeUser);
764                 }
765             }
766         }
767     }
768 #endif
769
770     rx_maxJumboRecvSize =
771         RX_HEADER_SIZE + rxi_nDgramPackets * RX_JUMBOBUFFERSIZE +
772         (rxi_nDgramPackets - 1) * RX_JUMBOHEADERSIZE;
773     rx_maxJumboRecvSize = MAX(rx_maxJumboRecvSize, rx_maxReceiveSize);
774
775     if (different) {
776         int l;
777         for (l = 0; l < i; l++) {
778             myNetMTUs[l] = mtus[l];
779             myNetAddrs[l] = addrs[l];
780         }
781     }
782     return different;
783 }
784
785 #if defined(AFS_DARWIN60_ENV) || defined(AFS_XBSD_ENV)
786 /* Returns ifnet which best matches address */
787 #ifdef AFS_DARWIN80_ENV
788 ifnet_t
789 #else
790 struct ifnet *
791 #endif
792 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
793 {
794     struct sockaddr_in s, sr;
795 #ifdef AFS_DARWIN80_ENV
796     ifaddr_t ifad;
797 #else
798     struct ifaddr *ifad;
799 #endif
800
801     s.sin_family = AF_INET;
802     s.sin_addr.s_addr = addr;
803 #ifdef AFS_DARWIN80_ENV
804     ifad = ifaddr_withnet((struct sockaddr *)&s);
805 #else
806     ifad = ifa_ifwithnet((struct sockaddr *)&s);
807 #endif
808
809 #ifdef AFS_DARWIN80_ENV
810     if (ifad && maskp) {
811         ifaddr_netmask(ifad, (struct sockaddr *)&sr, sizeof(sr));
812         *maskp = sr.sin_addr.s_addr;
813     }
814     return (ifad ? ifaddr_ifnet(ifad) : NULL);
815 #else
816     if (ifad && maskp)
817         *maskp = ((struct sockaddr_in *)ifad->ifa_netmask)->sin_addr.s_addr;
818     return (ifad ? ifad->ifa_ifp : NULL);
819 #endif
820 }
821
822 #else /* DARWIN60 || XBSD */
823
824 /* Returns ifnet which best matches address */
825 struct ifnet *
826 rxi_FindIfnet(afs_uint32 addr, afs_uint32 * maskp)
827 {
828     int match_value = 0;
829     extern struct in_ifaddr *in_ifaddr;
830     struct in_ifaddr *ifa, *ifad = NULL;
831
832     addr = ntohl(addr);
833
834 #if defined(AFS_DARWIN_ENV)
835     for (ifa = TAILQ_FIRST(&in_ifaddrhead); ifa;
836          ifa = TAILQ_NEXT(ifa, ia_link)) {
837 #else
838     for (ifa = in_ifaddr; ifa; ifa = ifa->ia_next) {
839 #endif
840         if ((addr & ifa->ia_netmask) == ifa->ia_net) {
841             if ((addr & ifa->ia_subnetmask) == ifa->ia_subnet) {
842                 if (IA_SIN(ifa)->sin_addr.s_addr == addr) {     /* ie, ME!!!  */
843                     match_value = 4;
844                     ifad = ifa;
845                     goto done;
846                 }
847                 if (match_value < 3) {
848                     ifad = ifa;
849                     match_value = 3;
850                 }
851             } else {
852                 if (match_value < 2) {
853                     ifad = ifa;
854                     match_value = 2;
855                 }
856             }
857         }                       /* if net matches */
858     }                           /* for all in_ifaddrs */
859
860   done:
861     if (ifad && maskp)
862         *maskp = ifad->ia_subnetmask;
863     return (ifad ? ifad->ia_ifp : NULL);
864 }
865 #endif /* else DARWIN60 || XBSD */
866 #endif /* else AFS_USERSPACE_IP_ADDR */
867 #endif /* !SUN5 && !SGI62 */
868
869
870 /* rxk_NewSocket, rxk_FreeSocket and osi_NetSend are from the now defunct
871  * afs_osinet.c. One could argue that rxi_NewSocket could go into the
872  * system specific subdirectories for all systems. But for the moment,
873  * most of it is simple to follow common code.
874  */
875 #if !defined(UKERNEL)
876 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
877 /* rxk_NewSocket creates a new socket on the specified port. The port is
878  * in network byte order.
879  */
880 osi_socket *
881 rxk_NewSocketHost(afs_uint32 ahost, short aport)
882 {
883     afs_int32 code;
884 #ifdef AFS_DARWIN80_ENV
885     socket_t newSocket;
886 #else
887     struct socket *newSocket;
888 #endif
889 #if (!defined(AFS_HPUX1122_ENV) && !defined(AFS_FBSD50_ENV))
890     struct mbuf *nam;
891 #endif
892     struct sockaddr_in myaddr;
893 #ifdef AFS_HPUX110_ENV
894     /* prototype copied from kernel source file streams/str_proto.h */
895     extern MBLKP allocb_wait(int, int);
896     MBLKP bindnam;
897     int addrsize = sizeof(struct sockaddr_in);
898     struct file *fp;
899     extern struct fileops socketops;
900 #endif
901 #ifdef AFS_SGI65_ENV
902     bhv_desc_t bhv;
903 #endif
904
905     AFS_STATCNT(osi_NewSocket);
906 #if (defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)) && defined(KERNEL_FUNNEL)
907     thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
908 #endif
909     AFS_ASSERT_GLOCK();
910     AFS_GUNLOCK();
911 #if     defined(AFS_HPUX102_ENV)
912 #if     defined(AFS_HPUX110_ENV)
913     /* we need a file associated with the socket so sosend in NetSend 
914      * will not fail */
915     /* blocking socket */
916     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, 0);
917     fp = falloc();
918     if (!fp)
919         goto bad;
920     fp->f_flag = FREAD | FWRITE;
921     fp->f_type = DTYPE_SOCKET;
922     fp->f_ops = &socketops;
923
924     fp->f_data = (void *)newSocket;
925     newSocket->so_fp = (void *)fp;
926
927 #else /* AFS_HPUX110_ENV */
928     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0, SS_NOWAIT);
929 #endif /* else AFS_HPUX110_ENV */
930 #elif defined(AFS_SGI65_ENV) || defined(AFS_OBSD_ENV)
931     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP);
932 #elif defined(AFS_FBSD50_ENV)
933     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP,
934                     afs_osi_credp, curthread);
935 #elif defined(AFS_FBSD40_ENV)
936     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, IPPROTO_UDP, curproc);
937 #elif defined(AFS_DARWIN80_ENV)
938     code = sock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, NULL, &newSocket);
939 #else
940     code = socreate(AF_INET, &newSocket, SOCK_DGRAM, 0);
941 #endif /* AFS_HPUX102_ENV */
942     if (code)
943         goto bad;
944
945     memset(&myaddr, 0, sizeof myaddr);
946     myaddr.sin_family = AF_INET;
947     myaddr.sin_port = aport;
948     myaddr.sin_addr.s_addr = ahost;
949 #ifdef STRUCT_SOCKADDR_HAS_SA_LEN
950     myaddr.sin_len = sizeof(myaddr);
951 #endif
952
953 #ifdef AFS_HPUX110_ENV
954     bindnam = allocb_wait((addrsize + SO_MSGOFFSET + 1), BPRI_MED);
955     if (!bindnam) {
956         setuerror(ENOBUFS);
957         goto bad;
958     }
959     memcpy((caddr_t) bindnam->b_rptr + SO_MSGOFFSET, (caddr_t) & myaddr,
960            addrsize);
961     bindnam->b_wptr = bindnam->b_rptr + (addrsize + SO_MSGOFFSET + 1);
962
963     code = sobind(newSocket, bindnam, addrsize);
964     if (code) {
965         soclose(newSocket);
966 #if !defined(AFS_HPUX1122_ENV)
967         m_freem(nam);
968 #endif
969         goto bad;
970     }
971
972     freeb(bindnam);
973 #else /* AFS_HPUX110_ENV */
974 #if defined(AFS_DARWIN80_ENV)
975     { 
976        int buflen = 50000;
977        int i,code2;
978        for (i=0;i<2;i++) {
979            code = sock_setsockopt(newSocket, SOL_SOCKET, SO_SNDBUF,
980                                   &buflen, sizeof(buflen));
981            code2 = sock_setsockopt(newSocket, SOL_SOCKET, SO_RCVBUF,
982                                   &buflen, sizeof(buflen));
983            if (!code && !code2)
984                break;
985            if (i == 2)
986               osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
987            buflen = 32766;
988        }
989     }
990 #else
991     code = soreserve(newSocket, 50000, 50000);
992     if (code) {
993         code = soreserve(newSocket, 32766, 32766);
994         if (code)
995             osi_Panic("osi_NewSocket: last attempt to reserve 32K failed!\n");
996     }
997 #endif
998 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
999 #if defined(AFS_FBSD50_ENV)
1000     code = sobind(newSocket, (struct sockaddr *)&myaddr, curthread);
1001 #elif defined(AFS_FBSD40_ENV)
1002     code = sobind(newSocket, (struct sockaddr *)&myaddr, curproc);
1003 #else
1004     code = sobind(newSocket, (struct sockaddr *)&myaddr);
1005 #endif
1006     if (code) {
1007         dpf(("sobind fails (%d)\n", (int)code));
1008         soclose(newSocket);
1009         AFS_GLOCK();
1010         goto bad;
1011     }
1012 #else /* defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV) */
1013 #ifdef  AFS_OSF_ENV
1014     nam = m_getclr(M_WAIT, MT_SONAME);
1015 #else /* AFS_OSF_ENV */
1016     nam = m_get(M_WAIT, MT_SONAME);
1017 #endif
1018     if (nam == NULL) {
1019 #if defined(KERNEL_HAVE_UERROR)
1020         setuerror(ENOBUFS);
1021 #endif
1022         goto bad;
1023     }
1024     nam->m_len = sizeof(myaddr);
1025     memcpy(mtod(nam, caddr_t), &myaddr, sizeof(myaddr));
1026 #if defined(AFS_SGI65_ENV)
1027     BHV_PDATA(&bhv) = (void *)newSocket;
1028     code = sobind(&bhv, nam);
1029     m_freem(nam);
1030 #elif defined(AFS_OBSD44_ENV)
1031     code = sobind(newSocket, nam, osi_curproc());
1032 #else
1033     code = sobind(newSocket, nam);
1034 #endif
1035     if (code) {
1036         dpf(("sobind fails (%d)\n", (int)code));
1037         soclose(newSocket);
1038 #ifndef AFS_SGI65_ENV
1039         m_freem(nam);
1040 #endif
1041         goto bad;
1042     }
1043 #endif /* else AFS_DARWIN_ENV */
1044 #endif /* else AFS_HPUX110_ENV */
1045
1046     AFS_GLOCK();
1047 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1048     thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1049 #endif
1050     return (osi_socket *)newSocket;
1051
1052   bad:
1053     AFS_GLOCK();
1054 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1055     thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1056 #endif
1057     return (osi_socket *)0;
1058 }
1059
1060 osi_socket *
1061 rxk_NewSocket(short aport)
1062 {
1063     return rxk_NewSocketHost(0, aport);
1064 }
1065
1066 /* free socket allocated by rxk_NewSocket */
1067 int
1068 rxk_FreeSocket(struct socket *asocket)
1069 {
1070     AFS_STATCNT(osi_FreeSocket);
1071 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1072     thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
1073 #endif
1074 #ifdef AFS_HPUX110_ENV
1075     if (asocket->so_fp) {
1076         struct file *fp = asocket->so_fp;
1077 #if !defined(AFS_HPUX1123_ENV)
1078         /* 11.23 still has falloc, but not FPENTRYFREE ! 
1079          * so for now if we shutdown, we will waist a file 
1080          * structure */
1081         FPENTRYFREE(fp);
1082         asocket->so_fp = NULL;
1083 #endif
1084     }
1085 #endif /* AFS_HPUX110_ENV */
1086     soclose(asocket);
1087 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
1088     thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
1089 #endif
1090     return 0;
1091 }
1092 #endif /* !SUN5 && !LINUX20 */
1093
1094 #if defined(RXK_LISTENER_ENV) || defined(AFS_SUN5_ENV)
1095 /*
1096  * Run RX event daemon every second (5 times faster than rest of systems)
1097  */
1098 void
1099 afs_rxevent_daemon(void)
1100 {
1101     struct clock temp;
1102     SPLVAR;
1103
1104     while (1) {
1105 #ifdef RX_ENABLE_LOCKS
1106         AFS_GUNLOCK();
1107 #endif /* RX_ENABLE_LOCKS */
1108         NETPRI;
1109         rxevent_RaiseEvents(&temp);
1110         USERPRI;
1111 #ifdef RX_ENABLE_LOCKS
1112         AFS_GLOCK();
1113 #endif /* RX_ENABLE_LOCKS */
1114 #ifdef RX_KERNEL_TRACE
1115         afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1116                    "before afs_osi_Wait()");
1117 #endif
1118         afs_osi_Wait(500, NULL, 0);
1119 #ifdef RX_KERNEL_TRACE
1120         afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1121                    "after afs_osi_Wait()");
1122 #endif
1123         if (afs_termState == AFSOP_STOP_RXEVENT) {
1124 #ifdef RXK_LISTENER_ENV
1125             afs_termState = AFSOP_STOP_RXK_LISTENER;
1126 #else
1127 #ifdef AFS_SUN510_ENV
1128             afs_termState = AFSOP_STOP_NETIF;
1129 #else
1130             afs_termState = AFSOP_STOP_COMPLETE;
1131 #endif
1132 #endif
1133             osi_rxWakeup(&afs_termState);
1134             return;
1135         }
1136     }
1137 }
1138 #endif
1139
1140 #ifdef RXK_LISTENER_ENV
1141
1142 /* rxk_ReadPacket returns 1 if valid packet, 0 on error. */
1143 int
1144 rxk_ReadPacket(osi_socket so, struct rx_packet *p, int *host, int *port)
1145 {
1146     int code;
1147     struct sockaddr_in from;
1148     int nbytes;
1149     afs_int32 rlen;
1150     afs_int32 tlen;
1151     afs_int32 savelen;          /* was using rlen but had aliasing problems */
1152     rx_computelen(p, tlen);
1153     rx_SetDataSize(p, tlen);    /* this is the size of the user data area */
1154
1155     tlen += RX_HEADER_SIZE;     /* now this is the size of the entire packet */
1156     rlen = rx_maxJumboRecvSize; /* this is what I am advertising.  Only check
1157                                  * it once in order to avoid races.  */
1158     tlen = rlen - tlen;
1159     if (tlen > 0) {
1160         tlen = rxi_AllocDataBuf(p, tlen, RX_PACKET_CLASS_RECV_CBUF);
1161         if (tlen > 0) {
1162             tlen = rlen - tlen;
1163         } else
1164             tlen = rlen;
1165     } else
1166         tlen = rlen;
1167
1168     /* add some padding to the last iovec, it's just to make sure that the 
1169      * read doesn't return more data than we expect, and is done to get around
1170      * our problems caused by the lack of a length field in the rx header. */
1171     savelen = p->wirevec[p->niovecs - 1].iov_len;
1172     p->wirevec[p->niovecs - 1].iov_len = savelen + RX_EXTRABUFFERSIZE;
1173
1174     nbytes = tlen + sizeof(afs_int32);
1175 #ifdef RX_KERNEL_TRACE
1176     if (ICL_SETACTIVE(afs_iclSetp)) {
1177         AFS_GLOCK();
1178         afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1179                    "before osi_NetRecive()");
1180         AFS_GUNLOCK();
1181     }
1182 #endif
1183     code = osi_NetReceive(rx_socket, &from, p->wirevec, p->niovecs, &nbytes);
1184
1185 #ifdef RX_KERNEL_TRACE
1186     if (ICL_SETACTIVE(afs_iclSetp)) {
1187         AFS_GLOCK();
1188         afs_Trace1(afs_iclSetp, CM_TRACE_TIMESTAMP, ICL_TYPE_STRING,
1189                    "after osi_NetRecive()");
1190         AFS_GUNLOCK();
1191     }
1192 #endif
1193     /* restore the vec to its correct state */
1194     p->wirevec[p->niovecs - 1].iov_len = savelen;
1195
1196     if (!code) {
1197         p->length = nbytes - RX_HEADER_SIZE;;
1198         if ((nbytes > tlen) || (p->length & 0x8000)) {  /* Bogus packet */
1199             if (nbytes <= 0) {
1200                 if (rx_stats_active) {
1201                     MUTEX_ENTER(&rx_stats_mutex);
1202                     rx_stats.bogusPacketOnRead++;
1203                     rx_stats.bogusHost = from.sin_addr.s_addr;
1204                     MUTEX_EXIT(&rx_stats_mutex);
1205                 }
1206                 dpf(("B: bogus packet from [%x,%d] nb=%d",
1207                      from.sin_addr.s_addr, from.sin_port, nbytes));
1208             }
1209             return -1;
1210         } else {
1211             /* Extract packet header. */
1212             rxi_DecodePacketHeader(p);
1213
1214             *host = from.sin_addr.s_addr;
1215             *port = from.sin_port;
1216             if (p->header.type > 0 && p->header.type < RX_N_PACKET_TYPES) {
1217                 if (rx_stats_active) {
1218                     MUTEX_ENTER(&rx_stats_mutex);
1219                     rx_stats.packetsRead[p->header.type - 1]++;
1220                     MUTEX_EXIT(&rx_stats_mutex);
1221                 }
1222             }
1223
1224 #ifdef RX_TRIMDATABUFS
1225             /* Free any empty packet buffers at the end of this packet */
1226             rxi_TrimDataBufs(p, 1);
1227 #endif
1228             return 0;
1229         }
1230     } else
1231         return code;
1232 }
1233
1234 /* rxk_Listener() 
1235  *
1236  * Listen for packets on socket. This thread is typically started after
1237  * rx_Init has called rxi_StartListener(), but nevertheless, ensures that
1238  * the start state is set before proceeding.
1239  *
1240  * Note that this thread is outside the AFS global lock for much of
1241  * it's existence.
1242  *
1243  * In many OS's, the socket receive code sleeps interruptibly. That's not what
1244  * we want here. So we need to either block all signals (including SIGKILL
1245  * and SIGSTOP) or reset the thread's signal state to unsignalled when the
1246  * OS's socket receive routine returns as a result of a signal.
1247  */
1248 int rxk_ListenerPid;            /* Used to signal process to wakeup at shutdown */
1249 #ifdef AFS_LINUX20_ENV
1250 struct task_struct *rxk_ListenerTask;
1251 #endif
1252
1253 #ifdef AFS_SUN5_ENV
1254 /*
1255  * Run the listener as a kernel thread.
1256  */
1257 void
1258 rxk_Listener(void)
1259 {
1260     extern id_t syscid;
1261     void rxk_ListenerProc(void);
1262     if (thread_create
1263         (NULL, DEFAULTSTKSZ, rxk_ListenerProc, 0, 0, &p0, TS_RUN,
1264          minclsyspri) == NULL)
1265         osi_Panic("rxk_Listener: failed to start listener thread!\n");
1266 }
1267
1268 void
1269 rxk_ListenerProc(void)
1270 #else /* AFS_SUN5_ENV */
1271 void
1272 rxk_Listener(void)
1273 #endif                          /* AFS_SUN5_ENV */
1274 {
1275     struct rx_packet *rxp = NULL;
1276     int code;
1277     int host, port;
1278
1279 #ifdef AFS_LINUX20_ENV
1280     rxk_ListenerPid = current->pid;
1281     rxk_ListenerTask = current;
1282 #endif
1283 #ifdef AFS_SUN5_ENV
1284     rxk_ListenerPid = 1;        /* No PID, just a flag that we're alive */
1285 #endif /* AFS_SUN5_ENV */
1286 #ifdef AFS_XBSD_ENV
1287     rxk_ListenerPid = curproc->p_pid;
1288 #endif /* AFS_FBSD_ENV */
1289 #ifdef AFS_DARWIN80_ENV
1290     rxk_ListenerPid = proc_selfpid();
1291 #elif defined(AFS_DARWIN_ENV)
1292     rxk_ListenerPid = current_proc()->p_pid;
1293 #endif
1294 #if defined(RX_ENABLE_LOCKS) && !defined(AFS_SUN5_ENV)
1295     AFS_GUNLOCK();
1296 #endif /* RX_ENABLE_LOCKS && !AFS_SUN5_ENV */
1297     while (afs_termState != AFSOP_STOP_RXK_LISTENER) {
1298         if (rxp) {
1299             rxi_RestoreDataBufs(rxp);
1300         } else {
1301             rxp = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE);
1302             if (!rxp)
1303                 osi_Panic("rxk_Listener: No more Rx buffers!\n");
1304         }
1305         if (!(code = rxk_ReadPacket(rx_socket, rxp, &host, &port))) {
1306             rxp = rxi_ReceivePacket(rxp, rx_socket, host, port, 0, 0);
1307         }
1308     }
1309
1310 #ifdef RX_ENABLE_LOCKS
1311     AFS_GLOCK();
1312 #endif /* RX_ENABLE_LOCKS */
1313     if (afs_termState == AFSOP_STOP_RXK_LISTENER) {
1314 #ifdef AFS_SUN510_ENV
1315         afs_termState = AFSOP_STOP_NETIF;
1316 #else
1317         afs_termState = AFSOP_STOP_COMPLETE;
1318 #endif
1319         osi_rxWakeup(&afs_termState);
1320     }
1321     rxk_ListenerPid = 0;
1322 #ifdef AFS_LINUX20_ENV
1323     rxk_ListenerTask = 0;
1324     osi_rxWakeup(&rxk_ListenerTask);
1325 #endif
1326 #if defined(AFS_SUN5_ENV)
1327     osi_rxWakeup(&rxk_ListenerPid);
1328 #endif
1329 #ifdef AFS_SUN5_ENV
1330     AFS_GUNLOCK();
1331 #endif /* AFS_SUN5_ENV */
1332 }
1333
1334 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
1335 /* The manner of stopping the rx listener thread may vary. Most unix's should
1336  * be able to call soclose.
1337  */
1338 void
1339 osi_StopListener(void)
1340 {
1341     soclose(rx_socket);
1342 }
1343 #endif
1344 #endif /* RXK_LISTENER_ENV */
1345
1346 #endif /* !NCR && !UKERNEL */