ip: throw exception on parse error

This commit is contained in:
Danny Robson 2016-01-19 18:30:53 +11:00
parent bf5b0916e8
commit 08ac4b0e9d
3 changed files with 27 additions and 22 deletions

View File

@ -14,7 +14,7 @@
* 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@nerdcruft.net>
* Copyright 2011-2016 Danny Robson <danny@nerdcruft.net>
*/
@ -35,9 +35,9 @@ const ipv4::ip ipv4::ip::ANY ( 0, 0, 0, 0);
//-----------------------------------------------------------------------------
const util::range<ipv4::port> ipv4::WELL_KNOWN_PORT ( 0, 1023),
ipv4::REGISTERED_PORT ( 1024, 49151),
ipv4::PRIVATE_PORT (49152, 65535);
const util::range<ipv4::port_t> ipv4::WELL_KNOWN_PORT ( 0, 1023),
ipv4::REGISTERED_PORT ( 1024, 49151),
ipv4::PRIVATE_PORT (49152, 65535);
///////////////////////////////////////////////////////////////////////////////
@ -131,7 +131,7 @@ ipv4::ip::ip (const std::string &data)
%%write exec;
if (!__success)
throw invalid_argument(data);
throw ipv4::error ();
m_octets[0] = __octets[0];
m_octets[1] = __octets[1];

23
ip.hpp
View File

@ -11,7 +11,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2011 Danny Robson <danny@nerdcruft.net>
* Copyright 2011-2016 Danny Robson <danny@nerdcruft.net>
*/
#ifndef __UTIL_IP_HPP
@ -46,12 +46,15 @@ namespace ipv4 {
};
typedef uint16_t port;
typedef uint32_t mask;
typedef uint16_t port_t;
typedef uint32_t mask_t;
extern const util::range<port_t> WELL_KNOWN_PORT,
REGISTERED_PORT,
PRIVATE_PORT;
class error : public std::exception { };
extern const util::range<port> WELL_KNOWN_PORT,
REGISTERED_PORT,
PRIVATE_PORT;
}
@ -62,13 +65,15 @@ namespace ipv6 {
explicit ip (const std::string&) { ; }
};
typedef uint16_t port;
typedef uint16_t port_t;
struct mask {
struct mask_t {
uint32_t m_quads[4];
explicit mask (uint32_t) { ; }
explicit mask_t (uint32_t) { ; }
};
class error : public std::exception { };
}

View File

@ -37,18 +37,18 @@ namespace net {
template <>
struct address_types <domain::INET> {
typedef ipv4::ip ip;
typedef ipv4::mask mask;
typedef ipv4::port port;
typedef sockaddr_in sockaddr;
typedef ipv4::ip ip;
typedef ipv4::mask_t mask_t;
typedef ipv4::port_t port_t;
typedef sockaddr_in sockaddr;
};
template <>
struct address_types <domain::INET6> {
typedef ipv6::ip ip;
typedef ipv6::mask mask;
typedef ipv6::port port;
typedef ipv6::mask_t mask_t;
typedef ipv6::port_t port_t;
typedef sockaddr_in6 sockaddr;
};
@ -58,8 +58,8 @@ namespace net {
class address {
public:
typedef typename address_types<D>::ip ip_type;
typedef typename address_types<D>::mask mask_type;
typedef typename address_types<D>::port port_type;
typedef typename address_types<D>::mask_t mask_type;
typedef typename address_types<D>::port_t port_type;
typedef typename address_types<D>::sockaddr sockaddr_type;
protected: