nftables-napi 0.0.2 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/nft_manager.h CHANGED
@@ -29,6 +29,10 @@ private:
29
29
  Napi::Value AddAddresses(const Napi::CallbackInfo& info);
30
30
  Napi::Value RemoveAddresses(const Napi::CallbackInfo& info);
31
31
  Napi::Value DeleteTable(const Napi::CallbackInfo& info);
32
+ Napi::Value AddPort(const Napi::CallbackInfo& info);
33
+ Napi::Value RemovePort(const Napi::CallbackInfo& info);
34
+ Napi::Value AddPorts(const Napi::CallbackInfo& info);
35
+ Napi::Value RemovePorts(const Napi::CallbackInfo& info);
32
36
 
33
37
  void Enqueue(std::unique_ptr<NlOperation> op, Napi::Promise::Deferred deferred);
34
38
  void DrainQueue();
@@ -41,4 +45,5 @@ private:
41
45
  std::shared_ptr<const nft::NftConfig> config_;
42
46
  std::queue<PendingOp> queue_;
43
47
  bool worker_active_ = false;
48
+ bool valid_ = false;
44
49
  };
@@ -1,6 +1,7 @@
1
1
  #include "validation.h"
2
2
 
3
3
  #include <arpa/inet.h>
4
+ #include <cmath>
4
5
  #include <cstring>
5
6
 
6
7
  IpAddr parse_ip(const std::string& ip) {
@@ -24,3 +25,14 @@ IpAddr parse_ip(const std::string& ip) {
24
25
 
25
26
  return result;
26
27
  }
28
+
29
+ PortVal parse_port(double value) {
30
+ if (std::isnan(value) || value < 0.0 || value > 65535.0) {
31
+ return {0, false};
32
+ }
33
+ double truncated = std::trunc(value);
34
+ if (value != truncated) {
35
+ return {0, false}; // reject fractional ports
36
+ }
37
+ return {static_cast<uint16_t>(truncated), true};
38
+ }
package/src/validation.h CHANGED
@@ -18,4 +18,13 @@ struct IpAddr {
18
18
 
19
19
  // Parse IP string, validate via inet_pton, store binary form.
20
20
  // Returns IpAddr with family=Invalid on failure.
21
- IpAddr parse_ip(const std::string& ip);
21
+ [[nodiscard]] IpAddr parse_ip(const std::string& ip);
22
+
23
+ struct PortVal {
24
+ uint16_t port;
25
+ bool valid;
26
+ };
27
+
28
+ // Parse port number. Returns {port, valid=true} on success.
29
+ // Valid range: 0-65535.
30
+ [[nodiscard]] PortVal parse_port(double value);