Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Loading...
Searching...
No Matches
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/stddefs.h"
20
21namespace roc {
22namespace address {
23
24//! Socket address.
26public:
27 //! Construct empty address.
29
30 //! Clear address.
31 void clear();
32
33 //! Check whether host and port are set.
34 bool has_host_port() const;
35
36 //! Set host address.
37 bool set_host_port(AddrFamily type, const char* host, int port);
38
39 //! Set host address, auto-detect family.
40 bool set_host_port_auto(const char* host, int port);
41
42 //! Set address from sockaddr struct.
43 bool set_host_port_saddr(const sockaddr* sa);
44
45 //! Get IP version (IPv4 or IPv6).
47
48 //! Check whether this is multicast address.
49 bool multicast() const;
50
51 //! Get host IP address.
52 bool get_host(char* buf, size_t bufsz) const;
53
54 //! Get address port.
55 int port() const;
56
57 //! Get sockaddr struct.
58 sockaddr* saddr();
59
60 //! Get sockaddr struct.
61 const sockaddr* saddr() const;
62
63 //! Get sockaddr struct length.
64 socklen_t slen() const;
65
66 //! Get maximum allowed sockaddr struct length.
67 socklen_t max_slen() const;
68
69 //! Compare addresses.
70 bool operator==(const SocketAddr& other) const;
71
72 //! Compare addresses.
73 bool operator!=(const SocketAddr& other) const;
74
75 enum {
76 // An estimate maximum length of a string representation of an address.
77 MaxStrLen = 196
78 };
79
80private:
81 static socklen_t saddr_size_(sa_family_t family);
82
83 sa_family_t saddr_family_() const;
84
85 bool set_host_port_ipv4_(const char* ip, int port);
86 bool set_host_port_ipv6_(const char* ip, int port);
87
88 union {
89 sockaddr_in addr4;
90 sockaddr_in6 addr6;
91 } saddr_;
92};
93
94} // namespace address
95} // namespace roc
96
97#endif // ROC_ADDRESS_SOCKET_ADDR_H_
Address family.
bool operator==(const SocketAddr &other) const
Compare addresses.
sockaddr * saddr()
Get sockaddr struct.
AddrFamily family() const
Get IP version (IPv4 or IPv6).
int port() const
Get address port.
bool operator!=(const SocketAddr &other) const
Compare addresses.
bool has_host_port() const
Check whether host and port are set.
SocketAddr()
Construct empty address.
bool set_host_port(AddrFamily type, const char *host, int port)
Set host address.
socklen_t max_slen() const
Get maximum allowed sockaddr struct length.
socklen_t slen() const
Get sockaddr struct length.
void clear()
Clear address.
const sockaddr * saddr() const
Get sockaddr struct.
bool set_host_port_auto(const char *host, int port)
Set host address, auto-detect family.
bool get_host(char *buf, size_t bufsz) const
Get host IP address.
bool multicast() const
Check whether this is multicast address.
bool set_host_port_saddr(const sockaddr *sa)
Set address from sockaddr struct.
AddrFamily
Address family.
Definition addr_family.h:19
Root namespace.
Commonly used types and functions.