]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
ff6e181a A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. Please obtain a copy of the License at | |
10 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
11 | * file. | |
1c79356b | 12 | * |
ff6e181a A |
13 | * The Original Code and all software distributed under the License are |
14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
1c79356b A |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
ff6e181a A |
17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
18 | * Please see the License for the specific language governing rights and | |
19 | * limitations under the License. | |
1c79356b A |
20 | * |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | /* | |
24 | * Copyright (c) 1995-1998 Apple Computer, Inc. | |
25 | * | |
26 | * Change Log: | |
27 | * Created February 20, 1995 by Tuyen Nguyen | |
28 | * Modified for MP, 1996 by Tuyen Nguyen | |
29 | * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX. | |
30 | */ | |
31 | #include <sys/errno.h> | |
32 | #include <sys/types.h> | |
33 | #include <sys/param.h> | |
34 | #include <machine/spl.h> | |
35 | #include <sys/systm.h> | |
36 | #include <sys/kernel.h> | |
91447636 | 37 | #include <sys/proc_internal.h> /* for p_fd in fdflags */ |
1c79356b A |
38 | #include <sys/filedesc.h> |
39 | #include <sys/fcntl.h> | |
40 | #include <sys/mbuf.h> | |
41 | #include <sys/malloc.h> | |
91447636 | 42 | #include <sys/file_internal.h> |
1c79356b A |
43 | #include <sys/socket.h> |
44 | #include <sys/socketvar.h> | |
91447636 A |
45 | #include <sys/sysproto.h> |
46 | #include <sys/kdebug.h> | |
1c79356b A |
47 | #include <net/if_var.h> |
48 | ||
49 | #include <netat/sysglue.h> | |
50 | #include <netat/appletalk.h> | |
51 | #include <netat/at_var.h> | |
52 | #include <netat/at_pcb.h> | |
53 | #include <netat/debug.h> | |
54 | ||
55 | int (*sys_ATsocket)() = 0; | |
56 | int (*sys_ATgetmsg)() = 0; | |
57 | int (*sys_ATputmsg)() = 0; | |
58 | int (*sys_ATPsndreq)() = 0; | |
59 | int (*sys_ATPsndrsp)() = 0; | |
60 | int (*sys_ATPgetreq)() = 0; | |
61 | int (*sys_ATPgetrsp)() = 0; | |
62 | ||
63 | extern at_state_t at_state; /* global state of AT network */ | |
64 | extern at_ifaddr_t *ifID_home; /* default interface */ | |
91447636 A |
65 | extern lck_mtx_t * atalk_mutex; |
66 | ||
67 | #define f_flag f_fglob->fg_flag | |
68 | #define f_type f_fglob->fg_type | |
69 | #define f_msgcount f_fglob->fg_msgcount | |
70 | #define f_cred f_fglob->fg_cred | |
71 | #define f_ops f_fglob->fg_ops | |
72 | #define f_offset f_fglob->fg_offset | |
73 | #define f_data f_fglob->fg_data | |
1c79356b A |
74 | |
75 | int ATsocket(proc, uap, retval) | |
91447636 | 76 | struct proc *proc; |
55e303ae | 77 | struct ATsocket_args *uap; |
1c79356b A |
78 | int *retval; |
79 | { | |
80 | int err; | |
91447636 | 81 | atalk_lock(); |
1c79356b A |
82 | if (sys_ATsocket) { |
83 | /* required check for all AppleTalk system calls */ | |
84 | if (!(at_state.flags & AT_ST_STARTED) || !ifID_home) { | |
85 | *retval = -1; | |
86 | err = ENOTREADY; | |
87 | } else { | |
88 | *retval = (*sys_ATsocket)(uap->proto, &err, proc); | |
89 | } | |
90 | } else { | |
91 | *retval = -1; | |
92 | err = ENXIO; | |
93 | } | |
91447636 | 94 | atalk_unlock(); |
1c79356b A |
95 | return err; |
96 | } | |
97 | ||
98 | int ATgetmsg(proc, uap, retval) | |
91447636 | 99 | struct proc *proc; |
55e303ae | 100 | struct ATgetmsg_args *uap; |
1c79356b A |
101 | int *retval; |
102 | { | |
103 | int err; | |
104 | ||
91447636 | 105 | atalk_lock(); |
1c79356b A |
106 | if (sys_ATgetmsg) { |
107 | /* required check for all AppleTalk system calls */ | |
108 | if (!(at_state.flags & AT_ST_STARTED) || !ifID_home) { | |
109 | *retval = -1; | |
110 | err = ENOTREADY; | |
111 | } else { | |
112 | *retval = | |
113 | (*sys_ATgetmsg)(uap->fd, uap->ctlptr, uap->datptr, | |
114 | uap->flags, &err, proc); | |
115 | } | |
116 | } else { | |
117 | *retval = -1; | |
118 | err = ENXIO; | |
119 | } | |
91447636 | 120 | atalk_unlock(); |
1c79356b A |
121 | return err; |
122 | } | |
123 | ||
55e303ae | 124 | int ATputmsg(proc, uap, retval) |
91447636 | 125 | struct proc *proc; |
55e303ae | 126 | struct ATputmsg_args *uap; |
1c79356b A |
127 | int *retval; |
128 | { | |
129 | int err; | |
130 | ||
91447636 | 131 | atalk_lock(); |
1c79356b A |
132 | if (sys_ATputmsg) { |
133 | /* required check for all AppleTalk system calls */ | |
134 | if (!(at_state.flags & AT_ST_STARTED) || !ifID_home) { | |
135 | *retval = -1; | |
136 | err = ENOTREADY; | |
137 | } else { | |
138 | *retval = | |
139 | (*sys_ATputmsg)(uap->fd, uap->ctlptr, uap->datptr, | |
140 | uap->flags, &err, proc); | |
141 | } | |
142 | } else { | |
143 | *retval = -1; | |
144 | err = ENXIO; | |
145 | } | |
91447636 | 146 | atalk_unlock(); |
1c79356b A |
147 | return err; |
148 | } | |
149 | ||
150 | int ATPsndreq(proc, uap, retval) | |
91447636 | 151 | struct proc *proc; |
55e303ae | 152 | struct ATPsndreq_args *uap; |
1c79356b A |
153 | int *retval; |
154 | { | |
155 | int err; | |
156 | ||
91447636 | 157 | atalk_lock(); |
1c79356b A |
158 | if (sys_ATPsndreq) { |
159 | /* required check for all AppleTalk system calls */ | |
160 | if (!(at_state.flags & AT_ST_STARTED) || !ifID_home) { | |
161 | *retval = -1; | |
162 | err = ENOTREADY; | |
163 | } else { | |
164 | *retval = | |
165 | (*sys_ATPsndreq)(uap->fd, uap->buf, uap->len, | |
166 | uap->nowait, &err, proc); | |
167 | } | |
168 | } else { | |
169 | *retval = -1; | |
170 | err= ENXIO; | |
171 | } | |
91447636 | 172 | atalk_unlock(); |
1c79356b A |
173 | return err; |
174 | } | |
175 | ||
55e303ae | 176 | int ATPsndrsp(proc, uap, retval) |
91447636 | 177 | struct proc *proc; |
55e303ae | 178 | struct ATPsndrsp_args *uap; |
1c79356b A |
179 | int *retval; |
180 | { | |
181 | int err; | |
182 | ||
91447636 | 183 | atalk_lock(); |
1c79356b A |
184 | if (sys_ATPsndrsp) { |
185 | /* required check for all AppleTalk system calls */ | |
186 | if (!(at_state.flags & AT_ST_STARTED) || !ifID_home) { | |
187 | *retval = -1; | |
188 | err = ENOTREADY; | |
189 | } else { | |
190 | *retval = | |
191 | (*sys_ATPsndrsp)(uap->fd, uap->respbuff, | |
192 | uap->resplen, uap->datalen, &err, proc); | |
193 | } | |
194 | } else { | |
195 | *retval = -1; | |
196 | err = ENXIO; | |
197 | } | |
91447636 | 198 | atalk_unlock(); |
1c79356b A |
199 | return err; |
200 | } | |
201 | ||
55e303ae | 202 | int ATPgetreq(proc, uap, retval) |
91447636 | 203 | struct proc *proc; |
55e303ae | 204 | struct ATPgetreq_args *uap; |
1c79356b A |
205 | int *retval; |
206 | { | |
207 | int err; | |
208 | ||
91447636 | 209 | atalk_lock(); |
1c79356b A |
210 | if (sys_ATPgetreq) { |
211 | /* required check for all AppleTalk system calls */ | |
212 | if (!(at_state.flags & AT_ST_STARTED) || !ifID_home) { | |
213 | *retval = -1; | |
214 | err = ENOTREADY; | |
215 | } else { | |
216 | *retval = | |
217 | (*sys_ATPgetreq)(uap->fd, uap->buf, uap->buflen, | |
218 | &err, proc); | |
219 | } | |
220 | } else { | |
221 | *retval = -1; | |
222 | err = ENXIO; | |
223 | } | |
91447636 | 224 | atalk_unlock(); |
1c79356b A |
225 | return err; |
226 | } | |
227 | ||
55e303ae | 228 | int ATPgetrsp(proc, uap, retval) |
91447636 | 229 | struct proc *proc; |
55e303ae | 230 | struct ATPgetrsp_args *uap; |
1c79356b A |
231 | int *retval; |
232 | { | |
233 | int err = 0; | |
234 | ||
91447636 | 235 | atalk_lock(); |
1c79356b A |
236 | if (sys_ATPgetrsp) { |
237 | /* required check for all AppleTalk system calls */ | |
238 | if (!(at_state.flags & AT_ST_STARTED) || !ifID_home) { | |
239 | *retval = -1; | |
240 | err = ENOTREADY; | |
241 | } else { | |
242 | *retval = | |
243 | (*sys_ATPgetrsp)(uap->fd, uap->bdsp, &err, proc); | |
244 | } | |
245 | } else { | |
246 | *retval = -1; | |
247 | err = ENXIO; | |
248 | } | |
91447636 | 249 | atalk_unlock(); |
1c79356b A |
250 | return err; |
251 | } | |
252 | ||
91447636 A |
253 | int atalk_closeref(fg, grefp) |
254 | struct fileglob *fg; | |
1c79356b A |
255 | gref_t **grefp; |
256 | { | |
91447636 A |
257 | if ((*grefp = (gref_t *)fg->fg_data)) { |
258 | fg->fg_data = 0; | |
1c79356b A |
259 | return(0); |
260 | } | |
261 | return(EBADF); | |
262 | } | |
263 | ||
264 | int atalk_openref(gref, retfd, proc) | |
265 | gref_t *gref; | |
266 | int *retfd; | |
267 | struct proc *proc; | |
268 | { | |
55e303ae | 269 | extern int _ATread(), _ATwrite(),_ATioctl(), _ATselect(), _ATclose(), _ATkqfilter(); |
1c79356b | 270 | static struct fileops fileops = |
91447636 | 271 | {_ATread, _ATwrite, _ATioctl, _ATselect, _ATclose, _ATkqfilter, 0}; |
1c79356b | 272 | int err, fd; |
91447636 A |
273 | struct fileproc *fp; |
274 | ||
275 | lck_mtx_assert(atalk_mutex, LCK_MTX_ASSERT_OWNED); | |
276 | ||
277 | proc_fdlock(proc); | |
278 | if ((err = falloc_locked(proc, &fp, &fd, 1)) != 0) { | |
279 | proc_fdunlock(proc); | |
1c79356b A |
280 | return err; |
281 | } | |
282 | ||
283 | fp->f_flag = FREAD|FWRITE; | |
284 | /*##### LD 5/7/96 Warning: we don't have a "DTYPE_OTHER" for | |
285 | * MacOSX, so defines DTYPE_ATALK as DTYPE_SOCKET... | |
286 | */ | |
287 | fp->f_type = DTYPE_ATALK+1; | |
288 | fp->f_ops = &fileops; | |
91447636 A |
289 | fp->f_data = (void *)gref; |
290 | ||
1c79356b A |
291 | *fdflags(proc, fd) &= ~UF_RESERVED; |
292 | *retfd = fd; | |
91447636 A |
293 | fp_drop(proc, fd, fp, 1); |
294 | proc_fdunlock(proc); | |
1c79356b A |
295 | /* |
296 | kprintf("atalk_openref: fp = 0x%x, gref = 0x%x\n", (u_int)fp, (u_int)gref); | |
297 | */ | |
1c79356b A |
298 | return 0; |
299 | } | |
300 | ||
91447636 A |
301 | /* |
302 | * go from file descriptor to gref, which has been saved in fp->f_data | |
303 | * | |
304 | * This routine returns with an iocount on the fileproc when the fp is null | |
305 | * as it converts fd to fileproc. Callers of this api who pass fp as null | |
306 | * need to drop the iocount when they are done with the fp | |
307 | */ | |
308 | int atalk_getref(fp, fd, grefp, proc, droponerr) | |
309 | struct fileproc *fp; | |
1c79356b A |
310 | int fd; |
311 | gref_t **grefp; | |
312 | struct proc *proc; | |
91447636 | 313 | int droponerr; |
1c79356b | 314 | { |
91447636 | 315 | int error; |
1c79356b | 316 | |
91447636 A |
317 | proc_fdlock(proc); |
318 | error = atalk_getref_locked(fp, fd, grefp, proc, droponerr); | |
319 | proc_fdunlock(proc); | |
320 | return error; | |
321 | } | |
1c79356b | 322 | |
91447636 A |
323 | int atalk_getref_locked(fp, fd, grefp, proc, droponerr) |
324 | struct fileproc *fp; | |
325 | int fd; | |
326 | gref_t **grefp; | |
327 | struct proc *proc; | |
328 | int droponerr; | |
329 | { | |
330 | lck_mtx_assert(atalk_mutex, LCK_MTX_ASSERT_OWNED); | |
331 | if (fp == 0) { | |
332 | int error = fp_lookup(proc, fd, &fp, 1); | |
333 | ||
334 | if (error) { | |
335 | ||
336 | *grefp = (gref_t *) 0; | |
337 | return EBADF; | |
338 | } | |
339 | } | |
340 | *grefp = (gref_t *)fp->f_data; | |
341 | if (*grefp == 0 || *grefp == (gref_t *)(-1)) { | |
342 | if (droponerr) | |
343 | fp_drop(proc, fd, fp, 1); | |
344 | printf("atalk_getref_locked EBADF f_data: %x\n", fp->f_data); | |
345 | return EBADF; | |
346 | } | |
347 | ||
348 | if ((*grefp)->errno) { | |
349 | if (droponerr) | |
350 | fp_drop(proc, fd, fp, 1); | |
351 | return (int)(*grefp)->errno; | |
352 | } | |
353 | return 0; | |
1c79356b | 354 | } |