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) 1996 Apple Computer, Inc.
25 * Created April 8, 1996 by Tuyen Nguyen
26 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
30 #include <sys/errno.h>
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <machine/spl.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
37 #include <sys/filedesc.h>
38 #include <sys/fcntl.h>
40 #include <sys/socket.h>
41 #include <sys/socketvar.h>
44 #include <netat/sysglue.h>
45 #include <netat/appletalk.h>
46 #include <netat/at_var.h>
47 #include <netat/routing_tables.h>
48 #include <netat/at_pcb.h>
49 #include <netat/aurp.h>
50 #include <netat/debug.h>
53 /* locked version of AURPsndOpenReq */
54 void AURPsndOpenReq_locked(state
)
58 AURPsndOpenReq(state
);
63 void AURPsndOpenReq(state
)
73 if (state
->rcv_retry
&& (state
->rcv_state
!= AURPSTATE_WaitingForOpenRsp
)) {
77 /* stop trying if the retry count exceeds the maximum value */
78 if (++state
->rcv_retry
> AURP_MaxRetry
) {
79 dPrintf(D_M_AURP
, D_L_WARNING
,
80 ("AURPsndOpenReq: no response, node %u\n",
82 state
->rcv_state
= AURPSTATE_Unconnected
;
88 msize
= sizeof(aurp_hdr_t
) + 3;
89 if ((m
= (gbuf_t
*)gbuf_alloc(msize
, PRI_MED
)) != 0) {
92 /* construct the open request packet */
93 hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
94 if (state
->rcv_retry
> 1)
95 hdrp
->connection_id
= state
->rcv_connection_id
;
97 if (++rcv_connection_id
== 0)
98 rcv_connection_id
= 1;
99 hdrp
->connection_id
= rcv_connection_id
;
101 hdrp
->sequence_number
= 0;
102 hdrp
->command_code
= AURPCMD_OpenReq
;
103 hdrp
->flags
= (AURPFLG_NA
| AURPFLG_ND
| AURPFLG_NDC
| AURPFLG_ZC
);
104 *(short *)(hdrp
+1) = AURP_Version
;
105 ((char *)(hdrp
+1))[2] = 0; /* option count */
107 /* update state info */
108 state
->rcv_connection_id
= hdrp
->connection_id
;
109 state
->rcv_state
= AURPSTATE_WaitingForOpenRsp
;
111 /* send the packet */
112 dPrintf(D_M_AURP
, D_L_TRACE
,
113 ("AURPsndOpenReq: sending AURPCMD_OpenReq, node %u\n",
115 AURPsend(m
, AUD_AURP
, state
->rem_node
);
118 /* start the retry timer */
119 timeout(AURPsndOpenReq_locked
, state
, AURP_RetryInterval
*HZ
);
124 void AURPrcvOpenReq(state
, m
)
129 aurp_hdr_t
*hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
130 unsigned short sui
= hdrp
->flags
;
132 /* make sure we're in a valid state to accept it */
133 if ((update_tmo
== 0) || ((state
->snd_state
!= AURPSTATE_Unconnected
) &&
134 (state
->snd_state
!= AURPSTATE_Connected
))) {
135 dPrintf(D_M_AURP
, D_L_WARNING
,
136 ("AURPrcvOpenReq: unexpected request, update_tmo=0x%x, snd_state=%u\n", (unsigned int) update_tmo
, state
->snd_state
));
141 /* check for the correct version number */
142 version
= *(short *)(hdrp
+1);
143 if (version
!= AURP_Version
) {
144 dPrintf(D_M_AURP
, D_L_WARNING
,
145 ("AURPrcvOpenReq: invalid version number %d, expected %d\n", version
, AURP_Version
));
146 rc
= AURPERR_InvalidVersionNumber
;
148 rc
= (short)AURP_UpdateRate
;
150 /* construct the open response packet */
151 gbuf_wset(m
,sizeof(aurp_hdr_t
)+sizeof(short));
152 hdrp
->command_code
= AURPCMD_OpenRsp
;
154 *(short *)(hdrp
+1) = rc
;
155 ((char *)(hdrp
+1))[2] = 0; /* option count */
158 * reset if we're in the Connected state and this is
159 * a completely new open request
161 if ((state
->snd_state
== AURPSTATE_Connected
) &&
162 ((state
->snd_connection_id
!= hdrp
->connection_id
) ||
163 (state
->snd_sequence_number
!= AURP_FirstSeqNum
))) {
164 extern void AURPsndTickle();
165 if (state
->rcv_state
== AURPSTATE_Connected
) {
166 state
->rcv_state
= AURPSTATE_Unconnected
;
167 untimeout(AURPsndTickle
, state
);
169 state
->snd_state
= AURPSTATE_Unconnected
;
171 AURPpurgeri(state
->rem_node
);
174 /* update state info */
175 if (state
->snd_state
== AURPSTATE_Unconnected
) {
176 state
->snd_state
= AURPSTATE_Connected
;
177 state
->snd_sui
= sui
;
178 state
->snd_connection_id
= hdrp
->connection_id
;
179 state
->snd_sequence_number
= AURP_FirstSeqNum
;
182 /* send the packet */
183 AURPsend(m
, AUD_AURP
, state
->rem_node
);
185 /* open connection for the data receiver side if not yet connected */
186 if (state
->rcv_state
== AURPSTATE_Unconnected
) {
187 state
->rcv_retry
= 0;
188 state
->tickle_retry
= 0;
189 state
->rcv_sequence_number
= 0;
190 AURPsndOpenReq(state
);
195 void AURPrcvOpenRsp(state
, m
)
199 extern void AURPsndTickle();
201 aurp_hdr_t
*hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
203 /* make sure we're in a valid state to accept it */
204 if (state
->rcv_state
!= AURPSTATE_WaitingForOpenRsp
) {
205 dPrintf(D_M_AURP
, D_L_WARNING
,
206 ("AURPrcvOpenRsp: unexpected response\n"));
211 /* check for the correct connection id */
212 if (hdrp
->connection_id
!= state
->rcv_connection_id
) {
213 dPrintf(D_M_AURP
, D_L_WARNING
,
214 ("AURPrcvOpenRsp: invalid connection id, r=%d, m=%d\n",
215 hdrp
->connection_id
, state
->rcv_connection_id
));
220 /* cancel the retry timer */
221 untimeout(AURPsndOpenReq_locked
, state
);
223 state
->rcv_retry
= 0;
225 /* update state info */
226 state
->rcv_sequence_number
= AURP_FirstSeqNum
;
227 state
->rcv_env
= hdrp
->flags
;
229 /* check for error */
230 rc
= *(short *)(hdrp
+1);
233 dPrintf(D_M_AURP
, D_L_WARNING
,
234 ("AURPrcvOpenRsp: error=%d\n", rc
));
238 /* update state info */
239 state
->rcv_update_rate
= (unsigned short)rc
;
240 state
->rcv_state
= AURPSTATE_Connected
;
241 dPrintf(D_M_AURP
, D_L_TRACE
, ("AURPrcvOpenRsp: moved rcv_state to AURPSTATE_Connected\n"));
244 timeout(AURPsndTickle
, state
, AURP_TickleRetryInterval
*HZ
);
246 /* get routing info */