static struct xmpp_connection *conn_new(int type, int fd, char *domain) { struct xmpp_connection *conn = NULL; conn = malloc(sizeof(struct xmpp_connection)); if(conn==NULL) { LM_ERR("out of memory\n"); return NULL; } memset(conn, 0, sizeof(struct xmpp_connection)); conn->domain = domain ? strdup(domain) : NULL; conn->type = type; conn->fd = fd; conn->todo = xode_new_tag("todo"); conn->pool = xode_pool_new(); conn->stream = xode_stream_new(conn->pool, (type==CONN_INBOUND)?in_stream_node_callback:out_stream_node_callback, conn); conn->next = conn_list; conn_list = conn; return conn; }
int xmpp_component_child_process(int data_pipe) { int fd, maxfd, rv; fd_set fdset; xode_pool pool; xode_stream stream; struct xmpp_private_data priv; struct xmpp_pipe_cmd *cmd; while (1) { fd = net_connect(xmpp_host, xmpp_port); if (fd < 0) { sleep(3); continue; } priv.fd = fd; priv.running = 1; pool = xode_pool_new(); stream = xode_stream_new(pool, stream_node_callback, &priv); net_printf(fd, "<?xml version='1.0'?>" "<stream:stream xmlns='jabber:component:accept' to='%s' " "version='1.0' xmlns:stream='http://etherx.jabber.org/streams'>", xmpp_domain); while (priv.running) { FD_ZERO(&fdset); FD_SET(data_pipe, &fdset); FD_SET(fd, &fdset); maxfd = fd > data_pipe ? fd : data_pipe; rv = select(maxfd + 1, &fdset, NULL, NULL, NULL); /* update the local config framework structures */ cfg_update(); if (rv < 0) { LM_ERR("select() failed: %s\n", strerror(errno)); } else if (!rv) { /* timeout */ } else if (FD_ISSET(fd, &fdset)) { char *buf = net_read_static(fd); if (!buf) /* connection closed */ break; LM_DBG("server read\n[%s]\n", buf); xode_stream_eat(stream, buf, strlen(buf)); } else if (FD_ISSET(data_pipe, &fdset)) { if (read(data_pipe, &cmd, sizeof(cmd)) != sizeof(cmd)) { LM_ERR("failed to read from command pipe: %s\n", strerror(errno)); } else { LM_DBG("got pipe cmd %d\n", cmd->type); switch (cmd->type) { case XMPP_PIPE_SEND_MESSAGE: do_send_message_component(&priv, cmd); break; case XMPP_PIPE_SEND_PACKET: case XMPP_PIPE_SEND_PSUBSCRIBE: case XMPP_PIPE_SEND_PNOTIFY: do_send_bulk_message_component(&priv, cmd); break; } xmpp_free_pipe_cmd(cmd); } } } xode_pool_free(pool); close(fd); } return 0; }
int xmpp_component_child_process(int data_pipe) { int fd, maxfd, rv; fd_set fdset; xode_pool pool; xode_stream stream; struct xmpp_private_data priv; struct xmpp_pipe_cmd *cmd; while (1) { fd = net_connect(xmpp_host, xmpp_port); if (fd < 0) { sleep(3); continue; } curr_fd = fd; priv.fd = fd; priv.running = 1; pool = xode_pool_new(); stream = xode_stream_new(pool, stream_node_callback, &priv); net_printf(fd, "<?xml version='1.0'?>" "<stream:stream xmlns='jabber:component:accept' to='%s' " "version='1.0' xmlns:stream='http://etherx.jabber.org/streams'>", xmpp_domain); while (priv.running) { FD_ZERO(&fdset); FD_SET(data_pipe, &fdset); FD_SET(fd, &fdset); maxfd = fd > data_pipe ? fd : data_pipe; rv = select(maxfd + 1, &fdset, NULL, NULL, NULL); if (rv < 0) { if (errno == EINTR) { continue; } LM_ERR("select() failed: %s\n", strerror(errno)); } else if (!rv) { /* timeout */ } else if (FD_ISSET(fd, &fdset)) { char *buf = net_read_static(fd); if (!buf) /* connection closed */ break; LM_DBG("server read\n[%s]\n", buf); xode_stream_eat(stream, buf, strlen(buf)); } else if (FD_ISSET(data_pipe, &fdset)) { if (read(data_pipe, &cmd, sizeof(cmd)) != sizeof(cmd)) { LM_ERR("failed to read from command pipe: %s\n", strerror(errno)); } else { xmpp_component_net_send(cmd, &priv); } } } xode_pool_free(pool); close(fd); } return 0; }