Socket.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //===-- Socket.h ------------------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef LLDB_HOST_SOCKET_H
  9. #define LLDB_HOST_SOCKET_H
  10. #include <memory>
  11. #include <string>
  12. #include "lldb/lldb-private.h"
  13. #include "lldb/Host/SocketAddress.h"
  14. #include "lldb/Utility/IOObject.h"
  15. #include "lldb/Utility/Predicate.h"
  16. #include "lldb/Utility/Status.h"
  17. #ifdef _WIN32
  18. #include "lldb/Host/windows/windows.h"
  19. #include <winsock2.h>
  20. #include <ws2tcpip.h>
  21. #endif
  22. namespace llvm {
  23. class StringRef;
  24. }
  25. namespace lldb_private {
  26. #if defined(_WIN32)
  27. typedef SOCKET NativeSocket;
  28. #else
  29. typedef int NativeSocket;
  30. #endif
  31. class TCPSocket;
  32. class UDPSocket;
  33. class Socket : public IOObject {
  34. public:
  35. enum SocketProtocol {
  36. ProtocolTcp,
  37. ProtocolUdp,
  38. ProtocolUnixDomain,
  39. ProtocolUnixAbstract
  40. };
  41. static const NativeSocket kInvalidSocketValue;
  42. ~Socket() override;
  43. static llvm::Error Initialize();
  44. static void Terminate();
  45. static std::unique_ptr<Socket> Create(const SocketProtocol protocol,
  46. bool child_processes_inherit,
  47. Status &error);
  48. virtual Status Connect(llvm::StringRef name) = 0;
  49. virtual Status Listen(llvm::StringRef name, int backlog) = 0;
  50. virtual Status Accept(Socket *&socket) = 0;
  51. // Initialize a Tcp Socket object in listening mode. listen and accept are
  52. // implemented separately because the caller may wish to manipulate or query
  53. // the socket after it is initialized, but before entering a blocking accept.
  54. static llvm::Expected<std::unique_ptr<TCPSocket>>
  55. TcpListen(llvm::StringRef host_and_port, bool child_processes_inherit,
  56. Predicate<uint16_t> *predicate, int backlog = 5);
  57. static llvm::Expected<std::unique_ptr<Socket>>
  58. TcpConnect(llvm::StringRef host_and_port, bool child_processes_inherit);
  59. static llvm::Expected<std::unique_ptr<UDPSocket>>
  60. UdpConnect(llvm::StringRef host_and_port, bool child_processes_inherit);
  61. static Status UnixDomainConnect(llvm::StringRef host_and_port,
  62. bool child_processes_inherit,
  63. Socket *&socket);
  64. static Status UnixDomainAccept(llvm::StringRef host_and_port,
  65. bool child_processes_inherit, Socket *&socket);
  66. static Status UnixAbstractConnect(llvm::StringRef host_and_port,
  67. bool child_processes_inherit,
  68. Socket *&socket);
  69. static Status UnixAbstractAccept(llvm::StringRef host_and_port,
  70. bool child_processes_inherit,
  71. Socket *&socket);
  72. int GetOption(int level, int option_name, int &option_value);
  73. int SetOption(int level, int option_name, int option_value);
  74. NativeSocket GetNativeSocket() const { return m_socket; }
  75. SocketProtocol GetSocketProtocol() const { return m_protocol; }
  76. Status Read(void *buf, size_t &num_bytes) override;
  77. Status Write(const void *buf, size_t &num_bytes) override;
  78. virtual Status PreDisconnect();
  79. Status Close() override;
  80. bool IsValid() const override { return m_socket != kInvalidSocketValue; }
  81. WaitableHandle GetWaitableHandle() override;
  82. static bool DecodeHostAndPort(llvm::StringRef host_and_port,
  83. std::string &host_str, std::string &port_str,
  84. int32_t &port, Status *error_ptr);
  85. // If this Socket is connected then return the URI used to connect.
  86. virtual std::string GetRemoteConnectionURI() const { return ""; };
  87. protected:
  88. Socket(SocketProtocol protocol, bool should_close,
  89. bool m_child_process_inherit);
  90. virtual size_t Send(const void *buf, const size_t num_bytes);
  91. static void SetLastError(Status &error);
  92. static NativeSocket CreateSocket(const int domain, const int type,
  93. const int protocol,
  94. bool child_processes_inherit, Status &error);
  95. static NativeSocket AcceptSocket(NativeSocket sockfd, struct sockaddr *addr,
  96. socklen_t *addrlen,
  97. bool child_processes_inherit, Status &error);
  98. SocketProtocol m_protocol;
  99. NativeSocket m_socket;
  100. bool m_child_processes_inherit;
  101. bool m_should_close_fd;
  102. };
  103. } // namespace lldb_private
  104. #endif // LLDB_HOST_SOCKET_H