Socket.cpp revision 280031
1//===-- Socket.cpp ----------------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Host/Socket.h"
11
12#include "lldb/Core/Log.h"
13#include "lldb/Core/RegularExpression.h"
14#include "lldb/Host/Config.h"
15#include "lldb/Host/FileSystem.h"
16#include "lldb/Host/Host.h"
17#include "lldb/Host/SocketAddress.h"
18#include "lldb/Host/TimeValue.h"
19#include "lldb/Interpreter/Args.h"
20
21#ifdef __ANDROID_NDK__
22#include <linux/tcp.h>
23#include <bits/error_constants.h>
24#include <asm-generic/errno-base.h>
25#include <errno.h>
26#include <arpa/inet.h>
27#endif
28
29#ifndef LLDB_DISABLE_POSIX
30#include <arpa/inet.h>
31#include <netdb.h>
32#include <netinet/in.h>
33#include <netinet/tcp.h>
34#include <sys/socket.h>
35#include <sys/un.h>
36#endif
37
38using namespace lldb;
39using namespace lldb_private;
40
41#if defined(_WIN32)
42typedef const char * set_socket_option_arg_type;
43typedef char * get_socket_option_arg_type;
44const NativeSocket Socket::kInvalidSocketValue = INVALID_SOCKET;
45#else // #if defined(_WIN32)
46typedef const void * set_socket_option_arg_type;
47typedef void * get_socket_option_arg_type;
48const NativeSocket Socket::kInvalidSocketValue = -1;
49#endif // #if defined(_WIN32)
50
51#ifdef __ANDROID__
52// Android does not have SUN_LEN
53#ifndef SUN_LEN
54#define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path) + strlen((ptr)->sun_path))
55#endif
56#endif // #ifdef __ANDROID__
57
58namespace {
59
60NativeSocket CreateSocket(const int domain, const int type, const int protocol, bool child_processes_inherit)
61{
62    auto socketType = type;
63#ifdef SOCK_CLOEXEC
64    if (!child_processes_inherit) {
65        socketType |= SOCK_CLOEXEC;
66    }
67#endif
68    return ::socket (domain, socketType, protocol);
69}
70
71NativeSocket Accept(NativeSocket sockfd, struct sockaddr *addr, socklen_t *addrlen, bool child_processes_inherit)
72{
73#ifdef SOCK_CLOEXEC
74    int flags = 0;
75    if (!child_processes_inherit) {
76        flags |= SOCK_CLOEXEC;
77    }
78    return ::accept4 (sockfd, addr, addrlen, flags);
79#else
80    return ::accept (sockfd, addr, addrlen);
81#endif
82}
83}
84
85Socket::Socket(NativeSocket socket, SocketProtocol protocol, bool should_close)
86    : IOObject(eFDTypeSocket, should_close)
87    , m_protocol(protocol)
88    , m_socket(socket)
89{
90
91}
92
93Socket::~Socket()
94{
95    Close();
96}
97
98Error Socket::TcpConnect(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&socket)
99{
100    // Store the result in a unique_ptr in case we error out, the memory will get correctly freed.
101    std::unique_ptr<Socket> final_socket;
102    NativeSocket sock = kInvalidSocketValue;
103    Error error;
104
105    Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_HOST));
106    if (log)
107        log->Printf ("Socket::TcpConnect (host/port = %s)", host_and_port.data());
108
109    std::string host_str;
110    std::string port_str;
111    int32_t port = INT32_MIN;
112    if (!DecodeHostAndPort (host_and_port, host_str, port_str, port, &error))
113        return error;
114
115    // Create the socket
116    sock = CreateSocket (AF_INET, SOCK_STREAM, IPPROTO_TCP, child_processes_inherit);
117    if (sock == kInvalidSocketValue)
118    {
119        // TODO: On Windows, use WSAGetLastError().
120        error.SetErrorToErrno();
121        return error;
122    }
123
124    // Since they both refer to the same socket descriptor, arbitrarily choose the send socket to
125    // be the owner.
126    final_socket.reset(new Socket(sock, ProtocolTcp, true));
127
128    // Enable local address reuse
129    final_socket->SetOption(SOL_SOCKET, SO_REUSEADDR, 1);
130
131    struct sockaddr_in sa;
132    ::memset (&sa, 0, sizeof (sa));
133    sa.sin_family = AF_INET;
134    sa.sin_port = htons (port);
135
136    int inet_pton_result = ::inet_pton (AF_INET, host_str.c_str(), &sa.sin_addr);
137
138    if (inet_pton_result <= 0)
139    {
140        struct hostent *host_entry = gethostbyname (host_str.c_str());
141        if (host_entry)
142            host_str = ::inet_ntoa (*(struct in_addr *)*host_entry->h_addr_list);
143        inet_pton_result = ::inet_pton (AF_INET, host_str.c_str(), &sa.sin_addr);
144        if (inet_pton_result <= 0)
145        {
146            // TODO: On Windows, use WSAGetLastError()
147            if (inet_pton_result == -1)
148                error.SetErrorToErrno();
149            else
150                error.SetErrorStringWithFormat("invalid host string: '%s'", host_str.c_str());
151
152            return error;
153        }
154    }
155
156    if (-1 == ::connect (sock, (const struct sockaddr *)&sa, sizeof(sa)))
157    {
158        // TODO: On Windows, use WSAGetLastError()
159        error.SetErrorToErrno();
160        return error;
161    }
162
163    // Keep our TCP packets coming without any delays.
164    final_socket->SetOption(IPPROTO_TCP, TCP_NODELAY, 1);
165    error.Clear();
166    socket = final_socket.release();
167    return error;
168}
169
170Error Socket::TcpListen(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&socket, Predicate<uint16_t>* predicate)
171{
172    std::unique_ptr<Socket> listen_socket;
173    NativeSocket listen_sock = kInvalidSocketValue;
174    Error error;
175
176    const sa_family_t family = AF_INET;
177    const int socktype = SOCK_STREAM;
178    const int protocol = IPPROTO_TCP;
179    listen_sock = ::CreateSocket (family, socktype, protocol, child_processes_inherit);
180    if (listen_sock == kInvalidSocketValue)
181    {
182        error.SetErrorToErrno();
183        return error;
184    }
185
186    listen_socket.reset(new Socket(listen_sock, ProtocolTcp, true));
187
188    // enable local address reuse
189    listen_socket->SetOption(SOL_SOCKET, SO_REUSEADDR, 1);
190
191    Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION));
192    if (log)
193        log->Printf ("ConnectionFileDescriptor::SocketListen (%s)", host_and_port.data());
194
195    std::string host_str;
196    std::string port_str;
197    int32_t port = INT32_MIN;
198    if (!DecodeHostAndPort (host_and_port, host_str, port_str, port, &error))
199        return error;
200
201    SocketAddress anyaddr;
202    if (anyaddr.SetToAnyAddress (family, port))
203    {
204        int err = ::bind (listen_sock, anyaddr, anyaddr.GetLength());
205        if (err == -1)
206        {
207            // TODO: On Windows, use WSAGetLastError()
208            error.SetErrorToErrno();
209            return error;
210        }
211
212        err = ::listen (listen_sock, 1);
213        if (err == -1)
214        {
215            // TODO: On Windows, use WSAGetLastError()
216            error.SetErrorToErrno();
217            return error;
218        }
219
220        // We were asked to listen on port zero which means we
221        // must now read the actual port that was given to us
222        // as port zero is a special code for "find an open port
223        // for me".
224        if (port == 0)
225            port = listen_socket->GetPortNumber();
226
227        // Set the port predicate since when doing a listen://<host>:<port>
228        // it often needs to accept the incoming connection which is a blocking
229        // system call. Allowing access to the bound port using a predicate allows
230        // us to wait for the port predicate to be set to a non-zero value from
231        // another thread in an efficient manor.
232        if (predicate)
233            predicate->SetValue(port, eBroadcastAlways);
234
235        socket = listen_socket.release();
236    }
237
238    return error;
239}
240
241Error Socket::BlockingAccept(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&socket)
242{
243    Error error;
244    std::string host_str;
245    std::string port_str;
246    int32_t port;
247    if (!DecodeHostAndPort(host_and_port, host_str, port_str, port, &error))
248        return error;
249
250    const sa_family_t family = AF_INET;
251    const int socktype = SOCK_STREAM;
252    const int protocol = IPPROTO_TCP;
253    SocketAddress listen_addr;
254    if (host_str.empty())
255        listen_addr.SetToLocalhost(family, port);
256    else if (host_str.compare("*") == 0)
257        listen_addr.SetToAnyAddress(family, port);
258    else
259    {
260        if (!listen_addr.getaddrinfo(host_str.c_str(), port_str.c_str(), family, socktype, protocol))
261        {
262            error.SetErrorStringWithFormat("unable to resolve hostname '%s'", host_str.c_str());
263            return error;
264        }
265    }
266
267    bool accept_connection = false;
268    std::unique_ptr<Socket> accepted_socket;
269
270    // Loop until we are happy with our connection
271    while (!accept_connection)
272    {
273        struct sockaddr_in accept_addr;
274        ::memset (&accept_addr, 0, sizeof accept_addr);
275#if !(defined (__linux__) || defined(_WIN32))
276        accept_addr.sin_len = sizeof accept_addr;
277#endif
278        socklen_t accept_addr_len = sizeof accept_addr;
279
280        int sock = Accept (this->GetNativeSocket(),
281                           (struct sockaddr *)&accept_addr,
282                           &accept_addr_len,
283                           child_processes_inherit);
284
285        if (sock == kInvalidSocketValue)
286        {
287            // TODO: On Windows, use WSAGetLastError()
288            error.SetErrorToErrno();
289            break;
290        }
291
292        bool is_same_addr = true;
293#if !(defined(__linux__) || (defined(_WIN32)))
294        is_same_addr = (accept_addr_len == listen_addr.sockaddr_in().sin_len);
295#endif
296        if (is_same_addr)
297            is_same_addr = (accept_addr.sin_addr.s_addr == listen_addr.sockaddr_in().sin_addr.s_addr);
298
299        if (is_same_addr || (listen_addr.sockaddr_in().sin_addr.s_addr == INADDR_ANY))
300        {
301            accept_connection = true;
302            // Since both sockets have the same descriptor, arbitrarily choose the send
303            // socket to be the owner.
304            accepted_socket.reset(new Socket(sock, ProtocolTcp, true));
305        }
306        else
307        {
308            const uint8_t *accept_ip = (const uint8_t *)&accept_addr.sin_addr.s_addr;
309            const uint8_t *listen_ip = (const uint8_t *)&listen_addr.sockaddr_in().sin_addr.s_addr;
310            ::fprintf (stderr, "error: rejecting incoming connection from %u.%u.%u.%u (expecting %u.%u.%u.%u)\n",
311                        accept_ip[0], accept_ip[1], accept_ip[2], accept_ip[3],
312                        listen_ip[0], listen_ip[1], listen_ip[2], listen_ip[3]);
313            accepted_socket.reset();
314        }
315    }
316
317    if (!accepted_socket)
318        return error;
319
320    // Keep our TCP packets coming without any delays.
321    accepted_socket->SetOption (IPPROTO_TCP, TCP_NODELAY, 1);
322    error.Clear();
323    socket = accepted_socket.release();
324    return error;
325
326}
327
328Error Socket::UdpConnect(llvm::StringRef host_and_port, bool child_processes_inherit, Socket *&send_socket, Socket *&recv_socket)
329{
330    std::unique_ptr<Socket> final_send_socket;
331    std::unique_ptr<Socket> final_recv_socket;
332    NativeSocket final_send_fd = kInvalidSocketValue;
333    NativeSocket final_recv_fd = kInvalidSocketValue;
334
335    Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION));
336    if (log)
337        log->Printf ("Socket::UdpConnect (host/port = %s)", host_and_port.data());
338
339    Error error;
340    std::string host_str;
341    std::string port_str;
342    int32_t port = INT32_MIN;
343    if (!DecodeHostAndPort (host_and_port, host_str, port_str, port, &error))
344        return error;
345
346    // Setup the receiving end of the UDP connection on this localhost
347    // on port zero. After we bind to port zero we can read the port.
348    final_recv_fd = ::CreateSocket (AF_INET, SOCK_DGRAM, 0, child_processes_inherit);
349    if (final_recv_fd == kInvalidSocketValue)
350    {
351        // Socket creation failed...
352        // TODO: On Windows, use WSAGetLastError().
353        error.SetErrorToErrno();
354    }
355    else
356    {
357        final_recv_socket.reset(new Socket(final_recv_fd, ProtocolUdp, true));
358
359        // Socket was created, now lets bind to the requested port
360        SocketAddress addr;
361        addr.SetToAnyAddress (AF_INET, 0);
362
363        if (::bind (final_recv_fd, addr, addr.GetLength()) == -1)
364        {
365            // Bind failed...
366            // TODO: On Windows use WSAGetLastError()
367            error.SetErrorToErrno();
368        }
369    }
370
371    assert(error.Fail() == !(final_recv_socket && final_recv_socket->IsValid()));
372    if (error.Fail())
373        return error;
374
375    // At this point we have setup the receive port, now we need to
376    // setup the UDP send socket
377
378    struct addrinfo hints;
379    struct addrinfo *service_info_list = NULL;
380
381    ::memset (&hints, 0, sizeof(hints));
382    hints.ai_family = AF_INET;
383    hints.ai_socktype = SOCK_DGRAM;
384    int err = ::getaddrinfo (host_str.c_str(), port_str.c_str(), &hints, &service_info_list);
385    if (err != 0)
386    {
387        error.SetErrorStringWithFormat("getaddrinfo(%s, %s, &hints, &info) returned error %i (%s)",
388                                       host_str.c_str(),
389                                       port_str.c_str(),
390                                       err,
391                                       gai_strerror(err));
392        return error;
393    }
394
395    for (struct addrinfo *service_info_ptr = service_info_list;
396         service_info_ptr != NULL;
397         service_info_ptr = service_info_ptr->ai_next)
398    {
399        final_send_fd = ::CreateSocket (service_info_ptr->ai_family,
400                                        service_info_ptr->ai_socktype,
401                                        service_info_ptr->ai_protocol,
402                                        child_processes_inherit);
403
404        if (final_send_fd != kInvalidSocketValue)
405        {
406            final_send_socket.reset(new Socket(final_send_fd, ProtocolUdp, true));
407            final_send_socket->m_udp_send_sockaddr = service_info_ptr;
408            break;
409        }
410        else
411            continue;
412    }
413
414    :: freeaddrinfo (service_info_list);
415
416    if (final_send_fd == kInvalidSocketValue)
417    {
418        // TODO: On Windows, use WSAGetLastError().
419        error.SetErrorToErrno();
420        return error;
421    }
422
423    send_socket = final_send_socket.release();
424    recv_socket = final_recv_socket.release();
425    error.Clear();
426    return error;
427}
428
429Error Socket::UnixDomainConnect(llvm::StringRef name, bool child_processes_inherit, Socket *&socket)
430{
431    Error error;
432#ifndef LLDB_DISABLE_POSIX
433    std::unique_ptr<Socket> final_socket;
434
435    // Open the socket that was passed in as an option
436    struct sockaddr_un saddr_un;
437    int fd = ::CreateSocket (AF_UNIX, SOCK_STREAM, 0, child_processes_inherit);
438    if (fd == kInvalidSocketValue)
439    {
440        error.SetErrorToErrno();
441        return error;
442    }
443
444    final_socket.reset(new Socket(fd, ProtocolUnixDomain, true));
445
446    saddr_un.sun_family = AF_UNIX;
447    ::strncpy(saddr_un.sun_path, name.data(), sizeof(saddr_un.sun_path) - 1);
448    saddr_un.sun_path[sizeof(saddr_un.sun_path) - 1] = '\0';
449#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
450    saddr_un.sun_len = SUN_LEN (&saddr_un);
451#endif
452
453    if (::connect (fd, (struct sockaddr *)&saddr_un, SUN_LEN (&saddr_un)) < 0)
454    {
455        error.SetErrorToErrno();
456        return error;
457    }
458
459    socket = final_socket.release();
460#else
461    error.SetErrorString("Unix domain sockets are not supported on this platform.");
462#endif
463    return error;
464}
465
466Error Socket::UnixDomainAccept(llvm::StringRef name, bool child_processes_inherit, Socket *&socket)
467{
468    Error error;
469#ifndef LLDB_DISABLE_POSIX
470    struct sockaddr_un saddr_un;
471    std::unique_ptr<Socket> listen_socket;
472    std::unique_ptr<Socket> final_socket;
473    NativeSocket listen_fd = kInvalidSocketValue;
474    NativeSocket socket_fd = kInvalidSocketValue;
475
476    listen_fd = ::CreateSocket (AF_UNIX, SOCK_STREAM, 0, child_processes_inherit);
477    if (listen_fd == kInvalidSocketValue)
478    {
479        error.SetErrorToErrno();
480        return error;
481    }
482
483    listen_socket.reset(new Socket(listen_fd, ProtocolUnixDomain, true));
484
485    saddr_un.sun_family = AF_UNIX;
486    ::strncpy(saddr_un.sun_path, name.data(), sizeof(saddr_un.sun_path) - 1);
487    saddr_un.sun_path[sizeof(saddr_un.sun_path) - 1] = '\0';
488#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__)
489    saddr_un.sun_len = SUN_LEN (&saddr_un);
490#endif
491
492    FileSystem::Unlink(name.data());
493    bool success = false;
494    if (::bind (listen_fd, (struct sockaddr *)&saddr_un, SUN_LEN (&saddr_un)) == 0)
495    {
496        if (::listen (listen_fd, 5) == 0)
497        {
498            socket_fd = Accept (listen_fd, NULL, 0, child_processes_inherit);
499            if (socket_fd > 0)
500            {
501                final_socket.reset(new Socket(socket_fd, ProtocolUnixDomain, true));
502                success = true;
503            }
504        }
505    }
506
507    if (!success)
508    {
509        error.SetErrorToErrno();
510        return error;
511    }
512    // We are done with the listen port
513    listen_socket.reset();
514
515    socket = final_socket.release();
516#else
517    error.SetErrorString("Unix domain sockets are not supported on this platform.");
518#endif
519    return error;
520}
521
522bool
523Socket::DecodeHostAndPort(llvm::StringRef host_and_port,
524                          std::string &host_str,
525                          std::string &port_str,
526                          int32_t& port,
527                          Error *error_ptr)
528{
529    static RegularExpression g_regex ("([^:]+):([0-9]+)");
530    RegularExpression::Match regex_match(2);
531    if (g_regex.Execute (host_and_port.data(), &regex_match))
532    {
533        if (regex_match.GetMatchAtIndex (host_and_port.data(), 1, host_str) &&
534            regex_match.GetMatchAtIndex (host_and_port.data(), 2, port_str))
535        {
536            port = Args::StringToSInt32 (port_str.c_str(), INT32_MIN);
537            if (port != INT32_MIN)
538            {
539                if (error_ptr)
540                    error_ptr->Clear();
541                return true;
542            }
543        }
544    }
545
546    // If this was unsuccessful, then check if it's simply a signed 32-bit integer, representing
547    // a port with an empty host.
548    host_str.clear();
549    port_str.clear();
550    port = Args::StringToSInt32(host_and_port.data(), INT32_MIN);
551    if (port != INT32_MIN)
552    {
553        port_str = host_and_port;
554        return true;
555    }
556
557    if (error_ptr)
558        error_ptr->SetErrorStringWithFormat("invalid host:port specification: '%s'", host_and_port.data());
559    return false;
560}
561
562IOObject::WaitableHandle Socket::GetWaitableHandle()
563{
564    // TODO: On Windows, use WSAEventSelect
565    return m_socket;
566}
567
568Error Socket::Read (void *buf, size_t &num_bytes)
569{
570    Error error;
571    int bytes_received = 0;
572    do
573    {
574        bytes_received = ::recv (m_socket, static_cast<char *>(buf), num_bytes, 0);
575        // TODO: Use WSAGetLastError on windows.
576    } while (bytes_received < 0 && errno == EINTR);
577
578    if (bytes_received < 0)
579    {
580        error.SetErrorToErrno();
581        num_bytes = 0;
582    }
583    else
584        num_bytes = bytes_received;
585
586    Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_HOST | LIBLLDB_LOG_COMMUNICATION));
587    if (log)
588    {
589        log->Printf ("%p Socket::Read() (socket = %" PRIu64 ", src = %p, src_len = %" PRIu64 ", flags = 0) => %" PRIi64 " (error = %s)",
590                     static_cast<void*>(this),
591                     static_cast<uint64_t>(m_socket),
592                     buf,
593                     static_cast<uint64_t>(num_bytes),
594                     static_cast<int64_t>(bytes_received),
595                     error.AsCString());
596    }
597
598    return error;
599}
600
601Error Socket::Write (const void *buf, size_t &num_bytes)
602{
603    Error error;
604    int bytes_sent = 0;
605    do
606    {
607        if (m_protocol == ProtocolUdp)
608        {
609            bytes_sent = ::sendto (m_socket,
610                                    static_cast<const char*>(buf),
611                                    num_bytes,
612                                    0,
613                                    m_udp_send_sockaddr,
614                                    m_udp_send_sockaddr.GetLength());
615        }
616        else
617            bytes_sent = ::send (m_socket, static_cast<const char *>(buf), num_bytes, 0);
618        // TODO: Use WSAGetLastError on windows.
619    } while (bytes_sent < 0 && errno == EINTR);
620
621    if (bytes_sent < 0)
622    {
623        // TODO: On Windows, use WSAGEtLastError.
624        error.SetErrorToErrno();
625        num_bytes = 0;
626    }
627    else
628        num_bytes = bytes_sent;
629
630    Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_HOST));
631    if (log)
632    {
633        log->Printf ("%p Socket::Write() (socket = %" PRIu64 ", src = %p, src_len = %" PRIu64 ", flags = 0) => %" PRIi64 " (error = %s)",
634                        static_cast<void*>(this),
635                        static_cast<uint64_t>(m_socket),
636                        buf,
637                        static_cast<uint64_t>(num_bytes),
638                        static_cast<int64_t>(bytes_sent),
639                        error.AsCString());
640    }
641
642    return error;
643}
644
645Error Socket::PreDisconnect()
646{
647    Error error;
648    return error;
649}
650
651Error Socket::Close()
652{
653    Error error;
654    if (!IsValid() || !m_should_close_fd)
655        return error;
656
657    Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION));
658    if (log)
659        log->Printf ("%p Socket::Close (fd = %i)", static_cast<void*>(this), m_socket);
660
661#if defined(_WIN32)
662    bool success = !!closesocket(m_socket);
663#else
664    bool success = !!::close (m_socket);
665#endif
666    // A reference to a FD was passed in, set it to an invalid value
667    m_socket = kInvalidSocketValue;
668    if (!success)
669    {
670        // TODO: On Windows, use WSAGetLastError().
671        error.SetErrorToErrno();
672    }
673
674    return error;
675}
676
677
678int Socket::GetOption(int level, int option_name, int &option_value)
679{
680    get_socket_option_arg_type option_value_p = reinterpret_cast<get_socket_option_arg_type>(&option_value);
681    socklen_t option_value_size = sizeof(int);
682	return ::getsockopt(m_socket, level, option_name, option_value_p, &option_value_size);
683}
684
685int Socket::SetOption(int level, int option_name, int option_value)
686{
687    set_socket_option_arg_type option_value_p = reinterpret_cast<get_socket_option_arg_type>(&option_value);
688	return ::setsockopt(m_socket, level, option_name, option_value_p, sizeof(option_value));
689}
690
691uint16_t Socket::GetPortNumber(const NativeSocket& socket)
692{
693    // We bound to port zero, so we need to figure out which port we actually bound to
694    if (socket >= 0)
695    {
696        SocketAddress sock_addr;
697        socklen_t sock_addr_len = sock_addr.GetMaxLength ();
698        if (::getsockname (socket, sock_addr, &sock_addr_len) == 0)
699            return sock_addr.GetPort ();
700    }
701    return 0;
702}
703
704// Return the port number that is being used by the socket.
705uint16_t Socket::GetPortNumber() const
706{
707    return GetPortNumber(m_socket);
708}
709