]> git.evergreen-ils.org Git - Evergreen.git/blob - OpenSRF/src/libtransport/transport_session.h
373ec1e7e9f84760264aa1ad7bbb671859df8f16
[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
12 #include "sha.h"
13
14 #include <string.h>
15 #include <libxml/globals.h>
16 #include <libxml/xmlerror.h>
17 #include <libxml/parser.h>
18 #include <libxml/parserInternals.h> /* only for xmlNewInputFromFile() */
19 #include <libxml/tree.h>
20 #include <libxml/debugXML.h>
21 #include <libxml/xmlmemory.h>
22
23 #ifndef TRANSPORT_SESSION_H
24 #define TRANSPORT_SESSION_H
25
26 #define CONNECTING_1 1 /* just starting the connection to Jabber */
27 #define CONNECTING_2 2 /* First <stream> packet sent and <stream> packet received from server */
28
29 /* Note. these are growing buffers, so all that's necessary is a sane starting point */
30 #define JABBER_BODY_BUFSIZE             4096
31 #define JABBER_SUBJECT_BUFSIZE  64      
32 #define JABBER_THREAD_BUFSIZE           64      
33 #define JABBER_JID_BUFSIZE                      64      
34 #define JABBER_STATUS_BUFSIZE           16 
35
36 // ---------------------------------------------------------------------------------
37 // Takes data from the socket handler and pushes it directly into the push parser
38 // ---------------------------------------------------------------------------------
39 void grab_incoming( void * session, char* data );
40
41 // ---------------------------------------------------------------------------------
42 // Callback for handling the startElement event.  Much of the jabber logic occurs
43 // in this and the characterHandler callbacks.
44 // Here we check for the various top level jabber elements: body, iq, etc.
45 // ---------------------------------------------------------------------------------
46 void startElementHandler( 
47                 void *session, const xmlChar *name, const xmlChar **atts);
48
49 // ---------------------------------------------------------------------------------
50 // Callback for handling the endElement event.  Updates the Jabber state machine
51 // to let us know the element is over.
52 // ---------------------------------------------------------------------------------
53 void endElementHandler( void *session, const xmlChar *name);
54
55 // ---------------------------------------------------------------------------------
56 // This is where we extract XML text content.  In particular, this is useful for
57 // extracting Jabber message bodies.
58 // ---------------------------------------------------------------------------------
59 void characterHandler(
60                 void *session, const xmlChar *ch, int len);
61
62 void  parseWarningHandler( void *session, const char* msg, ... );
63 void  parseErrorHandler( void *session, const char* msg, ... );
64
65 // ---------------------------------------------------------------------------------
66 // Tells the SAX parser which functions will be used as event callbacks
67 // ---------------------------------------------------------------------------------
68 static xmlSAXHandler SAXHandlerStruct = {
69    NULL,                                                        /* internalSubset */
70    NULL,                                                        /* isStandalone */
71    NULL,                                                        /* hasInternalSubset */
72    NULL,                                                        /* hasExternalSubset */
73    NULL,                                                        /* resolveEntity */
74    NULL,                                                        /* getEntity */
75    NULL,                                                        /* entityDecl */
76    NULL,                                                        /* notationDecl */
77    NULL,                                                        /* attributeDecl */
78    NULL,                                                        /* elementDecl */
79    NULL,                                                        /* unparsedEntityDecl */
80    NULL,                                                        /* setDocumentLocator */
81    NULL,                                                        /* startDocument */
82    NULL,                                                        /* endDocument */
83         startElementHandler,            /* startElement */
84         endElementHandler,              /* endElement */
85    NULL,                                                        /* reference */
86         characterHandler,                       /* characters */
87    NULL,                                                        /* ignorableWhitespace */
88    NULL,                                                        /* processingInstruction */
89    NULL,                                                        /* comment */
90    parseWarningHandler,         /* xmlParserWarning */
91    parseErrorHandler,           /* xmlParserError */
92    NULL,                                                        /* xmlParserFatalError : unused */
93    NULL,                                                        /* getParameterEntity */
94    NULL,                                                        /* cdataBlock; */
95    NULL,                                                        /* externalSubset; */
96    1,
97    NULL,
98    NULL,                                                        /* startElementNs */
99    NULL,                                                        /* endElementNs */
100         NULL                                                    /* xmlStructuredErrorFunc */
101 };
102
103 // ---------------------------------------------------------------------------------
104 // Our SAX handler pointer.
105 // ---------------------------------------------------------------------------------
106 static const xmlSAXHandlerPtr SAXHandler = &SAXHandlerStruct;
107
108 // ---------------------------------------------------------------------------------
109 // Jabber state machine.  This is how we know where we are in the Jabber
110 // conversation.
111 // ---------------------------------------------------------------------------------
112 struct jabber_state_machine_struct {
113         int connected;
114         int connecting;
115         int in_message;
116         int in_message_body;
117         int in_thread;
118         int in_subject;
119         int in_error;
120         int in_message_error;
121         int in_iq;
122         int in_presence;
123         int in_status;
124 };
125 typedef struct jabber_state_machine_struct jabber_machine;
126
127
128 enum TRANSPORT_AUTH_TYPE { AUTH_PLAIN, AUTH_DIGEST };
129
130 // ---------------------------------------------------------------------------------
131 // Transport session.  This maintains all the various parts of a session
132 // ---------------------------------------------------------------------------------
133 struct transport_session_struct {
134
135         /* our socket connection */
136         transport_socket* sock_obj;
137         /* our Jabber state machine */
138         jabber_machine* state_machine;
139         /* our SAX push parser context */
140         xmlParserCtxtPtr parser_ctxt;
141
142         /* our text buffers for holding text data */
143         growing_buffer* body_buffer;
144         growing_buffer* subject_buffer;
145         growing_buffer* thread_buffer;
146         growing_buffer* from_buffer;
147         growing_buffer* recipient_buffer;
148         growing_buffer* status_buffer;
149         growing_buffer* message_error_type;
150         growing_buffer* session_id;
151         int message_error_code;
152
153         /* for OILS extenstions */
154         growing_buffer* router_to_buffer;
155         growing_buffer* router_from_buffer;
156         growing_buffer* router_class_buffer;
157         growing_buffer* router_command_buffer;
158         int router_broadcast;
159
160         /* this can be anything.  It will show up in the 
161                 callbacks for your convenience. Otherwise, it's
162                 left untouched.  */
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