Skip to content

Commit c93832f

Browse files
authored
Merge pull request #1280 from AnnikaH/dev
Updates based on feedback on #1222 and more
2 parents 0245cec + 2bfe5d1 commit c93832f

47 files changed

Lines changed: 890 additions & 440 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

api/net/dns/client.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ namespace net
7777
}
7878

7979
void finish()
80-
{ callback(request.getFirstIP4()); }
80+
{
81+
Error err;
82+
callback(request.getFirstIP4(), err);
83+
}
8184

8285
void start_timeout(Timer::duration_t timeout)
8386
{ timer.start(timeout); }

api/net/http/client.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace http {
3333
class Client {
3434
public:
3535
using TCP = net::TCP;
36-
using Host = net::tcp::Socket;
36+
using Host = net::Socket;
3737

3838
using Response_handler = Client_connection::Response_handler;
3939
struct Options;
@@ -67,7 +67,7 @@ namespace http {
6767
};
6868

6969
private:
70-
using ResolveCallback = delegate<void(net::ip4::Addr)>;
70+
using ResolveCallback = delegate<void(net::ip4::Addr, net::Error&)>;
7171

7272
public:
7373
explicit Client(TCP& tcp, Request_handler on_send = nullptr);

api/net/http/connection.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace http {
3131
public:
3232
using Stream = net::tcp::Connection::Stream;
3333
using Stream_ptr = std::unique_ptr<Stream>;
34-
using Peer = net::tcp::Socket;
34+
using Peer = net::Socket;
3535
using buffer_t = net::tcp::buffer_t;
3636

3737
public:

api/net/http/websocket.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class WebSocket {
3030
// When a handshake is established and the WebSocket is created
3131
using Connect_handler = delegate<void(WebSocket_ptr)>;
3232
// Whether to accept the client or not before handshake
33-
using Accept_handler = delegate<bool(net::tcp::Socket, std::string)>;
33+
using Accept_handler = delegate<bool(net::Socket, std::string)>;
3434
// data read (data, length)
3535
typedef delegate<void(const char*, size_t)> read_func;
3636
// closed (status code)

api/net/http/ws_connector.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class WS_connector {
4444
*/
4545
class WS_server_connector : public WS_connector {
4646
public:
47-
using AcceptCallback = delegate<bool(net::tcp::Socket peer, const std::string& origin)>;
47+
using AcceptCallback = delegate<bool(net::Socket peer, const std::string& origin)>;
4848
using Request_handler = delegate<void(Request_ptr, Response_writer_ptr)>;
4949

5050
/**

api/net/inet.hpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace net {
3030
class TCP;
3131
class UDP;
3232
class DHClient;
33-
struct ICMPv4;
33+
class ICMPv4;
3434

3535
/**
3636
* An abstract IP-stack interface.
@@ -44,28 +44,28 @@ namespace net {
4444
using Route_checker = delegate<bool(typename IPV::addr)>;
4545
using IP_packet_factory = delegate<typename IPV::IP_packet_ptr(Protocol)>;
4646

47-
using Error_type = icmp4::Type;
48-
using Error_code = uint8_t;
49-
5047
template <typename IPv>
51-
using resolve_func = delegate<void(typename IPv::addr)>;
48+
using resolve_func = delegate<void(typename IPv::addr, Error&)>;
5249
using Vip_list = std::unordered_set<typename IPV::addr>;
5350

5451
///
5552
/// NETWORK CONFIGURATION
5653
///
5754

5855
/** Get IP address of this interface **/
59-
virtual typename IPV::addr ip_addr() = 0;
56+
virtual typename IPV::addr ip_addr() = 0;
6057

6158
/** Get netmask of this interface **/
62-
virtual typename IPV::addr netmask() = 0;
59+
virtual typename IPV::addr netmask() = 0;
6360

6461
/** Get default gateway for this interface **/
65-
virtual typename IPV::addr gateway() = 0;
62+
virtual typename IPV::addr gateway() = 0;
6663

6764
/** Get default dns for this interface **/
68-
virtual typename IPV::addr dns() = 0;
65+
virtual typename IPV::addr dns_addr() = 0;
66+
67+
/** Get broadcast address for this interface **/
68+
virtual typename IPV::addr broadcast_addr() = 0;
6969

7070
/** Set default gateway for this interface */
7171
virtual void set_gateway(typename IPV::addr server) = 0;
@@ -120,13 +120,15 @@ namespace net {
120120
virtual UDP& udp() = 0;
121121

122122
/** Get the ICMP protocol object for this interface */
123-
virtual ICMPv4& icmp() = 0;
123+
virtual ICMPv4& icmp() = 0;
124124

125125
/**
126-
* Error report in accordance with RFC 1122
126+
* Error reporting
127+
* Incl. ICMP error report in accordance with RFC 1122
127128
* An ICMP error message has been received - forward to transport layer (UDP or TCP)
128129
*/
129-
virtual void error_report(Error_type type, Error_code code, Packet_ptr orig_pckt) = 0;
130+
virtual void error_report(Error& err, Packet_ptr orig_pckt) = 0;
131+
130132

131133

132134
///
@@ -135,7 +137,7 @@ namespace net {
135137

136138
/** DNS resolution */
137139
virtual void resolve(const std::string& hostname, resolve_func<IPV> func) = 0;
138-
140+
virtual void resolve(const std::string& hostname, typename IPV::addr server, resolve_func<IPV> func) = 0;
139141

140142
///
141143
/// LINK LAYER

api/net/inet4.hpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@ namespace net {
5858
IP4::addr gateway() override
5959
{ return gateway_; }
6060

61-
IP4::addr dns() override
62-
{ return dns_server; }
61+
IP4::addr dns_addr() override
62+
{ return dns_server_; }
63+
64+
IP4::addr broadcast_addr() override
65+
{ return ip4_addr_ | ( ~ netmask_); }
6366

6467
IP4& ip_obj() override
6568
{ return ip4_; }
@@ -86,10 +89,11 @@ namespace net {
8689
auto dhclient() { return dhcp_; }
8790

8891
/**
89-
* Error report in accordance with RFC 1122
92+
* Error reporting
93+
* Incl. ICMP error report in accordance with RFC 1122
9094
* An ICMP error message has been received - forward to transport layer (UDP or TCP)
9195
*/
92-
void error_report(Error_type type, Error_code code, Packet_ptr orig_pckt) override;
96+
void error_report(Error& err, Packet_ptr orig_pckt) override;
9397

9498
/**
9599
* Set the forwarding delegate used by this stack.
@@ -141,7 +145,14 @@ namespace net {
141145
void resolve(const std::string& hostname,
142146
resolve_func<IP4> func) override
143147
{
144-
dns_.resolve(this->dns_server, hostname, func);
148+
dns_.resolve(this->dns_server_, hostname, func);
149+
}
150+
151+
void resolve(const std::string& hostname,
152+
IP4::addr server,
153+
resolve_func<IP4> func) override
154+
{
155+
dns_.resolve(server, hostname, func);
145156
}
146157

147158
void set_gateway(IP4::addr gateway) override
@@ -151,7 +162,7 @@ namespace net {
151162

152163
void set_dns_server(IP4::addr server) override
153164
{
154-
this->dns_server = server;
165+
this->dns_server_ = server;
155166
}
156167

157168
/**
@@ -181,12 +192,12 @@ namespace net {
181192
this->ip4_addr_ = addr;
182193
this->netmask_ = nmask;
183194
this->gateway_ = gateway;
184-
this->dns_server = (dns == IP4::ADDR_ANY) ? gateway : dns;
195+
this->dns_server_ = (dns == IP4::ADDR_ANY) ? gateway : dns;
185196
INFO("Inet4", "Network configured");
186197
INFO2("IP: \t\t%s", ip4_addr_.str().c_str());
187198
INFO2("Netmask: \t%s", netmask_.str().c_str());
188199
INFO2("Gateway: \t%s", gateway_.str().c_str());
189-
INFO2("DNS Server: \t%s", dns_server.str().c_str());
200+
INFO2("DNS Server: \t%s", dns_server_.str().c_str());
190201
}
191202

192203
virtual void
@@ -298,7 +309,7 @@ namespace net {
298309
IP4::addr ip4_addr_;
299310
IP4::addr netmask_;
300311
IP4::addr gateway_;
301-
IP4::addr dns_server;
312+
IP4::addr dns_server_;
302313

303314
Vip4_list vip4s_ = {{127,0,0,1}};
304315

api/net/inet_common.hpp

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,45 +59,98 @@ namespace net {
5959
return std::static_pointer_cast<T>(packet);
6060
}
6161

62-
6362
template<typename Derived, typename Base>
6463
auto static_unique_ptr_cast( std::unique_ptr<Base>&& p )
6564
{
6665
auto* d = static_cast<Derived *>(p.release());
6766
return std::unique_ptr<Derived>(d);
6867
}
6968

69+
/**
70+
* General Error class for the OS
71+
* ICMP_error f.ex. inherits from this class
72+
*/
7073
class Error {
7174
public:
75+
7276
enum class Type : uint8_t {
7377
no_error,
7478
general_IO,
7579
ifdown,
7680
ICMP
7781
// Add more as needed
78-
};
79-
80-
virtual Type type()
81-
{ return t_; }
82-
83-
virtual const char* what()
84-
{ return msg_; }
85-
86-
virtual ~Error() = default;
82+
};
8783

8884
Error() = default;
8985

9086
Error(Type t, const char* msg)
9187
: t_{t}, msg_{msg}
92-
{};
88+
{}
9389

94-
operator bool()
90+
virtual ~Error() = default;
91+
92+
Type type()
93+
{ return t_; }
94+
95+
operator bool() const noexcept
9596
{ return t_ != Type::no_error; }
9697

98+
bool is_icmp() const noexcept
99+
{ return t_ == Type::ICMP; }
100+
101+
virtual const char* what() const noexcept
102+
{ return msg_; }
103+
97104
private:
98-
Type t_ = Type::no_error;
99-
const char* msg_ = "No error";
100-
};
105+
Type t_{Type::no_error};
106+
const char* msg_{"No error"};
107+
108+
}; // < class Error
109+
110+
111+
/**
112+
* An object of this error class is sent to UDP and TCP (via Inet) when an ICMP error message
113+
* is received in ICMPv4::receive
114+
*/
115+
class ICMP_error : public Error {
116+
117+
public:
118+
using ICMP_type = icmp4::Type;
119+
using ICMP_code = uint8_t; // Codes in icmp4_common.hpp in namespace icmp4::code
120+
// icmp4::code::Dest_unreachable::PORT f.ex.
121+
122+
ICMP_error()
123+
: Error{}
124+
{}
125+
126+
ICMP_error(ICMP_type icmp_type, ICMP_code icmp_code)
127+
: Error{Error::Type::ICMP, "ICMP error message received"},
128+
icmp_type_{icmp_type}, icmp_code_{icmp_code}
129+
{}
130+
131+
ICMP_type icmp_type() const noexcept
132+
{ return icmp_type_; }
133+
134+
std::string icmp_type_str() const
135+
{ return icmp4::get_type_string(icmp_type_); }
136+
137+
void set_icmp_type(ICMP_type icmp_type)
138+
{ icmp_type_ = icmp_type; }
139+
140+
ICMP_code icmp_code() const noexcept
141+
{ return icmp_code_; }
142+
143+
std::string icmp_code_str() const
144+
{ return icmp4::get_code_string(icmp_type_, icmp_code_); }
145+
146+
void set_icmp_code(ICMP_code icmp_code)
147+
{ icmp_code_ = icmp_code; }
148+
149+
private:
150+
ICMP_type icmp_type_{ICMP_type::NO_ERROR};
151+
ICMP_code icmp_code_{0};
152+
153+
}; // < class ICMP_error
101154

102155

103156
/* RFC 6335 - IANA */

api/net/ip4/addr.hpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -323,15 +323,11 @@ struct Addr {
323323
{ return part(3) == 127; }
324324

325325
/**
326-
* Determine if an address is a-priori illegal as source address in *all* cases.
327-
*
328-
* @note: The class E range 240/4 for "future use" is not illegal here
329-
* @note: RFC-1122 prohibits 0.0.0.0 as source, but with exceptions
326+
* @note: The class E range 240/4 for "future use" is not included here
327+
* RFC-5771 defining multicast address range from 224.0.0.0 to 239.255.255.255
330328
*/
331-
bool is_illegal_src() const noexcept {
332-
return (part(3) >= 224 and part(3) < 240) // Multicast
333-
or (part(0) == 255); // Limited and directed broadcast
334-
}
329+
bool is_multicast() const noexcept
330+
{ return part(3) >= 224 and part(3) < 240; }
335331

336332
/* Data member */
337333
uint32_t whole;

0 commit comments

Comments
 (0)