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/StringConvert.h" 19 #include "lldb/Host/TimeValue.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 38 using namespace lldb; 39 using namespace lldb_private; 40 41 #if defined(_WIN32) 42 typedef const char * set_socket_option_arg_type; 43 typedef char * get_socket_option_arg_type; 44 const NativeSocket Socket::kInvalidSocketValue = INVALID_SOCKET; 45 #else // #if defined(_WIN32) 46 typedef const void * set_socket_option_arg_type; 47 typedef void * get_socket_option_arg_type; 48 const 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 58 namespace { 59 60 NativeSocket 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 71 NativeSocket 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 85 Socket::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 93 Socket::~Socket() 94 { 95 Close(); 96 } 97 98 Error 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 170 Error 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 ("Socket::TcpListen (%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->GetLocalPortNumber(); 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 241 Error 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 328 Error 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 429 Error 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 466 Error 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 522 bool 523 Socket::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(), ®ex_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 bool ok = false; 537 port = StringConvert::ToUInt32 (port_str.c_str(), UINT32_MAX, 10, &ok); 538 if (ok && port < UINT16_MAX) 539 { 540 if (error_ptr) 541 error_ptr->Clear(); 542 return true; 543 } 544 // port is too large 545 if (error_ptr) 546 error_ptr->SetErrorStringWithFormat("invalid host:port specification: '%s'", host_and_port.data()); 547 return false; 548 } 549 } 550 551 // If this was unsuccessful, then check if it's simply a signed 32-bit integer, representing 552 // a port with an empty host. 553 host_str.clear(); 554 port_str.clear(); 555 bool ok = false; 556 port = StringConvert::ToUInt32 (host_and_port.data(), UINT32_MAX, 10, &ok); 557 if (ok && port < UINT16_MAX) 558 { 559 port_str = host_and_port; 560 if (error_ptr) 561 error_ptr->Clear(); 562 return true; 563 } 564 565 if (error_ptr) 566 error_ptr->SetErrorStringWithFormat("invalid host:port specification: '%s'", host_and_port.data()); 567 return false; 568 } 569 570 IOObject::WaitableHandle Socket::GetWaitableHandle() 571 { 572 // TODO: On Windows, use WSAEventSelect 573 return m_socket; 574 } 575 576 Error Socket::Read (void *buf, size_t &num_bytes) 577 { 578 Error error; 579 int bytes_received = 0; 580 do 581 { 582 bytes_received = ::recv (m_socket, static_cast<char *>(buf), num_bytes, 0); 583 // TODO: Use WSAGetLastError on windows. 584 } while (bytes_received < 0 && errno == EINTR); 585 586 if (bytes_received < 0) 587 { 588 error.SetErrorToErrno(); 589 num_bytes = 0; 590 } 591 else 592 num_bytes = bytes_received; 593 594 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_HOST | LIBLLDB_LOG_COMMUNICATION)); 595 if (log) 596 { 597 log->Printf ("%p Socket::Read() (socket = %" PRIu64 ", src = %p, src_len = %" PRIu64 ", flags = 0) => %" PRIi64 " (error = %s)", 598 static_cast<void*>(this), 599 static_cast<uint64_t>(m_socket), 600 buf, 601 static_cast<uint64_t>(num_bytes), 602 static_cast<int64_t>(bytes_received), 603 error.AsCString()); 604 } 605 606 return error; 607 } 608 609 Error Socket::Write (const void *buf, size_t &num_bytes) 610 { 611 Error error; 612 int bytes_sent = 0; 613 do 614 { 615 if (m_protocol == ProtocolUdp) 616 { 617 bytes_sent = ::sendto (m_socket, 618 static_cast<const char*>(buf), 619 num_bytes, 620 0, 621 m_udp_send_sockaddr, 622 m_udp_send_sockaddr.GetLength()); 623 } 624 else 625 bytes_sent = ::send (m_socket, static_cast<const char *>(buf), num_bytes, 0); 626 // TODO: Use WSAGetLastError on windows. 627 } while (bytes_sent < 0 && errno == EINTR); 628 629 if (bytes_sent < 0) 630 { 631 // TODO: On Windows, use WSAGEtLastError. 632 error.SetErrorToErrno(); 633 num_bytes = 0; 634 } 635 else 636 num_bytes = bytes_sent; 637 638 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_HOST)); 639 if (log) 640 { 641 log->Printf ("%p Socket::Write() (socket = %" PRIu64 ", src = %p, src_len = %" PRIu64 ", flags = 0) => %" PRIi64 " (error = %s)", 642 static_cast<void*>(this), 643 static_cast<uint64_t>(m_socket), 644 buf, 645 static_cast<uint64_t>(num_bytes), 646 static_cast<int64_t>(bytes_sent), 647 error.AsCString()); 648 } 649 650 return error; 651 } 652 653 Error Socket::PreDisconnect() 654 { 655 Error error; 656 return error; 657 } 658 659 Error Socket::Close() 660 { 661 Error error; 662 if (!IsValid() || !m_should_close_fd) 663 return error; 664 665 Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION)); 666 if (log) 667 log->Printf ("%p Socket::Close (fd = %i)", static_cast<void*>(this), m_socket); 668 669 #if defined(_WIN32) 670 bool success = !!closesocket(m_socket); 671 #else 672 bool success = !!::close (m_socket); 673 #endif 674 // A reference to a FD was passed in, set it to an invalid value 675 m_socket = kInvalidSocketValue; 676 if (!success) 677 { 678 // TODO: On Windows, use WSAGetLastError(). 679 error.SetErrorToErrno(); 680 } 681 682 return error; 683 } 684 685 686 int Socket::GetOption(int level, int option_name, int &option_value) 687 { 688 get_socket_option_arg_type option_value_p = reinterpret_cast<get_socket_option_arg_type>(&option_value); 689 socklen_t option_value_size = sizeof(int); 690 return ::getsockopt(m_socket, level, option_name, option_value_p, &option_value_size); 691 } 692 693 int Socket::SetOption(int level, int option_name, int option_value) 694 { 695 set_socket_option_arg_type option_value_p = reinterpret_cast<get_socket_option_arg_type>(&option_value); 696 return ::setsockopt(m_socket, level, option_name, option_value_p, sizeof(option_value)); 697 } 698 699 uint16_t Socket::GetLocalPortNumber(const NativeSocket& socket) 700 { 701 // We bound to port zero, so we need to figure out which port we actually bound to 702 if (socket >= 0) 703 { 704 SocketAddress sock_addr; 705 socklen_t sock_addr_len = sock_addr.GetMaxLength (); 706 if (::getsockname (socket, sock_addr, &sock_addr_len) == 0) 707 return sock_addr.GetPort (); 708 } 709 return 0; 710 } 711 712 // Return the port number that is being used by the socket. 713 uint16_t Socket::GetLocalPortNumber() const 714 { 715 return GetLocalPortNumber (m_socket); 716 } 717 718 std::string Socket::GetLocalIPAddress () const 719 { 720 // We bound to port zero, so we need to figure out which port we actually bound to 721 if (m_socket >= 0) 722 { 723 SocketAddress sock_addr; 724 socklen_t sock_addr_len = sock_addr.GetMaxLength (); 725 if (::getsockname (m_socket, sock_addr, &sock_addr_len) == 0) 726 return sock_addr.GetIPAddress (); 727 } 728 return ""; 729 } 730 731 uint16_t Socket::GetRemotePortNumber () const 732 { 733 if (m_socket >= 0) 734 { 735 SocketAddress sock_addr; 736 socklen_t sock_addr_len = sock_addr.GetMaxLength (); 737 if (::getpeername (m_socket, sock_addr, &sock_addr_len) == 0) 738 return sock_addr.GetPort (); 739 } 740 return 0; 741 } 742 743 std::string Socket::GetRemoteIPAddress () const 744 { 745 // We bound to port zero, so we need to figure out which port we actually bound to 746 if (m_socket >= 0) 747 { 748 SocketAddress sock_addr; 749 socklen_t sock_addr_len = sock_addr.GetMaxLength (); 750 if (::getpeername (m_socket, sock_addr, &sock_addr_len) == 0) 751 return sock_addr.GetIPAddress (); 752 } 753 return ""; 754 } 755 756 757