diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/libmdsclient/comm.c | 53 | ||||
-rw-r--r-- | 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 <http://www.gnu.org/licenses/>. */ #include "comm.h" -#include "address.h" #include <stdlib.h> #include <unistd.h> @@ -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; @@ -186,6 +176,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 <stdint.h> #include <stddef.h> #include <pthread.h> @@ -136,6 +138,25 @@ __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 * |