winlin

extract get_local_ip and get_peer_ip to app utility

... ... @@ -35,6 +35,7 @@ using namespace std;
#include <srs_app_config.hpp>
#include <srs_core_autofree.hpp>
#include <srs_kernel_utility.hpp>
#include <srs_app_utility.hpp>
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;
... ...
... ... @@ -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.
*/
... ...
... ... @@ -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;
}
... ...
... ... @@ -30,6 +30,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_core.hpp>
#include <string>
#include <srs_app_st.hpp>
#include <srs_app_thread.hpp>
... ... @@ -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
... ...
... ... @@ -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) {
... ...
... ... @@ -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) {
... ...
... ... @@ -44,6 +44,7 @@ using namespace std;
#include <srs_app_http_hooks.hpp>
#include <srs_app_edge.hpp>
#include <srs_app_kbps.hpp>
#include <srs_app_utility.hpp>
// 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) {
... ...
... ... @@ -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;
}
... ...
... ... @@ -353,4 +353,10 @@ extern void srs_update_platform_info();
extern void srs_retrieve_local_ipv4_ips();
extern std::vector<std::string>& 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
... ...
... ... @@ -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
... ...