1 /*
2    Bacula(R) - The Network Backup Solution
3 
4    Copyright (C) 2000-2020 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 /*
21  * Written by Eric Bollengier MMXVIII
22  */
23 
24 /* Code to handle Bacula Client behind a NAT without a VPN
25  * We keep a connection idle with a short TCP keepalive to detect
26  * any problem very quickly.
27  *
28  * A thread can be responsible to listen all sockets and reply to
29  * a ISALIVE request. A ISALIVE request can be send after a wake up
30  * for example.
31  */
32 
33 #include "bacula.h"
34 #include "bsock_meeting.h"
35 
36 static int dbglvl = DT_NETWORK|50;
37 
BsockMeeting()38 BsockMeeting::BsockMeeting(): socket(NULL), keepidle(0), keepintvl(0) {
39    pthread_mutex_init(&mutex, NULL);
40    pthread_cond_init(&cond, NULL);
41 };
42 
~BsockMeeting()43 BsockMeeting::~BsockMeeting() {
44    pthread_mutex_destroy(&mutex);
45    pthread_cond_destroy(&cond);
46    free_bsock(socket);
47 };
48 
set(BSOCK * s)49 void BsockMeeting::set(BSOCK *s) {
50    int turnon=1;
51    P(mutex);
52    free_bsock(socket);          /* We are the owner of the socket object, we can use free */
53    socket = s;
54 
55    /*
56     * Keep socket from timing out from inactivity
57     */
58    if (setsockopt(socket->m_fd, SOL_SOCKET, SO_KEEPALIVE, (sockopt_val_t)&turnon, sizeof(turnon)) < 0) {
59       berrno be;
60       Dmsg1(dbglvl, _("Cannot set SO_KEEPALIVE on socket: %s\n"), be.bstrerror());
61    }
62 #if defined(TCP_KEEPIDLE)
63    if (getsockopt(socket->m_fd, SOL_TCP, TCP_KEEPIDLE, (sockopt_val_t)&keepidle, sizeof(keepidle)) < 0) {
64       berrno be;
65       Dmsg1(dbglvl, _("Cannot get TCP_KEEPIDLE on socket: %s\n"), be.bstrerror());
66    }
67    opt = 10000;
68    if (setsockopt(socket->m_fd, SOL_TCP, TCP_KEEPIDLE, (sockopt_val_t)&opt, sizeof(opt)) < 0) {
69       berrno be;
70       Dmsg1(dbglvl, _("Cannot set TCP_KEEPIDLE on socket: %s\n"), be.bstrerror());
71    }
72 #endif
73 #if defined(TCP_KEEPINTVL)
74    if (getsockopt(socket->m_fd, SOL_TCP, TCP_KEEPINTVL, (sockopt_val_t)&keepintvl, sizeof(keepintvl)) < 0) {
75       berrno be;
76       Dmsg1(dbglvl, _("Cannot get TCP_KEEPINTVL on socket: %s\n"), be.bstrerror());
77    }
78    opt = 5000;
79    if (setsockopt(socket->m_fd, SOL_TCP, TCP_KEEPINTVL, (sockopt_val_t)&opt, sizeof(opt)) < 0) {
80       berrno be;
81       Dmsg1(dbglvl, _("Cannot set TCP_KEEPINTVL on socket: %s\n"), be.bstrerror());
82    }
83 #endif
84    pthread_cond_signal(&cond);
85    V(mutex);
86 };
87 
88 /* Get the current Socket, wait timeout to get it */
get(int timeout)89 BSOCK *BsockMeeting::get(int timeout) {
90    BSOCK *ret=NULL;
91    struct timespec to;
92    btimer_t *t;
93    int32_t sig=0;
94 
95    P(mutex);
96    to.tv_sec = time(NULL) + timeout;
97    to.tv_nsec = 0;
98    while (socket == NULL) {
99       Dmsg0(dbglvl, "socket is null...\n");
100       int err = pthread_cond_timedwait(&cond, &mutex, &to);
101       if (err == ETIMEDOUT) {
102          Dmsg0(dbglvl, "Timeout\n");
103          /* timeout, do something */
104          break;
105       } else {
106          Dmsg2(dbglvl, "timedwait=%d socket=%p\n", err, socket);
107       }
108    }
109    /* TODO: We need to test if the socket is still valid, and we need a read() to raise
110     * an error. We don't want to give a broken socket to the job
111     */
112    if (socket) {
113       Dmsg0(dbglvl, "Found a socket in the proxy\n");
114       t = start_bsock_timer(socket, 10);
115       socket->signal(BNET_ISALIVE);
116       sig = socket->recv();
117       stop_bsock_timer(t);
118 
119       if (sig != -1 || socket->msglen != BNET_ISALIVE) {
120          Dmsg2(dbglvl, "Socket seems broken sig=%d msglen=%d\n", sig, socket->msglen);
121          free_bsock(socket);
122          V(mutex);           /* Let's get a new one */
123          return get(timeout);
124       }
125       Dmsg0(dbglvl, "Socket seems OK\n");
126    }
127    if (socket) {
128 #if defined(TCP_KEEPIDLE)
129       if (setsockopt(socket->m_fd, SOL_TCP, TCP_KEEPIDLE, (sockopt_val_t)&keepidle, sizeof(keepidle)) < 0) {
130          berrno be;
131          Dmsg1(dbglvl, _("Cannot set TCP_KEEPIDLE on socket: %s\n"), be.bstrerror());
132       }
133 #endif
134 #if defined(TCP_KEEPINTVL)
135       if (setsockopt(socket->m_fd, SOL_TCP, TCP_KEEPINTVL, (sockopt_val_t)&keepintvl, sizeof(keepintvl)) < 0) {
136          berrno be;
137          Dmsg1(dbglvl, _("Cannot set TCP_KEEPINTVL on socket: %s\n"), be.bstrerror());
138       }
139 #endif
140    }
141    ret = socket;
142    socket = NULL;
143    V(mutex);
144    return ret;
145 };
146 
is_set(POOLMEM * & address)147 bool BsockMeeting::is_set(POOLMEM *&address)
148 {
149    lock_guard g(mutex);
150    if (address) {
151       *address = 0;
152    }
153    if (!socket) {
154       return false;
155    }
156    if (address) {
157       pm_strcpy(address, socket->host());
158    }
159    return true;
160 }
161 
wait_request(BSOCK * dir)162 void BsockMeeting::wait_request(BSOCK *dir) {
163    int32_t sig = dir->recv();
164    if (sig == -1 && dir->msglen == BNET_ISALIVE) {
165       /* nop */
166       dir->signal(BNET_ISALIVE);
167    } else {
168       Dmsg1(dbglvl, "got incorrect message sig=%d\n", sig);
169       dir->close();
170    }
171 };
172