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