fixed some a bug in jserver, added some debug lines
[Evergreen.git] / OpenSRF / src / jserver / jserver-c.c
1 #include "jserver-c.h"
2
3 /* ------------------------------------------------
4         some pre-packaged Jabber XML
5         ------------------------------------------------ */
6 static const char* xml_parse_error = "<stream:stream xmlns:stream='http://etherx.jabber.org/streams'" 
7         "version='1.0'><stream:error xmlns:stream='http://etherx.jabber.org/streams'>"
8         "<xml-not-well-formed xmlns='urn:ietf:params:xml:ns:xmpp-streams'/>"
9         "<text xmlns='urn:ietf:params:xml:ns:xmpp-streams'>syntax error</text></stream:error></stream:stream>";
10
11 static const char* xml_login_ok = "<iq xmlns='jabber:client' id='asdfjkl' type='result'/>";
12
13
14 jserver* jserver_init() {
15         jserver* js                                     = safe_malloc(sizeof(jserver));
16         js->mgr                                         = safe_malloc(sizeof(socket_manager));
17         js->mgr->data_received  = &jserver_handle_request;
18         js->mgr->blob                           = js;
19         js->mgr->on_socket_closed       = &jserver_socket_closed;
20         js->client                                      = NULL;
21
22         return js;
23 }
24
25 void jserver_free(jserver* js) {
26         if(js == NULL) return;
27         jclient_node* node; 
28         while(js->client) {
29                 node = js->client->next;
30                 _jserver_remove_client_id(js, js->client->id);
31                 js->client = node;
32         }
33         socket_manager_free(js->mgr);
34         free(js);
35 }
36
37 void jserver_socket_closed(void* blob, int sock_id) {
38         jserver* js = (jserver*) blob;
39         if(js == NULL) return;
40         info_handler("Removing client %d - site closed socket",sock_id);
41         _jserver_remove_client_id(js, sock_id);
42 }
43
44 /* opens the inet and unix sockets that we're listening on */
45 int jserver_connect(jserver* js, int port, char* unix_path) {
46         if(js == NULL || js->mgr == NULL) return -1;
47         int status = 0;
48
49         if(port > 0) {
50                 status = socket_open_tcp_server(js->mgr, port);
51                 if(status == -1) return status;
52         }
53
54         if(unix_path != NULL) {
55                 status = socket_open_unix_server(js->mgr, unix_path);
56                 if(status == -1) return status;
57         }
58
59         return 0;
60 }
61
62 void _free_jclient_node(jclient_node* node) {
63         if(node == NULL) return;
64         free(node->addr);
65         jserver_session_free(node->session);
66         free(node);
67 }
68
69 /* allocates a new client node */
70 jclient_node* _new_jclient_node(int id) {
71         jclient_node* node = safe_malloc(sizeof(jclient_node));
72         node->id = id;
73         node->addr = NULL;
74         node->session = jserver_session_init();
75         node->session->blob = node;
76         node->session->on_msg_complete = &jserver_client_handle_msg; 
77         node->session->on_from_discovered = &jserver_client_from_found;
78         node->session->on_login_init = &jserver_client_login_init;
79         node->session->on_login_ok = &jserver_client_login_ok;
80         node->session->on_client_finish = &jserver_client_finish;
81         return node;
82 }
83
84 /* client has sent the end of it's session doc, we may now disconnect */
85 void jserver_client_finish(void* blob) {
86         jclient_node* node = (jclient_node*) blob;
87         if(node == NULL) return;
88         jserver_send_id(node->id, "</stream:stream>");
89         _jserver_remove_client(node->parent, node->addr);
90
91 }
92
93 void jserver_client_from_found(void* blob, char* from) {
94         jclient_node* node = (jclient_node*) blob;
95         if(node == NULL || from == NULL) return;
96
97         /* prevent duplicate login - kick off original */
98         _jserver_remove_client(node->parent, from);
99         info_handler("logged in: %s", from);
100         node->addr = strdup(from);
101 }
102
103 void jserver_client_login_init(void* blob, char* reply) {
104         debug_handler("here");
105         jclient_node* node = (jclient_node*) blob;
106         if(node == NULL || reply == NULL) return;
107         debug_handler("jserver handling login init");
108         jserver_send_id(node->id, reply);
109 }
110
111 void jserver_client_login_ok(void* blob) {
112         jclient_node* node = (jclient_node*) blob;
113         if(node == NULL) return;
114         info_handler("Client logging in ok => %d", node->id);
115         jserver_send_id(node->id, xml_login_ok);
116 }
117
118 void jserver_client_handle_msg( 
119         void* blob, char* xml, char* from, char* to ) {
120
121         jclient_node* node = (jclient_node*) blob;
122         if(node == NULL || xml == NULL || to == NULL) return;
123         int from_id = 0;
124
125         jclient_node* from_node = jserver_find_client(node->parent, from);
126         if(from_node)
127                 from_id = from_node->id;
128
129
130         debug_handler("Client %d received from %s message : %s", 
131                         node->id, from, xml );
132
133         jserver_send(node->parent, from_id, to, xml);
134 }
135
136 /* allocates a new client node and adds it to the set */
137 jclient_node* _jserver_add_client(jserver* js, int id) {
138         if(js == NULL) return NULL;
139         jclient_node* node = _new_jclient_node(id);
140         node->next = js->client;
141         js->client = node;
142         node->parent = js;
143         return node;
144 }
145
146
147 /* removes and frees a client node */
148 void _jserver_remove_client(jserver* js, char* addr) {
149         if(js == NULL || js->client == NULL || addr == NULL) return;
150
151         jclient_node* node = js->client;
152
153         if(node->addr && !strcmp(node->addr,addr)) {
154                 js->client = node->next;
155                 debug_handler("Removing the first jserver client");
156                 socket_disconnect(js->mgr, node->id);
157                 _free_jclient_node(node);
158                 return;
159         }
160
161         debug_handler("Searching for jclient to remove");
162         jclient_node* tail_node = node;
163         node = node->next;
164
165         while(node) {
166                 if(node->addr && !strcmp(node->addr,addr)) {
167                         tail_node->next = node->next;
168                         debug_handler("Removing a jserver client");
169                         socket_disconnect(js->mgr, node->id);
170                         _free_jclient_node(node);
171                         return;
172                 }
173                 tail_node = node;
174                 node = node->next;
175         }
176 }
177
178
179 /* removes and frees a client node */
180 void _jserver_remove_client_id(jserver* js, int id) {
181         if(js == NULL || js->client == NULL) return;
182
183         jclient_node* node = js->client;
184
185         if(node->id == id) {
186                 js->client = node->next;
187                 debug_handler("Removing the first jserver client");
188                 socket_disconnect(js->mgr, node->id);
189                 _free_jclient_node(node);
190                 return;
191         }
192
193         debug_handler("Searching for jclient to remove");
194         jclient_node* tail_node = node;
195         node = node->next;
196
197         while(node) {
198                 if(node->id == id) {
199                         tail_node->next = node->next;
200                         debug_handler("Removing a jserver client");
201                         socket_disconnect(js->mgr, node->id);
202                         _free_jclient_node(node);
203                         return;
204                 }
205                 tail_node = node;
206                 node = node->next;
207         }
208 }
209
210 /* finds a client node by addr */
211 jclient_node* jserver_find_client(jserver* js, char* addr) {
212         if(js == NULL || addr == NULL) return NULL;
213         jclient_node* node = js->client;
214         while(node) {
215                 if(node->addr && !strcmp(node->addr, addr)) 
216                         return node;
217                 node = node->next;
218         }
219         return NULL;
220 }
221
222 jclient_node* jserver_find_client_id(jserver* js, int id) {
223         if(js == NULL) return NULL;
224         jclient_node* node = js->client;
225         while(node) {
226                 if(node->id == id) 
227                         return node;
228                 node = node->next;
229         }
230         return NULL;
231 }
232
233 /* sends msg to client at 'to_addr' */
234 int jserver_send(jserver* js, int from_id, char* to_addr, const char* msg_xml) {
235         debug_handler("sending message to %s : %s", to_addr, msg_xml);
236         if(to_addr == NULL || msg_xml == NULL) return -1;
237
238         jclient_node* node = jserver_find_client(js, to_addr);
239
240         if(node == NULL) {
241                 info_handler("message to non-existent client %s", to_addr);
242                 if(from_id > 0) {
243                         jclient_node* from = jserver_find_client_id(js, from_id);
244
245                         if(from) {
246                                 info_handler("replying with error...");
247                                 char buf[2048];
248                                 memset(buf, 0, 2048);
249                                 snprintf(buf, 2047, "<message xmlns='jabber:client' type='error' from='%s' "
250                                         "to='%s'><error type='cancel' code='404'><item-not-found "
251                                         "xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>"
252                                         "<body>NOT ADDING BODY</body></message>", to_addr, from->addr );
253                                 jserver_send_id(from_id, buf);
254                         }
255                 }
256                 return -1;
257         }
258
259         return jserver_send_id(node->id, msg_xml);
260 }
261
262 int jserver_send_id(int client_id, const char* msg_xml) {
263         if(msg_xml == NULL || client_id < 1) return -1;
264         return socket_send(client_id, msg_xml );
265 }
266
267 /* waits for any incoming data */
268 int jserver_wait(jserver* js) {
269         if(js == NULL) return -1;
270         while(1) {
271                 if(socket_wait_all(js->mgr, -1) < 0)
272                         warning_handler(
273                                 "jserver_wait(): socket_wait_all() returned error");
274
275         }
276 }
277
278
279 int _jserver_push_client_data(jclient_node* node, char* data) {
280         if(node == NULL || data == NULL) return -1;
281         return jserver_session_push_data( node->session, data);
282 }
283
284 void jserver_handle_request(void* js_blob, 
285         socket_manager* mgr, int sock_id, char* data, int parent_id ) {
286
287         jserver* js = (jserver*) js_blob;
288
289         debug_handler("jsever received data from socket %d (parent %d)", sock_id, parent_id );
290
291         jclient_node* node = jserver_find_client_id(js, sock_id);
292         if(!node) {
293                 debug_handler("We have a new client connection, adding to list");
294                 node = _jserver_add_client(js, sock_id);
295         }
296
297         if(_jserver_push_client_data(node, data) == -1) {
298                 warning_handler("Client sent bad data, disconnecting...");
299                 jserver_send_id(node->id, xml_parse_error);
300                 _jserver_remove_client(js, node->addr);         
301
302         } else {
303                 debug_handler("Client data successfully parsed");
304         }
305
306 }
307
308
309
310