Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
socket_addr.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Roc Streaming authors
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 //! @file roc_address/target_posix/roc_address/socket_addr.h
10 //! @brief Socket address.
11 
12 #ifndef ROC_ADDRESS_SOCKET_ADDR_H_
13 #define ROC_ADDRESS_SOCKET_ADDR_H_
14 
15 #include <netinet/in.h>
16 #include <sys/socket.h>
17 
19 #include "roc_core/attributes.h"
20 #include "roc_core/stddefs.h"
21 
22 namespace roc {
23 namespace address {
24 
25 //! Socket address.
26 class SocketAddr {
27 public:
28  //! Construct empty address.
30 
31  //! Clear address.
32  void clear();
33 
34  //! Check whether host and port are set.
35  bool has_host_port() const;
36 
37  //! Set host address.
38  ROC_ATTR_NODISCARD bool set_host_port(AddrFamily type, const char* host, int port);
39 
40  //! Set host address, auto-detect family.
41  ROC_ATTR_NODISCARD bool set_host_port_auto(const char* host, int port);
42 
43  //! Set address from sockaddr struct.
44  ROC_ATTR_NODISCARD bool set_host_port_saddr(const sockaddr* sa);
45 
46  //! Get IP version (IPv4 or IPv6).
47  AddrFamily family() const;
48 
49  //! Check whether this is multicast address.
50  bool multicast() const;
51 
52  //! Get host IP address.
53  ROC_ATTR_NODISCARD bool get_host(char* buf, size_t bufsz) const;
54 
55  //! Get address port.
56  int port() const;
57 
58  //! Get sockaddr struct.
59  sockaddr* saddr();
60 
61  //! Get sockaddr struct.
62  const sockaddr* saddr() const;
63 
64  //! Get sockaddr struct length.
65  socklen_t slen() const;
66 
67  //! Get maximum allowed sockaddr struct length.
68  socklen_t max_slen() const;
69 
70  //! Convert to bool.
71  operator const struct unspecified_bool *() const;
72 
73  //! Compare addresses.
74  bool operator==(const SocketAddr& other) const;
75 
76  //! Compare addresses.
77  bool operator!=(const SocketAddr& other) const;
78 
79  enum {
80  // An estimate maximum length of a string representation of an address.
81  MaxStrLen = 196
82  };
83 
84 private:
85  static socklen_t saddr_size_(sa_family_t family);
86 
87  sa_family_t saddr_family_() const;
88 
89  bool set_host_port_ipv4_(const char* ip, int port);
90  bool set_host_port_ipv6_(const char* ip, int port);
91 
92  union {
93  sockaddr_in addr4;
94  sockaddr_in6 addr6;
95  } saddr_;
96 };
97 
98 } // namespace address
99 } // namespace roc
100 
101 #endif // ROC_ADDRESS_SOCKET_ADDR_H_
Address family.
Compiler attributes.
#define ROC_ATTR_NODISCARD
Emit warning if function result is not checked.
Definition: attributes.h:31
Socket address.
Definition: socket_addr.h:26
bool operator==(const SocketAddr &other) const
Compare addresses.
AddrFamily family() const
Get IP version (IPv4 or IPv6).
int port() const
Get address port.
bool operator!=(const SocketAddr &other) const
Compare addresses.
ROC_ATTR_NODISCARD bool set_host_port_auto(const char *host, int port)
Set host address, auto-detect family.
bool has_host_port() const
Check whether host and port are set.
ROC_ATTR_NODISCARD bool set_host_port(AddrFamily type, const char *host, int port)
Set host address.
SocketAddr()
Construct empty address.
socklen_t max_slen() const
Get maximum allowed sockaddr struct length.
socklen_t slen() const
Get sockaddr struct length.
ROC_ATTR_NODISCARD bool set_host_port_saddr(const sockaddr *sa)
Set address from sockaddr struct.
void clear()
Clear address.
ROC_ATTR_NODISCARD bool get_host(char *buf, size_t bufsz) const
Get host IP address.
const sockaddr * saddr() const
Get sockaddr struct.
bool multicast() const
Check whether this is multicast address.
sockaddr * saddr()
Get sockaddr struct.
AddrFamily
Address family.
Definition: addr_family.h:19
Root namespace.
Commonly used types and functions.