Newer
Older
#ifndef _SOCKADDR_HPP
#define _SOCKADDR_HPP
#include <string>
#include <netinet/in.h>
#include "ipaddr.hpp"
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* @brief SockAddr represents the combination of an ip address and a port number.
*
* @note This implementation of SockAddr does not contain the transfer protocol.
*/
class SockAddr
{
public:
/**
* @brief RawSockAddr can be interpreted as either the generic sockaddr,
* the Ipv4 sockaddr_in or the Ipv6 sockaddr_in6.
*
* This type is used to simplify the type punning required for working
* with the network stack.
*/
union RawSockAddr {
/** @brief Interpret the memory as generic sockaddr */
sockaddr generic;
/** @brief Interpret the memory as Ipv4 sockaddr_in */
sockaddr_in v4;
/** @brief Interpret the memory as Ipv6 sockaddr_in6 */
sockaddr_in6 v6;
};
private:
/**
* @brief The ip address associated with the SockAddr.
*/
IpAddr address;
/**
* @brief The port number associated with the SockAddr.
*/
uint16_t port;
/**
* @brief The raw sockaddr, used for interfacing with the network stack.
*
* This must represent the same state as the SockAddr IpAddr & port at
* all times.
*/
RawSockAddr raw_sockaddr;
/**
* @brief Parse the given IpAddr + port and set the raw sockaddr accordingly.
*
* @param address The ip address that will be used in the raw socket.
* @param port The port number taht will be used in the raw socket.
*/
void setRawSockaddr(IpAddr address, uint16_t port);
/**
* @brief Create the SockAddr by parsing a given raw sockaddr with
* specified type.
*
* This extracts the IpAddr with the specified type, as well as the prot
* from the sockaddr.
*
* @param raw_sockaddr Pointer to the raw sockaddr that should be parsed.
* @param type The ip type that raw_sockaddr contains. Must be Ipv4 or Ipv6.
*/
SockAddr(sockaddr *raw_sockaddr, IpAddr::Type type);
public:
/**
* @brief The default constructor creates a SockAddr with the Ipv4 address
* 0.0.0.0 and the port 0.
*/
SockAddr();
/**
* @brief Construct a SockAddr from IpAddr and port.
*
* @param address The ip address.
* @param port The port.
*/
SockAddr(IpAddr address, uint16_t port);
/**
* @brief Construct SockAddr from a string and a port. The string will be
* parsed as in IpAddr automatically.
*
* If the address cannot be parsed as a valid Ipv4 or Ipv6 address, an
* exception is thrown.
*
* @param address The ip address in string representation. This can be Ipv4
* or Ipv6.
* @param port The port.
*/
SockAddr(const std::string & address, uint16_t port);
/**
* @brief Construct SockAddr from a string containing both the address and
* port. The string must be in the form "address:port". To specify an Ipv6
* address, it has to be placed inside brackets "[ipv6addr]:port".
*
* If any part of the string fails to be parsed, an exception is thrown.
*
* Exampe for Ipv4: "127.0.0.1:8080". Example for Ipv6: "[::1]:8080".
*
* @param address_port The ip address and port combination as string.
*/
SockAddr(const std::string & address_port);
/**
* @brief Get the ip address.
*/
const IpAddr & getIpAddress() const;
/**
* @brief Get the ip address in string form.
*/
const std::string & getIpAddressString() const;
/**
* @brief Get the port number.
*/
const uint16_t getPort() const;
friend class TcpStream;
friend class TcpListener;
friend class UdpSocket;
};