diff options
Diffstat (limited to 'examples/basic.c')
-rw-r--r-- | examples/basic.c | 51 |
1 files changed, 31 insertions, 20 deletions
diff --git a/examples/basic.c b/examples/basic.c index 6e195bd..5df4f65 100644 --- a/examples/basic.c +++ b/examples/basic.c @@ -11,7 +11,10 @@ #include <errno.h> #include <syslog.h> #include <gsasl.h> +#include <time.h> +#include <stdlib.h> #include <rexmpp.h> +#include <rexmpp_xml.h> #include <rexmpp_sasl.h> int log_level = 8; @@ -65,16 +68,16 @@ int my_sasl_property_cb (rexmpp_t *s, rexmpp_sasl_property prop) { } /* An XML in callback, printing what was received. */ -int my_xml_in_cb (rexmpp_t *s, xmlNodePtr node) { - char *xml_buf = rexmpp_xml_serialize(node); +int my_xml_in_cb (rexmpp_t *s, rexmpp_xml_t *node) { + char *xml_buf = rexmpp_xml_serialize(node, 0); printf("recv: %s\n", xml_buf); free(xml_buf); return 0; } /* An XML out callback, printing what is about to be sent. */ -int my_xml_out_cb (rexmpp_t *s, xmlNodePtr node) { - char *xml_buf = rexmpp_xml_serialize(node); +int my_xml_out_cb (rexmpp_t *s, rexmpp_xml_t *node) { + char *xml_buf = rexmpp_xml_serialize(node, 0); printf("send: %s\n", xml_buf); free(xml_buf); return 0; @@ -85,6 +88,11 @@ int my_console_print_cb (rexmpp_t *s, const char *fmt, va_list args) { return 0; } +/* void my_socket_options(rexmpp_t *s, int sock) { */ +/* int pmtudisc = IP_PMTUDISC_WANT; */ +/* setsockopt(sock, IPPROTO_IP, IP_MTU_DISCOVER, &pmtudisc, sizeof(pmtudisc)); */ +/* } */ + void print_help (char *prog_name) { printf("Usage: %s [options] <jid>\n" \ "Options:\n" \ @@ -137,7 +145,8 @@ int main (int argc, char **argv) { /* Could set a client certificate for SASL EXTERNAL authentication and Jingle's DTLS here. */ - rexmpp_tls_set_x509_key_file(&s, "client.crt", "client.key"); + s.x509_key_file = "client.key"; + s.x509_cert_file = "client.crt"; /* Could also set various other things manually. */ /* s.socks_host = "127.0.0.1"; */ @@ -148,6 +157,7 @@ int main (int argc, char **argv) { /* rexmpp_openpgp_set_home_dir(&s, "pgp"); */ s.roster_cache_file = "roster.xml"; /* s.tls_policy = REXMPP_TLS_AVOID; */ + /* s.socket_cb = my_socket_options; */ /* Once the main structure is initialised and everything is sufficiently configured, we are ready to run the main loop and @@ -155,8 +165,10 @@ int main (int argc, char **argv) { fd_set read_fds, write_fds; int nfds; - struct timeval tv; - struct timeval *mtv; + struct timespec tv; + struct timespec *mtv; + struct timeval tv_ms; + struct timeval *mtv_ms; int n = 0; do { @@ -173,18 +185,11 @@ int main (int argc, char **argv) { if (strlen(input) != 0) { if (input[0] == '<' && xml_console) { /* Raw XML input. */ - xmlDocPtr doc = xmlReadMemory(input, input_len, "", "utf-8", 0); - if (doc != NULL) { - xmlNodePtr node = xmlDocGetRootElement(doc); - if (node != NULL) { - xmlUnlinkNode(node); - rexmpp_send(&s, node); - } else { - puts("No root node"); - } - xmlFreeDoc(doc); + rexmpp_xml_t *node = rexmpp_xml_parse(input, input_len); + if (node != NULL) { + rexmpp_send(&s, node); } else { - puts("Failed to read a document"); + puts("Failed to parse XML"); } } else if (txt_console) { rexmpp_console_feed(&s, input, input_len); @@ -217,7 +222,13 @@ int main (int argc, char **argv) { FD_ZERO(&read_fds); FD_ZERO(&write_fds); nfds = rexmpp_fds(&s, &read_fds, &write_fds); - mtv = rexmpp_timeout(&s, NULL, (struct timeval*)&tv); + mtv = rexmpp_timeout(&s, NULL, &tv); + mtv_ms = NULL; + if (mtv != NULL) { + tv_ms.tv_sec = mtv->tv_sec; + tv_ms.tv_usec = mtv->tv_nsec / 1000; + mtv_ms = &tv_ms; + } /* Add other file descriptors we are interested in, particularly stdin for user input. */ @@ -225,7 +236,7 @@ int main (int argc, char **argv) { /* Run select(2) with all those file descriptors and timeouts, waiting for either user input or some rexmpp event to occur. */ - n = select(nfds, &read_fds, &write_fds, NULL, mtv); + n = select(nfds, &read_fds, &write_fds, NULL, mtv_ms); if (n == -1) { printf("select error: %s\n", strerror(errno)); break; |