moved to the utils dir
[OpenSRF.git] / include / opensrf / transport_session.h
1 // ---------------------------------------------------------------------------------
2 // Manages the Jabber session.  Data is taken from the TCP object and pushed into
3 // a SAX push parser as it arrives.  When key Jabber documetn elements are met, 
4 // logic ensues.
5 // ---------------------------------------------------------------------------------
6 #include "opensrf/transport_socket.h"
7 #include "opensrf/transport_message.h"
8 #include "opensrf/generic_utils.h"
9
10 #include "sha.h"
11
12 #include <string.h>
13 #include <libxml/globals.h>
14 #include <libxml/xmlerror.h>
15 #include <libxml/parser.h>
16 #include <libxml/parserInternals.h> /* only for xmlNewInputFromFile() */
17 #include <libxml/tree.h>
18 #include <libxml/debugXML.h>
19 #include <libxml/xmlmemory.h>
20
21 #ifndef TRANSPORT_SESSION_H
22 #define TRANSPORT_SESSION_H
23
24 #define CONNECTING_1 1 /* just starting the connection to Jabber */
25 #define CONNECTING_2 2 /* First <stream> packet sent and <stream> packet received from server */
26
27 /* Note. these are growing buffers, so all that's necessary is a sane starting point */
28 #define JABBER_BODY_BUFSIZE             4096
29 #define JABBER_SUBJECT_BUFSIZE  64      
30 #define JABBER_THREAD_BUFSIZE           64      
31 #define JABBER_JID_BUFSIZE                      64      
32 #define JABBER_STATUS_BUFSIZE           16 
33
34 // ---------------------------------------------------------------------------------
35 // Takes data from the socket handler and pushes it directly into the push parser
36 // ---------------------------------------------------------------------------------
37 void grab_incoming( void * session, char* data );
38
39 // ---------------------------------------------------------------------------------
40 // Callback for handling the startElement event.  Much of the jabber logic occurs
41 // in this and the characterHandler callbacks.
42 // Here we check for the various top level jabber elements: body, iq, etc.
43 // ---------------------------------------------------------------------------------
44 void startElementHandler( 
45                 void *session, const xmlChar *name, const xmlChar **atts);
46
47 // ---------------------------------------------------------------------------------
48 // Callback for handling the endElement event.  Updates the Jabber state machine
49 // to let us know the element is over.
50 // ---------------------------------------------------------------------------------
51 void endElementHandler( void *session, const xmlChar *name);
52
53 // ---------------------------------------------------------------------------------
54 // This is where we extract XML text content.  In particular, this is useful for
55 // extracting Jabber message bodies.
56 // ---------------------------------------------------------------------------------
57 void characterHandler(
58                 void *session, const xmlChar *ch, int len);
59
60 void  parseWarningHandler( void *session, const char* msg, ... );
61 void  parseErrorHandler( void *session, const char* msg, ... );
62
63 // ---------------------------------------------------------------------------------
64 // Tells the SAX parser which functions will be used as event callbacks
65 // ---------------------------------------------------------------------------------
66 static xmlSAXHandler SAXHandlerStruct = {
67    NULL,                                                        /* internalSubset */
68    NULL,                                                        /* isStandalone */
69    NULL,                                                        /* hasInternalSubset */
70    NULL,                                                        /* hasExternalSubset */
71    NULL,                                                        /* resolveEntity */
72    NULL,                                                        /* getEntity */
73    NULL,                                                        /* entityDecl */
74    NULL,                                                        /* notationDecl */
75    NULL,                                                        /* attributeDecl */
76    NULL,                                                        /* elementDecl */
77    NULL,                                                        /* unparsedEntityDecl */
78    NULL,                                                        /* setDocumentLocator */
79    NULL,                                                        /* startDocument */
80    NULL,                                                        /* endDocument */
81         startElementHandler,            /* startElement */
82         endElementHandler,              /* endElement */
83    NULL,                                                        /* reference */
84         characterHandler,                       /* characters */
85    NULL,                                                        /* ignorableWhitespace */
86    NULL,                                                        /* processingInstruction */
87    NULL,                                                        /* comment */
88    parseWarningHandler,         /* xmlParserWarning */
89    parseErrorHandler,           /* xmlParserError */
90    NULL,                                                        /* xmlParserFatalError : unused */
91    NULL,                                                        /* getParameterEntity */
92    NULL,                                                        /* cdataBlock; */
93    NULL,                                                        /* externalSubset; */
94    1,
95    NULL,
96    NULL,                                                        /* startElementNs */
97    NULL,                                                        /* endElementNs */
98         NULL                                                    /* xmlStructuredErrorFunc */
99 };
100
101 // ---------------------------------------------------------------------------------
102 // Our SAX handler pointer.
103 // ---------------------------------------------------------------------------------
104 static const xmlSAXHandlerPtr SAXHandler = &SAXHandlerStruct;
105
106 // ---------------------------------------------------------------------------------
107 // Jabber state machine.  This is how we know where we are in the Jabber
108 // conversation.
109 // ---------------------------------------------------------------------------------
110 struct jabber_state_machine_struct {
111         int connected;
112         int connecting;
113         int in_message;
114         int in_message_body;
115         int in_thread;
116         int in_subject;
117         int in_error;
118         int in_message_error;
119         int in_iq;
120         int in_presence;
121         int in_status;
122 };
123 typedef struct jabber_state_machine_struct jabber_machine;
124
125
126 enum TRANSPORT_AUTH_TYPE { AUTH_PLAIN, AUTH_DIGEST };
127
128 // ---------------------------------------------------------------------------------
129 // Transport session.  This maintains all the various parts of a session
130 // ---------------------------------------------------------------------------------
131 struct transport_session_struct {
132
133         /* our socket connection */
134         transport_socket* sock_obj;
135         /* our Jabber state machine */
136         jabber_machine* state_machine;
137         /* our SAX push parser context */
138         xmlParserCtxtPtr parser_ctxt;
139
140         /* our text buffers for holding text data */
141         growing_buffer* body_buffer;
142         growing_buffer* subject_buffer;
143         growing_buffer* thread_buffer;
144         growing_buffer* from_buffer;
145         growing_buffer* recipient_buffer;
146         growing_buffer* status_buffer;
147         growing_buffer* message_error_type;
148         growing_buffer* session_id;
149         int message_error_code;
150
151         /* for OILS extenstions */
152         growing_buffer* router_to_buffer;
153         growing_buffer* router_from_buffer;
154         growing_buffer* router_class_buffer;
155         growing_buffer* router_command_buffer;
156         int router_broadcast;
157
158         /* this can be anything.  It will show up in the 
159                 callbacks for your convenience. We will *not*
160            deallocate whatever this is when we're done.  That's
161                 your job.
162          */
163         void* user_data;
164
165         int component; /* true if we're a component */
166
167         /* the Jabber message callback */
168         void (*message_callback) ( void* user_data, transport_message* msg );
169         //void (iq_callback) ( void* user_data, transport_iq_message* iq );
170 };
171 typedef struct transport_session_struct transport_session;
172
173
174 // ------------------------------------------------------------------
175 // Allocates and initializes the necessary transport session
176 // data structures.
177 // ------------------------------------------------------------------
178 transport_session* init_transport( char* server, int port, void* user_data, int component );
179
180 // ------------------------------------------------------------------
181 // Returns the value of the given XML attribute
182 // The xmlChar** construct is commonly returned from SAX event
183 // handlers.  Pass that in with the name of the attribute you want
184 // to retrieve.
185 // ------------------------------------------------------------------
186 char* get_xml_attr( const xmlChar** atts, char* attr_name );
187
188 // ------------------------------------------------------------------
189 // Waits  at most 'timeout' seconds  for data to arrive from the 
190 // TCP handler. A timeout of -1 means to wait indefinitely.
191 // ------------------------------------------------------------------
192 int session_wait( transport_session* session, int timeout );
193
194 // ---------------------------------------------------------------------------------
195 // Sends the given Jabber message
196 // ---------------------------------------------------------------------------------
197 int session_send_msg( transport_session* session, transport_message* msg );
198
199 // ---------------------------------------------------------------------------------
200 // Returns 1 if this session is connected to the jabber server. 0 otherwise
201 // ---------------------------------------------------------------------------------
202 int session_connected( transport_session* );
203
204 // ------------------------------------------------------------------
205 // Deallocates session memory
206 // ------------------------------------------------------------------
207 int session_free( transport_session* session );
208
209 // ------------------------------------------------------------------
210 // Connects to the Jabber server.  Waits at most connect_timeout
211 // seconds before failing
212 // ------------------------------------------------------------------
213 int session_connect( transport_session* session, 
214                 const char* username, const char* password, 
215                 const char* resource, int connect_timeout, 
216                 enum TRANSPORT_AUTH_TYPE auth_type );
217
218 int session_disconnect( transport_session* session );
219
220 int reset_session_buffers( transport_session* session );
221
222 #endif