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 (in_port_t port, PortBoundCallback callback, const void *callback_baton);
47     rnb_err_t Connect (const char *host, uint16_t port);
48 
49     rnb_err_t useFD(int fd);
50 
51 #ifdef WITH_LOCKDOWN
52     rnb_err_t ConnectToService();
53 #endif
54     rnb_err_t OpenFile (const char *path);
55     rnb_err_t Disconnect (bool save_errno);
56     rnb_err_t Read (std::string &p);
57     rnb_err_t Write (const void *buffer, size_t length);
58 
59     bool IsConnected () const { return m_fd != -1; }
60     void SaveErrno (int curr_errno);
61     DNBTimer& Timer() { return m_timer; }
62 
63     static int SetSocketOption(int fd, int level, int option_name, int option_value);
64 private:
65     // Outlaw some constructors
66     RNBSocket (const RNBSocket &);
67 
68 protected:
69     rnb_err_t ClosePort (int& fd, bool save_errno);
70 
71     int m_fd;    // Socket we use to communicate once conn established
72 
73 #ifdef WITH_LOCKDOWN
74     bool m_fd_from_lockdown;
75     lockdown_connection m_ld_conn;
76 #endif
77 
78     DNBTimer m_timer;
79 };
80 
81 
82 #endif // #ifndef __RNBSocket_h__
83