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