winlin

extract get_local_ip and get_peer_ip to app utility

@@ -35,6 +35,7 @@ using namespace std; @@ -35,6 +35,7 @@ using namespace std;
35 #include <srs_app_config.hpp> 35 #include <srs_app_config.hpp>
36 #include <srs_core_autofree.hpp> 36 #include <srs_core_autofree.hpp>
37 #include <srs_kernel_utility.hpp> 37 #include <srs_kernel_utility.hpp>
  38 +#include <srs_app_utility.hpp>
38 39
39 SrsBandwidth::SrsBandwidth() 40 SrsBandwidth::SrsBandwidth()
40 { 41 {
@@ -86,13 +87,8 @@ int SrsBandwidth::bandwidth_test(SrsRequest* _req, st_netfd_t stfd, SrsRtmpServe @@ -86,13 +87,8 @@ int SrsBandwidth::bandwidth_test(SrsRequest* _req, st_netfd_t stfd, SrsRtmpServe
86 // accept and do bandwidth check. 87 // accept and do bandwidth check.
87 last_check_time = time_now; 88 last_check_time = time_now;
88 89
89 - char* local_ip = 0;  
90 - if ((ret = get_local_ip(stfd, local_ip)) != ERROR_SUCCESS) {  
91 - srs_error("get local ip failed. ret = %d", ret);  
92 - return ret;  
93 - }  
94 -  
95 - if ((ret = rtmp->response_connect_app(req, local_ip)) != ERROR_SUCCESS) { 90 + std::string local_ip = srs_get_local_ip(st_netfd_fileno(stfd));
  91 + if ((ret = rtmp->response_connect_app(req, local_ip.c_str())) != ERROR_SUCCESS) {
96 srs_error("response connect app failed. ret=%d", ret); 92 srs_error("response connect app failed. ret=%d", ret);
97 return ret; 93 return ret;
98 } 94 }
@@ -100,40 +96,6 @@ int SrsBandwidth::bandwidth_test(SrsRequest* _req, st_netfd_t stfd, SrsRtmpServe @@ -100,40 +96,6 @@ int SrsBandwidth::bandwidth_test(SrsRequest* _req, st_netfd_t stfd, SrsRtmpServe
100 return do_bandwidth_check(); 96 return do_bandwidth_check();
101 } 97 }
102 98
103 -int SrsBandwidth::get_local_ip(st_netfd_t stfd, char *&local_ip)  
104 -{  
105 - int ret = ERROR_SUCCESS;  
106 -  
107 - int fd = st_netfd_fileno(stfd);  
108 -  
109 - // discovery client information  
110 - sockaddr_in addr;  
111 - socklen_t addrlen = sizeof(addr);  
112 - if (getsockname(fd, (sockaddr*)&addr, &addrlen) == -1) {  
113 - ret = ERROR_SOCKET_GET_LOCAL_IP;  
114 - srs_error("discovery local ip information failed. ret=%d", ret);  
115 - return ret;  
116 - }  
117 - srs_verbose("get local ip success.");  
118 -  
119 - // ip v4 or v6  
120 - char buf[INET6_ADDRSTRLEN];  
121 - memset(buf, 0, sizeof(buf));  
122 -  
123 - if ((inet_ntop(addr.sin_family, &addr.sin_addr, buf, sizeof(buf))) == NULL) {  
124 - ret = ERROR_SOCKET_GET_LOCAL_IP;  
125 - srs_error("convert local ip information failed. ret=%d", ret);  
126 - return ret;  
127 - }  
128 -  
129 - local_ip = new char[strlen(buf) + 1];  
130 - strcpy(local_ip, buf);  
131 -  
132 - srs_verbose("get local ip of client ip=%s, fd=%d", buf, fd);  
133 -  
134 - return ret;  
135 -}  
136 -  
137 int SrsBandwidth::do_bandwidth_check() 99 int SrsBandwidth::do_bandwidth_check()
138 { 100 {
139 int ret = ERROR_SUCCESS; 101 int ret = ERROR_SUCCESS;
@@ -84,7 +84,6 @@ public: @@ -84,7 +84,6 @@ public:
84 */ 84 */
85 virtual int bandwidth_test(SrsRequest* _req, st_netfd_t stfd, SrsRtmpServer* _rtmp); 85 virtual int bandwidth_test(SrsRequest* _req, st_netfd_t stfd, SrsRtmpServer* _rtmp);
86 private: 86 private:
87 - virtual int get_local_ip(st_netfd_t stfd, char *&local_ip);  
88 /** 87 /**
89 * used to process band width check from client. 88 * used to process band width check from client.
90 */ 89 */
@@ -28,10 +28,10 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -28,10 +28,10 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include <srs_kernel_log.hpp> 28 #include <srs_kernel_log.hpp>
29 #include <srs_kernel_error.hpp> 29 #include <srs_kernel_error.hpp>
30 #include <srs_app_server.hpp> 30 #include <srs_app_server.hpp>
  31 +#include <srs_app_utility.hpp>
31 32
32 SrsConnection::SrsConnection(SrsServer* srs_server, st_netfd_t client_stfd) 33 SrsConnection::SrsConnection(SrsServer* srs_server, st_netfd_t client_stfd)
33 { 34 {
34 - ip = NULL;  
35 server = srs_server; 35 server = srs_server;
36 stfd = client_stfd; 36 stfd = client_stfd;
37 connection_id = 0; 37 connection_id = 0;
@@ -58,6 +58,7 @@ int SrsConnection::cycle() @@ -58,6 +58,7 @@ int SrsConnection::cycle()
58 58
59 _srs_context->generate_id(); 59 _srs_context->generate_id();
60 connection_id = _srs_context->get_id(); 60 connection_id = _srs_context->get_id();
  61 + ip = srs_get_peer_ip(st_netfd_fileno(stfd));
61 62
62 ret = do_cycle(); 63 ret = do_cycle();
63 64
@@ -92,41 +93,5 @@ void SrsConnection::stop() @@ -92,41 +93,5 @@ void SrsConnection::stop()
92 { 93 {
93 srs_close_stfd(stfd); 94 srs_close_stfd(stfd);
94 srs_freep(pthread); 95 srs_freep(pthread);
95 - srs_freep(ip);  
96 -}  
97 -  
98 -int SrsConnection::get_peer_ip()  
99 -{  
100 - int ret = ERROR_SUCCESS;  
101 -  
102 - int fd = st_netfd_fileno(stfd);  
103 -  
104 - // discovery client information  
105 - sockaddr_in addr;  
106 - socklen_t addrlen = sizeof(addr);  
107 - if (getpeername(fd, (sockaddr*)&addr, &addrlen) == -1) {  
108 - ret = ERROR_SOCKET_GET_PEER_NAME;  
109 - srs_error("discovery client information failed. ret=%d", ret);  
110 - return ret;  
111 - }  
112 - srs_verbose("get peer name success.");  
113 -  
114 - // ip v4 or v6  
115 - char buf[INET6_ADDRSTRLEN];  
116 - memset(buf, 0, sizeof(buf));  
117 -  
118 - if ((inet_ntop(addr.sin_family, &addr.sin_addr, buf, sizeof(buf))) == NULL) {  
119 - ret = ERROR_SOCKET_GET_PEER_IP;  
120 - srs_error("convert client information failed. ret=%d", ret);  
121 - return ret;  
122 - }  
123 - srs_verbose("get peer ip of client ip=%s, fd=%d", buf, fd);  
124 -  
125 - ip = new char[strlen(buf) + 1];  
126 - strcpy(ip, buf);  
127 -  
128 - srs_verbose("get peer ip success. ip=%s, fd=%d", ip, fd);  
129 -  
130 - return ret;  
131 } 96 }
132 97
@@ -30,6 +30,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -30,6 +30,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 30
31 #include <srs_core.hpp> 31 #include <srs_core.hpp>
32 32
  33 +#include <string>
  34 +
33 #include <srs_app_st.hpp> 35 #include <srs_app_st.hpp>
34 #include <srs_app_thread.hpp> 36 #include <srs_app_thread.hpp>
35 37
@@ -39,10 +41,10 @@ class SrsConnection : public ISrsThreadHandler @@ -39,10 +41,10 @@ class SrsConnection : public ISrsThreadHandler
39 private: 41 private:
40 SrsThread* pthread; 42 SrsThread* pthread;
41 protected: 43 protected:
42 - char* ip;  
43 SrsServer* server; 44 SrsServer* server;
44 st_netfd_t stfd; 45 st_netfd_t stfd;
45 int connection_id; 46 int connection_id;
  47 + std::string ip;
46 public: 48 public:
47 SrsConnection(SrsServer* srs_server, st_netfd_t client_stfd); 49 SrsConnection(SrsServer* srs_server, st_netfd_t client_stfd);
48 virtual ~SrsConnection(); 50 virtual ~SrsConnection();
@@ -53,8 +55,6 @@ public: @@ -53,8 +55,6 @@ public:
53 protected: 55 protected:
54 virtual int do_cycle() = 0; 56 virtual int do_cycle() = 0;
55 virtual void stop(); 57 virtual void stop();
56 -protected:  
57 - virtual int get_peer_ip();  
58 }; 58 };
59 59
60 #endif 60 #endif
@@ -686,11 +686,7 @@ int SrsHttpApi::do_cycle() @@ -686,11 +686,7 @@ int SrsHttpApi::do_cycle()
686 { 686 {
687 int ret = ERROR_SUCCESS; 687 int ret = ERROR_SUCCESS;
688 688
689 - if ((ret = get_peer_ip()) != ERROR_SUCCESS) {  
690 - srs_error("get peer ip failed. ret=%d", ret);  
691 - return ret;  
692 - }  
693 - srs_trace("api get peer ip success. ip=%s", ip); 689 + srs_trace("api get peer ip success. ip=%s", ip.c_str());
694 690
695 // initialize parser 691 // initialize parser
696 if ((ret = parser->initialize(HTTP_REQUEST)) != ERROR_SUCCESS) { 692 if ((ret = parser->initialize(HTTP_REQUEST)) != ERROR_SUCCESS) {
@@ -514,11 +514,7 @@ int SrsHttpConn::do_cycle() @@ -514,11 +514,7 @@ int SrsHttpConn::do_cycle()
514 { 514 {
515 int ret = ERROR_SUCCESS; 515 int ret = ERROR_SUCCESS;
516 516
517 - if ((ret = get_peer_ip()) != ERROR_SUCCESS) {  
518 - srs_error("get peer ip failed. ret=%d", ret);  
519 - return ret;  
520 - }  
521 - srs_trace("http get peer ip success. ip=%s", ip); 517 + srs_trace("http get peer ip success. ip=%s", ip.c_str());
522 518
523 // initialize parser 519 // initialize parser
524 if ((ret = parser->initialize(HTTP_REQUEST)) != ERROR_SUCCESS) { 520 if ((ret = parser->initialize(HTTP_REQUEST)) != ERROR_SUCCESS) {
@@ -44,6 +44,7 @@ using namespace std; @@ -44,6 +44,7 @@ using namespace std;
44 #include <srs_app_http_hooks.hpp> 44 #include <srs_app_http_hooks.hpp>
45 #include <srs_app_edge.hpp> 45 #include <srs_app_edge.hpp>
46 #include <srs_app_kbps.hpp> 46 #include <srs_app_kbps.hpp>
  47 +#include <srs_app_utility.hpp>
47 48
48 // when stream is busy, for example, streaming is already 49 // when stream is busy, for example, streaming is already
49 // publishing, when a new client to request to publish, 50 // publishing, when a new client to request to publish,
@@ -98,11 +99,7 @@ int SrsRtmpConn::do_cycle() @@ -98,11 +99,7 @@ int SrsRtmpConn::do_cycle()
98 { 99 {
99 int ret = ERROR_SUCCESS; 100 int ret = ERROR_SUCCESS;
100 101
101 - if ((ret = get_peer_ip()) != ERROR_SUCCESS) {  
102 - srs_error("get peer ip failed. ret=%d", ret);  
103 - return ret;  
104 - }  
105 - srs_trace("serve client, peer ip=%s", ip); 102 + srs_trace("serve client, peer ip=%s", ip.c_str());
106 103
107 rtmp->set_recv_timeout(SRS_RECV_TIMEOUT_US); 104 rtmp->set_recv_timeout(SRS_RECV_TIMEOUT_US);
108 rtmp->set_send_timeout(SRS_SEND_TIMEOUT_US); 105 rtmp->set_send_timeout(SRS_SEND_TIMEOUT_US);
@@ -193,7 +190,8 @@ int SrsRtmpConn::service_cycle() @@ -193,7 +190,8 @@ int SrsRtmpConn::service_cycle()
193 return bandwidth->bandwidth_test(req, stfd, rtmp); 190 return bandwidth->bandwidth_test(req, stfd, rtmp);
194 } 191 }
195 192
196 - if ((ret = rtmp->response_connect_app(req)) != ERROR_SUCCESS) { 193 + std::string local_ip = srs_get_local_ip(st_netfd_fileno(stfd));
  194 + if ((ret = rtmp->response_connect_app(req, local_ip.c_str())) != ERROR_SUCCESS) {
197 srs_error("response connect app failed. ret=%d", ret); 195 srs_error("response connect app failed. ret=%d", ret);
198 return ret; 196 return ret;
199 } 197 }
@@ -299,7 +297,7 @@ int SrsRtmpConn::stream_service_cycle() @@ -299,7 +297,7 @@ int SrsRtmpConn::stream_service_cycle()
299 297
300 bool enabled_cache = _srs_config->get_gop_cache(req->vhost); 298 bool enabled_cache = _srs_config->get_gop_cache(req->vhost);
301 srs_trace("source found, ip=%s, url=%s, enabled_cache=%d, edge=%d", 299 srs_trace("source found, ip=%s, url=%s, enabled_cache=%d, edge=%d",
302 - ip, req->get_stream_url().c_str(), enabled_cache, vhost_is_edge); 300 + ip.c_str(), req->get_stream_url().c_str(), enabled_cache, vhost_is_edge);
303 source->set_cache(enabled_cache); 301 source->set_cache(enabled_cache);
304 302
305 switch (type) { 303 switch (type) {
@@ -33,6 +33,7 @@ using namespace std; @@ -33,6 +33,7 @@ using namespace std;
33 #include <srs_kernel_log.hpp> 33 #include <srs_kernel_log.hpp>
34 #include <srs_app_config.hpp> 34 #include <srs_app_config.hpp>
35 #include <srs_kernel_utility.hpp> 35 #include <srs_kernel_utility.hpp>
  36 +#include <srs_kernel_error.hpp>
36 37
37 #define SRS_LOCAL_LOOP_IP "127.0.0.1" 38 #define SRS_LOCAL_LOOP_IP "127.0.0.1"
38 39
@@ -494,3 +495,58 @@ vector<string>& srs_get_local_ipv4_ips() @@ -494,3 +495,58 @@ vector<string>& srs_get_local_ipv4_ips()
494 495
495 return _srs_system_ipv4_ips; 496 return _srs_system_ipv4_ips;
496 } 497 }
  498 +
  499 +string srs_get_local_ip(int fd)
  500 +{
  501 + std::string ip;
  502 +
  503 + // discovery client information
  504 + sockaddr_in addr;
  505 + socklen_t addrlen = sizeof(addr);
  506 + if (getsockname(fd, (sockaddr*)&addr, &addrlen) == -1) {
  507 + return ip;
  508 + }
  509 + srs_verbose("get local ip success.");
  510 +
  511 + // ip v4 or v6
  512 + char buf[INET6_ADDRSTRLEN];
  513 + memset(buf, 0, sizeof(buf));
  514 +
  515 + if ((inet_ntop(addr.sin_family, &addr.sin_addr, buf, sizeof(buf))) == NULL) {
  516 + return ip;
  517 + }
  518 +
  519 + ip = buf;
  520 +
  521 + srs_verbose("get local ip of client ip=%s, fd=%d", buf, fd);
  522 +
  523 + return ip;
  524 +}
  525 +
  526 +string srs_get_peer_ip(int fd)
  527 +{
  528 + std::string ip;
  529 +
  530 + // discovery client information
  531 + sockaddr_in addr;
  532 + socklen_t addrlen = sizeof(addr);
  533 + if (getpeername(fd, (sockaddr*)&addr, &addrlen) == -1) {
  534 + return ip;
  535 + }
  536 + srs_verbose("get peer name success.");
  537 +
  538 + // ip v4 or v6
  539 + char buf[INET6_ADDRSTRLEN];
  540 + memset(buf, 0, sizeof(buf));
  541 +
  542 + if ((inet_ntop(addr.sin_family, &addr.sin_addr, buf, sizeof(buf))) == NULL) {
  543 + return ip;
  544 + }
  545 + srs_verbose("get peer ip of client ip=%s, fd=%d", buf, fd);
  546 +
  547 + ip = buf;
  548 +
  549 + srs_verbose("get peer ip success. ip=%s, fd=%d", ip, fd);
  550 +
  551 + return ip;
  552 +}
@@ -353,4 +353,10 @@ extern void srs_update_platform_info(); @@ -353,4 +353,10 @@ extern void srs_update_platform_info();
353 extern void srs_retrieve_local_ipv4_ips(); 353 extern void srs_retrieve_local_ipv4_ips();
354 extern std::vector<std::string>& srs_get_local_ipv4_ips(); 354 extern std::vector<std::string>& srs_get_local_ipv4_ips();
355 355
  356 +// get local or peer ip.
  357 +// where local ip is the server ip which client connected.
  358 +std::string srs_get_local_ip(int fd);
  359 +// where peer ip is the client public ip which connected to server.
  360 +std::string srs_get_peer_ip(int fd);
  361 +
356 #endif 362 #endif
@@ -51,7 +51,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -51,7 +51,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
51 #define ERROR_SOCKET_WRITE 209 51 #define ERROR_SOCKET_WRITE 209
52 #define ERROR_SOCKET_WAIT 210 52 #define ERROR_SOCKET_WAIT 210
53 #define ERROR_SOCKET_TIMEOUT 211 53 #define ERROR_SOCKET_TIMEOUT 211
54 -#define ERROR_SOCKET_GET_LOCAL_IP 212 54 +//#define ERROR_SOCKET_GET_LOCAL_IP 212
55 55
56 #define ERROR_RTMP_PLAIN_REQUIRED 300 56 #define ERROR_RTMP_PLAIN_REQUIRED 300
57 #define ERROR_RTMP_CHUNK_START 301 57 #define ERROR_RTMP_CHUNK_START 301