1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2001-2011 Free Software Foundation Europe e.V.
5    Copyright (C) 2016-2019 Bareos GmbH & Co. KG
6 
7    This program is Free Software; you can redistribute it and/or
8    modify it under the terms of version three of the GNU Affero General Public
9    License as published by the Free Software Foundation and included
10    in the file LICENSE.
11 
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15    Affero General Public License for more details.
16 
17    You should have received a copy of the GNU Affero General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20    02110-1301, USA.
21 */
22 /*
23  * Kern Sibbald, May MMI previously in src/stored/fdmsg.c
24  */
25 /**
26  * @file
27  * Subroutines to receive network data and handle
28  * network signals for the FD and the SD.
29  */
30 
31 #include "include/bareos.h" /* pull in global headers */
32 #include "lib/bnet.h"
33 #include "lib/bget_msg.h"
34 #include "lib/bsock.h"
35 
36 static char OK_msg[] = "2000 OK\n";
37 static char TERM_msg[] = "2999 Terminate\n";
38 
39 #define messagelevel 500
40 
41 /**
42  * This routine does a BnetRecv(), then if a signal was
43  *   sent, it handles it.  The return codes are the same as
44  *   bne_recv() except the BNET_SIGNAL messages that can
45  *   be handled are done so without returning.
46  *
47  * Returns number of bytes read (may return zero)
48  * Returns -1 on signal (BNET_SIGNAL)
49  * Returns -2 on hard end of file (BNET_HARDEOF)
50  * Returns -3 on error  (BNET_ERROR)
51  */
BgetMsg(BareosSocket * sock)52 int BgetMsg(BareosSocket* sock)
53 {
54   int n;
55   for (;;) {
56     n = sock->recv();
57     if (n >= 0) { /* normal return */
58       return n;
59     }
60     if (IsBnetStop(sock)) { /* error return */
61       return n;
62     }
63 
64     /* BNET_SIGNAL (-1) return from BnetRecv() => network signal */
65     switch (sock->message_length) {
66       case BNET_EOD: /* end of data */
67         Dmsg0(messagelevel, "Got BNET_EOD\n");
68         return n;
69       case BNET_EOD_POLL:
70         Dmsg0(messagelevel, "Got BNET_EOD_POLL\n");
71         if (sock->IsTerminated()) {
72           sock->fsend(TERM_msg);
73         } else {
74           sock->fsend(OK_msg); /* send response */
75         }
76         return n; /* end of data */
77       case BNET_TERMINATE:
78         Dmsg0(messagelevel, "Got BNET_TERMINATE\n");
79         sock->SetTerminated();
80         return n;
81       case BNET_POLL:
82         Dmsg0(messagelevel, "Got BNET_POLL\n");
83         if (sock->IsTerminated()) {
84           sock->fsend(TERM_msg);
85         } else {
86           sock->fsend(OK_msg); /* send response */
87         }
88         break;
89       case BNET_HEARTBEAT:
90       case BNET_HB_RESPONSE:
91         break;
92       case BNET_STATUS:
93         /* *****FIXME***** Implement BNET_STATUS */
94         Dmsg0(messagelevel, "Got BNET_STATUS\n");
95         sock->fsend(_("Status OK\n"));
96         sock->signal(BNET_EOD);
97         break;
98       default:
99         Emsg1(M_ERROR, 0, _("BgetMsg: unknown signal %d\n"),
100               sock->message_length);
101         break;
102     }
103   }
104 }
105