1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2013-2019 Bareos GmbH & Co. KG
5 
6    This program is Free Software; you can redistribute it and/or
7    modify it under the terms of version three of the GNU Affero General Public
8    License as published by the Free Software Foundation and included
9    in the file LICENSE.
10 
11    This program is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14    Affero General Public License for more details.
15 
16    You should have received a copy of the GNU Affero General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19    02110-1301, USA.
20 */
21 
22 #ifndef BAREOS_LIB_BSOCK_TCP_H_
23 #define BAREOS_LIB_BSOCK_TCP_H_
24 
25 #include "lib/bsock.h"
26 
27 class BareosSocketTCP : public BareosSocket {
28  public:
29   /*
30    * the header of a Bareos packet is 32 bit long.
31    * A value
32    *    < 0: indicates a signal
33    *   >= 0: the length of the message that follows
34    */
35   static const int32_t header_length = sizeof(int32_t);
36 
37  private:
38   /*
39    * max size of each single packet.
40    * 1000000 is used by older version of Bareos/Bacula,
41    * so stick to this value to be compatible with older version of bconsole.
42    */
43   static const int32_t max_packet_size = 1000000;
44   static const int32_t max_message_len = max_packet_size - header_length;
45 
46   /* methods -- in bsock_tcp.c */
47   void FinInit(JobControlRecord* jcr,
48                int sockfd,
49                const char* who,
50                const char* host,
51                int port,
52                struct sockaddr* lclient_addr) override;
53   bool open(JobControlRecord* jcr,
54             const char* name,
55             const char* host,
56             char* service,
57             int port,
58             utime_t heart_beat,
59             int* fatal) override;
60   bool SetKeepalive(JobControlRecord* jcr,
61                     int sockfd,
62                     bool enable,
63                     int keepalive_start,
64                     int keepalive_interval);
65   bool SendPacket(int32_t* hdr, int32_t pktsiz);
66   void DumpNetworkMessageToFile(const char* ptr, int nbytes);
67 
68  public:
69   BareosSocketTCP();
70   ~BareosSocketTCP();
71 
72   /* methods -- in bsock_tcp.c */
73   BareosSocket* clone() override;
74   bool connect(JobControlRecord* jcr,
75                int retry_interval,
76                utime_t max_retry_time,
77                utime_t heart_beat,
78                const char* name,
79                const char* host,
80                char* service,
81                int port,
82                bool verbose) override;
83   int32_t recv() override;
84   bool send() override;
85   bool fsend(const char*, ...);
86   int32_t read_nbytes(char* ptr, int32_t nbytes) override;
87   int32_t write_nbytes(char* ptr, int32_t nbytes) override;
88   bool signal(int signal);
89   void close() override;
90   void destroy() override;
91   int GetPeer(char* buf, socklen_t buflen) override;
92   bool SetBufferSize(uint32_t size, int rw) override;
93   int SetNonblocking() override;
94   int SetBlocking() override;
95   void RestoreBlocking(int flags) override;
96   bool ConnectionReceivedTerminateSignal() override;
97   int WaitData(int sec, int usec = 0) override;
98   int WaitDataIntr(int sec, int usec = 0) override;
99 };
100 
101 #endif /* BAREOS_LIB_BSOCK_TCP_H_ */
102