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
91
92
93
94
95
96
97
98
99
100
101
102
103
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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
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
91
92
93
94
95
96
97
98
99
100
101
102
103
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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
-
-
-
-
+
+
+
+
-
-
-
-
+
+
+
+
-
-
-
+
+
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
-
+
+
-
-
+
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
|
{
Class c = [self class];
[self release];
@throw [OFNotImplementedException exceptionWithClass: c
selector: _cmd];
}
- initWithPriority: (uint16_t)priority_
weight: (uint16_t)weight_
port: (uint16_t)port_
target: (OFString*)target_
- initWithPriority: (uint16_t)priority
weight: (uint16_t)weight
port: (uint16_t)port
target: (OFString*)target
{
self = [super init];
@try {
priority = priority_;
weight = weight_;
port = port_;
target = [target_ copy];
_priority = priority;
_weight = weight;
_port = port;
_target = [target copy];
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- initWithResourceRecord: (ns_rr)resourceRecord
handle: (ns_msg)handle
{
self = [super init];
@try {
const uint16_t *rdata;
char buffer[NS_MAXDNAME];
rdata = (const uint16_t*)(void*)ns_rr_rdata(resourceRecord);
priority = ntohs(rdata[0]);
weight = ntohs(rdata[1]);
port = ntohs(rdata[2]);
_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)
@throw [OFInitializationFailedException
exceptionWithClass: [self class]];
target = [[OFString alloc]
_target = [[OFString alloc]
initWithCString: buffer
encoding: OF_STRING_ENCODING_NATIVE];
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)dealloc
{
[target release];
[_target release];
[super dealloc];
}
- (OFString*)description
{
return [OFString stringWithFormat:
@"<%@ priority: %" PRIu16 @", weight: %" PRIu16 @", target: %@:%"
PRIu16 @">", [self class], priority, weight, target, port];
PRIu16 @">", [self class], _priority, _weight, _target, _port];
}
- (uint16_t)priority
{
return priority;
return _priority;
}
- (uint16_t)weight
{
return weight;
return _weight;
}
- (void)setAccumulatedWeight: (uint32_t)accumulatedWeight_
- (void)setAccumulatedWeight: (uint32_t)accumulatedWeight
{
accumulatedWeight = accumulatedWeight_;
_accumulatedWeight = accumulatedWeight;
}
- (uint32_t)accumulatedWeight
{
return accumulatedWeight;
return _accumulatedWeight;
}
- (uint16_t)port
{
return port;
return _port;
}
- (OFString*)target
{
OF_GETTER(target, YES)
OF_GETTER(_target, YES)
}
@end
@implementation XMPPSRVLookup
+ lookupWithDomain: (OFString*)domain
{
return [[[self alloc] initWithDomain: domain] autorelease];
}
- initWithDomain: (OFString*)domain_
- initWithDomain: (OFString*)domain
{
self = [super init];
@try {
list = [[OFList alloc] init];
domain = [domain_ copy];
_list = [[OFList alloc] init];
_domain = [domain copy];
[self XMPP_lookup];
} @catch (id e) {
[self release];
@throw e;
}
return self;
}
- (void)dealloc
{
[list release];
[domain release];
[_list release];
[_domain release];
[super dealloc];
}
- (OFString*)domain;
{
OF_GETTER(domain, YES)
OF_GETTER(_domain, YES)
}
- (void)XMPP_lookup
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
unsigned char *answer = NULL;
size_t pageSize = [OFSystemInfo pageSize];
OFString *request;
request = [OFString stringWithFormat: @"_xmpp-client._tcp.%@", domain];
request = [OFString stringWithFormat: @"_xmpp-client._tcp.%@", _domain];
@try {
int answerLen, resourceRecordCount, i;
ns_rr resourceRecord;
ns_msg handle;
if (res_ninit(&resState))
if (res_ninit(&_resState))
@throw [OFAddressTranslationFailedException
exceptionWithClass: [self class]
socket: nil
host: domain];
host: _domain];
answer = [self allocMemoryWithSize: pageSize];
answerLen = res_nsearch(&resState,
answerLen = res_nsearch(&_resState,
[request cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
ns_c_in, ns_t_srv, answer, (int)pageSize);
if ((answerLen == -1) && ((h_errno == HOST_NOT_FOUND) ||
(h_errno == NO_DATA)))
return;
if (answerLen < 1 || answerLen > pageSize) {
@throw [OFAddressTranslationFailedException
exceptionWithClass: [self class]
socket: nil
host: domain];
host: _domain];
}
if (ns_initparse(answer, answerLen, &handle))
@throw [OFAddressTranslationFailedException
exceptionWithClass: [self class]
socket: nil
host: domain];
host: _domain];
resourceRecordCount = ns_msg_count(handle, ns_s_an);
for (i = 0; i < resourceRecordCount; i++) {
if (ns_parserr(&handle, ns_s_an, i, &resourceRecord))
continue;
if (ns_rr_type(resourceRecord) != ns_t_srv ||
ns_rr_class(resourceRecord) != ns_c_in)
continue;
[self XMPP_addEntry: [XMPPSRVEntry
entryWithResourceRecord: resourceRecord
handle: handle]];
}
} @finally {
[self freeMemory: answer];
#ifdef HAVE_RES_NDESTROY
res_ndestroy(&resState);
res_ndestroy(&_resState);
#endif
}
[pool release];
}
- (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) {
for (iter = [_list firstListObject]; iter != NULL; iter = iter->next) {
if ([[iter->object firstObject] priority] == [entry priority]) {
/*
* RFC 2782 says those with weight 0 should be at the
* beginning of the list.
*/
if ([entry weight] > 0)
[iter->object appendObject: entry];
|
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
-
-
+
+
-
+
-
+
|
pool = [[OFAutoreleasePool alloc] init];
subList = [OFList list];
[subList appendObject: entry];
if (iter != NULL)
[list insertObject: subList
beforeListObject: iter];
[_list insertObject: subList
beforeListObject: iter];
else
[list appendObject: subList];
[_list appendObject: subList];
[pool release];
}
- (OFEnumerator*)objectEnumerator
{
return [[[XMPPSRVEnumerator alloc] initWithList: list] autorelease];
return [[[XMPPSRVEnumerator alloc] initWithList: _list] autorelease];
}
@end
@implementation XMPPSRVEnumerator
- initWithList: (OFList*)list_
{
self = [super init];
|