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 #include <opensrf/transport_message.h>
8 #include <opensrf/utils.h>
9 #include <opensrf/log.h>
10 #include <opensrf/socket_bundle.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>
23 #ifndef TRANSPORT_SESSION_H
24 #define TRANSPORT_SESSION_H
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 */
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
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);
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);
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);
57 void parseWarningHandler( void *session, const char* msg, ... );
58 void parseErrorHandler( void *session, const char* msg, ... );
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 */
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 */
81 characterHandler, /* characters */
82 NULL, /* ignorableWhitespace */
83 NULL, /* processingInstruction */
85 parseWarningHandler, /* xmlParserWarning */
86 parseErrorHandler, /* xmlParserError */
87 NULL, /* xmlParserFatalError : unused */
88 NULL, /* getParameterEntity */
89 NULL, /* cdataBlock; */
90 NULL, /* externalSubset; */
93 NULL, /* startElementNs */
94 NULL, /* endElementNs */
95 NULL /* xmlStructuredErrorFunc */
98 // ---------------------------------------------------------------------------------
99 // Our SAX handler pointer.
100 // ---------------------------------------------------------------------------------
101 static const xmlSAXHandlerPtr SAXHandler = &SAXHandlerStruct;
103 // ---------------------------------------------------------------------------------
104 // Jabber state machine. This is how we know where we are in the Jabber
106 // ---------------------------------------------------------------------------------
107 struct jabber_state_machine_struct {
115 int in_message_error;
120 typedef struct jabber_state_machine_struct jabber_machine;
123 enum TRANSPORT_AUTH_TYPE { AUTH_PLAIN, AUTH_DIGEST };
125 // ---------------------------------------------------------------------------------
126 // Transport session. This maintains all the various parts of a session
127 // ---------------------------------------------------------------------------------
128 struct transport_session_struct {
130 /* our socket connection */
131 socket_manager* sock_mgr;
133 /* our Jabber state machine */
134 jabber_machine* state_machine;
135 /* our SAX push parser context */
136 xmlParserCtxtPtr parser_ctxt;
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;
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;
157 /* this can be anything. It will show up in the
158 callbacks for your convenience. Otherwise, it's
167 int component; /* true if we're a component */
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 );
173 typedef struct transport_session_struct transport_session;
176 // ------------------------------------------------------------------
177 // Allocates and initializes the necessary transport session
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 );
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 );
191 // ---------------------------------------------------------------------------------
192 // Sends the given Jabber message
193 // ---------------------------------------------------------------------------------
194 int session_send_msg( transport_session* session, transport_message* msg );
196 // ---------------------------------------------------------------------------------
197 // Returns 1 if this session is connected to the jabber server. 0 otherwise
198 // ---------------------------------------------------------------------------------
199 int session_connected( transport_session* );
201 // ------------------------------------------------------------------
202 // Deallocates session memory
203 // ------------------------------------------------------------------
204 int session_free( transport_session* session );
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 );
215 int session_disconnect( transport_session* session );