]>
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>
68 static void adsp_iocack();
69 static void adsp_iocnak();
70 void adsp_dequeue_ccb();
71 unsigned char adspAssignSocket();
72 int adspallocate(), adsprelease();
80 char adsp_inputC
[256];
81 CCB
*adsp_inputQ
[256];
83 extern at_ifaddr_t
*ifID_home
;
95 switch (gbuf_type(mp
)) {
97 p
= (at_ddp_t
*)gbuf_rptr(mp
);
98 sp
= adsp_inputQ
[p
->dst_socket
];
99 if ((sp
== 0) || (sp
->gref
==0) || (sp
->state
==sClosed
))
104 else if (sp
->otccbLink
!= 0) {
106 if ((sp
->remoteAddress
.a
.node
== p
->src_node
)
107 && (sp
->remoteAddress
.a
.socket
== p
->src_socket
)
108 && (sp
->remoteAddress
.a
.net
== NET_VALUE(p
->src_net
)))
110 } while ((sp
= sp
->otccbLink
) != 0);
119 if (sp
->deferred_mb
) {
120 for (mb
=sp
->deferred_mb
; gbuf_next(mb
); mb
=gbuf_next(mb
)) ;
123 sp
->deferred_mb
= mp
;
128 adsp_rput(sp
->gref
, mp
);
129 if ((mp
= sp
->deferred_mb
) != 0) {
130 sp
->deferred_mb
= gbuf_next(mp
);
139 gref
= (gref_t
*)((ioc_t
*)gbuf_rptr(mp
))->ioc_private
;
143 #ifdef APPLETALK_DEBUG
144 kprintf("unexpected MSG_IOCTL in adsp_input()");
157 int adsp_readable(gref
)
165 * we don't have the structure we need to determine
166 * if there's data available... we return readable in
167 * this case to keep from hanging up in the select
168 * a subsequent read will run into the same missing data
169 * structure and return an error... the ATselect code does
170 * this if it can't retrieve the 'gref' structure from the
171 * file table for the fd specified
175 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
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()
210 bzero(adsp_pidM
, sizeof(adsp_pidM
));
211 bzero(adsp_inputC
, sizeof(adsp_inputC
));
212 bzero(adsp_inputQ
, sizeof(adsp_inputQ
));
217 * ADSP open and close routines. These routines
218 * initalize and release the ADSP structures. They do not
219 * have anything to do with "connections"
230 if (!adspAllocateCCB(gref
))
231 return(ENOBUFS
); /* can't get buffers */
233 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
234 gref
->readable
= adsp_readable
;
235 gref
->writeable
= adsp_writeable
;
236 if ((sp
->otccbLink
= ccb_used_list
) != 0)
237 sp
->otccbLink
->ccbLink
= sp
;
245 unsigned char localSocket
;
247 /* make sure we've not yet removed the CCB (e.g., due to TrashSession) */
249 CCBPtr sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
250 localSocket
= sp
->localSocket
;
255 adsp_dequeue_ccb(sp
);
256 gbuf_freeb((gbuf_t
*)gref
->info
);
268 * ADSP streams read put and service routines.
271 void adsp_rput(gref
, mp
)
272 gref_t
*gref
; /* READ queue */
275 switch (gbuf_type(mp
)) {
279 switch (adspReadHandler(gref
, mp
)) {
281 atalk_putnext(gref
, mp
);
288 #ifdef APPLETALK_DEBUG
289 kprintf("adsp_rput received MSG_ERROR");
293 CheckReadQueue(gbuf_rptr(((gbuf_t
*)gref
->info
)));
294 CheckSend(gbuf_rptr(((gbuf_t
*)gref
->info
)));
296 switch (gbuf_type(mp
)) {
300 if (adspReadHandler(gref
, mp
) == STR_PUTNEXT
)
301 atalk_putnext(gref
, mp
);
304 atalk_putnext(gref
, mp
);
315 * ADSP streams write put and service routines.
319 int adsp_wput(gref
, mp
)
320 gref_t
*gref
; /* WRITE queue */
329 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
333 if (gbuf_type(mp
) == MSG_IOCTL
) {
334 iocbp
= (ioc_t
*)gbuf_rptr(mp
);
335 switch (iocbp
->ioc_cmd
) {
340 if (gbuf_cont(mp
) == NULL
) {
341 iocbp
->ioc_rval
= -1;
342 adsp_iocnak(gref
, mp
, EINVAL
);
344 v
= *(unsigned char *)gbuf_rptr(gbuf_cont(mp
));
346 && ((v
> DDP_SOCKET_LAST
) || (v
< 2)
347 || ddp_socket_inuse(v
, DDP_ADSP
))) {
348 iocbp
->ioc_rval
= -1;
349 adsp_iocnak(gref
, mp
, EINVAL
);
353 if ((v
= adspAssignSocket(gref
, 0)) == 0) {
354 iocbp
->ioc_rval
= -1;
355 adsp_iocnak(gref
, mp
, EINVAL
);
361 adsp_pidM
[v
] = sp
->pid
;
362 adsp_dequeue_ccb(sp
);
364 *(unsigned char *)gbuf_rptr(gbuf_cont(mp
)) = v
;
367 adsp_iocack(gref
, mp
);
377 if (((xm
= gbuf_cont(mp
)) == NULL
)
378 && ((xm
= gbuf_alloc(sizeof(at_inet_t
), PRI_MED
)) == NULL
)) {
379 iocbp
->ioc_rval
= -1;
380 adsp_iocnak(gref
, mp
, ENOBUFS
);
384 gbuf_wset(xm
,sizeof(at_inet_t
));
385 addr
= (at_inet_t
*)gbuf_rptr(xm
);
386 if (iocbp
->ioc_cmd
== ADSPGETSOCK
) {
387 /* Obtain Network and Node Id's from DDP */
388 /* *** was ddp_get_cfg() *** */
389 addr
->net
= ifID_home
->ifThisNode
.s_net
;
390 addr
->node
= ifID_home
->ifThisNode
.s_node
;
391 addr
->socket
= (sp
)? sp
->localSocket
: 0;
394 *addr
= sp
->remoteAddress
.a
;
401 adsp_iocack(gref
, mp
);
404 case DDP_IOC_GET_CFG
:
405 /* respond to an DDP_IOC_GET_CFG sent on an adsp fd */
406 if (((xm
= gbuf_cont(mp
)) == NULL
) &&
407 (xm
= gbuf_alloc(sizeof(ddp_addr_t
), PRI_MED
)) == NULL
) {
408 iocbp
->ioc_rval
= -1;
409 adsp_iocnak(gref
, mp
, ENOBUFS
);
413 gbuf_wset(xm
, sizeof(ddp_addr_t
));
414 /* Obtain Network and Node Id's from DDP */
416 /* *** was ddp_get_cfg() *** */
418 (ddp_addr_t
*)gbuf_rptr(gbuf_cont(mp
));
419 cfgp
->inet
.net
= ifID_home
->ifThisNode
.s_net
;
420 cfgp
->inet
.node
= ifID_home
->ifThisNode
.s_node
;
421 cfgp
->inet
.socket
= (sp
)? sp
->localSocket
: 0;
422 cfgp
->ddptype
= DDP_ADSP
;
425 adsp_iocack(gref
, mp
);
433 rc
= adspWriteHandler(gref
, mp
);
437 if (gbuf_type(mp
) == MSG_IOCTL
) {
438 iocbp
= (ioc_t
*)gbuf_rptr(mp
);
439 iocbp
->ioc_private
= (void *)gref
;
455 void adspioc_ack(errno
, m
, gref
)
464 iocbp
= (ioc_t
*) gbuf_rptr(m
);
466 iocbp
->ioc_error
= errno
; /* set the errno */
467 iocbp
->ioc_count
= gbuf_msgsize(gbuf_cont(m
));
468 if (gbuf_type(m
) == MSG_IOCTL
) /* if an ioctl, this is an ack */
469 gbuf_set_type(m
, MSG_IOCACK
); /* and ALWAYS update the user */
470 /* ioctl structure */
471 trace_mbufs(D_M_ADSP
,"A ", m
);
475 static void adsp_iocack(gref
, m
)
479 if (gbuf_type(m
) == MSG_IOCTL
)
480 gbuf_set_type(m
, MSG_IOCACK
);
483 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= gbuf_msgsize(gbuf_cont(m
));
485 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= 0;
491 static void adsp_iocnak(gref
, m
, err
)
496 if (gbuf_type(m
) == MSG_IOCTL
)
497 gbuf_set_type(m
, MSG_IOCNAK
);
498 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= 0;
502 ((ioc_t
*)gbuf_rptr(m
))->ioc_error
= err
;
505 gbuf_freem(gbuf_cont(m
));
512 adspAssignSocket(gref
, flag
)
516 unsigned char sVal
, sMax
, sMin
, sSav
, inputC
;
519 sMax
= flag
? DDP_SOCKET_LAST
-46 : DDP_SOCKET_LAST
-6;
520 sMin
= DDP_SOCKET_1st_DYNAMIC
;
522 for (inputC
=255, sVal
=sMax
; sVal
>= sMin
; sVal
--) {
523 if (!ddp_socket_inuse(sVal
, DDP_ADSP
))
526 if (adsp_inputC
[sVal
] &&
527 /* meaning that raw DDP doesn't have it */
528 (adsp_inputC
[sVal
] < inputC
)
529 && (adsp_inputQ
[sVal
]->state
== sOpen
)) {
530 inputC
= adsp_inputC
[sVal
];
536 if (!flag
|| (inputC
== 255))
540 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
541 adsp_dequeue_ccb(sp
);
543 sp
->otccbLink
= adsp_inputQ
[sVal
];
544 adsp_inputQ
[sVal
] = sp
;
546 adsp_pidM
[sVal
] = sp
->pid
;
551 adspDeassignSocket(sp
)
559 dPrintf(D_M_ADSP
, D_L_TRACE
, ("adspDeassignSocket: pid=%d,s=%d\n",
560 sp
->pid
, sp
->localSocket
));
561 sVal
= sp
->localSocket
;
562 if ((curr_sp
= adsp_inputQ
[sVal
]) != 0) {
564 while (curr_sp
!= sp
) {
566 curr_sp
= curr_sp
->otccbLink
;
570 prev_sp
->otccbLink
= sp
->otccbLink
;
572 adsp_inputQ
[sVal
] = sp
->otccbLink
;
573 if (adsp_inputQ
[sVal
])
576 pid
= adsp_pidM
[sVal
];
577 adsp_inputC
[sVal
] = 0;
587 dPrintf(D_M_ADSP
, D_L_ERROR
,
588 ("adspDeassignSocket: closing, no CCB block, trouble ahead\n"));
590 } /* adspDeassignSocket */
593 * remove CCB from the use list
600 if (sp
== ccb_used_list
) {
601 if ((ccb_used_list
= sp
->otccbLink
) != 0)
602 sp
->otccbLink
->ccbLink
= 0;
603 } else if (sp
->ccbLink
) {
604 if ((sp
->ccbLink
->otccbLink
= sp
->otccbLink
) != 0)
605 sp
->otccbLink
->ccbLink
= sp
->ccbLink
;
612 void SndMsgUp(gref
, mp
)
613 gref_t
*gref
; /* WRITE queue */
617 dPrintf(D_M_ADSP, D_L_TRACE,
618 ("SndMsgUp: gref=0x%x, mbuf=0x%x\n", (unsigned)gref, (unsigned)mp));
619 trace_mbufs(D_M_ADSP, " m", mp);
621 atalk_putnext(gref
, mp
);