reindent-20030715
[openafs.git] / src / rx / DARWIN / rx_knet.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 #include <afsconfig.h>
11 #include "afs/param.h"
12
13 RCSID
14     ("$Header$");
15
16 #include "rx/rx_kcommon.h"
17
18 int
19 osi_NetReceive(osi_socket so, struct sockaddr_in *addr, struct iovec *dvec,
20                int nvecs, int *alength)
21 {
22     struct socket *asocket = (struct socket *)so;
23     struct uio u;
24     int i;
25     struct iovec iov[RX_MAXIOVECS];
26     struct sockaddr *sa = NULL;
27     int code;
28
29     int haveGlock = ISAFS_GLOCK();
30     /*AFS_STATCNT(osi_NetReceive); */
31
32     if (nvecs > RX_MAXIOVECS)
33         osi_Panic("osi_NetReceive: %d: Too many iovecs.\n", nvecs);
34
35     for (i = 0; i < nvecs; i++)
36         iov[i] = dvec[i];
37
38     u.uio_iov = &iov[0];
39     u.uio_iovcnt = nvecs;
40     u.uio_offset = 0;
41     u.uio_resid = *alength;
42     u.uio_segflg = UIO_SYSSPACE;
43     u.uio_rw = UIO_READ;
44     u.uio_procp = NULL;
45
46     if (haveGlock)
47         AFS_GUNLOCK();
48 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
49     thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
50 #endif
51     code = soreceive(asocket, &sa, &u, NULL, NULL, NULL);
52 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
53     thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
54 #endif
55     if (haveGlock)
56         AFS_GLOCK();
57
58     if (code)
59         return code;
60     *alength -= u.uio_resid;
61     if (sa) {
62         if (sa->sa_family == AF_INET) {
63             if (addr)
64                 *addr = *(struct sockaddr_in *)sa;
65         } else
66             printf("Unknown socket family %d in NetReceive\n", sa->sa_family);
67         FREE(sa, M_SONAME);
68     }
69     return code;
70 }
71
72 extern int rxk_ListenerPid;
73 void
74 osi_StopListener(void)
75 {
76     struct proc *p;
77
78 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
79     thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
80 #endif
81     soclose(rx_socket);
82 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
83     thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
84 #endif
85     p = pfind(rxk_ListenerPid);
86     if (p)
87         psignal(p, SIGUSR1);
88 }
89
90 int
91 osi_NetSend(osi_socket asocket, struct sockaddr_in *addr, struct iovec *dvec,
92             int nvecs, afs_int32 alength, int istack)
93 {
94     register afs_int32 code;
95     int i;
96     struct iovec iov[RX_MAXIOVECS];
97     struct uio u;
98     int haveGlock = ISAFS_GLOCK();
99
100     AFS_STATCNT(osi_NetSend);
101     if (nvecs > RX_MAXIOVECS)
102         osi_Panic("osi_NetSend: %d: Too many iovecs.\n", nvecs);
103
104     for (i = 0; i < nvecs; i++)
105         iov[i] = dvec[i];
106
107     u.uio_iov = &iov[0];
108     u.uio_iovcnt = nvecs;
109     u.uio_offset = 0;
110     u.uio_resid = alength;
111     u.uio_segflg = UIO_SYSSPACE;
112     u.uio_rw = UIO_WRITE;
113     u.uio_procp = NULL;
114
115     addr->sin_len = sizeof(struct sockaddr_in);
116
117     if (haveGlock)
118         AFS_GUNLOCK();
119 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
120     thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
121 #endif
122     code = sosend(asocket, (struct sockaddr *)addr, &u, NULL, NULL, 0);
123 #if defined(AFS_DARWIN_ENV) && defined(KERNEL_FUNNEL)
124     thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
125 #endif
126     if (haveGlock)
127         AFS_GLOCK();
128     return code;
129 }