ObjXMPP  Diff

Differences From Artifact [b57e70e54d]:

To Artifact [e13fa9dc89]:


35
36
37
38
39
40
41









42
43
44
45
46
47
48
49
50
51

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

75
76
77
78
79
80
81

82
83
84
85
86
87
88
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76







77

78
79
80
81
82

83
84
85
86
87
88
89
90







+
+
+
+
+
+
+
+
+









-
+
















-
-
-
-
-
-
-
+
-





-
+







#include <netdb.h>
#include <sys/types.h>
#include <openssl/rand.h>

#import "XMPPSRVLookup.h"

#import <ObjFW/OFLocalization.h>

OF_ASSUME_NONNULL_BEGIN

@interface XMPPSRVLookup ()
- (void)XMPP_lookup;
- (void)XMPP_addEntry: (XMPPSRVEntry *)item;
@end

OF_ASSUME_NONNULL_END

@implementation XMPPSRVEntry
@synthesize priority = _priority, weight = _weight;
@synthesize accumulatedWeight = _accumulatedWeight, port = _port;
@synthesize target = _target;

+ (instancetype)entryWithPriority: (uint16_t)priority
			   weight: (uint16_t)weight
			     port: (uint16_t)port
			   target: (OFString*)target
			   target: (OFString *)target
{
	return [[[self alloc] initWithPriority: priority
					weight: weight
					  port: port
					target: target] autorelease];
}

+ (instancetype)entryWithResourceRecord: (ns_rr)resourceRecord
				 handle: (ns_msg)handle
{
	return [[[self alloc] initWithResourceRecord: resourceRecord
					      handle: handle] autorelease];
}

- init
{
	@try {
		[self doesNotRecognizeSelector: _cmd];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	OF_INVALID_INIT_METHOD
	abort();
}

- initWithPriority: (uint16_t)priority
	    weight: (uint16_t)weight
	      port: (uint16_t)port
	    target: (OFString*)target
	    target: (OFString *)target
{
	self = [super init];

	@try {
		_priority = priority;
		_weight = weight;
		_port = port;
100
101
102
103
104
105
106
107

108
109
110
111
112
113

114
115
116
117
118
119
120
102
103
104
105
106
107
108

109
110
111
112
113
114

115
116
117
118
119
120
121
122







-
+





-
+







{
	self = [super init];

	@try {
		const uint16_t *rdata;
		char buffer[NS_MAXDNAME];

		rdata = (const uint16_t*)(void*)ns_rr_rdata(resourceRecord);
		rdata = (const uint16_t *)(void *)ns_rr_rdata(resourceRecord);
		_priority = ntohs(rdata[0]);
		_weight = ntohs(rdata[1]);
		_port = ntohs(rdata[2]);

		if (dn_expand(ns_msg_base(handle), ns_msg_end(handle),
		    (uint8_t*)&rdata[3], buffer, NS_MAXDNAME) < 1)
		    (uint8_t *)&rdata[3], buffer, NS_MAXDNAME) < 1)
			@throw [OFInitializationFailedException
			    exceptionWithClass: [self class]];

		_target = [[OFString alloc]
		    initWithCString: buffer
			   encoding: [OFLocalization encoding]];
	} @catch (id e) {
128
129
130
131
132
133
134
135

136
137
138
139
140
141
142
143
144
145
146

147
148
149
150





151

152
153
154
155
156
157
158
130
131
132
133
134
135
136

137
138
139
140
141
142
143
144
145
146
147

148
149
150
151
152
153
154
155
156
157

158
159
160
161
162
163
164
165







-
+










-
+




+
+
+
+
+
-
+







- (void)dealloc
{
	[_target release];

	[super dealloc];
}

- (OFString*)description
- (OFString *)description
{
	return [OFString stringWithFormat:
	    @"<%@ priority: %" PRIu16 @", weight: %" PRIu16 @", target: %@:%"
	    PRIu16 @">", [self class], _priority, _weight, _target, _port];
}
@end

@implementation XMPPSRVLookup
@synthesize domain = _domain;

+ (instancetype)lookupWithDomain: (OFString*)domain
+ (instancetype)lookupWithDomain: (OFString *)domain
{
	return [[[self alloc] initWithDomain: domain] autorelease];
}

- init
{
	OF_INVALID_INIT_METHOD
}

- initWithDomain: (OFString*)domain
- initWithDomain: (OFString *)domain
{
	self = [super init];

	@try {
		_list = [[OFList alloc] init];
		_domain = [domain copy];

227
228
229
230
231
232
233
234

235
236
237
238
239
240
241
234
235
236
237
238
239
240

241
242
243
244
245
246
247
248







-
+







		res_ndestroy(&_resState);
#endif
	}

	[pool release];
}

- (void)XMPP_addEntry: (XMPPSRVEntry*)entry
- (void)XMPP_addEntry: (XMPPSRVEntry *)entry
{
	OFAutoreleasePool *pool;
	OFList *subList;
	of_list_object_t *iter;

	/* Look if there already is a list with the priority */
	for (iter = [_list firstListObject]; iter != NULL; iter = iter->next) {
269
270
271
272
273
274
275
276

277
278
279
280
281
282





283

284
285
286
287
288
289
290
276
277
278
279
280
281
282

283
284
285
286
287
288
289
290
291
292
293
294

295
296
297
298
299
300
301
302







-
+






+
+
+
+
+
-
+







		   beforeListObject: iter];
	else
		[_list appendObject: subList];

	[pool release];
}

- (OFEnumerator*)objectEnumerator
- (OFEnumerator *)objectEnumerator
{
	return [[[XMPPSRVEnumerator alloc] initWithList: _list] autorelease];
}
@end

@implementation XMPPSRVEnumerator
- init
{
	OF_INVALID_INIT_METHOD
}

- initWithList: (OFList*)list
- initWithList: (OFList *)list
{
	self = [super init];

	@try {
		_list = [list copy];
	} @catch (id e) {
		[self release];
317
318
319
320
321
322
323
324

325
326
327
328
329
330
331
329
330
331
332
333
334
335

336
337
338
339
340
341
342
343







-
+







		totalWeight += [iter->object weight];
		[iter->object setAccumulatedWeight: totalWeight];
	}

	if ([_subListCopy count] > 0)  {
		uint32_t randomWeight;

		RAND_pseudo_bytes((uint8_t*)&randomWeight, sizeof(uint32_t));
		RAND_pseudo_bytes((uint8_t *)&randomWeight, sizeof(uint32_t));
		randomWeight %= (totalWeight + 1);

		for (iter = [_subListCopy firstListObject]; iter != NULL;
		     iter = iter->next) {
			if ([iter->object accumulatedWeight] >= randomWeight) {
				ret = [[iter->object retain] autorelease];