From 0d546ff6ebe0363cab0d21d0c9b8d38d4ee3ac80 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 31 Aug 2015 21:24:11 +0200 Subject: add libmds_connection_establish_address MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/libmdsclient/comm.c | 53 +++++++++++++++++++++++++++++++++++++++---------- src/libmdsclient/comm.h | 21 ++++++++++++++++++++ 2 files changed, 63 insertions(+), 11 deletions(-) diff --git a/src/libmdsclient/comm.c b/src/libmdsclient/comm.c index 91502f3..0e1485b 100644 --- a/src/libmdsclient/comm.c +++ b/src/libmdsclient/comm.c @@ -16,7 +16,6 @@ * along with this program. If not, see . */ #include "comm.h" -#include "address.h" #include #include @@ -158,19 +157,10 @@ int libmds_connection_establish(libmds_connection_t* restrict this, const char** if (libmds_parse_display_adress(*display, &addr) < 0) goto fail; - if (addr.domain < 0) goto efault; - if (addr.type < 0) goto efault; - if (addr.protocol < 0) goto efault; - if (addr.address == NULL) goto efault; - this->socket_fd = socket(addr.domain, addr.type, addr.protocol); - if (this->socket_fd < 0) + if (libmds_connection_establish_address(this, &addr) < 0) goto fail; - while (connect(this->socket_fd, addr.address, addr.address_len)) - if (errno != EINTR) - goto fail; - free(addr.address); return 0; @@ -185,6 +175,47 @@ int libmds_connection_establish(libmds_connection_t* restrict this, const char** } +/** + * Connect to the display server + * + * @param this The connection descriptor, must not be `NULL` + * @param address The address to connect to, must not be `NULL`, + * and must be the result of a successful call to + * `libmds_parse_display_adress` + * @return Zero on success, -1 on error. On error, `display` + * will point to `NULL` if MDS_DISPLAY is not defiend, + * otherwise, `errno` will have been set to describe + * the error. + * + * @throws EFAULT `libmds_display_address_t` contains unset parameters. + * @throws Any error specified for socket(2) + * @throws Any error specified for connect(2), except EINTR + */ +int libmds_connection_establish_address(libmds_connection_t* restrict this, + const libmds_display_address_t* restrict address) +{ + if (address->domain < 0) goto efault; + if (address->type < 0) goto efault; + if (address->protocol < 0) goto efault; + if (address->address == NULL) goto efault; + + this->socket_fd = socket(address->domain, address->type, address->protocol); + if (this->socket_fd < 0) + goto fail; + + while (connect(this->socket_fd, address->address, address->address_len)) + if (errno != EINTR) + goto fail; + + return 0; + + efault: + errno = EFAULT; + fail: + return -1; +} + + /** * Wrapper for `libmds_connection_send_unlocked` that locks * the mutex of the connection diff --git a/src/libmdsclient/comm.h b/src/libmdsclient/comm.h index 94dcc7a..d26a2e2 100644 --- a/src/libmdsclient/comm.h +++ b/src/libmdsclient/comm.h @@ -19,6 +19,8 @@ #define MDS_LIBMDSCLIENT_COMM_H +#include "address.h" + #include #include #include @@ -135,6 +137,25 @@ void libmds_connection_free(libmds_connection_t* restrict this); __attribute__((nonnull)) int libmds_connection_establish(libmds_connection_t* restrict this, const char** restrict display); +/** + * Connect to the display server + * + * @param this The connection descriptor, must not be `NULL` + * @param address The address to connect to, must not be `NULL`, + * and must be the result of a successful call to + * `libmds_parse_display_adress` + * @return Zero on success, -1 on error. On error, `display` + * will point to `NULL` if MDS_DISPLAY is not defiend, + * otherwise, `errno` will have been set to describe + * the error. + * + * @throws Any error specified for socket(2) + * @throws Any error specified for connect(2), except EINTR + */ +__attribute__((nonnull)) +int libmds_connection_establish_address(libmds_connection_t* restrict this, + const libmds_display_address_t* restrict address); + /** * Wrapper for `libmds_connection_send_unlocked` that locks * the mutex of the connection -- cgit v1.2.3-70-g09d2