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) {
|
|
368 STATE_EVT(JCONN_STATE_OFF);
|
|
369 jab_stop(j);
|
|
370
|
|
371 }
|
|
372 }
|
|
373
|
|
374 /*
|
|
375 * jab_auth -- authorize user
|
|
376 *
|
|
377 * parameters
|
|
378 * j -- connection
|
|
379 *
|
|
380 * returns
|
|
381 * id of the iq packet
|
|
382 */
|
|
383 char *jab_auth(jconn j)
|
|
384 {
|
|
385 xmlnode x,y,z;
|
|
386 char *hash, *user, *id;
|
|
387
|
|
388 if(!j) return(NULL);
|
|
389
|
|
390 x = jutil_iqnew(JPACKET__SET, NS_AUTH);
|
|
391 id = jab_getid(j);
|
|
392 xmlnode_put_attrib(x, "id", id);
|
|
393 y = xmlnode_get_tag(x,"query");
|
|
394
|
|
395 user = j->user->user;
|
|
396
|
|
397 if (user)
|
|
398 {
|
|
399 z = xmlnode_insert_tag(y, "username");
|
|
400 xmlnode_insert_cdata(z, user, -1);
|
|
401 }
|
|
402
|
|
403 z = xmlnode_insert_tag(y, "resource");
|
|
404 xmlnode_insert_cdata(z, j->user->resource, -1);
|
|
405
|
|
406 if (j->sid)
|
|
407 {
|
|
408 z = xmlnode_insert_tag(y, "digest");
|
|
409 hash = pmalloc(x->p, strlen(j->sid)+strlen(j->pass)+1);
|
|
410 strcpy(hash, j->sid);
|
|
411 strcat(hash, j->pass);
|
|
412 hash = shahash(hash);
|
|
413 xmlnode_insert_cdata(z, hash, 40);
|
|
414 }
|
|
415 else
|
|
416 {
|
|
417 z = xmlnode_insert_tag(y, "password");
|
|
418 xmlnode_insert_cdata(z, j->pass, -1);
|
|
419 }
|
|
420
|
|
421 jab_send(j, x);
|
|
422 xmlnode_free(x);
|
|
423 return id;
|
|
424 }
|
|
425
|
|
426 /*
|
|
427 * jab_reg -- register user
|
|
428 *
|
|
429 * parameters
|
|
430 * j -- connection
|
|
431 *
|
|
432 * returns
|
|
433 * id of the iq packet
|
|
434 */
|
|
435 char *jab_reg(jconn j)
|
|
436 {
|
|
437 xmlnode x,y,z;
|
|
438 char *hash, *user, *id;
|
|
439
|
|
440 if (!j) return(NULL);
|
|
441
|
|
442 x = jutil_iqnew(JPACKET__SET, NS_REGISTER);
|
|
443 id = jab_getid(j);
|
|
444 xmlnode_put_attrib(x, "id", id);
|
|
445 y = xmlnode_get_tag(x,"query");
|
|
446
|
|
447 user = j->user->user;
|
|
448
|
|
449 if (user)
|
|
450 {
|
|
451 z = xmlnode_insert_tag(y, "username");
|
|
452 xmlnode_insert_cdata(z, user, -1);
|
|
453 }
|
|
454
|
|
455 z = xmlnode_insert_tag(y, "resource");
|
|
456 xmlnode_insert_cdata(z, j->user->resource, -1);
|
|
457
|
|
458 if (j->pass)
|
|
459 {
|
|
460 z = xmlnode_insert_tag(y, "password");
|
|
461 xmlnode_insert_cdata(z, j->pass, -1);
|
|
462 }
|
|
463
|
|
464 jab_send(j, x);
|
|
465 xmlnode_free(x);
|
|
466 j->state = JCONN_STATE_ON;
|
|
467 STATE_EVT(JCONN_STATE_ON)
|
|
468 return id;
|
|
469 }
|
|
470
|
|
471
|
|
472 /* local functions */
|
|
473
|
|
474 static void startElement(void *userdata, const char *name, const char **attribs)
|
|
475 {
|
|
476 xmlnode x;
|
|
477 jconn j = (jconn)userdata;
|
|
478
|
|
479 if(j->current)
|
|
480 {
|
|
481 /* Append the node to the current one */
|
|
482 x = xmlnode_insert_tag(j->current, name);
|
|
483 xmlnode_put_expat_attribs(x, attribs);
|
|
484
|
|
485 j->current = x;
|
|
486 }
|
|
487 else
|
|
488 {
|
|
489 x = xmlnode_new_tag(name);
|
|
490 xmlnode_put_expat_attribs(x, attribs);
|
|
491 if(strcmp(name, "stream:stream") == 0) {
|
|
492 /* special case: name == stream:stream */
|
|
493 /* id attrib of stream is stored for digest auth */
|
|
494 j->sid = xmlnode_get_attrib(x, "id");
|
|
495 /* STATE_EVT(JCONN_STATE_AUTH) */
|
|
496 } else {
|
|
497 j->current = x;
|
|
498 }
|
|
499 }
|
|
500 }
|
|
501
|
|
502 static void endElement(void *userdata, const char *name)
|
|
503 {
|
|
504 jconn j = (jconn)userdata;
|
|
505 xmlnode x;
|
|
506 jpacket p;
|
|
507
|
|
508 if(j->current == NULL) {
|
|
509 /* we got </stream:stream> */
|
|
510 STATE_EVT(JCONN_STATE_OFF)
|
|
511 return;
|
|
512 }
|
|
513
|
|
514 x = xmlnode_get_parent(j->current);
|
|
515
|
|
516 if(x == NULL)
|
|
517 {
|
|
518 /* it is time to fire the event */
|
|
519 p = jpacket_new(j->current);
|
|
520
|
|
521 if(j->on_packet)
|
|
522 (j->on_packet)(j, p);
|
|
523 else
|
|
524 xmlnode_free(j->current);
|
|
525 }
|
|
526
|
|
527 j->current = x;
|
|
528 }
|
|
529
|
|
530 static void charData(void *userdata, const char *s, int slen)
|
|
531 {
|
|
532 jconn j = (jconn)userdata;
|
|
533
|
|
534 if (j->current)
|
|
535 xmlnode_insert_cdata(j->current, s, slen);
|
|
536 }
|