2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1996 Apple Computer, Inc.
28 * Created April 8, 1996 by Tuyen Nguyen
29 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
33 #include <sys/errno.h>
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <machine/spl.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
40 #include <sys/filedesc.h>
41 #include <sys/fcntl.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
47 #include <netat/sysglue.h>
48 #include <netat/appletalk.h>
49 #include <netat/at_var.h>
50 #include <netat/routing_tables.h>
51 #include <netat/at_pcb.h>
52 #include <netat/aurp.h>
53 #include <netat/debug.h>
56 /* funnel version of AURPsndOpenReq */
57 void AURPsndOpenReq_funnel(state
)
60 thread_funnel_set(network_flock
, TRUE
);
61 AURPsndOpenReq(state
);
62 thread_funnel_set(network_flock
, FALSE
);
66 void AURPsndOpenReq(state
)
76 if (state
->rcv_retry
&& (state
->rcv_state
!= AURPSTATE_WaitingForOpenRsp
)) {
80 /* stop trying if the retry count exceeds the maximum value */
81 if (++state
->rcv_retry
> AURP_MaxRetry
) {
82 dPrintf(D_M_AURP
, D_L_WARNING
,
83 ("AURPsndOpenReq: no response, node %u\n",
85 state
->rcv_state
= AURPSTATE_Unconnected
;
91 msize
= sizeof(aurp_hdr_t
) + 3;
92 if ((m
= (gbuf_t
*)gbuf_alloc(msize
, PRI_MED
)) != 0) {
95 /* construct the open request packet */
96 hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
97 if (state
->rcv_retry
> 1)
98 hdrp
->connection_id
= state
->rcv_connection_id
;
100 if (++rcv_connection_id
== 0)
101 rcv_connection_id
= 1;
102 hdrp
->connection_id
= rcv_connection_id
;
104 hdrp
->sequence_number
= 0;
105 hdrp
->command_code
= AURPCMD_OpenReq
;
106 hdrp
->flags
= (AURPFLG_NA
| AURPFLG_ND
| AURPFLG_NDC
| AURPFLG_ZC
);
107 *(short *)(hdrp
+1) = AURP_Version
;
108 ((char *)(hdrp
+1))[2] = 0; /* option count */
110 /* update state info */
111 state
->rcv_connection_id
= hdrp
->connection_id
;
112 state
->rcv_state
= AURPSTATE_WaitingForOpenRsp
;
114 /* send the packet */
115 dPrintf(D_M_AURP
, D_L_TRACE
,
116 ("AURPsndOpenReq: sending AURPCMD_OpenReq, node %u\n",
118 AURPsend(m
, AUD_AURP
, state
->rem_node
);
121 /* start the retry timer */
122 timeout(AURPsndOpenReq_funnel
, state
, AURP_RetryInterval
*HZ
);
127 void AURPrcvOpenReq(state
, m
)
132 aurp_hdr_t
*hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
133 unsigned short sui
= hdrp
->flags
;
135 /* make sure we're in a valid state to accept it */
136 if ((update_tmo
== 0) || ((state
->snd_state
!= AURPSTATE_Unconnected
) &&
137 (state
->snd_state
!= AURPSTATE_Connected
))) {
138 dPrintf(D_M_AURP
, D_L_WARNING
,
139 ("AURPrcvOpenReq: unexpected request, update_tmo=0x%x, snd_state=%u\n", (unsigned int) update_tmo
, state
->snd_state
));
144 /* check for the correct version number */
145 version
= *(short *)(hdrp
+1);
146 if (version
!= AURP_Version
) {
147 dPrintf(D_M_AURP
, D_L_WARNING
,
148 ("AURPrcvOpenReq: invalid version number %d, expected %d\n", version
, AURP_Version
));
149 rc
= AURPERR_InvalidVersionNumber
;
151 rc
= (short)AURP_UpdateRate
;
153 /* construct the open response packet */
154 gbuf_wset(m
,sizeof(aurp_hdr_t
)+sizeof(short));
155 hdrp
->command_code
= AURPCMD_OpenRsp
;
157 *(short *)(hdrp
+1) = rc
;
158 ((char *)(hdrp
+1))[2] = 0; /* option count */
161 * reset if we're in the Connected state and this is
162 * a completely new open request
164 if ((state
->snd_state
== AURPSTATE_Connected
) &&
165 ((state
->snd_connection_id
!= hdrp
->connection_id
) ||
166 (state
->snd_sequence_number
!= AURP_FirstSeqNum
))) {
167 extern void AURPsndTickle();
168 if (state
->rcv_state
== AURPSTATE_Connected
) {
169 state
->rcv_state
= AURPSTATE_Unconnected
;
170 untimeout(AURPsndTickle
, state
);
172 state
->snd_state
= AURPSTATE_Unconnected
;
174 AURPpurgeri(state
->rem_node
);
177 /* update state info */
178 if (state
->snd_state
== AURPSTATE_Unconnected
) {
179 state
->snd_state
= AURPSTATE_Connected
;
180 state
->snd_sui
= sui
;
181 state
->snd_connection_id
= hdrp
->connection_id
;
182 state
->snd_sequence_number
= AURP_FirstSeqNum
;
185 /* send the packet */
186 AURPsend(m
, AUD_AURP
, state
->rem_node
);
188 /* open connection for the data receiver side if not yet connected */
189 if (state
->rcv_state
== AURPSTATE_Unconnected
) {
190 state
->rcv_retry
= 0;
191 state
->tickle_retry
= 0;
192 state
->rcv_sequence_number
= 0;
193 AURPsndOpenReq(state
);
198 void AURPrcvOpenRsp(state
, m
)
202 extern void AURPsndTickle();
204 aurp_hdr_t
*hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
206 /* make sure we're in a valid state to accept it */
207 if (state
->rcv_state
!= AURPSTATE_WaitingForOpenRsp
) {
208 dPrintf(D_M_AURP
, D_L_WARNING
,
209 ("AURPrcvOpenRsp: unexpected response\n"));
214 /* check for the correct connection id */
215 if (hdrp
->connection_id
!= state
->rcv_connection_id
) {
216 dPrintf(D_M_AURP
, D_L_WARNING
,
217 ("AURPrcvOpenRsp: invalid connection id, r=%d, m=%d\n",
218 hdrp
->connection_id
, state
->rcv_connection_id
));
223 /* cancel the retry timer */
224 untimeout(AURPsndOpenReq_funnel
, state
);
226 state
->rcv_retry
= 0;
228 /* update state info */
229 state
->rcv_sequence_number
= AURP_FirstSeqNum
;
230 state
->rcv_env
= hdrp
->flags
;
232 /* check for error */
233 rc
= *(short *)(hdrp
+1);
236 dPrintf(D_M_AURP
, D_L_WARNING
,
237 ("AURPrcvOpenRsp: error=%d\n", rc
));
241 /* update state info */
242 state
->rcv_update_rate
= (unsigned short)rc
;
243 state
->rcv_state
= AURPSTATE_Connected
;
244 dPrintf(D_M_AURP
, D_L_TRACE
, ("AURPrcvOpenRsp: moved rcv_state to AURPSTATE_Connected\n"));
247 timeout(AURPsndTickle
, state
, AURP_TickleRetryInterval
*HZ
);
249 /* get routing info */