]> git.evergreen-ils.org Git - OpenSRF.git/blob - include/opensrf/transport_session.h
Add compilation guard; prepare for #inclusion in C++
[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_message.h>
7
8 #include <opensrf/utils.h>
9 #include <opensrf/log.h>
10 #include <opensrf/socket_bundle.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 // Callback for handling the startElement event.  Much of the jabber logic occurs
38 // in this and the characterHandler callbacks.
39 // Here we check for the various top level jabber elements: body, iq, etc.
40 // ---------------------------------------------------------------------------------
41 void startElementHandler( 
42                 void *session, const xmlChar *name, const xmlChar **atts);
43
44 // ---------------------------------------------------------------------------------
45 // Callback for handling the endElement event.  Updates the Jabber state machine
46 // to let us know the element is over.
47 // ---------------------------------------------------------------------------------
48 void endElementHandler( void *session, const xmlChar *name);
49
50 // ---------------------------------------------------------------------------------
51 // This is where we extract XML text content.  In particular, this is useful for
52 // extracting Jabber message bodies.
53 // ---------------------------------------------------------------------------------
54 void characterHandler(
55                 void *session, const xmlChar *ch, int len);
56
57 void  parseWarningHandler( void *session, const char* msg, ... );
58 void  parseErrorHandler( void *session, const char* msg, ... );
59
60 // ---------------------------------------------------------------------------------
61 // Tells the SAX parser which functions will be used as event callbacks
62 // ---------------------------------------------------------------------------------
63 static xmlSAXHandler SAXHandlerStruct = {
64    NULL,                                                        /* internalSubset */
65    NULL,                                                        /* isStandalone */
66    NULL,                                                        /* hasInternalSubset */
67    NULL,                                                        /* hasExternalSubset */
68    NULL,                                                        /* resolveEntity */
69    NULL,                                                        /* getEntity */
70    NULL,                                                        /* entityDecl */
71    NULL,                                                        /* notationDecl */
72    NULL,                                                        /* attributeDecl */
73    NULL,                                                        /* elementDecl */
74    NULL,                                                        /* unparsedEntityDecl */
75    NULL,                                                        /* setDocumentLocator */
76    NULL,                                                        /* startDocument */
77    NULL,                                                        /* endDocument */
78         startElementHandler,            /* startElement */
79         endElementHandler,              /* endElement */
80    NULL,                                                        /* reference */
81         characterHandler,                       /* characters */
82    NULL,                                                        /* ignorableWhitespace */
83    NULL,                                                        /* processingInstruction */
84    NULL,                                                        /* comment */
85    parseWarningHandler,         /* xmlParserWarning */
86    parseErrorHandler,           /* xmlParserError */
87    NULL,                                                        /* xmlParserFatalError : unused */
88    NULL,                                                        /* getParameterEntity */
89    NULL,                                                        /* cdataBlock; */
90    NULL,                                                        /* externalSubset; */
91    1,
92    NULL,
93    NULL,                                                        /* startElementNs */
94    NULL,                                                        /* endElementNs */
95         NULL                                                    /* xmlStructuredErrorFunc */
96 };
97
98 // ---------------------------------------------------------------------------------
99 // Our SAX handler pointer.
100 // ---------------------------------------------------------------------------------
101 static const xmlSAXHandlerPtr SAXHandler = &SAXHandlerStruct;
102
103 // ---------------------------------------------------------------------------------
104 // Jabber state machine.  This is how we know where we are in the Jabber
105 // conversation.
106 // ---------------------------------------------------------------------------------
107 struct jabber_state_machine_struct {
108         int connected;
109         int connecting;
110         int in_message;
111         int in_message_body;
112         int in_thread;
113         int in_subject;
114         int in_error;
115         int in_message_error;
116         int in_iq;
117         int in_presence;
118         int in_status;
119 };
120 typedef struct jabber_state_machine_struct jabber_machine;
121
122
123 enum TRANSPORT_AUTH_TYPE { AUTH_PLAIN, AUTH_DIGEST };
124
125 // ---------------------------------------------------------------------------------
126 // Transport session.  This maintains all the various parts of a session
127 // ---------------------------------------------------------------------------------
128 struct transport_session_struct {
129
130         /* our socket connection */
131         socket_manager* sock_mgr;
132
133         /* our Jabber state machine */
134         jabber_machine* state_machine;
135         /* our SAX push parser context */
136         xmlParserCtxtPtr parser_ctxt;
137
138         /* our text buffers for holding text data */
139         growing_buffer* body_buffer;
140         growing_buffer* subject_buffer;
141         growing_buffer* thread_buffer;
142         growing_buffer* from_buffer;
143         growing_buffer* recipient_buffer;
144         growing_buffer* status_buffer;
145         growing_buffer* message_error_type;
146         growing_buffer* session_id;
147         int message_error_code;
148
149         /* for OILS extenstions */
150         growing_buffer* router_to_buffer;
151         growing_buffer* router_from_buffer;
152         growing_buffer* router_class_buffer;
153         growing_buffer* router_command_buffer;
154         growing_buffer* osrf_xid_buffer;
155         int router_broadcast;
156
157         /* this can be anything.  It will show up in the 
158                 callbacks for your convenience. Otherwise, it's
159                 left untouched.  */
160         void* user_data;
161
162         char* server;
163         char* unix_path;
164         int     port;
165         int sock_id;
166
167         int component; /* true if we're a component */
168
169         /* the Jabber message callback */
170         void (*message_callback) ( void* user_data, transport_message* msg );
171         //void (iq_callback) ( void* user_data, transport_iq_message* iq );
172 };
173 typedef struct transport_session_struct transport_session;
174
175
176 // ------------------------------------------------------------------
177 // Allocates and initializes the necessary transport session
178 // data structures.
179 // If port > 0, then this session uses  TCP connection.  Otherwise,
180 // if unix_path != NULL, it uses a UNIX domain socket.
181 // ------------------------------------------------------------------
182 transport_session* init_transport( const char* server, int port, 
183         const char* unix_path, void* user_data, int component );
184
185 // ------------------------------------------------------------------
186 // Waits  at most 'timeout' seconds  for data to arrive from the 
187 // TCP handler. A timeout of -1 means to wait indefinitely.
188 // ------------------------------------------------------------------
189 int session_wait( transport_session* session, int timeout );
190
191 // ---------------------------------------------------------------------------------
192 // Sends the given Jabber message
193 // ---------------------------------------------------------------------------------
194 int session_send_msg( transport_session* session, transport_message* msg );
195
196 // ---------------------------------------------------------------------------------
197 // Returns 1 if this session is connected to the jabber server. 0 otherwise
198 // ---------------------------------------------------------------------------------
199 int session_connected( transport_session* );
200
201 // ------------------------------------------------------------------
202 // Deallocates session memory
203 // ------------------------------------------------------------------
204 int session_free( transport_session* session );
205
206 // ------------------------------------------------------------------
207 // Connects to the Jabber server.  Waits at most connect_timeout
208 // seconds before failing
209 // ------------------------------------------------------------------
210 int session_connect( transport_session* session, 
211                 const char* username, const char* password, 
212                 const char* resource, int connect_timeout, 
213                 enum TRANSPORT_AUTH_TYPE auth_type );
214
215 int session_disconnect( transport_session* session );
216
217 #endif