Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
address.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Roc 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_packet/target_posix/roc_packet/address.h
10 //! @brief Network address.
11 
12 #ifndef ROC_PACKET_ADDRESS_H_
13 #define ROC_PACKET_ADDRESS_H_
14 
15 #include <netinet/in.h>
16 #include <sys/socket.h>
17 
18 #include "roc_core/stddefs.h"
19 
20 namespace roc {
21 namespace packet {
22 
23 //! Network address.
24 class Address {
25 public:
26  //! Construct invalid address.
27  Address();
28 
29  //! Check if the address was properly initialized.
30  bool valid() const;
31 
32  //! Set address from sockaddr struct.
33  bool set_saddr(const sockaddr* sa);
34 
35  //! Set IPv4 address.
36  bool set_ipv4(const char* ip, int port);
37 
38  //! Set IPv6 address.
39  bool set_ipv6(const char* ip, int port);
40 
41  //! Get sockaddr struct.
42  sockaddr* saddr();
43 
44  //! Get sockaddr struct.
45  const sockaddr* saddr() const;
46 
47  //! Get sockaddr struct length.
48  socklen_t slen() const;
49 
50  //! Get IP version (4 or 6).
51  int version() const;
52 
53  //! Get address port.
54  int port() const;
55 
56  //! Check whether this is multicast address.
57  bool multicast() const;
58 
59  //! Get IP address.
60  bool get_ip(char* buf, size_t bufsz) const;
61 
62  //! Compare addresses.
63  bool operator==(const Address& other) const;
64 
65  //! Compare addresses.
66  bool operator!=(const Address& other) const;
67 
68 private:
69  static socklen_t sizeof_(sa_family_t family);
70 
71  sa_family_t family_() const;
72 
73  union {
74  sockaddr_in addr4;
75  sockaddr_in6 addr6;
76  } sa_;
77 };
78 
79 } // namespace packet
80 } // namespace roc
81 
82 #endif // ROC_PACKET_ADDRESS_H_
bool operator==(const Address &other) const
Compare addresses.
bool set_ipv4(const char *ip, int port)
Set IPv4 address.
Root namespace.
int version() const
Get IP version (4 or 6).
int port() const
Get address port.
bool set_saddr(const sockaddr *sa)
Set address from sockaddr struct.
Network address.
Definition: address.h:24
Commonly used types and functions.
Address()
Construct invalid address.
sockaddr * saddr()
Get sockaddr struct.
bool multicast() const
Check whether this is multicast address.
bool operator!=(const Address &other) const
Compare addresses.
bool set_ipv6(const char *ip, int port)
Set IPv6 address.
bool get_ip(char *buf, size_t bufsz) const
Get IP address.
socklen_t slen() const
Get sockaddr struct length.
bool valid() const
Check if the address was properly initialized.