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