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