From b349a15d9221e1e9d6736eccb7357294399eb830 Mon Sep 17 00:00:00 2001 From: defanor Date: Thu, 7 Dec 2023 13:54:45 +0300 Subject: Provide a callback after socket creation, use _Bool The callback is provided to set socket options, instead of individual options such as path_mtu_discovery (which is now removed). Noticed that the Rust rexmpp structure's C representation does not match that of C, since Rust's "bool" maps to C99's "_Bool", while I thought that it maps to "int" (c_int). Adjusted C structures to use "bool" from stdbool.h as well, since C99 (GNU99) is used already. --- src/rexmpp.c | 28 +++++++++++++++------------- src/rexmpp.h | 29 +++++++++++++++-------------- src/rexmpp.rs | 7 ++++--- src/rexmpp_dns.h | 3 ++- src/rexmpp_dns.rs | 2 +- src/rexmpp_tcp.c | 7 +++---- src/rexmpp_tcp.h | 3 ++- src/rexmpp_tcp.rs | 14 +++++--------- 8 files changed, 47 insertions(+), 46 deletions(-) (limited to 'src') diff --git a/src/rexmpp.c b/src/rexmpp.c index 7edf610..24b9965 100644 --- a/src/rexmpp.c +++ b/src/rexmpp.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "config.h" @@ -586,30 +587,29 @@ rexmpp_err_t rexmpp_init (rexmpp_t *s, s->carbons_state = REXMPP_CARBONS_INACTIVE; s->manual_host = NULL; s->manual_port = 5222; - s->manual_direct_tls = 0; + s->manual_direct_tls = false; s->disco_node = "rexmpp"; s->socks_host = NULL; s->server_host = NULL; - s->enable_carbons = 1; - s->manage_roster = 1; + s->enable_carbons = true; + s->manage_roster = true; s->roster_cache_file = NULL; - s->track_roster_presence = 1; - s->track_roster_events = 1; - s->nick_notifications = 1; + s->track_roster_presence = true; + s->track_roster_events = true; + s->nick_notifications = true; #ifdef HAVE_GPGME - s->retrieve_openpgp_keys = 1; + s->retrieve_openpgp_keys = true; #else - s->retrieve_openpgp_keys = 0; + s->retrieve_openpgp_keys = false; #endif - s->autojoin_bookmarked_mucs = 1; + s->autojoin_bookmarked_mucs = true; s->tls_policy = REXMPP_TLS_REQUIRE; - s->enable_jingle = 1; + s->enable_jingle = true; s->client_name = PACKAGE_NAME; s->client_type = "console"; s->client_version = PACKAGE_VERSION; s->local_address = NULL; - s->jingle_prefer_rtcp_mux = 1; - s->path_mtu_discovery = -1; + s->jingle_prefer_rtcp_mux = true; s->muc_ping_default_delay = 600; s->send_buffer = NULL; s->send_queue = NULL; @@ -618,6 +618,7 @@ rexmpp_err_t rexmpp_init (rexmpp_t *s, s->server_srv_tls = NULL; s->server_srv_tls_cur = -1; s->server_socket = -1; + s->server_socket_dns_secure = false; s->current_element_root = NULL; s->current_element = NULL; s->input_queue = NULL; @@ -650,8 +651,9 @@ rexmpp_err_t rexmpp_init (rexmpp_t *s, s->xml_out_cb = NULL; s->roster_modify_cb = NULL; s->console_print_cb = NULL; + s->socket_cb = NULL; s->ping_delay = 600; - s->ping_requested = 0; + s->ping_requested = false; s->last_network_activity.tv_sec = 0; s->last_network_activity.tv_nsec = 0; s->muc_ping = NULL; diff --git a/src/rexmpp.h b/src/rexmpp.h index 1f7ab32..0aa6252 100644 --- a/src/rexmpp.h +++ b/src/rexmpp.h @@ -10,6 +10,7 @@ #define REXMPP_H #include +#include #include "config.h" @@ -251,6 +252,7 @@ typedef int (*xml_in_cb_t) (rexmpp_t *s, rexmpp_xml_t *node); typedef int (*xml_out_cb_t) (rexmpp_t *s, rexmpp_xml_t *node); typedef void (*roster_modify_cb_t) (rexmpp_t *s, rexmpp_xml_t *item); typedef int (*console_print_cb_t) (rexmpp_t *s, const char *format, va_list args); +typedef void (*socket_cb_t) (rexmpp_t *s, int socket); /** @brief Complete connection state */ struct rexmpp @@ -272,7 +274,7 @@ struct rexmpp /* Manual host/port configuration. */ const char *manual_host; uint16_t manual_port; - int manual_direct_tls; + bool manual_direct_tls; /* Miscellaneous settings */ const char *disco_node; @@ -282,23 +284,21 @@ struct rexmpp uint16_t socks_port; /* Various knobs (these are used instead of loadable modules). */ - int enable_carbons; /* XEP-0280 */ - int manage_roster; + bool enable_carbons; /* XEP-0280 */ + bool manage_roster; const char *roster_cache_file; - int track_roster_presence; - int track_roster_events; /* XEP-0163 */ - int nick_notifications; /* XEP-0172 */ - int retrieve_openpgp_keys; /* XEP-0373 */ - int autojoin_bookmarked_mucs; /* XEP-0402 */ + bool track_roster_presence; + bool track_roster_events; /* XEP-0163 */ + bool nick_notifications; /* XEP-0172 */ + bool retrieve_openpgp_keys; /* XEP-0373 */ + bool autojoin_bookmarked_mucs; /* XEP-0402 */ enum tls_pol tls_policy; - int enable_jingle; + bool enable_jingle; const char *client_name; /* XEP-0030, XEP-0092 */ const char *client_type; /* XEP-0030 */ const char *client_version; /* XEP-0092 */ const char *local_address; /* For ICE, XEP-0176 */ - int jingle_prefer_rtcp_mux; - int path_mtu_discovery; /* An IP_MTU_DISCOVER parameter for - TCP sockets, or -1 to not set it */ + bool jingle_prefer_rtcp_mux; /* A delay in seconds, to use for MUC self-ping by default */ unsigned int muc_ping_default_delay; @@ -322,6 +322,7 @@ struct rexmpp xml_out_cb_t xml_out_cb; roster_modify_cb_t roster_modify_cb; console_print_cb_t console_print_cb; + socket_cb_t socket_cb; /* Stream-related state. */ struct rexmpp_jid assigned_jid; @@ -358,7 +359,7 @@ struct rexmpp /* Server ping configuration and state. */ unsigned int ping_delay; - int ping_requested; + bool ping_requested; struct timespec last_network_activity; /* MUC self-ping */ @@ -380,7 +381,7 @@ struct rexmpp int server_socket; /* Whether the address it's connected to was verified with DNSSEC. */ - int server_socket_dns_secure; + bool server_socket_dns_secure; /* A structure used to establish a TCP connection. */ rexmpp_tcp_conn_t server_connection; diff --git a/src/rexmpp.rs b/src/rexmpp.rs index a6950a4..250da00 100644 --- a/src/rexmpp.rs +++ b/src/rexmpp.rs @@ -94,6 +94,9 @@ fn (s: *mut Rexmpp, cb_data: *mut c_void, request: *mut rexmpp_xml::RexmppXML, response: *mut rexmpp_xml::RexmppXML, success: c_int) -> (); +type SocketCallback = unsafe extern "C" +fn (s: *mut Rexmpp, socket: c_int) -> (); + #[repr(C)] pub struct RexmppIQ { pub requset: *mut rexmpp_xml::RexmppXML, @@ -153,9 +156,6 @@ pub struct Rexmpp { pub client_version: *const c_char, // XEP-0092 pub local_address: *const c_char, // For ICE, XEP-0176 pub jingle_prefer_rtcp_mux: bool, - // An IP_MTU_DISCOVER parameter for TCP sockets, or -1 to not set - // it - pub path_mtu_discovery: c_int, // A delay in seconds, to use for MUC self-ping by default pub muc_ping_default_delay: c_uint, // Resource limits @@ -182,6 +182,7 @@ pub struct Rexmpp { pub xml_out_cb: *const c_void, pub roster_modify_cb: *const c_void, pub console_print_cb: *const c_void, + pub socket_cb: Option, // Stream-related state pub assigned_jid: rexmpp_jid::RexmppJID, diff --git a/src/rexmpp_dns.h b/src/rexmpp_dns.h index 06aea8a..7abd2ef 100644 --- a/src/rexmpp_dns.h +++ b/src/rexmpp_dns.h @@ -12,6 +12,7 @@ #define REXMPP_DNS_H #include +#include #include "config.h" #include "rexmpp.h" @@ -58,7 +59,7 @@ struct rexmpp_dns_result { int *len; /** @brief Whether the result was retrieved securely (that is, verified with DNSSEC). */ - int secure; + bool secure; }; typedef struct rexmpp_dns_result rexmpp_dns_result_t; diff --git a/src/rexmpp_dns.rs b/src/rexmpp_dns.rs index 8ea8f31..1489835 100644 --- a/src/rexmpp_dns.rs +++ b/src/rexmpp_dns.rs @@ -29,7 +29,7 @@ extern { pub struct RexmppDNSResult { pub data: *mut *mut c_void, pub len: *mut c_int, - pub secure: c_int + pub secure: bool } #[repr(C)] diff --git a/src/rexmpp_tcp.c b/src/rexmpp_tcp.c index a9c5547..c6a53a5 100644 --- a/src/rexmpp_tcp.c +++ b/src/rexmpp_tcp.c @@ -106,10 +106,9 @@ int rexmpp_tcp_socket(rexmpp_t *s, int domain) { int flags = fcntl(sock, F_GETFL, 0); fcntl(sock, F_SETFL, flags | O_NONBLOCK); - /* Set path MTU discovery, if provided */ - if (s->path_mtu_discovery != -1) { - setsockopt(sock, SOL_IP, IP_MTU_DISCOVER, &(s->path_mtu_discovery), - sizeof(s->path_mtu_discovery)); + /* Call the socket creation callback, if provided */ + if (s->socket_cb != NULL) { + s->socket_cb(s, sock); } return sock; diff --git a/src/rexmpp_tcp.h b/src/rexmpp_tcp.h index 1440fa1..8ee32a0 100644 --- a/src/rexmpp_tcp.h +++ b/src/rexmpp_tcp.h @@ -20,6 +20,7 @@ #define REXMPP_TCP_H #include +#include #include "rexmpp.h" #include "rexmpp_dns.h" @@ -104,7 +105,7 @@ struct rexmpp_tcp_connection { int fd; /** @brief Whether the A or AAAA records used to establish the final connection were verified with DNSSEC. */ - int dns_secure; + bool dns_secure; }; /** diff --git a/src/rexmpp_tcp.rs b/src/rexmpp_tcp.rs index d895004..56c204c 100644 --- a/src/rexmpp_tcp.rs +++ b/src/rexmpp_tcp.rs @@ -1,6 +1,6 @@ use std::os::raw::{c_int, c_char}; use libc::*; -use std::ptr::null_mut; +use std::ptr::{null_mut,null}; use std::mem; use errno::{errno}; @@ -52,7 +52,7 @@ pub struct RexmppTCPConnection { pub connection_attempts: c_int, pub next_connection_time: timespec, pub fd: c_int, - pub dns_secure: c_int + pub dns_secure: bool } #[no_mangle] @@ -150,12 +150,8 @@ fn rexmpp_tcp_socket (s: *mut rexmpp::Rexmpp, domain: c_int) -> c_int { let flags: c_int = fcntl(sock, F_GETFL, 0); fcntl(sock, F_SETFL, flags | O_NONBLOCK); - // Set path MTU discovery, if provided - if (*s).path_mtu_discovery != -1 { - setsockopt(sock, SOL_IP, IP_MTU_DISCOVER, - &mut (*s).path_mtu_discovery as *mut i32 as *mut c_void, - mem::size_of::() as u32); - } + // Call the socket creation callback, if provided + ((*s).socket_cb).map(|cb| cb(s, sock)); return sock; } @@ -175,7 +171,7 @@ fn rexmpp_tcp_conn_init (s: *mut rexmpp::Rexmpp, (*conn).resolved_v4 = null_mut(); (*conn).resolved_v6 = null_mut(); (*conn).fd = -1; - (*conn).dns_secure = 0; + (*conn).dns_secure = false; (*conn).next_connection_time.tv_sec = 0; (*conn).next_connection_time.tv_nsec = 0; -- cgit v1.2.3