2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1996 Apple Computer, Inc.
33 * Created April 8, 1996 by Tuyen Nguyen
34 * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX.
41 #include <sys/errno.h>
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <machine/spl.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
48 #include <sys/filedesc.h>
49 #include <sys/fcntl.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
55 #include <netat/sysglue.h>
56 #include <netat/appletalk.h>
57 #include <netat/at_var.h>
58 #include <netat/routing_tables.h>
59 #include <netat/at_pcb.h>
60 #include <netat/aurp.h>
61 #include <netat/debug.h>
64 /* locked version of AURPsndOpenReq */
65 void AURPsndOpenReq_locked(state
)
69 AURPsndOpenReq(state
);
74 void AURPsndOpenReq(state
)
84 if (state
->rcv_retry
&& (state
->rcv_state
!= AURPSTATE_WaitingForOpenRsp
)) {
88 /* stop trying if the retry count exceeds the maximum value */
89 if (++state
->rcv_retry
> AURP_MaxRetry
) {
90 dPrintf(D_M_AURP
, D_L_WARNING
,
91 ("AURPsndOpenReq: no response, node %u\n",
93 state
->rcv_state
= AURPSTATE_Unconnected
;
99 msize
= sizeof(aurp_hdr_t
) + 3;
100 if ((m
= (gbuf_t
*)gbuf_alloc(msize
, PRI_MED
)) != 0) {
103 /* construct the open request packet */
104 hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
105 if (state
->rcv_retry
> 1)
106 hdrp
->connection_id
= state
->rcv_connection_id
;
108 if (++rcv_connection_id
== 0)
109 rcv_connection_id
= 1;
110 hdrp
->connection_id
= rcv_connection_id
;
112 hdrp
->sequence_number
= 0;
113 hdrp
->command_code
= AURPCMD_OpenReq
;
114 hdrp
->flags
= (AURPFLG_NA
| AURPFLG_ND
| AURPFLG_NDC
| AURPFLG_ZC
);
115 *(short *)(hdrp
+1) = AURP_Version
;
116 ((char *)(hdrp
+1))[2] = 0; /* option count */
118 /* update state info */
119 state
->rcv_connection_id
= hdrp
->connection_id
;
120 state
->rcv_state
= AURPSTATE_WaitingForOpenRsp
;
122 /* send the packet */
123 dPrintf(D_M_AURP
, D_L_TRACE
,
124 ("AURPsndOpenReq: sending AURPCMD_OpenReq, node %u\n",
126 AURPsend(m
, AUD_AURP
, state
->rem_node
);
129 /* start the retry timer */
130 timeout(AURPsndOpenReq_locked
, state
, AURP_RetryInterval
*HZ
);
135 void AURPrcvOpenReq(state
, m
)
140 aurp_hdr_t
*hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
141 unsigned short sui
= hdrp
->flags
;
143 /* make sure we're in a valid state to accept it */
144 if ((update_tmo
== 0) || ((state
->snd_state
!= AURPSTATE_Unconnected
) &&
145 (state
->snd_state
!= AURPSTATE_Connected
))) {
146 dPrintf(D_M_AURP
, D_L_WARNING
,
147 ("AURPrcvOpenReq: unexpected request, update_tmo=0x%x, snd_state=%u\n", (unsigned int) update_tmo
, state
->snd_state
));
152 /* check for the correct version number */
153 version
= *(short *)(hdrp
+1);
154 if (version
!= AURP_Version
) {
155 dPrintf(D_M_AURP
, D_L_WARNING
,
156 ("AURPrcvOpenReq: invalid version number %d, expected %d\n", version
, AURP_Version
));
157 rc
= AURPERR_InvalidVersionNumber
;
159 rc
= (short)AURP_UpdateRate
;
161 /* construct the open response packet */
162 gbuf_wset(m
,sizeof(aurp_hdr_t
)+sizeof(short));
163 hdrp
->command_code
= AURPCMD_OpenRsp
;
165 *(short *)(hdrp
+1) = rc
;
166 ((char *)(hdrp
+1))[2] = 0; /* option count */
169 * reset if we're in the Connected state and this is
170 * a completely new open request
172 if ((state
->snd_state
== AURPSTATE_Connected
) &&
173 ((state
->snd_connection_id
!= hdrp
->connection_id
) ||
174 (state
->snd_sequence_number
!= AURP_FirstSeqNum
))) {
175 extern void AURPsndTickle();
176 if (state
->rcv_state
== AURPSTATE_Connected
) {
177 state
->rcv_state
= AURPSTATE_Unconnected
;
178 untimeout(AURPsndTickle
, state
);
180 state
->snd_state
= AURPSTATE_Unconnected
;
182 AURPpurgeri(state
->rem_node
);
185 /* update state info */
186 if (state
->snd_state
== AURPSTATE_Unconnected
) {
187 state
->snd_state
= AURPSTATE_Connected
;
188 state
->snd_sui
= sui
;
189 state
->snd_connection_id
= hdrp
->connection_id
;
190 state
->snd_sequence_number
= AURP_FirstSeqNum
;
193 /* send the packet */
194 AURPsend(m
, AUD_AURP
, state
->rem_node
);
196 /* open connection for the data receiver side if not yet connected */
197 if (state
->rcv_state
== AURPSTATE_Unconnected
) {
198 state
->rcv_retry
= 0;
199 state
->tickle_retry
= 0;
200 state
->rcv_sequence_number
= 0;
201 AURPsndOpenReq(state
);
206 void AURPrcvOpenRsp(state
, m
)
210 extern void AURPsndTickle();
212 aurp_hdr_t
*hdrp
= (aurp_hdr_t
*)gbuf_rptr(m
);
214 /* make sure we're in a valid state to accept it */
215 if (state
->rcv_state
!= AURPSTATE_WaitingForOpenRsp
) {
216 dPrintf(D_M_AURP
, D_L_WARNING
,
217 ("AURPrcvOpenRsp: unexpected response\n"));
222 /* check for the correct connection id */
223 if (hdrp
->connection_id
!= state
->rcv_connection_id
) {
224 dPrintf(D_M_AURP
, D_L_WARNING
,
225 ("AURPrcvOpenRsp: invalid connection id, r=%d, m=%d\n",
226 hdrp
->connection_id
, state
->rcv_connection_id
));
231 /* cancel the retry timer */
232 untimeout(AURPsndOpenReq_locked
, state
);
234 state
->rcv_retry
= 0;
236 /* update state info */
237 state
->rcv_sequence_number
= AURP_FirstSeqNum
;
238 state
->rcv_env
= hdrp
->flags
;
240 /* check for error */
241 rc
= *(short *)(hdrp
+1);
244 dPrintf(D_M_AURP
, D_L_WARNING
,
245 ("AURPrcvOpenRsp: error=%d\n", rc
));
249 /* update state info */
250 state
->rcv_update_rate
= (unsigned short)rc
;
251 state
->rcv_state
= AURPSTATE_Connected
;
252 dPrintf(D_M_AURP
, D_L_TRACE
, ("AURPrcvOpenRsp: moved rcv_state to AURPSTATE_Connected\n"));
255 timeout(AURPsndTickle
, state
, AURP_TickleRetryInterval
*HZ
);
257 /* get routing info */
261 #endif /* AURP_SUPPORT */