]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/bget_msg.c
crypto: convert EVP_PKEY access and remainings bits for OpenSSL 1.1
[bacula/bacula] / bacula / src / lib / bget_msg.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2017 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many 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    This notice must be preserved when any source code is 
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  *  Subroutines to receive network data and handle
21  *   network signals for the FD and the SD.
22  *
23  *   Kern Sibbald, May MMI previously in src/stored/fdmsg.c
24  *
25  */
26
27 #include "bacula.h"
28 #include "jcr.h"
29
30 static char OK_msg[]   = "2000 OK\n";
31 static char TERM_msg[] = "2999 Terminate\n";
32
33 #define msglvl 500
34
35 /*
36  * This routine does a bnet_recv(), then if a signal was
37  *   sent, it handles it.  The return codes are the same as
38  *   bne_recv() except the BNET_SIGNAL messages that can
39  *   be handled are done so without returning.
40  *
41  * Returns number of bytes read (may return zero)
42  * Returns -1 on signal (BNET_SIGNAL)
43  * Returns -2 on hard end of file (BNET_HARDEOF)
44  * Returns -3 on error  (BNET_ERROR)
45  * Returns -4 on Command (BNET_COMMAND)
46  */
47 int bget_msg(BSOCK *sock)
48 {
49    int n;
50    for ( ;; ) {
51       n = sock->recv();
52       if (n >= 0) {                  /* normal return */
53          return n;
54       }
55       if (sock->is_stop()) {         /* error return */
56          return n;
57       }
58       if (n == BNET_COMMAND) {
59          return n;
60       }
61
62       /* BNET_SIGNAL (-1) return from bnet_recv() => network signal */
63       switch (sock->msglen) {
64       case BNET_EOD:               /* end of data */
65          Dmsg0(msglvl, "Got BNET_EOD\n");
66          return n;
67       case BNET_EOD_POLL:
68          Dmsg0(msglvl, "Got BNET_EOD_POLL\n");
69          if (sock->is_terminated()) {
70             sock->fsend(TERM_msg);
71          } else {
72             sock->fsend(OK_msg); /* send response */
73          }
74          return n;                 /* end of data */
75       case BNET_TERMINATE:
76          Dmsg0(msglvl, "Got BNET_TERMINATE\n");
77          sock->set_terminated();
78          return n;
79       case BNET_POLL:
80          Dmsg0(msglvl, "Got BNET_POLL\n");
81          if (sock->is_terminated()) {
82             sock->fsend(TERM_msg);
83          } else {
84             sock->fsend(OK_msg); /* send response */
85          }
86          break;
87       case BNET_HEARTBEAT:
88       case BNET_HB_RESPONSE:
89          break;
90       case BNET_STATUS:
91          /* *****FIXME***** Implement BNET_STATUS */
92          Dmsg0(msglvl, "Got BNET_STATUS\n");
93          sock->fsend(_("Status OK\n"));
94          sock->signal(BNET_EOD);
95          break;
96       default:
97          Emsg1(M_ERROR, 0, _("bget_msg: unknown signal %d\n"), sock->msglen);
98          break;
99       }
100    }
101 }
102
103 bmessage::bmessage(int bufsize)
104 {
105    msg = get_pool_memory(PM_BSOCK);
106    msg = realloc_pool_memory(msg, bufsize);
107    status = bmessage::bm_busy;
108    jobbytes = 0;
109 }
110
111 bmessage::~bmessage()
112 {
113    free_pool_memory(msg);
114 }
115
116 void bmessage::swap(BSOCK *sock)
117 {
118    POOLMEM *swap = sock->msg;
119    sock->msg = msg;
120    msg = swap;
121 }
122
123 GetMsg::GetMsg(JCR *a_jcr, BSOCK *a_bsock, const char *a_rec_header, int32_t a_bufsize):
124       jcr(a_jcr),
125       bsock(a_bsock),
126       rec_header(a_rec_header),
127       bufsize(a_bufsize),
128       m_is_stop(false),
129       m_is_done(false),
130       m_is_error(false),
131       m_use_count(1)
132 {
133    jcr->inc_use_count();        /* We own a copy of the JCR */
134    bmsg_aux = New(bmessage(bufsize));
135    bmsg = bmsg_aux;
136    pthread_mutex_init(&mutex, 0);
137    pthread_cond_init(&cond, NULL);
138 };
139
140 GetMsg::~GetMsg()
141 {
142    free_jcr(jcr);               /* Release our copy of the JCR */
143    delete bmsg_aux;
144    pthread_mutex_destroy(&mutex);
145    pthread_cond_destroy(&cond);
146 };
147
148 int GetMsg::bget_msg(bmessage **pbmsg)
149 {
150    // Get our own local copy of the socket
151
152    if (pbmsg == NULL) {
153       pbmsg = &bmsg_aux;
154    }
155    bmessage *bmsg = *pbmsg;
156    bmsg->ret = ::bget_msg(bsock);
157    bmsg->status = bmessage::bm_ready;
158    bmsg->rbuflen = bmsg->msglen = bmsg->origlen = bsock->msglen;
159 /*   bmsg->is_header = !bmsg->is_header; ALAIN SAYS: I think this line is useless */
160    /* swap msg instead of copying */
161    bmsg->swap(bsock);
162    bmsg->rbuf = bmsg->msg;
163
164    msglen = bmsg->msglen;
165    msg = bmsg->msg;
166    m_is_stop = bsock->is_stop() || bsock->is_error();
167    return bmsg->ret;
168 }