]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/bget_msg.c
Eliminate dependency on man2html.
[bacula/bacula] / bacula / src / lib / bget_msg.c
1 /*
2  *  Subroutines to receive network data and handle
3  *   network signals for the FD and the SD.
4  *
5  *   Kern Sibbald, May MMI previously in src/stored/fdmsg.c
6  *
7  *   Version $Id$
8  *
9  */
10 /*
11    Copyright (C) 2001-2006 Kern Sibbald
12
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License
15    version 2 as amended with additional clauses defined in the
16    file LICENSE in the main source directory.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
21    the file LICENSE for additional details.
22
23  */
24
25 #include "bacula.h"                   /* pull in global headers */
26
27 static char OK_msg[]   = "2000 OK\n";
28 static char TERM_msg[] = "2999 Terminate\n";
29
30 #define msglvl 500
31
32 /*
33  * This routine does a bnet_recv(), then if a signal was
34  *   sent, it handles it.  The return codes are the same as
35  *   bne_recv() except the BNET_SIGNAL messages that can
36  *   be handled are done so without returning.
37  *
38  * Returns number of bytes read (may return zero)
39  * Returns -1 on signal (BNET_SIGNAL)
40  * Returns -2 on hard end of file (BNET_HARDEOF)
41  * Returns -3 on error  (BNET_ERROR)
42  */
43 int bget_msg(BSOCK *sock)
44 {
45    int n;
46    for ( ;; ) {
47       n = bnet_recv(sock);
48       if (n >= 0) {                  /* normal return */
49          return n;
50       }
51       if (is_bnet_stop(sock)) {      /* error return */
52          return n;
53       }
54
55       /* BNET_SIGNAL (-1) return from bnet_recv() => network signal */
56       switch (sock->msglen) {
57       case BNET_EOD:               /* end of data */
58          Dmsg0(msglvl, "Got BNET_EOD\n");
59          return n;
60       case BNET_EOD_POLL:
61          Dmsg0(msglvl, "Got BNET_EOD_POLL\n");
62          if (sock->terminated) {
63             bnet_fsend(sock, TERM_msg);
64          } else {
65             bnet_fsend(sock, OK_msg); /* send response */
66          }
67          return n;                 /* end of data */
68       case BNET_TERMINATE:
69          Dmsg0(msglvl, "Got BNET_TERMINATE\n");
70          sock->terminated = 1;
71          return n;
72       case BNET_POLL:
73          Dmsg0(msglvl, "Got BNET_POLL\n");
74          if (sock->terminated) {
75             bnet_fsend(sock, TERM_msg);
76          } else {
77             bnet_fsend(sock, OK_msg); /* send response */
78          }
79          break;
80       case BNET_HEARTBEAT:
81       case BNET_HB_RESPONSE:
82          break;
83       case BNET_STATUS:
84          /* *****FIXME***** Implement BNET_STATUS */
85          Dmsg0(msglvl, "Got BNET_STATUS\n");
86          bnet_fsend(sock, _("Status OK\n"));
87          bnet_sig(sock, BNET_EOD);
88          break;
89       default:
90          Emsg1(M_ERROR, 0, _("bget_msg: unknown signal %d\n"), sock->msglen);
91          break;
92       }
93    }
94 }