sources/network/ipaddress.h

branch
protocol5
changeset 195
be953e1621d9
parent 190
90bf9049e5eb
equal deleted inserted replaced
176:060a13878ca0 195:be953e1621d9
1 /* 1 /*
2 Copyright 2014 - 2016 Teemu Piippo 2 Copyright 2014 - 2021 Teemu Piippo
3 All rights reserved. 3 All rights reserved.
4 4
5 Redistribution and use in source and binary forms, with or without 5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions 6 modification, are permitted provided that the following conditions
7 are met: 7 are met:
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #pragma once 31 #pragma once
32 #include <ostream>
32 #include "../main.h" 33 #include "../main.h"
33 34
34 struct sockaddr; 35 struct sockaddr;
35 struct sockaddr_in; 36 struct sockaddr_in;
36 37
37 BEGIN_ZFC_NAMESPACE 38 BEGIN_ZFC_NAMESPACE
38 39
39 struct IPAddress 40 namespace net
40 { 41 {
41 enum 42 using octet_t = std::uint8_t;
43 using port_t = std::uint16_t;
44 using host_t = std::uint32_t;
45 struct ip_address
42 { 46 {
43 LOCALHOST = 0x7f000001 47 net::host_t host = 0;
48 net::port_t port = 0;
49 bool operator<(const ip_address& other) const;
50 bool operator==(const ip_address& other) const;
44 }; 51 };
45 52 constexpr ip_address localhost = {0x7f000001, 0};
46 enum WithPort 53 int ip_compare(const ip_address& one, const ip_address& other);
47 { 54 net::octet_t ip_octet(const ip_address& address, unsigned char n);
48 WITH_PORT, 55 void ip_set_octet(ip_address* address, unsigned char n, net::octet_t octet);
49 NO_PORT 56 std::optional<unsigned short> ip_parse_port(const char* port_string, std::ostream& errorStream);
50 }; 57 std::optional<ip_address> ip_resolve_hostname(const std::string& node, std::ostream& errorStream);
51 58 std::optional<ip_address> ip_resolve(const std::string& input_string, std::ostream &errorStream);
52 class StringParseError : public std::exception 59 sockaddr_in ip_address_to_sockaddr_in(const ip_address& address);
53 { 60 std::string ip_address_to_string(const ip_address& address);
54 String m_message; 61 }
55
56 public:
57 StringParseError (String message) :
58 m_message (message) {}
59
60 const char* what() const throw()
61 {
62 return m_message.chars();
63 }
64 };
65
66 unsigned long host;
67 unsigned short port;
68
69 IPAddress();
70 IPAddress (unsigned long host, unsigned short port);
71 IPAddress (const IPAddress& other);
72
73 bool compare (const IPAddress& other) const;
74 unsigned char octet (int n) const;
75 void set_octet (int n, unsigned char oct);
76 String to_string (WithPort withport = NO_PORT) const;
77 sockaddr_in to_sockaddr_in() const;
78 bool operator< (const IPAddress& other) const;
79 bool operator== (const IPAddress& other) const { return compare (other); }
80 bool operator!= (const IPAddress& other) const { return not compare (other); }
81 unsigned char operator[] (int n) const { return octet (n); }
82
83 static IPAddress from_string (String input);
84 static IPAddress resolve (String node);
85 };
86 62
87 END_ZFC_NAMESPACE 63 END_ZFC_NAMESPACE

mercurial