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 * Subroutines to receive network data and handle
21 * network signals for the FD and the SD.
23 * Kern Sibbald, May MMI previously in src/stored/fdmsg.c
30 static char OK_msg[] = "2000 OK\n";
31 static char TERM_msg[] = "2999 Terminate\n";
36 * This routine does a bnet_recv(), then if a signal was
37 * sent, it handles it. The return codes are the same as
38 * bne_recv() except the BNET_SIGNAL messages that can
39 * be handled are done so without returning.
41 * Returns number of bytes read (may return zero)
42 * Returns -1 on signal (BNET_SIGNAL)
43 * Returns -2 on hard end of file (BNET_HARDEOF)
44 * Returns -3 on error (BNET_ERROR)
45 * Returns -4 on Command (BNET_COMMAND)
47 int bget_msg(BSOCK *sock)
52 if (n >= 0) { /* normal return */
55 if (sock->is_stop()) { /* error return */
58 if (n == BNET_COMMAND) {
62 /* BNET_SIGNAL (-1) return from bnet_recv() => network signal */
63 switch (sock->msglen) {
64 case BNET_EOD: /* end of data */
65 Dmsg0(msglvl, "Got BNET_EOD\n");
68 Dmsg0(msglvl, "Got BNET_EOD_POLL\n");
69 if (sock->is_terminated()) {
70 sock->fsend(TERM_msg);
72 sock->fsend(OK_msg); /* send response */
74 return n; /* end of data */
76 Dmsg0(msglvl, "Got BNET_TERMINATE\n");
77 sock->set_terminated();
80 Dmsg0(msglvl, "Got BNET_POLL\n");
81 if (sock->is_terminated()) {
82 sock->fsend(TERM_msg);
84 sock->fsend(OK_msg); /* send response */
88 case BNET_HB_RESPONSE:
91 /* *****FIXME***** Implement BNET_STATUS */
92 Dmsg0(msglvl, "Got BNET_STATUS\n");
93 sock->fsend(_("Status OK\n"));
94 sock->signal(BNET_EOD);
97 Emsg1(M_ERROR, 0, _("bget_msg: unknown signal %d\n"), sock->msglen);
103 bmessage::bmessage(int bufsize)
105 msg = get_pool_memory(PM_BSOCK);
106 msg = realloc_pool_memory(msg, bufsize);
107 status = bmessage::bm_busy;
111 bmessage::~bmessage()
113 free_pool_memory(msg);
116 void bmessage::swap(BSOCK *sock)
118 POOLMEM *swap = sock->msg;
123 GetMsg::GetMsg(JCR *a_jcr, BSOCK *a_bsock, const char *a_rec_header, int32_t a_bufsize):
126 rec_header(a_rec_header),
133 jcr->inc_use_count(); /* We own a copy of the JCR */
134 bmsg_aux = New(bmessage(bufsize));
136 pthread_mutex_init(&mutex, 0);
137 pthread_cond_init(&cond, NULL);
142 free_jcr(jcr); /* Release our copy of the JCR */
144 pthread_mutex_destroy(&mutex);
145 pthread_cond_destroy(&cond);
148 int GetMsg::bget_msg(bmessage **pbmsg)
150 // Get our own local copy of the socket
155 bmessage *bmsg = *pbmsg;
156 bmsg->ret = ::bget_msg(bsock);
157 bmsg->status = bmessage::bm_ready;
158 bmsg->rbuflen = bmsg->msglen = bmsg->origlen = bsock->msglen;
159 /* bmsg->is_header = !bmsg->is_header; ALAIN SAYS: I think this line is useless */
160 /* swap msg instead of copying */
162 bmsg->rbuf = bmsg->msg;
164 msglen = bmsg->msglen;
166 m_is_stop = bsock->is_stop() || bsock->is_error();