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

extract get_local_ip and get_peer_ip to app utility

This commit is contained in:
winlin 2014-05-27 16:45:02 +08:00
parent 384687a36d
commit 7920348e5f
10 changed files with 78 additions and 100 deletions

View file

@ -33,6 +33,7 @@ using namespace std;
#include <srs_kernel_log.hpp>
#include <srs_app_config.hpp>
#include <srs_kernel_utility.hpp>
#include <srs_kernel_error.hpp>
#define SRS_LOCAL_LOOP_IP "127.0.0.1"
@ -494,3 +495,58 @@ vector<string>& srs_get_local_ipv4_ips()
return _srs_system_ipv4_ips;
}
string srs_get_local_ip(int fd)
{
std::string ip;
// discovery client information
sockaddr_in addr;
socklen_t addrlen = sizeof(addr);
if (getsockname(fd, (sockaddr*)&addr, &addrlen) == -1) {
return ip;
}
srs_verbose("get local ip success.");
// ip v4 or v6
char buf[INET6_ADDRSTRLEN];
memset(buf, 0, sizeof(buf));
if ((inet_ntop(addr.sin_family, &addr.sin_addr, buf, sizeof(buf))) == NULL) {
return ip;
}
ip = buf;
srs_verbose("get local ip of client ip=%s, fd=%d", buf, fd);
return ip;
}
string srs_get_peer_ip(int fd)
{
std::string ip;
// discovery client information
sockaddr_in addr;
socklen_t addrlen = sizeof(addr);
if (getpeername(fd, (sockaddr*)&addr, &addrlen) == -1) {
return ip;
}
srs_verbose("get peer name success.");
// ip v4 or v6
char buf[INET6_ADDRSTRLEN];
memset(buf, 0, sizeof(buf));
if ((inet_ntop(addr.sin_family, &addr.sin_addr, buf, sizeof(buf))) == NULL) {
return ip;
}
srs_verbose("get peer ip of client ip=%s, fd=%d", buf, fd);
ip = buf;
srs_verbose("get peer ip success. ip=%s, fd=%d", ip, fd);
return ip;
}