forked from FernetMenta/vdr-plugin-vnsiserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vnsiserver.c
215 lines (183 loc) · 5.1 KB
/
vnsiserver.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* vdr-plugin-vnsi - KODI server plugin for VDR
*
* Copyright (C) 2010 Alwin Esch (Team XBMC)
* Copyright (C) 2010, 2011 Alexander Pipelka
* Copyright (C) 2015 Team KODI
*
* http://kodi.tv
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KODI; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "vnsiserver.h"
#include "vnsiclient.h"
#include "vnsi.h"
#include "channelfilter.h"
#include <netdb.h>
#include <poll.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <fcntl.h>
#include <errno.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <vdr/plugin.h>
unsigned int cVNSIServer::m_IdCnt = 0;
class cAllowedHosts : public cSVDRPhosts
{
public:
cAllowedHosts(const cString& AllowedHostsFile)
{
if (!Load(AllowedHostsFile, true, true))
{
ERRORLOG("Invalid or missing '%s'. falling back to 'svdrphosts.conf'.", *AllowedHostsFile);
cString Base = cString::sprintf("%s/../svdrphosts.conf", *VNSIServerConfig.ConfigDirectory);
if (!Load(Base, true, true))
{
ERRORLOG("Invalid or missing %s. Adding 127.0.0.1 to list of allowed hosts.", *Base);
cSVDRPhost *localhost = new cSVDRPhost;
if (localhost->Parse("127.0.0.1"))
Add(localhost);
else
delete localhost;
}
}
}
};
cVNSIServer::cVNSIServer(int listenPort) : cThread("VDR VNSI Server")
{
m_ServerPort = listenPort;
Start();
INFOLOG("VNSI Server started");
INFOLOG("Channel streaming timeout: %i seconds", VNSIServerConfig.stream_timeout);
return;
}
cVNSIServer::~cVNSIServer()
{
m_Status.Shutdown();
Cancel();
INFOLOG("VNSI Server stopped");
}
void cVNSIServer::NewClientConnected(int fd)
{
char buf[64];
struct sockaddr_in sin;
socklen_t len = sizeof(sin);
if (getpeername(fd, (struct sockaddr *)&sin, &len))
{
ERRORLOG("getpeername() failed, dropping new incoming connection %d", m_IdCnt);
close(fd);
return;
}
cAllowedHosts AllowedHosts(m_AllowedHostsFile);
if (!AllowedHosts.Acceptable(sin.sin_addr.s_addr))
{
ERRORLOG("Address not allowed to connect (%s)", *m_AllowedHostsFile);
close(fd);
return;
}
if (fcntl(fd, F_SETFL, fcntl (fd, F_GETFL) | O_NONBLOCK) == -1)
{
ERRORLOG("Error setting control socket to nonblocking mode");
close(fd);
return;
}
int val = 1;
setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
#ifdef SOL_TCP
val = 30;
setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &val, sizeof(val));
val = 15;
setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &val, sizeof(val));
val = 5;
setsockopt(fd, SOL_TCP, TCP_KEEPCNT, &val, sizeof(val));
val = 1;
setsockopt(fd, SOL_TCP, TCP_NODELAY, &val, sizeof(val));
#endif
INFOLOG("Client with ID %d connected: %s", m_IdCnt, cxSocket::ip2txt(sin.sin_addr.s_addr, sin.sin_port, buf));
cVNSIClient *connection = new cVNSIClient(fd, m_IdCnt, cxSocket::ip2txt(sin.sin_addr.s_addr, sin.sin_port, buf));
m_Status.AddClient(connection);
m_IdCnt++;
}
void cVNSIServer::Action(void)
{
fd_set fds;
struct timeval tv;
if(*VNSIServerConfig.ConfigDirectory)
{
m_AllowedHostsFile = cString::sprintf("%s/" ALLOWED_HOSTS_FILE, *VNSIServerConfig.ConfigDirectory);
}
else
{
ERRORLOG("cVNSIServer: missing ConfigDirectory!");
m_AllowedHostsFile = cString::sprintf("/video/" ALLOWED_HOSTS_FILE);
}
VNSIChannelFilter.Load();
VNSIChannelFilter.SortChannels();
m_Status.Start();
m_ServerFD = socket(AF_INET, SOCK_STREAM, 0);
if(m_ServerFD == -1)
return;
fcntl(m_ServerFD, F_SETFD, fcntl(m_ServerFD, F_GETFD) | FD_CLOEXEC);
int one = 1;
setsockopt(m_ServerFD, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int));
struct sockaddr_in s;
memset(&s, 0, sizeof(s));
s.sin_family = AF_INET;
s.sin_port = htons(m_ServerPort);
int x = bind(m_ServerFD, (struct sockaddr *)&s, sizeof(s));
if (x < 0)
{
close(m_ServerFD);
INFOLOG("Unable to start VNSI Server, port already in use ?");
m_ServerFD = -1;
return;
}
listen(m_ServerFD, 10);
while (Running())
{
FD_ZERO(&fds);
FD_SET(m_ServerFD, &fds);
tv.tv_sec = 0;
tv.tv_usec = 250*1000;
int r = select(m_ServerFD + 1, &fds, NULL, NULL, &tv);
if (r == -1)
{
ERRORLOG("failed during select");
continue;
}
if (r == 0)
{
continue;
}
int fd = accept(m_ServerFD, 0, 0);
if (fd >= 0)
{
NewClientConnected(fd);
}
else
{
ERRORLOG("accept failed");
}
}
return;
}