aboutsummaryrefslogtreecommitdiff
path: root/include/lldb/Host/SocketAddress.h
blob: 4dc62102103ad7ccf3a11081aa95a172705ef5a3 (plain) (blame)
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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//===-- SocketAddress.h -----------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef liblldb_SocketAddress_h_
#define liblldb_SocketAddress_h_

// C Includes
#include <stdint.h>

#ifdef _WIN32
#include "lldb/Host/windows/windows.h"
#include <winsock2.h>
#include <WS2tcpip.h>
typedef ADDRESS_FAMILY sa_family_t;
#else
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#endif

#if defined(__FreeBSD__)
#include <sys/types.h>
#endif

// C++ Includes
// Other libraries and framework includes
// Project includes

namespace lldb_private {

class SocketAddress
{
public:
    //------------------------------------------------------------------
    // Constructors and Destructors
    //------------------------------------------------------------------
    SocketAddress ();
    SocketAddress (const struct sockaddr &s);
    SocketAddress (const struct sockaddr_in &s);
    SocketAddress (const struct sockaddr_in6 &s);
    SocketAddress (const struct sockaddr_storage &s);
    SocketAddress (const SocketAddress& rhs);
    ~SocketAddress ();

    //------------------------------------------------------------------
    // Operators
    //------------------------------------------------------------------
    const SocketAddress&
    operator=(const SocketAddress& rhs);

    const SocketAddress&
    operator=(const struct addrinfo *addr_info);

    const SocketAddress&
    operator=(const struct sockaddr &s);

    const SocketAddress&
    operator=(const struct sockaddr_in &s);

    const SocketAddress&
    operator=(const struct sockaddr_in6 &s);

    const SocketAddress&
    operator=(const struct sockaddr_storage &s);
    
    //------------------------------------------------------------------
    // Clear the contents of this socket address
    //------------------------------------------------------------------
    void
    Clear ();

    //------------------------------------------------------------------
    // Get the length for the current socket address family
    //------------------------------------------------------------------
    socklen_t
    GetLength () const;

    //------------------------------------------------------------------
    // Get the mex length for the the largest socket address supported.
    //------------------------------------------------------------------
    static socklen_t
    GetMaxLength ();

    //------------------------------------------------------------------
    // Get the socket address family 
    //------------------------------------------------------------------
    sa_family_t
    GetFamily () const;

    //------------------------------------------------------------------
    // Set the socket address family 
    //------------------------------------------------------------------
    void
    SetFamily (sa_family_t family);

    //------------------------------------------------------------------
    // Get the port if the socket address for the family has a port
    //------------------------------------------------------------------
    uint16_t
    GetPort () const;

    //------------------------------------------------------------------
    // Set the port if the socket address for the family has a port. 
    // The family must be set correctly prior to calling this function.
    //------------------------------------------------------------------
    bool
    SetPort (uint16_t port);

    //------------------------------------------------------------------
    // Set the socket address according to the first match from a call
    // to getaddrinfo() (or equivalent functions for systems that don't
    // have getaddrinfo(). If "addr_info_ptr" is not NULL, it will get
    // filled in with the match that was used to populate this socket
    // address.
    //------------------------------------------------------------------
    bool
    getaddrinfo (const char *host,          // Hostname ("foo.bar.com" or "foo" or IP address string ("123.234.12.1" or "2001:0db8:85a3:0000:0000:8a2e:0370:7334")
                 const char *service,       // Protocol name ("tcp", "http", etc) or a raw port number string ("81")
                 int ai_family = PF_UNSPEC,
                 int ai_socktype = 0,
                 int ai_protocol = 0,
                 int ai_flags = 0);

    //------------------------------------------------------------------
    // Quick way to set the SocketAddress to localhost given the family.
    // Returns true if successful, false if "family" doesn't support 
    // localhost or if "family" is not supported by this class.
    //------------------------------------------------------------------
    bool
    SetToLocalhost (sa_family_t family, 
                    uint16_t port);

    bool
    SetToAnyAddress (sa_family_t family,
                     uint16_t port);

    //------------------------------------------------------------------
    // Returns true if there is a valid socket address in this object.
    //------------------------------------------------------------------
    bool
    IsValid () const;

    //------------------------------------------------------------------
    // Direct access to all of the sockaddr structures
    //------------------------------------------------------------------
    struct sockaddr &
    sockaddr ()
    {
        return m_socket_addr.sa;
    }

    const struct sockaddr &
    sockaddr () const
    {
        return m_socket_addr.sa;
    }
    
    struct sockaddr_in &
    sockaddr_in ()
    {
        return m_socket_addr.sa_ipv4;
    }
    
    const struct sockaddr_in &
    sockaddr_in () const
    {
        return m_socket_addr.sa_ipv4;
    }
    
    struct sockaddr_in6 &
    sockaddr_in6 ()
    {
        return m_socket_addr.sa_ipv6;
    }
    
    const struct sockaddr_in6 &
    sockaddr_in6 () const
    {
        return m_socket_addr.sa_ipv6;
    }
    
    struct sockaddr_storage &
    sockaddr_storage ()
    {
        return m_socket_addr.sa_storage;
    }

    
    const struct sockaddr_storage &
    sockaddr_storage () const
    {
        return m_socket_addr.sa_storage;
    }
    
    
    //------------------------------------------------------------------
    // Conversion operators to allow getting the contents of this class
    // as a pointer to the appropriate structure. This allows an instance
    // of this class to be used in calls that take one of the sockaddr
    // structure variants without having to manally use the correct
    // accessor function.
    //------------------------------------------------------------------
    
    operator struct sockaddr * ()
    {
        return &m_socket_addr.sa;
    }
    
    operator const struct sockaddr * () const
    {
        return &m_socket_addr.sa;
    }

    operator struct sockaddr_in * ()
    {
        return &m_socket_addr.sa_ipv4;
    }
    
    operator const struct sockaddr_in * () const
    {
        return &m_socket_addr.sa_ipv4;
    }

    operator struct sockaddr_in6 * ()
    {
        return &m_socket_addr.sa_ipv6;
    }
    
    operator const struct sockaddr_in6 * () const
    {
        return &m_socket_addr.sa_ipv6;
    }

    operator const struct sockaddr_storage * () const
    {
        return &m_socket_addr.sa_storage;
    }

    operator struct sockaddr_storage * ()
    {
        return &m_socket_addr.sa_storage;
    }

    
protected:
    typedef union sockaddr_tag
    {
        struct sockaddr         sa;
        struct sockaddr_in      sa_ipv4;
        struct sockaddr_in6     sa_ipv6;
        struct sockaddr_storage sa_storage;
    } sockaddr_t;

    //------------------------------------------------------------------
    // Classes that inherit from SocketAddress can see and modify these
    //------------------------------------------------------------------
    sockaddr_t m_socket_addr;
};


} // namespace lldb_private


#endif  // liblldb_SocketAddress_h_