]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/bget_msg.c
Backport from BEE
[bacula/bacula] / bacula / src / lib / bget_msg.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2001-2014 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from many
7    others, a complete list can be found in the file AUTHORS.
8
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.
13
14    Bacula® is a registered trademark of Kern Sibbald.
15 */
16 /*
17  *  Subroutines to receive network data and handle
18  *   network signals for the FD and the SD.
19  *
20  *   Kern Sibbald, May MMI previously in src/stored/fdmsg.c
21  *
22  */
23
24 #include "bacula.h"                   /* pull in global headers */
25
26 static char OK_msg[]   = "2000 OK\n";
27 static char TERM_msg[] = "2999 Terminate\n";
28
29 #define msglvl 500
30
31 /*
32  * This routine does a bnet_recv(), then if a signal was
33  *   sent, it handles it.  The return codes are the same as
34  *   bne_recv() except the BNET_SIGNAL messages that can
35  *   be handled are done so without returning.
36  *
37  * Returns number of bytes read (may return zero)
38  * Returns -1 on signal (BNET_SIGNAL)
39  * Returns -2 on hard end of file (BNET_HARDEOF)
40  * Returns -3 on error  (BNET_ERROR)
41  */
42 int bget_msg(BSOCK *sock)
43 {
44    int n;
45    for ( ;; ) {
46       n = sock->recv();
47       if (n >= 0) {                  /* normal return */
48          return n;
49       }
50       if (sock->is_stop()) {         /* error return */
51          return n;
52       }
53
54       /* BNET_SIGNAL (-1) return from bnet_recv() => network signal */
55       switch (sock->msglen) {
56       case BNET_EOD:               /* end of data */
57          Dmsg0(msglvl, "Got BNET_EOD\n");
58          return n;
59       case BNET_EOD_POLL:
60          Dmsg0(msglvl, "Got BNET_EOD_POLL\n");
61          if (sock->is_terminated()) {
62             sock->fsend(TERM_msg);
63          } else {
64             sock->fsend(OK_msg); /* send response */
65          }
66          return n;                 /* end of data */
67       case BNET_TERMINATE:
68          Dmsg0(msglvl, "Got BNET_TERMINATE\n");
69          sock->set_terminated();
70          return n;
71       case BNET_POLL:
72          Dmsg0(msglvl, "Got BNET_POLL\n");
73          if (sock->is_terminated()) {
74             sock->fsend(TERM_msg);
75          } else {
76             sock->fsend(OK_msg); /* send response */
77          }
78          break;
79       case BNET_HEARTBEAT:
80       case BNET_HB_RESPONSE:
81          break;
82       case BNET_STATUS:
83          /* *****FIXME***** Implement BNET_STATUS */
84          Dmsg0(msglvl, "Got BNET_STATUS\n");
85          sock->fsend(_("Status OK\n"));
86          sock->signal(BNET_EOD);
87          break;
88       default:
89          Emsg1(M_ERROR, 0, _("bget_msg: unknown signal %d\n"), sock->msglen);
90          break;
91       }
92    }
93 }