]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/bget_msg.c
Implement RestoreObject for sqlite + cleanups
[bacula/bacula] / bacula / src / lib / bget_msg.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2001-2011 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  *  Subroutines to receive network data and handle
30  *   network signals for the FD and the SD.
31  *
32  *   Kern Sibbald, May MMI previously in src/stored/fdmsg.c
33  *
34  */
35
36 #include "bacula.h"                   /* pull in global headers */
37
38 static char OK_msg[]   = "2000 OK\n";
39 static char TERM_msg[] = "2999 Terminate\n";
40
41 #define msglvl 500
42
43 /*
44  * This routine does a bnet_recv(), then if a signal was
45  *   sent, it handles it.  The return codes are the same as
46  *   bne_recv() except the BNET_SIGNAL messages that can
47  *   be handled are done so without returning.
48  *
49  * Returns number of bytes read (may return zero)
50  * Returns -1 on signal (BNET_SIGNAL)
51  * Returns -2 on hard end of file (BNET_HARDEOF)
52  * Returns -3 on error  (BNET_ERROR)
53  */
54 int bget_msg(BSOCK *sock)
55 {
56    int n;
57    for ( ;; ) {
58       n = sock->recv();
59       if (n >= 0) {                  /* normal return */
60          return n;
61       }
62       if (is_bnet_stop(sock)) {      /* error return */
63          return n;
64       }
65
66       /* BNET_SIGNAL (-1) return from bnet_recv() => network signal */
67       switch (sock->msglen) {
68       case BNET_EOD:               /* end of data */
69          Dmsg0(msglvl, "Got BNET_EOD\n");
70          return n;
71       case BNET_EOD_POLL:
72          Dmsg0(msglvl, "Got BNET_EOD_POLL\n");
73          if (sock->is_terminated()) {
74             sock->fsend(TERM_msg);
75          } else {
76             sock->fsend(OK_msg); /* send response */
77          }
78          return n;                 /* end of data */
79       case BNET_TERMINATE:
80          Dmsg0(msglvl, "Got BNET_TERMINATE\n");
81          sock->set_terminated();
82          return n;
83       case BNET_POLL:
84          Dmsg0(msglvl, "Got BNET_POLL\n");
85          if (sock->is_terminated()) {
86             sock->fsend(TERM_msg);
87          } else {
88             sock->fsend(OK_msg); /* send response */
89          }
90          break;
91       case BNET_HEARTBEAT:
92       case BNET_HB_RESPONSE:
93          break;
94       case BNET_STATUS:
95          /* *****FIXME***** Implement BNET_STATUS */
96          Dmsg0(msglvl, "Got BNET_STATUS\n");
97          sock->fsend(_("Status OK\n"));
98          sock->signal(BNET_EOD);
99          break;
100       default:
101          Emsg1(M_ERROR, 0, _("bget_msg: unknown signal %d\n"), sock->msglen);
102          break;
103       }
104    }
105 }