aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-rw-r--r--src/util/network.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/util/network.c b/src/util/network.c
index c82caafd9..66a468e45 100644
--- a/src/util/network.c
+++ b/src/util/network.c
@@ -1697,6 +1697,64 @@ initialize_select_thread ()
1697 1697
1698#endif 1698#endif
1699 1699
1700/**
1701 * Test if the given @a port is available.
1702 *
1703 * @param ipproto transport protocol to test (i.e. IPPROTO_TCP)
1704 * @param port port number to test
1705 * @return #GNUNET_OK if the port is available, #GNUNET_NO if not
1706 */
1707int
1708GNUNET_NETWORK_test_port_free (int ipproto,
1709 uint16_t port)
1710{
1711 struct GNUNET_NETWORK_Handle *socket;
1712 int bind_status;
1713 int socktype;
1714 char open_port_str[6];
1715 struct addrinfo hint;
1716 struct addrinfo *ret;
1717 struct addrinfo *ai;
1718
1719 GNUNET_snprintf (open_port_str,
1720 sizeof (open_port_str),
1721 "%u",
1722 (unsigned int) port);
1723 socktype = (IPPROTO_TCP == ipproto)
1724 ? SOCK_STREAM
1725 : SOCK_DGRAM;
1726 ret = NULL;
1727 memset (&hint, 0, sizeof (hint));
1728 hint.ai_family = AF_UNSPEC; /* IPv4 and IPv6 */
1729 hint.ai_socktype = socktype;
1730 hint.ai_protocol = ipproto;
1731 hint.ai_addrlen = 0;
1732 hint.ai_addr = NULL;
1733 hint.ai_canonname = NULL;
1734 hint.ai_next = NULL;
1735 hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
1736 GNUNET_assert (0 == getaddrinfo (NULL,
1737 open_port_str,
1738 &hint,
1739 &ret));
1740 for (ai = ret; NULL != ai; ai = ai->ai_next)
1741 {
1742 socket = GNUNET_NETWORK_socket_create (ai->ai_family,
1743 ai->ai_socktype,
1744 ai->ai_protocol);
1745 if (NULL == socket)
1746 continue;
1747 bind_status = GNUNET_NETWORK_socket_bind (socket,
1748 ai->ai_addr,
1749 ai->ai_addrlen);
1750 GNUNET_NETWORK_socket_close (socket);
1751 if (GNUNET_OK != bind_status)
1752 break;
1753 }
1754 freeaddrinfo (ret);
1755 return bind_status;
1756}
1757
1700 1758
1701#ifndef MINGW 1759#ifndef MINGW
1702/** 1760/**