2021-05-31 05:42:20 +00:00
|
|
|
//
|
2021-07-08 06:30:47 +00:00
|
|
|
// Copyright (c) 2013-2021 The SRS Authors
|
2021-05-31 05:42:20 +00:00
|
|
|
//
|
2022-01-13 10:40:17 +00:00
|
|
|
// SPDX-License-Identifier: MIT or MulanPSL-2.0
|
2021-05-31 05:42:20 +00:00
|
|
|
//
|
2014-03-01 07:59:55 +00:00
|
|
|
|
2015-09-22 01:11:07 +00:00
|
|
|
#include <srs_protocol_utility.hpp>
|
2014-03-01 07:59:55 +00:00
|
|
|
|
2015-05-24 17:02:06 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2022-03-11 04:24:35 +00:00
|
|
|
#include <arpa/inet.h>
|
2014-03-19 06:58:29 +00:00
|
|
|
#include <stdlib.h>
|
2015-08-25 14:29:00 +00:00
|
|
|
#include <sstream>
|
2014-06-29 02:03:29 +00:00
|
|
|
using namespace std;
|
2014-03-19 06:58:29 +00:00
|
|
|
|
|
|
|
#include <srs_kernel_log.hpp>
|
2014-06-08 05:03:03 +00:00
|
|
|
#include <srs_kernel_utility.hpp>
|
2015-09-22 00:48:55 +00:00
|
|
|
#include <srs_kernel_buffer.hpp>
|
2022-06-09 12:11:52 +00:00
|
|
|
#include <srs_protocol_rtmp_stack.hpp>
|
2015-01-31 11:46:55 +00:00
|
|
|
#include <srs_kernel_codec.hpp>
|
2015-05-22 03:20:25 +00:00
|
|
|
#include <srs_kernel_consts.hpp>
|
2022-06-09 12:11:52 +00:00
|
|
|
#include <srs_protocol_rtmp_stack.hpp>
|
2015-09-22 01:07:07 +00:00
|
|
|
#include <srs_protocol_io.hpp>
|
2014-03-19 06:58:29 +00:00
|
|
|
|
2022-06-09 11:59:51 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <ifaddrs.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <map>
|
|
|
|
#include <sstream>
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
#include <srs_protocol_st.hpp>
|
|
|
|
#include <srs_kernel_error.hpp>
|
|
|
|
#include <srs_kernel_consts.hpp>
|
|
|
|
#include <srs_kernel_log.hpp>
|
|
|
|
#include <srs_kernel_utility.hpp>
|
2022-06-09 12:11:52 +00:00
|
|
|
#include <srs_protocol_http_stack.hpp>
|
2022-06-09 11:59:51 +00:00
|
|
|
|
2015-12-07 02:15:08 +00:00
|
|
|
/**
|
|
|
|
* resolve the vhost in query string
|
|
|
|
* @pram vhost, update the vhost if query contains the vhost.
|
|
|
|
* @param app, may contains the vhost in query string format:
|
|
|
|
* app?vhost=request_vhost
|
|
|
|
* app...vhost...request_vhost
|
|
|
|
* @param param, the query, for example, ?vhost=xxx
|
|
|
|
*/
|
|
|
|
void srs_vhost_resolve(string& vhost, string& app, string& param)
|
|
|
|
{
|
|
|
|
// get original param
|
|
|
|
size_t pos = 0;
|
|
|
|
if ((pos = app.find("?")) != std::string::npos) {
|
|
|
|
param = app.substr(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
// filter tcUrl
|
|
|
|
app = srs_string_replace(app, ",", "?");
|
|
|
|
app = srs_string_replace(app, "...", "?");
|
|
|
|
app = srs_string_replace(app, "&&", "?");
|
2019-12-23 13:09:00 +00:00
|
|
|
app = srs_string_replace(app, "&", "?");
|
2015-12-07 02:15:08 +00:00
|
|
|
app = srs_string_replace(app, "=", "?");
|
|
|
|
|
2018-11-11 05:51:10 +00:00
|
|
|
if (srs_string_ends_with(app, "/_definst_")) {
|
|
|
|
app = srs_erase_last_substr(app, "/_definst_");
|
|
|
|
}
|
|
|
|
|
2015-12-07 02:15:08 +00:00
|
|
|
if ((pos = app.find("?")) != std::string::npos) {
|
|
|
|
std::string query = app.substr(pos + 1);
|
|
|
|
app = app.substr(0, pos);
|
|
|
|
|
|
|
|
if ((pos = query.find("vhost?")) != std::string::npos) {
|
|
|
|
query = query.substr(pos + 6);
|
|
|
|
if (!query.empty()) {
|
|
|
|
vhost = query;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-23 13:09:00 +00:00
|
|
|
|
|
|
|
// vhost with params.
|
|
|
|
if ((pos = vhost.find("?")) != std::string::npos) {
|
|
|
|
vhost = vhost.substr(0, pos);
|
|
|
|
}
|
2015-12-07 02:15:08 +00:00
|
|
|
|
|
|
|
/* others */
|
|
|
|
}
|
|
|
|
|
2018-02-13 00:52:57 +00:00
|
|
|
void srs_discovery_tc_url(string tcUrl, string& schema, string& host, string& vhost, string& app, string& stream, int& port, string& param)
|
|
|
|
{
|
2014-06-29 03:45:31 +00:00
|
|
|
size_t pos = std::string::npos;
|
|
|
|
std::string url = tcUrl;
|
|
|
|
|
|
|
|
if ((pos = url.find("://")) != std::string::npos) {
|
|
|
|
schema = url.substr(0, pos);
|
|
|
|
url = url.substr(schema.length() + 3);
|
|
|
|
srs_info("discovery schema=%s", schema.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((pos = url.find("/")) != std::string::npos) {
|
|
|
|
host = url.substr(0, pos);
|
|
|
|
url = url.substr(host.length() + 1);
|
|
|
|
srs_info("discovery host=%s", host.c_str());
|
|
|
|
}
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2014-07-20 05:11:53 +00:00
|
|
|
port = SRS_CONSTS_RTMP_DEFAULT_PORT;
|
2021-03-17 07:50:12 +00:00
|
|
|
if (schema == "https") {
|
|
|
|
port = SRS_DEFAULT_HTTPS_PORT;
|
|
|
|
}
|
|
|
|
|
2014-06-29 03:45:31 +00:00
|
|
|
if ((pos = host.find(":")) != std::string::npos) {
|
2015-09-24 10:33:07 +00:00
|
|
|
srs_parse_hostport(host, host, port);
|
2017-03-25 04:42:37 +00:00
|
|
|
srs_info("discovery host=%s, port=%d", host.c_str(), port);
|
2014-06-29 03:45:31 +00:00
|
|
|
}
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-09-17 05:36:02 +00:00
|
|
|
if (url.empty()) {
|
|
|
|
app = SRS_CONSTS_RTMP_DEFAULT_APP;
|
|
|
|
} else {
|
|
|
|
app = url;
|
|
|
|
}
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2014-06-29 03:45:31 +00:00
|
|
|
vhost = host;
|
2014-07-13 08:10:06 +00:00
|
|
|
srs_vhost_resolve(vhost, app, param);
|
2018-02-13 00:52:57 +00:00
|
|
|
srs_vhost_resolve(vhost, stream, param);
|
2018-08-11 11:23:51 +00:00
|
|
|
|
|
|
|
// Ignore when the param only contains the default vhost.
|
2020-02-25 05:45:05 +00:00
|
|
|
if (param == "?vhost=" SRS_CONSTS_RTMP_DEFAULT_VHOST) {
|
2018-08-11 11:23:51 +00:00
|
|
|
param = "";
|
|
|
|
}
|
2014-06-29 03:45:31 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 03:53:52 +00:00
|
|
|
void srs_parse_query_string(string q, map<string,string>& query)
|
|
|
|
{
|
|
|
|
// query string flags.
|
|
|
|
static vector<string> flags;
|
|
|
|
if (flags.empty()) {
|
|
|
|
flags.push_back("=");
|
|
|
|
flags.push_back(",");
|
|
|
|
flags.push_back("&&");
|
|
|
|
flags.push_back("&");
|
|
|
|
flags.push_back(";");
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<string> kvs = srs_string_split(q, flags);
|
|
|
|
for (int i = 0; i < (int)kvs.size(); i+=2) {
|
|
|
|
string k = kvs.at(i);
|
|
|
|
string v = (i < (int)kvs.size() - 1)? kvs.at(i+1):"";
|
|
|
|
|
|
|
|
query[k] = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-19 06:58:29 +00:00
|
|
|
void srs_random_generate(char* bytes, int size)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < size; i++) {
|
2014-08-21 01:48:19 +00:00
|
|
|
// the common value in [0x0f, 0xf0]
|
2021-07-04 08:04:51 +00:00
|
|
|
bytes[i] = 0x0f + (srs_random() % (256 - 0x0f - 0x0f));
|
2014-03-19 06:58:29 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-29 02:03:29 +00:00
|
|
|
|
2020-07-09 08:51:20 +00:00
|
|
|
std::string srs_random_str(int len)
|
|
|
|
{
|
|
|
|
static string random_table = "01234567890123456789012345678901234567890123456789abcdefghijklmnopqrstuvwxyz";
|
|
|
|
|
|
|
|
string ret;
|
|
|
|
ret.reserve(len);
|
|
|
|
for (int i = 0; i < len; ++i) {
|
2021-07-04 08:04:51 +00:00
|
|
|
ret.append(1, random_table[srs_random() % random_table.size()]);
|
2020-07-09 08:51:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-07-04 08:04:51 +00:00
|
|
|
long srs_random()
|
|
|
|
{
|
|
|
|
static bool _random_initialized = false;
|
|
|
|
if (!_random_initialized) {
|
|
|
|
_random_initialized = true;
|
|
|
|
::srandom((unsigned long)(srs_update_system_time() | (::getpid()<<13)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return random();
|
|
|
|
}
|
|
|
|
|
2018-08-02 01:17:49 +00:00
|
|
|
string srs_generate_tc_url(string host, string vhost, string app, int port)
|
2014-06-29 02:03:29 +00:00
|
|
|
{
|
|
|
|
string tcUrl = "rtmp://";
|
|
|
|
|
2014-07-20 05:11:53 +00:00
|
|
|
if (vhost == SRS_CONSTS_RTMP_DEFAULT_VHOST) {
|
2018-08-02 01:17:49 +00:00
|
|
|
tcUrl += host;
|
2014-06-29 02:03:29 +00:00
|
|
|
} else {
|
|
|
|
tcUrl += vhost;
|
|
|
|
}
|
|
|
|
|
2014-07-20 05:11:53 +00:00
|
|
|
if (port != SRS_CONSTS_RTMP_DEFAULT_PORT) {
|
2018-08-02 01:17:49 +00:00
|
|
|
tcUrl += ":" + srs_int2str(port);
|
2014-06-29 02:03:29 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 01:17:49 +00:00
|
|
|
tcUrl += "/" + app;
|
2014-06-29 02:03:29 +00:00
|
|
|
|
|
|
|
return tcUrl;
|
|
|
|
}
|
2014-07-05 03:10:42 +00:00
|
|
|
|
2020-12-26 14:11:23 +00:00
|
|
|
string srs_generate_stream_with_query(string host, string vhost, string stream, string param, bool with_vhost)
|
2016-01-08 05:58:19 +00:00
|
|
|
{
|
2018-08-02 01:17:49 +00:00
|
|
|
string url = stream;
|
|
|
|
string query = param;
|
2020-12-26 14:11:23 +00:00
|
|
|
|
2020-12-27 04:55:27 +00:00
|
|
|
// If no vhost in param, try to append one.
|
|
|
|
string guessVhost;
|
|
|
|
if (query.find("vhost=") == string::npos) {
|
|
|
|
if (vhost != SRS_CONSTS_RTMP_DEFAULT_VHOST) {
|
|
|
|
guessVhost = vhost;
|
|
|
|
} else if (!srs_is_ipv4(host)) {
|
|
|
|
guessVhost = host;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Well, if vhost exists, always append in query string.
|
|
|
|
if (!guessVhost.empty() && query.find("vhost=") == string::npos) {
|
|
|
|
query += "&vhost=" + guessVhost;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not pass in query, remove it.
|
|
|
|
if (!with_vhost) {
|
|
|
|
size_t pos = query.find("&vhost=");
|
|
|
|
if (pos == string::npos) {
|
|
|
|
pos = query.find("vhost=");
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t end = query.find("&", pos + 1);
|
|
|
|
if (end == string::npos) {
|
|
|
|
end = query.length();
|
2020-12-26 14:11:23 +00:00
|
|
|
}
|
|
|
|
|
2020-12-27 04:55:27 +00:00
|
|
|
if (pos != string::npos && end != string::npos && end > pos) {
|
|
|
|
query = query.substr(0, pos) + query.substr(end);
|
2018-08-02 01:17:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the start & when param is empty.
|
2019-12-18 13:18:11 +00:00
|
|
|
query = srs_string_trim_start(query, "&");
|
2019-12-11 09:56:31 +00:00
|
|
|
|
2018-08-02 01:17:49 +00:00
|
|
|
// Prefix query with ?.
|
2019-12-11 09:56:31 +00:00
|
|
|
if (!query.empty() && !srs_string_starts_with(query, "?")) {
|
2018-08-02 01:17:49 +00:00
|
|
|
url += "?";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append query to url.
|
|
|
|
if (!query.empty()) {
|
|
|
|
url += query;
|
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
2016-01-08 05:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2018-01-01 01:38:22 +00:00
|
|
|
srs_error_t srs_do_rtmp_create_msg(char type, uint32_t timestamp, char* data, int size, int stream_id, T** ppmsg)
|
2015-01-31 11:46:55 +00:00
|
|
|
{
|
2018-01-01 01:38:22 +00:00
|
|
|
srs_error_t err = srs_success;
|
2015-01-31 11:46:55 +00:00
|
|
|
|
|
|
|
*ppmsg = NULL;
|
2016-01-08 05:58:19 +00:00
|
|
|
T* msg = NULL;
|
2015-01-31 11:46:55 +00:00
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
if (type == SrsFrameTypeAudio) {
|
2015-01-31 11:46:55 +00:00
|
|
|
SrsMessageHeader header;
|
|
|
|
header.initialize_audio(size, timestamp, stream_id);
|
|
|
|
|
2016-01-08 05:58:19 +00:00
|
|
|
msg = new T();
|
2018-01-01 01:38:22 +00:00
|
|
|
if ((err = msg->create(&header, data, size)) != srs_success) {
|
2015-01-31 11:46:55 +00:00
|
|
|
srs_freep(msg);
|
2018-01-01 01:38:22 +00:00
|
|
|
return srs_error_wrap(err, "create message");
|
2015-01-31 11:46:55 +00:00
|
|
|
}
|
2017-02-12 12:38:39 +00:00
|
|
|
} else if (type == SrsFrameTypeVideo) {
|
2015-01-31 11:46:55 +00:00
|
|
|
SrsMessageHeader header;
|
|
|
|
header.initialize_video(size, timestamp, stream_id);
|
|
|
|
|
2016-01-08 05:58:19 +00:00
|
|
|
msg = new T();
|
2018-01-01 01:38:22 +00:00
|
|
|
if ((err = msg->create(&header, data, size)) != srs_success) {
|
2015-01-31 11:46:55 +00:00
|
|
|
srs_freep(msg);
|
2018-01-01 01:38:22 +00:00
|
|
|
return srs_error_wrap(err, "create message");
|
2015-01-31 11:46:55 +00:00
|
|
|
}
|
2017-02-12 12:38:39 +00:00
|
|
|
} else if (type == SrsFrameTypeScript) {
|
2015-01-31 11:46:55 +00:00
|
|
|
SrsMessageHeader header;
|
|
|
|
header.initialize_amf0_script(size, stream_id);
|
|
|
|
|
2016-01-08 05:58:19 +00:00
|
|
|
msg = new T();
|
2018-01-01 01:38:22 +00:00
|
|
|
if ((err = msg->create(&header, data, size)) != srs_success) {
|
2015-01-31 11:46:55 +00:00
|
|
|
srs_freep(msg);
|
2018-01-01 01:38:22 +00:00
|
|
|
return srs_error_wrap(err, "create message");
|
2015-01-31 11:46:55 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-01-01 01:38:22 +00:00
|
|
|
return srs_error_new(ERROR_STREAM_CASTER_FLV_TAG, "unknown tag=%#x", (uint8_t)type);
|
2015-01-31 11:46:55 +00:00
|
|
|
}
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-01-31 11:46:55 +00:00
|
|
|
*ppmsg = msg;
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2018-01-01 01:38:22 +00:00
|
|
|
return err;
|
2015-01-31 11:46:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-01 01:38:22 +00:00
|
|
|
srs_error_t srs_rtmp_create_msg(char type, uint32_t timestamp, char* data, int size, int stream_id, SrsSharedPtrMessage** ppmsg)
|
2015-01-31 11:46:55 +00:00
|
|
|
{
|
2018-01-01 01:38:22 +00:00
|
|
|
srs_error_t err = srs_success;
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-01-31 11:46:55 +00:00
|
|
|
// only when failed, we must free the data.
|
2018-01-01 01:38:22 +00:00
|
|
|
if ((err = srs_do_rtmp_create_msg(type, timestamp, data, size, stream_id, ppmsg)) != srs_success) {
|
2015-11-02 03:05:39 +00:00
|
|
|
srs_freepa(data);
|
2018-01-01 01:38:22 +00:00
|
|
|
return srs_error_wrap(err, "create message");
|
2015-01-31 11:46:55 +00:00
|
|
|
}
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2018-01-01 01:38:22 +00:00
|
|
|
return err;
|
2015-01-31 11:46:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-01 01:38:22 +00:00
|
|
|
srs_error_t srs_rtmp_create_msg(char type, uint32_t timestamp, char* data, int size, int stream_id, SrsCommonMessage** ppmsg)
|
2016-01-08 05:58:19 +00:00
|
|
|
{
|
2018-01-01 01:38:22 +00:00
|
|
|
srs_error_t err = srs_success;
|
2016-01-08 05:58:19 +00:00
|
|
|
|
|
|
|
// only when failed, we must free the data.
|
2018-01-01 01:38:22 +00:00
|
|
|
if ((err = srs_do_rtmp_create_msg(type, timestamp, data, size, stream_id, ppmsg)) != srs_success) {
|
2016-01-08 05:58:19 +00:00
|
|
|
srs_freepa(data);
|
2018-01-01 01:38:22 +00:00
|
|
|
return srs_error_wrap(err, "create message");
|
2016-01-08 05:58:19 +00:00
|
|
|
}
|
|
|
|
|
2018-01-01 01:38:22 +00:00
|
|
|
return err;
|
2016-01-08 05:58:19 +00:00
|
|
|
}
|
|
|
|
|
2015-10-13 08:06:37 +00:00
|
|
|
string srs_generate_stream_url(string vhost, string app, string stream)
|
2015-05-22 03:20:25 +00:00
|
|
|
{
|
|
|
|
std::string url = "";
|
|
|
|
|
|
|
|
if (SRS_CONSTS_RTMP_DEFAULT_VHOST != vhost){
|
2015-09-17 05:36:02 +00:00
|
|
|
url += vhost;
|
2015-05-22 03:20:25 +00:00
|
|
|
}
|
|
|
|
url += "/";
|
|
|
|
url += app;
|
|
|
|
url += "/";
|
|
|
|
url += stream;
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-05-22 03:20:25 +00:00
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2015-10-13 08:06:37 +00:00
|
|
|
void srs_parse_rtmp_url(string url, string& tcUrl, string& stream)
|
|
|
|
{
|
|
|
|
size_t pos;
|
|
|
|
|
|
|
|
if ((pos = url.rfind("/")) != string::npos) {
|
|
|
|
stream = url.substr(pos + 1);
|
|
|
|
tcUrl = url.substr(0, pos);
|
|
|
|
} else {
|
|
|
|
tcUrl = url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 01:17:49 +00:00
|
|
|
string srs_generate_rtmp_url(string server, int port, string host, string vhost, string app, string stream, string param)
|
2015-08-25 14:29:00 +00:00
|
|
|
{
|
2018-08-02 01:17:49 +00:00
|
|
|
string tcUrl = "rtmp://" + server + ":" + srs_int2str(port) + "/" + app;
|
|
|
|
string streamWithQuery = srs_generate_stream_with_query(host, vhost, stream, param);
|
|
|
|
string url = tcUrl + "/" + streamWithQuery;
|
|
|
|
return url;
|
2015-08-25 14:29:00 +00:00
|
|
|
}
|
|
|
|
|
2018-12-23 12:08:04 +00:00
|
|
|
srs_error_t srs_write_large_iovs(ISrsProtocolReadWriter* skt, iovec* iovs, int size, ssize_t* pnwrite)
|
2015-05-24 17:02:06 +00:00
|
|
|
{
|
2017-12-31 12:52:04 +00:00
|
|
|
srs_error_t err = srs_success;
|
2015-05-24 17:02:06 +00:00
|
|
|
|
|
|
|
// the limits of writev iovs.
|
|
|
|
#ifndef _WIN32
|
|
|
|
// for linux, generally it's 1024.
|
2015-08-22 05:57:34 +00:00
|
|
|
static int limits = (int)sysconf(_SC_IOV_MAX);
|
2015-05-24 17:02:06 +00:00
|
|
|
#else
|
|
|
|
static int limits = 1024;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// send in a time.
|
2020-03-21 12:20:40 +00:00
|
|
|
if (size <= limits) {
|
2017-12-31 12:52:04 +00:00
|
|
|
if ((err = skt->writev(iovs, size, pnwrite)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "writev");
|
2015-05-24 17:02:06 +00:00
|
|
|
}
|
2017-12-31 12:52:04 +00:00
|
|
|
return err;
|
2015-05-24 17:02:06 +00:00
|
|
|
}
|
2020-03-19 06:36:56 +00:00
|
|
|
|
2015-05-24 17:02:06 +00:00
|
|
|
// send in multiple times.
|
|
|
|
int cur_iov = 0;
|
2020-03-19 06:36:56 +00:00
|
|
|
ssize_t nwrite = 0;
|
2015-05-24 17:02:06 +00:00
|
|
|
while (cur_iov < size) {
|
|
|
|
int cur_count = srs_min(limits, size - cur_iov);
|
2020-03-19 06:36:56 +00:00
|
|
|
if ((err = skt->writev(iovs + cur_iov, cur_count, &nwrite)) != srs_success) {
|
2017-12-31 12:52:04 +00:00
|
|
|
return srs_error_wrap(err, "writev");
|
2015-05-24 17:02:06 +00:00
|
|
|
}
|
|
|
|
cur_iov += cur_count;
|
2020-03-19 06:36:56 +00:00
|
|
|
if (pnwrite) {
|
|
|
|
*pnwrite += nwrite;
|
|
|
|
}
|
2015-05-24 17:02:06 +00:00
|
|
|
}
|
|
|
|
|
2017-12-31 12:52:04 +00:00
|
|
|
return err;
|
2015-05-24 17:02:06 +00:00
|
|
|
}
|
|
|
|
|
2016-01-08 05:58:19 +00:00
|
|
|
bool srs_is_ipv4(string domain)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < (int)domain.length(); i++) {
|
|
|
|
char ch = domain.at(i);
|
|
|
|
if (ch == '.') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ch >= '0' && ch <= '9') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-11 04:24:35 +00:00
|
|
|
uint32_t srs_ipv4_to_num(string ip) {
|
|
|
|
uint32_t addr = 0;
|
|
|
|
if (inet_pton(AF_INET, ip.c_str(), &addr) <= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ntohl(addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool srs_ipv4_within_mask(string ip, string network, string mask) {
|
|
|
|
uint32_t ip_addr = srs_ipv4_to_num(ip);
|
|
|
|
uint32_t mask_addr = srs_ipv4_to_num(mask);
|
|
|
|
uint32_t network_addr = srs_ipv4_to_num(network);
|
|
|
|
|
|
|
|
return (ip_addr & mask_addr) == (network_addr & mask_addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct CIDR_VALUE {
|
|
|
|
size_t length;
|
|
|
|
std::string mask;
|
|
|
|
} CIDR_VALUES[32] = {
|
|
|
|
{ 1, "128.0.0.0" },
|
|
|
|
{ 2, "192.0.0.0" },
|
|
|
|
{ 3, "224.0.0.0" },
|
|
|
|
{ 4, "240.0.0.0" },
|
|
|
|
{ 5, "248.0.0.0" },
|
|
|
|
{ 6, "252.0.0.0" },
|
|
|
|
{ 7, "254.0.0.0" },
|
|
|
|
{ 8, "255.0.0.0" },
|
|
|
|
{ 9, "255.128.0.0" },
|
|
|
|
{ 10, "255.192.0.0" },
|
|
|
|
{ 11, "255.224.0.0" },
|
|
|
|
{ 12, "255.240.0.0" },
|
|
|
|
{ 13, "255.248.0.0" },
|
|
|
|
{ 14, "255.252.0.0" },
|
|
|
|
{ 15, "255.254.0.0" },
|
|
|
|
{ 16, "255.255.0.0" },
|
|
|
|
{ 17, "255.255.128.0" },
|
|
|
|
{ 18, "255.255.192.0" },
|
|
|
|
{ 19, "255.255.224.0" },
|
|
|
|
{ 20, "255.255.240.0" },
|
|
|
|
{ 21, "255.255.248.0" },
|
|
|
|
{ 22, "255.255.252.0" },
|
|
|
|
{ 23, "255.255.254.0" },
|
|
|
|
{ 24, "255.255.255.0" },
|
|
|
|
{ 25, "255.255.255.128" },
|
|
|
|
{ 26, "255.255.255.192" },
|
|
|
|
{ 27, "255.255.255.224" },
|
|
|
|
{ 28, "255.255.255.240" },
|
|
|
|
{ 29, "255.255.255.248" },
|
|
|
|
{ 30, "255.255.255.252" },
|
|
|
|
{ 31, "255.255.255.254" },
|
|
|
|
{ 32, "255.255.255.255" },
|
|
|
|
};
|
|
|
|
|
|
|
|
string srs_get_cidr_mask(string network_address) {
|
|
|
|
string delimiter = "/";
|
|
|
|
|
|
|
|
size_t delimiter_position = network_address.find(delimiter);
|
|
|
|
if (delimiter_position == string::npos) {
|
|
|
|
// Even if it does not have "/N", it can be a valid IP, by default "/32".
|
|
|
|
if (srs_is_ipv4(network_address)) {
|
|
|
|
return CIDR_VALUES[32-1].mask;
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change here to include IPv6 support.
|
|
|
|
string is_ipv4_address = network_address.substr(0, delimiter_position);
|
|
|
|
if (!srs_is_ipv4(is_ipv4_address)) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t cidr_length_position = delimiter_position + delimiter.length();
|
|
|
|
if (cidr_length_position >= network_address.length()) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
string cidr_length = network_address.substr(cidr_length_position, network_address.length());
|
|
|
|
if (cidr_length.length() <= 0) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t cidr_length_num = 31;
|
|
|
|
try {
|
|
|
|
cidr_length_num = atoi(cidr_length.c_str());
|
|
|
|
if (cidr_length_num <= 0) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
} catch (...) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return CIDR_VALUES[cidr_length_num-1].mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
string srs_get_cidr_ipv4(string network_address) {
|
|
|
|
string delimiter = "/";
|
|
|
|
|
|
|
|
size_t delimiter_position = network_address.find(delimiter);
|
|
|
|
if (delimiter_position == string::npos) {
|
|
|
|
// Even if it does not have "/N", it can be a valid IP, by default "/32".
|
|
|
|
if (srs_is_ipv4(network_address)) {
|
|
|
|
return network_address;
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change here to include IPv6 support.
|
|
|
|
string ipv4_address = network_address.substr(0, delimiter_position);
|
|
|
|
if (!srs_is_ipv4(ipv4_address)) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t cidr_length_position = delimiter_position + delimiter.length();
|
|
|
|
if (cidr_length_position >= network_address.length()) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
string cidr_length = network_address.substr(cidr_length_position, network_address.length());
|
|
|
|
if (cidr_length.length() <= 0) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
size_t cidr_length_num = atoi(cidr_length.c_str());
|
|
|
|
if (cidr_length_num <= 0) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
} catch (...) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return ipv4_address;
|
|
|
|
}
|
2022-06-09 11:59:51 +00:00
|
|
|
|
|
|
|
bool srs_string_is_http(string url)
|
|
|
|
{
|
|
|
|
return srs_string_starts_with(url, "http://", "https://");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool srs_string_is_rtmp(string url)
|
|
|
|
{
|
|
|
|
return srs_string_starts_with(url, "rtmp://");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool srs_is_digit_number(string str)
|
|
|
|
{
|
|
|
|
if (str.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* p = str.c_str();
|
|
|
|
const char* p_end = str.data() + str.length();
|
|
|
|
for (; p < p_end; p++) {
|
|
|
|
if (*p != '0') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p == p_end) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t v = ::atoll(p);
|
|
|
|
int64_t powv = (int64_t)pow(10, p_end - p - 1);
|
|
|
|
return v / powv >= 1 && v / powv <= 9;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we detect all network device as internet or intranet device, by its ip address.
|
|
|
|
// key is device name, for instance, eth0
|
|
|
|
// value is whether internet, for instance, true.
|
|
|
|
static std::map<std::string, bool> _srs_device_ifs;
|
|
|
|
|
|
|
|
bool srs_net_device_is_internet(string ifname)
|
|
|
|
{
|
|
|
|
srs_info("check ifname=%s", ifname.c_str());
|
|
|
|
|
|
|
|
if (_srs_device_ifs.find(ifname) == _srs_device_ifs.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return _srs_device_ifs[ifname];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool srs_net_device_is_internet(const sockaddr* addr)
|
|
|
|
{
|
|
|
|
if(addr->sa_family == AF_INET) {
|
|
|
|
const in_addr inaddr = ((sockaddr_in*)addr)->sin_addr;
|
|
|
|
const uint32_t addr_h = ntohl(inaddr.s_addr);
|
|
|
|
|
|
|
|
// lo, 127.0.0.0-127.0.0.1
|
|
|
|
if (addr_h >= 0x7f000000 && addr_h <= 0x7f000001) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Class A 10.0.0.0-10.255.255.255
|
|
|
|
if (addr_h >= 0x0a000000 && addr_h <= 0x0affffff) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Class B 172.16.0.0-172.31.255.255
|
|
|
|
if (addr_h >= 0xac100000 && addr_h <= 0xac1fffff) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Class C 192.168.0.0-192.168.255.255
|
|
|
|
if (addr_h >= 0xc0a80000 && addr_h <= 0xc0a8ffff) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if(addr->sa_family == AF_INET6) {
|
|
|
|
const sockaddr_in6* a6 = (const sockaddr_in6*)addr;
|
|
|
|
|
|
|
|
// IPv6 loopback is ::1
|
|
|
|
if (IN6_IS_ADDR_LOOPBACK(&a6->sin6_addr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// IPv6 unspecified is ::
|
|
|
|
if (IN6_IS_ADDR_UNSPECIFIED(&a6->sin6_addr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// From IPv4, you might know APIPA (Automatic Private IP Addressing) or AutoNet.
|
|
|
|
// Whenever automatic IP configuration through DHCP fails.
|
|
|
|
// The prefix of a site-local address is FE80::/10.
|
|
|
|
if (IN6_IS_ADDR_LINKLOCAL(&a6->sin6_addr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Site-local addresses are equivalent to private IP addresses in IPv4.
|
|
|
|
// The prefix of a site-local address is FEC0::/10.
|
|
|
|
// https://4sysops.com/archives/ipv6-tutorial-part-6-site-local-addresses-and-link-local-addresses/
|
|
|
|
if (IN6_IS_ADDR_SITELOCAL(&a6->sin6_addr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Others.
|
|
|
|
if (IN6_IS_ADDR_MULTICAST(&a6->sin6_addr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (IN6_IS_ADDR_MC_NODELOCAL(&a6->sin6_addr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (IN6_IS_ADDR_MC_LINKLOCAL(&a6->sin6_addr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (IN6_IS_ADDR_MC_SITELOCAL(&a6->sin6_addr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (IN6_IS_ADDR_MC_ORGLOCAL(&a6->sin6_addr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (IN6_IS_ADDR_MC_GLOBAL(&a6->sin6_addr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<SrsIPAddress*> _srs_system_ips;
|
|
|
|
|
|
|
|
void discover_network_iface(ifaddrs* cur, vector<SrsIPAddress*>& ips, stringstream& ss0, stringstream& ss1, bool ipv6, bool loopback)
|
|
|
|
{
|
|
|
|
char saddr[64];
|
|
|
|
char* h = (char*)saddr;
|
|
|
|
socklen_t nbh = (socklen_t)sizeof(saddr);
|
|
|
|
const int r0 = getnameinfo(cur->ifa_addr, sizeof(sockaddr_storage), h, nbh, NULL, 0, NI_NUMERICHOST);
|
|
|
|
if(r0) {
|
|
|
|
srs_warn("convert local ip failed: %s", gai_strerror(r0));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ip(saddr, strlen(saddr));
|
|
|
|
ss0 << ", iface[" << (int)ips.size() << "] " << cur->ifa_name << " " << (ipv6? "ipv6":"ipv4")
|
|
|
|
<< " 0x" << std::hex << cur->ifa_flags << std::dec << " " << ip;
|
|
|
|
|
|
|
|
SrsIPAddress* ip_address = new SrsIPAddress();
|
|
|
|
ip_address->ip = ip;
|
|
|
|
ip_address->is_ipv4 = !ipv6;
|
|
|
|
ip_address->is_loopback = loopback;
|
|
|
|
ip_address->ifname = cur->ifa_name;
|
|
|
|
ip_address->is_internet = srs_net_device_is_internet(cur->ifa_addr);
|
|
|
|
ips.push_back(ip_address);
|
|
|
|
|
|
|
|
// set the device internet status.
|
|
|
|
if (!ip_address->is_internet) {
|
|
|
|
ss1 << ", intranet ";
|
|
|
|
_srs_device_ifs[cur->ifa_name] = false;
|
|
|
|
} else {
|
|
|
|
ss1 << ", internet ";
|
|
|
|
_srs_device_ifs[cur->ifa_name] = true;
|
|
|
|
}
|
|
|
|
ss1 << cur->ifa_name << " " << ip;
|
|
|
|
}
|
|
|
|
|
|
|
|
void retrieve_local_ips()
|
|
|
|
{
|
|
|
|
vector<SrsIPAddress*>& ips = _srs_system_ips;
|
|
|
|
|
|
|
|
// Release previous IPs.
|
|
|
|
for (int i = 0; i < (int)ips.size(); i++) {
|
|
|
|
SrsIPAddress* ip = ips[i];
|
|
|
|
srs_freep(ip);
|
|
|
|
}
|
|
|
|
ips.clear();
|
|
|
|
|
|
|
|
// Get the addresses.
|
|
|
|
ifaddrs* ifap;
|
|
|
|
if (getifaddrs(&ifap) == -1) {
|
|
|
|
srs_warn("retrieve local ips, getifaddrs failed.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
stringstream ss0;
|
|
|
|
ss0 << "ips";
|
|
|
|
|
|
|
|
stringstream ss1;
|
|
|
|
ss1 << "devices";
|
|
|
|
|
|
|
|
// Discover IPv4 first.
|
|
|
|
for (ifaddrs* p = ifap; p ; p = p->ifa_next) {
|
|
|
|
ifaddrs* cur = p;
|
|
|
|
|
|
|
|
// Ignore if no address for this interface.
|
|
|
|
// @see https://github.com/ossrs/srs/issues/1087#issuecomment-408847115
|
|
|
|
if (!cur->ifa_addr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieve IP address, ignore the tun0 network device, whose addr is NULL.
|
|
|
|
// @see: https://github.com/ossrs/srs/issues/141
|
|
|
|
bool ipv4 = (cur->ifa_addr->sa_family == AF_INET);
|
|
|
|
bool ready = (cur->ifa_flags & IFF_UP) && (cur->ifa_flags & IFF_RUNNING);
|
|
|
|
// Ignore IFF_PROMISC(Interface is in promiscuous mode), which may be set by Wireshark.
|
|
|
|
bool ignored = (!cur->ifa_addr) || (cur->ifa_flags & IFF_LOOPBACK) || (cur->ifa_flags & IFF_POINTOPOINT);
|
|
|
|
bool loopback = (cur->ifa_flags & IFF_LOOPBACK);
|
|
|
|
if (ipv4 && ready && !ignored) {
|
|
|
|
discover_network_iface(cur, ips, ss0, ss1, false, loopback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then, discover IPv6 addresses.
|
|
|
|
for (ifaddrs* p = ifap; p ; p = p->ifa_next) {
|
|
|
|
ifaddrs* cur = p;
|
|
|
|
|
|
|
|
// Ignore if no address for this interface.
|
|
|
|
// @see https://github.com/ossrs/srs/issues/1087#issuecomment-408847115
|
|
|
|
if (!cur->ifa_addr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieve IP address, ignore the tun0 network device, whose addr is NULL.
|
|
|
|
// @see: https://github.com/ossrs/srs/issues/141
|
|
|
|
bool ipv6 = (cur->ifa_addr->sa_family == AF_INET6);
|
|
|
|
bool ready = (cur->ifa_flags & IFF_UP) && (cur->ifa_flags & IFF_RUNNING);
|
|
|
|
bool ignored = (!cur->ifa_addr) || (cur->ifa_flags & IFF_POINTOPOINT) || (cur->ifa_flags & IFF_PROMISC) || (cur->ifa_flags & IFF_LOOPBACK);
|
|
|
|
bool loopback = (cur->ifa_flags & IFF_LOOPBACK);
|
|
|
|
if (ipv6 && ready && !ignored) {
|
|
|
|
discover_network_iface(cur, ips, ss0, ss1, true, loopback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If empty, disover IPv4 loopback.
|
|
|
|
if (ips.empty()) {
|
|
|
|
for (ifaddrs* p = ifap; p ; p = p->ifa_next) {
|
|
|
|
ifaddrs* cur = p;
|
|
|
|
|
|
|
|
// Ignore if no address for this interface.
|
|
|
|
// @see https://github.com/ossrs/srs/issues/1087#issuecomment-408847115
|
|
|
|
if (!cur->ifa_addr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieve IP address, ignore the tun0 network device, whose addr is NULL.
|
|
|
|
// @see: https://github.com/ossrs/srs/issues/141
|
|
|
|
bool ipv4 = (cur->ifa_addr->sa_family == AF_INET);
|
|
|
|
bool ready = (cur->ifa_flags & IFF_UP) && (cur->ifa_flags & IFF_RUNNING);
|
|
|
|
bool ignored = (!cur->ifa_addr) || (cur->ifa_flags & IFF_POINTOPOINT) || (cur->ifa_flags & IFF_PROMISC);
|
|
|
|
bool loopback = (cur->ifa_flags & IFF_LOOPBACK);
|
|
|
|
if (ipv4 && ready && !ignored) {
|
|
|
|
discover_network_iface(cur, ips, ss0, ss1, false, loopback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
srs_trace("%s", ss0.str().c_str());
|
|
|
|
srs_trace("%s", ss1.str().c_str());
|
|
|
|
|
|
|
|
freeifaddrs(ifap);
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<SrsIPAddress*>& srs_get_local_ips()
|
|
|
|
{
|
|
|
|
if (_srs_system_ips.empty()) {
|
|
|
|
retrieve_local_ips();
|
|
|
|
}
|
|
|
|
|
|
|
|
return _srs_system_ips;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string _public_internet_address;
|
|
|
|
|
|
|
|
string srs_get_public_internet_address(bool ipv4_only)
|
|
|
|
{
|
|
|
|
if (!_public_internet_address.empty()) {
|
|
|
|
return _public_internet_address;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<SrsIPAddress*>& ips = srs_get_local_ips();
|
|
|
|
|
|
|
|
// find the best match public address.
|
|
|
|
for (int i = 0; i < (int)ips.size(); i++) {
|
|
|
|
SrsIPAddress* ip = ips[i];
|
|
|
|
if (!ip->is_internet) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ipv4_only && !ip->is_ipv4) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
srs_warn("use public address as ip: %s, ifname=%s", ip->ip.c_str(), ip->ifname.c_str());
|
|
|
|
_public_internet_address = ip->ip;
|
|
|
|
return ip->ip;
|
|
|
|
}
|
|
|
|
|
|
|
|
// no public address, use private address.
|
|
|
|
for (int i = 0; i < (int)ips.size(); i++) {
|
|
|
|
SrsIPAddress* ip = ips[i];
|
|
|
|
if (ip->is_loopback) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ipv4_only && !ip->is_ipv4) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
srs_warn("use private address as ip: %s, ifname=%s", ip->ip.c_str(), ip->ifname.c_str());
|
|
|
|
_public_internet_address = ip->ip;
|
|
|
|
return ip->ip;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, use first whatever kind of address.
|
|
|
|
if (!ips.empty() && _public_internet_address.empty()) {
|
|
|
|
SrsIPAddress* ip = ips[0];
|
|
|
|
|
|
|
|
srs_warn("use first address as ip: %s, ifname=%s", ip->ip.c_str(), ip->ifname.c_str());
|
|
|
|
_public_internet_address = ip->ip;
|
|
|
|
return ip->ip;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
string srs_get_original_ip(ISrsHttpMessage* r)
|
|
|
|
{
|
|
|
|
SrsHttpHeader* h = r->header();
|
|
|
|
|
|
|
|
string x_forwarded_for = h->get("X-Forwarded-For");
|
|
|
|
if (!x_forwarded_for.empty()) {
|
|
|
|
size_t pos = string::npos;
|
|
|
|
if ((pos = x_forwarded_for.find(",")) == string::npos) {
|
|
|
|
return x_forwarded_for;
|
|
|
|
}
|
|
|
|
return x_forwarded_for.substr(0, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
string x_real_ip = h->get("X-Real-IP");
|
|
|
|
if (!x_real_ip.empty()) {
|
|
|
|
size_t pos = string::npos;
|
|
|
|
if ((pos = x_real_ip.find(":")) == string::npos) {
|
|
|
|
return x_real_ip;
|
|
|
|
}
|
|
|
|
return x_real_ip.substr(0, pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string _srs_system_hostname;
|
|
|
|
|
|
|
|
string srs_get_system_hostname()
|
|
|
|
{
|
|
|
|
if (!_srs_system_hostname.empty()) {
|
|
|
|
return _srs_system_hostname;
|
|
|
|
}
|
|
|
|
|
|
|
|
char buf[256];
|
|
|
|
if (-1 == gethostname(buf, sizeof(buf))) {
|
|
|
|
srs_warn("gethostbyname fail");
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
_srs_system_hostname = std::string(buf);
|
|
|
|
return _srs_system_hostname;
|
|
|
|
}
|
|
|
|
|