OpenVPN 3 Core Library
Loading...
Searching...
No Matches
layer.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12// Class that encapsulates the definition of an OSI layer.
13
14#ifndef OPENVPN_TUN_LAYER_H
15#define OPENVPN_TUN_LAYER_H
16
18
19namespace openvpn {
20class Layer
21{
22 public:
29
31 : type_(NONE)
32 {
33 }
34 explicit Layer(const Type t)
35 : type_(t)
36 {
37 }
39 {
40 return type_;
41 }
42
43 bool defined() const
44 {
45 return type_ != NONE;
46 }
47
48 const char *dev_type() const
49 {
50 switch (type_)
51 {
52 case OSI_LAYER_2:
53 return "tap";
54 case OSI_LAYER_3:
55 return "tun";
56 default:
57 return "null";
58 }
59 }
60
61 const char *str() const
62 {
63 switch (type_)
64 {
65 case NONE:
66 return "UNDEF_LAYER";
67 case OSI_LAYER_2:
68 return "OSI_LAYER_2";
69 case OSI_LAYER_3:
70 return "OSI_LAYER_3";
71 default:
72 throw Exception("Layer: unrecognized layer type");
73 }
74 }
75
76 int value() const
77 {
78 switch (type_)
79 {
80 case NONE:
81 return 0;
82 case OSI_LAYER_2:
83 return 2;
84 case OSI_LAYER_3:
85 return 3;
86 default:
87 throw Exception("Layer: unrecognized layer type");
88 }
89 }
90
91 static Layer from_str(const std::string &str)
92 {
93 if (str == "OSI_LAYER_3")
94 return Layer(OSI_LAYER_3);
95 else if (str == "OSI_LAYER_2")
96 return Layer(OSI_LAYER_2);
97 else if (str == "UNDEF_LAYER")
98 return Layer(NONE);
99 else
100 throw Exception("Layer: unrecognized layer string");
101 }
102
103 static Layer from_value(const int value)
104 {
105 if (value == 3)
106 return Layer(OSI_LAYER_3);
107 else if (value == 2)
108 return Layer(OSI_LAYER_2);
109 else if (value == 0)
110 return Layer(NONE);
111 else
112 throw Exception("Layer: unrecognized layer value");
113 }
114
115 bool operator==(const Layer &other) const
116 {
117 return type_ == other.type_;
118 }
119
120 bool operator!=(const Layer &other) const
121 {
122 return type_ != other.type_;
123 }
124
125 private:
127};
128} // namespace openvpn
129
130#endif // OPENVPN_TUN_LAYER_H
static Layer from_value(const int value)
Definition layer.hpp:103
bool operator==(const Layer &other) const
Definition layer.hpp:115
int value() const
Definition layer.hpp:76
const char * dev_type() const
Definition layer.hpp:48
Type operator()() const
Definition layer.hpp:38
const char * str() const
Definition layer.hpp:61
static Layer from_str(const std::string &str)
Definition layer.hpp:91
Layer(const Type t)
Definition layer.hpp:34
bool operator!=(const Layer &other) const
Definition layer.hpp:120
bool defined() const
Definition layer.hpp:43