cleaned up Biblio some
[working/Evergreen.git] / OpenSRF / src / libtransport / transport_session.c
1 #include "transport_session.h"
2
3
4
5 // ---------------------------------------------------------------------------------
6 // returns a built and allocated transport_session object.
7 // This codes does no network activity, only memory initilization
8 // ---------------------------------------------------------------------------------
9 transport_session* init_transport(  char* server, 
10         int port, char* unix_path, void* user_data, int component ) {
11
12         /* create the session struct */
13         transport_session* session = 
14                 (transport_session*) safe_malloc( sizeof(transport_session) );
15
16         session->user_data = user_data;
17
18         session->component = component;
19
20         /* initialize the data buffers */
21         session->body_buffer                    = buffer_init( JABBER_BODY_BUFSIZE );
22         session->subject_buffer         = buffer_init( JABBER_SUBJECT_BUFSIZE );
23         session->thread_buffer          = buffer_init( JABBER_THREAD_BUFSIZE );
24         session->from_buffer                    = buffer_init( JABBER_JID_BUFSIZE );
25         session->status_buffer          = buffer_init( JABBER_STATUS_BUFSIZE );
26         session->recipient_buffer       = buffer_init( JABBER_JID_BUFSIZE );
27         session->message_error_type = buffer_init( JABBER_JID_BUFSIZE );
28         session->session_id                     = buffer_init( 64 ); 
29
30         /* for OpenSRF extensions */
31         session->router_to_buffer               = buffer_init( JABBER_JID_BUFSIZE );
32         session->router_from_buffer     = buffer_init( JABBER_JID_BUFSIZE );
33         session->router_class_buffer    = buffer_init( JABBER_JID_BUFSIZE );
34         session->router_command_buffer  = buffer_init( JABBER_JID_BUFSIZE );
35
36
37         if(     session->body_buffer            == NULL || session->subject_buffer       == NULL        ||
38                         session->thread_buffer  == NULL || session->from_buffer          == NULL        ||
39                         session->status_buffer  == NULL || session->recipient_buffer == NULL ||
40                         session->router_to_buffer       == NULL || session->router_from_buffer   == NULL ||
41                         session->router_class_buffer == NULL || session->router_command_buffer == NULL ||
42                         session->session_id == NULL ) { 
43
44                 fatal_handler( "init_transport(): buffer_init returned NULL" );
45                 return 0;
46         }
47
48
49         /* initialize the jabber state machine */
50         session->state_machine = (jabber_machine*) safe_malloc( sizeof(jabber_machine) );
51
52         /* initialize the sax push parser */
53         session->parser_ctxt = xmlCreatePushParserCtxt(SAXHandler, session, "", 0, NULL);
54
55         /* initialize the transport_socket structure */
56         session->sock_mgr = (socket_manager*) safe_malloc( sizeof(socket_manager) );
57
58         session->sock_mgr->data_received = &grab_incoming;
59         session->sock_mgr->blob = session;
60         
61         session->port = port;
62         session->server = strdup(server);
63         if(unix_path)   
64                 session->unix_path = strdup(unix_path);
65         else session->unix_path = NULL;
66
67         session->sock_id = 0;
68
69         return session;
70 }
71
72
73
74 /* XXX FREE THE BUFFERS */
75 int session_free( transport_session* session ) {
76         if( ! session ) { return 0; }
77
78         if(session->sock_mgr)
79                 socket_manager_free(session->sock_mgr);
80
81         if( session->state_machine ) free( session->state_machine );
82         if( session->parser_ctxt) {
83                 xmlFreeDoc( session->parser_ctxt->myDoc );
84                 xmlFreeParserCtxt(session->parser_ctxt);
85         }
86
87         xmlCleanupCharEncodingHandlers();
88         xmlDictCleanup();
89         xmlCleanupParser();
90
91         buffer_free(session->body_buffer);
92         buffer_free(session->subject_buffer);
93         buffer_free(session->thread_buffer);
94         buffer_free(session->from_buffer);
95         buffer_free(session->recipient_buffer);
96         buffer_free(session->status_buffer);
97         buffer_free(session->message_error_type);
98         buffer_free(session->router_to_buffer);
99         buffer_free(session->router_from_buffer);
100         buffer_free(session->router_class_buffer);
101         buffer_free(session->router_command_buffer);
102         buffer_free(session->session_id);
103
104         free(session->server);
105         free(session->unix_path);
106
107         free( session );
108         return 1;
109 }
110
111
112 int session_wait( transport_session* session, int timeout ) {
113         if( ! session || ! session->sock_mgr ) {
114                 return 0;
115         }
116
117         int ret =  socket_wait( session->sock_mgr, timeout, session->sock_id );
118
119         if( ret ) {
120                 session->state_machine->connected = 0;
121         }
122         return ret;
123 }
124
125 int session_send_msg( 
126                 transport_session* session, transport_message* msg ) {
127
128         if( ! session ) { return 0; }
129
130         if( ! session->state_machine->connected ) {
131                 warning_handler("State machine is not connected in send_msg()");
132                 return 0;
133         }
134
135         message_prepare_xml( msg );
136         //tcp_send( session->sock_obj, msg->msg_xml );
137         socket_send( session->sock_id, msg->msg_xml );
138
139         return 1;
140
141 }
142
143
144 /* connects to server and connects to jabber */
145 int session_connect( transport_session* session, 
146                 const char* username, const char* password, 
147                 const char* resource, int connect_timeout, enum TRANSPORT_AUTH_TYPE auth_type ) {
148
149         int size1 = 0;
150         int size2 = 0;
151
152         if( ! session ) { 
153                 warning_handler( "session is null in connect" );
154                 return 0; 
155         }
156
157
158         //char* server = session->sock_obj->server;
159         char* server = session->server;
160
161         if( ! session->sock_id ) {
162
163                 if(session->port > 0) {
164                         if( (session->sock_id = socket_open_tcp_client(
165                                 session->sock_mgr, session->port, session->server)) <= 0 ) 
166                         return 0;
167
168                 } else if(session->unix_path != NULL) {
169                         if( (session->sock_id = socket_open_unix_client(
170                                 session->sock_mgr, session->unix_path)) <= 0 ) 
171                         return 0;
172                 }
173         }
174
175         if( session->component ) {
176
177                 /* the first Jabber connect stanza */
178                 char* our_hostname = getenv("HOSTNAME");
179                 size1 = 150 + strlen( server );
180                 char stanza1[ size1 ]; 
181                 memset( stanza1, 0, size1 );
182                 sprintf( stanza1, 
183                                 "<stream:stream version='1.0' xmlns:stream='http://etherx.jabber.org/streams' "
184                                 "xmlns='jabber:component:accept' to='%s' from='%s' xml:lang='en'>",
185                                 username, our_hostname );
186
187                 /* send the first stanze */
188                 session->state_machine->connecting = CONNECTING_1;
189
190 //              if( ! tcp_send( session->sock_obj, stanza1 ) ) {
191                 if( socket_send( session->sock_id, stanza1 ) ) {
192                         warning_handler("error sending");
193                         return 0;
194                 }
195         
196                 /* wait for reply */
197                 //tcp_wait( session->sock_obj, connect_timeout ); /* make the timeout smarter XXX */
198                 socket_wait(session->sock_mgr, connect_timeout, session->sock_id);
199         
200                 /* server acknowledges our existence, now see if we can login */
201                 if( session->state_machine->connecting == CONNECTING_2 ) {
202         
203                         int ss = session->session_id->n_used + strlen(password) + 5;
204                         char hashstuff[ss];
205                         memset(hashstuff,0,ss);
206                         sprintf( hashstuff, "%s%s", session->session_id->buf, password );
207
208                         char* hash = shahash( hashstuff );
209                         size2 = 100 + strlen( hash );
210                         char stanza2[ size2 ];
211                         memset( stanza2, 0, size2 );
212                         sprintf( stanza2, "<handshake>%s</handshake>", hash );
213         
214                         //if( ! tcp_send( session->sock_obj, stanza2 )  ) {
215                         if( socket_send( session->sock_id, stanza2 )  ) {
216                                 warning_handler("error sending");
217                                 return 0;
218                         }
219                 }
220
221         } else { /* we're not a component */
222
223                 /* the first Jabber connect stanza */
224                 size1 = 100 + strlen( server );
225                 char stanza1[ size1 ]; 
226                 memset( stanza1, 0, size1 );
227                 sprintf( stanza1, 
228                                 "<stream:stream to='%s' xmlns='jabber:client' "
229                                 "xmlns:stream='http://etherx.jabber.org/streams'>",
230                         server );
231         
232
233                 /* send the first stanze */
234                 session->state_machine->connecting = CONNECTING_1;
235                 //if( ! tcp_send( session->sock_obj, stanza1 ) ) {
236                 if( socket_send( session->sock_id, stanza1 ) ) {
237                         warning_handler("error sending");
238                         return 0;
239                 }
240
241
242                 /* wait for reply */
243                 //tcp_wait( session->sock_obj, connect_timeout ); /* make the timeout smarter XXX */
244                 socket_wait( session->sock_mgr, connect_timeout, session->sock_id ); /* make the timeout smarter XXX */
245
246                 if( auth_type == AUTH_PLAIN ) {
247
248                         /* the second jabber connect stanza including login info*/
249                         size2 = 150 + strlen( username ) + strlen(password) + strlen(resource);
250                         char stanza2[ size2 ];
251                         memset( stanza2, 0, size2 );
252                 
253                         sprintf( stanza2, 
254                                         "<iq id='123456789' type='set'><query xmlns='jabber:iq:auth'>"
255                                         "<username>%s</username><password>%s</password><resource>%s</resource></query></iq>",
256                                         username, password, resource );
257         
258                         /* server acknowledges our existence, now see if we can login */
259                         if( session->state_machine->connecting == CONNECTING_2 ) {
260                                 //if( ! tcp_send( session->sock_obj, stanza2 )  ) {
261                                 if( socket_send( session->sock_id, stanza2 )  ) {
262                                         warning_handler("error sending");
263                                         return 0;
264                                 }
265                         }
266
267                 } else if( auth_type == AUTH_DIGEST ) {
268
269                         int ss = session->session_id->n_used + strlen(password) + 5;
270                         char hashstuff[ss];
271                         memset(hashstuff,0,ss);
272                         sprintf( hashstuff, "%s%s", session->session_id->buf, password );
273
274                         char* hash = shahash( hashstuff );
275
276                         /* the second jabber connect stanza including login info*/
277                         size2 = 150 + strlen( hash ) + strlen(password) + strlen(resource);
278                         char stanza2[ size2 ];
279                         memset( stanza2, 0, size2 );
280                 
281                         sprintf( stanza2, 
282                                         "<iq id='123456789' type='set'><query xmlns='jabber:iq:auth'>"
283                                         "<username>%s</username><digest>%s</digest><resource>%s</resource></query></iq>",
284                                         username, hash, resource );
285         
286                         /* server acknowledges our existence, now see if we can login */
287                         if( session->state_machine->connecting == CONNECTING_2 ) {
288                                 //if( ! tcp_send( session->sock_obj, stanza2 )  ) {
289                                 if( socket_send( session->sock_id, stanza2 )  ) {
290                                         warning_handler("error sending");
291                                         return 0;
292                                 }
293                         }
294
295                 }
296
297         } // not component
298
299
300         /* wait for reply */
301         //tcp_wait( session->sock_obj, connect_timeout );
302         socket_wait( session->sock_mgr, connect_timeout, session->sock_id );
303
304         if( session->state_machine->connected ) {
305                 /* yar! */
306                 return 1;
307         }
308
309         return 0;
310 }
311
312 // ---------------------------------------------------------------------------------
313 // TCP data callback. Shove the data into the push parser.
314 // ---------------------------------------------------------------------------------
315 //void grab_incoming( void * session, char* data ) {
316 void grab_incoming(void* blob, socket_manager* mgr, int sockid, char* data, int parent) {
317         transport_session* ses = (transport_session*) blob;
318         if( ! ses ) { return; }
319         xmlParseChunk(ses->parser_ctxt, data, strlen(data), 0);
320 }
321
322
323 void startElementHandler(
324         void *session, const xmlChar *name, const xmlChar **atts) {
325
326         transport_session* ses = (transport_session*) session;
327         if( ! ses ) { return; }
328
329         
330         if( strcmp( name, "message" ) == 0 ) {
331                 ses->state_machine->in_message = 1;
332                 buffer_add( ses->from_buffer, get_xml_attr( atts, "from" ) );
333                 buffer_add( ses->recipient_buffer, get_xml_attr( atts, "to" ) );
334                 buffer_add( ses->router_from_buffer, get_xml_attr( atts, "router_from" ) );
335                 buffer_add( ses->router_to_buffer, get_xml_attr( atts, "router_to" ) );
336                 buffer_add( ses->router_class_buffer, get_xml_attr( atts, "router_class" ) );
337                 buffer_add( ses->router_command_buffer, get_xml_attr( atts, "router_command" ) );
338                 char* broadcast = get_xml_attr( atts, "broadcast" );
339                 if( broadcast )
340                         ses->router_broadcast = atoi( broadcast );
341
342                 return;
343         }
344
345         if( ses->state_machine->in_message ) {
346
347                 if( strcmp( name, "body" ) == 0 ) {
348                         ses->state_machine->in_message_body = 1;
349                         return;
350                 }
351         
352                 if( strcmp( name, "subject" ) == 0 ) {
353                         ses->state_machine->in_subject = 1;
354                         return;
355                 }
356         
357                 if( strcmp( name, "thread" ) == 0 ) {
358                         ses->state_machine->in_thread = 1;
359                         return;
360                 }
361
362         }
363
364         if( strcmp( name, "presence" ) == 0 ) {
365                 ses->state_machine->in_presence = 1;
366                 buffer_add( ses->from_buffer, get_xml_attr( atts, "from" ) );
367                 buffer_add( ses->recipient_buffer, get_xml_attr( atts, "to" ) );
368                 return;
369         }
370
371         if( strcmp( name, "status" ) == 0 ) {
372                 ses->state_machine->in_status = 1;
373                 return;
374         }
375
376
377         if( strcmp( name, "stream:error" ) == 0 ) {
378                 ses->state_machine->in_error = 1;
379                 warning_handler( "Received <stream:error> message from Jabber server" );
380                 return;
381         }
382
383
384         /* first server response from a connect attempt */
385         if( strcmp( name, "stream:stream" ) == 0 ) {
386                 if( ses->state_machine->connecting == CONNECTING_1 ) {
387                         ses->state_machine->connecting = CONNECTING_2;
388                         buffer_add( ses->session_id, get_xml_attr(atts, "id") );
389                 }
390         }
391
392         if( strcmp( name, "handshake" ) == 0 ) {
393                 ses->state_machine->connected = 1;
394                 ses->state_machine->connecting = 0;
395                 return;
396         }
397
398
399         if( strcmp( name, "error" ) == 0 ) {
400                 ses->state_machine->in_message_error = 1;
401                 buffer_add( ses->message_error_type, get_xml_attr( atts, "type" ) );
402                 ses->message_error_code = atoi( get_xml_attr( atts, "code" ) );
403                 warning_handler( "Received <error> message with type %s and code %s", 
404                         get_xml_attr( atts, "type"), get_xml_attr( atts, "code") );
405                 return;
406         }
407
408         if( strcmp( name, "iq" ) == 0 ) {
409                 ses->state_machine->in_iq = 1;
410
411                 if( strcmp( get_xml_attr(atts, "type"), "result") == 0 
412                                 && ses->state_machine->connecting == CONNECTING_2 ) {
413                         ses->state_machine->connected = 1;
414                         ses->state_machine->connecting = 0;
415                         return;
416                 }
417
418                 if( strcmp( get_xml_attr(atts, "type"), "error") == 0 ) {
419                         warning_handler( "Error connecting to jabber" );
420                         return;
421                 }
422         }
423 }
424
425 char* get_xml_attr( const xmlChar** atts, char* attr_name ) {
426         int i;
427         if (atts != NULL) {
428                 for(i = 0;(atts[i] != NULL);i++) {
429                         if( strcmp( atts[i++], attr_name ) == 0 ) {
430                                 if( atts[i] != NULL ) {
431                                         return (char*) atts[i];
432                                 }
433                         }
434                 }
435         }
436         return NULL;
437 }
438
439
440 // ------------------------------------------------------------------
441 // See which tags are ending
442 // ------------------------------------------------------------------
443 void endElementHandler( void *session, const xmlChar *name) {
444         transport_session* ses = (transport_session*) session;
445         if( ! ses ) { return; }
446
447         if( strcmp( name, "message" ) == 0 ) {
448
449
450                 /* pass off the message info the callback */
451                 if( ses->message_callback ) {
452
453                         /* here it's ok to pass in the raw buffers because
454                                 message_init allocates new space for the chars 
455                                 passed in */
456                         transport_message* msg =  message_init( 
457                                 ses->body_buffer->buf, 
458                                 ses->subject_buffer->buf,
459                                 ses->thread_buffer->buf, 
460                                 ses->recipient_buffer->buf, 
461                                 ses->from_buffer->buf );
462
463                         message_set_router_info( msg, 
464                                 ses->router_from_buffer->buf, 
465                                 ses->router_to_buffer->buf, 
466                                 ses->router_class_buffer->buf,
467                                 ses->router_command_buffer->buf,
468                                 ses->router_broadcast );
469
470                         if( ses->message_error_type->n_used > 0 ) {
471                                 set_msg_error( msg, ses->message_error_type->buf, ses->message_error_code );
472                         }
473
474                         if( msg == NULL ) { return; }
475                         ses->message_callback( ses->user_data, msg );
476                 }
477
478                 ses->state_machine->in_message = 0;
479                 reset_session_buffers( session );
480                 return;
481         }
482         
483         if( strcmp( name, "body" ) == 0 ) {
484                 ses->state_machine->in_message_body = 0;
485                 return;
486         }
487
488         if( strcmp( name, "subject" ) == 0 ) {
489                 ses->state_machine->in_subject = 0;
490                 return;
491         }
492
493         if( strcmp( name, "thread" ) == 0 ) {
494                 ses->state_machine->in_thread = 0;
495                 return;
496         }
497         
498         if( strcmp( name, "iq" ) == 0 ) {
499                 ses->state_machine->in_iq = 0;
500                 if( ses->message_error_code > 0 ) {
501                         warning_handler( "Error in IQ packet: code %d",  ses->message_error_code );
502                         warning_handler( "Error 401 means not authorized" );
503                 }
504                 reset_session_buffers( session );
505                 return;
506         }
507
508         if( strcmp( name, "presence" ) == 0 ) {
509                 ses->state_machine->in_presence = 0;
510                 /*
511                 if( ses->presence_callback ) {
512                         // call the callback with the status, etc.
513                 }
514                 */
515                 reset_session_buffers( session );
516                 return;
517         }
518
519         if( strcmp( name, "status" ) == 0 ) {
520                 ses->state_machine->in_status = 0;
521                 return;
522         }
523
524         if( strcmp( name, "error" ) == 0 ) {
525                 ses->state_machine->in_message_error = 0;
526                 return;
527         }
528
529         if( strcmp( name, "error:error" ) == 0 ) {
530                 ses->state_machine->in_error = 0;
531                 return;
532         }
533 }
534
535 int reset_session_buffers( transport_session* ses ) {
536         buffer_reset( ses->body_buffer );
537         buffer_reset( ses->subject_buffer );
538         buffer_reset( ses->thread_buffer );
539         buffer_reset( ses->from_buffer );
540         buffer_reset( ses->recipient_buffer );
541         buffer_reset( ses->router_from_buffer );
542         buffer_reset( ses->router_to_buffer );
543         buffer_reset( ses->router_class_buffer );
544         buffer_reset( ses->router_command_buffer );
545         buffer_reset( ses->message_error_type );
546         buffer_reset( ses->session_id );
547
548         return 1;
549 }
550
551 // ------------------------------------------------------------------
552 // takes data out of the body of the message and pushes it into
553 // the appropriate buffer
554 // ------------------------------------------------------------------
555 void characterHandler(
556                 void *session, const xmlChar *ch, int len) {
557
558         char data[len+1];
559         memset( data, 0, len+1 );
560         strncpy( data, (char*) ch, len );
561         data[len] = 0;
562
563         //printf( "Handling characters: %s\n", data );
564         transport_session* ses = (transport_session*) session;
565         if( ! ses ) { return; }
566
567         /* set the various message parts */
568         if( ses->state_machine->in_message ) {
569
570                 if( ses->state_machine->in_message_body ) {
571                         buffer_add( ses->body_buffer, data );
572                 }
573
574                 if( ses->state_machine->in_subject ) {
575                         buffer_add( ses->subject_buffer, data );
576                 }
577
578                 if( ses->state_machine->in_thread ) {
579                         buffer_add( ses->thread_buffer, data );
580                 }
581         }
582
583         /* set the presence status */
584         if( ses->state_machine->in_presence && ses->state_machine->in_status ) {
585                 buffer_add( ses->status_buffer, data );
586         }
587
588         if( ses->state_machine->in_error ) {
589                 /* for now... */
590                 warning_handler( "ERROR Xml fragment: %s\n", ch );
591         }
592
593 }
594
595 /* XXX change to warning handlers */
596 void  parseWarningHandler( void *session, const char* msg, ... ) {
597
598         va_list args;
599         va_start(args, msg);
600         fprintf(stdout, "transport_session XML WARNING");
601         vfprintf(stdout, msg, args);
602         va_end(args);
603         fprintf(stderr, "XML WARNING: %s\n", msg ); 
604 }
605
606 void  parseErrorHandler( void *session, const char* msg, ... ){
607
608         va_list args;
609         va_start(args, msg);
610         fprintf(stdout, "transport_session XML ERROR");
611         vfprintf(stdout, msg, args);
612         va_end(args);
613         fprintf(stderr, "XML ERROR: %s\n", msg ); 
614
615 }
616
617 int session_disconnect( transport_session* session ) {
618         if( session == NULL ) { return 0; }
619         //tcp_send( session->sock_obj, "</stream:stream>");
620         socket_send(session->sock_id, "</stream:stream>");
621         socket_disconnect(session->sock_mgr, session->sock_id);
622         return 0;
623         //return tcp_disconnect( session->sock_obj );
624 }
625