diff options
Diffstat (limited to '')
-rw-r--r-- | src/libmdsclient.h | 1 | ||||
-rw-r--r-- | src/libmdsclient/address.c | 46 | ||||
-rw-r--r-- | src/libmdsclient/address.h | 85 | ||||
-rw-r--r-- | src/libmdsclient/comm.c | 72 | ||||
-rw-r--r-- | src/libmdsclient/comm.h | 27 |
5 files changed, 227 insertions, 4 deletions
diff --git a/src/libmdsclient.h b/src/libmdsclient.h index e2347d3..944dc2d 100644 --- a/src/libmdsclient.h +++ b/src/libmdsclient.h @@ -21,6 +21,7 @@ #include "libmdsclient/proto-util.h" #include "libmdsclient/comm.h" +#include "libmdsclient/address.h" #endif diff --git a/src/libmdsclient/address.c b/src/libmdsclient/address.c new file mode 100644 index 0000000..3a7aba3 --- /dev/null +++ b/src/libmdsclient/address.c @@ -0,0 +1,46 @@ +/** + * mds — A micro-display server + * Copyright © 2014, 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#include "address.h" + +#include <sys/un.h> +#include <limits.h> + + + +/** + * Parse a display address string + * + * @param display The address in MDS_DISPLAY-formatting, must not be `NULL` + * @param address Output parameter for parsed address, must not be `NULL` + * @return Zero on success, even if parsing failed, -1 on error, + * `errno` will have been set accordinly on error + * + * @throws ENOMEM Out of memory. Possibly, the application hit the + * RLIMIT_AS or RLIMIT_DATA limit described in getrlimit(2). + */ +int libmds_parse_display_adress(const char* restrict display, libmds_display_address_t* restrict address) +{ + address->domain = -1; + address->type = -1; + address->protocol = -1; + address->address = NULL; + address->address_len = 0; + + return (void) display, 0; /* TODO */ +} + diff --git a/src/libmdsclient/address.h b/src/libmdsclient/address.h new file mode 100644 index 0000000..b094772 --- /dev/null +++ b/src/libmdsclient/address.h @@ -0,0 +1,85 @@ +/** + * mds — A micro-display server + * Copyright © 2014, 2015 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +#ifndef MDS_LIBMDSCLIENT_ADDRESS_H +#define MDS_LIBMDSCLIENT_ADDRESS_H + + +#include <sys/socket.h> + + +/** + * The address of the display, parsed into arguments + */ +typedef struct libmds_display_address +{ + /** + * The domain (protocol family), that is + * the first argument for socket(2), a + * value whose constant is prefixed PF_; + * -1 if not detected + */ + int domain; + + /** + * The socket type, that is the second + * argument for socket(2), a value whose + * constant is prefixed SOCK_; -1 if not + * detected + */ + int type; + + /** + * The protocol, that is the third + * argument for socket(2), a value whose + * constant is prefixed IPPROTO_ (zero + * for the default); -1 if not detected + */ + int protocol; + + /** + * The address, `NULL` if not detected, + * you are responsible for freeing this + */ + struct sockaddr* address; + + /** + * The size of `address`, may be set + * even if `address` is `NULL` + */ + socklen_t address_len; + +} libmds_display_address_t; + + +/** + * Parse a display address string + * + * @param display The address in MDS_DISPLAY-formatting, must not be `NULL` + * @param address Output parameter for parsed address, must not be `NULL` + * @return Zero on success, even if parsing failed, -1 on error, + * `errno` will have been set accordinly on error + * + * @throws ENOMEM Out of memory. Possibly, the application hit the + * RLIMIT_AS or RLIMIT_DATA limit described in getrlimit(2). + */ +__attribute__((nonnull)) +int libmds_parse_display_adress(const char* restrict display, libmds_display_address_t* restrict address); + + +#endif + diff --git a/src/libmdsclient/comm.c b/src/libmdsclient/comm.c index a01d1d7..11d03d4 100644 --- a/src/libmdsclient/comm.c +++ b/src/libmdsclient/comm.c @@ -16,11 +16,15 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include "comm.h" +#include "address.h" #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> +#include <string.h> + +#include <libmdsserver/config.h> @@ -119,6 +123,70 @@ void libmds_connection_free(libmds_connection_t* restrict this) /** + * Connect to the display server + * + * @param this The connection descriptor, must not be `NULL` + * @param display Pointer to `NULL` to select display be looking at + * the environment. Pointer to a string with the + * address (formatted as the environment variable + * MDS_DISPLAY) if manually specified. The pointer + * itself must not be `NULL`; it will be updated + * with the address if it points to NULL. + * @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 If the display server's address is not properly + * formatted, or specifies an unsupported protocol, + * `libmds_parse_display_adress` can be used to + * figure out what is wrong. + * @throws Any error specified for socket(2) + * @throws Any error specified for connect(2), except EINTR + */ +int libmds_connection_establish(libmds_connection_t* restrict this, const char** restrict display) +{ + libmds_display_address_t addr; + int saved_errno; + + addr.address = NULL; + + if (*display == NULL) + *display = getenv("MDS_DISPLAY"); + + if ((*display == NULL) || (strchr(*display, ':') == NULL)) + goto efault; + + 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) + goto fail; + + while (connect(this->socket_fd, (struct sockaddr*)(addr.address), addr.address_len)) + if (errno != EINTR) + goto fail; + + free(addr.address); + return 0; + + efault: + free(addr.address); + return errno = EFAULT, -1; + + fail: + saved_errno = errno; + free(addr.address); + return errno = saved_errno, -1; +} + + +/** * Wrapper for `libmds_connection_send_unlocked` that locks * the mutex of the connection * @@ -142,7 +210,7 @@ void libmds_connection_free(libmds_connection_t* restrict this) * @throws EPIPE See send(2) * @throws See pthread_mutex_lock(3) */ -size_t libmds_connection_send(libmds_connection_t* restrict this, const char* message, size_t length) +size_t libmds_connection_send(libmds_connection_t* restrict this, const char* restrict message, size_t length) { int saved_errno; size_t r; @@ -183,7 +251,7 @@ size_t libmds_connection_send(libmds_connection_t* restrict this, const char* me * @throws ENOTSOCK See send(2) * @throws EPIPE See send(2) */ -size_t libmds_connection_send_unlocked(libmds_connection_t* restrict this, const char* message, +size_t libmds_connection_send_unlocked(libmds_connection_t* restrict this, const char* restrict message, size_t length, int continue_on_interrupt) { size_t block_size = length; diff --git a/src/libmdsclient/comm.h b/src/libmdsclient/comm.h index e982545..b868e77 100644 --- a/src/libmdsclient/comm.h +++ b/src/libmdsclient/comm.h @@ -109,6 +109,29 @@ void libmds_connection_destroy(libmds_connection_t* restrict this); */ void libmds_connection_free(libmds_connection_t* restrict this); +/** + * Connect to the display server + * + * @param this The connection descriptor, must not be `NULL` + * @param display Pointer to `NULL` to select display be looking at + * the environment. Pointer to a string with the + * address (formatted as the environment variable + * MDS_DISPLAY) if manually specified. The pointer + * itself must not be `NULL`; it will be updated + * with the address if it points to NULL. + * @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 If the display server's address is not properly + * formatted, or specifies an unsupported protocol, + * `libmds_parse_display_adress` can be used to + * figure out what is wrong. + * @throws Any error specified for socket(2) + * @throws Any error specified for connect(2), except EINTR + */__attribute__((nonnull)) +int libmds_connection_establish(libmds_connection_t* restrict this, const char** restrict display); /** * Wrapper for `libmds_connection_send_unlocked` that locks @@ -135,7 +158,7 @@ void libmds_connection_free(libmds_connection_t* restrict this); * @throws See pthread_mutex_lock(3) */ __attribute__((nonnull)) -size_t libmds_connection_send(libmds_connection_t* restrict this, const char* message, size_t length); +size_t libmds_connection_send(libmds_connection_t* restrict this, const char* restrict message, size_t length); /** * Send a message to the display server, without locking the @@ -163,7 +186,7 @@ size_t libmds_connection_send(libmds_connection_t* restrict this, const char* me * @throws EPIPE See send(2) */ __attribute__((nonnull)) -size_t libmds_connection_send_unlocked(libmds_connection_t* restrict this, const char* message, +size_t libmds_connection_send_unlocked(libmds_connection_t* restrict this, const char* restrict message, size_t length, int continue_on_interrupt); /** |