]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/libtransport/transport_session.c
changed a log level to clear up some error logging
[OpenSRF.git] / 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                 osrfLogError(OSRF_LOG_MARK,  "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                 osrfLogWarning(OSRF_LOG_MARK, "socket_wait returned error code %d", ret);
121                 session->state_machine->connected = 0;
122         }
123         return ret;
124 }
125
126 int session_send_msg( 
127                 transport_session* session, transport_message* msg ) {
128
129         if( ! session ) { return -1; }
130
131         if( ! session->state_machine->connected ) {
132                 osrfLogWarning(OSRF_LOG_MARK, "State machine is not connected in send_msg()");
133                 return -1;
134         }
135
136         message_prepare_xml( msg );
137         //tcp_send( session->sock_obj, msg->msg_xml );
138         return socket_send( session->sock_id, msg->msg_xml );
139
140 }
141
142
143 /* connects to server and connects to jabber */
144 int session_connect( transport_session* session, 
145                 const char* username, const char* password, 
146                 const char* resource, int connect_timeout, enum TRANSPORT_AUTH_TYPE auth_type ) {
147
148         int size1 = 0;
149         int size2 = 0;
150
151         if( ! session ) { 
152                 osrfLogWarning(OSRF_LOG_MARK,  "session is null in connect" );
153                 return 0; 
154         }
155
156
157         //char* server = session->sock_obj->server;
158         char* server = session->server;
159
160         if( ! session->sock_id ) {
161
162                 if(session->port > 0) {
163                         if( (session->sock_id = socket_open_tcp_client(
164                                 session->sock_mgr, session->port, session->server)) <= 0 ) 
165                         return 0;
166
167                 } else if(session->unix_path != NULL) {
168                         if( (session->sock_id = socket_open_unix_client(
169                                 session->sock_mgr, session->unix_path)) <= 0 ) 
170                         return 0;
171                 }
172         }
173
174         if( session->component ) {
175
176                 /* the first Jabber connect stanza */
177                 char* our_hostname = getenv("HOSTNAME");
178                 size1 = 150 + strlen( server );
179                 char stanza1[ size1 ]; 
180                 memset( stanza1, 0, size1 );
181                 sprintf( stanza1, 
182                                 "<stream:stream version='1.0' xmlns:stream='http://etherx.jabber.org/streams' "
183                                 "xmlns='jabber:component:accept' to='%s' from='%s' xml:lang='en'>",
184                                 username, our_hostname );
185
186                 /* send the first stanze */
187                 session->state_machine->connecting = CONNECTING_1;
188
189 //              if( ! tcp_send( session->sock_obj, stanza1 ) ) {
190                 if( socket_send( session->sock_id, stanza1 ) ) {
191                         osrfLogWarning(OSRF_LOG_MARK, "error sending");
192                         return 0;
193                 }
194         
195                 /* wait for reply */
196                 //tcp_wait( session->sock_obj, connect_timeout ); /* make the timeout smarter XXX */
197                 socket_wait(session->sock_mgr, connect_timeout, session->sock_id);
198         
199                 /* server acknowledges our existence, now see if we can login */
200                 if( session->state_machine->connecting == CONNECTING_2 ) {
201         
202                         int ss = session->session_id->n_used + strlen(password) + 5;
203                         char hashstuff[ss];
204                         memset(hashstuff,0,ss);
205                         sprintf( hashstuff, "%s%s", session->session_id->buf, password );
206
207                         char* hash = shahash( hashstuff );
208                         size2 = 100 + strlen( hash );
209                         char stanza2[ size2 ];
210                         memset( stanza2, 0, size2 );
211                         sprintf( stanza2, "<handshake>%s</handshake>", hash );
212         
213                         //if( ! tcp_send( session->sock_obj, stanza2 )  ) {
214                         if( socket_send( session->sock_id, stanza2 )  ) {
215                                 osrfLogWarning(OSRF_LOG_MARK, "error sending");
216                                 return 0;
217                         }
218                 }
219
220         } else { /* we're not a component */
221
222                 /* the first Jabber connect stanza */
223                 size1 = 100 + strlen( server );
224                 char stanza1[ size1 ]; 
225                 memset( stanza1, 0, size1 );
226                 sprintf( stanza1, 
227                                 "<stream:stream to='%s' xmlns='jabber:client' "
228                                 "xmlns:stream='http://etherx.jabber.org/streams'>",
229                         server );
230         
231
232                 /* send the first stanze */
233                 session->state_machine->connecting = CONNECTING_1;
234                 //if( ! tcp_send( session->sock_obj, stanza1 ) ) {
235                 if( socket_send( session->sock_id, stanza1 ) ) {
236                         osrfLogWarning(OSRF_LOG_MARK, "error sending");
237                         return 0;
238                 }
239
240
241                 /* wait for reply */
242                 //tcp_wait( session->sock_obj, connect_timeout ); /* make the timeout smarter XXX */
243                 socket_wait( session->sock_mgr, connect_timeout, session->sock_id ); /* make the timeout smarter XXX */
244
245                 if( auth_type == AUTH_PLAIN ) {
246
247                         /* the second jabber connect stanza including login info*/
248                         size2 = 150 + strlen( username ) + strlen(password) + strlen(resource);
249                         char stanza2[ size2 ];
250                         memset( stanza2, 0, size2 );
251                 
252                         sprintf( stanza2, 
253                                         "<iq id='123456789' type='set'><query xmlns='jabber:iq:auth'>"
254                                         "<username>%s</username><password>%s</password><resource>%s</resource></query></iq>",
255                                         username, password, resource );
256         
257                         /* server acknowledges our existence, now see if we can login */
258                         if( session->state_machine->connecting == CONNECTING_2 ) {
259                                 //if( ! tcp_send( session->sock_obj, stanza2 )  ) {
260                                 if( socket_send( session->sock_id, stanza2 )  ) {
261                                         osrfLogWarning(OSRF_LOG_MARK, "error sending");
262                                         return 0;
263                                 }
264                         }
265
266                 } else if( auth_type == AUTH_DIGEST ) {
267
268                         int ss = session->session_id->n_used + strlen(password) + 5;
269                         char hashstuff[ss];
270                         memset(hashstuff,0,ss);
271                         sprintf( hashstuff, "%s%s", session->session_id->buf, password );
272
273                         char* hash = shahash( hashstuff );
274
275                         /* the second jabber connect stanza including login info*/
276                         size2 = 150 + strlen( hash ) + strlen(password) + strlen(resource);
277                         char stanza2[ size2 ];
278                         memset( stanza2, 0, size2 );
279                 
280                         sprintf( stanza2, 
281                                         "<iq id='123456789' type='set'><query xmlns='jabber:iq:auth'>"
282                                         "<username>%s</username><digest>%s</digest><resource>%s</resource></query></iq>",
283                                         username, hash, resource );
284         
285                         /* server acknowledges our existence, now see if we can login */
286                         if( session->state_machine->connecting == CONNECTING_2 ) {
287                                 //if( ! tcp_send( session->sock_obj, stanza2 )  ) {
288                                 if( socket_send( session->sock_id, stanza2 )  ) {
289                                         osrfLogWarning(OSRF_LOG_MARK, "error sending");
290                                         return 0;
291                                 }
292                         }
293
294                 }
295
296         } // not component
297
298
299         /* wait for reply */
300         //tcp_wait( session->sock_obj, connect_timeout );
301         socket_wait( session->sock_mgr, connect_timeout, session->sock_id );
302
303         if( session->state_machine->connected ) {
304                 /* yar! */
305                 return 1;
306         }
307
308         return 0;
309 }
310
311 // ---------------------------------------------------------------------------------
312 // TCP data callback. Shove the data into the push parser.
313 // ---------------------------------------------------------------------------------
314 //void grab_incoming( void * session, char* data ) {
315 void grab_incoming(void* blob, socket_manager* mgr, int sockid, char* data, int parent) {
316         transport_session* ses = (transport_session*) blob;
317         if( ! ses ) { return; }
318         xmlParseChunk(ses->parser_ctxt, data, strlen(data), 0);
319 }
320
321
322 void startElementHandler(
323         void *session, const xmlChar *name, const xmlChar **atts) {
324
325         transport_session* ses = (transport_session*) session;
326         if( ! ses ) { return; }
327
328         
329         if( strcmp( name, "message" ) == 0 ) {
330                 ses->state_machine->in_message = 1;
331                 buffer_add( ses->from_buffer, get_xml_attr( atts, "from" ) );
332                 buffer_add( ses->recipient_buffer, get_xml_attr( atts, "to" ) );
333                 buffer_add( ses->router_from_buffer, get_xml_attr( atts, "router_from" ) );
334                 buffer_add( ses->router_to_buffer, get_xml_attr( atts, "router_to" ) );
335                 buffer_add( ses->router_class_buffer, get_xml_attr( atts, "router_class" ) );
336                 buffer_add( ses->router_command_buffer, get_xml_attr( atts, "router_command" ) );
337                 char* broadcast = get_xml_attr( atts, "broadcast" );
338                 if( broadcast )
339                         ses->router_broadcast = atoi( broadcast );
340
341                 return;
342         }
343
344         if( ses->state_machine->in_message ) {
345
346                 if( strcmp( name, "body" ) == 0 ) {
347                         ses->state_machine->in_message_body = 1;
348                         return;
349                 }
350         
351                 if( strcmp( name, "subject" ) == 0 ) {
352                         ses->state_machine->in_subject = 1;
353                         return;
354                 }
355         
356                 if( strcmp( name, "thread" ) == 0 ) {
357                         ses->state_machine->in_thread = 1;
358                         return;
359                 }
360
361         }
362
363         if( strcmp( name, "presence" ) == 0 ) {
364                 ses->state_machine->in_presence = 1;
365                 buffer_add( ses->from_buffer, get_xml_attr( atts, "from" ) );
366                 buffer_add( ses->recipient_buffer, get_xml_attr( atts, "to" ) );
367                 return;
368         }
369
370         if( strcmp( name, "status" ) == 0 ) {
371                 ses->state_machine->in_status = 1;
372                 return;
373         }
374
375
376         if( strcmp( name, "stream:error" ) == 0 ) {
377                 ses->state_machine->in_error = 1;
378                 ses->state_machine->connected = 0;
379                 osrfLogWarning(  OSRF_LOG_MARK, "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                 osrfLogInfo( OSRF_LOG_MARK,  "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                         osrfLogWarning( OSRF_LOG_MARK,  "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                         osrfLogWarning( OSRF_LOG_MARK,  "Error in IQ packet: code %d",  ses->message_error_code );
502                         osrfLogWarning( OSRF_LOG_MARK,  "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                 osrfLogWarning( OSRF_LOG_MARK,  "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