1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2013-2015 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 class BareosSocketTCP : public BareosSocket {
26 private:
27    /*
28     * the header of a Bareos packet is 32 bit long.
29     * A value
30     *    < 0: indicates a signal
31     *   >= 0: the length of the message that follows
32     */
33    static const int32_t header_length = sizeof(int32_t);
34 
35    /*
36     * max size of each single packet.
37     * 1000000 is used by older version of Bareos/Bacula,
38     * so stick to this value to be compatible with older version of bconsole.
39     */
40    static const int32_t max_packet_size = 1000000;
41    static const int32_t max_message_len = max_packet_size - header_length;
42 
43    /* methods -- in bsock_tcp.c */
44    void FinInit(JobControlRecord * jcr, int sockfd, const char *who, const char *host, int port,
45                  struct sockaddr *lclient_addr) override;
46    bool open(JobControlRecord *jcr, const char *name, const char *host, char *service,
47              int port, utime_t heart_beat, int *fatal) override;
48    bool SetKeepalive(JobControlRecord *jcr, int sockfd, bool enable, int keepalive_start, int keepalive_interval);
49    bool SendPacket(int32_t *hdr, int32_t pktsiz);
50 
51 public:
52    BareosSocketTCP();
53    ~BareosSocketTCP();
54 
55    /* methods -- in bsock_tcp.c */
56    BareosSocket *clone() override;
57    bool connect(JobControlRecord * jcr, int retry_interval, utime_t max_retry_time,
58                 utime_t heart_beat, const char *name, const char *host,
59                 char *service, int port, bool verbose) override;
60    int32_t recv() override;
61    bool send() override;
62    bool fsend(const char*, ...);
63    int32_t read_nbytes(char *ptr, int32_t nbytes) override;
64    int32_t write_nbytes(char *ptr, int32_t nbytes) override;
65    bool signal(int signal);
66    void close() override;
67    void destroy() override;
68    int GetPeer(char *buf, socklen_t buflen) override;
69    bool SetBufferSize(uint32_t size, int rw) override;
70    int SetNonblocking() override;
71    int SetBlocking() override;
72    void RestoreBlocking(int flags) override;
73    bool ConnectionReceivedTerminateSignal() override;
74    int WaitData(int sec, int usec = 0) override;
75    int WaitDataIntr(int sec, int usec = 0) override;
76 };
77 
78 typedef std::unique_ptr<BareosSocket,std::function<void(BareosSocket*)>> BareosSocketUniquePtr;
79 
MakeNewBareosSocketUniquePtr()80 inline BareosSocketUniquePtr MakeNewBareosSocketUniquePtr()
81 {
82   /* this will call the smartalloc deleter */
83   BareosSocketUniquePtr p(New(BareosSocketTCP), [](BareosSocket *p) {delete p;});
84   return p;
85 }
86 
87 #endif /* BAREOS_LIB_BSOCK_TCP_H_ */
88