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