Debian Codegraph @

src/process.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 #include <stdio.h> /* sprintf */
36
37 /*
38 * Check for bad length
39 */
40
41 #define CHECK_SIZE_MATCH(_iceConn, _opcode, _expected_len, _actual_len, _severity, _return) \
42 if ((((_actual_len) - SIZEOF (iceMsg)) >> 3) != _expected_len) \
43 { \
44 _IceErrorBadLength (_iceConn, 0, _opcode, _severity); \
45 return (_return); \
46 }
47
48 #define CHECK_AT_LEAST_SIZE(_iceConn, _opcode, _expected_len, _actual_len, _severity) \
49 if ((((_actual_len) - SIZEOF (iceMsg)) >> 3) > _expected_len) \
50 { \
51 _IceErrorBadLength (_iceConn, 0, _opcode, _severity); \
52 return (0); \
53 }
54
55 #define CHECK_COMPLETE_SIZE(_iceConn, _opcode, _expected_len, _actual_len, _pStart, _severity) \
56 if (((PADDED_BYTES64((_actual_len)) - SIZEOF (iceMsg)) >> 3) \
57 != _expected_len) \
58 { \
59 _IceErrorBadLength (_iceConn, 0, _opcode, _severity); \
60 IceDisposeCompleteMessage (iceConn, _pStart); \
61 return (0); \
62 }
63
64 #define BAIL_STRING(_iceConn, _opcode, _pStart) {\
65 _IceErrorBadLength (_iceConn, 0, _opcode, IceFatalToConnection);\
66 IceDisposeCompleteMessage (_iceConn, _pStart);\
67 return (0);\
68 }
69
70 #ifndef HAVE_ASPRINTF
71 # include <stdarg.h>
72
73 /* sprintf variant found in newer libc's which allocates string to print to */
74 static int _X_ATTRIBUTE_PRINTF(2,3)
75 asprintf(char ** ret, const char *format, ...)
76 {
77 char buf[256];
78 int len;
79 va_list ap;
80
81 va_start(ap, format);
82 len = vsnprintf(buf, sizeof(buf), format, ap);
83 va_end(ap);
84
85 if (len < 0)
86 return -1;
87
88 if (len < sizeof(buf))
89 {
90 *ret = strdup(buf);
91 }
92 else
93 {
94 *ret = malloc(len + 1); /* snprintf doesn't count trailing '\0' */
95 if (*ret != NULL)
96 {
97 va_start(ap, format);
98 len = vsnprintf(*ret, len + 1, format, ap);
99 va_end(ap);
100 if (len < 0) {
101 free(*ret);
102 *ret = NULL;
103 }
104 }
105 }
106
107 if (*ret == NULL)
108 return -1;
109
110 return len;
111 }
112 #endif
113
114
115 /*
116 * IceProcessMessages:
117 *
118 * If replyWait == NULL, the client is not waiting for a reply.
119 *
120 * If replyWait != NULL, the client is waiting for a reply...
121 *
122 * - replyWait->sequence_of_request is the sequence number of the
123 * message for which the client is waiting a reply. This is needed
124 * to determine if an error matches a replyWait.
125 *
126 * - replyWait->major_opcode_of_request is the major opcode of the
127 * message for which we are waiting a reply.
128 *
129 * - replyWait->minor_opcode_of_request is the minor opcode of the
130 * message for which we are waiting a reply.
131 *
132 * - replyWait->reply is a pointer to the reply message which will be
133 * filled in when the reply is ready (the protocol library should
134 * cast this IcePointer to the appropriate reply type). In most cases,
135 * the reply will have some fixed-size part, and the sender function
136 * will have provided a pointer to a structure (e.g.) to hold this
137 * fixed-size data. If there is variable-length data, it would be
138 * expected that the reply function will have to allocate additional
139 * memory and store pointer(s) to that memory in the fixed-size
140 * structure. If the entire data is variable length (e.g., a single
141 * variable-length string), then the sender function would probably
142 * just pass a pointer to fixed-size space to hold a pointer, and the
143 * reply function would allocate the storage and store the pointer.
144 * It is the responsibility of the client receiving the reply to
145 * free up any memory allocated on it's behalf.
146 *
147 * We might be waiting for several different replies (a function can wait
148 * for a reply, and while calling IceProcessMessages, a callback can be
149 * invoked which will wait for another reply). We take advantage of the
150 * fact that for a given protocol, we are guaranteed that messages are
151 * processed in the order we sent them. So, every time we have a new
152 * replyWait, we add it to the END of the 'saved_reply_waits' list. When
153 * we read a message and want to see if it matches a replyWait, we use the
154 * FIRST replyWait in the list with the major opcode of the message. If the
155 * reply is ready, we remove that replyWait from the list.
156 *
157 * If the reply/error is ready for the replyWait passed in to
158 * IceProcessMessages, *replyReadyRet is set to True.
159 *
160 * The return value of IceProcessMessages is one of the following:
161 *
162 * IceProcessMessagesSuccess - the message was processed successfully.
163 * IceProcessMessagesIOError - an IO error occurred. The caller should
164 * invoked IceCloseConnection.
165 * IceProcessMessagesConnectionClosed - the connection was closed as a
166 * result of shutdown negotiation.
167 */
168
169 IceProcessMessagesStatus
170 IceProcessMessages (
171 IceConn iceConn,
172 IceReplyWaitInfo *replyWait,
173 Bool *replyReadyRet
174 )
175 {
176 iceMsg *header;
177 Bool replyReady = False;
178 IceReplyWaitInfo *useThisReplyWait = NULL;
179 IceProcessMessagesStatus retStatus = IceProcessMessagesSuccess;
180
181 if (replyWait)
182 *replyReadyRet = False;
183
184 /*
185 * Each time IceProcessMessages is entered, we increment the dispatch
186 * level. Each time we leave it, we decrement the dispatch level.
187 */
188
189 iceConn->dispatch_level++;
190
191
192 /*
193 * Read the ICE message header.
194 */
195
196 if (!_IceRead (iceConn, (unsigned long) SIZEOF (iceMsg), iceConn->inbuf))
197 {
198 /*
199 * If we previously sent a WantToClose and now we detected
200 * that the connection was closed, _IceRead returns status 0.
201 * Since the connection was closed, we just want to return here.
202 */
203
204 return (IceProcessMessagesConnectionClosed);
205 }
206
207 if (!iceConn->io_ok)
208 {
209 /*
210 * An unexpected IO error occurred. The caller of IceProcessMessages
211 * should call IceCloseConnection which will cause the watch procedures
212 * to be invoked and the ICE connection to be freed.
213 */
214
215 iceConn->dispatch_level--;
216 iceConn->connection_status = IceConnectIOError;
217 return (IceProcessMessagesIOError);
218 }
219
220 header = (iceMsg *) iceConn->inbuf;
221 iceConn->inbufptr = iceConn->inbuf + SIZEOF (iceMsg);
222
223 iceConn->receive_sequence++;
224
225 if (iceConn->waiting_for_byteorder)
226 {
227 if (header->majorOpcode == 0 &&
228 header->minorOpcode == ICE_ByteOrder)
229 {
230 char byteOrder = ((iceByteOrderMsg *) header)->byteOrder;
231 int endian = 1;
232
233 CHECK_SIZE_MATCH (iceConn, ICE_ByteOrder,
234 header->length, SIZEOF (iceByteOrderMsg),
235 IceFatalToConnection, IceProcessMessagesIOError);
236
237 if (byteOrder != IceMSBfirst && byteOrder != IceLSBfirst)
238 {
239 _IceErrorBadValue (iceConn, 0,
240 ICE_ByteOrder, 2, 1, &byteOrder);
241
242 iceConn->connection_status = IceConnectRejected;
243 }
244 else
245 {
246 iceConn->swap =
247 (((*(char *) &endian) && byteOrder == IceMSBfirst) ||
248 (!(*(char *) &endian) && byteOrder == IceLSBfirst));
249
250 iceConn->waiting_for_byteorder = 0;
251 }
252 }
253 else
254 {
255 if (header->majorOpcode != 0)
256 {
257 _IceErrorBadMajor (iceConn, header->majorOpcode,
258 header->minorOpcode, IceFatalToConnection);
259 }
260 else
261 {
262 _IceErrorBadState (iceConn, 0,
263 header->minorOpcode, IceFatalToConnection);
264 }
265
266 iceConn->connection_status = IceConnectRejected;
267 }
268
269 iceConn->dispatch_level--;
270 if (!iceConn->io_ok)
271 {
272 iceConn->connection_status = IceConnectIOError;
273 retStatus = IceProcessMessagesIOError;
274 }
275
276 return (retStatus);
277 }
278
279 if (iceConn->swap)
280 {
281 /* swap the length field */
282
283 header->length = lswapl (header->length);
284 }
285
286 if (replyWait)
287 {
288 /*
289 * Add to the list of replyWaits (only if it doesn't exist
290 * in the list already.
291 */
292
293 _IceAddReplyWait (iceConn, replyWait);
294
295 /*
296 * Note that there are two different replyWaits. The first is
297 * the one passed into IceProcessMessages, and is the replyWait
298 * for the message the client is blocking on. The second is
299 * the replyWait for the message currently being processed
300 * by IceProcessMessages. We call it "useThisReplyWait".
301 *
302 * Also, when two hosts communicate over an ICE connection and use
303 * different major opcodes for a subprotocol, it is impossible
304 * to use message replies unless we translate opcodes before
305 * comparing them.
306 */
307
308 {
309 int op;
310
311 if (header->majorOpcode == 0)
312 {
313 op = 0;
314 }
315 else
316 {
317 int idx = header->majorOpcode - iceConn->his_min_opcode;
318 op = iceConn->process_msg_info[idx].my_opcode;
319 }
320 useThisReplyWait = _IceSearchReplyWaits (iceConn, op);
321 }
322 }
323
324 if (header->majorOpcode == 0)
325 {
326 /*
327 * ICE protocol
328 */
329
330 Bool connectionClosed;
331
332 _IceProcessCoreMsgProc processIce =
333 _IceVersions[iceConn->my_ice_version_index].process_core_msg_proc;
334
335 (*processIce) (iceConn, header->minorOpcode,
336 header->length, iceConn->swap,
337 useThisReplyWait, &replyReady, &connectionClosed);
338
339 if (connectionClosed)
340 {
341 /*
342 * As a result of shutdown negotiation, the connection was closed.
343 */
344
345 return (IceProcessMessagesConnectionClosed);
346 }
347 }
348 else
349 {
350 /*
351 * Sub protocol
352 */
353
354 if ((int) header->majorOpcode < iceConn->his_min_opcode ||
355 (int) header->majorOpcode > iceConn->his_max_opcode ||
356 !(iceConn->process_msg_info[header->majorOpcode -
357 iceConn->his_min_opcode].in_use))
358 {
359 /*
360 * The protocol of the message we just read is not supported.
361 */
362
363 _IceErrorBadMajor (iceConn, header->majorOpcode,
364 header->minorOpcode, IceCanContinue);
365
366 _IceReadSkip (iceConn, header->length << 3);
367 }
368 else
369 {
370 _IceProcessMsgInfo *processMsgInfo = &iceConn->process_msg_info[
371 header->majorOpcode - iceConn->his_min_opcode];
372
373 if (processMsgInfo->accept_flag)
374 {
375 IcePaProcessMsgProc processProc =
376 processMsgInfo->process_msg_proc.accept_client;
377
378 (*processProc) (iceConn, processMsgInfo->client_data,
379 header->minorOpcode, header->length, iceConn->swap);
380 }
381 else
382 {
383 IcePoProcessMsgProc processProc =
384 processMsgInfo->process_msg_proc.orig_client;
385
386 (*processProc) (iceConn,
387 processMsgInfo->client_data, header->minorOpcode,
388 header->length, iceConn->swap,
389 useThisReplyWait, &replyReady);
390 }
391 }
392 }
393
394 if (replyReady)
395 {
396 _IceSetReplyReady (iceConn, useThisReplyWait);
397 }
398
399
400 /*
401 * Now we check if the reply is ready for the replyWait passed
402 * into IceProcessMessages. The replyWait is removed from the
403 * replyWait list if it is ready.
404 */
405
406 if (replyWait)
407 *replyReadyRet = _IceCheckReplyReady (iceConn, replyWait);
408
409
410 /*
411 * Decrement the dispatch level. If we reach level 0, and the
412 * free_asap bit is set, free the connection now. Also check for
413 * possible bad IO status.
414 */
415
416 iceConn->dispatch_level--;
417
418 if (iceConn->dispatch_level == 0 && iceConn->free_asap)
419 {
420 _IceFreeConnection (iceConn);
421 retStatus = IceProcessMessagesConnectionClosed;
422 }
423 else if (!iceConn->io_ok)
424 {
425 iceConn->connection_status = IceConnectIOError;
426 retStatus = IceProcessMessagesIOError;
427 }
428
429 return (retStatus);
430 }
431
432
433
434 static void
435 AuthRequired (
436 IceConn iceConn,
437 int authIndex,
438 int authDataLen,
439 IcePointer authData
440 )
441 {
442 iceAuthRequiredMsg *pMsg;
443
444 IceGetHeader (iceConn, 0, ICE_AuthRequired,
445 SIZEOF (iceAuthRequiredMsg), iceAuthRequiredMsg, pMsg);
446
447 pMsg->authIndex = authIndex;
448 pMsg->authDataLength = authDataLen;
449 pMsg->length += WORD64COUNT (authDataLen);
450
451 IceWriteData (iceConn, authDataLen, (char *) authData);
452
453 if (PAD64 (authDataLen))
454 IceWritePad (iceConn, PAD64 (authDataLen));
455
456 IceFlush (iceConn);
457 }
458
459
460
461 static void
462 AuthReply (
463 IceConn iceConn,
464 int authDataLen,
465 IcePointer authData
466 )
467 {
468 iceAuthReplyMsg *pMsg;
469
470 IceGetHeader (iceConn, 0, ICE_AuthReply,
471 SIZEOF (iceAuthReplyMsg), iceAuthReplyMsg, pMsg);
472
473 pMsg->authDataLength = authDataLen;
474 pMsg->length += WORD64COUNT (authDataLen);
475
476 IceWriteData (iceConn, authDataLen, (char *) authData);
477
478 if (PAD64 (authDataLen))
479 IceWritePad (iceConn, PAD64 (authDataLen));
480
481 IceFlush (iceConn);
482 }
483
484
485
486 static void
487 AuthNextPhase (
488 IceConn iceConn,
489 int authDataLen,
490 IcePointer authData
491 )
492 {
493 iceAuthNextPhaseMsg *pMsg;
494
495 IceGetHeader (iceConn, 0, ICE_AuthNextPhase,
496 SIZEOF (iceAuthNextPhaseMsg), iceAuthNextPhaseMsg, pMsg);
497
498 pMsg->authDataLength = authDataLen;
499 pMsg->length += WORD64COUNT (authDataLen);
500
501 IceWriteData (iceConn, authDataLen, (char *) authData);
502
503 if (PAD64 (authDataLen))
504 IceWritePad (iceConn, PAD64 (authDataLen));
505
506 IceFlush (iceConn);
507 }
508
509
510
511 static void
512 AcceptConnection (
513 IceConn iceConn,
514 int versionIndex
515 )
516 {
517 iceConnectionReplyMsg *pMsg;
518 char *pData;
519 int extra;
520
521 extra = STRING_BYTES (IceVendorString) + STRING_BYTES (IceReleaseString);
522
523 IceGetHeaderExtra (iceConn, 0, ICE_ConnectionReply,
524 SIZEOF (iceConnectionReplyMsg), WORD64COUNT (extra),
525 iceConnectionReplyMsg, pMsg, pData);
526
527 pMsg->versionIndex = versionIndex;
528
529 if (pData != NULL) {
530 STORE_STRING (pData, IceVendorString);
531 STORE_STRING (pData, IceReleaseString);
532
533 IceFlush (iceConn);
534 } else {
535 SEND_STRING (iceConn, IceVendorString);
536 SEND_STRING (iceConn, IceReleaseString);
537 }
538
539 iceConn->connection_status = IceConnectAccepted;
540 }
541
542
543
544 static void
545 AcceptProtocol (
546 IceConn iceConn,
547 int hisOpcode,
548 int myOpcode,
549 int versionIndex,
550 char *vendor,
551 char *release
552 )
553 {
554 iceProtocolReplyMsg *pMsg;
555 char *pData;
556 int extra;
557
558 extra = STRING_BYTES (vendor) + STRING_BYTES (release);
559
560 IceGetHeaderExtra (iceConn, 0, ICE_ProtocolReply,
561 SIZEOF (iceProtocolReplyMsg), WORD64COUNT (extra),
562 iceProtocolReplyMsg, pMsg, pData);
563
564 pMsg->protocolOpcode = myOpcode;
565 pMsg->versionIndex = versionIndex;
566
567 if (pData != NULL) {
568 STORE_STRING (pData, vendor);
569 STORE_STRING (pData, release);
570
571 IceFlush (iceConn);
572 } else {
573 SEND_STRING (iceConn, vendor);
574 SEND_STRING (iceConn, release);
575 }
576
577
578 /*
579 * We may be using a different major opcode for this protocol
580 * than the other client. Whenever we get a message, we must
581 * map to our own major opcode.
582 */
583
584 _IceAddOpcodeMapping (iceConn, hisOpcode, myOpcode);
585 }
586
587
588
589 static void
590 PingReply (
591 IceConn iceConn
592 )
593 {
594 IceSimpleMessage (iceConn, 0, ICE_PingReply);
595 IceFlush (iceConn);
596 }
597
598
599
600 static Bool
601 ProcessError (
602 IceConn iceConn,
603 unsigned long length,
604 Bool swap,
605 IceReplyWaitInfo *replyWait
606 )
607 {
608 int invokeHandler = 0;
609 Bool errorReturned = False;
610 iceErrorMsg *message;
611 char *pData, *pStart;
612 char severity;
613
614 CHECK_AT_LEAST_SIZE (iceConn, ICE_Error,
615 length, SIZEOF (iceErrorMsg),
616 (iceConn->connect_to_you || iceConn->connect_to_me) ?
617 IceFatalToConnection : IceFatalToProtocol);
618
619 IceReadCompleteMessage (iceConn, SIZEOF (iceErrorMsg),
620 iceErrorMsg, message, pStart);
621
622 if (!IceValidIO (iceConn))
623 {
624 IceDisposeCompleteMessage (iceConn, pStart);
625 return (0);
626 }
627
628 severity = message->severity;
629
630 if (severity != IceCanContinue && severity != IceFatalToProtocol &&
631 severity != IceFatalToConnection)
632 {
633 _IceErrorBadValue (iceConn, 0,
634 ICE_Error, 9, 1, &severity);
635 IceDisposeCompleteMessage (iceConn, pStart);
636 return (0);
637 }
638
639 pData = pStart;
640
641 if (swap)
642 {
643 message->errorClass = lswaps (message->errorClass);
644 message->offendingSequenceNum = lswapl (message->offendingSequenceNum);
645 }
646
647 if (!replyWait ||
648 message->offendingSequenceNum != replyWait->sequence_of_request)
649 {
650 invokeHandler = 1;
651 }
652 else
653 {
654 if (iceConn->connect_to_you &&
655 ((!iceConn->connect_to_you->auth_active &&
656 message->offendingMinorOpcode == ICE_ConnectionSetup) ||
657 (iceConn->connect_to_you->auth_active &&
658 message->offendingMinorOpcode == ICE_AuthReply)))
659 {
660 _IceConnectionError *errorReply =
661 &(((_IceReply *) (replyWait->reply))->connection_error);
662 char *errorStr = NULL;
663 const char *tempstr, *prefix;
664 char *temp;
665
666 invokeHandler = 0;
667 errorReturned = True;
668
669 switch (message->errorClass)
670 {
671 case IceNoVersion:
672
673 tempstr =
674 "None of the ICE versions specified are supported";
675 errorStr = strdup(tempstr);
676 break;
677
678 case IceNoAuth:
679
680 tempstr =
681 "None of the authentication protocols specified are supported";
682 errorStr = strdup(tempstr);
683 break;
684
685 case IceSetupFailed:
686
687 prefix = "Connection Setup Failed, reason : ";
688
689 EXTRACT_STRING (pData, swap, temp);
690 if (asprintf (&errorStr, "%s%s", prefix, temp) == -1)
691 errorStr = NULL;
692 free (temp);
693 break;
694
695 case IceAuthRejected:
696
697 prefix = "Authentication Rejected, reason : ";
698 EXTRACT_STRING (pData, swap, temp);
699 if (asprintf (&errorStr, "%s%s", prefix, temp) == -1)
700 errorStr = NULL;
701 free (temp);
702 break;
703
704 case IceAuthFailed:
705
706 prefix = "Authentication Failed, reason : ";
707 EXTRACT_STRING (pData, swap, temp);
708 if (asprintf (&errorStr, "%s%s", prefix, temp) == -1)
709 errorStr = NULL;
710 free (temp);
711 break;
712
713 default:
714 invokeHandler = 1;
715 }
716
717 if (!errorStr)
718 {
719 errorStr = strdup("");
720 }
721
722 errorReply->type = ICE_CONNECTION_ERROR;
723 errorReply->error_message = errorStr;
724 }
725 else if (iceConn->protosetup_to_you &&
726 ((!iceConn->protosetup_to_you->auth_active &&
727 message->offendingMinorOpcode == ICE_ProtocolSetup) ||
728 (iceConn->protosetup_to_you->auth_active &&
729 message->offendingMinorOpcode == ICE_AuthReply)))
730 {
731 _IceProtocolError *errorReply =
732 &(((_IceReply *) (replyWait->reply))->protocol_error);
733 char *errorStr = NULL;
734 const char *prefix;
735 char *temp;
736
737 invokeHandler = 0;
738 errorReturned = True;
739
740 switch (message->errorClass)
741 {
742 case IceNoVersion:
743
744 errorStr = strdup(
745 "None of the protocol versions specified are supported");
746 break;
747
748 case IceNoAuth:
749
750 errorStr = strdup(
751 "None of the authentication protocols specified are supported");
752
753 break;
754
755 case IceSetupFailed:
756
757 prefix = "Protocol Setup Failed, reason : ";
758
759 EXTRACT_STRING (pData, swap, temp);
760 if (asprintf (&errorStr, "%s%s", prefix, temp) == -1)
761 errorStr = NULL;
762 free (temp);
763 break;
764
765 case IceAuthRejected:
766
767 prefix = "Authentication Rejected, reason : ";
768 EXTRACT_STRING (pData, swap, temp);
769 if (asprintf (&errorStr, "%s%s", prefix, temp) == -1)
770 errorStr = NULL;
771 free (temp);
772 break;
773
774 case IceAuthFailed:
775
776 prefix = "Authentication Failed, reason : ";
777 EXTRACT_STRING (pData, swap, temp);
778 if (asprintf (&errorStr, "%s%s", prefix, temp) == -1)
779 errorStr = NULL;
780 free (temp);
781 break;
782
783 case IceProtocolDuplicate:
784
785 prefix = "Protocol was already registered : ";
786 EXTRACT_STRING (pData, swap, temp);
787 if (asprintf (&errorStr, "%s%s", prefix, temp) == -1)
788 errorStr = NULL;
789 free (temp);
790 break;
791
792 case IceMajorOpcodeDuplicate:
793
794 prefix = "The major opcode was already used : ";
795 if (asprintf (&errorStr, "%s%d", prefix, (int) *pData) == -1)
796 errorStr = NULL;
797 break;
798
799 case IceUnknownProtocol:
800
801 prefix = "Unknown Protocol : ";
802 EXTRACT_STRING (pData, swap, temp);
803 if (asprintf (&errorStr, "%s%s", prefix, temp) == -1)
804 errorStr = NULL;
805 free (temp);
806 break;
807
808 default:
809 invokeHandler = 1;
810 }
811
812 if (!errorStr)
813 {
814 errorStr = strdup("");
815 }
816
817 errorReply->type = ICE_PROTOCOL_ERROR;
818 errorReply->error_message = errorStr;
819 }
820
821 if (errorReturned == True)
822 {
823 /*
824 * If we tried to authenticate, tell the authentication
825 * procedure to clean up.
826 */
827
828 IcePoAuthProc authProc;
829
830 if (iceConn->connect_to_you &&
831 iceConn->connect_to_you->auth_active)
832 {
833 authProc = _IcePoAuthProcs[(int)
834 (iceConn->connect_to_you->my_auth_index)];
835
836 (*authProc) (iceConn, &iceConn->connect_to_you->my_auth_state,
837 True /* clean up */, False /* swap */,
838 0, NULL, NULL, NULL, NULL);
839 }
840 else if (iceConn->protosetup_to_you &&
841 iceConn->protosetup_to_you->auth_active)
842 {
843 _IcePoProtocol *protocol = _IceProtocols[
844 iceConn->protosetup_to_you->my_opcode - 1].orig_client;
845
846 authProc = protocol->auth_procs[(int)(iceConn->
847 protosetup_to_you->my_auth_index)];
848
849 (*authProc) (iceConn,
850 &iceConn->protosetup_to_you->my_auth_state,
851 True /* clean up */, False /* swap */,
852 0, NULL, NULL, NULL, NULL);
853 }
854 }
855 }
856
857 if (invokeHandler)
858 {
859 (*_IceErrorHandler) (iceConn, swap, message->offendingMinorOpcode,
860 message->offendingSequenceNum, message->errorClass,
861 message->severity, (IcePointer) pData);
862 }
863
864 IceDisposeCompleteMessage (iceConn, pStart);
865
866 return (errorReturned);
867 }
868
869
870
871 static int
872 ProcessConnectionSetup (
873 IceConn iceConn,
874 unsigned long length,
875 Bool swap
876 )
877 {
878 iceConnectionSetupMsg *message;
879 const int myVersionCount = _IceVersionCount;
880 int hisVersionCount;
881 int myVersionIndex, hisVersionIndex;
882 int hisMajorVersion, hisMinorVersion;
883 int myAuthCount, hisAuthCount;
884 int found, i, j;
885 char **hisAuthNames = NULL;
886 char *pData, *pStart, *pEnd;
887 char *vendor = NULL;
888 char *release = NULL;
889 int myAuthIndex = 0;
890 int hisAuthIndex = 0;
891 int accept_setup_now = 0;
892 char mustAuthenticate;
893 int authUsableCount;
894 int authUsableFlags[MAX_ICE_AUTH_NAMES];
895 int authIndices[MAX_ICE_AUTH_NAMES];
896
897 CHECK_AT_LEAST_SIZE (iceConn, ICE_ConnectionSetup,
898 length, SIZEOF (iceConnectionSetupMsg), IceFatalToConnection);
899
900 IceReadCompleteMessage (iceConn, SIZEOF (iceConnectionSetupMsg),
901 iceConnectionSetupMsg, message, pStart);
902
903 if (!IceValidIO (iceConn))
904 {
905 IceDisposeCompleteMessage (iceConn, pStart);
906 return (0);
907 }
908
909 pData = pStart;
910 pEnd = pStart + (length << 3);
911
912 SKIP_STRING (pData, swap, pEnd,
913 BAIL_STRING(iceConn, ICE_ConnectionSetup,
914 pStart)); /* vendor */
915 SKIP_STRING (pData, swap, pEnd,
916 BAIL_STRING(iceConn, ICE_ConnectionSetup,
917 pStart)); /* release */
918 SKIP_LISTOF_STRING (pData, swap, (int) message->authCount, pEnd,
919 BAIL_STRING(iceConn, ICE_ConnectionSetup,
920 pStart)); /* auth names */
921
922 pData += (message->versionCount * 4); /* versions */
923
924 CHECK_COMPLETE_SIZE (iceConn, ICE_ConnectionSetup,
925 length, pData - pStart + SIZEOF (iceConnectionSetupMsg),
926 pStart, IceFatalToConnection);
927
928 mustAuthenticate = message->mustAuthenticate;
929 if (mustAuthenticate != 0 && mustAuthenticate != 1)
930 {
931 _IceErrorBadValue (iceConn, 0,
932 ICE_ConnectionSetup, 8, 1, &mustAuthenticate);
933 iceConn->connection_status = IceConnectRejected;
934 IceDisposeCompleteMessage (iceConn, pStart);
935 return (0);
936 }
937
938 pData = pStart;
939
940 EXTRACT_STRING (pData, swap, vendor);
941 EXTRACT_STRING (pData, swap, release);
942
943 hisAuthCount = message->authCount;
944 if (hisAuthCount > 0)
945 {
946 hisAuthNames = malloc (hisAuthCount * sizeof (char *));
947 EXTRACT_LISTOF_STRING (pData, swap, hisAuthCount, hisAuthNames);
948 }
949
950 hisVersionCount = message->versionCount;
951
952 hisVersionIndex = myVersionIndex = found = 0;
953
954 for (i = 0; i < hisVersionCount && !found; i++)
955 {
956 EXTRACT_CARD16 (pData, swap, hisMajorVersion);
957 EXTRACT_CARD16 (pData, swap, hisMinorVersion);
958
959 for (j = 0; j < myVersionCount && !found; j++)
960 {
961 if (_IceVersions[j].major_version == hisMajorVersion &&
962 _IceVersions[j].minor_version == hisMinorVersion)
963 {
964 hisVersionIndex = i;
965 myVersionIndex = j;
966 found = 1;
967 }
968 }
969 }
970
971 if (!found)
972 {
973 _IceErrorNoVersion (iceConn, ICE_ConnectionSetup);
974 iceConn->connection_status = IceConnectRejected;
975
976 free (vendor);
977 free (release);
978
979 if (hisAuthCount > 0)
980 {
981 for (i = 0; i < hisAuthCount; i++)
982 free (hisAuthNames[i]);
983
984 free (hisAuthNames);
985 }
986
987 IceDisposeCompleteMessage (iceConn, pStart);
988 return (0);
989 }
990
991 _IceGetPaValidAuthIndices ("ICE", iceConn->connection_string,
992 _IceAuthCount, _IceAuthNames, &authUsableCount, authIndices);
993
994 for (i = 0; i < _IceAuthCount; i++)
995 {
996 authUsableFlags[i] = 0;
997 for (j = 0; j < authUsableCount && !authUsableFlags[i]; j++)
998 authUsableFlags[i] = (authIndices[j] == i);
999 }
1000
1001 myAuthCount = _IceAuthCount;
1002
1003 for (i = found = 0; i < myAuthCount && !found; i++)
1004 {
1005 if (authUsableFlags[i])
1006 {
1007 const char *myAuthName = _IceAuthNames[i];
1008
1009 for (j = 0; j < hisAuthCount && !found; j++)
1010 if (strcmp (myAuthName, hisAuthNames[j]) == 0)
1011 {
1012 myAuthIndex = i;
1013 hisAuthIndex = j;
1014 found = 1;
1015 }
1016 }
1017 }
1018
1019 if (!found)
1020 {
1021 /*
1022 * None of the authentication methods specified by the
1023 * other client is supported. If the other client requires
1024 * authentication, we must reject the connection now.
1025 * Otherwise, we can invoke the host-based authentication callback
1026 * to see if we can accept this connection.
1027 */
1028
1029 if (mustAuthenticate || !iceConn->listen_obj->host_based_auth_proc)
1030 {
1031 _IceErrorNoAuthentication (iceConn, ICE_ConnectionSetup);
1032 iceConn->connection_status = IceConnectRejected;
1033 }
1034 else
1035 {
1036 char *hostname = _IceGetPeerName (iceConn);
1037
1038 if ((*iceConn->listen_obj->host_based_auth_proc) (hostname))
1039 {
1040 accept_setup_now = 1;
1041 }
1042 else
1043 {
1044 _IceErrorAuthenticationRejected (iceConn,
1045 ICE_ConnectionSetup, "None of the authentication protocols specified are supported and host-based authentication failed");
1046
1047 iceConn->connection_status = IceConnectRejected;
1048 }
1049
1050 free (hostname);
1051 }
1052
1053 if (iceConn->connection_status == IceConnectRejected)
1054 {
1055 free (vendor);
1056 free (release);
1057 }
1058 }
1059 else
1060 {
1061 IcePaAuthStatus status;
1062 int authDataLen;
1063 IcePointer authData = NULL;
1064 IcePointer authState;
1065 char *errorString = NULL;
1066 IcePaAuthProc authProc = _IcePaAuthProcs[myAuthIndex];
1067
1068 authState = NULL;
1069
1070 status = (*authProc) (iceConn, &authState,
1071 swap, 0, NULL, &authDataLen, &authData, &errorString);
1072
1073 if (status == IcePaAuthContinue)
1074 {
1075 _IceConnectToMeInfo *setupInfo;
1076
1077 AuthRequired (iceConn, hisAuthIndex, authDataLen, authData);
1078
1079 iceConn->connect_to_me = setupInfo =
1080 malloc (sizeof (_IceConnectToMeInfo));
1081
1082 setupInfo->my_version_index = myVersionIndex;
1083 setupInfo->his_version_index = hisVersionIndex;
1084 setupInfo->his_vendor = vendor;
1085 setupInfo->his_release = release;
1086 setupInfo->my_auth_index = myAuthIndex;
1087 setupInfo->my_auth_state = authState;
1088 setupInfo->must_authenticate = mustAuthenticate;
1089 }
1090 else if (status == IcePaAuthAccepted)
1091 {
1092 accept_setup_now = 1;
1093 }
1094 else
1095 {
1096 free (vendor);
1097 free (release);
1098 }
1099
1100 if (authData && authDataLen > 0)
1101 free (authData);
1102
1103 free (errorString);
1104 }
1105
1106 if (accept_setup_now)
1107 {
1108 AcceptConnection (iceConn, hisVersionIndex);
1109
1110 iceConn->vendor = vendor;
1111 iceConn->release = release;
1112 iceConn->my_ice_version_index = myVersionIndex;
1113 }
1114
1115 if (hisAuthCount > 0)
1116 {
1117 for (i = 0; i < hisAuthCount; i++)
1118 free (hisAuthNames[i]);
1119
1120 free (hisAuthNames);
1121 }
1122
1123 IceDisposeCompleteMessage (iceConn, pStart);
1124 return (0);
1125 }
1126
1127
1128
1129 static Bool
1130 ProcessAuthRequired (
1131 IceConn iceConn,
1132 unsigned long length,
1133 Bool swap,
1134 IceReplyWaitInfo *replyWait
1135 )
1136 {
1137 iceAuthRequiredMsg *message;
1138 int authDataLen;
1139 IcePointer authData;
1140 int replyDataLen;
1141 IcePointer replyData = NULL;
1142 char *errorString = NULL;
1143 IcePoAuthProc authProc;
1144 IcePoAuthStatus status;
1145 IcePointer authState;
1146 int realAuthIndex = 0;
1147
1148 CHECK_AT_LEAST_SIZE (iceConn, ICE_AuthRequired,
1149 length, SIZEOF (iceAuthRequiredMsg),
1150 iceConn->connect_to_you ? IceFatalToConnection : IceFatalToProtocol);
1151
1152 IceReadCompleteMessage (iceConn, SIZEOF (iceAuthRequiredMsg),
1153 iceAuthRequiredMsg, message, authData);
1154
1155 if (!IceValidIO (iceConn))
1156 {
1157 IceDisposeCompleteMessage (iceConn, authData);
1158 return (0);
1159 }
1160
1161 if (swap)
1162 {
1163 message->authDataLength = lswaps (message->authDataLength);
1164 }
1165
1166 CHECK_COMPLETE_SIZE (iceConn, ICE_AuthRequired, length,
1167 message->authDataLength + SIZEOF (iceAuthRequiredMsg), authData,
1168 iceConn->connect_to_you ? IceFatalToConnection : IceFatalToProtocol);
1169
1170 if (iceConn->connect_to_you)
1171 {
1172 if ((int) message->authIndex >= _IceAuthCount)
1173 {
1174 _IceConnectionError *errorReply =
1175 &(((_IceReply *) (replyWait->reply))->connection_error);
1176
1177 const char *tempstr
1178 = "Received bad authIndex in the AuthRequired message";
1179 char errIndex = (int) message->authIndex;
1180
1181 errorString = strdup(tempstr);
1182
1183 errorReply->type = ICE_CONNECTION_ERROR;
1184 errorReply->error_message = errorString;
1185
1186 _IceErrorBadValue (iceConn, 0,
1187 ICE_AuthRequired, 2, 1, &errIndex);
1188
1189 IceDisposeCompleteMessage (iceConn, authData);
1190 return (1);
1191 }
1192 else
1193 {
1194 authProc = _IcePoAuthProcs[message->authIndex];
1195
1196 iceConn->connect_to_you->auth_active = 1;
1197 }
1198 }
1199 else if (iceConn->protosetup_to_you)
1200 {
1201 if ((int) message->authIndex >=
1202 iceConn->protosetup_to_you->my_auth_count)
1203 {
1204 _IceProtocolError *errorReply =
1205 &(((_IceReply *) (replyWait->reply))->protocol_error);
1206
1207 const char *tempstr
1208 = "Received bad authIndex in the AuthRequired message";
1209 char errIndex = (int) message->authIndex;
1210
1211 errorString = strdup(tempstr);
1212
1213 errorReply->type = ICE_PROTOCOL_ERROR;
1214 errorReply->error_message = errorString;
1215
1216 _IceErrorBadValue (iceConn, 0,
1217 ICE_AuthRequired, 2, 1, &errIndex);
1218
1219 IceDisposeCompleteMessage (iceConn, authData);
1220 return (1);
1221 }
1222 else
1223 {
1224 _IcePoProtocol *myProtocol = _IceProtocols[
1225 iceConn->protosetup_to_you->my_opcode - 1].orig_client;
1226
1227 realAuthIndex = iceConn->protosetup_to_you->
1228 my_auth_indices[message->authIndex];
1229
1230 authProc = myProtocol->auth_procs[realAuthIndex];
1231
1232 iceConn->protosetup_to_you->auth_active = 1;
1233 }
1234 }
1235 else
1236 {
1237 /*
1238 * Unexpected message
1239 */
1240
1241 _IceErrorBadState (iceConn, 0, ICE_AuthRequired, IceCanContinue);
1242
1243 IceDisposeCompleteMessage (iceConn, authData);
1244 return (0);
1245 }
1246
1247 authState = NULL;
1248 authDataLen = message->authDataLength;
1249
1250 status = (*authProc) (iceConn, &authState, False /* don't clean up */,
1251 swap, authDataLen, authData, &replyDataLen, &replyData, &errorString);
1252
1253 if (status == IcePoAuthHaveReply)
1254 {
1255 AuthReply (iceConn, replyDataLen, replyData);
1256
1257 replyWait->sequence_of_request = iceConn->send_sequence;
1258 replyWait->minor_opcode_of_request = ICE_AuthReply;
1259
1260 if (iceConn->connect_to_you)
1261 {
1262 iceConn->connect_to_you->my_auth_state = authState;
1263 iceConn->connect_to_you->my_auth_index = message->authIndex;
1264 }
1265 else if (iceConn->protosetup_to_you)
1266 {
1267 iceConn->protosetup_to_you->my_auth_state = authState;
1268 iceConn->protosetup_to_you->my_auth_index = realAuthIndex;
1269 }
1270 }
1271 else if (status == IcePoAuthRejected || status == IcePoAuthFailed)
1272 {
1273 const char *prefix;
1274 char *returnErrorString;
1275
1276 if (status == IcePoAuthRejected)
1277 {
1278 _IceErrorAuthenticationRejected (iceConn,
1279 ICE_AuthRequired, errorString);
1280
1281 prefix = "Authentication Rejected, reason : ";
1282 }
1283 else
1284 {
1285 _IceErrorAuthenticationFailed (iceConn,
1286 ICE_AuthRequired, errorString);
1287
1288 prefix = "Authentication Failed, reason : ";
1289 }
1290
1291 if (asprintf (&returnErrorString, "%s%s", prefix, errorString) == -1)
1292 returnErrorString = strdup("");
1293 free (errorString);
1294
1295 if (iceConn->connect_to_you)
1296 {
1297 _IceConnectionError *errorReply =
1298 &(((_IceReply *) (replyWait->reply))->connection_error);
1299
1300 errorReply->type = ICE_CONNECTION_ERROR;
1301 errorReply->error_message = returnErrorString;
1302 }
1303 else
1304 {
1305 _IceProtocolError *errorReply =
1306 &(((_IceReply *) (replyWait->reply))->protocol_error);
1307
1308 errorReply->type = ICE_PROTOCOL_ERROR;
1309 errorReply->error_message = returnErrorString;
1310 }
1311 }
1312
1313 if (replyData && replyDataLen > 0)
1314 free (replyData);
1315
1316 IceDisposeCompleteMessage (iceConn, authData);
1317
1318 return (status != IcePoAuthHaveReply);
1319 }
1320
1321
1322
1323 static int
1324 ProcessAuthReply (
1325 IceConn iceConn,
1326 unsigned long length,
1327 Bool swap
1328 )
1329 {
1330 iceAuthReplyMsg *message;
1331 int replyDataLen;
1332 IcePointer replyData;
1333 int authDataLen;
1334 IcePointer authData = NULL;
1335 char *errorString = NULL;
1336
1337 CHECK_AT_LEAST_SIZE (iceConn, ICE_AuthReply,
1338 length, SIZEOF (iceAuthReplyMsg),
1339 iceConn->connect_to_me ? IceFatalToConnection : IceFatalToProtocol);
1340
1341 IceReadCompleteMessage (iceConn, SIZEOF (iceAuthReplyMsg),
1342 iceAuthReplyMsg, message, replyData);
1343
1344 if (!IceValidIO (iceConn))
1345 {
1346 IceDisposeCompleteMessage (iceConn, replyData);
1347 return (0);
1348 }
1349
1350 if (swap)
1351 {
1352 message->authDataLength = lswaps (message->authDataLength);
1353 }
1354
1355 CHECK_COMPLETE_SIZE (iceConn, ICE_AuthReply, length,
1356 message->authDataLength + SIZEOF (iceAuthReplyMsg), replyData,
1357 iceConn->connect_to_me ? IceFatalToConnection : IceFatalToProtocol);
1358
1359 replyDataLen = message->authDataLength;
1360
1361 if (iceConn->connect_to_me)
1362 {
1363 IcePaAuthProc authProc = _IcePaAuthProcs[(int)
1364 (iceConn->connect_to_me->my_auth_index)];
1365 IcePaAuthStatus status =
1366 (*authProc) (iceConn, &iceConn->connect_to_me->my_auth_state, swap,
1367 replyDataLen, replyData, &authDataLen, &authData, &errorString);
1368
1369 if (status == IcePaAuthContinue)
1370 {
1371 AuthNextPhase (iceConn, authDataLen, authData);
1372 }
1373 else if (status == IcePaAuthRejected || status == IcePaAuthFailed)
1374 {
1375 /*
1376 * Before we reject, invoke host-based authentication callback
1377 * and give it a chance to accept the connection (only if the
1378 * other client doesn't require authentication).
1379 */
1380
1381 if (!iceConn->connect_to_me->must_authenticate &&
1382 iceConn->listen_obj->host_based_auth_proc)
1383 {
1384 char *hostname = _IceGetPeerName (iceConn);
1385
1386 if ((*iceConn->listen_obj->host_based_auth_proc) (hostname))
1387 {
1388 status = IcePaAuthAccepted;
1389 }
1390
1391 free (hostname);
1392 }
1393
1394 if (status != IcePaAuthAccepted)
1395 {
1396 free (iceConn->connect_to_me->his_vendor);
1397 free (iceConn->connect_to_me->his_release);
1398 free (iceConn->connect_to_me);
1399 iceConn->connect_to_me = NULL;
1400
1401 iceConn->connection_status = IceConnectRejected;
1402
1403 if (status == IcePaAuthRejected)
1404 {
1405 _IceErrorAuthenticationRejected (iceConn,
1406 ICE_AuthReply, errorString);
1407 }
1408 else
1409 {
1410 _IceErrorAuthenticationFailed (iceConn,
1411 ICE_AuthReply, errorString);
1412 }
1413 }
1414 }
1415
1416 if (status == IcePaAuthAccepted)
1417 {
1418 AcceptConnection (iceConn,
1419 iceConn->connect_to_me->his_version_index);
1420
1421 iceConn->vendor = iceConn->connect_to_me->his_vendor;
1422 iceConn->release = iceConn->connect_to_me->his_release;
1423 iceConn->my_ice_version_index =
1424 iceConn->connect_to_me->my_version_index;
1425
1426 free (iceConn->connect_to_me);
1427 iceConn->connect_to_me = NULL;
1428 }
1429 }
1430 else if (iceConn->protosetup_to_me)
1431 {
1432 _IcePaProtocol *myProtocol = _IceProtocols[iceConn->protosetup_to_me->
1433 my_opcode - 1].accept_client;
1434 IcePaAuthProc authProc = myProtocol->auth_procs[(int)
1435 (iceConn->protosetup_to_me->my_auth_index)];
1436 IcePaAuthStatus status =
1437 (*authProc) (iceConn, &iceConn->protosetup_to_me->my_auth_state,
1438 swap, replyDataLen, replyData,
1439 &authDataLen, &authData, &errorString);
1440 int free_setup_info = 1;
1441
1442 if (status == IcePaAuthContinue)
1443 {
1444 AuthNextPhase (iceConn, authDataLen, authData);
1445 free_setup_info = 0;
1446 }
1447 else if (status == IcePaAuthRejected || status == IcePaAuthFailed)
1448 {
1449 /*
1450 * Before we reject, invoke host-based authentication callback
1451 * and give it a chance to accept the Protocol Setup (only if the
1452 * other client doesn't require authentication).
1453 */
1454
1455 if (!iceConn->protosetup_to_me->must_authenticate &&
1456 myProtocol->host_based_auth_proc)
1457 {
1458 char *hostname = _IceGetPeerName (iceConn);
1459
1460 if ((*myProtocol->host_based_auth_proc) (hostname))
1461 {
1462 status = IcePaAuthAccepted;
1463 }
1464
1465 free (hostname);
1466 }
1467
1468 if (status == IcePaAuthRejected)
1469 {
1470 _IceErrorAuthenticationRejected (iceConn,
1471 ICE_AuthReply, errorString);
1472 }
1473 else
1474 {
1475 _IceErrorAuthenticationFailed (iceConn,
1476 ICE_AuthReply, errorString);
1477 }
1478 }
1479
1480 if (status == IcePaAuthAccepted)
1481 {
1482 IcePaProcessMsgProc processMsgProc;
1483 IceProtocolSetupProc protocolSetupProc;
1484 IceProtocolActivateProc protocolActivateProc;
1485 _IceProcessMsgInfo *process_msg_info;
1486 IcePointer clientData = NULL;
1487 char *failureReason = NULL;
1488 Status setupStatus = 1;
1489
1490 protocolSetupProc = myProtocol->protocol_setup_proc;
1491 protocolActivateProc = myProtocol->protocol_activate_proc;
1492
1493 if (protocolSetupProc)
1494 {
1495 /*
1496 * Notify the client of the Protocol Setup.
1497 */
1498
1499 setupStatus = (*protocolSetupProc) (iceConn,
1500 myProtocol->version_recs[iceConn->protosetup_to_me->
1501 my_version_index].major_version,
1502 myProtocol->version_recs[iceConn->protosetup_to_me->
1503 my_version_index].minor_version,
1504 iceConn->protosetup_to_me->his_vendor,
1505 iceConn->protosetup_to_me->his_release,
1506 &clientData, &failureReason);
1507
1508 /*
1509 * Set vendor and release pointers to NULL, so it won't
1510 * get freed below. The ProtocolSetupProc should
1511 * free it.
1512 */
1513
1514 iceConn->protosetup_to_me->his_vendor = NULL;
1515 iceConn->protosetup_to_me->his_release = NULL;
1516 }
1517
1518 if (setupStatus != 0)
1519 {
1520 /*
1521 * Send the Protocol Reply
1522 */
1523
1524 AcceptProtocol (iceConn,
1525 iceConn->protosetup_to_me->his_opcode,
1526 iceConn->protosetup_to_me->my_opcode,
1527 iceConn->protosetup_to_me->his_version_index,
1528 myProtocol->vendor, myProtocol->release);
1529
1530
1531 /*
1532 * Set info for this protocol.
1533 */
1534
1535 processMsgProc = myProtocol->version_recs[
1536 iceConn->protosetup_to_me->
1537 my_version_index].process_msg_proc;
1538
1539 process_msg_info = &iceConn->process_msg_info[
1540 iceConn->protosetup_to_me->
1541 his_opcode -iceConn->his_min_opcode];
1542
1543 process_msg_info->client_data = clientData;
1544 process_msg_info->accept_flag = 1;
1545 process_msg_info->process_msg_proc.
1546 accept_client = processMsgProc;
1547
1548
1549 /*
1550 * Increase the reference count for the number
1551 * of active protocols.
1552 */
1553
1554 iceConn->proto_ref_count++;
1555
1556
1557 /*
1558 * Notify the client that the protocol is active. The reason
1559 * we have this 2nd callback invoked is because the client
1560 * may wish to immediately generate a message for this
1561 * protocol, but it must wait until we send the Protocol Reply.
1562 */
1563
1564 if (protocolActivateProc)
1565 {
1566 (*protocolActivateProc) (iceConn,
1567 process_msg_info->client_data);
1568 }
1569 }
1570 else
1571 {
1572 /*
1573 * An error was encountered.
1574 */
1575
1576 _IceErrorSetupFailed (iceConn, ICE_ProtocolSetup,
1577 failureReason);
1578
1579 free (failureReason);
1580 }
1581 }
1582
1583
1584 if (free_setup_info)
1585 {
1586 free (iceConn->protosetup_to_me->his_vendor);
1587 free (iceConn->protosetup_to_me->his_release);
1588 free (iceConn->protosetup_to_me);
1589 iceConn->protosetup_to_me = NULL;
1590 }
1591 }
1592 else
1593 {
1594 /*
1595 * Unexpected message
1596 */
1597
1598 _IceErrorBadState (iceConn, 0, ICE_AuthReply, IceCanContinue);
1599 }
1600
1601 if (authData && authDataLen > 0)
1602 free (authData);
1603
1604
1605 free (errorString);
1606
1607 IceDisposeCompleteMessage (iceConn, replyData);
1608 return (0);
1609 }
1610
1611
1612
1613 static Bool
1614 ProcessAuthNextPhase (
1615 IceConn iceConn,
1616 unsigned long length,
1617 Bool swap,
1618 IceReplyWaitInfo *replyWait
1619 )
1620 {
1621 iceAuthNextPhaseMsg *message;
1622 int authDataLen;
1623 IcePointer authData;
1624 int replyDataLen;
1625 IcePointer replyData = NULL;
1626 char *errorString = NULL;
1627 IcePoAuthProc authProc;
1628 IcePoAuthStatus status;
1629 IcePointer *authState;
1630
1631 CHECK_AT_LEAST_SIZE (iceConn, ICE_AuthNextPhase,
1632 length, SIZEOF (iceAuthNextPhaseMsg),
1633 iceConn->connect_to_you ? IceFatalToConnection : IceFatalToProtocol);
1634
1635 IceReadCompleteMessage (iceConn, SIZEOF (iceAuthNextPhaseMsg),
1636 iceAuthNextPhaseMsg, message, authData);
1637
1638 if (!IceValidIO (iceConn))
1639 {
1640 IceDisposeCompleteMessage (iceConn, authData);
1641 return (0);
1642 }
1643
1644 if (swap)
1645 {
1646 message->authDataLength = lswaps (message->authDataLength);
1647 }
1648
1649 CHECK_COMPLETE_SIZE (iceConn, ICE_AuthNextPhase, length,
1650 message->authDataLength + SIZEOF (iceAuthNextPhaseMsg), authData,
1651 iceConn->connect_to_you ? IceFatalToConnection : IceFatalToProtocol);
1652
1653 if (iceConn->connect_to_you)
1654 {
1655 authProc = _IcePoAuthProcs[(int)
1656 (iceConn->connect_to_you->my_auth_index)];
1657
1658 authState = &iceConn->connect_to_you->my_auth_state;
1659 }
1660 else if (iceConn->protosetup_to_you)
1661 {
1662 _IcePoProtocol *myProtocol =
1663 _IceProtocols[iceConn->protosetup_to_you->my_opcode - 1].orig_client;
1664
1665 authProc = myProtocol->auth_procs[(int)
1666 (iceConn->protosetup_to_you->my_auth_index)];
1667
1668 authState = &iceConn->protosetup_to_you->my_auth_state;
1669 }
1670 else
1671 {
1672 /*
1673 * Unexpected message
1674 */
1675
1676 _IceErrorBadState (iceConn, 0, ICE_AuthNextPhase, IceCanContinue);
1677
1678 IceDisposeCompleteMessage (iceConn, authData);
1679 return (0);
1680 }
1681
1682 authDataLen = message->authDataLength;
1683
1684 status = (*authProc) (iceConn, authState, False /* don't clean up */,
1685 swap, authDataLen, authData, &replyDataLen, &replyData, &errorString);
1686
1687 if (status == IcePoAuthHaveReply)
1688 {
1689 AuthReply (iceConn, replyDataLen, replyData);
1690
1691 replyWait->sequence_of_request = iceConn->send_sequence;
1692 }
1693 else if (status == IcePoAuthRejected || status == IcePoAuthFailed)
1694 {
1695 const char *prefix = NULL;
1696 char *returnErrorString;
1697
1698 if (status == IcePoAuthRejected)
1699 {
1700 _IceErrorAuthenticationRejected (iceConn,
1701 ICE_AuthNextPhase, errorString);
1702
1703 prefix = "Authentication Rejected, reason : ";
1704 }
1705 else if (status == IcePoAuthFailed)
1706 {
1707 _IceErrorAuthenticationFailed (iceConn,
1708 ICE_AuthNextPhase, errorString);
1709
1710 prefix = "Authentication Failed, reason : ";
1711 }
1712
1713 if (asprintf (&returnErrorString, "%s%s", prefix, errorString) == -1)
1714 returnErrorString = strdup("");
1715 free (errorString);
1716
1717 if (iceConn->connect_to_you)
1718 {
1719 _IceConnectionError *errorReply =
1720 &(((_IceReply *) (replyWait->reply))->connection_error);
1721
1722 errorReply->type = ICE_CONNECTION_ERROR;
1723 errorReply->error_message = returnErrorString;
1724 }
1725 else
1726 {
1727 _IceProtocolError *errorReply =
1728 &(((_IceReply *) (replyWait->reply))->protocol_error);
1729
1730 errorReply->type = ICE_PROTOCOL_ERROR;
1731 errorReply->error_message = returnErrorString;
1732 }
1733 }
1734
1735 if (replyData && replyDataLen > 0)
1736 free (replyData);
1737
1738 IceDisposeCompleteMessage (iceConn, authData);
1739
1740 return (status != IcePoAuthHaveReply);
1741 }
1742
1743
1744
1745 static Bool
1746 ProcessConnectionReply (
1747 IceConn iceConn,
1748 unsigned long length,
1749 Bool swap,
1750 IceReplyWaitInfo *replyWait
1751 )
1752 {
1753 iceConnectionReplyMsg *message;
1754 char *pData, *pStart, *pEnd;
1755 Bool replyReady;
1756
1757 #if 0 /* No-op */
1758 CHECK_AT_LEAST_SIZE (iceConn, ICE_ConnectionReply,
1759 length, SIZEOF (iceConnectionReplyMsg), IceFatalToConnection);
1760 #endif
1761
1762 IceReadCompleteMessage (iceConn, SIZEOF (iceConnectionReplyMsg),
1763 iceConnectionReplyMsg, message, pStart);
1764
1765 if (!IceValidIO (iceConn))
1766 {
1767 IceDisposeCompleteMessage (iceConn, pStart);
1768 return (0);
1769 }
1770
1771 pData = pStart;
1772 pEnd = pStart + (length << 3);
1773
1774 SKIP_STRING (pData, swap, pEnd,
1775 BAIL_STRING (iceConn, ICE_ConnectionReply,
1776 pStart)); /* vendor */
1777 SKIP_STRING (pData, swap, pEnd,
1778 BAIL_STRING (iceConn, ICE_ConnectionReply,
1779 pStart)); /* release */
1780
1781 CHECK_COMPLETE_SIZE (iceConn, ICE_ConnectionReply,
1782 length, pData - pStart + SIZEOF (iceConnectionReplyMsg),
1783 pStart, IceFatalToConnection);
1784
1785 pData = pStart;
1786
1787 if (iceConn->connect_to_you)
1788 {
1789 if (iceConn->connect_to_you->auth_active)
1790 {
1791 /*
1792 * Tell the authentication procedure to clean up.
1793 */
1794
1795 IcePoAuthProc authProc = _IcePoAuthProcs[(int)
1796 (iceConn->connect_to_you->my_auth_index)];
1797
1798 (*authProc) (iceConn, &iceConn->connect_to_you->my_auth_state,
1799 True /* clean up */, False /* swap */,
1800 0, NULL, NULL, NULL, NULL);
1801 }
1802
1803 if ((int) message->versionIndex >= _IceVersionCount)
1804 {
1805 _IceConnectionError *errorReply =
1806 &(((_IceReply *) (replyWait->reply))->connection_error);
1807 char errIndex = message->versionIndex;
1808
1809 _IceErrorBadValue (iceConn, 0,
1810 ICE_ConnectionReply, 2, 1, &errIndex);
1811
1812 errorReply->type = ICE_CONNECTION_ERROR;
1813 errorReply->error_message =
1814 strdup("Received bad version index in Connection Reply");
1815 }
1816 else
1817 {
1818 _IceReply *reply = (_IceReply *) (replyWait->reply);
1819
1820 reply->type = ICE_CONNECTION_REPLY;
1821 reply->connection_reply.version_index = message->versionIndex;
1822
1823 EXTRACT_STRING (pData, swap, reply->connection_reply.vendor);
1824 EXTRACT_STRING (pData, swap, reply->connection_reply.release);
1825 }
1826
1827 replyReady = True;
1828 }
1829 else
1830 {
1831 /*
1832 * Unexpected message
1833 */
1834
1835 _IceErrorBadState (iceConn, 0, ICE_ConnectionReply, IceCanContinue);
1836
1837 replyReady = False;
1838 }
1839
1840 IceDisposeCompleteMessage (iceConn, pStart);
1841
1842 return (replyReady);
1843 }
1844
1845
1846
1847 static int
1848 ProcessProtocolSetup (
1849 IceConn iceConn,
1850 unsigned long length,
1851 Bool swap
1852 )
1853 {
1854 iceProtocolSetupMsg *message;
1855 _IcePaProtocol *myProtocol;
1856 int myVersionCount, hisVersionCount;
1857 int myVersionIndex, hisVersionIndex;
1858 int hisMajorVersion, hisMinorVersion;
1859 int myAuthCount, hisAuthCount;
1860 int myOpcode, hisOpcode;
1861 int found, i, j;
1862 char **hisAuthNames = NULL;
1863 char *protocolName;
1864 char *pData, *pStart, *pEnd;
1865 char *vendor = NULL;
1866 char *release = NULL;
1867 int accept_setup_now = 0;
1868 int myAuthIndex = 0;
1869 int hisAuthIndex = 0;
1870 char mustAuthenticate;
1871 int authUsableCount;
1872 int authUsableFlags[MAX_ICE_AUTH_NAMES];
1873 int authIndices[MAX_ICE_AUTH_NAMES];
1874
1875 CHECK_AT_LEAST_SIZE (iceConn, ICE_ProtocolSetup,
1876 length, SIZEOF (iceProtocolSetupMsg), IceFatalToProtocol);
1877
1878 if (iceConn->want_to_close)
1879 {
1880 /*
1881 * If we sent a WantToClose message, but just got a ProtocolSetup,
1882 * we must cancel our WantToClose. It is the responsibility of the
1883 * other client to send a WantToClose later on.
1884 */
1885
1886 iceConn->want_to_close = 0;
1887 }
1888
1889 IceReadCompleteMessage (iceConn, SIZEOF (iceProtocolSetupMsg),
1890 iceProtocolSetupMsg, message, pStart);
1891
1892 if (!IceValidIO (iceConn))
1893 {
1894 IceDisposeCompleteMessage (iceConn, pStart);
1895 return (0);
1896 }
1897
1898 pData = pStart;
1899 pEnd = pStart + (length << 3);
1900
1901 SKIP_STRING (pData, swap, pEnd,
1902 BAIL_STRING(iceConn, ICE_ProtocolSetup,
1903 pStart)); /* proto name */
1904 SKIP_STRING (pData, swap, pEnd,
1905 BAIL_STRING(iceConn, ICE_ProtocolSetup,
1906 pStart)); /* vendor */
1907 SKIP_STRING (pData, swap, pEnd,
1908 BAIL_STRING(iceConn, ICE_ProtocolSetup,
1909 pStart)); /* release */
1910 SKIP_LISTOF_STRING (pData, swap, (int) message->authCount, pEnd,
1911 BAIL_STRING(iceConn, ICE_ProtocolSetup,
1912 pStart)); /* auth names */
1913 pData += (message->versionCount * 4); /* versions */
1914
1915 CHECK_COMPLETE_SIZE (iceConn, ICE_ProtocolSetup,
1916 length, pData - pStart + SIZEOF (iceProtocolSetupMsg),
1917 pStart, IceFatalToProtocol);
1918
1919 mustAuthenticate = message->mustAuthenticate;
1920
1921 if (mustAuthenticate != 0 && mustAuthenticate != 1)
1922 {
1923 _IceErrorBadValue (iceConn, 0,
1924 ICE_ProtocolSetup, 4, 1, &mustAuthenticate);
1925 IceDisposeCompleteMessage (iceConn, pStart);
1926 return (0);
1927 }
1928
1929 pData = pStart;
1930
1931 if (iceConn->process_msg_info &&
1932 (int) message->protocolOpcode >= iceConn->his_min_opcode &&
1933 (int) message->protocolOpcode <= iceConn->his_max_opcode &&
1934 iceConn->process_msg_info[
1935 message->protocolOpcode - iceConn->his_min_opcode].in_use)
1936 {
1937 _IceErrorMajorOpcodeDuplicate (iceConn, message->protocolOpcode);
1938 IceDisposeCompleteMessage (iceConn, pStart);
1939 return (0);
1940 }
1941
1942 EXTRACT_STRING (pData, swap, protocolName);
1943
1944 if (iceConn->process_msg_info)
1945 {
1946 for (i = 0;
1947 i <= (iceConn->his_max_opcode - iceConn->his_min_opcode); i++)
1948 {
1949 if (iceConn->process_msg_info[i].in_use && strcmp (protocolName,
1950 iceConn->process_msg_info[i].protocol->protocol_name) == 0)
1951 {
1952 _IceErrorProtocolDuplicate (iceConn, protocolName);
1953 free (protocolName);
1954 IceDisposeCompleteMessage (iceConn, pStart);
1955 return (0);
1956 }
1957 }
1958 }
1959
1960 for (i = 0; i < _IceLastMajorOpcode; i++)
1961 if (strcmp (protocolName, _IceProtocols[i].protocol_name) == 0)
1962 break;
1963
1964 if (i < _IceLastMajorOpcode &&
1965 (myProtocol = _IceProtocols[i].accept_client) != NULL)
1966 {
1967 hisOpcode = message->protocolOpcode;
1968 myOpcode = i + 1;
1969 free (protocolName);
1970 }
1971 else
1972 {
1973 _IceErrorUnknownProtocol (iceConn, protocolName);
1974 free (protocolName);
1975 IceDisposeCompleteMessage (iceConn, pStart);
1976 return (0);
1977 }
1978
1979 EXTRACT_STRING (pData, swap, vendor);
1980 EXTRACT_STRING (pData, swap, release);
1981
1982 hisAuthCount = message->authCount;
1983 if (hisAuthCount > 0)
1984 {
1985 hisAuthNames = malloc (hisAuthCount * sizeof (char *));
1986 EXTRACT_LISTOF_STRING (pData, swap, hisAuthCount, hisAuthNames);
1987 }
1988
1989 hisVersionCount = message->versionCount;
1990 myVersionCount = myProtocol->version_count;
1991
1992 hisVersionIndex = myVersionIndex = found = 0;
1993
1994 for (i = 0; i < hisVersionCount && !found; i++)
1995 {
1996 EXTRACT_CARD16 (pData, swap, hisMajorVersion);
1997 EXTRACT_CARD16 (pData, swap, hisMinorVersion);
1998
1999 for (j = 0; j < myVersionCount && !found; j++)
2000 {
2001 if (myProtocol->version_recs[j].major_version == hisMajorVersion &&
2002 myProtocol->version_recs[j].minor_version == hisMinorVersion)
2003 {
2004 hisVersionIndex = i;
2005 myVersionIndex = j;
2006 found = 1;
2007 }
2008 }
2009 }
2010
2011 if (!found)
2012 {
2013 _IceErrorNoVersion (iceConn, ICE_ProtocolSetup);
2014
2015 free (vendor);
2016 free (release);
2017
2018 if (hisAuthCount > 0)
2019 {
2020 for (i = 0; i < hisAuthCount; i++)
2021 free (hisAuthNames[i]);
2022
2023 free (hisAuthNames);
2024 }
2025
2026 IceDisposeCompleteMessage (iceConn, pStart);
2027 return (0);
2028 }
2029
2030 myAuthCount = myProtocol->auth_count;
2031
2032 _IceGetPaValidAuthIndices (
2033 _IceProtocols[myOpcode - 1].protocol_name,
2034 iceConn->connection_string, myAuthCount,
2035 (const char **) myProtocol->auth_names,
2036 &authUsableCount, authIndices);
2037
2038 for (i = 0; i < myAuthCount; i++)
2039 {
2040 authUsableFlags[i] = 0;
2041 for (j = 0; j < authUsableCount && !authUsableFlags[i]; j++)
2042 authUsableFlags[i] = (authIndices[j] == i);
2043 }
2044
2045 for (i = found = 0; i < myAuthCount && !found; i++)
2046 {
2047 if (authUsableFlags[i])
2048 {
2049 const char *myAuthName = myProtocol->auth_names[i];
2050
2051 for (j = 0; j < hisAuthCount && !found; j++)
2052 if (strcmp (myAuthName, hisAuthNames[j]) == 0)
2053 {
2054 myAuthIndex = i;
2055 hisAuthIndex = j;
2056 found = 1;
2057 }
2058 }
2059 }
2060
2061 if (!found)
2062 {
2063 /*
2064 * None of the authentication methods specified by the
2065 * other client is supported. If the other client requires
2066 * authentication, we must reject the Protocol Setup now.
2067 * Otherwise, we can invoke the host-based authentication callback
2068 * to see if we can accept this Protocol Setup.
2069 */
2070
2071 if (mustAuthenticate || !myProtocol->host_based_auth_proc)
2072 {
2073 _IceErrorNoAuthentication (iceConn, ICE_ProtocolSetup);
2074 }
2075 else
2076 {
2077 char *hostname = _IceGetPeerName (iceConn);
2078
2079 if ((*myProtocol->host_based_auth_proc) (hostname))
2080 {
2081 accept_setup_now = 1;
2082 }
2083 else
2084 {
2085 _IceErrorAuthenticationRejected (iceConn,
2086 ICE_ProtocolSetup, "None of the authentication protocols specified are supported and host-based authentication failed");
2087 }
2088
2089 free (hostname);
2090 }
2091 }
2092 else
2093 {
2094 IcePaAuthStatus status;
2095 int authDataLen;
2096 IcePointer authData = NULL;
2097 IcePointer authState;
2098 char *errorString = NULL;
2099 IcePaAuthProc authProc =
2100 myProtocol->auth_procs[myAuthIndex];
2101
2102 authState = NULL;
2103
2104 status = (*authProc) (iceConn, &authState, swap, 0, NULL,
2105 &authDataLen, &authData, &errorString);
2106
2107 if (status == IcePaAuthContinue)
2108 {
2109 _IceProtoSetupToMeInfo *setupInfo;
2110
2111 AuthRequired (iceConn, hisAuthIndex, authDataLen, authData);
2112
2113 iceConn->protosetup_to_me = setupInfo =
2114 malloc (sizeof (_IceProtoSetupToMeInfo));
2115
2116 setupInfo->his_opcode = hisOpcode;
2117 setupInfo->my_opcode = myOpcode;
2118 setupInfo->my_version_index = myVersionIndex;
2119 setupInfo->his_version_index = hisVersionIndex;
2120 setupInfo->his_vendor = vendor;
2121 setupInfo->his_release = release;
2122 vendor = release = NULL; /* so we don't free it */
2123 setupInfo->my_auth_index = myAuthIndex;
2124 setupInfo->my_auth_state = authState;
2125 setupInfo->must_authenticate = mustAuthenticate;
2126 }
2127 else if (status == IcePaAuthAccepted)
2128 {
2129 accept_setup_now = 1;
2130 }
2131
2132 if (authData && authDataLen > 0)
2133 free (authData);
2134
2135
2136 free (errorString);
2137 }
2138
2139 if (accept_setup_now)
2140 {
2141 IcePaProcessMsgProc processMsgProc;
2142 IceProtocolSetupProc protocolSetupProc;
2143 IceProtocolActivateProc protocolActivateProc;
2144 _IceProcessMsgInfo *process_msg_info;
2145 IcePointer clientData = NULL;
2146 char *failureReason = NULL;
2147 Status status = 1;
2148
2149 protocolSetupProc = myProtocol->protocol_setup_proc;
2150 protocolActivateProc = myProtocol->protocol_activate_proc;
2151
2152 if (protocolSetupProc)
2153 {
2154 /*
2155 * Notify the client of the Protocol Setup.
2156 */
2157
2158 status = (*protocolSetupProc) (iceConn,
2159 myProtocol->version_recs[myVersionIndex].major_version,
2160 myProtocol->version_recs[myVersionIndex].minor_version,
2161 vendor, release, &clientData, &failureReason);
2162
2163 vendor = release = NULL; /* so we don't free it */
2164 }
2165
2166 if (status != 0)
2167 {
2168 /*
2169 * Send the Protocol Reply
2170 */
2171
2172 AcceptProtocol (iceConn, hisOpcode, myOpcode, hisVersionIndex,
2173 myProtocol->vendor, myProtocol->release);
2174
2175
2176 /*
2177 * Set info for this protocol.
2178 */
2179
2180 processMsgProc = myProtocol->version_recs[
2181 myVersionIndex].process_msg_proc;
2182
2183 process_msg_info = &iceConn->process_msg_info[hisOpcode -
2184 iceConn->his_min_opcode];
2185
2186 process_msg_info->client_data = clientData;
2187 process_msg_info->accept_flag = 1;
2188 process_msg_info->process_msg_proc.accept_client = processMsgProc;
2189
2190
2191 /*
2192 * Increase the reference count for the number of active protocols.
2193 */
2194
2195 iceConn->proto_ref_count++;
2196
2197
2198 /*
2199 * Notify the client that the protocol is active. The reason
2200 * we have this 2nd callback invoked is because the client
2201 * may wish to immediately generate a message for this
2202 * protocol, but it must wait until we send the Protocol Reply.
2203 */
2204
2205 if (protocolActivateProc)
2206 {
2207 (*protocolActivateProc) (iceConn,
2208 process_msg_info->client_data);
2209 }
2210 }
2211 else
2212 {
2213 /*
2214 * An error was encountered.
2215 */
2216
2217 _IceErrorSetupFailed (iceConn, ICE_ProtocolSetup, failureReason);
2218
2219 free (failureReason);
2220 }
2221 }
2222
2223
2224 free (vendor);
2225 free (release);
2226
2227 if (hisAuthCount > 0)
2228 {
2229 for (i = 0; i < hisAuthCount; i++)
2230 free (hisAuthNames[i]);
2231
2232 free (hisAuthNames);
2233 }
2234
2235 IceDisposeCompleteMessage (iceConn, pStart);
2236 return (0);
2237 }
2238
2239
2240
2241 static Bool
2242 ProcessProtocolReply (
2243 IceConn iceConn,
2244 unsigned long length,
2245 Bool swap,
2246 IceReplyWaitInfo *replyWait
2247 )
2248 {
2249 iceProtocolReplyMsg *message;
2250 char *pData, *pStart, *pEnd;
2251 Bool replyReady;
2252
2253 #if 0 /* No-op */
2254 CHECK_AT_LEAST_SIZE (iceConn, ICE_ProtocolReply,
2255 length, SIZEOF (iceProtocolReplyMsg), IceFatalToProtocol);
2256 #endif
2257
2258 IceReadCompleteMessage (iceConn, SIZEOF (iceProtocolReplyMsg),
2259 iceProtocolReplyMsg, message, pStart);
2260
2261 if (!IceValidIO (iceConn))
2262 {
2263 IceDisposeCompleteMessage (iceConn, pStart);
2264 return (0);
2265 }
2266
2267 pData = pStart;
2268 pEnd = pStart + (length << 3);
2269
2270 SKIP_STRING (pData, swap, pEnd,
2271 BAIL_STRING(iceConn, ICE_ProtocolReply,
2272 pStart)); /* vendor */
2273 SKIP_STRING (pData, swap, pEnd,
2274 BAIL_STRING(iceConn, ICE_ProtocolReply,
2275 pStart)); /* release */
2276
2277 CHECK_COMPLETE_SIZE (iceConn, ICE_ProtocolReply,
2278 length, pData - pStart + SIZEOF (iceProtocolReplyMsg),
2279 pStart, IceFatalToProtocol);
2280
2281 pData = pStart;
2282
2283 if (iceConn->protosetup_to_you)
2284 {
2285 if (iceConn->protosetup_to_you->auth_active)
2286 {
2287 /*
2288 * Tell the authentication procedure to clean up.
2289 */
2290
2291 _IcePoProtocol *myProtocol = _IceProtocols[
2292 iceConn->protosetup_to_you->my_opcode - 1].orig_client;
2293
2294 IcePoAuthProc authProc = myProtocol->auth_procs[(int)
2295 (iceConn->protosetup_to_you->my_auth_index)];
2296
2297 (*authProc) (iceConn,
2298 &iceConn->protosetup_to_you->my_auth_state,
2299 True /* clean up */, False /* swap */,
2300 0, NULL, NULL, NULL, NULL);
2301 }
2302
2303 if ((int) message->versionIndex >= _IceVersionCount)
2304 {
2305 _IceProtocolError *errorReply =
2306 &(((_IceReply *) (replyWait->reply))->protocol_error);
2307 char errIndex = message->versionIndex;
2308
2309 _IceErrorBadValue (iceConn, 0,
2310 ICE_ProtocolReply, 2, 1, &errIndex);
2311
2312 errorReply->type = ICE_PROTOCOL_ERROR;
2313 errorReply->error_message =
2314 strdup("Received bad version index in Protocol Reply");
2315 }
2316 else
2317 {
2318 _IceProtocolReply *reply =
2319 &(((_IceReply *) (replyWait->reply))->protocol_reply);
2320
2321 reply->type = ICE_PROTOCOL_REPLY;
2322 reply->major_opcode = message->protocolOpcode;
2323 reply->version_index = message->versionIndex;
2324
2325 EXTRACT_STRING (pData, swap, reply->vendor);
2326 EXTRACT_STRING (pData, swap, reply->release);
2327 }
2328
2329 replyReady = True;
2330 }
2331 else
2332 {
2333 _IceErrorBadState (iceConn, 0, ICE_ProtocolReply, IceCanContinue);
2334
2335 replyReady = False;
2336 }
2337
2338 IceDisposeCompleteMessage (iceConn, pStart);
2339
2340 return (replyReady);
2341 }
2342
2343
2344
2345 static int
2346 ProcessPing (
2347 IceConn iceConn,
2348 unsigned long length
2349 )
2350 {
2351 CHECK_SIZE_MATCH (iceConn, ICE_Ping,
2352 length, SIZEOF (icePingMsg), IceFatalToConnection, 0);
2353
2354 PingReply (iceConn);
2355
2356 return (0);
2357 }
2358
2359
2360
2361 static int
2362 ProcessPingReply (
2363 IceConn iceConn,
2364 unsigned long length
2365 )
2366 {
2367 CHECK_SIZE_MATCH (iceConn, ICE_PingReply,
2368 length, SIZEOF (icePingReplyMsg), IceFatalToConnection, 0);
2369
2370 if (iceConn->ping_waits)
2371 {
2372 _IcePingWait *next = iceConn->ping_waits->next;
2373
2374 (*iceConn->ping_waits->ping_reply_proc) (iceConn,
2375 iceConn->ping_waits->client_data);
2376
2377 free (iceConn->ping_waits);
2378 iceConn->ping_waits = next;
2379 }
2380 else
2381 {
2382 _IceErrorBadState (iceConn, 0, ICE_PingReply, IceCanContinue);
2383 }
2384
2385 return (0);
2386 }
2387
2388
2389
2390 static int
2391 ProcessWantToClose (
2392 IceConn iceConn,
2393 unsigned long length,
2394 Bool *connectionClosedRet
2395 )
2396 {
2397 *connectionClosedRet = False;
2398
2399 CHECK_SIZE_MATCH (iceConn, ICE_WantToClose,
2400 length, SIZEOF (iceWantToCloseMsg), IceFatalToConnection, 0);
2401
2402 if (iceConn->want_to_close || iceConn->open_ref_count == 0)
2403 {
2404 /*
2405 * We just received a WantToClose. Either we also sent a
2406 * WantToClose, so we close the connection, or the iceConn
2407 * is not being used, so we close the connection. This
2408 * second case is possible if we sent a WantToClose because
2409 * the iceConn->open_ref_count reached zero, but then we
2410 * received a NoClose.
2411 */
2412
2413 _IceConnectionClosed (iceConn); /* invoke watch procs */
2414 _IceFreeConnection (iceConn);
2415 *connectionClosedRet = True;
2416 }
2417 else if (iceConn->proto_ref_count > 0)
2418 {
2419 /*
2420 * We haven't shut down all of our protocols yet. We send a NoClose,
2421 * and it's up to us to generate a WantToClose later on.
2422 */
2423
2424 IceSimpleMessage (iceConn, 0, ICE_NoClose);
2425 IceFlush (iceConn);
2426 }
2427 else
2428 {
2429 /*
2430 * The reference count on this iceConn is zero. This means that
2431 * there are no active protocols, but the client didn't explicitly
2432 * close the connection yet. If we didn't just send a Protocol Setup,
2433 * we send a NoClose, and it's up to us to generate a WantToClose
2434 * later on.
2435 */
2436
2437 if (!iceConn->protosetup_to_you)
2438 {
2439 IceSimpleMessage (iceConn, 0, ICE_NoClose);
2440 IceFlush (iceConn);
2441 }
2442 }
2443
2444 return (0);
2445 }
2446
2447
2448
2449 static int
2450 ProcessNoClose (
2451 IceConn iceConn,
2452 unsigned long length
2453 )
2454 {
2455 CHECK_SIZE_MATCH (iceConn, ICE_NoClose,
2456 length, SIZEOF (iceNoCloseMsg), IceFatalToConnection, 0);
2457
2458 if (iceConn->want_to_close)
2459 {
2460 /*
2461 * The other side can't close now. We cancel our WantToClose,
2462 * and we can expect a WantToClose from the other side.
2463 */
2464
2465 iceConn->want_to_close = 0;
2466 }
2467 else
2468 {
2469 _IceErrorBadState (iceConn, 0, ICE_NoClose, IceCanContinue);
2470 }
2471
2472 return (0);
2473 }
2474
2475
2476
2477 static void
2478 _IceProcessCoreMessage (
2479 IceConn iceConn,
2480 int opcode,
2481 unsigned long length,
2482 Bool swap,
2483 IceReplyWaitInfo *replyWait,
2484 Bool *replyReadyRet,
2485 Bool *connectionClosedRet
2486 )
2487 {
2488 Bool replyReady = False;
2489
2490 *connectionClosedRet = False;
2491
2492 switch (opcode)
2493 {
2494 case ICE_Error:
2495
2496 replyReady = ProcessError (iceConn, length, swap, replyWait);
2497 break;
2498
2499 case ICE_ConnectionSetup:
2500
2501 ProcessConnectionSetup (iceConn, length, swap);
2502 break;
2503
2504 case ICE_AuthRequired:
2505
2506 replyReady = ProcessAuthRequired (iceConn, length, swap, replyWait);
2507 break;
2508
2509 case ICE_AuthReply:
2510
2511 ProcessAuthReply (iceConn, length, swap);
2512 break;
2513
2514 case ICE_AuthNextPhase:
2515
2516 replyReady = ProcessAuthNextPhase (iceConn, length, swap, replyWait);
2517 break;
2518
2519 case ICE_ConnectionReply:
2520
2521 replyReady = ProcessConnectionReply (iceConn, length, swap, replyWait);
2522 break;
2523
2524 case ICE_ProtocolSetup:
2525
2526 ProcessProtocolSetup (iceConn, length, swap);
2527 break;
2528
2529 case ICE_ProtocolReply:
2530
2531 replyReady = ProcessProtocolReply (iceConn, length, swap, replyWait);
2532 break;
2533
2534 case ICE_Ping:
2535
2536 ProcessPing (iceConn, length);
2537 break;
2538
2539 case ICE_PingReply:
2540
2541 ProcessPingReply (iceConn, length);
2542 break;
2543
2544 case ICE_WantToClose:
2545
2546 ProcessWantToClose (iceConn, length, connectionClosedRet);
2547 break;
2548
2549 case ICE_NoClose:
2550
2551 ProcessNoClose (iceConn, length);
2552 break;
2553
2554 default:
2555
2556 _IceErrorBadMinor (iceConn, 0, opcode, IceCanContinue);
2557 _IceReadSkip (iceConn, length << 3);
2558 break;
2559 }
2560
2561 if (replyWait)
2562 *replyReadyRet = replyReady;
2563 }
2564
2565 const int _IceVersionCount = 1;
2566 const _IceVersion _IceVersions[] = {
2567 {IceProtoMajor, IceProtoMinor, _IceProcessCoreMessage}};