]> git.saurik.com Git - apple/xnu.git/blob - bsd/netat/adsp_RxAttn.c
2c398c82d86c74d00bb0fe54acb032028100141a
[apple/xnu.git] / bsd / netat / adsp_RxAttn.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * RxAttn.c
30 *
31 * From v01.12 06/12/90 mbs
32 * Modified, April 9, 1997 by Tuyen Nguyen for MacOSX.
33 */
34
35 #include <sys/errno.h>
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <machine/spl.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/proc.h>
42 #include <sys/filedesc.h>
43 #include <sys/fcntl.h>
44 #include <sys/mbuf.h>
45 #include <sys/socket.h>
46 #include <sys/time.h>
47
48 #include <netat/sysglue.h>
49 #include <netat/appletalk.h>
50 #include <netat/at_pcb.h>
51 #include <netat/debug.h>
52 #include <netat/adsp.h>
53 #include <netat/adsp_internal.h>
54
55 /*
56 * Used to search down queue of sessions for a session that matches
57 * sender and source connection ID
58 */
59 typedef struct
60 {
61 AddrUnion addr;
62 word srcCID;
63 } MATCH_SENDER, *MATCH_SENDERPtr;
64
65 /*
66 * MatchSender
67 *
68 */
69
70 static boolean MatchSender(sp, m) /* (CCBPtr sp, MATCH_SENDERPtr m) */
71 CCBPtr sp;
72 MATCH_SENDERPtr m;
73 {
74
75 if (sp->state != sOpen && sp->state != sClosing)
76 return 0;
77
78 if (sp->remCID != m->srcCID)
79 return 0;
80
81 if (sp->remoteAddress.a.node != m->addr.a.node)
82 return 0;
83 if (sp->remoteAddress.a.socket != m->addr.a.socket)
84 return 0;
85 if (sp->remoteAddress.a.net && m->addr.a.net &&
86 (sp->remoteAddress.a.net != m->addr.a.net))
87 return 0;
88
89 return 1;
90 }
91
92
93 /*
94 * FindSender
95 *
96 * Given an ADSP Packet, find the stream it is associated with.
97 *
98 * This should only be used for ADSP Packets that could be received
99 * by an OPEN connection.
100 *
101 * INPUTS:
102 * Pointer to ADSP header & address of sender
103 * OUTPUTS:
104 * Pointer to stream if found, else 0
105 */
106 CCBPtr FindSender(f, a) /* (ADSP_FRAMEPtr f, AddrUnion a) */
107 ADSP_FRAMEPtr f;
108 AddrUnion a;
109 {
110 MATCH_SENDER m;
111
112 m.addr = a;
113 m.srcCID = UAS_VALUE(f->CID);
114 return (CCBPtr)qfind_m(AT_ADSP_STREAMS, &m, (ProcPtr)MatchSender);
115 }
116
117 /*
118 * RXAttention
119 *
120 * We just got an Attention Packet.
121 * See if it came from anybody we know.
122 * Then check to see if it is an attention data packet or acknowledgement
123 *
124 * Interrupts are masked OFF at this point.
125 *
126 * INPUTS:
127 * stream pointer
128 * Pointer to ADSP header,
129 * Length of header plus data
130 * OUTPUTS:
131 * Returns 1 if packet was ignored
132 */
133 int RXAttention(sp, mp, f, len) /* (CCBPtr sp, ADSP_FRAMEPtr f, word len) */
134 CCBPtr sp;
135 gbuf_t *mp;
136 ADSP_FRAMEPtr f;
137 int len;
138 {
139 int offset;
140 struct adspcmd *pb;
141 long diff;
142
143 if (UAS_VALUE(f->pktRecvWdw)) /* This field must be 0 in attn pkts */
144 return 1;
145
146 if ((f->descriptor ==
147 (char)(ADSP_ATTENTION_BIT | ADSP_ACK_REQ_BIT)) && /* Attention Data */
148 ((sp->userFlags & eAttention) == 0)) /* & he read the previous */
149 {
150 diff = netdw(UAL_VALUE(f->pktFirstByteSeq)) - sp->attnRecvSeq;
151 if (diff > 0) /* Hey, he missed one */
152 return 1;
153
154 if (diff == 0) /* This is the one we expected */
155 {
156 len -= ADSP_FRAME_LEN; /* remove adsp header */
157 if (len < 2) /* Poorly formed attn packet */
158 return 1;
159 sp->attnCode = (f->data[0] << 8) + f->data[1]; /* Save attn code */
160 sp->attn_mb = mp;
161 offset = ((unsigned char *)&f->data[2]) - (unsigned char *)gbuf_rptr(mp);
162 gbuf_rinc(mp,offset);
163 sp->attnPtr = (unsigned char *)gbuf_rptr(mp);
164 mp = 0; /* mp has been queued don't free it */
165
166 /* Interrupts are off here, or otherwise we have to do
167 * these three operations automically.
168 */
169 sp->attnSize = len - 2; /* Tell user how many bytes */
170 ++sp->attnRecvSeq;
171 /* Set flag saying we got attn message */
172 sp->userFlags |= eAttention;
173 UrgentUser(sp); /* Notify user */
174 /* BEFORE sending acknowledge */
175 } /* in sequence */
176
177 sp->sendAttnAck = 1; /* send attention ack for dupl. &
178 * expected data */
179 sp->callSend = 1;
180 } /* Attn Data */
181
182 /*
183 * Interrupts are OFF here, otherwise we have to do this atomically
184 */
185 /* Check to see if this acknowledges anything */
186 if ((sp->attnSendSeq + 1) == netdw(UAL_VALUE(f->pktNextRecvSeq))) {
187 sp->attnSendSeq++;
188 if ((pb = sp->sapb) == 0) { /* We never sent data ? !!! */
189 if (mp)
190 gbuf_freem(mp);
191 return 0;
192 }
193
194 sp->sapb = (struct adspcmd *)pb->qLink; /* Unlink from queue */
195
196 /* Remove timer */
197 RemoveTimerElem(&adspGlobal.fastTimers, &sp->AttnTimer);
198
199 pb->ioResult = 0;
200 if (gbuf_cont(pb->mp)) {
201 gbuf_freem(gbuf_cont(pb->mp)); /* free the data */
202 gbuf_cont(pb->mp) = 0;
203 }
204 completepb(sp, pb); /* Done with the send attention */
205
206 if (sp->sapb) { /* Another send attention pending? */
207 sp->sendAttnData = 1;
208 sp->callSend = 1;
209 } else {
210 if (sp->state == sClosing) /* this ack may allow us to close... */
211 CheckOkToClose(sp);
212 }
213 }
214 if (mp)
215 gbuf_freem(mp);
216 return 0;
217 }