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

@ -28,10 +28,10 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_kernel_log.hpp>
#include <srs_kernel_error.hpp>
#include <srs_app_server.hpp>
#include <srs_app_utility.hpp>
SrsConnection::SrsConnection(SrsServer* srs_server, st_netfd_t client_stfd)
{
ip = NULL;
server = srs_server;
stfd = client_stfd;
connection_id = 0;
@ -58,6 +58,7 @@ int SrsConnection::cycle()
_srs_context->generate_id();
connection_id = _srs_context->get_id();
ip = srs_get_peer_ip(st_netfd_fileno(stfd));
ret = do_cycle();
@ -92,41 +93,5 @@ void SrsConnection::stop()
{
srs_close_stfd(stfd);
srs_freep(pthread);
srs_freep(ip);
}
int SrsConnection::get_peer_ip()
{
int ret = ERROR_SUCCESS;
int fd = st_netfd_fileno(stfd);
// discovery client information
sockaddr_in addr;
socklen_t addrlen = sizeof(addr);
if (getpeername(fd, (sockaddr*)&addr, &addrlen) == -1) {
ret = ERROR_SOCKET_GET_PEER_NAME;
srs_error("discovery client information failed. ret=%d", ret);
return ret;
}
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) {
ret = ERROR_SOCKET_GET_PEER_IP;
srs_error("convert client information failed. ret=%d", ret);
return ret;
}
srs_verbose("get peer ip of client ip=%s, fd=%d", buf, fd);
ip = new char[strlen(buf) + 1];
strcpy(ip, buf);
srs_verbose("get peer ip success. ip=%s, fd=%d", ip, fd);
return ret;
}