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