scclib
Stable Cloud Computing C++ Library
inet.h
Go to the documentation of this file.
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #ifndef _SCC_NET_INET_H
32 #define _SCC_NET_INET_H
33 
34 #include <string>
35 #include <cstdint>
36 #include <memory>
37 #include <net/socket.h>
38 
39 // forward declarations
40 struct sockaddr;
41 struct sockaddr_in6;
42 
43 namespace scc::net {
44 
74 {
75  prot_mask= 0xf,
76  ipv4= 0x1,
77  ipv6= 0x2,
78 
79  type_mask= 0xf0,
80  any= 0x10,
81  loopback= 0x20,
82  unicast= 0x40,
83  multicast= 0x80,
84 
85  scope_mask= 0xff00,
86  if_local= 0x0100,
87  link_local= 0x0200,
88  realm_local= 0x0400,
89  admin_local= 0x0800,
90  site_local= 0x1000,
91  org_local= 0x2000,
92  global= 0x4000,
93 
94  mcast_flags_mask= 0xf0000,
95  mcast_rendezvous = 0x10000,
96  mcast_prefix= 0x20000,
97  mcast_dynamic= 0x40000,
98 
99  mcast_reserved_mask= 0xff00000,
100  mcast_all_nodes= 0x0100000,
101  mcast_all_routers= 0x0200000,
102 
103  unicast_special_mask= 0xf0000000,
104  unique_local_address = 0x10000000,
105 };
106 
119 class InetAddr : public SockaddrBase
120 {
121  sockaddr_in6* m_addr;
122 
123  void init();
124 
125 public:
129  InetAddr();
133  InetAddr(unsigned);
137  InetAddr(const std::string&, unsigned);
141  InetAddr(const sockaddr*);
145  InetAddr(const InetAddr&);
149  InetAddr& operator=(const InetAddr&);
153  InetAddr(InetAddr&&);
161  virtual ~InetAddr();
162 
166  virtual operator const sockaddr*() const;
170  virtual operator sockaddr*();
174  virtual unsigned len() const;
178  virtual std::string str() const;
182  virtual std::string host() const;
183 
187  operator const sockaddr_in6*() const { return m_addr; }
188 
192  void any_host();
196  void local_host();
204  void host(const std::string&);
208  unsigned port() const;
212  void port(unsigned);
225  void scope_id(uint32_t);
229  uint32_t scope_id() const;
230 
236  int flags() const;
240  bool test_flags(int f) const
241  {
242  return (flags() & f) == f;
243  }
244 };
245 
250 class InetTcpSock : public TcpSocket
251 {
252 protected:
253  explicit InetTcpSock(int fd) : TcpSocket(fd) {}
254 
255 public:
259  InetTcpSock();
260 
264  virtual void reset();
268  InetAddr get_addr();
269 
278  std::shared_ptr<InetTcpSock> accept_shared();
287  std::shared_ptr<InetTcpSock> accept_shared(InetAddr&);
288 };
289 
294 class InetUdpSock : public UdpSocket
295 {
296 public:
300  InetUdpSock();
301 
305  virtual void reset();
306 
310  InetAddr get_addr();
311 
321  void mcast_join_group(const InetAddr&, unsigned = 0);
325  void mcast_leave_group(const InetAddr&, unsigned = 0);
333  void mcast_interface(unsigned = 0);
341  void mcast_loopback(bool = true);
349  void mcast_hops(unsigned = 1);
350 };
351 
355 } // namespace
356 
359 std::ostream& operator<<(std::ostream&, const scc::net::InetAddr&);
360 
361 #endif
Ipv6 internet address.
Definition: inet.h:120
bool test_flags(int f) const
Test if flags are set in this address.
Definition: inet.h:240
virtual unsigned len() const
Socket address length in bytes.
Definition: inet.cc:139
unsigned port() const
Get the port.
Definition: inet.cc:159
InetAddr & operator=(const InetAddr &)
ipv6 internet address, copy assigned.
Definition: inet.cc:104
uint32_t scope_id() const
Get the scope id of the address.
Definition: inet.cc:170
void any_host()
Set to "any" host address ::
Definition: inet.cc:144
void local_host()
Set to local (loopback) address ::1.
Definition: inet.cc:149
virtual std::string host() const
Get host.
Definition: inet.cc:188
virtual ~InetAddr()
ipv6 internet address destructor.
Definition: inet.cc:65
virtual std::string str() const
Readable address string.
Definition: inet.cc:325
InetAddr()
ipv6 internet address, initialized with "any" address
Definition: inet.cc:60
int flags() const
Return the address flags.
Definition: inet.cc:215
Internet transmission control protocol (tcp) socket.
Definition: inet.h:251
virtual void reset()
Close the connection and reset the socket.
Definition: inet.cc:418
InetTcpSock accept()
Accept a connection from an anonymous peer.
Definition: inet.cc:430
InetTcpSock()
Create an IPv6 stream socket.
Definition: inet.cc:415
InetAddr get_addr()
Get the socket address.
Definition: inet.cc:423
Internet user datagram protocol (udp) socket.
Definition: inet.h:295
virtual void reset()
Reset the socket.
Definition: inet.cc:484
InetUdpSock()
Create an IPv6 datagram socket.
Definition: inet.cc:482
void mcast_leave_group(const InetAddr &, unsigned=0)
Leave a multicast group.
Definition: inet.cc:513
InetAddr get_addr()
Get the socket address.
Definition: inet.cc:489
void mcast_interface(unsigned=0)
Set the default interface for outgoing multicast messages.
Definition: inet.cc:530
void mcast_loopback(bool=true)
Enable or disable multicast loopback.
Definition: inet.cc:543
void mcast_hops(unsigned=1)
Hop limit for outgoing multicast messages.
Definition: inet.cc:556
void mcast_join_group(const InetAddr &, unsigned=0)
Join a multicast group.
Definition: inet.cc:496
Socket address base class.
Definition: socket.h:60
int fd() const
Return the underlying socket handle.
Definition: socket.h:112
Tcp socket base class.
Definition: socket.h:291
Udp socket base class.
Definition: socket.h:349
InetAddrFlag
Internet address flags.
Definition: inet.h:74
@ unicast
Unicast address.
Definition: inet.h:82
@ link_local
Traffic is restricted to the local link.
Definition: inet.h:87
@ realm_local
Traffic is restricted to the local realm.
Definition: inet.h:88
@ scope_mask
Scope for multicast addresses mask.
Definition: inet.h:85
@ mcast_all_nodes
Reaches all nodes in the scope, e.g. ff0X::1.
Definition: inet.h:100
@ mcast_rendezvous
Address has a rendezvous point embedded.
Definition: inet.h:95
@ mcast_flags_mask
Multicast flags mask.
Definition: inet.h:94
@ admin_local
Traffic is restricted to the local admin.
Definition: inet.h:89
@ unicast_special_mask
Some special unicast addresses.
Definition: inet.h:103
@ global
Global traffic is allowed.
Definition: inet.h:92
@ org_local
Traffic is restricted to the local organization.
Definition: inet.h:91
@ mcast_all_routers
Reaches all routers in the scope, e.g. ff0X::2.
Definition: inet.h:101
@ any
Any address.
Definition: inet.h:80
@ loopback
Loopback address.
Definition: inet.h:81
@ prot_mask
Protocol mask.
Definition: inet.h:75
@ if_local
Traffic is restricted to the local interface.
Definition: inet.h:86
@ type_mask
Address mask.
Definition: inet.h:79
@ ipv4
IPv4.
Definition: inet.h:76
@ mcast_reserved_mask
Some reserved multicast addresses.
Definition: inet.h:99
@ mcast_dynamic
Dynamic (temporary) address, otherwise permanent (assigned).
Definition: inet.h:97
@ ipv6
IPv6.
Definition: inet.h:77
@ site_local
Traffic is restricted to the local site.
Definition: inet.h:90
@ multicast
Multicast address.
Definition: inet.h:83
@ unique_local_address
Address which can be used freely within a site: e.g. fd00::/8.
Definition: inet.h:104
@ mcast_prefix
Prefix-based address.
Definition: inet.h:96
std::ostream & operator<<(std::ostream &, const scc::net::InetAddr &)
Print the socket address details to an output stream.
Definition: inet.cc:320
Low-level tcp and udp sockets.