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,
5 // ---------------------------------------------------------------------------------
6 #ifndef TRANSPORT_SESSION_H
7 #define TRANSPORT_SESSION_H
9 #include <opensrf/transport_message.h>
11 #include <opensrf/utils.h>
12 #include <opensrf/log.h>
13 #include <opensrf/socket_bundle.h>
18 #include <libxml/globals.h>
19 #include <libxml/xmlerror.h>
20 #include <libxml/parser.h>
21 #include <libxml/parserInternals.h> /* only for xmlNewInputFromFile() */
22 #include <libxml/tree.h>
23 #include <libxml/debugXML.h>
24 #include <libxml/xmlmemory.h>
30 #define CONNECTING_1 1 /* just starting the connection to Jabber */
31 #define CONNECTING_2 2 /* First <stream> packet sent and <stream> packet received from server */
33 /* Note. these are growing buffers, so all that's necessary is a sane starting point */
34 #define JABBER_BODY_BUFSIZE 4096
35 #define JABBER_SUBJECT_BUFSIZE 64
36 #define JABBER_THREAD_BUFSIZE 64
37 #define JABBER_JID_BUFSIZE 64
38 #define JABBER_STATUS_BUFSIZE 16
40 // ---------------------------------------------------------------------------------
41 // Callback for handling the startElement event. Much of the jabber logic occurs
42 // in this and the characterHandler callbacks.
43 // Here we check for the various top level jabber elements: body, iq, etc.
44 // ---------------------------------------------------------------------------------
45 void startElementHandler(
46 void *session, const xmlChar *name, const xmlChar **atts);
48 // ---------------------------------------------------------------------------------
49 // Callback for handling the endElement event. Updates the Jabber state machine
50 // to let us know the element is over.
51 // ---------------------------------------------------------------------------------
52 void endElementHandler( void *session, const xmlChar *name);
54 // ---------------------------------------------------------------------------------
55 // This is where we extract XML text content. In particular, this is useful for
56 // extracting Jabber message bodies.
57 // ---------------------------------------------------------------------------------
58 void characterHandler(
59 void *session, const xmlChar *ch, int len);
61 void parseWarningHandler( void *session, const char* msg, ... );
62 void parseErrorHandler( void *session, const char* msg, ... );
64 // ---------------------------------------------------------------------------------
65 // Tells the SAX parser which functions will be used as event callbacks
66 // ---------------------------------------------------------------------------------
67 static xmlSAXHandler SAXHandlerStruct = {
68 NULL, /* internalSubset */
69 NULL, /* isStandalone */
70 NULL, /* hasInternalSubset */
71 NULL, /* hasExternalSubset */
72 NULL, /* resolveEntity */
74 NULL, /* entityDecl */
75 NULL, /* notationDecl */
76 NULL, /* attributeDecl */
77 NULL, /* elementDecl */
78 NULL, /* unparsedEntityDecl */
79 NULL, /* setDocumentLocator */
80 NULL, /* startDocument */
81 NULL, /* endDocument */
82 startElementHandler, /* startElement */
83 endElementHandler, /* endElement */
85 characterHandler, /* characters */
86 NULL, /* ignorableWhitespace */
87 NULL, /* processingInstruction */
89 parseWarningHandler, /* xmlParserWarning */
90 parseErrorHandler, /* xmlParserError */
91 NULL, /* xmlParserFatalError : unused */
92 NULL, /* getParameterEntity */
93 NULL, /* cdataBlock; */
94 NULL, /* externalSubset; */
97 NULL, /* startElementNs */
98 NULL, /* endElementNs */
99 NULL /* xmlStructuredErrorFunc */
102 // ---------------------------------------------------------------------------------
103 // Our SAX handler pointer.
104 // ---------------------------------------------------------------------------------
105 static const xmlSAXHandlerPtr SAXHandler = &SAXHandlerStruct;
107 // ---------------------------------------------------------------------------------
108 // Jabber state machine. This is how we know where we are in the Jabber
110 // ---------------------------------------------------------------------------------
111 struct jabber_state_machine_struct {
119 int in_message_error;
124 typedef struct jabber_state_machine_struct jabber_machine;
127 enum TRANSPORT_AUTH_TYPE { AUTH_PLAIN, AUTH_DIGEST };
129 // ---------------------------------------------------------------------------------
130 // Transport session. This maintains all the various parts of a session
131 // ---------------------------------------------------------------------------------
132 struct transport_session_struct {
134 /* our socket connection */
135 socket_manager* sock_mgr;
137 /* our Jabber state machine */
138 jabber_machine* state_machine;
139 /* our SAX push parser context */
140 xmlParserCtxtPtr parser_ctxt;
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;
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 growing_buffer* osrf_xid_buffer;
159 int router_broadcast;
161 /* this can be anything. It will show up in the
162 callbacks for your convenience. Otherwise, it's
171 int component; /* true if we're a component */
173 /* the Jabber message callback */
174 void (*message_callback) ( void* user_data, transport_message* msg );
175 //void (iq_callback) ( void* user_data, transport_iq_message* iq );
177 typedef struct transport_session_struct transport_session;
180 // ------------------------------------------------------------------
181 // Allocates and initializes the necessary transport session
183 // If port > 0, then this session uses TCP connection. Otherwise,
184 // if unix_path != NULL, it uses a UNIX domain socket.
185 // ------------------------------------------------------------------
186 transport_session* init_transport( const char* server, int port,
187 const char* unix_path, void* user_data, int component );
189 // ------------------------------------------------------------------
190 // Waits at most 'timeout' seconds for data to arrive from the
191 // TCP handler. A timeout of -1 means to wait indefinitely.
192 // ------------------------------------------------------------------
193 int session_wait( transport_session* session, int timeout );
195 // ---------------------------------------------------------------------------------
196 // Sends the given Jabber message
197 // ---------------------------------------------------------------------------------
198 int session_send_msg( transport_session* session, transport_message* msg );
200 // ---------------------------------------------------------------------------------
201 // Returns 1 if this session is connected to the jabber server. 0 otherwise
202 // ---------------------------------------------------------------------------------
203 int session_connected( transport_session* );
205 // ------------------------------------------------------------------
206 // Deallocates session memory
207 // ------------------------------------------------------------------
208 int session_free( transport_session* session );
210 // ------------------------------------------------------------------
211 // Connects to the Jabber server. Waits at most connect_timeout
212 // seconds before failing
213 // ------------------------------------------------------------------
214 int session_connect( transport_session* session,
215 const char* username, const char* password,
216 const char* resource, int connect_timeout,
217 enum TRANSPORT_AUTH_TYPE auth_type );
219 int session_disconnect( transport_session* session );