2
0

xmpp_component.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * $Id$
  3. *
  4. * XMPP Module
  5. * This file is part of Kamailio, a free SIP server.
  6. *
  7. * Copyright (C) 2006 Voice Sistem S.R.L.
  8. *
  9. * Kamailio is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version
  13. *
  14. * Kamailio is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. * Author: Andreea Spirea
  24. *
  25. */
  26. /*! \file
  27. * \brief Kamailio XMPP :: XMPP Component interface support
  28. * \ingroup xmpp
  29. */
  30. /*
  31. * An inbound SIP message:
  32. * from sip:user1@domain1 to sip:user2*domain2@gateway_domain
  33. * is translated to an XMPP message:
  34. * from user1*domain1@xmpp_domain to user2@domain2
  35. *
  36. * An inbound XMPP message:
  37. * from user1@domain1 to user2*domain2@xmpp_domain
  38. * is translated to a SIP message:
  39. * from sip:user1*domain1@gateway_domain to sip:user2@domain2
  40. *
  41. * Where '*' is the domain_separator, and gateway_domain and
  42. * xmpp_domain are defined below.
  43. */
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <errno.h>
  47. #include <string.h>
  48. #include "../../sr_module.h"
  49. #include "../../cfg/cfg_struct.h"
  50. #include "xmpp.h"
  51. #include "xmpp_api.h"
  52. #include "network.h"
  53. #include "xode.h"
  54. struct xmpp_private_data {
  55. int fd; /* socket */
  56. int running;
  57. };
  58. static int xode_send(int fd, xode x)
  59. {
  60. char *str = xode_to_str(x);
  61. int len = strlen(str);
  62. LM_DBG("xode_send [%s]\n", str);
  63. if (net_send(fd, str, len) != len) {
  64. LM_ERR("send() error: %s\n", strerror(errno));
  65. return -1;
  66. }
  67. /* should str be freed?!?! */
  68. return len;
  69. }
  70. static void stream_node_callback(int type, xode node, void *arg)
  71. {
  72. struct xmpp_private_data *priv = (struct xmpp_private_data *) arg;
  73. char *id, *hash, *tag;
  74. char buf[4096];
  75. xode x;
  76. LM_DBG("stream callback: %d: %s\n", type, node ? xode_get_name(node) : "n/a");
  77. switch (type) {
  78. case XODE_STREAM_ROOT:
  79. id = xode_get_attrib(node, "id");
  80. snprintf(buf, sizeof(buf), "%s%s", id, xmpp_password);
  81. hash = shahash(buf);
  82. x = xode_new_tag("handshake");
  83. xode_insert_cdata(x, hash, -1);
  84. xode_send(priv->fd, x);
  85. xode_free(x);
  86. break;
  87. case XODE_STREAM_NODE:
  88. tag = xode_get_name(node);
  89. if (!strcmp(tag, "handshake")) {
  90. LM_DBG("handshake succeeded\n");
  91. } else if (!strcmp(tag, "message")) {
  92. LM_DBG("XMPP IM received\n");
  93. char *from = xode_get_attrib(node, "from");
  94. char *to = xode_get_attrib(node, "to");
  95. char *type = xode_get_attrib(node, "type");
  96. xode body = xode_get_tag(node, "body");
  97. char *msg;
  98. if (!type)
  99. type = "chat";
  100. if (!strcmp(type, "error")) {
  101. LM_DBG("received message error stanza\n");
  102. goto out;
  103. }
  104. if (!from || !to || !body) {
  105. LM_DBG("invalid <message/> attributes\n");
  106. goto out;
  107. }
  108. if (!(msg = xode_get_data(body)))
  109. msg = "";
  110. xmpp_send_sip_msg(
  111. encode_uri_xmpp_sip(from),
  112. decode_uri_xmpp_sip(to),
  113. msg);
  114. } else if (!strcmp(tag, "presence")) {
  115. /* call presence callbacks */
  116. LM_DBG("XMPP Presence received\n");
  117. run_xmpp_callbacks(XMPP_RCV_PRESENCE, xode_to_str(node));
  118. }else if (!strcmp(tag, "iq")) {
  119. /* call presence callbacks */
  120. LM_DBG("XMPP IQ received\n");
  121. run_xmpp_callbacks(XMPP_RCV_IQ, xode_to_str(node));
  122. }
  123. break;
  124. case XODE_STREAM_ERROR:
  125. LM_ERR("stream error\n");
  126. /* fall-through */
  127. case XODE_STREAM_CLOSE:
  128. priv->running = 0;
  129. break;
  130. }
  131. out:
  132. xode_free(node);
  133. }
  134. /*!
  135. *
  136. */
  137. static int do_send_message_component(struct xmpp_private_data *priv,
  138. struct xmpp_pipe_cmd *cmd)
  139. {
  140. xode x;
  141. LM_DBG("do_send_message_component from=[%s] to=[%s] body=[%s]\n",
  142. cmd->from, cmd->to, cmd->body);
  143. x = xode_new_tag("message");
  144. xode_put_attrib(x, "id", cmd->id); // XXX
  145. xode_put_attrib(x, "from", encode_uri_sip_xmpp(cmd->from));
  146. xode_put_attrib(x, "to", decode_uri_sip_xmpp(cmd->to));
  147. xode_put_attrib(x, "type", "chat");
  148. xode_insert_cdata(xode_insert_tag(x, "body"), cmd->body, -1);
  149. xode_send(priv->fd, x);
  150. xode_free(x);
  151. /* missing error handling here ?!?!*/
  152. return 0;
  153. }
  154. static int do_send_bulk_message_component(struct xmpp_private_data *priv,
  155. struct xmpp_pipe_cmd *cmd)
  156. {
  157. int len;
  158. LM_DBG("do_send_bulk_message_component from=[%s] to=[%s] body=[%s]\n",
  159. cmd->from, cmd->to, cmd->body);
  160. len = strlen(cmd->body);
  161. if (net_send(priv->fd, cmd->body, len) != len) {
  162. LM_ERR("do_send_bulk_message_component: %s\n",strerror(errno));
  163. return -1;
  164. }
  165. return 0;
  166. }
  167. int xmpp_component_child_process(int data_pipe)
  168. {
  169. int fd, maxfd, rv;
  170. fd_set fdset;
  171. xode_pool pool;
  172. xode_stream stream;
  173. struct xmpp_private_data priv;
  174. struct xmpp_pipe_cmd *cmd;
  175. while (1) {
  176. fd = net_connect(xmpp_host, xmpp_port);
  177. if (fd < 0) {
  178. sleep(3);
  179. continue;
  180. }
  181. priv.fd = fd;
  182. priv.running = 1;
  183. pool = xode_pool_new();
  184. stream = xode_stream_new(pool, stream_node_callback, &priv);
  185. net_printf(fd,
  186. "<?xml version='1.0'?>"
  187. "<stream:stream xmlns='jabber:component:accept' to='%s' "
  188. "version='1.0' xmlns:stream='http://etherx.jabber.org/streams'>",
  189. xmpp_domain);
  190. while (priv.running) {
  191. FD_ZERO(&fdset);
  192. FD_SET(data_pipe, &fdset);
  193. FD_SET(fd, &fdset);
  194. maxfd = fd > data_pipe ? fd : data_pipe;
  195. rv = select(maxfd + 1, &fdset, NULL, NULL, NULL);
  196. /* update the local config framework structures */
  197. cfg_update();
  198. if (rv < 0) {
  199. LM_ERR("select() failed: %s\n", strerror(errno));
  200. } else if (!rv) {
  201. /* timeout */
  202. } else if (FD_ISSET(fd, &fdset)) {
  203. char *buf = net_read_static(fd);
  204. if (!buf)
  205. /* connection closed */
  206. break;
  207. LM_DBG("server read\n[%s]\n", buf);
  208. xode_stream_eat(stream, buf, strlen(buf));
  209. } else if (FD_ISSET(data_pipe, &fdset)) {
  210. if (read(data_pipe, &cmd, sizeof(cmd)) != sizeof(cmd)) {
  211. LM_ERR("failed to read from command pipe: %s\n",
  212. strerror(errno));
  213. } else {
  214. LM_DBG("got pipe cmd %d\n", cmd->type);
  215. switch (cmd->type) {
  216. case XMPP_PIPE_SEND_MESSAGE:
  217. do_send_message_component(&priv, cmd);
  218. break;
  219. case XMPP_PIPE_SEND_PACKET:
  220. case XMPP_PIPE_SEND_PSUBSCRIBE:
  221. case XMPP_PIPE_SEND_PNOTIFY:
  222. do_send_bulk_message_component(&priv, cmd);
  223. break;
  224. }
  225. xmpp_free_pipe_cmd(cmd);
  226. }
  227. }
  228. }
  229. xode_pool_free(pool);
  230. close(fd);
  231. }
  232. return 0;
  233. }