2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_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 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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1996 Apple Computer, Inc.
31 * Created April 8, 1996 by Tuyen Nguyen
32 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
36 #include <sys/errno.h>
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <machine/spl.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/filedesc.h>
44 #include <sys/fcntl.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
50 #include <netat/sysglue.h>
51 #include <netat/appletalk.h>
52 #include <netat/at_var.h>
53 #include <netat/routing_tables.h>
54 #include <netat/at_pcb.h>
55 #include <netat/aurp.h>
56 #include <netat/debug.h>
59 /* locked version of AURPsndOpenReq */
60 void AURPsndOpenReq_locked(state
)
64 AURPsndOpenReq(state
);
69 void AURPsndOpenReq(state
)
79 if (state
->rcv_retry
&& (state
->rcv_state
!= AURPSTATE_WaitingForOpenRsp
)) {
83 /* stop trying if the retry count exceeds the maximum value */
84 if (++state
->rcv_retry
> AURP_MaxRetry
) {
85 dPrintf(D_M_AURP
, D_L_WARNING
,
86 ("AURPsndOpenReq: no response, node %u\n",
88 state
->rcv_state
= AURPSTATE_Unconnected
;
94 msize
= sizeof(aurp_hdr_t
) + 3;
95 if ((m
= (gbuf_t
*)gbuf_alloc(msize
, PRI_MED
)) != 0) {
98 /* construct the open request packet */
99 hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
100 if (state
->rcv_retry
> 1)
101 hdrp
->connection_id
= state
->rcv_connection_id
;
103 if (++rcv_connection_id
== 0)
104 rcv_connection_id
= 1;
105 hdrp
->connection_id
= rcv_connection_id
;
107 hdrp
->sequence_number
= 0;
108 hdrp
->command_code
= AURPCMD_OpenReq
;
109 hdrp
->flags
= (AURPFLG_NA
| AURPFLG_ND
| AURPFLG_NDC
| AURPFLG_ZC
);
110 *(short *)(hdrp
+1) = AURP_Version
;
111 ((char *)(hdrp
+1))[2] = 0; /* option count */
113 /* update state info */
114 state
->rcv_connection_id
= hdrp
->connection_id
;
115 state
->rcv_state
= AURPSTATE_WaitingForOpenRsp
;
117 /* send the packet */
118 dPrintf(D_M_AURP
, D_L_TRACE
,
119 ("AURPsndOpenReq: sending AURPCMD_OpenReq, node %u\n",
121 AURPsend(m
, AUD_AURP
, state
->rem_node
);
124 /* start the retry timer */
125 timeout(AURPsndOpenReq_locked
, state
, AURP_RetryInterval
*HZ
);
130 void AURPrcvOpenReq(state
, m
)
135 aurp_hdr_t
*hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
136 unsigned short sui
= hdrp
->flags
;
138 /* make sure we're in a valid state to accept it */
139 if ((update_tmo
== 0) || ((state
->snd_state
!= AURPSTATE_Unconnected
) &&
140 (state
->snd_state
!= AURPSTATE_Connected
))) {
141 dPrintf(D_M_AURP
, D_L_WARNING
,
142 ("AURPrcvOpenReq: unexpected request, update_tmo=0x%x, snd_state=%u\n", (unsigned int) update_tmo
, state
->snd_state
));
147 /* check for the correct version number */
148 version
= *(short *)(hdrp
+1);
149 if (version
!= AURP_Version
) {
150 dPrintf(D_M_AURP
, D_L_WARNING
,
151 ("AURPrcvOpenReq: invalid version number %d, expected %d\n", version
, AURP_Version
));
152 rc
= AURPERR_InvalidVersionNumber
;
154 rc
= (short)AURP_UpdateRate
;
156 /* construct the open response packet */
157 gbuf_wset(m
,sizeof(aurp_hdr_t
)+sizeof(short));
158 hdrp
->command_code
= AURPCMD_OpenRsp
;
160 *(short *)(hdrp
+1) = rc
;
161 ((char *)(hdrp
+1))[2] = 0; /* option count */
164 * reset if we're in the Connected state and this is
165 * a completely new open request
167 if ((state
->snd_state
== AURPSTATE_Connected
) &&
168 ((state
->snd_connection_id
!= hdrp
->connection_id
) ||
169 (state
->snd_sequence_number
!= AURP_FirstSeqNum
))) {
170 extern void AURPsndTickle();
171 if (state
->rcv_state
== AURPSTATE_Connected
) {
172 state
->rcv_state
= AURPSTATE_Unconnected
;
173 untimeout(AURPsndTickle
, state
);
175 state
->snd_state
= AURPSTATE_Unconnected
;
177 AURPpurgeri(state
->rem_node
);
180 /* update state info */
181 if (state
->snd_state
== AURPSTATE_Unconnected
) {
182 state
->snd_state
= AURPSTATE_Connected
;
183 state
->snd_sui
= sui
;
184 state
->snd_connection_id
= hdrp
->connection_id
;
185 state
->snd_sequence_number
= AURP_FirstSeqNum
;
188 /* send the packet */
189 AURPsend(m
, AUD_AURP
, state
->rem_node
);
191 /* open connection for the data receiver side if not yet connected */
192 if (state
->rcv_state
== AURPSTATE_Unconnected
) {
193 state
->rcv_retry
= 0;
194 state
->tickle_retry
= 0;
195 state
->rcv_sequence_number
= 0;
196 AURPsndOpenReq(state
);
201 void AURPrcvOpenRsp(state
, m
)
205 extern void AURPsndTickle();
207 aurp_hdr_t
*hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
209 /* make sure we're in a valid state to accept it */
210 if (state
->rcv_state
!= AURPSTATE_WaitingForOpenRsp
) {
211 dPrintf(D_M_AURP
, D_L_WARNING
,
212 ("AURPrcvOpenRsp: unexpected response\n"));
217 /* check for the correct connection id */
218 if (hdrp
->connection_id
!= state
->rcv_connection_id
) {
219 dPrintf(D_M_AURP
, D_L_WARNING
,
220 ("AURPrcvOpenRsp: invalid connection id, r=%d, m=%d\n",
221 hdrp
->connection_id
, state
->rcv_connection_id
));
226 /* cancel the retry timer */
227 untimeout(AURPsndOpenReq_locked
, state
);
229 state
->rcv_retry
= 0;
231 /* update state info */
232 state
->rcv_sequence_number
= AURP_FirstSeqNum
;
233 state
->rcv_env
= hdrp
->flags
;
235 /* check for error */
236 rc
= *(short *)(hdrp
+1);
239 dPrintf(D_M_AURP
, D_L_WARNING
,
240 ("AURPrcvOpenRsp: error=%d\n", rc
));
244 /* update state info */
245 state
->rcv_update_rate
= (unsigned short)rc
;
246 state
->rcv_state
= AURPSTATE_Connected
;
247 dPrintf(D_M_AURP
, D_L_TRACE
, ("AURPrcvOpenRsp: moved rcv_state to AURPSTATE_Connected\n"));
250 timeout(AURPsndTickle
, state
, AURP_TickleRetryInterval
*HZ
);
252 /* get routing info */