]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netat/adsp_stream.c
7eaaaabfc302bd8fdf0312dc321958537dd1a3f4
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1995-1998 Apple Computer, Inc.
32 * All Rights Reserved.
36 * 09/07/95 - Modified for performance (Tuyen Nguyen)
37 * Modified for MP, 1996 by Tuyen Nguyen
38 * Modified, April 9, 1997 by Tuyen Nguyen for MacOSX.
40 #include <sys/errno.h>
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <machine/spl.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
47 #include <sys/filedesc.h>
48 #include <sys/fcntl.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
53 #include <sys/ioctl.h>
54 #include <sys/malloc.h>
58 #include <netat/sysglue.h>
59 #include <netat/appletalk.h>
60 #include <netat/ddp.h>
61 #include <netat/at_snmp.h>
62 #include <netat/at_pcb.h>
63 #include <netat/debug.h>
64 #include <netat/at_var.h>
65 #include <netat/adsp.h>
66 #include <netat/adsp_internal.h>
70 static void adsp_iocack();
71 static void adsp_iocnak();
72 void adsp_dequeue_ccb();
73 unsigned char adspAssignSocket();
74 int adspallocate(), adsprelease();
82 char adsp_inputC
[256];
83 CCB
*adsp_inputQ
[256];
85 extern at_ifaddr_t
*ifID_home
;
97 switch (gbuf_type(mp
)) {
99 p
= (at_ddp_t
*)gbuf_rptr(mp
);
100 sp
= adsp_inputQ
[p
->dst_socket
];
101 if ((sp
== 0) || (sp
->gref
==0) || (sp
->state
==sClosed
))
106 else if (sp
->otccbLink
!= 0) {
108 if ((sp
->remoteAddress
.a
.node
== p
->src_node
)
109 && (sp
->remoteAddress
.a
.socket
== p
->src_socket
)
110 && (sp
->remoteAddress
.a
.net
== NET_VALUE(p
->src_net
)))
112 } while ((sp
= sp
->otccbLink
) != 0);
121 if (sp
->deferred_mb
) {
122 for (mb
=sp
->deferred_mb
; gbuf_next(mb
); mb
=gbuf_next(mb
)) ;
125 sp
->deferred_mb
= mp
;
130 adsp_rput(sp
->gref
, mp
);
131 if ((mp
= sp
->deferred_mb
) != 0) {
132 sp
->deferred_mb
= gbuf_next(mp
);
141 gref
= (gref_t
*)((ioc_t
*)gbuf_rptr(mp
))->ioc_private
;
145 #ifdef APPLETALK_DEBUG
146 kprintf("unexpected MSG_IOCTL in adsp_input()");
159 int adsp_readable(gref
)
167 * we don't have the structure we need to determine
168 * if there's data available... we return readable in
169 * this case to keep from hanging up in the select
170 * a subsequent read will run into the same missing data
171 * structure and return an error... the ATselect code does
172 * this if it can't retrieve the 'gref' structure from the
173 * file table for the fd specified
177 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
183 int adsp_writeable(gref
)
191 * we don't have the structure we need to determine
192 * if there's room available... we return writeable in
193 * this case to keep from hanging up in the select
194 * a subsequent write will run into the same missing data
195 * structure and return an error... the ATselect code does
196 * this if it can't retrieve the 'gref' structure from the
197 * file table for the fd specified
201 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
202 rc
= CalcSendQFree(sp
);
207 static void adsp_init()
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(gbuf_rptr(((gbuf_t
*)gref
->info
)));
296 CheckSend(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
, 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
);