blob: be2199d5316efa97550512a0d10de1498250b5fa [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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
Jason Molenda7eaf54d2013-01-18 01:20:12 +000023#ifdef WITH_LOCKDOWN
24#include "lockdown.h"
25#endif
26
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027class RNBSocket
28{
29public:
Greg Clayton8b82f082011-04-12 05:54:46 +000030 typedef void (*PortBoundCallback) (const void *baton, in_port_t port);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031
32 RNBSocket () :
Greg Clayton8b82f082011-04-12 05:54:46 +000033 m_fd (-1),
Jason Molenda42999a42012-02-22 02:18:59 +000034#ifdef WITH_LOCKDOWN
Greg Clayton8b82f082011-04-12 05:54:46 +000035 m_fd_from_lockdown (false),
Jason Molenda7eaf54d2013-01-18 01:20:12 +000036 m_ld_conn (),
Jason Molenda42999a42012-02-22 02:18:59 +000037#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038 m_timer (true) // Make a thread safe timer
39 {
40 }
41 ~RNBSocket (void)
42 {
43 Disconnect (false);
44 }
45
Greg Clayton8b82f082011-04-12 05:54:46 +000046 rnb_err_t Listen (in_port_t port, PortBoundCallback callback, const void *callback_baton);
Greg Clayton95bf0fd2011-04-01 00:29:43 +000047 rnb_err_t Connect (const char *host, uint16_t port);
48
Jason Molenda42999a42012-02-22 02:18:59 +000049 rnb_err_t useFD(int fd);
50
51#ifdef WITH_LOCKDOWN
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052 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
Greg Clayton8b82f082011-04-12 05:54:46 +000059 bool IsConnected () const { return m_fd != -1; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060 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);
64private:
65 // Outlaw some constructors
66 RNBSocket (const RNBSocket &);
67
68protected:
69 rnb_err_t ClosePort (int& fd, bool save_errno);
70
Greg Clayton8b82f082011-04-12 05:54:46 +000071 int m_fd; // Socket we use to communicate once conn established
Jason Molenda42999a42012-02-22 02:18:59 +000072
73#ifdef WITH_LOCKDOWN
Greg Clayton8b82f082011-04-12 05:54:46 +000074 bool m_fd_from_lockdown;
Jason Molenda7eaf54d2013-01-18 01:20:12 +000075 lockdown_connection m_ld_conn;
Jason Molenda42999a42012-02-22 02:18:59 +000076#endif
77
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078 DNBTimer m_timer;
79};
80
81
82#endif // #ifndef __RNBSocket_h__