25
|
1 /*
|
|
2 * This program is free software; you can redistribute it and/or modify
|
|
3 * it under the terms of the GNU General Public License as published by
|
|
4 * the Free Software Foundation; either version 2 of the License, or
|
|
5 * (at your option) any later version.
|
|
6 *
|
|
7 * This program is distributed in the hope that it will be useful,
|
|
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 * GNU General Public License for more details.
|
|
11 *
|
|
12 * You should have received a copy of the GNU General Public License
|
|
13 * along with this program; if not, write to the Free Software
|
|
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
15 *
|
|
16 * Jabber
|
|
17 * Copyright (C) 1998-1999 The Jabber Team http://jabber.org/
|
|
18 */
|
|
19
|
|
20 #include "jabber.h"
|
|
21 #include "connwrap.h"
|
|
22
|
|
23 /* local macros for launching event handlers */
|
|
24 #define STATE_EVT(arg) if(j->on_state) { (j->on_state)(j, (arg) ); }
|
|
25
|
|
26 /* prototypes of the local functions */
|
|
27 static void startElement(void *userdata, const char *name, const char **attribs);
|
|
28 static void endElement(void *userdata, const char *name);
|
|
29 static void charData(void *userdata, const char *s, int slen);
|
|
30
|
|
31 /*
|
|
32 * jab_new -- initialize a new jabber connection
|
|
33 *
|
|
34 * parameters
|
|
35 * user -- jabber id of the user
|
|
36 * pass -- password of the user
|
|
37 *
|
|
38 * results
|
|
39 * a pointer to the connection structure
|
|
40 * or NULL if allocations failed
|
|
41 */
|
|
42 jconn jab_new(char *user, char *pass, int port, int ssl)
|
|
43 {
|
|
44 pool p;
|
|
45 jconn j;
|
|
46
|
|
47 if(!user) return(NULL);
|
|
48
|
|
49 p = pool_new();
|
|
50 if(!p) return(NULL);
|
|
51 j = pmalloc_x(p, sizeof(jconn_struct), 0);
|
|
52 if(!j) return(NULL);
|
|
53 j->p = p;
|
|
54
|
|
55 j->user = jid_new(p, user);
|
|
56 j->pass = pstrdup(p, pass);
|
|
57 j->port = port;
|
|
58
|
|
59 j->state = JCONN_STATE_OFF;
|
|
60 j->cw_state = 0;
|
|
61 j->id = 1;
|
|
62 j->fd = -1;
|
|
63 j->ssl = ssl;
|
|
64
|
|
65 return j;
|
|
66 }
|
|
67
|
|
68 /*
|
|
69 * jab_delete -- free a jabber connection
|
|
70 *
|
|
71 * parameters
|
|
72 * j -- connection
|
|
73 *
|
|
74 */
|
|
75 void jab_delete(jconn j)
|
|
76 {
|
|
77 if(!j) return;
|
|
78
|
|
79 jab_stop(j);
|
|
80 pool_free(j->p);
|
|
81 }
|
|
82
|
|
83 /*
|
|
84 * jab_state_handler -- set callback handler for state change
|
|
85 *
|
|
86 * parameters
|
|
87 * j -- connection
|
|
88 * h -- name of the handler function
|
|
89 */
|
|
90 void jab_state_handler(jconn j, jconn_state_h h)
|
|
91 {
|
|
92 if(!j) return;
|
|
93
|
|
94 j->on_state = h;
|
|
95 }
|
|
96
|
|
97 /*
|
|
98 * jab_packet_handler -- set callback handler for incoming packets
|
|
99 *
|
|
100 * parameters
|
|
101 * j -- connection
|
|
102 * h -- name of the handler function
|
|
103 */
|
|
104 void jab_packet_handler(jconn j, jconn_packet_h h)
|
|
105 {
|
|
106 if(!j) return;
|
|
107
|
|
108 j->on_packet = h;
|
|
109 }
|
|
110
|
|
111 void jab_logger(jconn j, jconn_logger h)
|
|
112 {
|
|
113 if(!j) return;
|
|
114
|
|
115 j->logger = h;
|
|
116 }
|
|
117
|
|
118
|
|
119 /*
|
|
120 * jab_start -- start connection
|
|
121 *
|
|
122 * parameters
|
|
123 * j -- connection
|
|
124 *
|
|
125 */
|
|
126 void jab_start(jconn j)
|
|
127 {
|
|
128 xmlnode x;
|
|
129 char *t,*t2;
|
|
130
|
|
131 if(!j || (j->state != JCONN_STATE_OFF && j->state != JCONN_STATE_CONNECTING) ) return;
|
|
132
|
|
133 if (!(j->cw_state & CW_CONNECT_WANT_SOMETHING)) { /* same as state != JCONN_STATE_CONNECTING */
|
|
134 j->parser = XML_ParserCreate(NULL);
|
|
135 XML_SetUserData(j->parser, (void *)j);
|
|
136 XML_SetElementHandler(j->parser, startElement, endElement);
|
|
137 XML_SetCharacterDataHandler(j->parser, charData);
|
|
138
|
|
139 if (j->cw_state & CW_CONNECT_BLOCKING)
|
|
140 j->fd = make_netsocket(j->port, j->user->server, NETSOCKET_CLIENT, j->ssl);
|
|
141 else
|
|
142 j->fd = make_nb_netsocket(j->port, j->user->server, NETSOCKET_CLIENT, j->ssl, &j->cw_state);
|
|
143
|
|
144 if(j->fd < 0) {
|
|
145 STATE_EVT(JCONN_STATE_OFF);
|
|
146 return;
|
|
147 }
|
|
148 }
|
|
149 else { /* subsequent calls to cw_nb_connect until it finishes negociation */
|
|
150 if (cw_nb_connect(j->fd, 0, 0, j->ssl, &j->cw_state)) {
|
|
151 STATE_EVT(JCONN_STATE_OFF);
|
|
152 return;
|
|
153 }
|
|
154 }
|
|
155 if (j->cw_state & CW_CONNECT_WANT_SOMETHING){ /* check if it finished negociation */
|
|
156 j->state = JCONN_STATE_CONNECTING;
|
|
157 STATE_EVT(JCONN_STATE_CONNECTING);
|
|
158 return;
|
|
159 }
|
|
160 change_socket_to_blocking(j->fd);
|
|
161
|
|
162 j->state = JCONN_STATE_CONNECTED;
|
|
163 STATE_EVT(JCONN_STATE_CONNECTED)
|
|
164
|
|
165 /* start stream */
|
|
166 x = jutil_header(NS_CLIENT, j->user->server);
|
|
167 t = xmlnode2str(x);
|
|
168 /* this is ugly, we can create the string here instead of jutil_header */
|
|
169 /* what do you think about it? -madcat */
|
|
170 t2 = strstr(t,"/>");
|
|
171 *t2++ = '>';
|
|
172 *t2 = '\0';
|
|
173 jab_send_raw(j,"<?xml version='1.0'?>");
|
|
174 jab_send_raw(j,t);
|
|
175 xmlnode_free(x);
|
|
176
|
|
177 j->state = JCONN_STATE_ON;
|
|
178 STATE_EVT(JCONN_STATE_ON)
|
|
179
|
|
180 }
|
|
181
|
|
182 /*
|
|
183 * jab_stop -- stop connection
|
|
184 *
|
|
185 * parameters
|
|
186 * j -- connection
|
|
187 */
|
|
188 void jab_stop(jconn j)
|
|
189 {
|
|
190 if(!j || j->state == JCONN_STATE_OFF) return;
|
|
191
|
|
192 j->state = JCONN_STATE_OFF;
|
|
193 cw_close(j->fd);
|
|
194 j->fd = -1;
|
|
195 XML_ParserFree(j->parser);
|
|
196 }
|
|
197
|
|
198 /*
|
|
199 * jab_getfd -- get file descriptor of connection socket
|
|
200 *
|
|
201 * parameters
|
|
202 * j -- connection
|
|
203 *
|
|
204 * returns
|
|
205 * fd of the socket or -1 if socket was not connected
|
|
206 */
|
|
207 int jab_getfd(jconn j)
|
|
208 {
|
|
209 if(j)
|
|
210 return j->fd;
|
|
211 else
|
|
212 return -1;
|
|
213 }
|
|
214
|
|
215 /*
|
|
216 * jab_getjid -- get jid structure of user
|
|
217 *
|
|
218 * parameters
|
|
219 * j -- connection
|
|
220 */
|
|
221 jid jab_getjid(jconn j)
|
|
222 {
|
|
223 if(j)
|
|
224 return(j->user);
|
|
225 else
|
|
226 return NULL;
|
|
227 }
|
|
228
|
|
229 /* jab_getsid -- get stream id
|
|
230 * This is the id of server's <stream:stream> tag and used for
|
|
231 * digest authorization.
|
|
232 *
|
|
233 * parameters
|
|
234 * j -- connection
|
|
235 */
|
|
236 char *jab_getsid(jconn j)
|
|
237 {
|
|
238 if(j)
|
|
239 return(j->sid);
|
|
240 else
|
|
241 return NULL;
|
|
242 }
|
|
243
|
|
244 /*
|
|
245 * jab_getid -- get a unique id
|
|
246 *
|
|
247 * parameters
|
|
248 * j -- connection
|
|
249 */
|
|
250 char *jab_getid(jconn j)
|
|
251 {
|
|
252 snprintf(j->idbuf, 8, "%d", j->id++);
|
|
253 return &j->idbuf[0];
|
|
254 }
|
|
255
|
|
256 /*
|
|
257 * jab_send -- send xml data
|
|
258 *
|
|
259 * parameters
|
|
260 * j -- connection
|
|
261 * x -- xmlnode structure
|
|
262 */
|
|
263 void jab_send(jconn j, xmlnode x)
|
|
264 {
|
|
265 if (j && j->state != JCONN_STATE_OFF)
|
|
266 {
|
|
267 char *buf = xmlnode2str(x);
|
|
268 if (buf) {
|
|
269 cw_write(j->fd, buf, strlen(buf), j->ssl);
|
|
270 if (j->logger)
|
|
271 (j->logger)(j, 0, buf);
|
|
272 }
|
|
273
|
|
274 #ifdef JDEBUG
|
|
275 printf ("out: %s\n", buf);
|
|
276 #endif
|
|
277 }
|
|
278 }
|
|
279
|
|
280 /*
|
|
281 * jab_send_raw -- send a string
|
|
282 *
|
|
283 * parameters
|
|
284 * j -- connection
|
|
285 * str -- xml string
|
|
286 */
|
|
287 void jab_send_raw(jconn j, const char *str)
|
|
288 {
|
|
289 if (j && j->state != JCONN_STATE_OFF) {
|
|
290 cw_write(j->fd, str, strlen(str), j->ssl);
|
|
291
|
|
292 if (j->logger)
|
|
293 (j->logger)(j, 0, str);
|
|
294 }
|
|
295
|
|
296 #ifdef JDEBUG
|
|
297 printf ("out: %s\n", str);
|
|
298 #endif
|
|
299 }
|
|
300
|
|
301 /*
|
|
302 * jab_recv -- read and parse incoming data
|
|
303 *
|
|
304 * parameters
|
|
305 * j -- connection
|
|
306 */
|
|
307 void jab_recv(jconn j)
|
|
308 {
|
|
309 static char buf[32768];
|
|
310 int len;
|
|
311
|
|
312 if(!j || j->state == JCONN_STATE_OFF)
|
|
313 return;
|
|
314
|
|
315 len = cw_read(j->fd, buf, sizeof(buf)-1, j->ssl);
|
|
316 if(len>0)
|
|
317 {
|
|
318 buf[len] = '\0';
|
|
319
|
|
320 if (j->logger)
|
|
321 (j->logger)(j, 1, buf);
|
|
322
|
|
323 #ifdef JDEBUG
|
|
324 printf (" in: %s\n", buf);
|
|
325 #endif
|
|
326 XML_Parse(j->parser, buf, len, 0);
|
|
327 }
|
|
328 else if(len<=0)
|
|
329 {
|
|
330 STATE_EVT(JCONN_STATE_OFF);
|
|
331 jab_stop(j);
|
|
332 }
|
|
333 }
|
|
334
|
|
335 /*
|
|
336 * jab_poll -- check socket for incoming data
|
|
337 *
|
|
338 * parameters
|
|
339 * j -- connection
|
|
340 * timeout -- poll timeout
|
|
341 */
|
|
342 void jab_poll(jconn j, int timeout)
|
|
343 {
|
|
344 fd_set fds;
|
|
345 struct timeval tv;
|
|
346 int r;
|
|
347
|
|
348 if (!j || j->state == JCONN_STATE_OFF)
|
|
349 return;
|
|
350
|
|
351 FD_ZERO(&fds);
|
|
352 FD_SET(j->fd, &fds);
|
|
353
|
|
354 if(timeout <= 0) {
|
|
355 r = select(j->fd + 1, &fds, NULL, NULL, NULL);
|
|
356
|
|
357 } else {
|
|
358 tv.tv_sec = 0;
|
|
359 tv.tv_usec = timeout;
|
|
360 r = select(j->fd + 1, &fds, NULL, NULL, &tv);
|
|
361
|
|
362 }
|
|
363
|
|
364 if(r > 0) {
|
|
365 jab_recv(j);
|
|
366
|
|
367 } else if(r) {
|
171
|
368 /* Don't disconnect for interrupted system call */
|
|
369 if(errno == EINTR) return;
|
|
370
|
25
|
371 STATE_EVT(JCONN_STATE_OFF);
|
|
372 jab_stop(j);
|
|
373
|
|
374 }
|
|
375 }
|
|
376
|
|
377 /*
|
|
378 * jab_auth -- authorize user
|
|
379 *
|
|
380 * parameters
|
|
381 * j -- connection
|
|
382 *
|
|
383 * returns
|
|
384 * id of the iq packet
|
|
385 */
|
|
386 char *jab_auth(jconn j)
|
|
387 {
|
|
388 xmlnode x,y,z;
|
|
389 char *hash, *user, *id;
|
|
390
|
|
391 if(!j) return(NULL);
|
|
392
|
|
393 x = jutil_iqnew(JPACKET__SET, NS_AUTH);
|
|
394 id = jab_getid(j);
|
|
395 xmlnode_put_attrib(x, "id", id);
|
|
396 y = xmlnode_get_tag(x,"query");
|
|
397
|
|
398 user = j->user->user;
|
|
399
|
|
400 if (user)
|
|
401 {
|
|
402 z = xmlnode_insert_tag(y, "username");
|
|
403 xmlnode_insert_cdata(z, user, -1);
|
|
404 }
|
|
405
|
|
406 z = xmlnode_insert_tag(y, "resource");
|
|
407 xmlnode_insert_cdata(z, j->user->resource, -1);
|
|
408
|
|
409 if (j->sid)
|
|
410 {
|
|
411 z = xmlnode_insert_tag(y, "digest");
|
|
412 hash = pmalloc(x->p, strlen(j->sid)+strlen(j->pass)+1);
|
|
413 strcpy(hash, j->sid);
|
|
414 strcat(hash, j->pass);
|
|
415 hash = shahash(hash);
|
|
416 xmlnode_insert_cdata(z, hash, 40);
|
|
417 }
|
|
418 else
|
|
419 {
|
|
420 z = xmlnode_insert_tag(y, "password");
|
|
421 xmlnode_insert_cdata(z, j->pass, -1);
|
|
422 }
|
|
423
|
|
424 jab_send(j, x);
|
|
425 xmlnode_free(x);
|
|
426 return id;
|
|
427 }
|
|
428
|
|
429 /*
|
|
430 * jab_reg -- register user
|
|
431 *
|
|
432 * parameters
|
|
433 * j -- connection
|
|
434 *
|
|
435 * returns
|
|
436 * id of the iq packet
|
|
437 */
|
|
438 char *jab_reg(jconn j)
|
|
439 {
|
|
440 xmlnode x,y,z;
|
|
441 char *hash, *user, *id;
|
|
442
|
|
443 if (!j) return(NULL);
|
|
444
|
|
445 x = jutil_iqnew(JPACKET__SET, NS_REGISTER);
|
|
446 id = jab_getid(j);
|
|
447 xmlnode_put_attrib(x, "id", id);
|
|
448 y = xmlnode_get_tag(x,"query");
|
|
449
|
|
450 user = j->user->user;
|
|
451
|
|
452 if (user)
|
|
453 {
|
|
454 z = xmlnode_insert_tag(y, "username");
|
|
455 xmlnode_insert_cdata(z, user, -1);
|
|
456 }
|
|
457
|
|
458 z = xmlnode_insert_tag(y, "resource");
|
|
459 xmlnode_insert_cdata(z, j->user->resource, -1);
|
|
460
|
|
461 if (j->pass)
|
|
462 {
|
|
463 z = xmlnode_insert_tag(y, "password");
|
|
464 xmlnode_insert_cdata(z, j->pass, -1);
|
|
465 }
|
|
466
|
|
467 jab_send(j, x);
|
|
468 xmlnode_free(x);
|
|
469 j->state = JCONN_STATE_ON;
|
|
470 STATE_EVT(JCONN_STATE_ON)
|
|
471 return id;
|
|
472 }
|
|
473
|
|
474
|
|
475 /* local functions */
|
|
476
|
|
477 static void startElement(void *userdata, const char *name, const char **attribs)
|
|
478 {
|
|
479 xmlnode x;
|
|
480 jconn j = (jconn)userdata;
|
|
481
|
|
482 if(j->current)
|
|
483 {
|
|
484 /* Append the node to the current one */
|
|
485 x = xmlnode_insert_tag(j->current, name);
|
|
486 xmlnode_put_expat_attribs(x, attribs);
|
|
487
|
|
488 j->current = x;
|
|
489 }
|
|
490 else
|
|
491 {
|
|
492 x = xmlnode_new_tag(name);
|
|
493 xmlnode_put_expat_attribs(x, attribs);
|
|
494 if(strcmp(name, "stream:stream") == 0) {
|
|
495 /* special case: name == stream:stream */
|
|
496 /* id attrib of stream is stored for digest auth */
|
|
497 j->sid = xmlnode_get_attrib(x, "id");
|
|
498 /* STATE_EVT(JCONN_STATE_AUTH) */
|
|
499 } else {
|
|
500 j->current = x;
|
|
501 }
|
|
502 }
|
|
503 }
|
|
504
|
|
505 static void endElement(void *userdata, const char *name)
|
|
506 {
|
|
507 jconn j = (jconn)userdata;
|
|
508 xmlnode x;
|
|
509 jpacket p;
|
|
510
|
|
511 if(j->current == NULL) {
|
|
512 /* we got </stream:stream> */
|
|
513 STATE_EVT(JCONN_STATE_OFF)
|
|
514 return;
|
|
515 }
|
|
516
|
|
517 x = xmlnode_get_parent(j->current);
|
|
518
|
|
519 if(x == NULL)
|
|
520 {
|
|
521 /* it is time to fire the event */
|
|
522 p = jpacket_new(j->current);
|
|
523
|
|
524 if(j->on_packet)
|
|
525 (j->on_packet)(j, p);
|
|
526 else
|
|
527 xmlnode_free(j->current);
|
|
528 }
|
|
529
|
|
530 j->current = x;
|
|
531 }
|
|
532
|
|
533 static void charData(void *userdata, const char *s, int slen)
|
|
534 {
|
|
535 jconn j = (jconn)userdata;
|
|
536
|
|
537 if (j->current)
|
|
538 xmlnode_insert_cdata(j->current, s, slen);
|
|
539 }
|