-
Notifications
You must be signed in to change notification settings - Fork 256
/
socket_server.cc
153 lines (130 loc) · 4.37 KB
/
socket_server.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2000-2012 Free Software Foundation Europe e.V.
Copyright (C) 2014-2016 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* Kern Sibbald, May MMI
* Extracted from other source files by Marco van Wieringen, October 2014
*/
/**
* @file
* This file handles external connections made to the storage daemon.
*/
#include "include/bareos.h"
#include "stored/autochanger.h"
#include "stored/stored.h"
#include "stored/stored_globals.h"
#include "stored/dir_cmd.h"
#include "stored/fd_cmds.h"
#include "stored/sd_cmds.h"
#include "lib/bnet_server_tcp.h"
#include "lib/bsock.h"
#include "lib/try_tls_handshake_as_a_server.h"
namespace storagedaemon {
static workq_t socket_workq;
static alist* sock_fds = NULL;
static pthread_t tcp_server_tid;
/**
* Sanity check for the lengths of the Hello messages.
*/
#define MIN_MSG_LEN 25
#define MAX_MSG_LEN (int)sizeof(name) + 30
/**
* Connection request. We accept connections either from the
* Director, Storage Daemon or a Client (File daemon).
*
* Note, we are running as a separate thread of the Storage daemon.
*
* Basic tasks done here:
* - If it was a connection from the FD, call HandleFiledConnection()
* - If it was a connection from another SD, call handle_stored_connection()
* - Otherwise it was a connection from the DIR, call
* handle_director_connection()
*/
void* HandleConnectionRequest(ConfigurationParser* config, void* arg)
{
BareosSocket* bs = (BareosSocket*)arg;
char name[MAX_NAME_LENGTH];
char tbuf[MAX_TIME_LENGTH];
if (!TryTlsHandshakeAsAServer(bs, config)) {
bs->signal(BNET_TERMINATE);
bs->close();
delete bs;
return nullptr;
}
if (bs->recv() <= 0) {
Emsg1(M_ERROR, 0, _("Connection request from %s failed.\n"), bs->who());
Bmicrosleep(5, 0); /* make user wait 5 seconds */
bs->signal(BNET_TERMINATE);
bs->close();
return NULL;
}
/*
* Do a sanity check on the message received
*/
if (bs->message_length < MIN_MSG_LEN || bs->message_length > MAX_MSG_LEN) {
Dmsg1(000, "<filed: %s", bs->msg);
Emsg2(M_ERROR, 0, _("Invalid connection from %s. Len=%d\n"), bs->who(),
bs->message_length);
Bmicrosleep(5, 0); /* make user wait 5 seconds */
bs->signal(BNET_TERMINATE);
bs->close();
return NULL;
}
Dmsg1(110, "Conn: %s", bs->msg);
/*
* See if this is a File daemon connection. If so call FD handler.
*/
if (sscanf(bs->msg, "Hello Start Job %127s", name) == 1) {
Dmsg1(110, "Got a FD connection at %s\n",
bstrftimes(tbuf, sizeof(tbuf), (utime_t)time(NULL)));
return HandleFiledConnection(bs, name);
}
/*
* See if this is a Storage daemon connection. If so call SD handler.
*/
if (sscanf(bs->msg, "Hello Start Storage Job %127s", name) == 1) {
Dmsg1(110, "Got a SD connection at %s\n",
bstrftimes(tbuf, sizeof(tbuf), (utime_t)time(NULL)));
return handle_stored_connection(bs, name);
}
Dmsg1(110, "Got a DIR connection at %s\n",
bstrftimes(tbuf, sizeof(tbuf), (utime_t)time(NULL)));
return HandleDirectorConnection(bs);
}
void StartSocketServer(dlist* addrs)
{
IPADDR* p;
tcp_server_tid = pthread_self();
/*
* Become server, and handle requests
*/
foreach_dlist (p, addrs) {
Dmsg1(10, "stored: listening on port %d\n", p->GetPortHostOrder());
}
sock_fds = new alist(10, not_owned_by_alist);
BnetThreadServerTcp(addrs, me->MaxConnections, sock_fds, &socket_workq,
me->nokeepalive, HandleConnectionRequest, my_config);
}
void StopSocketServer()
{
if (sock_fds) {
BnetStopAndWaitForThreadServerTcp(tcp_server_tid);
delete sock_fds;
sock_fds = NULL;
}
}
} /* namespace storagedaemon */