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