]>
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_LICENSE_HEADER_START@
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.
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
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1995-1998 Apple Computer, Inc.
24 * All Rights Reserved.
28 * 09/07/95 - Modified for performance (Tuyen Nguyen)
29 * Modified for MP, 1996 by Tuyen Nguyen
30 * Modified, April 9, 1997 by Tuyen Nguyen for MacOSX.
32 #include <sys/errno.h>
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <machine/spl.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/filedesc.h>
40 #include <sys/fcntl.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
45 #include <sys/ioctl.h>
46 #include <sys/malloc.h>
50 #include <netat/sysglue.h>
51 #include <netat/appletalk.h>
52 #include <netat/ddp.h>
53 #include <netat/at_snmp.h>
54 #include <netat/at_pcb.h>
55 #include <netat/debug.h>
56 #include <netat/at_var.h>
57 #include <netat/adsp.h>
58 #include <netat/adsp_internal.h>
62 static void adsp_iocack();
63 static void adsp_iocnak();
64 void adsp_dequeue_ccb();
65 unsigned char adspAssignSocket();
66 int adspallocate(), adsprelease();
74 char adsp_inputC
[256];
75 CCB
*adsp_inputQ
[256];
77 extern at_ifaddr_t
*ifID_home
;
89 switch (gbuf_type(mp
)) {
91 p
= (at_ddp_t
*)gbuf_rptr(mp
);
92 sp
= adsp_inputQ
[p
->dst_socket
];
93 if ((sp
== 0) || (sp
->gref
==0) || (sp
->state
==sClosed
))
98 else if (sp
->otccbLink
!= 0) {
100 if ((sp
->remoteAddress
.a
.node
== p
->src_node
)
101 && (sp
->remoteAddress
.a
.socket
== p
->src_socket
)
102 && (sp
->remoteAddress
.a
.net
== NET_VALUE(p
->src_net
)))
104 } while ((sp
= sp
->otccbLink
) != 0);
113 if (sp
->deferred_mb
) {
114 for (mb
=sp
->deferred_mb
; gbuf_next(mb
); mb
=gbuf_next(mb
)) ;
117 sp
->deferred_mb
= mp
;
122 adsp_rput(sp
->gref
, mp
);
123 if ((mp
= sp
->deferred_mb
) != 0) {
124 sp
->deferred_mb
= gbuf_next(mp
);
133 gref
= (gref_t
*)((ioc_t
*)gbuf_rptr(mp
))->ioc_private
;
137 #ifdef APPLETALK_DEBUG
138 kprintf("unexpected MSG_IOCTL in adsp_input()");
151 int adsp_readable(gref
)
159 * we don't have the structure we need to determine
160 * if there's data available... we return readable in
161 * this case to keep from hanging up in the select
162 * a subsequent read will run into the same missing data
163 * structure and return an error... the ATselect code does
164 * this if it can't retrieve the 'gref' structure from the
165 * file table for the fd specified
169 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
175 int adsp_writeable(gref
)
183 * we don't have the structure we need to determine
184 * if there's room available... we return writeable in
185 * this case to keep from hanging up in the select
186 * a subsequent write will run into the same missing data
187 * structure and return an error... the ATselect code does
188 * this if it can't retrieve the 'gref' structure from the
189 * file table for the fd specified
193 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
194 rc
= CalcSendQFree(sp
);
199 static void adsp_init()
204 bzero(adsp_pidM
, sizeof(adsp_pidM
));
205 bzero(adsp_inputC
, sizeof(adsp_inputC
));
206 bzero(adsp_inputQ
, sizeof(adsp_inputQ
));
211 * ADSP open and close routines. These routines
212 * initalize and release the ADSP structures. They do not
213 * have anything to do with "connections"
224 if (!adspAllocateCCB(gref
))
225 return(ENOBUFS
); /* can't get buffers */
227 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
228 gref
->readable
= adsp_readable
;
229 gref
->writeable
= adsp_writeable
;
230 if ((sp
->otccbLink
= ccb_used_list
) != 0)
231 sp
->otccbLink
->ccbLink
= sp
;
239 unsigned char localSocket
;
241 /* make sure we've not yet removed the CCB (e.g., due to TrashSession) */
243 CCBPtr sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
244 localSocket
= sp
->localSocket
;
249 adsp_dequeue_ccb(sp
);
250 gbuf_freeb((gbuf_t
*)gref
->info
);
262 * ADSP streams read put and service routines.
265 void adsp_rput(gref
, mp
)
266 gref_t
*gref
; /* READ queue */
269 switch (gbuf_type(mp
)) {
273 switch (adspReadHandler(gref
, mp
)) {
275 atalk_putnext(gref
, mp
);
282 #ifdef APPLETALK_DEBUG
283 kprintf("adsp_rput received MSG_ERROR");
287 CheckReadQueue(gbuf_rptr(((gbuf_t
*)gref
->info
)));
288 CheckSend(gbuf_rptr(((gbuf_t
*)gref
->info
)));
290 switch (gbuf_type(mp
)) {
294 if (adspReadHandler(gref
, mp
) == STR_PUTNEXT
)
295 atalk_putnext(gref
, mp
);
298 atalk_putnext(gref
, mp
);
309 * ADSP streams write put and service routines.
313 int adsp_wput(gref
, mp
)
314 gref_t
*gref
; /* WRITE queue */
323 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
327 if (gbuf_type(mp
) == MSG_IOCTL
) {
328 iocbp
= (ioc_t
*)gbuf_rptr(mp
);
329 switch (iocbp
->ioc_cmd
) {
334 if (gbuf_cont(mp
) == NULL
) {
335 iocbp
->ioc_rval
= -1;
336 adsp_iocnak(gref
, mp
, EINVAL
);
338 v
= *(unsigned char *)gbuf_rptr(gbuf_cont(mp
));
340 && ((v
> DDP_SOCKET_LAST
) || (v
< 2)
341 || ddp_socket_inuse(v
, DDP_ADSP
))) {
342 iocbp
->ioc_rval
= -1;
343 adsp_iocnak(gref
, mp
, EINVAL
);
347 if ((v
= adspAssignSocket(gref
, 0)) == 0) {
348 iocbp
->ioc_rval
= -1;
349 adsp_iocnak(gref
, mp
, EINVAL
);
355 adsp_pidM
[v
] = sp
->pid
;
356 adsp_dequeue_ccb(sp
);
358 *(unsigned char *)gbuf_rptr(gbuf_cont(mp
)) = v
;
361 adsp_iocack(gref
, mp
);
371 if (((xm
= gbuf_cont(mp
)) == NULL
)
372 && ((xm
= gbuf_alloc(sizeof(at_inet_t
), PRI_MED
)) == NULL
)) {
373 iocbp
->ioc_rval
= -1;
374 adsp_iocnak(gref
, mp
, ENOBUFS
);
378 gbuf_wset(xm
,sizeof(at_inet_t
));
379 addr
= (at_inet_t
*)gbuf_rptr(xm
);
380 if (iocbp
->ioc_cmd
== ADSPGETSOCK
) {
381 /* Obtain Network and Node Id's from DDP */
382 /* *** was ddp_get_cfg() *** */
383 addr
->net
= ifID_home
->ifThisNode
.s_net
;
384 addr
->node
= ifID_home
->ifThisNode
.s_node
;
385 addr
->socket
= (sp
)? sp
->localSocket
: 0;
388 *addr
= sp
->remoteAddress
.a
;
395 adsp_iocack(gref
, mp
);
398 case DDP_IOC_GET_CFG
:
399 /* respond to an DDP_IOC_GET_CFG sent on an adsp fd */
400 if (((xm
= gbuf_cont(mp
)) == NULL
) &&
401 (xm
= gbuf_alloc(sizeof(ddp_addr_t
), PRI_MED
)) == NULL
) {
402 iocbp
->ioc_rval
= -1;
403 adsp_iocnak(gref
, mp
, ENOBUFS
);
407 gbuf_wset(xm
, sizeof(ddp_addr_t
));
408 /* Obtain Network and Node Id's from DDP */
410 /* *** was ddp_get_cfg() *** */
412 (ddp_addr_t
*)gbuf_rptr(gbuf_cont(mp
));
413 cfgp
->inet
.net
= ifID_home
->ifThisNode
.s_net
;
414 cfgp
->inet
.node
= ifID_home
->ifThisNode
.s_node
;
415 cfgp
->inet
.socket
= (sp
)? sp
->localSocket
: 0;
416 cfgp
->ddptype
= DDP_ADSP
;
419 adsp_iocack(gref
, mp
);
427 rc
= adspWriteHandler(gref
, mp
);
431 if (gbuf_type(mp
) == MSG_IOCTL
) {
432 iocbp
= (ioc_t
*)gbuf_rptr(mp
);
433 iocbp
->ioc_private
= (void *)gref
;
449 void adspioc_ack(errno
, m
, gref
)
458 iocbp
= (ioc_t
*) gbuf_rptr(m
);
460 iocbp
->ioc_error
= errno
; /* set the errno */
461 iocbp
->ioc_count
= gbuf_msgsize(gbuf_cont(m
));
462 if (gbuf_type(m
) == MSG_IOCTL
) /* if an ioctl, this is an ack */
463 gbuf_set_type(m
, MSG_IOCACK
); /* and ALWAYS update the user */
464 /* ioctl structure */
465 trace_mbufs(D_M_ADSP
,"A ", m
);
469 static void adsp_iocack(gref
, m
)
473 if (gbuf_type(m
) == MSG_IOCTL
)
474 gbuf_set_type(m
, MSG_IOCACK
);
477 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= gbuf_msgsize(gbuf_cont(m
));
479 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= 0;
485 static void adsp_iocnak(gref
, m
, err
)
490 if (gbuf_type(m
) == MSG_IOCTL
)
491 gbuf_set_type(m
, MSG_IOCNAK
);
492 ((ioc_t
*)gbuf_rptr(m
))->ioc_count
= 0;
496 ((ioc_t
*)gbuf_rptr(m
))->ioc_error
= err
;
499 gbuf_freem(gbuf_cont(m
));
506 adspAssignSocket(gref
, flag
)
510 unsigned char sVal
, sMax
, sMin
, sSav
, inputC
;
513 sMax
= flag
? DDP_SOCKET_LAST
-46 : DDP_SOCKET_LAST
-6;
514 sMin
= DDP_SOCKET_1st_DYNAMIC
;
516 for (inputC
=255, sVal
=sMax
; sVal
>= sMin
; sVal
--) {
517 if (!ddp_socket_inuse(sVal
, DDP_ADSP
))
520 if (adsp_inputC
[sVal
] &&
521 /* meaning that raw DDP doesn't have it */
522 (adsp_inputC
[sVal
] < inputC
)
523 && (adsp_inputQ
[sVal
]->state
== sOpen
)) {
524 inputC
= adsp_inputC
[sVal
];
530 if (!flag
|| (inputC
== 255))
534 sp
= (CCBPtr
)gbuf_rptr(((gbuf_t
*)gref
->info
));
535 adsp_dequeue_ccb(sp
);
537 sp
->otccbLink
= adsp_inputQ
[sVal
];
538 adsp_inputQ
[sVal
] = sp
;
540 adsp_pidM
[sVal
] = sp
->pid
;
545 adspDeassignSocket(sp
)
553 dPrintf(D_M_ADSP
, D_L_TRACE
, ("adspDeassignSocket: pid=%d,s=%d\n",
554 sp
->pid
, sp
->localSocket
));
555 sVal
= sp
->localSocket
;
556 if ((curr_sp
= adsp_inputQ
[sVal
]) != 0) {
558 while (curr_sp
!= sp
) {
560 curr_sp
= curr_sp
->otccbLink
;
564 prev_sp
->otccbLink
= sp
->otccbLink
;
566 adsp_inputQ
[sVal
] = sp
->otccbLink
;
567 if (adsp_inputQ
[sVal
])
570 pid
= adsp_pidM
[sVal
];
571 adsp_inputC
[sVal
] = 0;
581 dPrintf(D_M_ADSP
, D_L_ERROR
,
582 ("adspDeassignSocket: closing, no CCB block, trouble ahead\n"));
584 } /* adspDeassignSocket */
587 * remove CCB from the use list
594 if (sp
== ccb_used_list
) {
595 if ((ccb_used_list
= sp
->otccbLink
) != 0)
596 sp
->otccbLink
->ccbLink
= 0;
597 } else if (sp
->ccbLink
) {
598 if ((sp
->ccbLink
->otccbLink
= sp
->otccbLink
) != 0)
599 sp
->otccbLink
->ccbLink
= sp
->ccbLink
;
606 void SndMsgUp(gref
, mp
)
607 gref_t
*gref
; /* WRITE queue */
611 dPrintf(D_M_ADSP, D_L_TRACE,
612 ("SndMsgUp: gref=0x%x, mbuf=0x%x\n", (unsigned)gref, (unsigned)mp));
613 trace_mbufs(D_M_ADSP, " m", mp);
615 atalk_putnext(gref
, mp
);