Add first pass of ipv4::ip parsing and object
This commit is contained in:
parent
20f81a7630
commit
8568a325c0
1
.gitignore
vendored
1
.gitignore
vendored
@ -8,6 +8,7 @@
|
|||||||
.dirstamp
|
.dirstamp
|
||||||
/Doxyfile
|
/Doxyfile
|
||||||
/install-sh
|
/install-sh
|
||||||
|
/ip.cpp
|
||||||
/json.cpp
|
/json.cpp
|
||||||
/libgim-*.tar.*
|
/libgim-*.tar.*
|
||||||
.libs
|
.libs
|
||||||
|
@ -13,6 +13,7 @@ UTIL_INCLUDE = \
|
|||||||
except.hpp \
|
except.hpp \
|
||||||
float.hpp \
|
float.hpp \
|
||||||
io.hpp \
|
io.hpp \
|
||||||
|
ip.hpp \
|
||||||
json.hpp \
|
json.hpp \
|
||||||
maths.hpp \
|
maths.hpp \
|
||||||
matrix.hpp \
|
matrix.hpp \
|
||||||
@ -30,6 +31,7 @@ UTIL_FILES = \
|
|||||||
except.cpp \
|
except.cpp \
|
||||||
float.cpp \
|
float.cpp \
|
||||||
io.cpp \
|
io.cpp \
|
||||||
|
ip.cpp \
|
||||||
json.cpp \
|
json.cpp \
|
||||||
maths.cpp \
|
maths.cpp \
|
||||||
matrix.cpp \
|
matrix.cpp \
|
||||||
@ -40,8 +42,8 @@ UTIL_FILES = \
|
|||||||
vector.cpp \
|
vector.cpp \
|
||||||
version.cpp
|
version.cpp
|
||||||
|
|
||||||
CLEANFILES = json.cpp version.cpp
|
CLEANFILES = json.cpp version.cpp ip.cpp
|
||||||
EXTRA_DIST = json.cpp.rl version.cpp.rl
|
EXTRA_DIST = json.cpp.rl version.cpp.rl ip.cpp.rl
|
||||||
|
|
||||||
RAGELFLAGS = -F1
|
RAGELFLAGS = -F1
|
||||||
SUFFIXES = .cpp .cpp.rl
|
SUFFIXES = .cpp .cpp.rl
|
||||||
|
107
ip.cpp.rl
Normal file
107
ip.cpp.rl
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of libgim.
|
||||||
|
*
|
||||||
|
* libgim is free software: you can redistribute it and/or modify it under the
|
||||||
|
* terms of the GNU General Public License as published by the Free Software
|
||||||
|
* Foundation, either version 3 of the License, or (at your option) any later
|
||||||
|
* version.
|
||||||
|
*
|
||||||
|
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||||
|
* details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* Copyright 2011 Danny Robson <danny@blubinc.net>
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#include "ip.hpp"
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
|
ipv4::ip::ip (uint8_t a, uint8_t b, uint8_t c, uint8_t d):
|
||||||
|
m_octets ({ a, b, c, d })
|
||||||
|
{ ; }
|
||||||
|
|
||||||
|
|
||||||
|
ipv4::ip&
|
||||||
|
ipv4::ip::operator = (const ipv4::ip &rhs) {
|
||||||
|
m_octets[0] = rhs.m_octets[0];
|
||||||
|
m_octets[1] = rhs.m_octets[1];
|
||||||
|
m_octets[2] = rhs.m_octets[2];
|
||||||
|
m_octets[3] = rhs.m_octets[3];
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool
|
||||||
|
ipv4::ip::operator == (const ipv4::ip &rhs) const {
|
||||||
|
return m_octets[0] == rhs.m_octets[0] &&
|
||||||
|
m_octets[1] == rhs.m_octets[1] &&
|
||||||
|
m_octets[2] == rhs.m_octets[2] &&
|
||||||
|
m_octets[3] == rhs.m_octets[3];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// RFC 3986
|
||||||
|
%%{
|
||||||
|
machine ipv4;
|
||||||
|
octet = ( [0-9][0-9]? |
|
||||||
|
'1'[0-9][0-9] |
|
||||||
|
'2'[0-4][0-9] |
|
||||||
|
'25'[0-5])
|
||||||
|
> {
|
||||||
|
octetstart = fpc;
|
||||||
|
}
|
||||||
|
% {
|
||||||
|
octetend = fpc;
|
||||||
|
__octet = 0;
|
||||||
|
|
||||||
|
for (auto i = octetstart; i < octetend; ++i)
|
||||||
|
__octet = __octet * 10 + *i - '0';
|
||||||
|
};
|
||||||
|
|
||||||
|
ipv4 := (octet %{ __octets[0] = __octet; } '.'
|
||||||
|
octet %{ __octets[1] = __octet; } '.'
|
||||||
|
octet %{ __octets[2] = __octet; } '.'
|
||||||
|
octet %{ __octets[3] = __octet; })
|
||||||
|
> { __success = false; }
|
||||||
|
% { __success = true; }
|
||||||
|
$!{ __success = false; };
|
||||||
|
}%%
|
||||||
|
|
||||||
|
%%write data;
|
||||||
|
|
||||||
|
|
||||||
|
ipv4::ip
|
||||||
|
ipv4::ip::parse (const string &data) {
|
||||||
|
bool __success = true;
|
||||||
|
uint8_t __octets[4];
|
||||||
|
const char *octetstart, *octetend;
|
||||||
|
uint8_t __octet;
|
||||||
|
|
||||||
|
int cs = 0;
|
||||||
|
const char *p = data.data (),
|
||||||
|
*pe = p + data.size (),
|
||||||
|
*eof = pe;
|
||||||
|
|
||||||
|
%%write init;
|
||||||
|
%%write exec;
|
||||||
|
|
||||||
|
if (!__success)
|
||||||
|
throw invalid_argument(data);
|
||||||
|
|
||||||
|
return ipv4::ip(__octets[0],
|
||||||
|
__octets[1],
|
||||||
|
__octets[2],
|
||||||
|
__octets[3]);
|
||||||
|
}
|
44
ip.hpp
Normal file
44
ip.hpp
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of libgim.
|
||||||
|
*
|
||||||
|
* libgim is free software: you can redistribute it and/or modify it under the
|
||||||
|
* terms of the GNU General Public License as published by the Free Software
|
||||||
|
* Foundation, either version 3 of the License, or (at your option) any later
|
||||||
|
* version.
|
||||||
|
*
|
||||||
|
* libgim is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||||
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||||||
|
* details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with libgim. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* Copyright 2011 Danny Robson <danny@blubinc.net>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __UTIL_IP_HPP
|
||||||
|
#define __UTIL_IP_HPP
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
|
namespace ipv4 {
|
||||||
|
struct ip {
|
||||||
|
uint8_t m_octets[4];
|
||||||
|
|
||||||
|
ip (uint8_t a, uint8_t b, uint8_t c, uint8_t d);
|
||||||
|
|
||||||
|
ip& operator = (const ip &);
|
||||||
|
bool operator == (const ip &) const;
|
||||||
|
|
||||||
|
static ip parse (const std::string &);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
typedef uint16_t port_t;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif // __UTIL_IP_HPP
|
5
test/.gitignore
vendored
5
test/.gitignore
vendored
@ -1,7 +1,8 @@
|
|||||||
/backtrace
|
/backtrace
|
||||||
/float
|
/float
|
||||||
/range
|
/ip
|
||||||
|
/json-check
|
||||||
/maths
|
/maths
|
||||||
/matrix
|
/matrix
|
||||||
|
/range
|
||||||
/version
|
/version
|
||||||
/json-check
|
|
||||||
|
@ -7,7 +7,7 @@ AM_CPPFLAGS = \
|
|||||||
|
|
||||||
AM_LDFLAGS = $(COMMON_LDFLAGS)
|
AM_LDFLAGS = $(COMMON_LDFLAGS)
|
||||||
|
|
||||||
TEST_BIN = backtrace float range maths matrix version
|
TEST_BIN = backtrace float range maths matrix version ip
|
||||||
TESTS = $(TEST_BIN) json.pl
|
TESTS = $(TEST_BIN) json.pl
|
||||||
check_PROGRAMS = $(TEST_BIN) json-check
|
check_PROGRAMS = $(TEST_BIN) json-check
|
||||||
EXTRA_DIST = json.pl
|
EXTRA_DIST = json.pl
|
||||||
@ -31,5 +31,8 @@ matrix_LDADD = $(builddir)/../libutil.la $(BOOST_SYSTEM_LIB)
|
|||||||
version_SOURCES = version.cpp
|
version_SOURCES = version.cpp
|
||||||
version_LDADD = $(builddir)/../libutil.la $(BOOST_SYSTEM_LIB)
|
version_LDADD = $(builddir)/../libutil.la $(BOOST_SYSTEM_LIB)
|
||||||
|
|
||||||
|
ip_SOURCES = ip.cpp
|
||||||
|
ip_LDADD = $(builddir)/../libutil.la $(BOOST_SYSTEM_LIB)
|
||||||
|
|
||||||
json_check_SOURCES = json-check.cpp
|
json_check_SOURCES = json-check.cpp
|
||||||
json_check_LDADD = $(builddir)/../libutil.la $(BOOST_FILESYSTEM_LIB)
|
json_check_LDADD = $(builddir)/../libutil.la $(BOOST_FILESYSTEM_LIB)
|
||||||
|
28
test/ip.cpp
Normal file
28
test/ip.cpp
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
|
||||||
|
#include "../ip.hpp"
|
||||||
|
|
||||||
|
#include "../debug.hpp"
|
||||||
|
#include "../types.hpp"
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
main (int argc, char **argv) {
|
||||||
|
struct ip_test {
|
||||||
|
const char *str;
|
||||||
|
const ipv4::ip ip;
|
||||||
|
} data [] = {
|
||||||
|
{ "0.0.0.0", { 0, 0, 0, 0 } },
|
||||||
|
{ "255.255.255.255", { 255, 255, 255, 255 } },
|
||||||
|
{ "127.0.0.1", { 127, 0, 0, 1 } }
|
||||||
|
};
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < elems (data); ++i)
|
||||||
|
check_hard (ipv4::ip::parse (data[i].str) == data[i].ip);
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user