/** Initiate a connection to the XMPP server. * This function returns immediately after starting the connection * process to the XMPP server, and notifiations of connection state changes * will be sent to the callback function. The domain and port to connect to * are usually determined by an SRV lookup for the xmpp-client service at * the domain specified in the JID. If SRV lookup fails, altdomain and * altport will be used instead if specified. * * @param conn a Strophe connection object * @param altdomain a string with domain to use if SRV lookup fails. If this * is NULL, the domain from the JID will be used. * @param altport an integer port number to use if SRV lookup fails. If this * is 0, the default port (5222) will be assumed. * @param callback a xmpp_conn_handler callback function that will receive * notifications of connection status * @param userdata an opaque data pointer that will be passed to the callback * * @return 0 on success and -1 on an error * * @ingroup Connections */ int xmpp_connect_client(xmpp_conn_t * const conn, const char * const altdomain, unsigned short altport, xmpp_conn_handler callback, void * const userdata) { char connectdomain[2048]; int connectport; const char * domain; conn->type = XMPP_CLIENT; conn->domain = xmpp_jid_domain(conn->ctx, conn->jid); if (!conn->domain) return -1; if (altdomain) { xmpp_debug(conn->ctx, "xmpp", "Connecting via altdomain."); strcpy(connectdomain, altdomain); connectport = altport ? altport : 5222; } else if (!sock_srv_lookup("xmpp-client", "tcp", conn->domain, connectdomain, 2048, &connectport)) { xmpp_debug(conn->ctx, "xmpp", "SRV lookup failed."); if (!altdomain) domain = conn->domain; else domain = altdomain; xmpp_debug(conn->ctx, "xmpp", "Using alternate domain %s, port %d", altdomain, altport); strcpy(connectdomain, domain); connectport = altport ? altport : 5222; } conn->sock = sock_connect(connectdomain, connectport); xmpp_debug(conn->ctx, "xmpp", "sock_connect to %s:%d returned %d", connectdomain, connectport, conn->sock); if (conn->sock == -1) return -1; /* setup handler */ conn->conn_handler = callback; conn->userdata = userdata; /* FIXME: it could happen that the connect returns immediately as * successful, though this is pretty unlikely. This would be a little * hard to fix, since we'd have to detect and fire off the callback * from within the event loop */ conn->state = XMPP_STATE_CONNECTING; conn->timeout_stamp = time_stamp(); xmpp_debug(conn->ctx, "xmpp", "attempting to connect to %s", connectdomain); if (conn->xev) conn_ev_add_connect_handler(conn->xev); return 0; }
/** Initiate a connection to the XMPP server. * This function returns immediately after starting the connection * process to the XMPP server, and notifiations of connection state changes * will be sent to the callback function. The domain and port to connect to * are usually determined by an SRV lookup for the xmpp-client service at * the domain specified in the JID. If SRV lookup fails, altdomain and * altport will be used instead if specified. * * @param conn a Strophe connection object * @param altdomain a string with domain to use if SRV lookup fails. If this * is NULL, the domain from the JID will be used. * @param altport an integer port number to use if SRV lookup fails. If this * is 0, the default port will be assumed. * @param callback a xmpp_conn_handler callback function that will receive * notifications of connection status * @param userdata an opaque data pointer that will be passed to the callback * * @return 0 on success and -1 on an error * * @ingroup Connections */ int xmpp_connect_client(xmpp_conn_t * const conn, const char * const altdomain, unsigned short altport, xmpp_conn_handler callback, void * const userdata) { char domain[2048]; int port; const char *prefdomain = NULL; int found; if (conn->state != XMPP_STATE_DISCONNECTED) return -1; if (conn->domain != NULL) xmpp_free(conn->ctx, conn->domain); conn->type = XMPP_CLIENT; conn->secured = 0; conn->tls_failed = 0; conn->domain = xmpp_jid_domain(conn->ctx, conn->jid); if (!conn->domain) return -1; if (altdomain != NULL) { xmpp_debug(conn->ctx, "xmpp", "Connecting via altdomain."); prefdomain = altdomain; port = altport ? altport : _conn_default_port(conn); } else { found = sock_srv_lookup("xmpp-client", "tcp", conn->domain, domain, sizeof(domain), &port); if (!found) { xmpp_debug(conn->ctx, "xmpp", "SRV lookup failed, " "connecting via domain."); prefdomain = conn->domain; port = altport ? altport : _conn_default_port(conn); } if (conn->tls_legacy_ssl) { /* SSL tunneled connection on 5223 port is legacy and doesn't * have an SRV record. Force port 5223 here unless altport is * specified. */ port = altport ? altport : XMPP_PORT_CLIENT_LEGACY_SSL; } } if (prefdomain != NULL) { strncpy(domain, prefdomain, sizeof(domain)); domain[sizeof(domain) - 1] = '\0'; } conn->sock = sock_connect(domain, port); xmpp_debug(conn->ctx, "xmpp", "sock_connect to %s:%d returned %d", domain, port, conn->sock); if (conn->sock == -1) return -1; /* setup handler */ conn->conn_handler = callback; conn->userdata = userdata; /* FIXME: it could happen that the connect returns immediately as * successful, though this is pretty unlikely. This would be a little * hard to fix, since we'd have to detect and fire off the callback * from within the event loop */ conn->state = XMPP_STATE_CONNECTING; conn->timeout_stamp = time_stamp(); xmpp_debug(conn->ctx, "xmpp", "attempting to connect to %s", domain); return 0; }