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