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