]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/bsock.h
Backport from BEE
[bacula/bacula] / bacula / src / lib / bsock.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from many
7    others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    Bacula® is a registered trademark of Kern Sibbald.
15 */
16 /*
17  * Bacula Sock Class definition
18  *   Note, the old non-class code is in bnet.c, and the
19  *   new class code associated with this file is in bsock.c
20  *
21  * Kern Sibbald, May MM
22  *
23  * Zero msglen from other end indicates soft eof (usually
24  *   end of some binary data stream, but not end of conversation).
25  *
26  * Negative msglen, is special "signal" (no data follows).
27  *   See below for SIGNAL codes.
28  *
29  */
30
31 #ifndef __BSOCK_H_
32 #define __BSOCK_H_
33
34 struct btimer_t;                      /* forward reference */
35 class BSOCK;
36 /* Effectively disable the bsock time out */
37 #define BSOCK_TIMEOUT  3600 * 24 * 200;  /* default 200 days */
38 btimer_t *start_bsock_timer(BSOCK *bs, uint32_t wait);
39 void stop_bsock_timer(btimer_t *wid);
40
41
42 class BSOCK {
43 /*
44  * Note, keep this public part before the private otherwise
45  *  bat breaks on some systems such as RedHat.
46  */
47 public:
48    uint64_t read_seqno;               /* read sequence number */
49    POOLMEM *msg;                      /* message pool buffer */
50    POOLMEM *errmsg;                   /* edited error message */
51    RES *res;                          /* Resource to which we are connected */
52    FILE *m_spool_fd;                  /* spooling file */
53    TLS_CONNECTION *tls;               /* associated tls connection */
54    IPADDR *src_addr;                  /* IP address to source connections from */
55    uint32_t in_msg_no;                /* input message number */
56    uint32_t out_msg_no;               /* output message number */
57    int32_t msglen;                    /* message length */
58    volatile time_t timer_start;       /* time started read/write */
59    volatile time_t timeout;           /* timeout BSOCK after this interval */
60    int m_fd;                          /* socket file descriptor */
61    int b_errno;                       /* bsock errno */
62    int m_blocking;                    /* blocking state (0 = nonblocking, 1 = blocking) */
63    volatile int errors;               /* incremented for each error on socket */
64    volatile bool m_suppress_error_msgs; /* set to suppress error messages */
65
66    struct sockaddr client_addr;       /* client's IP address */
67    struct sockaddr_in peer_addr;      /* peer's IP address */
68
69 private:
70    BSOCK *m_next;                     /* next BSOCK if duped */
71    JCR *m_jcr;                        /* jcr or NULL for error msgs */
72    pthread_mutex_t m_mutex;           /* for locking if use_locking set */
73    char *m_who;                       /* Name of daemon to which we are talking */
74    char *m_host;                      /* Host name/IP */
75    int m_port;                        /* desired port */
76    btimer_t *m_tid;                   /* timer id */
77    boffset_t m_data_end;              /* offset of data written */
78    boffset_t m_last_data_end;          /* offset of last valid data written */
79    int32_t m_FileIndex;               /* attr spool FI */
80    int32_t m_lastFileIndex;           /* last valid attr spool FI */
81    volatile bool m_timed_out: 1;      /* timed out in read/write */
82    volatile bool m_terminated: 1;     /* set when BNET_TERMINATE arrives */
83    bool m_closed: 1;                  /* set when socket is closed */
84    bool m_duped: 1;                   /* set if duped BSOCK */
85    bool m_spool: 1;                   /* set for spooling */
86    bool m_use_locking: 1;             /* set to use locking */
87
88    int64_t m_bwlimit;                 /* set to limit bandwidth */
89    int64_t m_nb_bytes;                /* bytes sent/recv since the last tick */
90    btime_t m_last_tick;               /* last tick used by bwlimit */
91
92    void fin_init(JCR * jcr, int sockfd, const char *who, const char *host, int port,
93                struct sockaddr *lclient_addr);
94    bool open(JCR *jcr, const char *name, char *host, char *service,
95                int port, utime_t heart_beat, int *fatal);
96
97 public:
98    /* methods -- in bsock.c */
99    void init();
100    void free_tls();
101    bool connect(JCR * jcr, int retry_interval, utime_t max_retry_time,
102                 utime_t heart_beat, const char *name, char *host,
103                 char *service, int port, int verbose);
104    int32_t recv();
105    bool send();
106    bool fsend(const char*, ...);
107    bool signal(int signal);
108    void close();                      /* close connection and destroy packet */
109    void destroy();                    /* destroy socket packet */
110    const char *bstrerror();           /* last error on socket */
111    int get_peer(char *buf, socklen_t buflen);
112    bool despool(void update_attr_spool_size(ssize_t size), ssize_t tsize);
113    bool set_buffer_size(uint32_t size, int rw);
114    int set_nonblocking();
115    int set_blocking();
116    void restore_blocking(int flags);
117    void set_killable(bool killable);
118    int wait_data(int sec, int usec=0);
119    int wait_data_intr(int sec, int usec=0);
120    bool authenticate_director(const char *name, const char *password,
121                   TLS_CONTEXT *tls_ctx, char *response, int response_len);
122    bool set_locking();                /* in bsock.c */
123    void clear_locking();              /* in bsock.c */
124    void set_source_address(dlist *src_addr_list);
125    void control_bwlimit(int bytes);   /* in bsock.c */
126
127    /* Inline functions */
128    void suppress_error_messages(bool flag) { m_suppress_error_msgs = flag; };
129    void set_jcr(JCR *jcr) { m_jcr = jcr; };
130    void set_who(char *who) { m_who = who; };
131    void set_host(char *host) { m_host = host; };
132    void set_port(int port) { m_port = port; };
133    char *who() const { return m_who; };
134    char *host() const { return m_host; };
135    int port() const { return m_port; };
136    JCR *jcr() const { return m_jcr; };
137    JCR *get_jcr() const { return m_jcr; };
138    bool is_spooling() const { return m_spool; };
139    bool is_duped() const { return m_duped; };
140    bool is_terminated() const { return m_terminated; };
141    bool is_timed_out() const { return m_timed_out; };
142    bool is_closed() const { return m_closed; };
143    bool is_open() const { return !m_closed; };
144    bool is_stop() const { return errors || is_terminated() || is_closed(); }
145    bool is_error() { errno = b_errno; return errors; };
146    void set_data_end(int32_t FileIndex) {
147           if (m_spool && FileIndex > m_FileIndex) {
148               m_lastFileIndex = m_FileIndex;
149               m_last_data_end = m_data_end;
150               m_FileIndex = FileIndex;
151               m_data_end = ftello(m_spool_fd);
152            }
153         };
154    boffset_t get_last_data_end() { return m_last_data_end; };
155    int32_t get_lastFileIndex() { return m_lastFileIndex; };
156    void set_bwlimit(int64_t maxspeed) { m_bwlimit = maxspeed; };
157    bool use_bwlimit() { return m_bwlimit > 0;};
158    void set_spooling() { m_spool = true; };
159    void clear_spooling() { m_spool = false; };
160    void set_duped() { m_duped = true; };
161    void set_timed_out() { m_timed_out = true; };
162    void clear_timed_out() { m_timed_out = false; };
163    void set_terminated() { m_terminated = true; };
164    void set_closed() { m_closed = true; };
165    void start_timer(int sec) { m_tid = start_bsock_timer(this, sec); };
166    void stop_timer() { stop_bsock_timer(m_tid); };
167    void swap_msgs();
168 };
169
170 /*
171  *  Signal definitions for use in bsock->signal()
172  *  Note! These must be negative.  There are signals that are generated
173  *   by the bsock software not by the OS ...
174  */
175 enum {
176    BNET_EOD            = -1,          /* End of data stream, new data may follow */
177    BNET_EOD_POLL       = -2,          /* End of data and poll all in one */
178    BNET_STATUS         = -3,          /* Send full status */
179    BNET_TERMINATE      = -4,          /* Conversation terminated, doing close() */
180    BNET_POLL           = -5,          /* Poll request, I'm hanging on a read */
181    BNET_HEARTBEAT      = -6,          /* Heartbeat Response requested */
182    BNET_HB_RESPONSE    = -7,          /* Only response permited to HB */
183    BNET_xxxxxxPROMPT   = -8,          /* No longer used -- Prompt for subcommand */
184    BNET_BTIME          = -9,          /* Send UTC btime */
185    BNET_BREAK          = -10,         /* Stop current command -- ctl-c */
186    BNET_START_SELECT   = -11,         /* Start of a selection list */
187    BNET_END_SELECT     = -12,         /* End of a select list */
188    BNET_INVALID_CMD    = -13,         /* Invalid command sent */
189    BNET_CMD_FAILED     = -14,         /* Command failed */
190    BNET_CMD_OK         = -15,         /* Command succeeded */
191    BNET_CMD_BEGIN      = -16,         /* Start command execution */
192    BNET_MSGS_PENDING   = -17,         /* Messages pending */
193    BNET_MAIN_PROMPT    = -18,         /* Server ready and waiting */
194    BNET_SELECT_INPUT   = -19,         /* Return selection input */
195    BNET_WARNING_MSG    = -20,         /* Warning message */
196    BNET_ERROR_MSG      = -21,         /* Error message -- command failed */
197    BNET_INFO_MSG       = -22,         /* Info message -- status line */
198    BNET_RUN_CMD        = -23,         /* Run command follows */
199    BNET_YESNO          = -24,         /* Request yes no response */
200    BNET_START_RTREE    = -25,         /* Start restore tree mode */
201    BNET_END_RTREE      = -26,         /* End restore tree mode */
202    BNET_SUB_PROMPT     = -27,         /* Indicate we are at a subprompt */
203    BNET_TEXT_INPUT     = -28          /* Get text input from user */
204 };
205
206 #define BNET_SETBUF_READ  1           /* Arg for bnet_set_buffer_size */
207 #define BNET_SETBUF_WRITE 2           /* Arg for bnet_set_buffer_size */
208
209 /*
210  * Return status from bnet_recv()
211  * Note, the HARDEOF and ERROR refer to comm status/problems
212  *  rather than the BNET_xxx above, which are software signals.
213  */
214 enum {
215    BNET_SIGNAL         = -1,
216    BNET_HARDEOF        = -2,
217    BNET_ERROR          = -3
218 };
219
220 /*
221  * TLS enabling values. Value is important for comparison, ie:
222  * if (tls_remote_need < BNET_TLS_REQUIRED) { ... }
223  */
224 enum {
225    BNET_TLS_NONE        = 0,          /* cannot do TLS */
226    BNET_TLS_OK          = 1,          /* can do, but not required on my end */
227    BNET_TLS_REQUIRED    = 2           /* TLS is required */
228 };
229
230 int32_t read_nbytes(BSOCK * bsock, char *ptr, int32_t nbytes);
231 int32_t write_nbytes(BSOCK * bsock, char *ptr, int32_t nbytes);
232
233 BSOCK *new_bsock();
234 /*
235  * Completely release the socket packet, and NULL the pointer
236  */
237 #define free_bsock(a) do{if(a){(a)->close(); (a)->destroy(); (a)=NULL;}} while(0)
238
239 /*
240  * Does the socket exist and is it open?
241  */
242 #define is_bsock_open(a) ((a) && (a)->is_open())
243
244 #endif /* __BSOCK_H_ */