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