]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netat/adsp_stream.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1995-1998 Apple Computer, Inc.
30 * All Rights Reserved.
34 * 09/07/95 - Modified for performance (Tuyen Nguyen)
35 * Modified for MP, 1996 by Tuyen Nguyen
36 * Modified, April 9, 1997 by Tuyen Nguyen for MacOSX.
38 #include <sys/errno.h>
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <machine/spl.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
45 #include <sys/filedesc.h>
46 #include <sys/fcntl.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
51 #include <sys/ioctl.h>
52 #include <sys/malloc.h>
56 #include <netat/sysglue.h>
57 #include <netat/appletalk.h>
58 #include <netat/ddp.h>
59 #include <netat/at_snmp.h>
60 #include <netat/at_pcb.h>
61 #include <netat/debug.h>
62 #include <netat/at_var.h>
63 #include <netat/adsp.h>
64 #include <netat/adsp_internal.h>
66 void adsp_rput(gref_t
*, gbuf_t
*);
67 static void adsp_iocack(gref_t
*, gbuf_t
*);
68 static void adsp_iocnak(gref_t
*, gbuf_t
*, int err
);
69 void adsp_dequeue_ccb(CCB
*);
77 char adsp_inputC
[256];
78 CCB
*adsp_inputQ
[256];
80 extern at_ifaddr_t
*ifID_home
;
92 switch (gbuf_type(mp
)) {
94 p
= (at_ddp_t
*)gbuf_rptr(mp
);
95 sp
= adsp_inputQ
[p
->dst_socket
];
96 if ((sp
== 0) || (sp
->gref
==0) || (sp
->state
==sClosed
))
101 else if (sp
->otccbLink
!= 0) {
103 if ((sp
->remoteAddress
.a
.node
== p
->src_node
)
104 && (sp
->remoteAddress
.a
.socket
== p
->src_socket
)
105 && (sp
->remoteAddress
.a
.net
== NET_VALUE(p
->src_net
)))
107 } while ((sp
= sp
->otccbLink
) != 0);
116 if (sp
->deferred_mb
) {
117 for (mb
=sp
->deferred_mb
; gbuf_next(mb
); mb
=gbuf_next(mb
)) ;
120 sp
->deferred_mb
= mp
;
125 adsp_rput(sp
->gref
, mp
);
126 if ((mp
= sp
->deferred_mb
) != 0) {
127 sp
->deferred_mb
= gbuf_next(mp
);
136 gref
= (gref_t
*)((ioc_t
*)gbuf_rptr(mp
))->ioc_private
;
140 #ifdef APPLETALK_DEBUG
141 kprintf("unexpected MSG_IOCTL in adsp_input()");
154 int adsp_readable(gref_t
*);
156 int adsp_readable(gref
)
164 * we don't have the structure we need to determine
165 * if there's data available... we return readable in
166 * this case to keep from hanging up in the select
167 * a subsequent read will run into the same missing data
168 * structure and return an error... the ATselect code does
169 * this if it can't retrieve the 'gref' structure from the
170 * file table for the fd specified
174 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
180 int adsp_writeable(gref_t
*);
181 int adsp_writeable(gref
)
189 * we don't have the structure we need to determine
190 * if there's room available... we return writeable in
191 * this case to keep from hanging up in the select
192 * a subsequent write will run into the same missing data
193 * structure and return an error... the ATselect code does
194 * this if it can't retrieve the 'gref' structure from the
195 * file table for the fd specified
199 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
200 rc
= CalcSendQFree(sp
);
205 static void adsp_init(void);
207 static void adsp_init(void)
212 bzero(adsp_pidM
, sizeof(adsp_pidM
));
213 bzero(adsp_inputC
, sizeof(adsp_inputC
));
214 bzero(adsp_inputQ
, sizeof(adsp_inputQ
));
219 * ADSP open and close routines. These routines
220 * initalize and release the ADSP structures. They do not
221 * have anything to do with "connections"
232 if (!adspAllocateCCB(gref
))
233 return(ENOBUFS
); /* can't get buffers */
235 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
236 gref
->readable
= adsp_readable
;
237 gref
->writeable
= adsp_writeable
;
238 if ((sp
->otccbLink
= ccb_used_list
) != 0)
239 sp
->otccbLink
->ccbLink
= sp
;
247 unsigned char localSocket
;
249 /* make sure we've not yet removed the CCB (e.g., due to TrashSession) */
251 CCBPtr sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
252 localSocket
= sp
->localSocket
;
257 adsp_dequeue_ccb(sp
);
258 gbuf_freeb((gbuf_t
*)gref
->info
);
270 * ADSP streams read put and service routines.
273 void adsp_rput(gref
, mp
)
274 gref_t
*gref
; /* READ queue */
277 switch (gbuf_type(mp
)) {
281 switch (adspReadHandler(gref
, mp
)) {
283 atalk_putnext(gref
, mp
);
290 #ifdef APPLETALK_DEBUG
291 kprintf("adsp_rput received MSG_ERROR");
295 CheckReadQueue((CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
)));
296 CheckSend((CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
)));
298 switch (gbuf_type(mp
)) {
302 if (adspReadHandler(gref
, mp
) == STR_PUTNEXT
)
303 atalk_putnext(gref
, mp
);
306 atalk_putnext(gref
, mp
);
317 * ADSP streams write put and service routines.
321 int adsp_wput(gref
, mp
)
322 gref_t
*gref
; /* WRITE queue */
331 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
335 if (gbuf_type(mp
) == MSG_IOCTL
) {
336 iocbp
= (ioc_t
*)gbuf_rptr(mp
);
337 switch (iocbp
->ioc_cmd
) {
342 if (gbuf_cont(mp
) == NULL
) {
343 iocbp
->ioc_rval
= -1;
344 adsp_iocnak(gref
, mp
, EINVAL
);
346 v
= *(unsigned char *)gbuf_rptr(gbuf_cont(mp
));
348 && ((v
> DDP_SOCKET_LAST
) || (v
< 2)
349 || ddp_socket_inuse(v
, DDP_ADSP
))) {
350 iocbp
->ioc_rval
= -1;
351 adsp_iocnak(gref
, mp
, EINVAL
);
355 if ((v
= adspAssignSocket(gref
, 0)) == 0) {
356 iocbp
->ioc_rval
= -1;
357 adsp_iocnak(gref
, mp
, EINVAL
);
363 adsp_pidM
[v
] = sp
->pid
;
364 adsp_dequeue_ccb(sp
);
366 *(unsigned char *)gbuf_rptr(gbuf_cont(mp
)) = v
;
369 adsp_iocack(gref
, mp
);
379 if (((xm
= gbuf_cont(mp
)) == NULL
)
380 && ((xm
= gbuf_alloc(sizeof(at_inet_t
), PRI_MED
)) == NULL
)) {
381 iocbp
->ioc_rval
= -1;
382 adsp_iocnak(gref
, mp
, ENOBUFS
);
386 gbuf_wset(xm
,sizeof(at_inet_t
));
387 addr
= (at_inet_t
*)gbuf_rptr(xm
);
388 if (iocbp
->ioc_cmd
== ADSPGETSOCK
) {
389 /* Obtain Network and Node Id's from DDP */
390 /* *** was ddp_get_cfg() *** */
391 addr
->net
= ifID_home
->ifThisNode
.s_net
;
392 addr
->node
= ifID_home
->ifThisNode
.s_node
;
393 addr
->socket
= (sp
)? sp
->localSocket
: 0;
396 *addr
= sp
->remoteAddress
.a
;
403 adsp_iocack(gref
, mp
);
406 case DDP_IOC_GET_CFG
:
407 /* respond to an DDP_IOC_GET_CFG sent on an adsp fd */
408 if (((xm
= gbuf_cont(mp
)) == NULL
) &&
409 (xm
= gbuf_alloc(sizeof(ddp_addr_t
), PRI_MED
)) == NULL
) {
410 iocbp
->ioc_rval
= -1;
411 adsp_iocnak(gref
, mp
, ENOBUFS
);
415 gbuf_wset(xm
, sizeof(ddp_addr_t
));
416 /* Obtain Network and Node Id's from DDP */
418 /* *** was ddp_get_cfg() *** */
420 (ddp_addr_t
*)gbuf_rptr(gbuf_cont(mp
));
421 cfgp
->inet
.net
= ifID_home
->ifThisNode
.s_net
;
422 cfgp
->inet
.node
= ifID_home
->ifThisNode
.s_node
;
423 cfgp
->inet
.socket
= (sp
)? sp
->localSocket
: 0;
424 cfgp
->ddptype
= DDP_ADSP
;
427 adsp_iocack(gref
, mp
);
435 rc
= adspWriteHandler(gref
, mp
);
439 if (gbuf_type(mp
) == MSG_IOCTL
) {
440 iocbp
= (ioc_t
*)gbuf_rptr(mp
);
441 iocbp
->ioc_private
= (void *)gref
;
457 void adspioc_ack(errno
, m
, gref
)
466 iocbp
= (ioc_t
*) gbuf_rptr(m
);
468 iocbp
->ioc_error
= errno
; /* set the errno */
469 iocbp
->ioc_count
= gbuf_msgsize(gbuf_cont(m
));
470 if (gbuf_type(m
) == MSG_IOCTL
) /* if an ioctl, this is an ack */
471 gbuf_set_type(m
, MSG_IOCACK
); /* and ALWAYS update the user */
472 /* ioctl structure */
473 trace_mbufs(D_M_ADSP
,"A ", m
);
477 static void adsp_iocack(gref
, m
)
481 if (gbuf_type(m
) == MSG_IOCTL
)
482 gbuf_set_type(m
, MSG_IOCACK
);
485 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= gbuf_msgsize(gbuf_cont(m
));
487 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= 0;
493 static void adsp_iocnak(gref
, m
, err
)
498 if (gbuf_type(m
) == MSG_IOCTL
)
499 gbuf_set_type(m
, MSG_IOCNAK
);
500 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= 0;
504 ((ioc_t
*)gbuf_rptr(m
))->ioc_error
= err
;
507 gbuf_freem(gbuf_cont(m
));
514 adspAssignSocket(gref
, flag
)
518 unsigned char sVal
, sMax
, sMin
, sSav
= 0, inputC
;
521 sMax
= flag
? DDP_SOCKET_LAST
-46 : DDP_SOCKET_LAST
-6;
522 sMin
= DDP_SOCKET_1st_DYNAMIC
;
524 for (inputC
=255, sVal
=sMax
; sVal
>= sMin
; sVal
--) {
525 if (!ddp_socket_inuse(sVal
, DDP_ADSP
))
528 if (adsp_inputC
[sVal
] &&
529 /* meaning that raw DDP doesn't have it */
530 (adsp_inputC
[sVal
] < inputC
)
531 && (adsp_inputQ
[sVal
]->state
== sOpen
)) {
532 inputC
= adsp_inputC
[sVal
];
538 if (!flag
|| (inputC
== 255))
542 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
543 adsp_dequeue_ccb(sp
);
545 sp
->otccbLink
= adsp_inputQ
[sVal
];
546 adsp_inputQ
[sVal
] = sp
;
548 adsp_pidM
[sVal
] = sp
->pid
;
553 adspDeassignSocket(sp
)
561 dPrintf(D_M_ADSP
, D_L_TRACE
, ("adspDeassignSocket: pid=%d,s=%d\n",
562 sp
->pid
, sp
->localSocket
));
563 sVal
= sp
->localSocket
;
564 if ((curr_sp
= adsp_inputQ
[sVal
]) != 0) {
566 while (curr_sp
!= sp
) {
568 curr_sp
= curr_sp
->otccbLink
;
572 prev_sp
->otccbLink
= sp
->otccbLink
;
574 adsp_inputQ
[sVal
] = sp
->otccbLink
;
575 if (adsp_inputQ
[sVal
])
578 pid
= adsp_pidM
[sVal
];
579 adsp_inputC
[sVal
] = 0;
589 dPrintf(D_M_ADSP
, D_L_ERROR
,
590 ("adspDeassignSocket: closing, no CCB block, trouble ahead\n"));
592 } /* adspDeassignSocket */
595 * remove CCB from the use list
602 if (sp
== ccb_used_list
) {
603 if ((ccb_used_list
= sp
->otccbLink
) != 0)
604 sp
->otccbLink
->ccbLink
= 0;
605 } else if (sp
->ccbLink
) {
606 if ((sp
->ccbLink
->otccbLink
= sp
->otccbLink
) != 0)
607 sp
->otccbLink
->ccbLink
= sp
->ccbLink
;
614 void SndMsgUp(gref
, mp
)
615 gref_t
*gref
; /* WRITE queue */
619 dPrintf(D_M_ADSP, D_L_TRACE,
620 ("SndMsgUp: gref=0x%x, mbuf=0x%x\n", (unsigned)gref, (unsigned)mp));
621 trace_mbufs(D_M_ADSP, " m", mp);
623 atalk_putnext(gref
, mp
);