]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
5d5c5d0d A |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. |
3 | * | |
8f6c56a5 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
8f6c56a5 A |
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 | |
8ad349bb | 24 | * limitations under the License. |
8f6c56a5 A |
25 | * |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ | |
1c79356b A |
27 | */ |
28 | /* | |
29 | * Copyright (c) 1996 Apple Computer, Inc. | |
30 | * | |
31 | * Created April 8, 1996 by Tuyen Nguyen | |
32 | * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX. | |
33 | * | |
34 | * File: open.c | |
35 | */ | |
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> | |
42 | #include <sys/proc.h> | |
43 | #include <sys/filedesc.h> | |
44 | #include <sys/fcntl.h> | |
45 | #include <sys/mbuf.h> | |
46 | #include <sys/socket.h> | |
47 | #include <sys/socketvar.h> | |
48 | #include <net/if.h> | |
49 | ||
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> | |
57 | ||
58 | ||
91447636 A |
59 | /* locked version of AURPsndOpenReq */ |
60 | void AURPsndOpenReq_locked(state) | |
1c79356b A |
61 | aurp_state_t *state; |
62 | { | |
91447636 | 63 | atalk_lock(); |
1c79356b | 64 | AURPsndOpenReq(state); |
91447636 | 65 | atalk_unlock(); |
1c79356b A |
66 | } |
67 | ||
68 | /* */ | |
69 | void AURPsndOpenReq(state) | |
70 | aurp_state_t *state; | |
71 | { | |
72 | int msize; | |
73 | gbuf_t *m; | |
74 | aurp_hdr_t *hdrp; | |
75 | ||
76 | if (aurp_gref == 0) { | |
77 | return; | |
78 | } | |
79 | if (state->rcv_retry && (state->rcv_state != AURPSTATE_WaitingForOpenRsp)) { | |
80 | return; | |
81 | } | |
82 | ||
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", | |
87 | state->rem_node)); | |
88 | state->rcv_state = AURPSTATE_Unconnected; | |
89 | state->rcv_tmo = 0; | |
90 | state->rcv_retry = 0; | |
91 | return; | |
92 | } | |
93 | ||
94 | msize = sizeof(aurp_hdr_t) + 3; | |
95 | if ((m = (gbuf_t *)gbuf_alloc(msize, PRI_MED)) != 0) { | |
96 | gbuf_wset(m,msize); | |
97 | ||
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; | |
102 | else { | |
103 | if (++rcv_connection_id == 0) | |
104 | rcv_connection_id = 1; | |
105 | hdrp->connection_id = rcv_connection_id; | |
106 | } | |
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 */ | |
112 | ||
113 | /* update state info */ | |
114 | state->rcv_connection_id = hdrp->connection_id; | |
115 | state->rcv_state = AURPSTATE_WaitingForOpenRsp; | |
116 | ||
117 | /* send the packet */ | |
118 | dPrintf(D_M_AURP, D_L_TRACE, | |
119 | ("AURPsndOpenReq: sending AURPCMD_OpenReq, node %u\n", | |
120 | state->rem_node)); | |
121 | AURPsend(m, AUD_AURP, state->rem_node); | |
122 | } | |
123 | ||
124 | /* start the retry timer */ | |
91447636 | 125 | timeout(AURPsndOpenReq_locked, state, AURP_RetryInterval*HZ); |
1c79356b A |
126 | state->rcv_tmo = 1; |
127 | } | |
128 | ||
129 | /* */ | |
130 | void AURPrcvOpenReq(state, m) | |
131 | aurp_state_t *state; | |
132 | gbuf_t *m; | |
133 | { | |
134 | short rc, version; | |
135 | aurp_hdr_t *hdrp = (aurp_hdr_t *)gbuf_rptr(m); | |
136 | unsigned short sui = hdrp->flags; | |
137 | ||
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)); | |
143 | gbuf_freem(m); | |
144 | return; | |
145 | } | |
146 | ||
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; | |
153 | } else | |
154 | rc = (short)AURP_UpdateRate; | |
155 | ||
156 | /* construct the open response packet */ | |
157 | gbuf_wset(m,sizeof(aurp_hdr_t)+sizeof(short)); | |
158 | hdrp->command_code = AURPCMD_OpenRsp; | |
159 | hdrp->flags = 0; | |
160 | *(short *)(hdrp+1) = rc; | |
161 | ((char *)(hdrp+1))[2] = 0; /* option count */ | |
162 | ||
163 | /* | |
164 | * reset if we're in the Connected state and this is | |
165 | * a completely new open request | |
166 | */ | |
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); | |
174 | } | |
175 | state->snd_state = AURPSTATE_Unconnected; | |
176 | AURPcleanup(state); | |
177 | AURPpurgeri(state->rem_node); | |
178 | } | |
179 | ||
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; | |
186 | } | |
187 | ||
188 | /* send the packet */ | |
189 | AURPsend(m, AUD_AURP, state->rem_node); | |
190 | ||
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); | |
197 | } | |
198 | } | |
199 | ||
200 | /* */ | |
201 | void AURPrcvOpenRsp(state, m) | |
202 | aurp_state_t *state; | |
203 | gbuf_t *m; | |
204 | { | |
205 | extern void AURPsndTickle(); | |
206 | short rc; | |
207 | aurp_hdr_t *hdrp = (aurp_hdr_t *)gbuf_rptr(m); | |
208 | ||
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")); | |
213 | gbuf_freem(m); | |
214 | return; | |
215 | } | |
216 | ||
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)); | |
222 | gbuf_freem(m); | |
223 | return; | |
224 | } | |
225 | ||
226 | /* cancel the retry timer */ | |
91447636 | 227 | untimeout(AURPsndOpenReq_locked, state); |
1c79356b A |
228 | state->rcv_tmo = 0; |
229 | state->rcv_retry = 0; | |
230 | ||
231 | /* update state info */ | |
232 | state->rcv_sequence_number = AURP_FirstSeqNum; | |
233 | state->rcv_env = hdrp->flags; | |
234 | ||
235 | /* check for error */ | |
236 | rc = *(short *)(hdrp+1); | |
237 | gbuf_freem(m); | |
238 | if (rc < 0) { | |
239 | dPrintf(D_M_AURP, D_L_WARNING, | |
240 | ("AURPrcvOpenRsp: error=%d\n", rc)); | |
241 | return; | |
242 | } | |
243 | ||
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")); | |
248 | ||
249 | /* start tickle */ | |
250 | timeout(AURPsndTickle, state, AURP_TickleRetryInterval*HZ); | |
251 | ||
252 | /* get routing info */ | |
253 | AURPsndRIReq(state); | |
254 | } |