Skip to content

Commit

Permalink
Merge pull request #484 from ksooo/8-1-1
Browse files Browse the repository at this point in the history
8.1.1: Fix IPv6 support.
  • Loading branch information
ksooo authored Nov 26, 2020
2 parents 686e9d5 + bd99584 commit 87dfe57
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 39 deletions.
57 changes: 19 additions & 38 deletions lib/kissnet/kissnet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
* - port_t : a 16 bit unsigned number. Represent a network port number
* - endpoint : a structure that represent a location where you need to connect
* to. Contains a hostname (as std::string) and a port number (as port_t)
* - socket<protocol, ip> : a templated class that represent a socket. Protocol
* is either TCP or UDP, and ip is either v4 or v6
* - socket<protocol> : a templated class that represents an ipv4 or ipv6 socket.
* Protocol is either TCP or UDP
*
* Kissnet does error handling in 2 ways:
*
Expand Down Expand Up @@ -400,12 +400,6 @@ namespace kissnet
udp
};

///Represent ipv4 vs ipv6
enum class ip {
v4,
v6
};

///File descriptor set types
static constexpr int fds_read = 0x1;
static constexpr int fds_write = 0x2;
Expand Down Expand Up @@ -669,7 +663,7 @@ namespace kissnet
#endif

///Class that represent a socket
template <protocol sock_proto, ip ipver = ip::v4>
template <protocol sock_proto>
class socket
{
///Represent a number of bytes with a status information. Some of the methods of this class returns this.
Expand All @@ -693,8 +687,9 @@ namespace kissnet
addrinfo getaddrinfo_hints{};
addrinfo* getaddrinfo_results = nullptr;

void initialize_addrinfo(int& type, short& family)
void initialize_addrinfo()
{
int type{};
int iprotocol{};
if constexpr (sock_proto == protocol::tcp || sock_proto == protocol::tcp_ssl)
{
Expand All @@ -708,18 +703,8 @@ namespace kissnet
iprotocol = IPPROTO_UDP;
}

if constexpr (ipver == ip::v4)
{
family = AF_INET;
}

else if constexpr (ipver == ip::v6)
{
family = AF_INET6;
}

(void)memset(&getaddrinfo_hints, 0, sizeof getaddrinfo_hints);
getaddrinfo_hints.ai_family = family;
getaddrinfo_hints.ai_family = AF_UNSPEC;
getaddrinfo_hints.ai_socktype = type;
getaddrinfo_hints.ai_protocol = iprotocol;
getaddrinfo_hints.ai_flags = AI_ADDRCONFIG;
Expand Down Expand Up @@ -824,20 +809,23 @@ namespace kissnet
KISSNET_OS_INIT;

//Do we use streams or datagrams
int type;
short family;
initialize_addrinfo(type, family);
initialize_addrinfo();

sock = syscall_socket(family, type, 0);
if (sock == INVALID_SOCKET)
if (getaddrinfo(bind_loc.address.c_str(), std::to_string(bind_loc.port).c_str(), &getaddrinfo_hints, &getaddrinfo_results) != 0)
{
kissnet_fatal_error("socket() syscall failed!");
kissnet_fatal_error("getaddrinfo failed!");
}

if (getaddrinfo(bind_loc.address.c_str(), std::to_string(bind_loc.port).c_str(), &getaddrinfo_hints, &getaddrinfo_results) != 0)
for (auto* addr = getaddrinfo_results; addr; addr = addr->ai_next)
{
//TODO There can be more than one address to attempt to use in the getaddrinfo_results (It's a list). Try to go further down that list in error condition
kissnet_fatal_error("getaddrinfo failed!");
sock = syscall_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
if (sock != INVALID_SOCKET)
break;
}

if (sock == INVALID_SOCKET)
{
kissnet_fatal_error("unable to craete socket!");
}

//Fill socket_input with 0s
Expand All @@ -850,10 +838,7 @@ namespace kissnet
{
KISSNET_OS_INIT;

short family;
int type;

initialize_addrinfo(type, family);
initialize_addrinfo();

//Fill socket_input with 0s
memset(static_cast<void*>(&socket_input), 0, sizeof socket_input);
Expand Down Expand Up @@ -1270,10 +1255,6 @@ namespace kissnet
using tcp_ssl_socket = socket<protocol::tcp_ssl>;
///Alias for socket<protocol::udp>
using udp_socket = socket<protocol::udp>;
///IPV6 version of a TCP socket
using tcp_socket_v6 = socket<protocol::tcp, ip::v6>;
///IPV6 version of an UDP socket
using udp_socket_v6 = socket<protocol::udp, ip::v6>;
}

//cleanup preprocessor macros
Expand Down
2 changes: 1 addition & 1 deletion pvr.hts/addon.xml.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<addon
id="pvr.hts"
version="8.1.0"
version="8.1.1"
name="Tvheadend HTSP Client"
provider-name="Adam Sutton, Sam Stenvall, Lars Op den Kamp, Kai Sommerfeld">
<requires>@ADDON_DEPENDS@</requires>
Expand Down
3 changes: 3 additions & 0 deletions pvr.hts/changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
8.1.1
- Fix IPv6 support

8.1.0
- Update inputstream API 3.0.1
- Fix wrong flags bit shift
Expand Down

0 comments on commit 87dfe57

Please sign in to comment.