/src/node/src/quic/preferredaddress.h
Line | Count | Source |
1 | | #pragma once |
2 | | |
3 | | #if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
4 | | |
5 | | #include <env.h> |
6 | | #include <ngtcp2/ngtcp2.h> |
7 | | #include <node_internals.h> |
8 | | #include <node_realm.h> |
9 | | #include <v8.h> |
10 | | #include <string> |
11 | | #include "defs.h" |
12 | | |
13 | | namespace node::quic { |
14 | | |
15 | | // PreferredAddress is a helper class used only when a client Session receives |
16 | | // an advertised preferred address from a server. The helper provides |
17 | | // information about the server advertised preferred address and allows |
18 | | // the preferred address to be selected. |
19 | | class PreferredAddress final { |
20 | | public: |
21 | | enum class Policy : uint8_t { |
22 | | // Ignore the server-advertised preferred address. |
23 | | IGNORE_PREFERRED, |
24 | | // Use the server-advertised preferred address. |
25 | | USE_PREFERRED, |
26 | | }; |
27 | | |
28 | | static v8::Maybe<Policy> tryGetPolicy(Environment* env, |
29 | | v8::Local<v8::Value> value); |
30 | | static inline v8::Maybe<Policy> tryGetPolicy(Realm* realm, |
31 | 0 | v8::Local<v8::Value> value) { |
32 | 0 | return tryGetPolicy(realm->env(), value); |
33 | 0 | } |
34 | | |
35 | | static void Initialize(Environment* env, v8::Local<v8::Object> target); |
36 | 0 | static inline void Initialize(Realm* realm, v8::Local<v8::Object> target) { |
37 | 0 | return Initialize(realm->env(), target); |
38 | 0 | } |
39 | | |
40 | | struct AddressInfo final { |
41 | | char host[NI_MAXHOST]; |
42 | | int family; |
43 | | uint16_t port; |
44 | | std::string_view address; |
45 | | }; |
46 | | |
47 | | explicit PreferredAddress(ngtcp2_path* dest, |
48 | | const ngtcp2_preferred_addr* paddr); |
49 | | DISALLOW_COPY_AND_MOVE(PreferredAddress) |
50 | | void* operator new(size_t) = delete; |
51 | | void operator delete(void*) = delete; |
52 | | |
53 | | void Use(const AddressInfo& address); |
54 | | |
55 | | std::optional<const AddressInfo> ipv4() const; |
56 | | std::optional<const AddressInfo> ipv6() const; |
57 | | |
58 | | const CID cid() const; |
59 | | |
60 | | // Set the preferred address in the transport params. |
61 | | // The address family (ipv4 or ipv6) will be automatically |
62 | | // detected from the given addr. Any other address family |
63 | | // will be ignored. |
64 | | static void Set(ngtcp2_transport_params* params, const sockaddr* addr); |
65 | | |
66 | | private: |
67 | | ngtcp2_path* dest_; |
68 | | const ngtcp2_preferred_addr* paddr_; |
69 | | }; |
70 | | |
71 | | } // namespace node::quic |
72 | | |
73 | | #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |