Added additional param to all the osrfLog* calls which mark
[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                 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                 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 -1; }
129
130         if( ! session->state_machine->connected ) {
131                 osrfLogWarning(OSRF_LOG_MARK, "State machine is not connected in send_msg()");
132                 return -1;
133         }
134
135         message_prepare_xml( msg );
136         //tcp_send( session->sock_obj, msg->msg_xml );
137         return socket_send( session->sock_id, msg->msg_xml );
138
139 }
140
141
142 /* connects to server and connects to jabber */
143 int session_connect( transport_session* session, 
144                 const char* username, const char* password, 
145                 const char* resource, int connect_timeout, enum TRANSPORT_AUTH_TYPE auth_type ) {
146
147         int size1 = 0;
148         int size2 = 0;
149
150         if( ! session ) { 
151                 osrfLogWarning(OSRF_LOG_MARK,  "session is null in connect" );
152                 return 0; 
153         }
154
155
156         //char* server = session->sock_obj->server;
157         char* server = session->server;
158
159         if( ! session->sock_id ) {
160
161                 if(session->port > 0) {
162                         if( (session->sock_id = socket_open_tcp_client(
163                                 session->sock_mgr, session->port, session->server)) <= 0 ) 
164                         return 0;
165
166                 } else if(session->unix_path != NULL) {
167                         if( (session->sock_id = socket_open_unix_client(
168                                 session->sock_mgr, session->unix_path)) <= 0 ) 
169                         return 0;
170                 }
171         }
172
173         if( session->component ) {
174
175                 /* the first Jabber connect stanza */
176                 char* our_hostname = getenv("HOSTNAME");
177                 size1 = 150 + strlen( server );
178                 char stanza1[ size1 ]; 
179                 memset( stanza1, 0, size1 );
180                 sprintf( stanza1, 
181                                 "<stream:stream version='1.0' xmlns:stream='http://etherx.jabber.org/streams' "
182                                 "xmlns='jabber:component:accept' to='%s' from='%s' xml:lang='en'>",
183                                 username, our_hostname );
184
185                 /* send the first stanze */
186                 session->state_machine->connecting = CONNECTING_1;
187
188 //              if( ! tcp_send( session->sock_obj, stanza1 ) ) {
189                 if( socket_send( session->sock_id, stanza1 ) ) {
190                         osrfLogWarning(OSRF_LOG_MARK, "error sending");
191                         return 0;
192                 }
193         
194                 /* wait for reply */
195                 //tcp_wait( session->sock_obj, connect_timeout ); /* make the timeout smarter XXX */
196                 socket_wait(session->sock_mgr, connect_timeout, session->sock_id);
197         
198                 /* server acknowledges our existence, now see if we can login */
199                 if( session->state_machine->connecting == CONNECTING_2 ) {
200         
201                         int ss = session->session_id->n_used + strlen(password) + 5;
202                         char hashstuff[ss];
203                         memset(hashstuff,0,ss);
204                         sprintf( hashstuff, "%s%s", session->session_id->buf, password );
205
206                         char* hash = shahash( hashstuff );
207                         size2 = 100 + strlen( hash );
208                         char stanza2[ size2 ];
209                         memset( stanza2, 0, size2 );
210                         sprintf( stanza2, "<handshake>%s</handshake>", hash );
211         
212                         //if( ! tcp_send( session->sock_obj, stanza2 )  ) {
213                         if( socket_send( session->sock_id, stanza2 )  ) {
214                                 osrfLogWarning(OSRF_LOG_MARK, "error sending");
215                                 return 0;
216                         }
217                 }
218
219         } else { /* we're not a component */
220
221                 /* the first Jabber connect stanza */
222                 size1 = 100 + strlen( server );
223                 char stanza1[ size1 ]; 
224                 memset( stanza1, 0, size1 );
225                 sprintf( stanza1, 
226                                 "<stream:stream to='%s' xmlns='jabber:client' "
227                                 "xmlns:stream='http://etherx.jabber.org/streams'>",
228                         server );
229         
230
231                 /* send the first stanze */
232                 session->state_machine->connecting = CONNECTING_1;
233                 //if( ! tcp_send( session->sock_obj, stanza1 ) ) {
234                 if( socket_send( session->sock_id, stanza1 ) ) {
235                         osrfLogWarning(OSRF_LOG_MARK, "error sending");
236                         return 0;
237                 }
238
239
240                 /* wait for reply */
241                 //tcp_wait( session->sock_obj, connect_timeout ); /* make the timeout smarter XXX */
242                 socket_wait( session->sock_mgr, connect_timeout, session->sock_id ); /* make the timeout smarter XXX */
243
244                 if( auth_type == AUTH_PLAIN ) {
245
246                         /* the second jabber connect stanza including login info*/
247                         size2 = 150 + strlen( username ) + strlen(password) + strlen(resource);
248                         char stanza2[ size2 ];
249                         memset( stanza2, 0, size2 );
250                 
251                         sprintf( stanza2, 
252                                         "<iq id='123456789' type='set'><query xmlns='jabber:iq:auth'>"
253                                         "<username>%s</username><password>%s</password><resource>%s</resource></query></iq>",
254                                         username, password, resource );
255         
256                         /* server acknowledges our existence, now see if we can login */
257                         if( session->state_machine->connecting == CONNECTING_2 ) {
258                                 //if( ! tcp_send( session->sock_obj, stanza2 )  ) {
259                                 if( socket_send( session->sock_id, stanza2 )  ) {
260                                         osrfLogWarning(OSRF_LOG_MARK, "error sending");
261                                         return 0;
262                                 }
263                         }
264
265                 } else if( auth_type == AUTH_DIGEST ) {
266
267                         int ss = session->session_id->n_used + strlen(password) + 5;
268                         char hashstuff[ss];
269                         memset(hashstuff,0,ss);
270                         sprintf( hashstuff, "%s%s", session->session_id->buf, password );
271
272                         char* hash = shahash( hashstuff );
273
274                         /* the second jabber connect stanza including login info*/
275                         size2 = 150 + strlen( hash ) + strlen(password) + strlen(resource);
276                         char stanza2[ size2 ];
277                         memset( stanza2, 0, size2 );
278                 
279                         sprintf( stanza2, 
280                                         "<iq id='123456789' type='set'><query xmlns='jabber:iq:auth'>"
281                                         "<username>%s</username><digest>%s</digest><resource>%s</resource></query></iq>",
282                                         username, hash, resource );
283         
284                         /* server acknowledges our existence, now see if we can login */
285                         if( session->state_machine->connecting == CONNECTING_2 ) {
286                                 //if( ! tcp_send( session->sock_obj, stanza2 )  ) {
287                                 if( socket_send( session->sock_id, stanza2 )  ) {
288                                         osrfLogWarning(OSRF_LOG_MARK, "error sending");
289                                         return 0;
290                                 }
291                         }
292
293                 }
294
295         } // not component
296
297
298         /* wait for reply */
299         //tcp_wait( session->sock_obj, connect_timeout );
300         socket_wait( session->sock_mgr, connect_timeout, session->sock_id );
301
302         if( session->state_machine->connected ) {
303                 /* yar! */
304                 return 1;
305         }
306
307         return 0;
308 }
309
310 // ---------------------------------------------------------------------------------
311 // TCP data callback. Shove the data into the push parser.
312 // ---------------------------------------------------------------------------------
313 //void grab_incoming( void * session, char* data ) {
314 void grab_incoming(void* blob, socket_manager* mgr, int sockid, char* data, int parent) {
315         transport_session* ses = (transport_session*) blob;
316         if( ! ses ) { return; }
317         xmlParseChunk(ses->parser_ctxt, data, strlen(data), 0);
318 }
319
320
321 void startElementHandler(
322         void *session, const xmlChar *name, const xmlChar **atts) {
323
324         transport_session* ses = (transport_session*) session;
325         if( ! ses ) { return; }
326
327         
328         if( strcmp( name, "message" ) == 0 ) {
329                 ses->state_machine->in_message = 1;
330                 buffer_add( ses->from_buffer, get_xml_attr( atts, "from" ) );
331                 buffer_add( ses->recipient_buffer, get_xml_attr( atts, "to" ) );
332                 buffer_add( ses->router_from_buffer, get_xml_attr( atts, "router_from" ) );
333                 buffer_add( ses->router_to_buffer, get_xml_attr( atts, "router_to" ) );
334                 buffer_add( ses->router_class_buffer, get_xml_attr( atts, "router_class" ) );
335                 buffer_add( ses->router_command_buffer, get_xml_attr( atts, "router_command" ) );
336                 char* broadcast = get_xml_attr( atts, "broadcast" );
337                 if( broadcast )
338                         ses->router_broadcast = atoi( broadcast );
339
340                 return;
341         }
342
343         if( ses->state_machine->in_message ) {
344
345                 if( strcmp( name, "body" ) == 0 ) {
346                         ses->state_machine->in_message_body = 1;
347                         return;
348                 }
349         
350                 if( strcmp( name, "subject" ) == 0 ) {
351                         ses->state_machine->in_subject = 1;
352                         return;
353                 }
354         
355                 if( strcmp( name, "thread" ) == 0 ) {
356                         ses->state_machine->in_thread = 1;
357                         return;
358                 }
359
360         }
361
362         if( strcmp( name, "presence" ) == 0 ) {
363                 ses->state_machine->in_presence = 1;
364                 buffer_add( ses->from_buffer, get_xml_attr( atts, "from" ) );
365                 buffer_add( ses->recipient_buffer, get_xml_attr( atts, "to" ) );
366                 return;
367         }
368
369         if( strcmp( name, "status" ) == 0 ) {
370                 ses->state_machine->in_status = 1;
371                 return;
372         }
373
374
375         if( strcmp( name, "stream:error" ) == 0 ) {
376                 ses->state_machine->in_error = 1;
377                 osrfLogWarning(  OSRF_LOG_MARK, "Received <stream:error> message from Jabber server" );
378                 return;
379         }
380
381
382         /* first server response from a connect attempt */
383         if( strcmp( name, "stream:stream" ) == 0 ) {
384                 if( ses->state_machine->connecting == CONNECTING_1 ) {
385                         ses->state_machine->connecting = CONNECTING_2;
386                         buffer_add( ses->session_id, get_xml_attr(atts, "id") );
387                 }
388         }
389
390         if( strcmp( name, "handshake" ) == 0 ) {
391                 ses->state_machine->connected = 1;
392                 ses->state_machine->connecting = 0;
393                 return;
394         }
395
396
397         if( strcmp( name, "error" ) == 0 ) {
398                 ses->state_machine->in_message_error = 1;
399                 buffer_add( ses->message_error_type, get_xml_attr( atts, "type" ) );
400                 ses->message_error_code = atoi( get_xml_attr( atts, "code" ) );
401                 osrfLogInfo( OSRF_LOG_MARK,  "Received <error> message with type %s and code %s", 
402                         get_xml_attr( atts, "type"), get_xml_attr( atts, "code") );
403                 return;
404         }
405
406         if( strcmp( name, "iq" ) == 0 ) {
407                 ses->state_machine->in_iq = 1;
408
409                 if( strcmp( get_xml_attr(atts, "type"), "result") == 0 
410                                 && ses->state_machine->connecting == CONNECTING_2 ) {
411                         ses->state_machine->connected = 1;
412                         ses->state_machine->connecting = 0;
413                         return;
414                 }
415
416                 if( strcmp( get_xml_attr(atts, "type"), "error") == 0 ) {
417                         osrfLogWarning( OSRF_LOG_MARK,  "Error connecting to jabber" );
418                         return;
419                 }
420         }
421 }
422
423 char* get_xml_attr( const xmlChar** atts, char* attr_name ) {
424         int i;
425         if (atts != NULL) {
426                 for(i = 0;(atts[i] != NULL);i++) {
427                         if( strcmp( atts[i++], attr_name ) == 0 ) {
428                                 if( atts[i] != NULL ) {
429                                         return (char*) atts[i];
430                                 }
431                         }
432                 }
433         }
434         return NULL;
435 }
436
437
438 // ------------------------------------------------------------------
439 // See which tags are ending
440 // ------------------------------------------------------------------
441 void endElementHandler( void *session, const xmlChar *name) {
442         transport_session* ses = (transport_session*) session;
443         if( ! ses ) { return; }
444
445         if( strcmp( name, "message" ) == 0 ) {
446
447
448                 /* pass off the message info the callback */
449                 if( ses->message_callback ) {
450
451                         /* here it's ok to pass in the raw buffers because
452                                 message_init allocates new space for the chars 
453                                 passed in */
454                         transport_message* msg =  message_init( 
455                                 ses->body_buffer->buf, 
456                                 ses->subject_buffer->buf,
457                                 ses->thread_buffer->buf, 
458                                 ses->recipient_buffer->buf, 
459                                 ses->from_buffer->buf );
460
461                         message_set_router_info( msg, 
462                                 ses->router_from_buffer->buf, 
463                                 ses->router_to_buffer->buf, 
464                                 ses->router_class_buffer->buf,
465                                 ses->router_command_buffer->buf,
466                                 ses->router_broadcast );
467
468                         if( ses->message_error_type->n_used > 0 ) {
469                                 set_msg_error( msg, ses->message_error_type->buf, ses->message_error_code );
470                         }
471
472                         if( msg == NULL ) { return; }
473                         ses->message_callback( ses->user_data, msg );
474                 }
475
476                 ses->state_machine->in_message = 0;
477                 reset_session_buffers( session );
478                 return;
479         }
480         
481         if( strcmp( name, "body" ) == 0 ) {
482                 ses->state_machine->in_message_body = 0;
483                 return;
484         }
485
486         if( strcmp( name, "subject" ) == 0 ) {
487                 ses->state_machine->in_subject = 0;
488                 return;
489         }
490
491         if( strcmp( name, "thread" ) == 0 ) {
492                 ses->state_machine->in_thread = 0;
493                 return;
494         }
495         
496         if( strcmp( name, "iq" ) == 0 ) {
497                 ses->state_machine->in_iq = 0;
498                 if( ses->message_error_code > 0 ) {
499                         osrfLogWarning( OSRF_LOG_MARK,  "Error in IQ packet: code %d",  ses->message_error_code );
500                         osrfLogWarning( OSRF_LOG_MARK,  "Error 401 means not authorized" );
501                 }
502                 reset_session_buffers( session );
503                 return;
504         }
505
506         if( strcmp( name, "presence" ) == 0 ) {
507                 ses->state_machine->in_presence = 0;
508                 /*
509                 if( ses->presence_callback ) {
510                         // call the callback with the status, etc.
511                 }
512                 */
513                 reset_session_buffers( session );
514                 return;
515         }
516
517         if( strcmp( name, "status" ) == 0 ) {
518                 ses->state_machine->in_status = 0;
519                 return;
520         }
521
522         if( strcmp( name, "error" ) == 0 ) {
523                 ses->state_machine->in_message_error = 0;
524                 return;
525         }
526
527         if( strcmp( name, "error:error" ) == 0 ) {
528                 ses->state_machine->in_error = 0;
529                 return;
530         }
531 }
532
533 int reset_session_buffers( transport_session* ses ) {
534         buffer_reset( ses->body_buffer );
535         buffer_reset( ses->subject_buffer );
536         buffer_reset( ses->thread_buffer );
537         buffer_reset( ses->from_buffer );
538         buffer_reset( ses->recipient_buffer );
539         buffer_reset( ses->router_from_buffer );
540         buffer_reset( ses->router_to_buffer );
541         buffer_reset( ses->router_class_buffer );
542         buffer_reset( ses->router_command_buffer );
543         buffer_reset( ses->message_error_type );
544         buffer_reset( ses->session_id );
545
546         return 1;
547 }
548
549 // ------------------------------------------------------------------
550 // takes data out of the body of the message and pushes it into
551 // the appropriate buffer
552 // ------------------------------------------------------------------
553 void characterHandler(
554                 void *session, const xmlChar *ch, int len) {
555
556         char data[len+1];
557         memset( data, 0, len+1 );
558         strncpy( data, (char*) ch, len );
559         data[len] = 0;
560
561         //printf( "Handling characters: %s\n", data );
562         transport_session* ses = (transport_session*) session;
563         if( ! ses ) { return; }
564
565         /* set the various message parts */
566         if( ses->state_machine->in_message ) {
567
568                 if( ses->state_machine->in_message_body ) {
569                         buffer_add( ses->body_buffer, data );
570                 }
571
572                 if( ses->state_machine->in_subject ) {
573                         buffer_add( ses->subject_buffer, data );
574                 }
575
576                 if( ses->state_machine->in_thread ) {
577                         buffer_add( ses->thread_buffer, data );
578                 }
579         }
580
581         /* set the presence status */
582         if( ses->state_machine->in_presence && ses->state_machine->in_status ) {
583                 buffer_add( ses->status_buffer, data );
584         }
585
586         if( ses->state_machine->in_error ) {
587                 /* for now... */
588                 osrfLogWarning( OSRF_LOG_MARK,  "ERROR Xml fragment: %s\n", ch );
589         }
590
591 }
592
593 /* XXX change to warning handlers */
594 void  parseWarningHandler( void *session, const char* msg, ... ) {
595
596         va_list args;
597         va_start(args, msg);
598         fprintf(stdout, "transport_session XML WARNING");
599         vfprintf(stdout, msg, args);
600         va_end(args);
601         fprintf(stderr, "XML WARNING: %s\n", msg ); 
602 }
603
604 void  parseErrorHandler( void *session, const char* msg, ... ){
605
606         va_list args;
607         va_start(args, msg);
608         fprintf(stdout, "transport_session XML ERROR");
609         vfprintf(stdout, msg, args);
610         va_end(args);
611         fprintf(stderr, "XML ERROR: %s\n", msg ); 
612
613 }
614
615 int session_disconnect( transport_session* session ) {
616         if( session == NULL ) { return 0; }
617         //tcp_send( session->sock_obj, "</stream:stream>");
618         socket_send(session->sock_id, "</stream:stream>");
619         socket_disconnect(session->sock_mgr, session->sock_id);
620         return 0;
621         //return tcp_disconnect( session->sock_obj );
622 }
623