2011-06-21 21:42:20 +10:00
|
|
|
/*
|
|
|
|
* 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/>.
|
|
|
|
*
|
2016-01-19 18:30:53 +11:00
|
|
|
* Copyright 2011-2016 Danny Robson <danny@nerdcruft.net>
|
2011-06-21 21:42:20 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "ip.hpp"
|
|
|
|
|
2015-11-17 16:19:27 +11:00
|
|
|
#include "cast.hpp"
|
2012-06-13 15:45:33 +10:00
|
|
|
|
2011-06-21 21:42:20 +10:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
2015-09-09 18:42:39 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2011-06-29 21:24:06 +10:00
|
|
|
const ipv4::ip ipv4::ip::LOOPBACK (127, 0, 0, 1);
|
|
|
|
const ipv4::ip ipv4::ip::ANY ( 0, 0, 0, 0);
|
|
|
|
|
2015-09-09 18:42:39 +10:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2016-01-19 18:30:53 +11:00
|
|
|
const util::range<ipv4::port_t> ipv4::WELL_KNOWN_PORT ( 0, 1023),
|
|
|
|
ipv4::REGISTERED_PORT ( 1024, 49151),
|
|
|
|
ipv4::PRIVATE_PORT (49152, 65535);
|
2011-06-23 22:07:55 +10:00
|
|
|
|
|
|
|
|
2015-09-09 18:42:39 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2011-06-30 20:34:53 +10:00
|
|
|
ipv4::ip::ip (uint32_t _integer):
|
|
|
|
m_integer (_integer)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
2015-09-09 18:42:39 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2012-05-26 18:01:04 +10:00
|
|
|
ipv4::ip::ip (uint8_t a, uint8_t b, uint8_t c, uint8_t d)
|
|
|
|
{
|
|
|
|
m_octets[0] = a;
|
|
|
|
m_octets[1] = b;
|
|
|
|
m_octets[2] = c;
|
|
|
|
m_octets[3] = d;
|
|
|
|
}
|
2011-06-21 21:42:20 +10:00
|
|
|
|
|
|
|
|
2015-09-09 18:42:39 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2011-06-21 21:42:20 +10:00
|
|
|
bool
|
2015-09-09 18:42:39 +10:00
|
|
|
ipv4::ip::operator== (const ipv4::ip &rhs) const
|
|
|
|
{
|
2011-06-21 21:42:20 +10:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-09 18:42:39 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2011-06-21 21:42:20 +10:00
|
|
|
// 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)
|
2012-06-13 15:45:33 +10:00
|
|
|
__octet = __octet * 10u + sign_cast<unsigned> (*i - '0');
|
2011-06-21 21:42:20 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
ipv4 := (octet %{ __octets[0] = __octet; } '.'
|
|
|
|
octet %{ __octets[1] = __octet; } '.'
|
|
|
|
octet %{ __octets[2] = __octet; } '.'
|
|
|
|
octet %{ __octets[3] = __octet; })
|
|
|
|
> { __success = false; }
|
|
|
|
% { __success = true; }
|
|
|
|
$!{ __success = false; };
|
|
|
|
}%%
|
|
|
|
|
2015-09-09 18:42:39 +10:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-06-21 21:42:20 +10:00
|
|
|
%%write data;
|
|
|
|
|
|
|
|
|
2015-09-09 18:42:39 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-09-09 18:42:00 +10:00
|
|
|
ipv4::ip::ip (const std::string &data)
|
|
|
|
{
|
2011-06-21 21:42:20 +10:00
|
|
|
bool __success = true;
|
2012-04-11 15:54:52 +10:00
|
|
|
uint8_t __octets[4] = { 0, 0, 0, 0 };
|
2015-09-09 18:42:00 +10:00
|
|
|
const char *octetstart = data.data ();
|
|
|
|
const char *octetend = nullptr;
|
2011-06-21 21:42:20 +10:00
|
|
|
uint8_t __octet;
|
|
|
|
|
|
|
|
int cs = 0;
|
|
|
|
const char *p = data.data (),
|
|
|
|
*pe = p + data.size (),
|
|
|
|
*eof = pe;
|
|
|
|
|
|
|
|
%%write init;
|
|
|
|
%%write exec;
|
|
|
|
|
|
|
|
if (!__success)
|
2016-01-19 18:30:53 +11:00
|
|
|
throw ipv4::error ();
|
2011-06-21 21:42:20 +10:00
|
|
|
|
2011-06-29 21:24:06 +10:00
|
|
|
m_octets[0] = __octets[0];
|
|
|
|
m_octets[1] = __octets[1];
|
|
|
|
m_octets[2] = __octets[2];
|
|
|
|
m_octets[3] = __octets[3];
|
2011-06-21 21:42:20 +10:00
|
|
|
}
|
2011-06-29 21:24:06 +10:00
|
|
|
|
|
|
|
|
2015-09-09 18:42:39 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2011-06-29 21:24:06 +10:00
|
|
|
ipv4::ip
|
2016-03-11 13:28:56 +11:00
|
|
|
ipv4::ip::parse (const std::string &data)
|
2011-06-29 21:24:06 +10:00
|
|
|
{ return ipv4::ip (data); }
|
|
|
|
|