Example #1
0
int main(int argc, char **argv)
{
    xmpp_ctx_t *ctx;
    xmpp_mem_t mymem;
    xmpp_log_t mylog;
    char my_str[5] = "asdf";
    void *testptr1, *testptr2;

    ctx = xmpp_ctx_new(NULL, NULL);
    if (ctx == NULL) return 1;

    /* destroy context */
    xmpp_ctx_free(ctx);

    /* setup our memory handler */
    mymem.alloc = my_alloc;
    mymem.free = my_free;
    mymem.realloc = my_realloc;

    /* setup our logger */
    mylog.handler = my_logger;
    mylog.userdata = my_str;

    ctx = xmpp_ctx_new(&mymem, &mylog);
    xmpp_debug(ctx, "test", "hello");

    testptr1 = xmpp_alloc(ctx, 1024);
    if (testptr1 == NULL) {
	xmpp_ctx_free(ctx);
	return 1;
    }

    testptr2 = xmpp_realloc(ctx, testptr1, 2048);
    if (testptr2 == NULL) {
	xmpp_free(ctx, testptr1);
	xmpp_ctx_free(ctx);
	return 1;
    }

    xmpp_free(ctx, testptr2);

    xmpp_ctx_free(ctx);

    /* check for test failure */
    if (!(log_called && mem_alloc_called && mem_realloc_called && 
	  mem_free_called))
	return 1;
    if (mem_alloc_called != mem_free_called)
        return 1;
    
    return 0;
}
Example #2
0
void cleanup(struct soap* soap) {
  /* release our connection and context */
  xmpp_conn_release(conn);
  xmpp_ctx_free(ctx);
  
  /* final shutdown of the library */
  xmpp_shutdown();

  if (rsa_private_key) {
    EVP_PKEY_free(rsa_private_key);
  }

  if (cert) {
    X509_free(cert);
  }

  if (soap) {
    soap_end(soap);
    soap_done(soap);
    soap_free(soap);
  }
  
  ERR_remove_state(0);
  ENGINE_cleanup();
  CONF_modules_unload(1);

  ERR_free_strings();
  EVP_cleanup();
  CRYPTO_cleanup_all_ex_data();
}
Example #3
0
void
jabber_disconnect(void)
{
    // if connected, send end stream and wait for response
    if (jabber_conn.conn_status == JABBER_CONNECTED) {
        char *account_name = jabber_get_account_name();
        const char *fulljid = jabber_get_fulljid();
        plugins_on_disconnect(account_name, fulljid);
        log_info("Closing connection");
        accounts_set_last_activity(jabber_get_account_name());
        jabber_conn.conn_status = JABBER_DISCONNECTING;
        xmpp_disconnect(jabber_conn.conn);

        while (jabber_get_connection_status() == JABBER_DISCONNECTING) {
            jabber_process_events(10);
        }
        _connection_free_saved_account();
        _connection_free_saved_details();
        _connection_free_session_data();
        if (jabber_conn.conn) {
            xmpp_conn_release(jabber_conn.conn);
            jabber_conn.conn = NULL;
        }
        if (jabber_conn.ctx) {
            xmpp_ctx_free(jabber_conn.ctx);
            jabber_conn.ctx = NULL;
        }
    }

    jabber_conn.conn_status = JABBER_STARTED;
    FREE_SET_NULL(jabber_conn.presence_message);
    FREE_SET_NULL(jabber_conn.domain);
}
Example #4
0
void
jabber_autoping_fail(void)
{
    if (jabber_conn.conn_status == JABBER_CONNECTED) {
        log_info("Closing connection");
        char *account_name = jabber_get_account_name();
        const char *fulljid = jabber_get_fulljid();
        plugins_on_disconnect(account_name, fulljid);
        accounts_set_last_activity(jabber_get_account_name());
        jabber_conn.conn_status = JABBER_DISCONNECTING;
        xmpp_disconnect(jabber_conn.conn);

        while (jabber_get_connection_status() == JABBER_DISCONNECTING) {
            jabber_process_events(10);
        }
        if (jabber_conn.conn) {
            xmpp_conn_release(jabber_conn.conn);
            jabber_conn.conn = NULL;
        }
        if (jabber_conn.ctx) {
            xmpp_ctx_free(jabber_conn.ctx);
            jabber_conn.ctx = NULL;
        }
    }

    FREE_SET_NULL(jabber_conn.presence_message);
    FREE_SET_NULL(jabber_conn.domain);

    jabber_conn.conn_status = JABBER_DISCONNECTED;
    _jabber_lost_connection();
}
Example #5
0
static void
_jabber_disconnect(void)
{
    // if connected, send end stream and wait for response
    if (jabber_conn.conn_status == JABBER_CONNECTED) {
        log_info("Closing connection");
        jabber_conn.conn_status = JABBER_DISCONNECTING;
        xmpp_disconnect(jabber_conn.conn);

        while (jabber_get_connection_status() == JABBER_DISCONNECTING) {
            jabber_process_events();
        }
        _connection_free_saved_account();
        _connection_free_saved_details();
        _connection_free_session_data();
        if (jabber_conn.conn != NULL) {
            xmpp_conn_release(jabber_conn.conn);
            jabber_conn.conn = NULL;
        }
        if (jabber_conn.ctx != NULL) {
            xmpp_ctx_free(jabber_conn.ctx);
            jabber_conn.ctx = NULL;
        }
    }

    jabber_conn.conn_status = JABBER_STARTED;
    FREE_SET_NULL(jabber_conn.presence_message);
    FREE_SET_NULL(jabber_conn.domain);
}
Example #6
0
int main(int argc, char *argv[])
{
    xmpp_ctx_t *ctx;
    int ret;

    printf("allocating context... ");   
    ctx = xmpp_ctx_new(NULL, NULL);
    if (ctx == NULL) printf("failed to create context\n");
    if (ctx == NULL) return -1;
    printf("ok.\n");

    printf("testing jid routines... ");   
    ret = test_jid(ctx);
    if (ret) printf("failed!\n");
    if (ret) return ret;
    printf("ok.\n");

    printf("testing jid new routines... ");   
    ret = test_jid_new(ctx);
    if (ret) printf("failed!\n");
    if (ret) return ret;
    printf("ok.\n");

    printf("freeing context... ");
    xmpp_ctx_free(ctx);
    printf("ok.\n");

    return ret;
}
Example #7
0
int config_load_file(FILE *config_file, struct Config *dest) {
    char *rdbuf = malloc(CONFIG_CHUNK_SIZE);
    int ret = -1;
    xmpp_ctx_t *ctx = xmpp_ctx_new(NULL, NULL);
    parser_t *parser = parser_new(ctx,
        NULL,
        NULL,
        config_parse_stanza,
        dest);

    size_t read_bytes;
    do {
        read_bytes = fread(rdbuf, 1, CONFIG_CHUNK_SIZE, config_file);
        if (!parser_feed(parser, rdbuf, read_bytes)) {
            ret = 2;
            goto __cleanup__;
        }
    } while (read_bytes == CONFIG_CHUNK_SIZE);
    if (errno != 0) {
        ret = 1;
    } else {
        ret = 0;
    }

__cleanup__:
    free(rdbuf);
    parser_free(parser);
    xmpp_ctx_free(ctx);
    return ret;
}
Example #8
0
int main(int argc, char *argv[])
{
    xmpp_ctx_t *ctx;
    int ret;

    printf("allocating context... ");   
    ctx = xmpp_ctx_new(NULL, NULL);
    if (ctx == NULL) printf("failed to create context\n");
    if (ctx == NULL) return -1;
    printf("ok.\n");

    printf("testing SASL PLAIN... ");   
    ret = test_plain(ctx);
    if (ret) printf("failed!\n");
    if (ret) return ret;
    printf("ok.\n");

    printf("testing SASL DIGEST-MD5... ");
    ret = test_digest_md5(ctx);
    if (ret) printf("failed!\n");
    if (ret) return ret;
    printf("ok.\n");

    printf("freeing context... ");
    xmpp_ctx_free(ctx);
    printf("ok.\n");

    return ret;
}
Example #9
0
static jabber_conn_status_t
_jabber_connect(const char * const fulljid, const char * const passwd,
    const char * const altdomain, int port)
{
    assert(fulljid != NULL);
    assert(passwd != NULL);

    Jid *jid = jid_create(fulljid);

    if (jid == NULL) {
        log_error("Malformed JID not able to connect: %s", fulljid);
        jabber_conn.conn_status = JABBER_DISCONNECTED;
        return jabber_conn.conn_status;
    } else if (jid->fulljid == NULL) {
        log_error("Full JID required to connect, received: %s", fulljid);
        jabber_conn.conn_status = JABBER_DISCONNECTED;
        return jabber_conn.conn_status;
    }

    jid_destroy(jid);

    log_info("Connecting as %s", fulljid);
    if (jabber_conn.log) {
        free(jabber_conn.log);
    }
    jabber_conn.log = _xmpp_get_file_logger();

    if (jabber_conn.conn) {
        xmpp_conn_release(jabber_conn.conn);
    }
    if (jabber_conn.ctx) {
        xmpp_ctx_free(jabber_conn.ctx);
    }
    jabber_conn.ctx = xmpp_ctx_new(NULL, jabber_conn.log);
    if (jabber_conn.ctx == NULL) {
        log_warning("Failed to get libstrophe ctx during connect");
        return JABBER_DISCONNECTED;
    }
    jabber_conn.conn = xmpp_conn_new(jabber_conn.ctx);
    if (jabber_conn.conn == NULL) {
        log_warning("Failed to get libstrophe conn during connect");
        return JABBER_DISCONNECTED;
    }
    xmpp_conn_set_jid(jabber_conn.conn, fulljid);
    xmpp_conn_set_pass(jabber_conn.conn, passwd);
    if (jabber_conn.tls_disabled) {
        xmpp_conn_disable_tls(jabber_conn.conn);
    }

    int connect_status = xmpp_connect_client(jabber_conn.conn, altdomain, port,
        _connection_handler, jabber_conn.ctx);

    if (connect_status == 0)
        jabber_conn.conn_status = JABBER_CONNECTING;
    else
        jabber_conn.conn_status = JABBER_DISCONNECTED;

    return jabber_conn.conn_status;
}
Example #10
0
void xmpp_ua_uninit(void)
{
	xmpp_stop(_xmpp_ctx);
	WaitForSingleObject(_xmpp_thread, INFINITE);
	CloseHandle(_xmpp_thread);
	CloseHandle(_mutex_4_conn);
	CloseHandle(_mutex_4_id);
	xmpp_ctx_free(_xmpp_ctx);
	xmpp_shutdown();
}
Example #11
0
int main(int argc, char **argv)
{
    xmpp_ctx_t *ctx;
    xmpp_conn_t *conn;
    xmpp_log_t *log;
    char *jid, *pass;
    char *server;

    /* take a jid and password on the command line,
       with optional server to connect to */
    if ((argc < 3) || (argc > 4)) {
	fprintf(stderr, "Usage: basic <jid> <pass> [<server>]\n\n");
	return 1;
    }
    
    jid = argv[1];
    pass = argv[2];
    server = NULL;
    /* Normally we pass NULL for the connection domain, in which case
       the library derives the target host from the jid, but we can
       override this for testing. */
    if (argc >= 4) server = argv[3];
    
    /* init library */
    xmpp_initialize();

    /* create a context */
//    log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); /* pass NULL instead to silence output */
//    log = xmpp_get_default_logger(NULL); /* pass NULL instead to silence output */
    ctx = xmpp_ctx_new(NULL, NULL);

    /* create a connection */
    conn = xmpp_conn_new(ctx);

    /* setup authentication information */
    xmpp_conn_set_jid(conn, jid);
    xmpp_conn_set_pass(conn, pass);

    /* initiate connection */
    xmpp_connect_client(conn, server, conn_handler, ctx);

    /* enter the event loop - 
       our connect handler will trigger an exit */
    xmpp_run(ctx);

    /* release our connection and context */
    xmpp_conn_release(conn);
    xmpp_ctx_free(ctx);

    /* final shutdown of the library */
    xmpp_shutdown();

    return 0;
}
Example #12
0
int main(int argc, char **argv)
{
    xmpp_ctx_t *ctx;
    xmpp_conn_t *conn;
    xmpp_log_t *log;
    char *jid, *pass;

    /* take a jid and password on the command line */
    if (argc != 3) {
        fprintf(stderr, "Usage: bot <jid> <pass>\n\n");
        return 1;
    }

    jid = argv[1];
    pass = argv[2];

    /* init library */
    xmpp_initialize();

    /* create a context */
    log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); /* pass NULL instead to silence output */
    ctx = xmpp_ctx_new(NULL, log);

    /* create a connection */
    conn = xmpp_conn_new(ctx);

    /*
     * also you can disable TLS support or force legacy SSL
     * connection without STARTTLS
     *
     * see xmpp_conn_set_flags() or examples/basic.c
     */

    /* setup authentication information */
    xmpp_conn_set_jid(conn, jid);
    xmpp_conn_set_pass(conn, pass);

    /* initiate connection */
    xmpp_connect_client(conn, NULL, 0, conn_handler, ctx);

    /* enter the event loop - 
       our connect handler will trigger an exit */
    xmpp_run(ctx);

    /* release our connection and context */
    xmpp_conn_release(conn);
    xmpp_ctx_free(ctx);

    /* final shutdown of the library */
    xmpp_shutdown();

    return 0;
}
Example #13
0
int main(int argc, char **argv)
{
    xmpp_ctx_t *ctx;
    xmpp_conn_t *conn;
    xmpp_log_t *log;
    char *jid, *pass, *host;

    /* take a jid and password on the command line */
    if (argc < 3 || argc > 4) {
        fprintf(stderr, "Usage: basic <jid> <pass> [<host>]\n\n");
        return 1;
    }

    jid = argv[1];
    pass = argv[2];
    host = NULL;

    if (argc == 4)
        host = argv[3];

    /* init library */
    xmpp_initialize();

    /* create a context */
    log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); /* pass NULL instead to silence output */
    ctx = xmpp_ctx_new(NULL, log);

    /* create a connection */
    conn = xmpp_conn_new(ctx);

    /* setup authentication information */
    xmpp_conn_set_jid(conn, jid);
    xmpp_conn_set_pass(conn, pass);

    /* initiate connection */
    xmpp_connect_client(conn, host, 0, conn_handler, ctx);

    /* enter the event loop -
       our connect handler will trigger an exit */
    xmpp_run(ctx);

    /* release our connection and context */
    xmpp_conn_release(conn);
    xmpp_ctx_free(ctx);

    /* final shutdown of the library */
    xmpp_shutdown();

    return 0;
}
Example #14
0
void Release()
{
    /* release our connection and context */
    xmpp_conn_release(conn);
    xmpp_ctx_free(ctx);

    /* final shutdown of the library */
    xmpp_shutdown();

    unregister_plugin(cs);
    cs_removeplugin(cs, PLUGIN_NAME);
    /* release the CmdState */
    cs_free(cs);
}
Example #15
0
void* run_conflate(void *arg) {
    conflate_handle_t* handle = (conflate_handle_t*)arg;

    /* Before connecting and all that, load the stored config */
    kvpair_t* conf = load_kvpairs(handle, handle->conf->save_path);
    if (conf) {
        handle->conf->new_config(handle->conf->userdata, conf);
        free_kvpair(conf);
    }

    xmpp_log_t strophe_logger = { &conflate_strophe_logger, handle };

    /* Run forever */
    for (;;) {
        handle->ctx = xmpp_ctx_new(NULL, &strophe_logger);
        assert(handle->ctx);

        handle->conn = xmpp_conn_new(handle->ctx);
        assert(handle->conn);

        /* Use the stored jid if there is one */
        char *db_jid = conflate_get_private(handle, STORED_JID_KEY,
                                            handle->conf->save_path);
        if (db_jid) {
            CONFLATE_LOG(handle, LOG_LVL_DEBUG, "Using jid from db: %s",
                         db_jid);
            xmpp_conn_set_jid(handle->conn, db_jid);
            free(db_jid);
        } else {
            CONFLATE_LOG(handle, LOG_LVL_DEBUG, "Using provided jid:  %s",
                         handle->conf->jid);
            xmpp_conn_set_jid(handle->conn, handle->conf->jid);
        }

        xmpp_conn_set_pass(handle->conn, handle->conf->pass);

        xmpp_connect_client(handle->conn, handle->conf->host, 0,
                            conn_handler, handle);
        xmpp_run(handle->ctx);
        CONFLATE_LOG(handle, LOG_LVL_INFO, "xmpp_run exited");

        xmpp_conn_release(handle->conn);
        xmpp_ctx_free(handle->ctx);

        sleep(5);
        CONFLATE_LOG(handle, LOG_LVL_INFO, "reconnecting");
    }
    CONFLATE_LOG(handle, LOG_LVL_FATAL, "Exited an infinite loop.");
    return NULL;
}
Example #16
0
int main(int argc, char *argv[])
{
    xmpp_ctx_t *ctx;
    unsigned char *udec;
    char *dec;
    char *enc;
    size_t len;
    int i;

    printf("BASE64 tests.\n");

    ctx = xmpp_ctx_new(NULL, NULL);
    if (ctx == NULL) {
        fprintf(stderr, "failed to create context\n");
        return 1;
    }

    for (i = 0; i < ARRAY_SIZE(tests); ++i) {
        printf("Test #%d: ", (int)i + 1);
        enc = xmpp_base64_encode(ctx, (unsigned char *)tests[i].raw,
                                 strlen(tests[i].raw));
        assert(enc != NULL);
        COMPARE(tests[i].base64, enc);
        xmpp_free(ctx, enc);

        dec = xmpp_base64_decode_str(ctx, tests[i].base64,
                                     strlen(tests[i].base64));
        assert(dec != NULL);
        COMPARE_BUF(tests[i].raw, strlen(tests[i].raw), dec, strlen(dec));
        xmpp_free(ctx, dec);
        printf("ok\n");
    }

    printf("Test with binary data: ");
    enc = xmpp_base64_encode(ctx, bin_data, sizeof(bin_data));
    assert(enc != NULL);
    xmpp_base64_decode_bin(ctx, enc, strlen(enc), &udec, &len);
    assert(udec != NULL);
    assert(len != 0);
    assert(len == sizeof(bin_data));
    COMPARE_BUF(bin_data, sizeof(bin_data), udec, len);
    xmpp_free(ctx, udec);
    xmpp_free(ctx, enc);
    printf("ok\n");

    xmpp_ctx_free(ctx);

    return 0;
}
Example #17
0
int main(int argc, char **argv)
{
    int flags;
    xmpp_ctx_t *ctx;
    xmpp_conn_t *conn;
    xmpp_log_t *log;
    char *jid_from, *pass, *jid_to;

    if (argc != 4) {
    fprintf(stderr, "Usage: xmppipe <jid_from> <pass_from> <jid_to>\n\n");
    return 1;
    }

    jid_from = argv[1];
    pass = argv[2];
    jid_to = argv[3];

    /* init library */
    xmpp_initialize();

    /* create a context */
    ctx = xmpp_ctx_new(NULL, NULL);

    /* create a connection */
    conn = xmpp_conn_new(ctx);

    /* setup authentication information */
    xmpp_conn_set_jid(conn, jid_from);
    xmpp_conn_set_pass(conn, pass);

    /* initiate connection */
    xmpp_connect_client(conn, NULL, 0, conn_handler, ctx);

    /* set stdin to be non blocking */
    flags = fcntl(STDIN_FILENO, F_GETFL);
    flags |= O_NONBLOCK;
    fcntl(STDIN_FILENO, F_SETFL, flags);

    main_loop(conn, ctx, jid_to);

    /* release our connection and context */
    xmpp_conn_release(conn);
    xmpp_ctx_free(ctx);

    /* final shutdown of the library */
    xmpp_shutdown();

    return 0;
}
Example #18
0
int main(int argc, char **argv)
{
    xmpp_ctx_t *ctx;
    xmpp_conn_t *conn;

    if (argc != 3) {
	fprintf(stderr, "Usage: active <jid> <pass>\n\n");
	return 1;
    }

    /* initialize lib */
    xmpp_initialize();

    /* create a context */
    ctx = xmpp_ctx_new(NULL, NULL);

    /* create a connection */
    conn = xmpp_conn_new(ctx);

    /*
     * also you can disable TLS support or force legacy SSL
     * connection without STARTTLS
     *
     * see xmpp_conn_set_flags() or examples/basic.c
     */

    /* setup authentication information */
    xmpp_conn_set_jid(conn, argv[1]);
    xmpp_conn_set_pass(conn, argv[2]);

    /* initiate connection */
    xmpp_connect_client(conn, NULL, 0, NULL, conn_handler, ctx);

    /* start the event loop */
    xmpp_run(ctx);

    /* release our connection and context */
    xmpp_conn_release(conn);
    xmpp_ctx_free(ctx);

    /* shutdown lib */
    xmpp_shutdown();

    return 0;
}
void XMPP_Close(xmpp_ctx_t *ctx,  xmpp_conn_t *conn)
{

    if(conn !=NULL)
        xmpp_disconnect(conn);
  
 //   xmpp_handler_delete(conn, iq_ibb_open_handler);

    xmpp_conn_release(conn);

    fprintf(stderr, "Conn release!");

    /* final shutdown of the library */

   if(ctx!=NULL)
        xmpp_ctx_free(ctx);
   xmpp_shutdown();

}
Example #20
0
void
connection_disconnect(void)
{
    conn.conn_status = JABBER_DISCONNECTING;
    xmpp_disconnect(conn.xmpp_conn);

    while (conn.conn_status == JABBER_DISCONNECTING) {
        session_process_events();
    }

    if (conn.xmpp_conn) {
        xmpp_conn_release(conn.xmpp_conn);
        conn.xmpp_conn = NULL;
    }

    if (conn.xmpp_ctx) {
        xmpp_ctx_free(conn.xmpp_ctx);
        conn.xmpp_ctx = NULL;
    }
}
Example #21
0
int main(int argc, char **argv)
{
	struct nefit_easy easy;
	char *serial_number, *access_key, *password;
	EASY_UNUSED(argc);
	EASY_UNUSED(argv);

	/* init library */
	xmpp_initialize();

	serial_number = getenv("NEFIT_SERIAL_NUMBER");
	access_key = getenv("NEFIT_ACCESS_KEY");
	password = getenv("NEFIT_PASSWORD");

	if (!serial_number || !access_key || !password) {
		printf("The following environmental variabled must be set\n");
		printf("NEFIT_SERIAL_NUMBER=123456789\n");
		printf("NEFIT_ACCESS_KEY=abcdefhijklmnopq\n");
		printf("NEFIT_PASSWORD=wachtw\n");
		exit(1);
	}

	easy_connect(&easy, serial_number, access_key, password, value_obtained);

	get_values(&easy);
	//manual_temperature(&easy, 23);
	//clock_mode(&easy);

	/* enter the event loop */
	xmpp_run(easy.xmpp_ctx);

	/* release our connection and context */
	xmpp_conn_release(easy.xmpp_conn);
	xmpp_ctx_free(easy.xmpp_ctx);

	/* shutdown lib */
	xmpp_shutdown();

	return 0;
}
Example #22
0
int main(int argc, char **argv)
{
    xmpp_ctx_t *ctx;
    xmpp_conn_t *conn;

    if (argc != 3) {
	fprintf(stderr, "Usage: active <jid> <pass>\n\n");
	return 1;
    }

    /* initialize lib */
    xmpp_initialize();

    /* create a context */
    ctx = xmpp_ctx_new(NULL, NULL);

    /* create a connection */
    conn = xmpp_conn_new(ctx);

    /* setup authentication information */
    xmpp_conn_set_jid(conn, argv[1]);
    xmpp_conn_set_pass(conn, argv[2]);

    /* initiate connection */
    xmpp_connect_client(conn, NULL, 0, conn_handler, ctx);

    /* start the event loop */
    xmpp_run(ctx);

    /* release our connection and context */
    xmpp_conn_release(conn);
    xmpp_ctx_free(ctx);

    /* shutdown lib */
    xmpp_shutdown();

    return 0;
}
Example #23
0
int main(int argc, char *argv[])
{
	xmpp_ctx_t *ctx;
	xmpp_conn_t *conn;
	xmpp_log_t *log;
	const char *username;
	const char *password;

	username = bot_get_username();
	password = bot_get_password();

	xmpp_initialize();

	/* context */
	log = xmpp_get_default_logger(XMPP_LEVEL_INFO);
	ctx = xmpp_ctx_new(NULL, log);

	/* 创建连接 */
	conn = xmpp_conn_new(ctx);

	/* 设置认证信息 */
	xmpp_conn_set_jid(conn, username);
	xmpp_conn_set_pass(conn, password);

	/* 初始化连接 */
	xmpp_connect_client(conn, NULL, 0, bot_conn_handler, ctx);

	/* 进入主循环 */
	xmpp_run(ctx);

	/* 断开连接和释放资源 */
	xmpp_conn_release(conn);
	xmpp_ctx_free(ctx);

	xmpp_shutdown();
	
	return 0;
}
Example #24
0
int main(int argc, char **argv)
{
    xmpp_ctx_t *ctx;
    hash_t *table, *clone;
    hash_iterator_t *iter;
    unsigned int seed;
    const char *key;
    char *result;
    int err = 0;
    int i;

    /* initialize random numbers */
    if (argc > 2) {
	/* use a seed from the command line */
	seed = (unsigned int)atoi(argv[1]);
    } else {
	seed = (unsigned int)clock();
    }
    /* using random seed 'seed' */
    srand(seed);

    /* allocate a default context */
    ctx = xmpp_ctx_new(NULL, NULL);
    if (ctx == NULL) {
	/* ctx allocation failed! */
	return -1;
    }

    /* allocate a hash table */
    table = hash_new(ctx, TABLESIZE, NULL);
    if (table == NULL) {
	/* table allocation failed! */
	return 1;
    }

    /* test insertion */
    for (i = 0; i < nkeys; i++) {
	err = hash_add(table, keys[i], (void*)values[i]);
	if (err) return err;
    }

    /* test key count */
    if (hash_num_keys(table) != nkeys) {
	/* wrong number of keys in table! */
	return 1;
    }

    /* test cloning */
    clone = hash_clone(table);

    /* test lookup */
    for (i = 0; i < nkeys; i++) {
	result = hash_get(clone, keys[i]);
	if (result == NULL) {
	    /* lookup failed! */
	    return 1;
	}
	if (strcmp(values[i], result)) {
	    /* lookup returned incorrect value! */
	    return 1;
	}
    }

    /* test key iterator */
    iter = hash_iter_new(clone);
    if (iter == NULL) {
	/* iterator allocation failed! */
	return 1;
    }
    for (i = 0; i < nkeys; i++) {
	key = hash_iter_next(iter);
	printf("key: '%s'\n", key);
    }
    key = hash_iter_next(iter);
    if (key != NULL) {
	/* extra keys returned! */
	return 1;
    }
    key = hash_iter_next(iter);
    if (key != NULL) {
	/* extra keys returned! */
	return 1;
    }
    hash_iter_release(iter);

    /* release the hash table */
    hash_release(table);

    /* test drops */
    hash_drop(clone, keys[2]);
    if (hash_get(clone, keys[2]) != NULL) return 1;
    hash_drop(clone, keys[1]);
    hash_drop(clone, keys[4]);
    if (hash_get(clone, keys[4]) != NULL) return 1;
    if (hash_get(clone, keys[1]) != NULL) return 1;
    /* keys 0,3 should still be available */
    if (hash_get(clone, keys[0]) == NULL) return 1;
    if (hash_get(clone, keys[3]) == NULL) return 1;

    /* release our clone */
    hash_release(clone);

    /* release our library context */
    xmpp_ctx_free(ctx);

    return err;
}
Example #25
0
int main(int argc, char **argv)
{
    xmpp_ctx_t *ctx;
    xmpp_conn_t *conn;
    xmpp_log_t *log;
    char *jid, *pass, *host = NULL;
    long flags = 0;
    int i;

    /* take a jid and password on the command line */
    for (i = 1; i < argc; ++i) {
        if (strcmp(argv[i], "--disable-tls") == 0)
            flags |= XMPP_CONN_FLAG_DISABLE_TLS;
        else if (strcmp(argv[i], "--mandatory-tls") == 0)
            flags |= XMPP_CONN_FLAG_MANDATORY_TLS;
        else if (strcmp(argv[i], "--legacy-ssl") == 0)
            flags |= XMPP_CONN_FLAG_LEGACY_SSL;
        else
            break;
    }
    if ((argc - i) < 2 || (argc - i) > 3) {
        fprintf(stderr, "Usage: basic [options] <jid> <pass> [<host>]\n\n"
                        "Options:\n"
                        "  --disable-tls        Disable TLS.\n"
                        "  --mandatory-tls      Deny plaintext connection.\n"
                        "  --legacy-ssl         Use old style SSL.\n\n"
                        "Note: --disable-tls conflicts with --mandatory-tls or "
                              "--legacy-ssl\n");
        return 1;
    }

    jid = argv[i];
    pass = argv[i + 1];
    if (i + 2 < argc)
        host = argv[i + 2];

    /*
     * Note, this example doesn't handle errors. Applications should check
     * return values of non-void functions.
     */

    /* init library */
    xmpp_initialize();

    /* create a context */
    log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); /* pass NULL instead to silence output */
    ctx = xmpp_ctx_new(NULL, log);

    /* create a connection */
    conn = xmpp_conn_new(ctx);

    /* configure connection properties (optional) */
    xmpp_conn_set_flags(conn, flags);

    /* setup authentication information */
    xmpp_conn_set_jid(conn, jid);
    xmpp_conn_set_pass(conn, pass);

    /* initiate connection */
    xmpp_connect_client(conn, host, 0, NULL, conn_handler, ctx);

    /* enter the event loop -
       our connect handler will trigger an exit */
    xmpp_run(ctx);

    /* release our connection and context */
    xmpp_conn_release(conn);
    xmpp_ctx_free(ctx);

    /* final shutdown of the library */
    xmpp_shutdown();

    return 0;
}
Example #26
0
static jabber_conn_status_t
_jabber_connect(const char *const fulljid, const char *const passwd, const char *const altdomain, int port,
    const char *const tls_policy)
{
    assert(fulljid != NULL);
    assert(passwd != NULL);

    Jid *jid = jid_create(fulljid);

    if (jid == NULL) {
        log_error("Malformed JID not able to connect: %s", fulljid);
        jabber_conn.conn_status = JABBER_DISCONNECTED;
        return jabber_conn.conn_status;
    } else if (jid->fulljid == NULL) {
        log_error("Full JID required to connect, received: %s", fulljid);
        jabber_conn.conn_status = JABBER_DISCONNECTED;
        jid_destroy(jid);
        return jabber_conn.conn_status;
    }

    jid_destroy(jid);

    log_info("Connecting as %s", fulljid);
    if (jabber_conn.log) {
        free(jabber_conn.log);
    }
    jabber_conn.log = _xmpp_get_file_logger();

    if (jabber_conn.conn) {
        xmpp_conn_release(jabber_conn.conn);
    }
    if (jabber_conn.ctx) {
        xmpp_ctx_free(jabber_conn.ctx);
    }
    jabber_conn.ctx = xmpp_ctx_new(NULL, jabber_conn.log);
    if (jabber_conn.ctx == NULL) {
        log_warning("Failed to get libstrophe ctx during connect");
        return JABBER_DISCONNECTED;
    }
    jabber_conn.conn = xmpp_conn_new(jabber_conn.ctx);
    if (jabber_conn.conn == NULL) {
        log_warning("Failed to get libstrophe conn during connect");
        return JABBER_DISCONNECTED;
    }
    xmpp_conn_set_jid(jabber_conn.conn, fulljid);
    xmpp_conn_set_pass(jabber_conn.conn, passwd);

    if (!tls_policy || (g_strcmp0(tls_policy, "force") == 0)) {
        xmpp_conn_set_flags(jabber_conn.conn, XMPP_CONN_FLAG_MANDATORY_TLS);
    } else if (g_strcmp0(tls_policy, "disable") == 0) {
        xmpp_conn_set_flags(jabber_conn.conn, XMPP_CONN_FLAG_DISABLE_TLS);
    }

#ifdef HAVE_LIBMESODE
    char *cert_path = prefs_get_string(PREF_TLS_CERTPATH);
    if (cert_path) {
        xmpp_conn_tlscert_path(jabber_conn.conn, cert_path);
    }
    prefs_free_string(cert_path);
#endif

#ifdef HAVE_LIBMESODE
    int connect_status = xmpp_connect_client(
        jabber_conn.conn,
        altdomain,
        port,
        _connection_certfail_cb,
        _connection_handler,
        jabber_conn.ctx);
#else
    int connect_status = xmpp_connect_client(
        jabber_conn.conn,
        altdomain,
        port,
        _connection_handler,
        jabber_conn.ctx);
#endif

    if (connect_status == 0) {
        jabber_conn.conn_status = JABBER_CONNECTING;
    } else {
        jabber_conn.conn_status = JABBER_DISCONNECTED;
    }

    return jabber_conn.conn_status;
}
Example #27
0
jabber_conn_status_t
connection_connect(const char *const jid, const char *const passwd, const char *const altdomain, int port,
    const char *const tls_policy)
{
    assert(jid != NULL);
    assert(passwd != NULL);

    Jid *jidp = jid_create(jid);
    if (jidp == NULL) {
        log_error("Malformed JID not able to connect: %s", jid);
        conn.conn_status = JABBER_DISCONNECTED;
        return conn.conn_status;
    }
    jid_destroy(jidp);

    log_info("Connecting as %s", jid);

    if (conn.xmpp_log) {
        free(conn.xmpp_log);
    }
    conn.xmpp_log = _xmpp_get_file_logger();

    if (conn.xmpp_conn) {
        xmpp_conn_release(conn.xmpp_conn);
    }
    if (conn.xmpp_ctx) {
        xmpp_ctx_free(conn.xmpp_ctx);
    }
    conn.xmpp_ctx = xmpp_ctx_new(NULL, conn.xmpp_log);
    if (conn.xmpp_ctx == NULL) {
        log_warning("Failed to get libstrophe ctx during connect");
        return JABBER_DISCONNECTED;
    }
    conn.xmpp_conn = xmpp_conn_new(conn.xmpp_ctx);
    if (conn.xmpp_conn == NULL) {
        log_warning("Failed to get libstrophe conn during connect");
        return JABBER_DISCONNECTED;
    }
    xmpp_conn_set_jid(conn.xmpp_conn, jid);
    xmpp_conn_set_pass(conn.xmpp_conn, passwd);

    if (!tls_policy || (g_strcmp0(tls_policy, "force") == 0)) {
        xmpp_conn_set_flags(conn.xmpp_conn, XMPP_CONN_FLAG_MANDATORY_TLS);
    } else if (g_strcmp0(tls_policy, "disable") == 0) {
        xmpp_conn_set_flags(conn.xmpp_conn, XMPP_CONN_FLAG_DISABLE_TLS);
    }

#ifdef HAVE_LIBMESODE
    char *cert_path = prefs_get_tls_certpath();
    if (cert_path) {
        xmpp_conn_tlscert_path(conn.xmpp_conn, cert_path);
        free(cert_path);
    }

    int connect_status = xmpp_connect_client(
        conn.xmpp_conn,
        altdomain,
        port,
        _connection_certfail_cb,
        _connection_handler,
        conn.xmpp_ctx);
#else
    int connect_status = xmpp_connect_client(
        conn.xmpp_conn,
        altdomain,
        port,
        _connection_handler,
        conn.xmpp_ctx);
#endif

    if (connect_status == 0) {
        conn.conn_status = JABBER_CONNECTING;
    } else {
        conn.conn_status = JABBER_DISCONNECTED;
    }

    return conn.conn_status;
}
Example #28
0
/**
 * Frees the resources allocated and shutdown the underlaying modules of XMPP
 */
void sdvp_Shutdown() {
	xmpp_ctx_free(ctx);
	xmpp_shutdown();
	sdvpIsInitialised = false;
}
Example #29
0
    int
main(int argc, char **argv)
{
    xmppipe_state_t *state = NULL;
    xmpp_log_t *log = NULL;
    char *jid = NULL;
    char *pass = NULL;
    char *addr = NULL;
    u_int16_t port = 0;
    int flags = 0;

    int ch = 0;

    if (setvbuf(stdout, NULL, _IOLBF, 0) < 0)
        err(EXIT_FAILURE, "setvbuf");

    state = xmppipe_calloc(1, sizeof(xmppipe_state_t));

    xmppipe_next_state(state, XMPPIPE_S_CONNECTING);
    state->bufsz = 2049;
    state->poll = 10;
    state->keepalive = 60 * 1000;
    state->keepalive_limit = 3;
    state->sm_request_interval = 1;
    state->sm_fc = 15;
    state->sm_unacked = 5;
    state->room = xmppipe_roomname("stdout");
    state->resource = xmppipe_strdup(XMPPIPE_RESOURCE);
    state->opt |= XMPPIPE_OPT_GROUPCHAT;

    jid = xmppipe_getenv("XMPPIPE_USERNAME");
    pass = xmppipe_getenv("XMPPIPE_PASSWORD");

    if (xmppipe_sandbox_init(state) < 0)
        err(EXIT_FAILURE, "sandbox failed");

    while ( (ch = getopt_long(argc, argv, "a:b:c:dDeF:hI:k:K:o:P:p:r:sS:u:U:vx",
                    long_options, NULL)) != -1) {
        switch (ch) {
            case 'u':
                /* username/jid */
                free(jid);
                jid = xmppipe_strdup(optarg);
                break;
            case 'p':
                /* password */
                free(pass);
                pass = xmppipe_strdup(optarg);
                break;
            case 'o':
                /* output/muc */
                free(state->room);
                state->room = xmppipe_strdup(optarg);
                break;
            case 'a': {
                    /* address:port */
                    char *p = NULL;
                    free(addr);
                    addr = xmppipe_strdup(optarg);
                    p = strchr(addr, ':');
                    if (p) {
                        *p++ = '\0';
                        port = xmppipe_strtonum(state, p, 0, 0xfffe);
                    }
                }
                break;
            case 'r':
                free(state->resource);
                state->resource = xmppipe_strdup(optarg);
                break;
            case 'S':
                free(state->subject);
                state->subject = xmppipe_strdup(optarg);
                break;
            case 'v':
                state->verbose++;
                break;
            case 'F':
                if (strcmp(optarg, "text") == 0)
                    state->format = XMPPIPE_FMT_TEXT;
                else if (strcmp(optarg, "csv") == 0)
                    state->format = XMPPIPE_FMT_CSV;
                else
                    usage(state);

                break;
            case 'x':
                state->encode = 1;
                break;

            case 'b':
                /* read buffer size */
                state->bufsz = xmppipe_strtonum(state, optarg, 3, 0xfffe);
                break;
            case 'c':
                /* XEP-0198: stream management flow control */
                state->sm_fc = xmppipe_strtonum(state, optarg, 0, 0xfffe);
                break;
            case 'I':
                /* XEP-0198: stream management request interval */
                state->sm_request_interval = xmppipe_strtonum(state, optarg, 0,
                        0xfffe);
                break;
            case 'k':
                /* XEP-0199: XMPP ping keepalives */
                state->sm_request_interval = xmppipe_strtonum(state, optarg, 0,
                        0xfffe) * 1000;
                break;
            case 'K':
                /* XEP-0199: number of keepalive without a reply */
                state->keepalive_limit = xmppipe_strtonum(state, optarg, 1,
                        0xfffe);
                break;
            case 'P':
                /* poll delay */
                state->poll = xmppipe_strtonum(state, optarg, 0, 0xfffe);
                break;
            case 'U':
                /* XEP-0198: stream management unacked requests */
                state->sm_unacked = xmppipe_strtonum(state, optarg, 0, 0xfffe);
                break;

            case 'd':
                state->opt |= XMPPIPE_OPT_DISCARD;
                break;
            case 'D':
                state->opt |= XMPPIPE_OPT_DISCARD;
                state->opt |= XMPPIPE_OPT_DISCARD_TO_STDOUT;
                break;
            case 'e':
                state->opt |= XMPPIPE_OPT_EOF;
                break;
            case 's':
                state->opt |= XMPPIPE_OPT_SIGPIPE;
                break;

            case OPT_NO_TLS_VERIFY:
                flags |= XMPP_CONN_FLAG_TRUST_TLS;
                break;
            case OPT_CHAT:
                state->opt &= ~XMPPIPE_OPT_GROUPCHAT;
                break;

            case OPT_CHAT_MARKER:
                state->opt |= XMPPIPE_OPT_CHAT_MARKER;
                break;

            case 'h':
            default:
                usage(state);
        }
    }

    argc -= optind;
    argv += optind;

    if (argc > 0) {
      free(state->room);
      state->room = xmppipe_strdup(argv[0]);
    }

    if (jid == NULL)
        usage(state);

    if (state->encode && BASE64_LENGTH(state->bufsz) + 1 > 0xffff)
        usage(state);

    state->server = xmppipe_servername(jid);

    if (strchr(state->room, '@')) {
        state->out = xmppipe_strdup(state->room);
        state->mucjid = xmppipe_mucjid(state->out, state->resource);
    }
    else if (!(state->opt & XMPPIPE_OPT_GROUPCHAT)) {
        state->out = xmppipe_strdup(jid);
    }

    if (xmppipe_fmt_init() < 0)
        errx(EXIT_FAILURE, "xmppipe_fmt_init");

    xmpp_initialize();

    log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG);

    state->ctx = xmpp_ctx_new(NULL, (state->verbose > 1 ? log : NULL));
    if (state->ctx == NULL)
        errx(EXIT_FAILURE, "could not allocate context");

    state->conn = xmpp_conn_new(state->ctx);
    if (state->conn == NULL)
        errx(EXIT_FAILURE, "could not allocate connection");

    if (xmpp_conn_set_flags(state->conn, flags) < 0)
        errx(EXIT_FAILURE, "failed to set connection flags");

    xmpp_conn_set_jid(state->conn, jid);
    xmpp_conn_set_pass(state->conn, pass);

    if (xmpp_connect_client(state->conn, addr, port, handle_connection, state) < 0)
        errx(EXIT_FAILURE, "connection failed");

    if (xmppipe_connect_init(state) < 0)
        errx(EXIT_FAILURE, "XMPP handshake failed");

    if (state->verbose)
        (void)fprintf(stderr, "sandbox: stdin: %s\n", XMPPIPE_SANDBOX);

    if (xmppipe_sandbox_stdin(state) < 0)
        err(EXIT_FAILURE, "sandbox failed");

    if (xmppipe_stream_init(state) < 0)
        errx(EXIT_FAILURE, "enabling stream management failed");

    if ( (state->opt & XMPPIPE_OPT_GROUPCHAT) && xmppipe_muc_init(state) < 0)
        errx(EXIT_FAILURE, "failed to join MUC");

    if (xmppipe_presence_init(state) < 0)
        errx(EXIT_FAILURE, "publishing presence failed");

    if ( (state->opt & XMPPIPE_OPT_GROUPCHAT) && state->subject)
        xmppipe_muc_subject(state, state->subject);

    event_loop(state);

    xmppipe_stream_close(state);
    (void)xmpp_conn_release(state->conn);
    xmpp_ctx_free(state->ctx);
    xmpp_shutdown();

    return 0;
}
Example #30
0
int main(int argc, char **argv)
{
    safe_mem_init();


    xmpp_ctx_t *ctx;
    xmpp_conn_t *conn;
    xmpp_log_t *log;

    char *jid = NULL, *pass = NULL, *host = NULL;

    if (argc > 1) {
        jid = argv[1];
    }
    if (argc > 2) {
        pass = argv[2];
    }
    if (argc > 3) {
        host = argv[3];
    }

    jid = require_string("输入账号", 64, jid);
    pass = require_string("输入密码", 64, pass);
    host = require_string("输入主机", 128, host);

    xmpp_initialize();

    if (test_thread(argc, argv)) {
        printf("test thread ok.\n");
    } else {
        printf("test thread fail.\n");
    }


    log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG);
    ctx = xmpp_ctx_new(NULL, log);

    conn = xmpp_conn_new(ctx);

    xmpp_conn_set_jid(conn, jid);
    xmpp_conn_set_pass(conn, pass);

    //// 连接服务器
    xmpp_connect_client(conn, host, 0, conn_handler, ctx);

    int err = pthread_create(&xmpp_thread, NULL, xmpp_routine, ctx);
    if (err != 0) {
        printf("can't create xmpp thread: %s\n", strerror(err));
        return 1;
    }

    pthread_join(xmpp_thread, NULL);
    pthread_join(console_thread, NULL);

    xmpp_conn_release(conn);
    xmpp_ctx_free(ctx);
    xmpp_shutdown();

    safe_mem_free(jid);
    safe_mem_free(pass);
    if (host) {
        safe_mem_free(host);
    }

    safe_mem_check(memcb, NULL);
    _CrtDumpMemoryLeaks();

    printf("按任意按键关闭..");
    getchar();
    return 0;
};