-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsocket_address.h
More file actions
55 lines (41 loc) · 1.07 KB
/
Copy pathsocket_address.h
File metadata and controls
55 lines (41 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#ifndef SOCKET_ADDRESS_H
#define SOCKET_ADDRESS_H
#include <cstring>
#include <sys/socket.h>
class socket_address {
public:
virtual sockaddr & cast() = 0;
virtual const sockaddr & cast() const = 0;
virtual socklen_t length() const = 0;
int protocol() const;
int family() const;
bool is_inet() const;
bool is_inet6() const;
bool is_local() const;
};
template <int AF, typename SA>
class basic_socket_address : public socket_address {
public:
basic_socket_address() {
memset(&sa, 0, sizeof sa);
cast().sa_family = AF;
}
sockaddr & cast() {
return reinterpret_cast<sockaddr &>(sa);
}
const sockaddr & cast() const {
return reinterpret_cast<const sockaddr &>(sa);
}
socklen_t length() const { return sizeof sa; }
protected:
SA sa;
static const int af = AF;
};
class any_address : public basic_socket_address<AF_UNSPEC,sockaddr_storage> {
public:
any_address();
any_address(const socket_address &);
any_address(const struct sockaddr *, socklen_t);
any_address & operator=(const socket_address &);
};
#endif//SOCKET_ADDRESS_H