| 1 | //===-- RNBSocket.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 | // |
| 9 | // Created by Greg Clayton on 12/12/07. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLDB_TOOLS_DEBUGSERVER_SOURCE_RNBSOCKET_H |
| 14 | #define LLDB_TOOLS_DEBUGSERVER_SOURCE_RNBSOCKET_H |
| 15 | |
| 16 | #include "DNBTimer.h" |
| 17 | #include "RNBDefs.h" |
| 18 | #include <string> |
| 19 | #include <sys/socket.h> |
| 20 | #include <sys/types.h> |
| 21 | |
| 22 | #ifdef WITH_LOCKDOWN |
| 23 | #include "lockdown.h" |
| 24 | #endif |
| 25 | |
| 26 | class RNBSocket { |
| 27 | public: |
| 28 | typedef void (*PortBoundCallback)(const void *baton, uint16_t port); |
| 29 | |
| 30 | RNBSocket() |
| 31 | : m_fd(-1), |
| 32 | #ifdef WITH_LOCKDOWN |
| 33 | m_fd_from_lockdown(false), m_ld_conn(), |
| 34 | #endif |
| 35 | m_timer(true) // Make a thread safe timer |
| 36 | { |
| 37 | } |
| 38 | ~RNBSocket(void) { Disconnect(save_errno: false); } |
| 39 | |
| 40 | rnb_err_t Listen(const char *listen_host, uint16_t port, |
| 41 | PortBoundCallback callback, const void *callback_baton); |
| 42 | rnb_err_t Connect(const char *host, uint16_t port); |
| 43 | |
| 44 | rnb_err_t useFD(int fd); |
| 45 | |
| 46 | #ifdef WITH_LOCKDOWN |
| 47 | rnb_err_t ConnectToService(); |
| 48 | #endif |
| 49 | rnb_err_t OpenFile(const char *path); |
| 50 | rnb_err_t Disconnect(bool save_errno); |
| 51 | rnb_err_t Read(std::string &p); |
| 52 | rnb_err_t Write(const void *buffer, size_t length); |
| 53 | |
| 54 | bool IsConnected() const { return m_fd != -1; } |
| 55 | void SaveErrno(int curr_errno); |
| 56 | DNBTimer &Timer() { return m_timer; } |
| 57 | |
| 58 | static int SetSocketOption(int fd, int level, int option_name, |
| 59 | int option_value); |
| 60 | |
| 61 | private: |
| 62 | RNBSocket(const RNBSocket &) = delete; |
| 63 | |
| 64 | protected: |
| 65 | rnb_err_t ClosePort(int &fd, bool save_errno); |
| 66 | |
| 67 | int m_fd; // Socket we use to communicate once conn established |
| 68 | |
| 69 | #ifdef WITH_LOCKDOWN |
| 70 | bool m_fd_from_lockdown; |
| 71 | lockdown_connection m_ld_conn; |
| 72 | #endif |
| 73 | |
| 74 | DNBTimer m_timer; |
| 75 | }; |
| 76 | |
| 77 | #endif // LLDB_TOOLS_DEBUGSERVER_SOURCE_RNBSOCKET_H |
| 78 | |