ObjXMPP  Check-in [d0ef8af167]

Overview
Comment:Add support for SRV records
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: d0ef8af16747ff9b29f69430591a42b321875b2cb17db860e684f43a6edd2385
User & Date: florob@babelmonkeys.de on 2011-05-24 01:01:47
Other Links: manifest | tags
Context
2011-05-24
10:18
Add files added in last commit to .xcodeproj. check-in: 33e02f170b user: js tags: trunk
01:01
Add support for SRV records check-in: d0ef8af167 user: florob@babelmonkeys.de tags: trunk
2011-05-22
21:50
Add dealloc for XMPPJID check-in: 4854b771a9 user: florob@babelmonkeys.de tags: trunk
Changes

Modified configure.ac from [e73fb2f584] to [7ac5f3f225].

28
29
30
31
32
33
34































35
36
37
38
39
40
41
])
AC_ARG_ENABLE(static, AS_HELP_STRING([--enable-static], [build static library]))
AS_IF([test x"$enable_static" = x"yes" -o x"$enable_shared" = x"no"], [
	AC_PATH_TOOL(AR, ar)
	AC_PROG_RANLIB
	AC_SUBST(OBJXMPP_STATIC_LIB, "libobjxmpp.a")
])
































AC_CHECK_LIB(objopenssl, main, [
	LIBS="$LIBS -lobjopenssl -lcrypto"
], [
	AC_MSG_ERROR(You need ObjOpenSSL installed!)
])








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







28
29
30
31
32
33
34
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
])
AC_ARG_ENABLE(static, AS_HELP_STRING([--enable-static], [build static library]))
AS_IF([test x"$enable_static" = x"yes" -o x"$enable_shared" = x"no"], [
	AC_PATH_TOOL(AR, ar)
	AC_PROG_RANLIB
	AC_SUBST(OBJXMPP_STATIC_LIB, "libobjxmpp.a")
])

# This is an adapted version of what glib does for res_query
# It should recognize the correct library on (at least) Linux,
# NetBSD, FreeBSD, Mac OS X and Haiku
AC_MSG_CHECKING([for res_nsearch])
AC_TRY_LINK([#include <sys/types.h>
	     #include <netinet/in.h>
	     #include <arpa/nameser.h>
	     #include <resolv.h>],
	     [res_nsearch(&_res, "test", 0, 0, (void *)0, 0);],
	     [AC_MSG_RESULT([yes])],
	     [save_libs="$LIBS"
	      LIBS="$LIBS -lresolv"
	      AC_TRY_LINK([#include <sys/types.h>
			   #include <netinet/in.h>
			   #include <arpa/nameser.h>
			   #include <resolv.h>],
			   [res_nsearch(&_res, "test", 0, 0, (void *)0, 0);],
			   [AC_MSG_RESULT([in -lresolv])],
			   [LIBS="$save_libs -lnetwork"
			    AC_TRY_LINK([#include <sys/types.h>
					 #include <netinet/in.h>
					 #include <arpa/nameser.h>
					 #include <resolv.h>],
					[res_nsearch(&_res, "test", 0, 0, (void *)0, 0);],
					[AC_MSG_RESULT([in -lnetwork])],
					[LIBS="$save_libs -lbind"
					 AC_TRY_LINK([#include <resolv.h>],
						     [res_nsearch(&_res, "test", 0, 0, (void *)0, 0);],
						     [AC_MSG_RESULT([in -lbind])],
						     [AC_MSG_ERROR(not found)])])])])

AC_CHECK_LIB(objopenssl, main, [
	LIBS="$LIBS -lobjopenssl -lcrypto"
], [
	AC_MSG_ERROR(You need ObjOpenSSL installed!)
])

Modified src/Makefile from [a2d4863fd2] to [e16ddb2283].

12
13
14
15
16
17
18

19
20
21
22
23
24
25
       XMPPJID.m		\
       XMPPMessage.m		\
       XMPPPLAINAuth.m		\
       XMPPPresence.m		\
       XMPPRoster.m		\
       XMPPRosterItem.m		\
       XMPPSCRAMAuth.m		\

       XMPPStanza.m

INCLUDES = ${SRCS:.m=.h}	\
	   namespaces.h		\
	   ObjXMPP.h

include ../buildsys.mk







>







12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
       XMPPJID.m		\
       XMPPMessage.m		\
       XMPPPLAINAuth.m		\
       XMPPPresence.m		\
       XMPPRoster.m		\
       XMPPRosterItem.m		\
       XMPPSCRAMAuth.m		\
       XMPPSRVEnumerator.m	\
       XMPPStanza.m

INCLUDES = ${SRCS:.m=.h}	\
	   namespaces.h		\
	   ObjXMPP.h

include ../buildsys.mk

Modified src/XMPPConnection.m from [f2b85b75c9] to [ad06d62613].

27
28
29
30
31
32
33

34
35
36
37
38
39
40

#include <stringprep.h>
#include <idna.h>

#import <ObjOpenSSL/SSLSocket.h>

#import "XMPPConnection.h"

#import "XMPPSCRAMAuth.h"
#import "XMPPPLAINAuth.h"
#import "XMPPStanza.h"
#import "XMPPJID.h"
#import "XMPPIQ.h"
#import "XMPPMessage.h"
#import "XMPPPresence.h"







>







27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

#include <stringprep.h>
#include <idna.h>

#import <ObjOpenSSL/SSLSocket.h>

#import "XMPPConnection.h"
#import "XMPPSRVEnumerator.h"
#import "XMPPSCRAMAuth.h"
#import "XMPPPLAINAuth.h"
#import "XMPPStanza.h"
#import "XMPPJID.h"
#import "XMPPIQ.h"
#import "XMPPMessage.h"
#import "XMPPPresence.h"
192
193
194
195
196
197
198



199















200
201
202
203
204
205
206
207
208
{
	return [[password copy] autorelease];
}

- (void)connect
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];



















	[sock connectToHost: server
		     onPort: port];
	[self XMPP_startStream];

	[pool release];
}

- (void)handleConnection
{







>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|







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
{
	return [[password copy] autorelease];
}

- (void)connect
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	XMPPSRVEnumerator *SRVEnumerator =
		[XMPPSRVEnumerator enumeratorWithDomain: server];
	XMPPSRVEntry *candidate;

	while ((candidate = [SRVEnumerator nextObject])) {
		@try {
			[sock connectToHost: [candidate target]
				     onPort: [candidate port]];
			break;
		} @catch (id e) {
			if (([e class] == [OFAddressTranslationFailedException
					class]) || ([e class] ==
					[OFConnectionFailedException class]))
				continue;
			else
				@throw e;
		}
	}
	if (!candidate)
		[sock connectToHost: server
			     onPort: port];
	[self XMPP_startStream];

	[pool release];
}

- (void)handleConnection
{

Added src/XMPPSRVEnumerator.h version [f1ada3cf65].











































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
/*
 * Copyright (c) 2011, Florian Zeitz <florob@babelmonkeys.de>
 *
 * https://webkeks.org/hg/objxmpp/
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <arpa/nameser.h>
#import <ObjFW/ObjFW.h>

@interface XMPPSRVEntry: OFObject
{
	uint16_t priority;
	uint16_t weight;
	uint16_t port;
	OFString *target;
}
#ifdef OF_HAVE_PROPERTIES
@property uint16_t priority;
@property uint16_t weight;
@property uint16_t port;
@property (copy) OFString *target;
#endif

- (void) setPriority: (uint16_t)priority_;
- (uint16_t) priority;
- (void) setWeight: (uint16_t)weight_;
- (uint16_t) weight;
- (void) setPort: (uint16_t)port_;
- (uint16_t) port;
- (void) setTarget: (OFString*)target_;
- (OFString*) target;
@end

@interface XMPPSRVEnumerator: OFEnumerator <OFFastEnumeration>
{
	OFString *domain;
	OFList *priorityList;
}
#ifdef OF_HAVE_PROPERTIES
@property (copy) OFString *domain;
#endif

+ enumeratorWithDomain: (OFString*)domain;

- initWithDomain: (OFString*)domain;
- (void) setDomain: (OFString*)domain;
- (OFString*) domain;
- (void)XMPP_parseSRVRRWithHandle: (const ns_msg)handle
			       RR: (const ns_rr)rr
			   result: (XMPPSRVEntry*)result;
- (void)XMPP_addSRVEntry: (XMPPSRVEntry*)item
    toSortedPriorityList: (OFList*)list;
@end

Added src/XMPPSRVEnumerator.m version [4fe9779194].









































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
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
/*
 * Copyright (c) 2011, Florian Zeitz <florob@babelmonkeys.de>
 *
 * https://webkeks.org/hg/objxmpp/
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice is present in all copies.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <resolv.h>
#include <sys/types.h>
#include <openssl/rand.h>
#include <assert.h>

#import "XMPPSRVEnumerator.h"

@implementation XMPPSRVEntry
- (void)dealloc
{
	[target release];

	[super dealloc];
}

- (OFString*)description
{
	return [OFString stringWithFormat: @"priority: %" PRIu16
				@" weight: %" PRIu16
				@" target: %@:%" PRIu16, priority, weight,
				target, port];
}

- (void)setPriority: (uint16_t)priority_
{
	priority = priority_;
}

- (uint16_t)priority
{
	return priority;
}

- (void)setWeight: (uint16_t)weight_
{
	weight = weight_;
}

- (uint16_t)weight
{
	return weight;
}

- (void)setPort: (uint16_t)port_
{
	port = port_;
}

- (uint16_t)port
{
	return port;
}

- (void)setTarget: (OFString*)target_
{
	OFString *old = target;
	target = [target_ copy];
	[old release];
}

- (OFString*)target
{
	return [[target copy] autorelease];
}
@end

@implementation XMPPSRVEnumerator
+ enumeratorWithDomain: (OFString*)domain_
{
	return [[[self alloc] initWithDomain: domain_] autorelease];
}

- initWithDomain: (OFString*)domain_
{
	self = [super init];
	priorityList = [[OFList alloc] init];
	[self setDomain: domain_];

	return self;
}

- (void)dealloc
{
	[priorityList release];
	[domain release];

	[super dealloc];
}

- (void)setDomain: (OFString*)domain_
{
	OFString *old = domain;
	domain = [domain_ copy];
	[old release];
	[self reset];
}

- (OFString*)domain;
{
	return [[domain copy] autorelease];
}

- (void)XMPP_parseSRVRRWithHandle: (const ns_msg)handle
			       RR: (const ns_rr)rr
			   result: (XMPPSRVEntry*)result
{
	const uint16_t *rdata = (uint16_t*) ns_rr_rdata(rr);
	char target[NS_MAXDNAME];
	[result setPriority: ntohs(rdata[0])];
	[result setWeight: ntohs(rdata[1])];
	[result setPort: ntohs(rdata[2])];
	dn_expand(ns_msg_base(handle), ns_msg_end(handle),
			(uint8_t*) &rdata[3], target, NS_MAXDNAME);
	[result setTarget: [OFString stringWithCString: target]];
}

- (id)nextObject
{
	if ([priorityList firstListObject]) {
		uint16_t weight = 0;
		of_list_object_t *iter;
		XMPPSRVEntry *ret;
		OFList *weightList = [priorityList firstObject];
		uint16_t maximumWeight = [[weightList lastObject] weight];

		if (maximumWeight) {
			RAND_pseudo_bytes((unsigned char *)&weight, 2);
			weight %= maximumWeight;
		}

		iter = [weightList firstListObject];
		while (iter) {
			if (weight <= [iter->object weight]) {
				ret = [iter->object retain];
				[weightList removeListObject: iter];
				if (![weightList firstListObject])
					[priorityList removeListObject:
						[priorityList firstListObject]];
				return [ret autorelease];
			}
			iter = iter->next;
		}
		assert(0);
	}

	return nil;
}

- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state
			   objects: (id*)objects
			     count: (int)count
{
	int len = 0;
	XMPPSRVEntry *entry = [self nextObject];
	state->itemsPtr = objects;
	while ((len < count) && entry) {
		state->mutationsPtr = (unsigned long *)self;
		objects[len++] = entry;
		entry = [self nextObject];
	}
	return len;
}

- (void)reset
{
	int i, rrCount;
	unsigned char *answer;
	OFString *request;
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];

	request = [OFString stringWithFormat: @"_xmpp-client._tcp.%@", domain];
	answer = [self allocMemoryWithSize: NS_MAXMSG];

	res_ninit(&_res);
	if (!(res_nsearch(&_res, [request cString], ns_c_in, ns_t_srv, answer,
				NS_MAXMSG) < 0)) {
		ns_rr rr;
		ns_msg handle;

		ns_initparse(answer, NS_MAXMSG, &handle);
		rrCount = ns_msg_count(handle, ns_s_an);
		for (i = 0; i < rrCount ; i++) {
			XMPPSRVEntry *result = [[XMPPSRVEntry alloc] init];
			ns_parserr(&handle, ns_s_an, i, &rr);
			if ((ns_rr_type(rr) != ns_t_srv)
					|| ns_rr_class(rr) != ns_c_in)
				@throw [OFInvalidServerReplyException
					newWithClass: isa];

			[self XMPP_parseSRVRRWithHandle: handle
						     RR: rr
						 result: result];

			[self XMPP_addSRVEntry: result
			  toSortedPriorityList: priorityList];
		}
	}
	[self freeMemory: answer];
	[pool release];


}

- (void)XMPP_addSRVEntry: (XMPPSRVEntry*)item
    toSortedPriorityList: (OFList*)list
{
	of_list_object_t *priorityIter =
		[list firstListObject];
	while (1) {
		if (priorityIter == NULL ||
				[[priorityIter->object firstObject]
					priority] > [item priority]) {
			OFList *newList = [OFList list];
			[newList appendObject: item];
			if (priorityIter)
				[list insertObject: newList
				  beforeListObject: priorityIter];
			else
				[list appendObject: newList];
			break;
		}
		if ([[priorityIter->object firstObject] priority]
				== [item priority]) {
			if ([item weight] == 0)
				[priorityIter->object prependObject: item];
			else {
				[item setWeight: [item weight] +
				    [[priorityIter->object lastObject] weight]];
				[priorityIter->object appendObject: item];
			}
			break;
		}
		priorityIter = priorityIter->next;
	}
}
@end

Modified tests/test.m from [f856f65516] to [4642dd9fd4].

98
99
100
101
102
103
104
105
106

107
108
109
110
111
112
113
	}

	[conn setServer: [arguments objectAtIndex: 0]];
	[conn setUsername: [arguments objectAtIndex: 1]];
	[conn setPassword: [arguments objectAtIndex: 2]];
	[conn setResource: @"ObjXMPP"];

	[conn connect];
	@try {

		[conn handleConnection];
	} @catch (id e) {
		of_log(@"%@", e);
	}
}

- (void)connectionWasAuthenticated: (XMPPConnection*)conn







<

>







98
99
100
101
102
103
104

105
106
107
108
109
110
111
112
113
	}

	[conn setServer: [arguments objectAtIndex: 0]];
	[conn setUsername: [arguments objectAtIndex: 1]];
	[conn setPassword: [arguments objectAtIndex: 2]];
	[conn setResource: @"ObjXMPP"];


	@try {
		[conn connect];
		[conn handleConnection];
	} @catch (id e) {
		of_log(@"%@", e);
	}
}

- (void)connectionWasAuthenticated: (XMPPConnection*)conn