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  //! Compare addresses.
71  bool operator==(const SocketAddr& other) const;
72 
73  //! Compare addresses.
74  bool operator!=(const SocketAddr& other) const;
75 
76  enum {
77  // An estimate maximum length of a string representation of an address.
78  MaxStrLen = 196
79  };
80 
81 private:
82  static socklen_t saddr_size_(sa_family_t family);
83 
84  sa_family_t saddr_family_() const;
85 
86  bool set_host_port_ipv4_(const char* ip, int port);
87  bool set_host_port_ipv6_(const char* ip, int port);
88 
89  union {
90  sockaddr_in addr4;
91  sockaddr_in6 addr6;
92  } saddr_;
93 };
94 
95 } // namespace address
96 } // namespace roc
97 
98 #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.