2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2017 Kern Sibbald
6 The original author of Bacula is Kern Sibbald, with contributions
7 from many others, a complete list can be found in the file AUTHORS.
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.
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
20 * Bacula Sock Class definition
21 * Note, the old non-class code is in bnet.c, and the
22 * new class code associated with this file is in bsock.c
24 * Kern Sibbald, May MM
26 * Zero msglen from other end indicates soft eof (usually
27 * end of some binary data stream, but not end of conversation).
29 * Negative msglen, is special "signal" (no data follows).
30 * See below for SIGNAL codes.
36 struct btimer_t; /* forward reference */
38 /* Effectively disable the bsock time out */
39 #define BSOCK_TIMEOUT 3600 * 24 * 200; /* default 200 days */
40 btimer_t *start_bsock_timer(BSOCK *bs, uint32_t wait);
41 void stop_bsock_timer(btimer_t *wid);
46 virtual ~BSOCKCallback();
47 virtual bool bsock_send_cb() = 0;
52 * Note, keep this public part before the private otherwise
53 * bat breaks on some systems such as RedHat.
56 uint64_t read_seqno; /* read sequence number */
57 POOLMEM *msg; /* message pool buffer */
58 POOLMEM *cmsg; /* Compress buffer */
59 POOLMEM *errmsg; /* edited error message */
60 RES *res; /* Resource to which we are connected */
61 FILE *m_spool_fd; /* spooling file */
62 TLS_CONNECTION *tls; /* associated tls connection */
63 IPADDR *src_addr; /* IP address to source connections from */
64 uint32_t in_msg_no; /* input message number */
65 uint32_t out_msg_no; /* output message number */
66 uint32_t *pout_msg_no; /* pointer to the above */
67 int32_t msglen; /* message length */
68 volatile time_t timer_start; /* time started read/write */
69 volatile time_t timeout; /* timeout BSOCK after this interval */
70 int m_fd; /* socket file descriptor */
71 int b_errno; /* bsock errno */
72 int m_blocking; /* blocking state (0 = nonblocking, 1 = blocking) */
73 volatile int errors; /* incremented for each error on socket */
74 volatile bool m_suppress_error_msgs; /* set to suppress error messages */
76 struct sockaddr client_addr; /* client's IP address */
77 struct sockaddr_in peer_addr; /* peer's IP address */
79 /* when "installed", send_hook_cb->bsock_send_cb() is called before
82 BSOCKCallback *send_hook_cb;
85 /* m_master is used by "duped" BSOCK to access some attributes of the "parent"
86 * thread to have an up2date status (for example when the job is canceled,
87 * the "parent" BSOCK is "terminated", but the duped BSOCK is unchanged)
88 * In the future more attributes and method could use the "m_master"
90 * master->m_rmutex could replace pm_rmutex, idem for the (w)rite" mutex
91 * "m_master->error" should be incremented instead of "error", but
92 * this require a lock.
94 * USAGE: the parent thread MUST be sure that the child thread have quit
95 * before to free the "parent" BSOCK.
97 BSOCK *m_next; /* next BSOCK if duped (not actually used) */
98 JCR *m_jcr; /* jcr or NULL for error msgs */
99 pthread_mutex_t m_rmutex; /* for read locking if use_locking set */
100 pthread_mutex_t m_wmutex; /* for write locking if use_locking set */
101 mutable pthread_mutex_t m_mmutex; /* when accessing the master/next chain */
102 pthread_mutex_t *pm_rmutex; /* Pointer to the read mutex */
103 pthread_mutex_t *pm_wmutex; /* Pointer to the write mutex */
104 char *m_who; /* Name of daemon to which we are talking */
105 char *m_host; /* Host name/IP */
106 int m_port; /* desired port */
107 btimer_t *m_tid; /* timer id */
108 boffset_t m_data_end; /* offset of data written */
109 boffset_t m_last_data_end; /* offset of last valid data written */
110 int32_t m_FileIndex; /* attr spool FI */
111 int32_t m_lastFileIndex; /* last valid attr spool FI */
112 uint32_t m_flags; /* Special flags */
113 volatile bool m_timed_out: 1; /* timed out in read/write */
114 volatile bool m_terminated: 1; /* set when BNET_TERMINATE arrives */
115 bool m_closed: 1; /* set when socket is closed */
116 bool m_duped: 1; /* set if duped BSOCK */
117 bool m_spool: 1; /* set for spooling */
118 bool m_compress: 1; /* set to use comm line compression */
119 bool m_use_locking; /* set to use locking (out of a bitfield */
120 /* to avoid race conditions) */
122 int64_t m_bwlimit; /* set to limit bandwidth */
123 int64_t m_nb_bytes; /* bytes sent/recv since the last tick */
124 btime_t m_last_tick; /* last tick used by bwlimit */
125 uint64_t m_CommBytes; /* Bytes sent */
126 uint64_t m_CommCompressedBytes; /* Compressed bytes sent */
128 void fin_init(JCR * jcr, int sockfd, const char *who, const char *host, int port,
129 struct sockaddr *lclient_addr);
130 bool open(JCR *jcr, const char *name, char *host, char *service,
131 int port, utime_t heart_beat, int *fatal);
132 void master_lock() const { if (m_use_locking) pP((&m_mmutex)); };
133 void master_unlock() const { if (m_use_locking) pV((&m_mmutex)); };
136 BSOCK *m_master; /* "this" or the "parent" BSOCK if duped */
137 /* methods -- in bsock.c */
140 bool connect(JCR * jcr, int retry_interval, utime_t max_retry_time,
141 utime_t heart_beat, const char *name, char *host,
142 char *service, int port, int verbose);
144 bool send() { return send(0); };
145 bool send(int flags);
146 bool fsend(const char*, ...);
147 bool signal(int signal);
148 void close(); /* close connection and destroy packet */
149 void _destroy(); /* called by destroy() */
150 void destroy(); /* destroy socket packet */
151 bool comm_compress(); /* in bsock.c */
152 const char *bstrerror(); /* last error on socket */
153 int get_peer(char *buf, socklen_t buflen);
154 bool despool(void update_attr_spool_size(ssize_t size), ssize_t tsize);
155 bool set_buffer_size(uint32_t size, int rw);
156 int set_nonblocking();
158 void restore_blocking(int flags);
159 void set_killable(bool killable);
160 int wait_data(int sec, int msec=0);
161 int wait_data_intr(int sec, int msec=0);
162 bool authenticate_director(const char *name, const char *password,
163 TLS_CONTEXT *tls_ctx, char *response, int response_len);
164 bool set_locking(); /* in bsock.c */
165 void clear_locking(); /* in bsock.c */
166 void set_source_address(dlist *src_addr_list);
167 void control_bwlimit(int bytes); /* in bsock.c */
169 /* Inline functions */
170 void suppress_error_messages(bool flag) { m_suppress_error_msgs = flag; };
171 void set_jcr(JCR *jcr) { m_jcr = jcr; };
172 void set_who(char *who) { m_who = who; };
173 void set_host(char *host) { m_host = host; };
174 void set_port(int port) { m_port = port; };
175 char *who() const { return m_who; };
176 char *host() const { return m_host; };
177 int port() const { return m_port; };
178 JCR *jcr() const { return m_jcr; };
179 JCR *get_jcr() const { return m_jcr; };
180 bool is_spooling() const { return m_spool; };
181 bool is_duped() const { return m_duped; };
182 bool is_terminated() const { return m_terminated; };
183 bool is_timed_out() const { return m_timed_out; };
184 bool is_closed() const { return m_closed; };
185 bool is_open() const { return !m_closed; };
186 bool is_stop() const { return errors || is_terminated() || is_closed(); };
187 bool is_error() { errno = b_errno; return errors; };
188 bool can_compress() const { return m_compress; };
189 void set_data_end(int32_t FileIndex) {
190 if (m_spool && FileIndex > m_FileIndex) {
191 m_lastFileIndex = m_FileIndex;
192 m_last_data_end = m_data_end;
193 m_FileIndex = FileIndex;
194 m_data_end = ftello(m_spool_fd);
197 boffset_t get_last_data_end() { return m_last_data_end; };
198 int32_t get_lastFileIndex() { return m_lastFileIndex; };
199 uint32_t CommBytes() { return m_CommBytes; };
200 uint32_t CommCompressedBytes() { return m_CommCompressedBytes; };
201 void set_bwlimit(int64_t maxspeed) { m_bwlimit = maxspeed; };
202 bool use_bwlimit() { return m_bwlimit > 0;};
203 void set_spooling() { m_spool = true; };
204 void clear_spooling() { m_spool = false; };
205 void set_compress() { m_compress = true; };
206 void clear_compress() { m_compress = false; };
207 void set_duped() { m_duped = true; };
208 void set_master(BSOCK *master) { master_lock(); m_master = master; m_next = master->m_next; master->m_next = this; master_unlock(); };
209 void set_timed_out() { m_timed_out = true; };
210 void clear_timed_out() { m_timed_out = false; };
211 void set_terminated() { m_terminated = true; };
212 void set_closed() { m_closed = true; };
213 void start_timer(int sec) { m_tid = start_bsock_timer(this, sec); };
214 void stop_timer() { stop_bsock_timer(m_tid); };
216 void install_send_hook_cb(BSOCKCallback *obj) { send_hook_cb=obj; };
217 void uninstall_send_hook_cb() { send_hook_cb=NULL; };
218 void cancel(); /* call it when JCR is canceled */
223 * Signal definitions for use in bsock->signal()
224 * Note! These must be negative. There are signals that are generated
225 * by the bsock software not by the OS ...
228 BNET_EOD = -1, /* End of data stream, new data may follow */
229 BNET_EOD_POLL = -2, /* End of data and poll all in one */
230 BNET_STATUS = -3, /* Send full status */
231 BNET_TERMINATE = -4, /* Conversation terminated, doing close() */
232 BNET_POLL = -5, /* Poll request, I'm hanging on a read */
233 BNET_HEARTBEAT = -6, /* Heartbeat Response requested */
234 BNET_HB_RESPONSE = -7, /* Only response permited to HB */
235 BNET_xxxxxxPROMPT = -8, /* No longer used -- Prompt for subcommand */
236 BNET_BTIME = -9, /* Send UTC btime */
237 BNET_BREAK = -10, /* Stop current command -- ctl-c */
238 BNET_START_SELECT = -11, /* Start of a selection list */
239 BNET_END_SELECT = -12, /* End of a select list */
240 BNET_INVALID_CMD = -13, /* Invalid command sent */
241 BNET_CMD_FAILED = -14, /* Command failed */
242 BNET_CMD_OK = -15, /* Command succeeded */
243 BNET_CMD_BEGIN = -16, /* Start command execution */
244 BNET_MSGS_PENDING = -17, /* Messages pending */
245 BNET_MAIN_PROMPT = -18, /* Server ready and waiting */
246 BNET_SELECT_INPUT = -19, /* Return selection input */
247 BNET_WARNING_MSG = -20, /* Warning message */
248 BNET_ERROR_MSG = -21, /* Error message -- command failed */
249 BNET_INFO_MSG = -22, /* Info message -- status line */
250 BNET_RUN_CMD = -23, /* Run command follows */
251 BNET_YESNO = -24, /* Request yes no response */
252 BNET_START_RTREE = -25, /* Start restore tree mode */
253 BNET_END_RTREE = -26, /* End restore tree mode */
254 BNET_SUB_PROMPT = -27, /* Indicate we are at a subprompt */
255 BNET_TEXT_INPUT = -28, /* Get text input from user */
256 BNET_EXT_TERMINATE = -29, /* A Terminate condition has been met and
257 already reported somewhere else */
258 BNET_FDCALLED = -30 /* The FD should keep the connection for a new job */
262 * These bits ares set in the packet length field. Attempt to
263 * keep the number of bits to a minimum and instead use the new
264 * flag field for passing bits using the BNET_HDR_EXTEND bit.
265 * Note: we must not set the high bit as that indicates a signal.
267 #define BNET_COMPRESSED (1<<30) /* set for lz4 compressed data */
268 #define BNET_HDR_EXTEND (1<<29) /* extended header */
271 * The following bits are kept in flags. The high 16 bits are
272 * for flags, and the low 16 bits are for other info such as
273 * compressed data offset (BNET_OFFSET)
275 #define BNET_IS_CMD (1<<28) /* set for command data */
276 #define BNET_OFFSET (1<<27) /* Data compression offset specified */
277 #define BNET_NOCOMPRESS (1<<25) /* Disable compression */
278 #define BNET_DATACOMPRESSED (1<<24) /* Data compression */
280 #define BNET_SETBUF_READ 1 /* Arg for bnet_set_buffer_size */
281 #define BNET_SETBUF_WRITE 2 /* Arg for bnet_set_buffer_size */
284 * Return status from bnet_recv()
285 * Note, the HARDEOF and ERROR refer to comm status/problems
286 * rather than the BNET_xxx above, which are software signals.
296 * Inter-daemon commands
297 * When BNET_IS_CMD is on, the next int32 is a command
299 #define BNET_CMD_SIZE sizeof(int32_t)
302 BNET_CMD_NONE = 0, /* reserved */
303 BNET_CMD_ACK_HASH = 1, /* backup SD->FD SD already know this hash, don't need the block */
304 BNET_CMD_UNK_HASH = 2, /* restore SD->FD hash is unknown */
305 BNET_CMD_GET_HASH = 3, /* backup SD->FD SD ask FD to send the corresponding block */
306 /* restore FD->SD FD ask SD to send the corresponding block */
307 BNET_CMD_STO_BLOCK = 4, /* backup FD->SD FD send requested block */
308 BNET_CMD_REC_ACK = 5, /* restore FD->SD FD has consumed records from the buffer */
309 BNET_CMD_STP_THREAD = 6, /* restore FD->SD SD must stop thread */
310 BNET_CMD_STP_FLOWCTRL = 7, /* backup FD->SD SD must stop sending flowcontrol information */
313 const char *bnet_cmd_to_name(int val);
316 * TLS enabling values. Value is important for comparison, ie:
317 * if (tls_remote_need < BNET_TLS_REQUIRED) { ... }
320 BNET_TLS_NONE = 0, /* cannot do TLS */
321 BNET_TLS_OK = 1, /* can do, but not required on my end */
322 BNET_TLS_REQUIRED = 2 /* TLS is required */
325 int32_t read_nbytes(BSOCK * bsock, char *ptr, int32_t nbytes);
326 int32_t write_nbytes(BSOCK * bsock, char *ptr, int32_t nbytes);
330 * Completely release the socket packet, and NULL the pointer
332 #define free_bsock(a) do{if(a){(a)->close(); (a)->destroy(); (a)=NULL;}} while(0)
335 * Does the socket exist and is it open?
337 #define is_bsock_open(a) ((a) && (a)->is_open())
339 #endif /* __BSOCK_H_ */