diff options
author | Christian Grothoff <christian@grothoff.org> | 2017-03-24 18:17:50 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2017-03-24 18:18:48 +0100 |
commit | 2ad934742422ecb63fe3fafdc8c73d067a9e2fb7 (patch) | |
tree | 780edc647a3ce9fdfd824e2dc233dd70a2bb79f3 /src/util/network.c | |
parent | 7307d73db807dd1fa295629d2b89c70cf603aeb4 (diff) | |
download | gnunet-2ad934742422ecb63fe3fafdc8c73d067a9e2fb7.tar.gz gnunet-2ad934742422ecb63fe3fafdc8c73d067a9e2fb7.zip |
implement GNUNET_NETWORK_test_port_free() for testcases to conveniently check if a port is available
Diffstat (limited to 'src/util/network.c')
-rw-r--r-- | src/util/network.c | 58 |
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 | */ | ||
1707 | int | ||
1708 | GNUNET_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 | /** |