]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
d7e50217 | 6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. |
1c79356b | 7 | * |
d7e50217 A |
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 | |
13 | * file. | |
14 | * | |
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 | |
1c79356b A |
17 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
18 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
d7e50217 A |
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. | |
1c79356b A |
22 | * |
23 | * @APPLE_LICENSE_HEADER_END@ | |
24 | */ | |
25 | /* | |
26 | * Copyright (c) 1996 Apple Computer, Inc. | |
27 | * | |
28 | * Created April 8, 1996 by Tuyen Nguyen | |
29 | * Modified, March 17, 1997 by Tuyen Nguyen for MacOSX. | |
30 | * | |
31 | * File: open.c | |
32 | */ | |
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> | |
39 | #include <sys/proc.h> | |
40 | #include <sys/filedesc.h> | |
41 | #include <sys/fcntl.h> | |
42 | #include <sys/mbuf.h> | |
43 | #include <sys/socket.h> | |
44 | #include <sys/socketvar.h> | |
45 | #include <net/if.h> | |
46 | ||
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> | |
54 | ||
55 | ||
56 | /* funnel version of AURPsndOpenReq */ | |
57 | void AURPsndOpenReq_funnel(state) | |
58 | aurp_state_t *state; | |
59 | { | |
60 | thread_funnel_set(network_flock, TRUE); | |
61 | AURPsndOpenReq(state); | |
62 | thread_funnel_set(network_flock, FALSE); | |
63 | } | |
64 | ||
65 | /* */ | |
66 | void AURPsndOpenReq(state) | |
67 | aurp_state_t *state; | |
68 | { | |
69 | int msize; | |
70 | gbuf_t *m; | |
71 | aurp_hdr_t *hdrp; | |
72 | ||
73 | if (aurp_gref == 0) { | |
74 | return; | |
75 | } | |
76 | if (state->rcv_retry && (state->rcv_state != AURPSTATE_WaitingForOpenRsp)) { | |
77 | return; | |
78 | } | |
79 | ||
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", | |
84 | state->rem_node)); | |
85 | state->rcv_state = AURPSTATE_Unconnected; | |
86 | state->rcv_tmo = 0; | |
87 | state->rcv_retry = 0; | |
88 | return; | |
89 | } | |
90 | ||
91 | msize = sizeof(aurp_hdr_t) + 3; | |
92 | if ((m = (gbuf_t *)gbuf_alloc(msize, PRI_MED)) != 0) { | |
93 | gbuf_wset(m,msize); | |
94 | ||
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; | |
99 | else { | |
100 | if (++rcv_connection_id == 0) | |
101 | rcv_connection_id = 1; | |
102 | hdrp->connection_id = rcv_connection_id; | |
103 | } | |
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 */ | |
109 | ||
110 | /* update state info */ | |
111 | state->rcv_connection_id = hdrp->connection_id; | |
112 | state->rcv_state = AURPSTATE_WaitingForOpenRsp; | |
113 | ||
114 | /* send the packet */ | |
115 | dPrintf(D_M_AURP, D_L_TRACE, | |
116 | ("AURPsndOpenReq: sending AURPCMD_OpenReq, node %u\n", | |
117 | state->rem_node)); | |
118 | AURPsend(m, AUD_AURP, state->rem_node); | |
119 | } | |
120 | ||
121 | /* start the retry timer */ | |
122 | timeout(AURPsndOpenReq_funnel, state, AURP_RetryInterval*HZ); | |
123 | state->rcv_tmo = 1; | |
124 | } | |
125 | ||
126 | /* */ | |
127 | void AURPrcvOpenReq(state, m) | |
128 | aurp_state_t *state; | |
129 | gbuf_t *m; | |
130 | { | |
131 | short rc, version; | |
132 | aurp_hdr_t *hdrp = (aurp_hdr_t *)gbuf_rptr(m); | |
133 | unsigned short sui = hdrp->flags; | |
134 | ||
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)); | |
140 | gbuf_freem(m); | |
141 | return; | |
142 | } | |
143 | ||
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; | |
150 | } else | |
151 | rc = (short)AURP_UpdateRate; | |
152 | ||
153 | /* construct the open response packet */ | |
154 | gbuf_wset(m,sizeof(aurp_hdr_t)+sizeof(short)); | |
155 | hdrp->command_code = AURPCMD_OpenRsp; | |
156 | hdrp->flags = 0; | |
157 | *(short *)(hdrp+1) = rc; | |
158 | ((char *)(hdrp+1))[2] = 0; /* option count */ | |
159 | ||
160 | /* | |
161 | * reset if we're in the Connected state and this is | |
162 | * a completely new open request | |
163 | */ | |
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); | |
171 | } | |
172 | state->snd_state = AURPSTATE_Unconnected; | |
173 | AURPcleanup(state); | |
174 | AURPpurgeri(state->rem_node); | |
175 | } | |
176 | ||
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; | |
183 | } | |
184 | ||
185 | /* send the packet */ | |
186 | AURPsend(m, AUD_AURP, state->rem_node); | |
187 | ||
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); | |
194 | } | |
195 | } | |
196 | ||
197 | /* */ | |
198 | void AURPrcvOpenRsp(state, m) | |
199 | aurp_state_t *state; | |
200 | gbuf_t *m; | |
201 | { | |
202 | extern void AURPsndTickle(); | |
203 | short rc; | |
204 | aurp_hdr_t *hdrp = (aurp_hdr_t *)gbuf_rptr(m); | |
205 | ||
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")); | |
210 | gbuf_freem(m); | |
211 | return; | |
212 | } | |
213 | ||
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)); | |
219 | gbuf_freem(m); | |
220 | return; | |
221 | } | |
222 | ||
223 | /* cancel the retry timer */ | |
224 | untimeout(AURPsndOpenReq_funnel, state); | |
225 | state->rcv_tmo = 0; | |
226 | state->rcv_retry = 0; | |
227 | ||
228 | /* update state info */ | |
229 | state->rcv_sequence_number = AURP_FirstSeqNum; | |
230 | state->rcv_env = hdrp->flags; | |
231 | ||
232 | /* check for error */ | |
233 | rc = *(short *)(hdrp+1); | |
234 | gbuf_freem(m); | |
235 | if (rc < 0) { | |
236 | dPrintf(D_M_AURP, D_L_WARNING, | |
237 | ("AURPrcvOpenRsp: error=%d\n", rc)); | |
238 | return; | |
239 | } | |
240 | ||
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")); | |
245 | ||
246 | /* start tickle */ | |
247 | timeout(AURPsndTickle, state, AURP_TickleRetryInterval*HZ); | |
248 | ||
249 | /* get routing info */ | |
250 | AURPsndRIReq(state); | |
251 | } |