From 7920348e5f8b54965a030a0bdcbb42556906b6f1 Mon Sep 17 00:00:00 2001 From: winlin Date: Tue, 27 May 2014 16:45:02 +0800 Subject: [PATCH] extract get_local_ip and get_peer_ip to app utility --- trunk/src/app/srs_app_bandwidth.cpp | 44 ++------------------- trunk/src/app/srs_app_bandwidth.hpp | 1 - trunk/src/app/srs_app_conn.cpp | 39 +------------------ trunk/src/app/srs_app_conn.hpp | 6 +-- trunk/src/app/srs_app_http_api.cpp | 6 +-- trunk/src/app/srs_app_http_conn.cpp | 6 +-- trunk/src/app/srs_app_rtmp_conn.cpp | 12 +++--- trunk/src/app/srs_app_utility.cpp | 56 +++++++++++++++++++++++++++ trunk/src/app/srs_app_utility.hpp | 6 +++ trunk/src/kernel/srs_kernel_error.hpp | 2 +- 10 files changed, 78 insertions(+), 100 deletions(-) diff --git a/trunk/src/app/srs_app_bandwidth.cpp b/trunk/src/app/srs_app_bandwidth.cpp index 5401bb862..398f1acc3 100644 --- a/trunk/src/app/srs_app_bandwidth.cpp +++ b/trunk/src/app/srs_app_bandwidth.cpp @@ -35,6 +35,7 @@ using namespace std; #include #include #include +#include SrsBandwidth::SrsBandwidth() { @@ -86,13 +87,8 @@ int SrsBandwidth::bandwidth_test(SrsRequest* _req, st_netfd_t stfd, SrsRtmpServe // accept and do bandwidth check. last_check_time = time_now; - char* local_ip = 0; - if ((ret = get_local_ip(stfd, local_ip)) != ERROR_SUCCESS) { - srs_error("get local ip failed. ret = %d", ret); - return ret; - } - - if ((ret = rtmp->response_connect_app(req, local_ip)) != ERROR_SUCCESS) { + std::string local_ip = srs_get_local_ip(st_netfd_fileno(stfd)); + if ((ret = rtmp->response_connect_app(req, local_ip.c_str())) != ERROR_SUCCESS) { srs_error("response connect app failed. ret=%d", ret); return ret; } @@ -100,40 +96,6 @@ int SrsBandwidth::bandwidth_test(SrsRequest* _req, st_netfd_t stfd, SrsRtmpServe return do_bandwidth_check(); } -int SrsBandwidth::get_local_ip(st_netfd_t stfd, char *&local_ip) -{ - int ret = ERROR_SUCCESS; - - int fd = st_netfd_fileno(stfd); - - // discovery client information - sockaddr_in addr; - socklen_t addrlen = sizeof(addr); - if (getsockname(fd, (sockaddr*)&addr, &addrlen) == -1) { - ret = ERROR_SOCKET_GET_LOCAL_IP; - srs_error("discovery local ip information failed. ret=%d", ret); - return ret; - } - 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) { - ret = ERROR_SOCKET_GET_LOCAL_IP; - srs_error("convert local ip information failed. ret=%d", ret); - return ret; - } - - local_ip = new char[strlen(buf) + 1]; - strcpy(local_ip, buf); - - srs_verbose("get local ip of client ip=%s, fd=%d", buf, fd); - - return ret; -} - int SrsBandwidth::do_bandwidth_check() { int ret = ERROR_SUCCESS; diff --git a/trunk/src/app/srs_app_bandwidth.hpp b/trunk/src/app/srs_app_bandwidth.hpp index 9d9760117..9f13c65a9 100644 --- a/trunk/src/app/srs_app_bandwidth.hpp +++ b/trunk/src/app/srs_app_bandwidth.hpp @@ -84,7 +84,6 @@ public: */ virtual int bandwidth_test(SrsRequest* _req, st_netfd_t stfd, SrsRtmpServer* _rtmp); private: - virtual int get_local_ip(st_netfd_t stfd, char *&local_ip); /** * used to process band width check from client. */ diff --git a/trunk/src/app/srs_app_conn.cpp b/trunk/src/app/srs_app_conn.cpp index 13e14e3cf..5f595b14d 100644 --- a/trunk/src/app/srs_app_conn.cpp +++ b/trunk/src/app/srs_app_conn.cpp @@ -28,10 +28,10 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include #include #include +#include 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; } diff --git a/trunk/src/app/srs_app_conn.hpp b/trunk/src/app/srs_app_conn.hpp index 9a6f34c61..96ac6c98a 100644 --- a/trunk/src/app/srs_app_conn.hpp +++ b/trunk/src/app/srs_app_conn.hpp @@ -30,6 +30,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include +#include + #include #include @@ -39,10 +41,10 @@ class SrsConnection : public ISrsThreadHandler private: SrsThread* pthread; protected: - char* ip; SrsServer* server; st_netfd_t stfd; int connection_id; + std::string ip; public: SrsConnection(SrsServer* srs_server, st_netfd_t client_stfd); virtual ~SrsConnection(); @@ -53,8 +55,6 @@ public: protected: virtual int do_cycle() = 0; virtual void stop(); -protected: - virtual int get_peer_ip(); }; #endif \ No newline at end of file diff --git a/trunk/src/app/srs_app_http_api.cpp b/trunk/src/app/srs_app_http_api.cpp index bf477ee67..296f72529 100644 --- a/trunk/src/app/srs_app_http_api.cpp +++ b/trunk/src/app/srs_app_http_api.cpp @@ -686,11 +686,7 @@ int SrsHttpApi::do_cycle() { int ret = ERROR_SUCCESS; - if ((ret = get_peer_ip()) != ERROR_SUCCESS) { - srs_error("get peer ip failed. ret=%d", ret); - return ret; - } - srs_trace("api get peer ip success. ip=%s", ip); + srs_trace("api get peer ip success. ip=%s", ip.c_str()); // initialize parser if ((ret = parser->initialize(HTTP_REQUEST)) != ERROR_SUCCESS) { diff --git a/trunk/src/app/srs_app_http_conn.cpp b/trunk/src/app/srs_app_http_conn.cpp index 3b8bb0a1b..d32bd7318 100644 --- a/trunk/src/app/srs_app_http_conn.cpp +++ b/trunk/src/app/srs_app_http_conn.cpp @@ -514,11 +514,7 @@ int SrsHttpConn::do_cycle() { int ret = ERROR_SUCCESS; - if ((ret = get_peer_ip()) != ERROR_SUCCESS) { - srs_error("get peer ip failed. ret=%d", ret); - return ret; - } - srs_trace("http get peer ip success. ip=%s", ip); + srs_trace("http get peer ip success. ip=%s", ip.c_str()); // initialize parser if ((ret = parser->initialize(HTTP_REQUEST)) != ERROR_SUCCESS) { diff --git a/trunk/src/app/srs_app_rtmp_conn.cpp b/trunk/src/app/srs_app_rtmp_conn.cpp index 944692a70..88f81f0f7 100644 --- a/trunk/src/app/srs_app_rtmp_conn.cpp +++ b/trunk/src/app/srs_app_rtmp_conn.cpp @@ -44,6 +44,7 @@ using namespace std; #include #include #include +#include // when stream is busy, for example, streaming is already // publishing, when a new client to request to publish, @@ -98,11 +99,7 @@ int SrsRtmpConn::do_cycle() { int ret = ERROR_SUCCESS; - if ((ret = get_peer_ip()) != ERROR_SUCCESS) { - srs_error("get peer ip failed. ret=%d", ret); - return ret; - } - srs_trace("serve client, peer ip=%s", ip); + srs_trace("serve client, peer ip=%s", ip.c_str()); rtmp->set_recv_timeout(SRS_RECV_TIMEOUT_US); rtmp->set_send_timeout(SRS_SEND_TIMEOUT_US); @@ -193,7 +190,8 @@ int SrsRtmpConn::service_cycle() return bandwidth->bandwidth_test(req, stfd, rtmp); } - if ((ret = rtmp->response_connect_app(req)) != ERROR_SUCCESS) { + std::string local_ip = srs_get_local_ip(st_netfd_fileno(stfd)); + if ((ret = rtmp->response_connect_app(req, local_ip.c_str())) != ERROR_SUCCESS) { srs_error("response connect app failed. ret=%d", ret); return ret; } @@ -299,7 +297,7 @@ int SrsRtmpConn::stream_service_cycle() bool enabled_cache = _srs_config->get_gop_cache(req->vhost); srs_trace("source found, ip=%s, url=%s, enabled_cache=%d, edge=%d", - ip, req->get_stream_url().c_str(), enabled_cache, vhost_is_edge); + ip.c_str(), req->get_stream_url().c_str(), enabled_cache, vhost_is_edge); source->set_cache(enabled_cache); switch (type) { diff --git a/trunk/src/app/srs_app_utility.cpp b/trunk/src/app/srs_app_utility.cpp index 9cb626cc5..efd7fecef 100644 --- a/trunk/src/app/srs_app_utility.cpp +++ b/trunk/src/app/srs_app_utility.cpp @@ -33,6 +33,7 @@ using namespace std; #include #include #include +#include #define SRS_LOCAL_LOOP_IP "127.0.0.1" @@ -494,3 +495,58 @@ vector& 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; +} diff --git a/trunk/src/app/srs_app_utility.hpp b/trunk/src/app/srs_app_utility.hpp index 0a36bc027..4c4a8a276 100644 --- a/trunk/src/app/srs_app_utility.hpp +++ b/trunk/src/app/srs_app_utility.hpp @@ -353,4 +353,10 @@ extern void srs_update_platform_info(); extern void srs_retrieve_local_ipv4_ips(); extern std::vector& srs_get_local_ipv4_ips(); +// get local or peer ip. +// where local ip is the server ip which client connected. +std::string srs_get_local_ip(int fd); +// where peer ip is the client public ip which connected to server. +std::string srs_get_peer_ip(int fd); + #endif diff --git a/trunk/src/kernel/srs_kernel_error.hpp b/trunk/src/kernel/srs_kernel_error.hpp index e04051e70..3d53e737d 100644 --- a/trunk/src/kernel/srs_kernel_error.hpp +++ b/trunk/src/kernel/srs_kernel_error.hpp @@ -51,7 +51,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #define ERROR_SOCKET_WRITE 209 #define ERROR_SOCKET_WAIT 210 #define ERROR_SOCKET_TIMEOUT 211 -#define ERROR_SOCKET_GET_LOCAL_IP 212 +//#define ERROR_SOCKET_GET_LOCAL_IP 212 #define ERROR_RTMP_PLAIN_REQUIRED 300 #define ERROR_RTMP_CHUNK_START 301