/
socket_server.c
132 lines (112 loc) · 4.02 KB
/
socket_server.c
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
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2000-2012 Free Software Foundation Europe e.V.
Copyright (C) 2014-2014 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.
*/
/*
* This file handles external connections made to the storage daemon.
*
* Kern Sibbald, May MMI
*
* Extracted from other source files by Marco van Wieringen, October 2014
*/
#include "bareos.h"
#include "stored.h"
/* Global variables */
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 seperate thread of the Storage daemon.
*
* Basic tasks done here:
* - If it was a connection from the FD, call handle_filed_connection()
* - If it was a connection from an other SD, call handle_stored_connection()
* - Otherwise it was a connection from the DIR, call handle_director_connection()
*/
static void *handle_connection_request(void *arg)
{
BSOCK *bs = (BSOCK *)arg;
char name[MAX_NAME_LENGTH];
char tbuf[MAX_TIME_LENGTH];
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->close();
return NULL;
}
/*
* Do a sanity check on the message received
*/
if (bs->msglen < MIN_MSG_LEN || bs->msglen > MAX_MSG_LEN) {
Dmsg1(000, "<filed: %s", bs->msg);
Emsg2(M_ERROR, 0, _("Invalid connection from %s. Len=%d\n"), bs->who(), bs->msglen);
bmicrosleep(5, 0); /* make user wait 5 seconds */
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 handle_filed_connection(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 handle_director_connection(bs);
}
void start_socket_server(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->get_port_host_order());
}
sock_fds = New(alist(10, not_owned_by_alist));
bnet_thread_server_tcp(addrs,
me->max_concurrent_jobs * 2 + 1,
sock_fds,
&socket_workq,
me->nokeepalive,
handle_connection_request);
}
void stop_socket_server()
{
if (sock_fds) {
bnet_stop_thread_server_tcp(tcp_server_tid);
cleanup_bnet_thread_server_tcp(sock_fds, &socket_workq);
delete sock_fds;
sock_fds = NULL;
}
}