提交 68fa78ca authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Remove Ragel based IP parser. Use uv_inet_pton instead. Fixes #365

上级 ce49d4d5
...@@ -118,7 +118,7 @@ Rewrites mediasoup-worker C++ files using [clang-format](https://clang.llvm.org/ ...@@ -118,7 +118,7 @@ Rewrites mediasoup-worker C++ files using [clang-format](https://clang.llvm.org/
Generates the `worker/compile_commands_template.json` file which is a ["Clang compilation database"](https://clang.llvm.org/docs/JSONCompilationDatabase.html). Generates the `worker/compile_commands_template.json` file which is a ["Clang compilation database"](https://clang.llvm.org/docs/JSONCompilationDatabase.html).
It requires [Bear](https://github.com/rizsotto/Bear) and [jq](https://stedolan.github.io/jq/) command-line JSON processor. Install them in Debian/Ubuntu via `apt install bear jq` and in OSX via `brew install bear jq`. It requires [Bear](https://github.com/rizsotto/Bear). Install it in Debian/Ubuntu via `apt install bear` and in OSX via `brew install bear`.
**NOTE:** For now, Bear version must be 2.1.X. **NOTE:** For now, Bear version must be 2.1.X.
......
...@@ -13,27 +13,13 @@ const workerFiles = ...@@ -13,27 +13,13 @@ const workerFiles =
gulp.task('lint:worker', () => gulp.task('lint:worker', () =>
{ {
const src = workerFiles.concat( return gulp.src(workerFiles)
// Ignore Ragel generated files.
'!worker/src/Utils/IP.cpp',
// Ignore json.hpp.
'!worker/include/json.hpp'
);
return gulp.src(src)
.pipe(clangFormat.checkFormat('file', null, { verbose: true, fail: true })); .pipe(clangFormat.checkFormat('file', null, { verbose: true, fail: true }));
}); });
gulp.task('format:worker', () => gulp.task('format:worker', () =>
{ {
const src = workerFiles.concat( return gulp.src(workerFiles, { base: '.' })
// Ignore Ragel generated files.
'!worker/src/Utils/IP.cpp',
// Ignore json.hpp.
'!worker/include/json.hpp'
);
return gulp.src(src, { base: '.' })
.pipe(clangFormat.format('file')) .pipe(clangFormat.format('file'))
.pipe(gulp.dest('.')); .pipe(gulp.dest('.'));
}); });
...@@ -7,9 +7,8 @@ RUN \ ...@@ -7,9 +7,8 @@ RUN \
&& apt-get install --yes \ && apt-get install --yes \
bash-completion wget curl subversion screen gcc g++ cmake ninja-build golang \ bash-completion wget curl subversion screen gcc g++ cmake ninja-build golang \
autoconf libtool apache2 python-dev pkg-config zlib1g-dev libgcrypt11-dev \ autoconf libtool apache2 python-dev pkg-config zlib1g-dev libgcrypt11-dev \
libgss-dev libssl-dev libxml2-dev ragel nasm libarchive-dev make bear \ libgss-dev libssl-dev libxml2-dev nasm libarchive-dev make bear automake \
automake libdbus-1-dev libboost-dev autoconf-archive bash-completion \ libdbus-1-dev libboost-dev autoconf-archive bash-completion python-yaml
python-yaml jq
# Install node 10. # Install node 10.
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash - RUN curl -sL https://deb.nodesource.com/setup_10.x | bash -
......
...@@ -11,7 +11,6 @@ MEDIASOUP_BUILDTYPE ?= Release ...@@ -11,7 +11,6 @@ MEDIASOUP_BUILDTYPE ?= Release
GULP = ../node_modules/.bin/gulp GULP = ../node_modules/.bin/gulp
LCOV = ./deps/lcov/bin/lcov LCOV = ./deps/lcov/bin/lcov
BEAR ?= bear BEAR ?= bear
JQ ?= jq
SED ?= sed SED ?= sed
DOCKER ?= docker DOCKER ?= docker
...@@ -50,9 +49,7 @@ format: ...@@ -50,9 +49,7 @@ format:
bear: bear:
$(MAKE) clean $(MAKE) clean
$(BEAR) -o compile_commands_template.tmp.json $(MAKE) $(BEAR) -o compile_commands_template.json $(MAKE)
$(JQ) 'del(.[] | select(.file == "$(PWD)/src/Utils/IP.cpp"))' compile_commands_template.tmp.json > compile_commands_template.json
$(RM) -f compile_commands_template.tmp.json
$(SED) -i "s|$(PWD)|PATH|g" compile_commands_template.json $(SED) -i "s|$(PWD)|PATH|g" compile_commands_template.json
tidy: tidy:
......
差异被折叠。
#define MS_CLASS "Utils::IP"
// #define MS_LOG_DEV_LEVEL 3
#include "Utils.hpp"
#include "Logger.hpp"
#include "MediaSoupErrors.hpp"
#include <uv.h>
namespace Utils
{
int IP::GetFamily(const char *ip, size_t ipLen)
{
MS_TRACE();
int ipFamily{ 0 };
/**
* Ragel: machine definition.
*/
%%{
machine IPParser;
alphtype unsigned char;
action on_ipv4
{
ipFamily = AF_INET;
}
action on_ipv6
{
ipFamily = AF_INET6;
}
include grammar "grammar.rl";
main := IPv4address @on_ipv4 |
IPv6address @on_ipv6;
}%%
/**
* Ragel: %%write data
* This generates Ragel's static variables.
*/
%%write data;
// Used by Ragel.
size_t cs;
const unsigned char* p;
const unsigned char* pe;
p = (const unsigned char*)ip;
pe = p + ipLen;
/**
* Ragel: %%write init
*/
%%write init;
/**
* Ragel: %%write exec
* This updates cs variable needed by Ragel.
*/
%%write exec;
// Ensure that the parsing has consumed all the given length.
if (ipLen == static_cast<size_t>(p - (const unsigned char*)ip))
return ipFamily;
else
return AF_UNSPEC;
}
void IP::GetAddressInfo(const struct sockaddr* addr, int& family, std::string& ip, uint16_t& port)
{
MS_TRACE();
char ipBuffer[INET6_ADDRSTRLEN] = { 0 };
int err;
switch (addr->sa_family)
{
case AF_INET:
{
err = uv_inet_ntop(
AF_INET, std::addressof(reinterpret_cast<const struct sockaddr_in*>(addr)->sin_addr), ipBuffer, sizeof(ipBuffer));
if (err)
MS_ABORT("uv_inet_ntop() failed: %s", uv_strerror(err));
port = static_cast<uint16_t>(ntohs(reinterpret_cast<const struct sockaddr_in*>(addr)->sin_port));
break;
}
case AF_INET6:
{
err = uv_inet_ntop(
AF_INET6, std::addressof(reinterpret_cast<const struct sockaddr_in6*>(addr)->sin6_addr), ipBuffer, sizeof(ipBuffer));
if (err)
MS_ABORT("uv_inet_ntop() failed: %s", uv_strerror(err));
port = static_cast<uint16_t>(ntohs(reinterpret_cast<const struct sockaddr_in6*>(addr)->sin6_port));
break;
}
default:
{
MS_ABORT("unknown network family: %d", static_cast<int>(addr->sa_family));
}
}
family = addr->sa_family;
ip.assign(ipBuffer);
}
void IP::NormalizeIp(std::string& ip)
{
MS_TRACE();
static sockaddr_storage addrStorage;
char ipBuffer[INET6_ADDRSTRLEN] = { 0 };
int err;
switch (IP::GetFamily(ip))
{
case AF_INET:
{
err = uv_ip4_addr(
ip.c_str(),
0,
reinterpret_cast<struct sockaddr_in*>(&addrStorage));
if (err != 0)
{
MS_THROW_TYPE_ERROR(
"uv_ip4_addr() failed [ip:'%s']: %s",
ip.c_str(), uv_strerror(err));
}
err = uv_ip4_name(
reinterpret_cast<const struct sockaddr_in*>(std::addressof(addrStorage)),
ipBuffer,
sizeof(ipBuffer));
if (err != 0)
{
MS_THROW_TYPE_ERROR(
"uv_ipv4_name() failed [ip:'%s']: %s",
ip.c_str(), uv_strerror(err));
}
ip.assign(ipBuffer);
break;
}
case AF_INET6:
{
err = uv_ip6_addr(
ip.c_str(),
0,
reinterpret_cast<struct sockaddr_in6*>(&addrStorage));
if (err != 0)
{
MS_THROW_TYPE_ERROR(
"uv_ip6_addr() failed [ip:'%s']: %s",
ip.c_str(), uv_strerror(err));
}
err = uv_ip6_name(
reinterpret_cast<const struct sockaddr_in6*>(std::addressof(addrStorage)),
ipBuffer,
sizeof(ipBuffer));
if (err != 0)
{
MS_THROW_TYPE_ERROR(
"uv_ipv6_name() failed [ip:'%s']: %s",
ip.c_str(), uv_strerror(err));
}
ip.assign(ipBuffer);
break;
}
default:
{
MS_THROW_TYPE_ERROR("invalid ip '%s'", ip.c_str());
}
}
}
} // namespace Utils
#!/usr/bin/env bash
ROOT_DIR="../../"
RAGEL_FILE="src/Utils/IP.rl"
RAGEL_COMPILE_TYPE="-G2"
PRODUCTION=false
set -e
which ragel >/dev/null
if [ $? -ne 0 ] ; then
echo ">>> ERROR: ragel binary not found, cannot compile the Ragel grammar" >&2
exit 1
else
ragel -v
fi
cd $ROOT_DIR
cpp_file="${RAGEL_FILE%.rl}.cpp"
echo ">>> INFO: compiling $RAGEL_FILE:"
if [ "$PRODUCTION" == "true" ] ; then
show_ragel_lines="-L"
else
show_ragel_lines=""
fi
ragel $RAGEL_COMPILE_TYPE -C $RAGEL_FILE $show_ragel_lines -o $cpp_file
# Remove unused Ragel variables.
sed -i".delete_me" -E "s/.*static const int IPParser_(en_main|first_final|error).*//g" $cpp_file
echo ">>> DEBUG: Ragel's unused static variables removed from $cpp_file"
rm -f ${cpp_file}.delete_me
echo ">>> INFO: $cpp_file generated"
%%{
machine grammar;
CRLF = "\r\n";
SP = 0x20; # Space.
DIGIT = "0".."9";
ALPHA = "a".."z" | "A".."Z";
alphanum = ALPHA | DIGIT;
HEXDIG = DIGIT | "A"i | "B"i | "C"i | "D"i | "E"i | "F"i;
VCHAR = 0x21..0x7E; # Visible (printing) characters.
POS_DIGIT = "1".."9";
integer = POS_DIGIT DIGIT*;
# Max unsigned long (in 32 bits) is +4.294.967.295, so just allow 9 digits.
ULONG = DIGIT{1,9};
decimal_uchar = DIGIT | ( 0x31..0x39 DIGIT ) | ( "1" DIGIT{2} ) | ( "2" 0x30..0x34 DIGIT ) |
( "25" 0x30..0x35 ); # 0-255.
token = ( 0x21 | 0x23..0x27 | 0x2A..0x2B | 0x2D..0x2E | 0x30..0x39 | 0x41..0x5A |
0x5E..0x7E )+;
byte_string = ( 0x01..0x09 | 0x0B..0x0C | 0x0E..0xFF )+; # Any byte except NULL, CR or LF.
non_ws_string = ( VCHAR | 0x80..0xFF )+; # String of visible characters.
text = byte_string;
IPv4address = decimal_uchar "." decimal_uchar "." decimal_uchar "." decimal_uchar;
h16 = HEXDIG{1,4};
ls32 = ( h16 ":" h16 ) | IPv4address;
scopeId = "%" alphanum{1,20};
IPv6address = ( ( h16 ":" ){6} ls32 scopeId? ) |
( "::" ( h16 ":" ){5} ls32 scopeId? ) |
( h16? "::" ( h16 ":" ){4} ls32 scopeId? ) |
( ( ( h16 ":" )? h16 )? "::" ( h16 ":" ){3} ls32 scopeId? ) |
( ( ( h16 ":" ){,2} h16 )? "::" ( h16 ":" ){2} ls32 scopeId? ) |
( ( ( h16 ":" ){,3} h16 )? "::" h16 ":" ls32 scopeId? ) |
( ( ( h16 ":" ){,4} h16 )? "::" ls32 scopeId? ) |
( ( ( h16 ":" ){,5} h16 )? "::" h16 scopeId? ) |
( ( ( h16 ":" ){,6} h16 )? "::" scopeId? );
port = DIGIT{1,4} |
"1".."5" DIGIT{4} |
"6" "0".."4" DIGIT{3} |
"6" "5" "0".."4" DIGIT{2} |
"6" "5" "5" "0".."2" DIGIT |
"6" "5" "5" "3" "0".."5";
domainlabel = alphanum | ( alphanum ( alphanum | "-" | "_" )* alphanum );
toplabel = ALPHA | ( ALPHA ( alphanum | "-" | "_" )* alphanum );
# TODO: Probably relax it and allow "anything".
FQDN = ( domainlabel "." )* toplabel "."?;
IPv4address_multicast = IPv4address "/" decimal_uchar ( "/" integer )?;
IPv6address_multicast = IPv6address ( "/" integer )?;
unicast_address = IPv4address | IPv6address | FQDN; # NOTE: let's remove non_ws_string.
multicast_address = IPv4address_multicast | IPv6address_multicast | FQDN; # NOTE: let's remove non_ws_string.
}%%
...@@ -31,14 +31,11 @@ SCENARIO("Utils::IP::GetFamily()") ...@@ -31,14 +31,11 @@ SCENARIO("Utils::IP::GetFamily()")
ip = "a:b:c:D::0"; ip = "a:b:c:D::0";
REQUIRE(IP::GetFamily(ip) == AF_INET6); REQUIRE(IP::GetFamily(ip) == AF_INET6);
// NOTE: This is problematic since it's detected as valid IPv6 by our Ragel
// parser but then it fails in NormalizeIp() in uv_ip6_addr().
ip = "::0:"; ip = "::0:";
REQUIRE(IP::GetFamily(ip) == AF_INET6); REQUIRE(IP::GetFamily(ip) == AF_UNSPEC);
// NOTE: The same.
ip = "3::3:1:"; ip = "3::3:1:";
REQUIRE(IP::GetFamily(ip) == AF_INET6); REQUIRE(IP::GetFamily(ip) == AF_UNSPEC);
ip = "chicken"; ip = "chicken";
REQUIRE(IP::GetFamily(ip) == AF_UNSPEC); REQUIRE(IP::GetFamily(ip) == AF_UNSPEC);
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论