Debian Codegraph @

src/protosetup.c

raw
1 /******************************************************************************
2
3
4 Copyright 1993, 1998 The Open Group
5
6 Permission to use, copy, modify, distribute, and sell this software and its
7 documentation for any purpose is hereby granted without fee, provided that
8 the above copyright notice appear in all copies and that both that
9 copyright notice and this permission notice appear in supporting
10 documentation.
11
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 Except as contained in this notice, the name of The Open Group shall not be
23 used in advertising or otherwise to promote the sale, use or other dealings
24 in this Software without prior written authorization from The Open Group.
25
26 Author: Ralph Mor, X Consortium
27 ******************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32 #include <X11/ICE/ICElib.h>
33 #include "ICElibint.h"
34
35
36 IceProtocolSetupStatus
37 IceProtocolSetup (
38 IceConn iceConn,
39 int myOpcode,
40 IcePointer clientData,
41 Bool mustAuthenticate,
42 int *majorVersionRet,
43 int *minorVersionRet,
44 char **vendorRet,
45 char **releaseRet,
46 int errorLength,
47 char *errorStringRet
48 )
49 {
50 iceProtocolSetupMsg *pMsg;
51 char *pData;
52 _IceProtocol *myProtocol;
53 int extra;
54 Bool gotReply, ioErrorOccured;
55 int accepted, i;
56 int hisOpcode;
57 unsigned long setup_sequence;
58 IceReplyWaitInfo replyWait;
59 _IceReply reply;
60 IcePoVersionRec *versionRec = NULL;
61 int authCount;
62 int *authIndices;
63
64 if (errorStringRet && errorLength > 0)
65 *errorStringRet = '\0';
66
67 *majorVersionRet = 0;
68 *minorVersionRet = 0;
69 *vendorRet = NULL;
70 *releaseRet = NULL;
71
72 if (myOpcode < 1 || myOpcode > _IceLastMajorOpcode)
73 {
74 if (errorStringRet && errorLength > 0) {
75 strncpy (errorStringRet, "myOpcode out of range", errorLength);
76 errorStringRet[errorLength - 1] = '\0';
77 }
78 return (IceProtocolSetupFailure);
79 }
80
81 myProtocol = &_IceProtocols[myOpcode - 1];
82
83 if (myProtocol->orig_client == NULL)
84 {
85 if (errorStringRet && errorLength > 0) {
86 strncpy (errorStringRet,
87 "IceRegisterForProtocolSetup was not called", errorLength);
88 errorStringRet[errorLength - 1] = '\0';
89 }
90 return (IceProtocolSetupFailure);
91 }
92
93
94 /*
95 * Make sure this protocol hasn't been activated already.
96 */
97
98 if (iceConn->process_msg_info)
99 {
100 for (i = iceConn->his_min_opcode; i <= iceConn->his_max_opcode; i++)
101 {
102 if (iceConn->process_msg_info[
103 i - iceConn->his_min_opcode].in_use &&
104 iceConn->process_msg_info[
105 i - iceConn->his_min_opcode ].my_opcode == myOpcode)
106 break;
107 }
108
109 if (i <= iceConn->his_max_opcode)
110 {
111 return (IceProtocolAlreadyActive);
112 }
113 }
114
115 /*
116 * Generate the message.
117 */
118
119 if (myProtocol->orig_client->auth_count > 0)
120 {
121 authIndices = malloc (
122 myProtocol->orig_client->auth_count * sizeof (int));
123
124 _IceGetPoValidAuthIndices (myProtocol->protocol_name,
125 iceConn->connection_string,
126 myProtocol->orig_client->auth_count,
127 (const char **) myProtocol->orig_client->auth_names,
128 &authCount, authIndices);
129
130 }
131 else
132 {
133 authCount = 0;
134 authIndices = NULL;
135 }
136
137 extra = STRING_BYTES (myProtocol->protocol_name) +
138 STRING_BYTES (myProtocol->orig_client->vendor) +
139 STRING_BYTES (myProtocol->orig_client->release);
140
141 for (i = 0; i < authCount; i++)
142 {
143 extra += STRING_BYTES (myProtocol->orig_client->auth_names[
144 authIndices[i]]);
145 }
146
147 extra += (myProtocol->orig_client->version_count * 4);
148
149 IceGetHeaderExtra (iceConn, 0, ICE_ProtocolSetup,
150 SIZEOF (iceProtocolSetupMsg), WORD64COUNT (extra),
151 iceProtocolSetupMsg, pMsg, pData);
152
153 if (pData == NULL) {
154 iceConn->outbufptr -= SIZEOF (iceProtocolSetupMsg);
155 free(authIndices);
156 if (errorStringRet && errorLength > 0) {
157 strncpy (errorStringRet,
158 "Too much extra data for iceProtocolSetupMsg", errorLength);
159 errorStringRet[errorLength - 1] = '\0';
160 }
161 return (IceProtocolSetupFailure);
162 }
163
164 setup_sequence = iceConn->send_sequence;
165
166 pMsg->protocolOpcode = myOpcode;
167 pMsg->versionCount = myProtocol->orig_client->version_count;
168 pMsg->authCount = authCount;
169 pMsg->mustAuthenticate = mustAuthenticate;
170
171 STORE_STRING (pData, myProtocol->protocol_name);
172 STORE_STRING (pData, myProtocol->orig_client->vendor);
173 STORE_STRING (pData, myProtocol->orig_client->release);
174
175 for (i = 0; i < authCount; i++)
176 {
177 STORE_STRING (pData, myProtocol->orig_client->auth_names[
178 authIndices[i]]);
179 }
180
181 for (i = 0; i < myProtocol->orig_client->version_count; i++)
182 {
183 STORE_CARD16 (pData,
184 myProtocol->orig_client->version_recs[i].major_version);
185 STORE_CARD16 (pData,
186 myProtocol->orig_client->version_recs[i].minor_version);
187 }
188
189 IceFlush (iceConn);
190
191
192 /*
193 * Process messages until we get a Protocol Reply.
194 */
195
196 replyWait.sequence_of_request = setup_sequence;
197 replyWait.major_opcode_of_request = 0;
198 replyWait.minor_opcode_of_request = ICE_ProtocolSetup;
199 replyWait.reply = (IcePointer) &reply;
200
201 iceConn->protosetup_to_you = malloc (sizeof (_IceProtoSetupToYouInfo));
202 iceConn->protosetup_to_you->my_opcode = myOpcode;
203 iceConn->protosetup_to_you->my_auth_count = authCount;
204 iceConn->protosetup_to_you->auth_active = 0;
205 iceConn->protosetup_to_you->my_auth_indices = authIndices;
206
207 gotReply = False;
208 ioErrorOccured = False;
209 accepted = 0;
210
211 while (!gotReply && !ioErrorOccured)
212 {
213 ioErrorOccured = (IceProcessMessages (
214 iceConn, &replyWait, &gotReply) == IceProcessMessagesIOError);
215
216 if (ioErrorOccured)
217 {
218 if (errorStringRet && errorLength > 0) {
219 strncpy (errorStringRet,
220 "IO error occurred doing Protocol Setup on connection",
221 errorLength);
222 errorStringRet[errorLength - 1] = '\0';
223 }
224 return (IceProtocolSetupIOError);
225 }
226 else if (gotReply)
227 {
228 if (reply.type == ICE_PROTOCOL_REPLY)
229 {
230 if (reply.protocol_reply.version_index >=
231 myProtocol->orig_client->version_count)
232 {
233 if (errorStringRet && errorLength > 0) {
234 strncpy (errorStringRet,
235 "Got a bad version index in the Protocol Reply",
236 errorLength);
237 errorStringRet[errorLength - 1] = '\0';
238 }
239
240 free (reply.protocol_reply.vendor);
241 free (reply.protocol_reply.release);
242 }
243 else
244 {
245 versionRec = &(myProtocol->orig_client->version_recs[
246 reply.protocol_reply.version_index]);
247
248 accepted = 1;
249 }
250 }
251 else /* reply.type == ICE_PROTOCOL_ERROR */
252 {
253 /* Protocol Setup failed */
254
255 if (errorStringRet && errorLength > 0) {
256 strncpy (errorStringRet, reply.protocol_error.error_message,
257 errorLength);
258 errorStringRet[errorLength - 1] = '\0';
259 }
260
261 free (reply.protocol_error.error_message);
262 }
263
264 if (iceConn->protosetup_to_you->my_auth_indices)
265 free (iceConn->protosetup_to_you->my_auth_indices);
266 free (iceConn->protosetup_to_you);
267 iceConn->protosetup_to_you = NULL;
268 }
269 }
270
271 if (accepted)
272 {
273 _IceProcessMsgInfo *process_msg_info;
274
275 *majorVersionRet = versionRec->major_version;
276 *minorVersionRet = versionRec->minor_version;
277 *vendorRet = reply.protocol_reply.vendor;
278 *releaseRet = reply.protocol_reply.release;
279
280
281 /*
282 * Increase the reference count for the number of active protocols.
283 */
284
285 iceConn->proto_ref_count++;
286
287
288 /*
289 * We may be using a different major opcode for this protocol
290 * than the other client. Whenever we get a message, we must
291 * map to our own major opcode.
292 */
293
294 hisOpcode = reply.protocol_reply.major_opcode;
295
296 _IceAddOpcodeMapping (iceConn, hisOpcode, myOpcode);
297
298 process_msg_info = &iceConn->process_msg_info[hisOpcode -
299 iceConn->his_min_opcode];
300
301 process_msg_info->client_data = clientData;
302 process_msg_info->accept_flag = 0;
303
304 process_msg_info->process_msg_proc.orig_client =
305 versionRec->process_msg_proc;
306
307 return (IceProtocolSetupSuccess);
308 }
309 else
310 {
311 return (IceProtocolSetupFailure);
312 }
313 }