1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

IPv6 support (for 3.0 release) (#988)

* IPv6 support, part 1.

* IPv6 support, part 2.

* Some more IPv6 work.

* Made functions for address:port paŕsing IPv6-capable.

* Fixed type (compile warning).

* Fixed formatting.

* Reverted option change.

* Replaced abort() by proper error handling.

* Also retrieving local IPv6 addresses now.
This commit is contained in:
Thomas Dreibholz 2017-10-14 05:29:33 +02:00 committed by winlin
parent db08f1586c
commit feaae341b9
20 changed files with 296 additions and 201 deletions

View file

@ -26,6 +26,7 @@
#include <st.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netdb.h>
using namespace std;
#include <srs_kernel_error.hpp>
@ -104,8 +105,6 @@ srs_thread_t srs_thread_self()
srs_error_t srs_socket_connect(string server, int port, int64_t tm, srs_netfd_t* pstfd)
{
srs_error_t err = srs_success;
st_utime_t timeout = ST_UTIME_NO_TIMEOUT;
if (tm != SRS_CONSTS_NO_TMMS) {
timeout = (st_utime_t)(tm * 1000);
@ -113,45 +112,43 @@ srs_error_t srs_socket_connect(string server, int port, int64_t tm, srs_netfd_t*
*pstfd = NULL;
srs_netfd_t stfd = NULL;
sockaddr_in addr;
int sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock == -1){
return srs_error_new(ERROR_SOCKET_CREATE, "create socket");
char port_string[8];
snprintf(port_string, sizeof(port_string), "%d", port);
addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
addrinfo* result = NULL;
if(getaddrinfo(server.c_str(), port_string, (const addrinfo*)&hints, &result) != 0) {
return srs_error_new(ERROR_SYSTEM_IP_INVALID, "dns resolve server error");
}
srs_fd_close_exec(sock);
int sock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if(sock == -1){
freeaddrinfo(result);
return srs_error_new(ERROR_SOCKET_CREATE, "create socket");
}
srs_assert(!stfd);
stfd = st_netfd_open_socket(sock);
if(stfd == NULL){
srs_close_stfd(stfd);
freeaddrinfo(result);
return srs_error_new(ERROR_ST_OPEN_SOCKET, "open socket");
}
// connect to server.
std::string ip = srs_dns_resolve(server);
if (ip.empty()) {
return srs_error_new(ERROR_SYSTEM_IP_INVALID, "resolve server %s", server.c_str());
}
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(ip.c_str());
if (st_connect((st_netfd_t)stfd, (const struct sockaddr*)&addr, sizeof(sockaddr_in), timeout) == -1){
err = srs_error_new(ERROR_ST_CONNECT, "connect to %s:%d", ip.c_str(), port);
goto failed;
}
srs_info("connect ok. server=%s, ip=%s, port=%d", server.c_str(), ip.c_str(), port);
*pstfd = stfd;
return err;
failed:
if (stfd) {
if (st_connect((st_netfd_t)stfd, result->ai_addr, result->ai_addrlen, timeout) == -1){
srs_close_stfd(stfd);
freeaddrinfo(result);
return srs_error_new(ERROR_ST_CONNECT, "connect to %s:%d", server.c_str(), port);
}
return err;
srs_info("connect ok. server=%s, port=%d", server.c_str(), port);
freeaddrinfo(result);
*pstfd = stfd;
return srs_success;
}
srs_cond_t srs_cond_new()

View file

@ -26,6 +26,7 @@
#include <unistd.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <netdb.h>
#include <map>
#include <sstream>
using namespace std;
@ -61,44 +62,54 @@ bool srs_net_device_is_internet(string ifname)
return _srs_device_ifs[ifname];
}
bool srs_net_device_is_internet(in_addr_t addr)
bool srs_net_device_is_internet(const sockaddr* addr)
{
uint32_t addr_h = ntohl(addr);
// lo, 127.0.0.0-127.0.0.1
if (addr_h >= 0x7f000000 && addr_h <= 0x7f000001) {
return false;
if(addr->sa_family == AF_INET) {
const in_addr inaddr = ((sockaddr_in*)addr)->sin_addr;
const uint32_t addr_h = ntohl(inaddr.s_addr);
// lo, 127.0.0.0-127.0.0.1
if (addr_h >= 0x7f000000 && addr_h <= 0x7f000001) {
return false;
}
// Class A 10.0.0.0-10.255.255.255
if (addr_h >= 0x0a000000 && addr_h <= 0x0affffff) {
return false;
}
// Class B 172.16.0.0-172.31.255.255
if (addr_h >= 0xac100000 && addr_h <= 0xac1fffff) {
return false;
}
// Class C 192.168.0.0-192.168.255.255
if (addr_h >= 0xc0a80000 && addr_h <= 0xc0a8ffff) {
return false;
}
}
// Class A 10.0.0.0-10.255.255.255
if (addr_h >= 0x0a000000 && addr_h <= 0x0affffff) {
return false;
}
// Class B 172.16.0.0-172.31.255.255
if (addr_h >= 0xac100000 && addr_h <= 0xac1fffff) {
return false;
}
// Class C 192.168.0.0-192.168.255.255
if (addr_h >= 0xc0a80000 && addr_h <= 0xc0a8ffff) {
return false;
else if(addr->sa_family == AF_INET6) {
const sockaddr_in6* a6 = (const sockaddr_in6*)addr;
if ((IN6_IS_ADDR_LINKLOCAL(&a6->sin6_addr)) ||
(IN6_IS_ADDR_SITELOCAL(&a6->sin6_addr))) {
return false;
}
}
return true;
}
vector<string> _srs_system_ipv4_ips;
vector<string> _srs_system_ips;
void retrieve_local_ipv4_ips()
void retrieve_local_ips()
{
vector<string>& ips = _srs_system_ipv4_ips;
vector<string>& ips = _srs_system_ips;
ips.clear();
ifaddrs* ifap;
if (getifaddrs(&ifap) == -1) {
srs_warn("retrieve local ips, ini ifaddrs failed.");
srs_warn("retrieve local ips, getifaddrs failed.");
return;
}
@ -111,32 +122,33 @@ void retrieve_local_ipv4_ips()
ifaddrs* p = ifap;
while (p != NULL) {
ifaddrs* cur = p;
sockaddr* addr = cur->ifa_addr;
p = p->ifa_next;
// retrieve ipv4 addr
// retrieve IP address
// ignore the tun0 network device,
// which addr is NULL.
// @see: https://github.com/ossrs/srs/issues/141
if (addr && addr->sa_family == AF_INET) {
in_addr* inaddr = &((sockaddr_in*)addr)->sin_addr;
char buf[16];
memset(buf, 0, sizeof(buf));
if ((inet_ntop(addr->sa_family, inaddr, buf, sizeof(buf))) == NULL) {
srs_warn("convert local ip failed");
if ( (cur->ifa_addr) &&
( (cur->ifa_addr->sa_family == AF_INET) ||
(cur->ifa_addr->sa_family == AF_INET6) ) ) {
char address_string[64];
const int success = getnameinfo(cur->ifa_addr, sizeof(sockaddr_storage),
(char*)&address_string, sizeof(address_string),
NULL, 0,
NI_NUMERICHOST);
if(success != 0) {
srs_warn("convert local ip failed: %s", gai_strerror(success));
break;
}
std::string ip = buf;
std::string ip = address_string;
if (ip != SRS_CONSTS_LOCALHOST) {
ss0 << ", local[" << (int)ips.size() << "] ipv4 " << ip;
ips.push_back(ip);
}
// set the device internet status.
if (!srs_net_device_is_internet(inaddr->s_addr)) {
if (!srs_net_device_is_internet(cur->ifa_addr)) {
ss1 << ", intranet ";
_srs_device_ifs[cur->ifa_name] = false;
} else {
@ -152,13 +164,13 @@ void retrieve_local_ipv4_ips()
freeifaddrs(ifap);
}
vector<string>& srs_get_local_ipv4_ips()
vector<string>& srs_get_local_ips()
{
if (_srs_system_ipv4_ips.empty()) {
retrieve_local_ipv4_ips();
if (_srs_system_ips.empty()) {
retrieve_local_ips();
}
return _srs_system_ipv4_ips;
return _srs_system_ips;
}
std::string _public_internet_address;
@ -169,7 +181,7 @@ string srs_get_public_internet_address()
return _public_internet_address;
}
std::vector<std::string>& ips = srs_get_local_ipv4_ips();
std::vector<std::string>& ips = srs_get_local_ips();
// find the best match public address.
for (int i = 0; i < (int)ips.size(); i++) {

View file

@ -37,14 +37,14 @@ extern bool srs_string_is_http(std::string url);
extern bool srs_string_is_rtmp(std::string url);
// get local ip, fill to @param ips
extern std::vector<std::string>& srs_get_local_ipv4_ips();
extern std::vector<std::string>& srs_get_local_ips();
// get local public ip, empty string if no public internet address found.
extern std::string srs_get_public_internet_address();
// detect whether specified device is internet public address.
extern bool srs_net_device_is_internet(std::string ifname);
extern bool srs_net_device_is_internet(in_addr_t addr);
extern bool srs_net_device_is_internet(const sockaddr* addr);
#endif