This file is part of GNUnet.
Copyright (C) 2009-2013 GNUnet e.V.
- GNUnet 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, or (at your
- option) any later version.
+ GNUnet is free software: you can redistribute it and/or modify it
+ under the terms of the GNU Affero General Public License as published
+ by the Free Software Foundation, either version 3 of the License,
+ or (at your option) any later version.
GNUnet 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.
+ Affero General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with GNUnet; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- Boston, MA 02110-1301, USA.
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+ SPDX-License-Identifier: AGPL3.0-or-later
*/
/**
int
GNUNET_NETWORK_test_pf (int pf)
{
+ static int cache_v4 = -1;
+ static int cache_v6 = -1;
+ static int cache_un = -1;
int s;
+ int ret;
+ switch (pf)
+ {
+ case PF_INET:
+ if (-1 != cache_v4)
+ return cache_v4;
+ break;
+ case PF_INET6:
+ if (-1 != cache_v6)
+ return cache_v6;
+ break;
+#ifdef PF_UNIX
+ case PF_UNIX:
+ if (-1 != cache_un)
+ return cache_un;
+ break;
+#endif
+ }
s = socket (pf, SOCK_STREAM, 0);
if (-1 == s)
{
- if (EAFNOSUPPORT == errno)
- return GNUNET_NO;
- GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
- "Failed to create test socket: %s\n",
- STRERROR (errno));
- return GNUNET_SYSERR;
+ if (EAFNOSUPPORT != errno)
+ {
+ GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
+ "socket");
+ return GNUNET_SYSERR;
+ }
+ ret = GNUNET_NO;
}
+ else
+ {
#if WINDOWS
- closesocket (s);
+ closesocket (s);
#else
- close (s);
+ close (s);
#endif
- return GNUNET_OK;
+ ret = GNUNET_OK;
+ }
+ switch (pf)
+ {
+ case PF_INET:
+ cache_v4 = ret;
+ break;
+ case PF_INET6:
+ cache_v6 = ret;
+ break;
+#ifdef PF_UNIX
+ case PF_UNIX:
+ cache_un = ret;
+ break;
+#endif
+ }
+ return ret;
}
if (slen < upm)
return unixpath; /* no shortening required */
GNUNET_CRYPTO_hash (unixpath, slen, &sh);
- while (16 +
- strlen (unixpath) >= upm)
+ while (16 + strlen (unixpath) >= upm)
{
if (NULL == (end = strrchr (unixpath, '/')))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- _("Unable to shorten unix path `%s' while keeping name unique\n"),
- unixpath);
+ _("Unable to shorten unix path `%s' while keeping name unique\n"),
+ unixpath);
GNUNET_free (unixpath);
return NULL;
}
*end = '\0';
}
GNUNET_CRYPTO_hash_to_enc (&sh, &ae);
- strncat (unixpath, (char*) ae.encoding, 16);
+ ae.encoding[16] = '\0';
+ strcat (unixpath, (char *) ae.encoding);
return unixpath;
}
int flags = fcntl (fd->fd, F_GETFL);
if (flags == -1)
-
{
LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
"fcntl");
if (0 !=
setsockopt (h->fd, SOL_SOCKET, SO_NOSIGPIPE,
- (const void *) &abs_value,
+ (const void *) &abs_value,
sizeof (abs_value)))
LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "setsockopt");
}
const char *abs_value = "1";
if (0 !=
- setsockopt (h->fd, IPPROTO_TCP, TCP_NODELAY,
- (const void *) abs_value,
+ setsockopt (h->fd,
+ IPPROTO_TCP,
+ TCP_NODELAY,
+ (const void *) abs_value,
sizeof (abs_value)))
LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
"setsockopt");
*/
static int
initialize_network_handle (struct GNUNET_NETWORK_Handle *h,
- int af,
+ int af,
int type)
{
int eno;
#ifdef AF_UNIX
&& (af != AF_UNIX)
#endif
- )
+ )
socket_set_nodelay (h);
return GNUNET_OK;
}
struct GNUNET_NETWORK_Handle *
GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,
struct sockaddr *address,
- socklen_t *address_len)
+ socklen_t *address_len)
{
struct GNUNET_NETWORK_Handle *ret;
int eno;
if (0 == gsn)
LOG (GNUNET_ERROR_TYPE_DEBUG,
- "Accepting connection on `%s'\n",
+ "Accepting connection on `%s'\n",
GNUNET_a2s ((const struct sockaddr *) &name,
namelen));
}
(NULL != address) ? address->sa_family : desc->af,
SOCK_STREAM))
{
-
return NULL;
}
return ret;
const int on = 1;
if (AF_INET6 == desc->af)
- if (setsockopt (desc->fd, IPPROTO_IPV6, IPV6_V6ONLY,
- (const void *) &on,
- sizeof (on)))
+ if (setsockopt (desc->fd,
+ IPPROTO_IPV6,
+ IPV6_V6ONLY,
+ (const void *) &on,
+ sizeof (on)))
LOG_STRERROR (GNUNET_ERROR_TYPE_DEBUG,
"setsockopt");
}
if (0 != unlink (dirname))
{
LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
- "unlink",
- dirname);
+ "unlink",
+ dirname);
}
else
{
to->handles_size,
from->handles_pos * 2);
GNUNET_memcpy (to->handles,
- from->handles,
- from->handles_pos * sizeof (struct GNUNET_NETWORK_Handle *));
+ from->handles,
+ from->handles_pos * sizeof (struct GNUNET_NETWORK_Handle *));
to->handles_pos = from->handles_pos;
#endif
}
* @return POSIX file descriptor
*/
int
-GNUNET_NETWORK_get_fd (struct GNUNET_NETWORK_Handle *desc)
+GNUNET_NETWORK_get_fd (const struct GNUNET_NETWORK_Handle *desc)
{
return desc->fd;
}
* @return sockaddr
*/
struct sockaddr*
-GNUNET_NETWORK_get_addr (struct GNUNET_NETWORK_Handle *desc)
+GNUNET_NETWORK_get_addr (const struct GNUNET_NETWORK_Handle *desc)
{
return desc->addr;
}
* @return socklen_t for sockaddr
*/
socklen_t
-GNUNET_NETWORK_get_addrlen (struct GNUNET_NETWORK_Handle *desc)
+GNUNET_NETWORK_get_addrlen (const struct GNUNET_NETWORK_Handle *desc)
{
return desc->addrlen;
}
#else
int fd;
- GNUNET_DISK_internal_file_handle_ (h,
- &fd,
- sizeof (int));
+ GNUNET_assert (GNUNET_OK ==
+ GNUNET_DISK_internal_file_handle_ (h,
+ &fd,
+ sizeof (int)));
FD_SET (fd,
&fds->sds);
fds->nsds = GNUNET_MAX (fd + 1,
*/
int
GNUNET_NETWORK_test_port_free (int ipproto,
- uint16_t port)
+ uint16_t port)
{
struct GNUNET_NETWORK_Handle *socket;
int bind_status;
struct addrinfo *ai;
GNUNET_snprintf (open_port_str,
- sizeof (open_port_str),
- "%u",
- (unsigned int) port);
- socktype = (IPPROTO_TCP == ipproto)
- ? SOCK_STREAM
- : SOCK_DGRAM;
+ sizeof (open_port_str),
+ "%u",
+ (unsigned int) port);
+ socktype = (IPPROTO_TCP == ipproto) ? SOCK_STREAM : SOCK_DGRAM;
ret = NULL;
memset (&hint, 0, sizeof (hint));
- hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
+ hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
hint.ai_socktype = socktype;
hint.ai_protocol = ipproto;
hint.ai_addrlen = 0;
hint.ai_addr = NULL;
hint.ai_canonname = NULL;
hint.ai_next = NULL;
- hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
+ hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
GNUNET_assert (0 == getaddrinfo (NULL,
- open_port_str,
- &hint,
- &ret));
+ open_port_str,
+ &hint,
+ &ret));
+ bind_status = GNUNET_NO;
for (ai = ret; NULL != ai; ai = ai->ai_next)
{
socket = GNUNET_NETWORK_socket_create (ai->ai_family,
- ai->ai_socktype,
- ai->ai_protocol);
+ ai->ai_socktype,
+ ai->ai_protocol);
if (NULL == socket)
continue;
bind_status = GNUNET_NETWORK_socket_bind (socket,
- ai->ai_addr,
- ai->ai_addrlen);
+ ai->ai_addr,
+ ai->ai_addrlen);
GNUNET_NETWORK_socket_close (socket);
if (GNUNET_OK != bind_status)
break;
_("Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
"select");
}
- tv.tv_sec = timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us;
- tv.tv_usec =
- (timeout.rel_value_us -
- (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
+ if (timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us > (unsigned long long) LONG_MAX)
+ {
+ tv.tv_sec = LONG_MAX;
+ tv.tv_usec = 999999L;
+ }
+ else
+ {
+ tv.tv_sec = (long) (timeout.rel_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us);
+ tv.tv_usec =
+ (timeout.rel_value_us -
+ (tv.tv_sec * GNUNET_TIME_UNIT_SECONDS.rel_value_us));
+ }
return select (nfds,
- (NULL != rfds) ? &rfds->sds : NULL,
+ (NULL != rfds) ? &rfds->sds : NULL,
(NULL != wfds) ? &wfds->sds : NULL,
(NULL != efds) ? &efds->sds : NULL,
(timeout.rel_value_us ==
{
LOG (GNUNET_ERROR_TYPE_DEBUG,
"Adding the socket event to the array as %d\n",
- nhandles);
+ nhandles);
handle_array[nhandles++] = select_finished_event;
if (timeout.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
{