︙ | | |
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
-
+
-
-
|
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
[connection connect];
[self performSelector: @selector(didConnect)
onThread: sourceThread
waitUntilDone: NO];
waitUntilDone: false];
[pool release];
return nil;
}
@end
@implementation XMPPConnection
+ connection
{
return [[[self alloc] init] autorelease];
}
- init
{
self = [super init];
@try {
_port = 5222;
_encrypted = NO;
_streamOpen = NO;
_delegates = [[XMPPMulticastDelegate alloc] init];
_callbacks = [[OFMutableDictionary alloc] init];
} @catch (id e) {
[self release];
@throw e;
}
|
︙ | | |
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
-
+
-
+
-
+
-
+
|
- (OFString*)password
{
return [[_password copy] autorelease];
}
- (void)setPrivateKeyFile: (OFString*)privateKeyFile
{
OF_SETTER(_privateKeyFile, privateKeyFile, YES, YES)
OF_SETTER(_privateKeyFile, privateKeyFile, true, 1)
}
- (OFString*)privateKeyFile
{
OF_GETTER(_privateKeyFile, YES)
OF_GETTER(_privateKeyFile, true)
}
- (void)setCertificateFile: (OFString*)certificateFile
{
OF_SETTER(_certificateFile, certificateFile, YES, YES)
OF_SETTER(_certificateFile, certificateFile, true, 1)
}
- (OFString*)certificateFile
{
OF_GETTER(_certificateFile, YES)
OF_GETTER(_certificateFile, true)
}
- (void)connect
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
XMPPSRVEntry *candidate = nil;
XMPPSRVLookup *SRVLookup = nil;
|
︙ | | |
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
|
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
|
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
|
[[[[XMPPConnection_ConnectThread alloc]
initWithSourceThread: [OFThread currentThread]
connection: self] autorelease] start];
[pool release];
}
- (BOOL)XMPP_parseBuffer: (const void*)buffer
- (bool)XMPP_parseBuffer: (const void*)buffer
length: (size_t)length
{
if ([_socket isAtEndOfStream]) {
[_delegates broadcastSelector: @selector(connectionWasClosed:)
withObject: self];
return NO;
return false;
}
@try {
[_parser parseBuffer: buffer
length: length];
} @catch (OFMalformedXMLException *e) {
[self XMPP_sendStreamError: @"bad-format"
text: nil];
[self close];
return NO;
return false;
}
return YES;
return true;
}
- (void)parseBuffer: (const void*)buffer
length: (size_t)length
{
[self XMPP_parseBuffer: buffer
length: length];
[_oldParser release];
[_oldElementBuilder release];
_oldParser = nil;
_oldElementBuilder = nil;
}
- (BOOL)stream: (OFStream*)stream
- (bool)stream: (OFStream*)stream
didReadIntoBuffer: (char*)buffer
length: (size_t)length
exception: (OFException*)exception
{
if (exception != nil) {
[_delegates broadcastSelector: @selector(connection:
didThrowException:)
withObject: self
withObject: exception];
[self close];
return NO;
return false;
}
@try {
if (![self XMPP_parseBuffer: buffer
length: length])
return NO;
return false;
} @catch (id e) {
[_delegates broadcastSelector: @selector(connection:
didThrowException:)
withObject: self
withObject: e];
[self close];
return NO;
return false;
}
if (_oldParser != nil || _oldElementBuilder != nil) {
[_oldParser release];
[_oldElementBuilder release];
_oldParser = nil;
_oldElementBuilder = nil;
[_socket asyncReadIntoBuffer: buffer
length: BUFFER_LENGTH
target: self
selector: @selector(stream:
didReadIntoBuffer:length:
exception:)];
return NO;
return false;
}
return YES;
return true;
}
- (OFTCPSocket*)socket
{
return [[_socket retain] autorelease];
}
- (BOOL)encryptionRequired
- (bool)encryptionRequired
{
return _encryptionRequired;
}
- (void)setEncryptionRequired: (BOOL)encryptionRequired
- (void)setEncryptionRequired: (bool)encryptionRequired
{
_encryptionRequired = encryptionRequired;
}
- (BOOL)encrypted
- (bool)encrypted
{
return _encrypted;
}
- (BOOL)streamOpen
- (bool)streamOpen
{
return _streamOpen;
}
- (BOOL)supportsRosterVersioning
- (bool)supportsRosterVersioning
{
return _supportsRosterVersioning;
}
- (BOOL)supportsStreamManagement
- (bool)supportsStreamManagement
{
return _supportsStreamManagement;
}
- (BOOL)checkCertificateAndGetReason: (OFString**)reason
- (bool)checkCertificateAndGetReason: (OFString**)reason
{
X509Certificate *cert;
OFDictionary *SANs;
BOOL serviceSpecific = NO;
bool serviceSpecific = false;
@try {
[_socket verifyPeerCertificate];
} @catch (SSLInvalidCertificateException *e) {
if (reason != NULL)
*reason = [[[e reason] copy] autorelease];
return NO;
return false;
}
cert = [_socket peerCertificate];
SANs = [cert subjectAlternativeName];
if ([[SANs objectForKey: @"otherName"]
objectForKey: OID_SRVName] != nil ||
[SANs objectForKey: @"dNSName"] != nil ||
[SANs objectForKey: @"uniformResourceIdentifier"] != nil)
serviceSpecific = YES;
serviceSpecific = true;
if ([cert hasSRVNameMatchingDomain: _domainToASCII
service: @"xmpp-client"] ||
[cert hasDNSNameMatchingDomain: _domainToASCII])
return YES;
return true;
if (!serviceSpecific &&
[cert hasCommonNameMatchingDomain: _domainToASCII])
return YES;
return true;
return NO;
return false;
}
- (void)sendStanza: (OFXMLElement*)element
{
[_delegates broadcastSelector: @selector(connection:didSendElement:)
withObject: self
withObject: element];
|
︙ | | |
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
|
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
|
-
+
-
-
+
+
|
[_socket writeFormat: @"<?xml version='1.0'?>\n"
@"<stream:stream to='%@' "
@"xmlns='" XMPP_NS_CLIENT @"' "
@"xmlns:stream='" XMPP_NS_STREAM @"' %@"
@"version='1.0'>", _domain, langString];
_streamOpen = YES;
_streamOpen = true;
}
- (void)close
{
if (_streamOpen)
[_socket writeString: @"</stream:stream>"];
[_oldParser release];
_oldParser = nil;
[_oldElementBuilder release];
_oldElementBuilder = nil;
[_authModule release];
_authModule = nil;
[_socket release];
_socket = nil;
[_JID release];
_JID = nil;
_streamOpen = _needsSession = _encrypted = NO;
_supportsRosterVersioning = _supportsStreamManagement = NO;
_streamOpen = _needsSession = _encrypted = false;
_supportsRosterVersioning = _supportsStreamManagement = false;
_lastID = 0;
}
- (void)XMPP_handleStanza: (OFXMLElement*)element
{
if ([[element name] isEqual: @"iq"]) {
[self XMPP_handleIQ: [XMPPIQ stanzaWithElement: element]];
|
︙ | | |
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
|
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
|
-
+
|
[newSock setCertificateFile: _certificateFile];
[newSock setPrivateKeyFile: _privateKeyFile];
[newSock setPrivateKeyPassphrase: _privateKeyPassphrase];
[newSock startTLS];
[_socket release];
_socket = newSock;
_encrypted = YES;
_encrypted = true;
[_delegates broadcastSelector: @selector(
connectionDidUpgradeToTLS:)
withObject: self];
/* Stream restart */
[self XMPP_startStream];
|
︙ | | |
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
|
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
|
-
+
|
}
assert(0);
}
- (void)XMPP_handleIQ: (XMPPIQ*)iq
{
BOOL handled = NO;
bool handled = false;
XMPPCallback *callback;
if ((callback = [_callbacks objectForKey: [iq ID]])) {
[callback runWithIQ: iq
connection: self];
[_callbacks removeObjectForKey: [iq ID]];
return;
|
︙ | | |
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
|
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
|
-
+
-
+
|
if (_encryptionRequired && !_encrypted)
/* TODO: Find/create an exception to throw here */
@throw [OFException exception];
if ([element elementForName: @"ver"
namespace: XMPP_NS_ROSTERVER] != nil)
_supportsRosterVersioning = YES;
_supportsRosterVersioning = true;
if ([element elementForName: @"sm"
namespace: XMPP_NS_SM] != nil)
_supportsStreamManagement = YES;
_supportsStreamManagement = true;
if (mechs != nil) {
OFEnumerator *enumerator;
OFXMLElement *mech;
enumerator = [[mechs children] objectEnumerator];
while ((mech = [enumerator nextObject]) != nil)
|
︙ | | |
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
|
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
|
-
+
-
+
-
+
|
if ([mechanisms containsObject: @"SCRAM-SHA-1-PLUS"]) {
_authModule = [[XMPPSCRAMAuth alloc]
initWithAuthcid: _username
password: _password
connection: self
hash: [OFSHA1Hash class]
plusAvailable: YES];
plusAvailable: true];
[self XMPP_sendAuth: @"SCRAM-SHA-1-PLUS"];
return;
}
if ([mechanisms containsObject: @"SCRAM-SHA-1"]) {
_authModule = [[XMPPSCRAMAuth alloc]
initWithAuthcid: _username
password: _password
connection: self
hash: [OFSHA1Hash class]
plusAvailable: NO];
plusAvailable: false];
[self XMPP_sendAuth: @"SCRAM-SHA-1"];
return;
}
if ([mechanisms containsObject: @"PLAIN"] && _encrypted) {
_authModule = [[XMPPPLAINAuth alloc]
initWithAuthcid: _username
password: _password];
[self XMPP_sendAuth: @"PLAIN"];
return;
}
assert(0);
}
if (session != nil)
_needsSession = YES;
_needsSession = true;
if (bind != nil) {
[self XMPP_sendResourceBind];
return;
}
assert(0);
|
︙ | | |
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
|
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
|
-
+
-
+
|
- (id <XMPPStorage>)dataStorage
{
return _dataStorage;
}
- (void)setLanguage: (OFString*)language
{
OF_SETTER(_language, language, YES, YES)
OF_SETTER(_language, language, true, 1)
}
- (OFString*)language
{
OF_GETTER(_language, YES)
OF_GETTER(_language, true)
}
- (void)addDelegate: (id <XMPPConnectionDelegate>)delegate
{
[_delegates addDelegate: delegate];
}
|
︙ | | |