From fa7d6b32530d3d8c2d7e542a15319c74c22061b6 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Thu, 26 Sep 2013 18:48:59 +0000 Subject: moving from abstract unix domain socket paths to normal unix domain socket paths (#2887), removing now unnecessary/dead flags argument to GNUNET_NETWORK_socket_bind --- src/util/client.c | 30 +++++++++++++----------------- src/util/connection.c | 15 +++------------ src/util/disk.c | 12 ++++++------ src/util/network.c | 28 ++++++++-------------------- src/util/resolver.conf.in | 2 +- src/util/server.c | 2 +- src/util/service.c | 24 ++++++++++-------------- src/util/test_connection.c | 6 +++--- src/util/test_connection_addressing.c | 2 +- src/util/test_connection_receive_cancel.c | 6 +++--- src/util/test_connection_timeout.c | 6 +++--- src/util/test_server_with_client_unix.c | 7 +------ 12 files changed, 53 insertions(+), 87 deletions(-) (limited to 'src/util') diff --git a/src/util/client.c b/src/util/client.c index 30c80bf03..64960f232 100644 --- a/src/util/client.c +++ b/src/util/client.c @@ -251,7 +251,9 @@ try_unixpath (const char *service_name, struct sockaddr_un s_un; unixpath = NULL; - if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", &unixpath)) && + if ((GNUNET_OK == + GNUNET_CONFIGURATION_get_value_filename (cfg, service_name, + "UNIXPATH", &unixpath)) && (0 < strlen (unixpath))) { /* We have a non-NULL unixpath, need to validate it */ @@ -299,7 +301,8 @@ test_service_configuration (const char *service_name, #if AF_UNIX char *unixpath = NULL; - if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", &unixpath)) && + if ((GNUNET_OK == + GNUNET_CONFIGURATION_get_value_filename (cfg, service_name, "UNIXPATH", &unixpath)) && (0 < strlen (unixpath))) ret = GNUNET_OK; GNUNET_free_non_null (unixpath); @@ -829,11 +832,12 @@ GNUNET_CLIENT_service_test (const char *service, { /* probe UNIX support */ struct sockaddr_un s_un; - size_t slen; char *unixpath; unixpath = NULL; - if ((GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg, service, "UNIXPATH", &unixpath)) && (0 < strlen (unixpath))) /* We have a non-NULL unixpath, does that mean it's valid? */ + if ((GNUNET_OK == + GNUNET_CONFIGURATION_get_value_filename (cfg, service, "UNIXPATH", &unixpath)) && + (0 < strlen (unixpath))) /* We have a non-NULL unixpath, does that mean it's valid? */ { if (strlen (unixpath) >= sizeof (s_un.sun_path)) { @@ -852,21 +856,13 @@ GNUNET_CLIENT_service_test (const char *service, { memset (&s_un, 0, sizeof (s_un)); s_un.sun_family = AF_UNIX; - slen = strlen (unixpath) + 1; - if (slen >= sizeof (s_un.sun_path)) - slen = sizeof (s_un.sun_path) - 1; - memcpy (s_un.sun_path, unixpath, slen); - s_un.sun_path[slen] = '\0'; - slen = sizeof (struct sockaddr_un); -#if LINUX - s_un.sun_path[0] = '\0'; -#endif + strncpy (s_un.sun_path, unixpath, sizeof (s_un.sun_path) - 1); #if HAVE_SOCKADDR_IN_SIN_LEN - s_un.sun_len = (u_char) slen; + s_un.sun_len = (u_char) sizeof (struct sockaddr_un); #endif if (GNUNET_OK != GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_un, - slen, GNUNET_BIND_EXCLUSIVE)) + sizeof (struct sockaddr_un))) { /* failed to bind => service must be running */ GNUNET_free (unixpath); @@ -916,7 +912,7 @@ GNUNET_CLIENT_service_test (const char *service, { if (GNUNET_OK != GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in, - sizeof (s_in), GNUNET_BIND_EXCLUSIVE)) + sizeof (s_in))) { /* failed to bind => service must be running */ GNUNET_free (hostname); @@ -949,7 +945,7 @@ GNUNET_CLIENT_service_test (const char *service, { if (GNUNET_OK != GNUNET_NETWORK_socket_bind (sock, (const struct sockaddr *) &s_in6, - sizeof (s_in6), GNUNET_BIND_EXCLUSIVE)) + sizeof (s_in6))) { /* failed to bind => service must be running */ GNUNET_free (hostname); diff --git a/src/util/connection.c b/src/util/connection.c index 3290abcfc..c8d9919ea 100644 --- a/src/util/connection.c +++ b/src/util/connection.c @@ -828,22 +828,13 @@ GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct #ifdef AF_UNIX struct GNUNET_CONNECTION_Handle *connection; struct sockaddr_un *un; - size_t slen; GNUNET_assert (0 < strlen (unixpath)); /* sanity check */ un = GNUNET_new (struct sockaddr_un); un->sun_family = AF_UNIX; - slen = strlen (unixpath); - if (slen >= sizeof (un->sun_path)) - slen = sizeof (un->sun_path) - 1; - memcpy (un->sun_path, unixpath, slen); - un->sun_path[slen] = '\0'; - slen = sizeof (struct sockaddr_un); + strncpy(un->sun_path, unixpath, sizeof(un->sun_path) - 1); #if HAVE_SOCKADDR_IN_SIN_LEN - un->sun_len = (u_char) slen; -#endif -#if LINUX - un->sun_path[0] = '\0'; + un->sun_len = (u_char) sizeof (struct sockaddr_un); #endif connection = GNUNET_new (struct GNUNET_CONNECTION_Handle); connection->cfg = cfg; @@ -852,7 +843,7 @@ GNUNET_CONNECTION_create_from_connect_to_unixpath (const struct connection->port = 0; connection->hostname = NULL; connection->addr = (struct sockaddr *) un; - connection->addrlen = slen; + connection->addrlen = sizeof (struct sockaddr_un); connection->sock = GNUNET_NETWORK_socket_create (AF_UNIX, SOCK_STREAM, 0); if (NULL == connection->sock) { diff --git a/src/util/disk.c b/src/util/disk.c index 066b0b99b..34480e36a 100644 --- a/src/util/disk.c +++ b/src/util/disk.c @@ -759,13 +759,12 @@ GNUNET_DISK_directory_create (const char *dir) /** - * Create the directory structure for storing - * a file. + * Create the directory structure for storing a file. * * @param filename name of a file in the directory - * @returns GNUNET_OK on success, - * GNUNET_SYSERR on failure, - * GNUNET_NO if the directory + * @returns #GNUNET_OK on success, + * #GNUNET_SYSERR on failure, + * #GNUNET_NO if the directory * exists but is not writeable for us */ int @@ -792,10 +791,11 @@ GNUNET_DISK_directory_create_for_file (const char *filename) /** * Read the contents of a binary file into a buffer. + * * @param h handle to an open file * @param result the buffer to write the result to * @param len the maximum number of bytes to read - * @return the number of bytes read on success, GNUNET_SYSERR on failure + * @return the number of bytes read on success, #GNUNET_SYSERR on failure */ ssize_t GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle * h, void *result, diff --git a/src/util/network.c b/src/util/network.c index c0b977368..793290b45 100644 --- a/src/util/network.c +++ b/src/util/network.c @@ -87,7 +87,9 @@ GNUNET_NETWORK_test_pf (int pf) { if (EAFNOSUPPORT == errno) return GNUNET_NO; - fprintf (stderr, "Failed to create test socket: %s\n", STRERROR (errno)); + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "Failed to create test socket: %s\n", + STRERROR (errno)); return GNUNET_SYSERR; } #if WINDOWS @@ -383,20 +385,18 @@ GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc, * @param desc socket to bind * @param address address to be bound * @param address_len length of @a address - * @param flags flags affecting bind behaviour * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise */ int GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc, const struct sockaddr *address, - socklen_t address_len, - int flags) + socklen_t address_len) { int ret; socklen_t bind_address_len = address_len; #ifdef LINUX - if (address->sa_family == AF_UNIX) + if (AF_UNIX == address->sa_family) { const struct sockaddr_un *address_un = (const struct sockaddr_un *)address; if (address_un->sun_path[0] == '\0') @@ -430,16 +430,6 @@ GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc, && (0 != setsockopt (desc->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)))) LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG, "setsockopt"); } -#endif -#ifndef LINUX -#ifndef MINGW - if (address->sa_family == AF_UNIX && (flags & GNUNET_BIND_EXCLUSIVE) == 0) - { - const struct sockaddr_un *un = (const struct sockaddr_un *) address; - - (void) unlink (un->sun_path); - } -#endif #endif ret = bind (desc->fd, address, bind_address_len); #ifdef MINGW @@ -449,11 +439,9 @@ GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc, if (ret != 0) return GNUNET_SYSERR; #ifndef MINGW -#ifndef LINUX desc->addr = GNUNET_malloc (address_len); memcpy (desc->addr, address, address_len); desc->addrlen = address_len; -#endif #endif return GNUNET_OK; } @@ -483,16 +471,16 @@ GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc) #else ret = close (desc->fd); #endif -#ifndef LINUX #ifndef MINGW if ((desc->af == AF_UNIX) && (NULL != desc->addr)) { const struct sockaddr_un *un = (const struct sockaddr_un *) desc->addr; if (0 != unlink (un->sun_path)) - LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", un->sun_path); + LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, + "unlink", + un->sun_path); } -#endif #endif GNUNET_NETWORK_socket_free_memory_only_ (desc); return (ret == 0) ? GNUNET_OK : GNUNET_SYSERR; diff --git a/src/util/resolver.conf.in b/src/util/resolver.conf.in index cccb60c2f..005082ea5 100644 --- a/src/util/resolver.conf.in +++ b/src/util/resolver.conf.in @@ -6,7 +6,7 @@ HOME = $SERVICEHOME BINARY = gnunet-service-resolver ACCEPT_FROM = 127.0.0.1; ACCEPT_FROM6 = ::1; -UNIXPATH = /tmp/gnunet-service-resolver.sock +UNIXPATH = $SERVICEHOME/gnunet-service-resolver.sock UNIX_MATCH_UID = NO UNIX_MATCH_GID = NO # DISABLE_SOCKET_FORWARDING = NO diff --git a/src/util/server.c b/src/util/server.c index 9e88992ca..4df8fd83d 100644 --- a/src/util/server.c +++ b/src/util/server.c @@ -469,7 +469,7 @@ open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen) return NULL; } /* bind the socket */ - if (GNUNET_OK != GNUNET_NETWORK_socket_bind (sock, serverAddr, socklen, 0)) + if (GNUNET_OK != GNUNET_NETWORK_socket_bind (sock, serverAddr, socklen)) { eno = errno; if (EADDRINUSE != errno) diff --git a/src/util/service.c b/src/util/service.c index b91be9664..489dd2e55 100644 --- a/src/util/service.c +++ b/src/util/service.c @@ -841,24 +841,15 @@ add_unixpath (struct sockaddr **saddrs, socklen_t * saddrlens, { #ifdef AF_UNIX struct sockaddr_un *un; - size_t slen; un = GNUNET_malloc (sizeof (struct sockaddr_un)); un->sun_family = AF_UNIX; - slen = strlen (unixpath) + 1; - if (slen >= sizeof (un->sun_path)) - slen = sizeof (un->sun_path) - 1; - memcpy (un->sun_path, unixpath, slen); - un->sun_path[slen] = '\0'; - slen = sizeof (struct sockaddr_un); -#if LINUX - un->sun_path[0] = '\0'; -#endif + strncpy (un->sun_path, unixpath, sizeof (un->sun_path) - 1); #if HAVE_SOCKADDR_IN_SIN_LEN - un->sun_len = (u_char) slen; + un->sun_len = (u_char) sizeof (struct sockaddr_un); #endif *saddrs = (struct sockaddr *) un; - *saddrlens = slen; + *saddrlens = sizeof (struct sockaddr_un); #else /* this function should never be called * unless AF_UNIX is defined! */ @@ -980,8 +971,8 @@ GNUNET_SERVICE_get_server_addresses (const char *service_name, if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (cfg, service_name, "UNIXPATH")) && (GNUNET_OK == - GNUNET_CONFIGURATION_get_value_string (cfg, service_name, "UNIXPATH", - &unixpath)) && + GNUNET_CONFIGURATION_get_value_filename (cfg, service_name, "UNIXPATH", + &unixpath)) && (0 < strlen (unixpath))) { /* probe UNIX support */ @@ -996,6 +987,11 @@ GNUNET_SERVICE_get_server_addresses (const char *service_name, LOG (GNUNET_ERROR_TYPE_INFO, _("Using `%s' instead\n"), unixpath); } + if (GNUNET_OK != + GNUNET_DISK_directory_create_for_file (unixpath)) + GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, + "mkdir", + unixpath); } if (NULL != unixpath) { diff --git a/src/util/test_connection.c b/src/util/test_connection.c index 1ba20498f..6644623d4 100644 --- a/src/util/test_connection.c +++ b/src/util/test_connection.c @@ -65,9 +65,9 @@ open_listen_socket () if (GNUNET_NETWORK_socket_setsockopt (desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK) GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "setsockopt"); - GNUNET_assert (GNUNET_NETWORK_socket_bind - (desc, (const struct sockaddr *) &sa, - sizeof (sa), 0) == GNUNET_OK); + GNUNET_assert (GNUNET_OK == + GNUNET_NETWORK_socket_bind (desc, (const struct sockaddr *) &sa, + sizeof (sa))); GNUNET_NETWORK_socket_listen (desc, 5); return desc; } diff --git a/src/util/test_connection_addressing.c b/src/util/test_connection_addressing.c index eeb610dd1..e5c8edeeb 100644 --- a/src/util/test_connection_addressing.c +++ b/src/util/test_connection_addressing.c @@ -68,7 +68,7 @@ open_listen_socket () GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "setsockopt"); if (GNUNET_OK != GNUNET_NETWORK_socket_bind (desc, (const struct sockaddr *) &sa, - sizeof (sa), 0)) + sizeof (sa))) { GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "bind"); diff --git a/src/util/test_connection_receive_cancel.c b/src/util/test_connection_receive_cancel.c index 99905e232..c61272a57 100644 --- a/src/util/test_connection_receive_cancel.c +++ b/src/util/test_connection_receive_cancel.c @@ -64,9 +64,9 @@ open_listen_socket () if (GNUNET_NETWORK_socket_setsockopt (desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK) GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "setsockopt"); - GNUNET_assert (GNUNET_NETWORK_socket_bind - (desc, (const struct sockaddr *) &sa, - sizeof (sa), 0) == GNUNET_OK); + GNUNET_assert (GNUNET_OK == + GNUNET_NETWORK_socket_bind (desc, (const struct sockaddr *) &sa, + sizeof (sa))); GNUNET_NETWORK_socket_listen (desc, 5); return desc; } diff --git a/src/util/test_connection_timeout.c b/src/util/test_connection_timeout.c index 6608e18db..7241bbfae 100644 --- a/src/util/test_connection_timeout.c +++ b/src/util/test_connection_timeout.c @@ -61,9 +61,9 @@ open_listen_socket () if (GNUNET_NETWORK_socket_setsockopt (desc, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) != GNUNET_OK) GNUNET_log (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "setsockopt"); - GNUNET_assert (GNUNET_NETWORK_socket_bind - (desc, (const struct sockaddr *) &sa, - sizeof (sa), 0) == GNUNET_OK); + GNUNET_assert (GNUNET_OK == + GNUNET_NETWORK_socket_bind (desc, (const struct sockaddr *) &sa, + sizeof (sa))); GNUNET_NETWORK_socket_listen (desc, 5); return desc; } diff --git a/src/util/test_server_with_client_unix.c b/src/util/test_server_with_client_unix.c index 57a67d3f4..a4e0ffe5d 100644 --- a/src/util/test_server_with_client_unix.c +++ b/src/util/test_server_with_client_unix.c @@ -134,20 +134,15 @@ task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { struct sockaddr_un un; const char *unixpath = "/tmp/testsock"; - size_t slen = strlen (unixpath); struct sockaddr *sap[2]; socklen_t slens[2]; memset (&un, 0, sizeof (un)); un.sun_family = AF_UNIX; - memcpy (un.sun_path, unixpath, slen); - un.sun_path[slen] = '\0'; + strncpy(un.sun_path, unixpath, sizeof (un.sun_path) - 1); #if HAVE_SOCKADDR_IN_SIN_LEN un.sun_len = (u_char) sizeof (un); #endif -#if LINUX - un.sun_path[0] = '\0'; -#endif sap[0] = (struct sockaddr *) &un; slens[0] = sizeof (un); -- cgit v1.2.3