dodo  0.0.1
A C++ library to create containerized Linux services
socketparams.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the dodo library (https://github.com/jmspit/dodo).
3  * Copyright (c) 2019 Jan-Marten Spit.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, version 3.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /**
19  * @file socketparams.hpp
20  * Defines the dodo::network::SocketParams class.
21  */
22 
23 #ifndef network_socketparams_hpp
24 #define network_socketparams_hpp
25 
26 #include <netdb.h>
27 #include <sstream>
28 #include <string>
29 
30 namespace dodo::network {
31 
32  /**
33  * Socket parameters - the family (domain), socket type and protocol triplet.
34  */
35  class SocketParams {
36  public:
37 
38  /**
39  * Addres family type.
40  */
42  afLOCAL = AF_LOCAL,
43  afINET = AF_INET,
44  afINET6 = AF_INET6,
45  afIPX = AF_IPX,
46  afNETLINK = AF_NETLINK,
47  afX25 = AF_X25,
48  afAX25 = AF_AX25,
49  afATMPVC = AF_ATMPVC,
50  afAPPLETALK = AF_APPLETALK,
51  afPACKET = AF_PACKET,
52  afALG = AF_ALG,
53  afUNSPEC = AF_UNSPEC
54  };
55 
56  /**
57  * Socket Type type.
58  */
59  enum SocketType {
60  stSTREAM = SOCK_STREAM,
61  stDGRAM = SOCK_DGRAM,
62  stSEQPACKET = SOCK_SEQPACKET,
63  stRAW = SOCK_RAW,
64  stRDM = SOCK_RDM,
65  stPACKET = SOCK_PACKET
66  };
67 
68 
69  /**
70  * IANA Protocol number.
71  * https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml
72  */
74  pnHOPOPT = 0,
75  pnICMP = IPPROTO_ICMP,
76  pnICMPV6 = IPPROTO_ICMPV6,
77  pnIGMP = 2,
78  pnGGP = 3,
79  pnIPv4 = 4,
80  pnST = 5,
81  pnTCP = 6,
82  pnCBT = 7,
83  pnEGP = 8,
84  pnIGP = 9,
85  pnBBN_RCC_MON = 10,
86  pnNVP_II = 11,
87  pnPUP = 12,
88  pnARGUS = 13,
89  pnEMCON = 14,
90  pnXNET = 15,
91  pnCHAOS = 16,
92  pnUDP = 17,
93  pnMUX = 18,
94  pnDCN_MEAS = 19,
95  pnHMP = 20,
96  pnPRM = 21,
97  pnXNS_IDP = 22,
98  pnTRUNK_1 = 23,
99  pnTRUNK_2 = 24,
100  pnLEAF_1 = 25,
101  pnLEAF_2 = 26,
102  pnRDP = 27,
103  pnIRTP = 28,
104  pnISO_TP4 = 29,
105  pnNETBLT = 30,
106  pnMFE_NSP = 31,
107  pnMERIT_INP = 32,
108  pnDCCP = 33,
109  pn3PC = 34,
110  pnIDPR = 35,
111  pnXTP = 36,
112  pnDDP = 37,
113  pnIDPR_CMTP = 38
114  };
115 
116  /**
117  * Default constructor to AF_INET6, SOCK_STREAM, protocol pnHOPOPT
118  */
119  SocketParams() : family_(afINET6), sockettype_(stSTREAM), protocol_(pnHOPOPT) {};
120 
121  /**
122  * Constructor only sets the AddressFamily.
123  * @param family The AddressFamily.
124  */
125  SocketParams( AddressFamily family ) : family_(family), sockettype_(stSTREAM),protocol_(pnHOPOPT) {};
126 
127  /**
128  * Constructor sets the AddressFamily and SocketType.
129  * @param family The AddressFamily.
130  * @param sockettype The SocketType.
131  */
132  SocketParams( AddressFamily family, SocketType sockettype ) :
133  family_(family), sockettype_(sockettype),protocol_(pnHOPOPT) {};
134 
135  /**
136  * Constructor sets the AddressFamily, SocketType and ProtocolNumber.
137  * @param family The AddressFamily.
138  * @param sockettype The SocketType.
139  * @param protocol The ProtocolNumber.
140  */
141  SocketParams( AddressFamily family, SocketType sockettype, ProtocolNumber protocol ) :
142  family_(family), sockettype_(sockettype),protocol_(protocol) {};
143 
144  /**
145  * Get the AddressFamily.
146  * @return The AddressFamily.
147  */
149 
150  /**
151  * Get the SocketType.
152  * @return The SocketType.
153  */
154  SocketType getSocketType() const { return sockettype_; };
155 
156  /**
157  * Get the ProtocolNumber.
158  * @return The ProtocolNumber.
159  */
160  ProtocolNumber getProtocol() const { return protocol_; };
161 
162  /**
163  * Set the AddressFamily.
164  * @param family The AddressFamily.
165  */
166  void setAddressFamily( AddressFamily family ) { family_ = family; };
167 
168  /**
169  * Set the SocketType.
170  * @param sockettype The SocketType.
171  */
172  void setSocketType( SocketType sockettype ) { sockettype_ = sockettype; };
173 
174  /**
175  * Set the ProtocolNumber.
176  * @param protocol The ProtocolNumber.
177  */
178  void setProtocol( ProtocolNumber protocol ) { protocol_ = protocol; };
179 
180  /**
181  * Return the parameters as a string.
182  * @return The string.
183  */
184  std::string asString() const {
185  std::stringstream ss;
186  ss << familyString( family_ ) << " ";
187  ss << socketTypeString( sockettype_ ) << " ";
188  ss << protocolString( protocol_ );
189  return ss.str();
190  };
191 
192 
193  /**
194  * Return the AddressFamily name as a string.
195  * @param family The AddressFamily to convert.
196  * @return The string.
197  */
198  static std::string familyString( AddressFamily family ) {
199  std::stringstream ss;
200  switch ( family ) {
201  case AF_LOCAL : ss << "AF_LOCAL"; break;
202  case AF_INET : ss << "AF_INET"; break;
203  case AF_INET6 : ss << "AF_INET6"; break;
204  case AF_IPX : ss << "AF_IPX"; break;
205  case AF_NETLINK : ss << "AF_NETLINK"; break;
206  case AF_X25 : ss << "AF_X25"; break;
207  case AF_AX25 : ss << "AF_AX25"; break;
208  case AF_ATMPVC : ss << "AF_ATMPVC"; break;
209  case AF_APPLETALK : ss << "AF_APPLETALK"; break;
210  case AF_PACKET : ss << "AF_PACKET"; break;
211  case AF_ALG : ss << "AF_ALG"; break;
212  case AF_UNSPEC : ss << "AF_UNSPEC"; break;
213  default : ss << "unhandled protocol family"; break;
214  }
215  return ss.str();
216  };
217 
218  /**
219  * Return the SocketType name as a string.
220  * @param sockettype The SocketType to convert.
221  * @return The string.
222  */
223  static std::string socketTypeString( SocketType sockettype ) {
224  std::stringstream ss;
225  switch ( sockettype ) {
226  case SOCK_STREAM : ss << "SOCK_STREAM"; break;
227  case SOCK_DGRAM : ss << "SOCK_DGRAM"; break;
228  case SOCK_SEQPACKET : ss << "SOCK_SEQPACKET"; break;
229  case SOCK_RAW : ss << "SOCK_RAW"; break;
230  case SOCK_RDM : ss << "SOCK_RDM"; break;
231  case SOCK_PACKET : ss << "SOCK_PACKET"; break;
232  default : ss << "unhandled socket type"; break;
233  }
234  return ss.str();
235  };
236 
237 
238  /**
239  * Return the ProtocolNumber name as a string.
240  * @param protocol The ProtocolNumber to convert.
241  * @return The string.
242  */
243  static std::string protocolString( ProtocolNumber protocol ) {
244  std::stringstream ss;
245  switch ( protocol ) {
246  case pnHOPOPT : ss << "HOPOPT"; break;
247  case pnICMP : ss << "ICMP"; break;
248  case pnIGMP : ss << "IGMP"; break;
249  case pnGGP : ss << "GGP"; break;
250  case pnIPv4 : ss << "IPv4"; break;
251  case pnST : ss << "ST"; break;
252  case pnTCP : ss << "TCP"; break;
253  case pnCBT : ss << "CBT"; break;
254  case pnEGP : ss << "EGP"; break;
255  case pnIGP : ss << "IGP"; break;
256  case pnBBN_RCC_MON : ss << "BBN-RCC-MON"; break;
257  case pnNVP_II : ss << "NVP-II"; break;
258  case pnPUP : ss << "PUP"; break;
259  case pnARGUS : ss << "ARGUS"; break;
260  case pnEMCON : ss << "EMCON"; break;
261  case pnXNET : ss << "XNET"; break;
262  case pnCHAOS : ss << "CHAOS"; break;
263  case pnUDP : ss << "UDP"; break;
264  case pnMUX : ss << "MUX"; break;
265  case pnDCN_MEAS : ss << "DCN_MEAS"; break;
266  case pnHMP : ss << "HMP"; break;
267  default : ss << "unknown"; break;
268  }
269  return ss.str();
270  };
271 
272  private:
273  /**
274  * The address family.
275  */
277 
278  /**
279  * The socket type.
280  */
282 
283  /**
284  * The protocol.
285  */
287  };
288 
289 };
290 
291 #endif
dodo::network::SocketParams::getAddressFamily
AddressFamily getAddressFamily() const
Get the AddressFamily.
Definition: socketparams.hpp:148
dodo::network::SocketParams::setAddressFamily
void setAddressFamily(AddressFamily family)
Set the AddressFamily.
Definition: socketparams.hpp:166
dodo::network::SocketParams::protocolString
static std::string protocolString(ProtocolNumber protocol)
Return the ProtocolNumber name as a string.
Definition: socketparams.hpp:243
dodo::network::SocketParams::SocketParams
SocketParams()
Default constructor to AF_INET6, SOCK_STREAM, protocol pnHOPOPT.
Definition: socketparams.hpp:119
dodo::network::SocketParams::AddressFamily
AddressFamily
Addres family type.
Definition: socketparams.hpp:41
dodo::network::SocketParams::family_
AddressFamily family_
The address family.
Definition: socketparams.hpp:270
dodo::network::SocketParams::protocol_
ProtocolNumber protocol_
The protocol.
Definition: socketparams.hpp:286
dodo::network::SocketParams
Socket parameters - the family (domain), socket type and protocol triplet.
Definition: socketparams.hpp:35
dodo::network::SocketParams::familyString
static std::string familyString(AddressFamily family)
Return the AddressFamily name as a string.
Definition: socketparams.hpp:198
dodo::network::SocketParams::SocketParams
SocketParams(AddressFamily family, SocketType sockettype, ProtocolNumber protocol)
Constructor sets the AddressFamily, SocketType and ProtocolNumber.
Definition: socketparams.hpp:141
dodo::network::SocketParams::SocketParams
SocketParams(AddressFamily family, SocketType sockettype)
Constructor sets the AddressFamily and SocketType.
Definition: socketparams.hpp:132
dodo::network::SocketParams::asString
std::string asString() const
Return the parameters as a string.
Definition: socketparams.hpp:184
dodo::network::SocketParams::setSocketType
void setSocketType(SocketType sockettype)
Set the SocketType.
Definition: socketparams.hpp:172
dodo::network::SocketParams::SocketParams
SocketParams(AddressFamily family)
Constructor only sets the AddressFamily.
Definition: socketparams.hpp:125
dodo::network::SocketParams::setProtocol
void setProtocol(ProtocolNumber protocol)
Set the ProtocolNumber.
Definition: socketparams.hpp:178
dodo::network
Interface for network communication.
Definition: address.hpp:37
dodo::network::SocketParams::socketTypeString
static std::string socketTypeString(SocketType sockettype)
Return the SocketType name as a string.
Definition: socketparams.hpp:223
dodo::network::SocketParams::getSocketType
SocketType getSocketType() const
Get the SocketType.
Definition: socketparams.hpp:154
dodo::network::SocketParams::ProtocolNumber
ProtocolNumber
IANA Protocol number.
Definition: socketparams.hpp:73
dodo::network::SocketParams::SocketType
SocketType
Socket Type type.
Definition: socketparams.hpp:59
dodo::network::SocketParams::sockettype_
SocketType sockettype_
The socket type.
Definition: socketparams.hpp:281
dodo::network::SocketParams::getProtocol
ProtocolNumber getProtocol() const
Get the ProtocolNumber.
Definition: socketparams.hpp:160