1 | #include "globals.h"
|
---|
2 | #include "oscam-client.h"
|
---|
3 | #include "oscam-failban.h"
|
---|
4 | #include "oscam-lock.h"
|
---|
5 | #include "oscam-net.h"
|
---|
6 | #include "oscam-string.h"
|
---|
7 | #include "oscam-time.h"
|
---|
8 | #include "oscam-work.h"
|
---|
9 |
|
---|
10 | extern CS_MUTEX_LOCK gethostbyname_lock;
|
---|
11 | extern int32_t exit_oscam;
|
---|
12 |
|
---|
13 | #ifndef IPV6SUPPORT
|
---|
14 | static int32_t inet_byteorder = 0;
|
---|
15 |
|
---|
16 | static in_addr_t cs_inet_order(in_addr_t n)
|
---|
17 | {
|
---|
18 | if(!inet_byteorder)
|
---|
19 | { inet_byteorder = (inet_addr("1.2.3.4") + 1 == inet_addr("1.2.3.5")) ? 1 : 2; }
|
---|
20 | switch(inet_byteorder)
|
---|
21 | {
|
---|
22 | case 1:
|
---|
23 | break;
|
---|
24 | case 2:
|
---|
25 | n = ((n & 0xff000000) >> 24) |
|
---|
26 | ((n & 0x00ff0000) >> 8) |
|
---|
27 | ((n & 0x0000ff00) << 8) |
|
---|
28 | ((n & 0x000000ff) << 24);
|
---|
29 | break;
|
---|
30 | }
|
---|
31 | return n;
|
---|
32 | }
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | char *cs_inet_ntoa(IN_ADDR_T addr)
|
---|
36 | {
|
---|
37 | #ifdef IPV6SUPPORT
|
---|
38 | static char buff[INET6_ADDRSTRLEN];
|
---|
39 | if(IN6_IS_ADDR_V4MAPPED(&addr) || IN6_IS_ADDR_V4COMPAT(&addr))
|
---|
40 | {
|
---|
41 | snprintf(buff, sizeof(buff), "%d.%d.%d.%d",
|
---|
42 | addr.s6_addr[12], addr.s6_addr[13], addr.s6_addr[14], addr.s6_addr[15]);
|
---|
43 | }
|
---|
44 | else
|
---|
45 | {
|
---|
46 | inet_ntop(AF_INET6, &(addr.s6_addr), buff, INET6_ADDRSTRLEN);
|
---|
47 | }
|
---|
48 | return buff;
|
---|
49 | #else
|
---|
50 | struct in_addr in;
|
---|
51 | in.s_addr = addr;
|
---|
52 | return (char *)inet_ntoa(in);
|
---|
53 | #endif
|
---|
54 | }
|
---|
55 |
|
---|
56 | void cs_inet_addr(char *txt, IN_ADDR_T *out)
|
---|
57 | {
|
---|
58 | #ifdef IPV6SUPPORT
|
---|
59 | char buff[INET6_ADDRSTRLEN];
|
---|
60 | //trying as IPv6 address
|
---|
61 | if(inet_pton(AF_INET6, txt, out->s6_addr) == 0)
|
---|
62 | {
|
---|
63 | //now trying as mapped IPv4
|
---|
64 | snprintf(buff, sizeof(buff), "::ffff:%s", txt);
|
---|
65 | inet_pton(AF_INET6, buff, out->s6_addr);
|
---|
66 | }
|
---|
67 | #else
|
---|
68 | *out = inet_addr(txt);
|
---|
69 | #endif
|
---|
70 | }
|
---|
71 |
|
---|
72 | void cs_resolve(const char *hostname, IN_ADDR_T *ip, struct SOCKADDR *sock, socklen_t *sa_len)
|
---|
73 | {
|
---|
74 | #ifdef IPV6SUPPORT
|
---|
75 | cs_getIPv6fromHost(hostname, ip, sock, sa_len);
|
---|
76 | #else
|
---|
77 | *ip = cs_getIPfromHost(hostname);
|
---|
78 | if(sa_len)
|
---|
79 | { *sa_len = sizeof(*sock); }
|
---|
80 | #endif
|
---|
81 | }
|
---|
82 |
|
---|
83 | #ifdef IPV6SUPPORT
|
---|
84 | int32_t cs_in6addr_equal(struct in6_addr *a1, struct in6_addr *a2)
|
---|
85 | {
|
---|
86 | return memcmp(a1, a2, 16) == 0;
|
---|
87 | }
|
---|
88 |
|
---|
89 | int32_t cs_in6addr_lt(struct in6_addr *a, struct in6_addr *b)
|
---|
90 | {
|
---|
91 | int i;
|
---|
92 | for(i = 0; i < 4; i++)
|
---|
93 | {
|
---|
94 | if((i == 2) && ((IN6_IS_ADDR_V4COMPAT(a) && IN6_IS_ADDR_V4MAPPED(b)) ||
|
---|
95 | (IN6_IS_ADDR_V4COMPAT(b) && IN6_IS_ADDR_V4MAPPED(a))))
|
---|
96 | { continue; } //skip comparing this part
|
---|
97 |
|
---|
98 | if(a->s6_addr32[i] != b->s6_addr32[i])
|
---|
99 | { return ntohl(a->s6_addr32[i]) < ntohl(b->s6_addr32[i]); }
|
---|
100 | }
|
---|
101 |
|
---|
102 | return 0;
|
---|
103 | }
|
---|
104 |
|
---|
105 | int32_t cs_in6addr_isnull(struct in6_addr *addr)
|
---|
106 | {
|
---|
107 | int i;
|
---|
108 | for(i = 0; i < 16; i++)
|
---|
109 | if(addr->s6_addr[i])
|
---|
110 | { return 0; }
|
---|
111 | return 1;
|
---|
112 | }
|
---|
113 |
|
---|
114 | void cs_in6addr_copy(struct in6_addr *dst, struct in6_addr *src)
|
---|
115 | {
|
---|
116 | memcpy(dst, src, 16);
|
---|
117 | }
|
---|
118 |
|
---|
119 | void cs_in6addr_ipv4map(struct in6_addr *dst, in_addr_t src)
|
---|
120 | {
|
---|
121 | memset(dst->s6_addr, 0, 16);
|
---|
122 | dst->s6_addr[10] = 0xff;
|
---|
123 | dst->s6_addr[11] = 0xff;
|
---|
124 | memcpy(dst->s6_addr + 12, &src, 4);
|
---|
125 | }
|
---|
126 | #endif
|
---|
127 |
|
---|
128 | IN_ADDR_T get_null_ip(void)
|
---|
129 | {
|
---|
130 | IN_ADDR_T ip;
|
---|
131 | #ifdef IPV6SUPPORT
|
---|
132 | cs_inet_addr("::", &ip);
|
---|
133 | #else
|
---|
134 | ip = 0;
|
---|
135 | #endif
|
---|
136 | return ip;
|
---|
137 | }
|
---|
138 |
|
---|
139 | void set_null_ip(IN_ADDR_T *ip)
|
---|
140 | {
|
---|
141 | #ifdef IPV6SUPPORT
|
---|
142 | cs_inet_addr("::", ip);
|
---|
143 | #else
|
---|
144 | *ip = 0;
|
---|
145 | #endif
|
---|
146 | }
|
---|
147 |
|
---|
148 | void set_localhost_ip(IN_ADDR_T *ip)
|
---|
149 | {
|
---|
150 | #ifdef IPV6SUPPORT
|
---|
151 | cs_inet_addr("::1", ip);
|
---|
152 | #else
|
---|
153 | cs_inet_addr("127.0.0.1", ip);
|
---|
154 | #endif
|
---|
155 | }
|
---|
156 |
|
---|
157 | int32_t check_ip(struct s_ip *ip, IN_ADDR_T n)
|
---|
158 | {
|
---|
159 | struct s_ip *p_ip;
|
---|
160 | int32_t ok = 0;
|
---|
161 | #ifdef IPV6SUPPORT
|
---|
162 | for(p_ip = ip; (p_ip) && (!ok); p_ip = p_ip->next)
|
---|
163 | {
|
---|
164 | ok = cs_in6addr_lt(&n, &p_ip->ip[0]);
|
---|
165 | ok |= cs_in6addr_lt(&p_ip->ip[1], &n);
|
---|
166 | ok = !ok;
|
---|
167 | }
|
---|
168 | #else
|
---|
169 | for(p_ip = ip; (p_ip) && (!ok); p_ip = p_ip->next)
|
---|
170 | { ok = ((cs_inet_order(n) >= cs_inet_order(p_ip->ip[0])) && (cs_inet_order(n) <= cs_inet_order(p_ip->ip[1]))); }
|
---|
171 | #endif
|
---|
172 | return ok;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /* Returns the ip from the given hostname. If gethostbyname is configured in the config file, a lock
|
---|
176 | will be held until the ip has been resolved. */
|
---|
177 | uint32_t cs_getIPfromHost(const char *hostname)
|
---|
178 | {
|
---|
179 | uint32_t result = 0;
|
---|
180 | //Resolve with gethostbyname:
|
---|
181 | if(cfg.resolve_gethostbyname)
|
---|
182 | {
|
---|
183 | cs_writelock(&gethostbyname_lock);
|
---|
184 | struct hostent *rht = gethostbyname(hostname);
|
---|
185 | if(!rht)
|
---|
186 | { cs_log("can't resolve %s", hostname); }
|
---|
187 | else
|
---|
188 | { result = ((struct in_addr *)rht->h_addr)->s_addr; }
|
---|
189 | cs_writeunlock(&gethostbyname_lock);
|
---|
190 | }
|
---|
191 | else //Resolve with getaddrinfo:
|
---|
192 | {
|
---|
193 | struct addrinfo hints, *res = NULL;
|
---|
194 | memset(&hints, 0, sizeof(hints));
|
---|
195 | hints.ai_socktype = SOCK_STREAM;
|
---|
196 | hints.ai_family = AF_INET;
|
---|
197 | hints.ai_protocol = IPPROTO_TCP;
|
---|
198 |
|
---|
199 | int32_t err = getaddrinfo(hostname, NULL, &hints, &res);
|
---|
200 | if(err != 0 || !res || !res->ai_addr)
|
---|
201 | {
|
---|
202 | cs_log("can't resolve %s, error: %s", hostname, err ? gai_strerror(err) : "unknown");
|
---|
203 | }
|
---|
204 | else
|
---|
205 | {
|
---|
206 | result = ((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr;
|
---|
207 | }
|
---|
208 | if(res) { freeaddrinfo(res); }
|
---|
209 | }
|
---|
210 | return result;
|
---|
211 | }
|
---|
212 |
|
---|
213 | #ifdef IPV6SUPPORT
|
---|
214 | void cs_getIPv6fromHost(const char *hostname, struct in6_addr *addr, struct sockaddr_storage *sa, socklen_t *sa_len)
|
---|
215 | {
|
---|
216 | uint32_t ipv4addr = 0;
|
---|
217 | struct addrinfo hints, *res = NULL;
|
---|
218 | memset(&hints, 0, sizeof(hints));
|
---|
219 | hints.ai_socktype = SOCK_STREAM;
|
---|
220 | hints.ai_family = AF_UNSPEC;
|
---|
221 | hints.ai_protocol = IPPROTO_TCP;
|
---|
222 | int32_t err = getaddrinfo(hostname, NULL, &hints, &res);
|
---|
223 | if(err != 0 || !res || !res->ai_addr)
|
---|
224 | {
|
---|
225 | cs_log("can't resolve %s, error: %s", hostname, err ? gai_strerror(err) : "unknown");
|
---|
226 | }
|
---|
227 | else
|
---|
228 | {
|
---|
229 | ipv4addr = ((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr;
|
---|
230 | if(res->ai_family == AF_INET)
|
---|
231 | { cs_in6addr_ipv4map(addr, ipv4addr); }
|
---|
232 | else
|
---|
233 | { IP_ASSIGN(*addr, SIN_GET_ADDR(*res->ai_addr)); }
|
---|
234 | if(sa)
|
---|
235 | { memcpy(sa, res->ai_addr, res->ai_addrlen); }
|
---|
236 | if(sa_len)
|
---|
237 | { *sa_len = res->ai_addrlen; }
|
---|
238 | }
|
---|
239 | if(res)
|
---|
240 | { freeaddrinfo(res); }
|
---|
241 | }
|
---|
242 | #endif
|
---|
243 |
|
---|
244 | int set_socket_priority(int fd, int priority)
|
---|
245 | {
|
---|
246 | #ifdef SO_PRIORITY
|
---|
247 | return priority ? setsockopt(fd, SOL_SOCKET, SO_PRIORITY, (void *)&priority, sizeof(int *)) : -1;
|
---|
248 | #else
|
---|
249 | (void)fd;
|
---|
250 | (void)priority;
|
---|
251 | return -1;
|
---|
252 | #endif
|
---|
253 | }
|
---|
254 |
|
---|
255 | void setTCPTimeouts(int32_t sock)
|
---|
256 | {
|
---|
257 | int32_t flag = 1;
|
---|
258 | // this is not only for a real keepalive but also to detect closed connections so it should not be configurable
|
---|
259 | if(setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &flag, sizeof(flag)) && errno != EBADF)
|
---|
260 | {
|
---|
261 | cs_log("Setting SO_KEEPALIVE failed, errno=%d, %s", errno, strerror(errno));
|
---|
262 | }
|
---|
263 | #if defined(TCP_KEEPIDLE) && defined(TCP_KEEPCNT) && defined(TCP_KEEPINTVL)
|
---|
264 | flag = 10;
|
---|
265 | if(setsockopt(sock, IPPROTO_TCP, TCP_KEEPIDLE, &flag, sizeof(flag)) && errno != EBADF) //send first keepalive packet after 10 seconds of last package received (keepalive packets included)
|
---|
266 | {
|
---|
267 | cs_log("Setting TCP_KEEPIDLE failed, errno=%d, %s", errno, strerror(errno));
|
---|
268 | }
|
---|
269 | flag = 3;
|
---|
270 | if(setsockopt(sock, IPPROTO_TCP, TCP_KEEPCNT, &flag, sizeof(flag)) && errno != EBADF) //send up to 3 keepalive packets out (in interval TCP_KEEPINTVL), then disconnect if no response
|
---|
271 | {
|
---|
272 | cs_log("Setting TCP_KEEPCNT failed, errno=%d, %s", errno, strerror(errno));
|
---|
273 | }
|
---|
274 | flag = 1;
|
---|
275 | if(setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &flag, sizeof(flag)) && errno != EBADF)
|
---|
276 | {
|
---|
277 | ; //send a keepalive packet out every second (until answer has been received or TCP_KEEPCNT has been reached)
|
---|
278 | cs_log("Setting TCP_KEEPINTVL failed, errno=%d, %s", errno, strerror(errno));
|
---|
279 | }
|
---|
280 | #endif
|
---|
281 | struct timeval tv;
|
---|
282 | tv.tv_sec = 60;
|
---|
283 | tv.tv_usec = 0;
|
---|
284 | if(setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(struct timeval)) && errno != EBADF)
|
---|
285 | {
|
---|
286 | ;
|
---|
287 | cs_log("Setting SO_SNDTIMEO failed, errno=%d, %s", errno, strerror(errno));
|
---|
288 | }
|
---|
289 | tv.tv_sec = 600;
|
---|
290 | tv.tv_usec = 0;
|
---|
291 | if(setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval)) && errno != EBADF)
|
---|
292 | {
|
---|
293 | ;
|
---|
294 | cs_log("Setting SO_RCVTIMEO failed, errno=%d, %s", errno, strerror(errno));
|
---|
295 | }
|
---|
296 | #if defined(TCP_USER_TIMEOUT)
|
---|
297 | int timeout = 60000; // RFC 5482 user timeout in milliseconds
|
---|
298 | setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT, (char *) &timeout, sizeof(timeout));
|
---|
299 | #endif
|
---|
300 | }
|
---|
301 |
|
---|
302 | int set_nonblock(int32_t fd, bool nonblock)
|
---|
303 | {
|
---|
304 | int32_t flags = fcntl(fd, F_GETFL);
|
---|
305 | if (flags == -1)
|
---|
306 | return -1;
|
---|
307 | if (nonblock)
|
---|
308 | flags |= O_NONBLOCK;
|
---|
309 | else
|
---|
310 | flags &= (~O_NONBLOCK);
|
---|
311 | return fcntl(fd, F_SETFL, flags);
|
---|
312 | }
|
---|
313 |
|
---|
314 | int8_t check_fd_for_data(int32_t fd)
|
---|
315 | {
|
---|
316 | int32_t rc;
|
---|
317 | struct pollfd pfd[1];
|
---|
318 |
|
---|
319 | pfd[0].fd = fd;
|
---|
320 | pfd[0].events = (POLLIN | POLLPRI);
|
---|
321 | rc = poll(pfd, 1, 0);
|
---|
322 |
|
---|
323 | if(rc == -1)
|
---|
324 | { cs_log("check_fd_for_data(fd=%d) failed: (errno=%d %s)", fd, errno, strerror(errno)); }
|
---|
325 |
|
---|
326 | if(rc == -1 || rc == 0)
|
---|
327 | { return rc; }
|
---|
328 |
|
---|
329 | if(pfd[0].revents & (POLLHUP | POLLNVAL | POLLERR))
|
---|
330 | { return -2; }
|
---|
331 |
|
---|
332 | return 1;
|
---|
333 | }
|
---|
334 |
|
---|
335 | int32_t recv_from_udpipe(uint8_t *buf)
|
---|
336 | {
|
---|
337 | uint16_t n;
|
---|
338 | if(buf[0] != 'U')
|
---|
339 | {
|
---|
340 | cs_log("INTERNAL PIPE-ERROR");
|
---|
341 | cs_exit(1);
|
---|
342 | }
|
---|
343 | memcpy(&n, buf + 1, 2);
|
---|
344 | memmove(buf, buf + 3, n);
|
---|
345 | return n;
|
---|
346 | }
|
---|
347 |
|
---|
348 | int32_t process_input(uint8_t *buf, int32_t buflen, int32_t timeout)
|
---|
349 | {
|
---|
350 | int32_t rc, i, pfdcount, polltime, timeoutms;
|
---|
351 | struct pollfd pfd[2];
|
---|
352 | struct s_client *cl = cur_client();
|
---|
353 |
|
---|
354 | struct timeb starttime;
|
---|
355 | struct timeb currenttime;
|
---|
356 | timeoutms = 1000 * timeout;
|
---|
357 | cs_ftime(&starttime);
|
---|
358 | polltime = timeoutms; // initial polltime = timeoutms
|
---|
359 | while(1)
|
---|
360 | {
|
---|
361 | pfdcount = 0;
|
---|
362 | if(cl->pfd)
|
---|
363 | {
|
---|
364 | pfd[pfdcount].fd = cl->pfd;
|
---|
365 | pfd[pfdcount++].events = POLLIN | POLLPRI;
|
---|
366 | }
|
---|
367 | int32_t p_rc = poll(pfd, pfdcount, polltime);
|
---|
368 |
|
---|
369 | cs_ftime(¤ttime);
|
---|
370 | int32_t gone = comp_timeb(¤ttime, &starttime);
|
---|
371 | polltime = timeoutms - gone; // calculate polltime left
|
---|
372 | if(polltime < 0)
|
---|
373 | {
|
---|
374 | polltime = 0;
|
---|
375 | }
|
---|
376 | if(p_rc < 0)
|
---|
377 | {
|
---|
378 | if(errno == EINTR)
|
---|
379 | { continue; }
|
---|
380 | else
|
---|
381 | { return 0; }
|
---|
382 | }
|
---|
383 |
|
---|
384 | if((p_rc == 0) && (timeout != 0) && (gone >= timeoutms)) // client maxidle reached? timeout = 0, idle disconnect disabled
|
---|
385 | {
|
---|
386 | rc = -9;
|
---|
387 | break;
|
---|
388 | }
|
---|
389 |
|
---|
390 | for(i = 0; i < pfdcount && p_rc > 0; i++)
|
---|
391 | {
|
---|
392 | if(pfd[i].revents & POLLHUP) // POLLHUP is only valid in revents so it doesn't need to be set above in events
|
---|
393 | {
|
---|
394 | return 0;
|
---|
395 | }
|
---|
396 | if(!(pfd[i].revents & (POLLIN | POLLPRI)))
|
---|
397 | { continue; }
|
---|
398 |
|
---|
399 | if(pfd[i].fd == cl->pfd)
|
---|
400 | { return get_module(cl)->recv(cl, buf, buflen); }
|
---|
401 | }
|
---|
402 | }
|
---|
403 | return rc;
|
---|
404 | }
|
---|
405 |
|
---|
406 | static struct s_client *find_client_by_ip(IN_ADDR_T ip, in_port_t port)
|
---|
407 | {
|
---|
408 | struct s_client *cl;
|
---|
409 | for(cl = first_client; cl; cl = cl->next)
|
---|
410 | {
|
---|
411 | if(!cl->kill &&
|
---|
412 | IP_EQUAL(cl->ip, ip) && cl->port == port &&
|
---|
413 | (cl->typ == 'c' || cl->typ == 'm'))
|
---|
414 | {
|
---|
415 | return cl;
|
---|
416 | }
|
---|
417 | }
|
---|
418 | return NULL;
|
---|
419 | }
|
---|
420 |
|
---|
421 | int32_t accept_connection(struct s_module *module, int8_t module_idx, int8_t port_idx)
|
---|
422 | {
|
---|
423 | struct SOCKADDR cad;
|
---|
424 | int32_t scad = sizeof(cad), n;
|
---|
425 | struct s_client *cl;
|
---|
426 | struct s_port *port = &module->ptab.ports[port_idx];
|
---|
427 |
|
---|
428 | if(module->type == MOD_CONN_UDP)
|
---|
429 | {
|
---|
430 | uchar *buf;
|
---|
431 | if(!cs_malloc(&buf, 1024))
|
---|
432 | { return -1; }
|
---|
433 | if((n = recvfrom(port->fd, buf + 3, 1024 - 3, 0, (struct sockaddr *)&cad, (socklen_t *)&scad)) > 0)
|
---|
434 | {
|
---|
435 | uint16_t rl;
|
---|
436 | cl = find_client_by_ip(SIN_GET_ADDR(cad), ntohs(SIN_GET_PORT(cad)));
|
---|
437 | rl = n;
|
---|
438 | buf[0] = 'U';
|
---|
439 | memcpy(buf + 1, &rl, 2);
|
---|
440 |
|
---|
441 | if(cs_check_violation(SIN_GET_ADDR(cad), port->s_port))
|
---|
442 | {
|
---|
443 | NULLFREE(buf);
|
---|
444 | return 0;
|
---|
445 | }
|
---|
446 |
|
---|
447 | cs_debug_mask(D_TRACE, "got %d bytes on port %d from ip %s:%d client %s",
|
---|
448 | n, port->s_port,
|
---|
449 | cs_inet_ntoa(SIN_GET_ADDR(cad)), SIN_GET_PORT(cad),
|
---|
450 | username(cl));
|
---|
451 |
|
---|
452 | if(!cl)
|
---|
453 | {
|
---|
454 | cl = create_client(SIN_GET_ADDR(cad));
|
---|
455 | if(!cl) { return 0; }
|
---|
456 |
|
---|
457 | cl->module_idx = module_idx;
|
---|
458 | cl->port_idx = port_idx;
|
---|
459 | cl->udp_fd = port->fd;
|
---|
460 | cl->udp_sa = cad;
|
---|
461 | cl->udp_sa_len = sizeof(cl->udp_sa);
|
---|
462 |
|
---|
463 | cl->port = ntohs(SIN_GET_PORT(cad));
|
---|
464 | cl->typ = 'c';
|
---|
465 |
|
---|
466 | add_job(cl, ACTION_CLIENT_INIT, NULL, 0);
|
---|
467 | }
|
---|
468 | add_job(cl, ACTION_CLIENT_UDP, buf, n + 3);
|
---|
469 | }
|
---|
470 | else
|
---|
471 | { NULLFREE(buf); }
|
---|
472 | }
|
---|
473 | else //TCP
|
---|
474 | {
|
---|
475 | int32_t pfd3;
|
---|
476 | if((pfd3 = accept(port->fd, (struct sockaddr *)&cad, (socklen_t *)&scad)) > 0)
|
---|
477 | {
|
---|
478 |
|
---|
479 | if(cs_check_violation(SIN_GET_ADDR(cad), port->s_port))
|
---|
480 | {
|
---|
481 | close(pfd3);
|
---|
482 | return 0;
|
---|
483 | }
|
---|
484 |
|
---|
485 | cl = create_client(SIN_GET_ADDR(cad));
|
---|
486 | if(cl == NULL)
|
---|
487 | {
|
---|
488 | close(pfd3);
|
---|
489 | return 0;
|
---|
490 | }
|
---|
491 |
|
---|
492 | int32_t flag = 1;
|
---|
493 | setsockopt(pfd3, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag));
|
---|
494 | setTCPTimeouts(pfd3);
|
---|
495 |
|
---|
496 | cl->module_idx = module_idx;
|
---|
497 | cl->udp_fd = pfd3;
|
---|
498 | cl->port_idx = port_idx;
|
---|
499 |
|
---|
500 | cl->pfd = pfd3;
|
---|
501 | cl->port = ntohs(SIN_GET_PORT(cad));
|
---|
502 | cl->typ = 'c';
|
---|
503 |
|
---|
504 | add_job(cl, ACTION_CLIENT_INIT, NULL, 0);
|
---|
505 | }
|
---|
506 | }
|
---|
507 | return 0;
|
---|
508 | }
|
---|
509 |
|
---|
510 | int32_t start_listener(struct s_module *module, struct s_port *port)
|
---|
511 | {
|
---|
512 | int32_t ov = 1, timeout, is_udp, i;
|
---|
513 | char ptxt[2][32];
|
---|
514 | struct SOCKADDR sad; // structure to hold server's address
|
---|
515 | socklen_t sad_len;
|
---|
516 |
|
---|
517 | ptxt[0][0] = ptxt[1][0] = '\0';
|
---|
518 | if(!port->s_port)
|
---|
519 | {
|
---|
520 | cs_debug_mask(D_TRACE, "%s: disabled", module->desc);
|
---|
521 | return 0;
|
---|
522 | }
|
---|
523 | is_udp = (module->type == MOD_CONN_UDP);
|
---|
524 |
|
---|
525 | memset(&sad, 0 , sizeof(sad));
|
---|
526 | #ifdef IPV6SUPPORT
|
---|
527 | SIN_GET_FAMILY(sad) = AF_INET6;
|
---|
528 | SIN_GET_ADDR(sad) = in6addr_any;
|
---|
529 | sad_len = sizeof(struct sockaddr_in6);
|
---|
530 | #else
|
---|
531 | sad.sin_family = AF_INET;
|
---|
532 | sad_len = sizeof(struct sockaddr);
|
---|
533 | if(!module->s_ip)
|
---|
534 | { module->s_ip = cfg.srvip; }
|
---|
535 | if(module->s_ip)
|
---|
536 | {
|
---|
537 | sad.sin_addr.s_addr = module->s_ip;
|
---|
538 | snprintf(ptxt[0], sizeof(ptxt[0]), ", ip=%s", inet_ntoa(sad.sin_addr));
|
---|
539 | }
|
---|
540 | else
|
---|
541 | {
|
---|
542 | sad.sin_addr.s_addr = INADDR_ANY;
|
---|
543 | }
|
---|
544 | #endif
|
---|
545 | timeout = cfg.bindwait;
|
---|
546 | port->fd = 0;
|
---|
547 |
|
---|
548 | if(port->s_port > 0) // test for illegal value
|
---|
549 | {
|
---|
550 | SIN_GET_PORT(sad) = htons((uint16_t)port->s_port);
|
---|
551 | }
|
---|
552 | else
|
---|
553 | {
|
---|
554 | cs_log("%s: Bad port %d", module->desc, port->s_port);
|
---|
555 | return 0;
|
---|
556 | }
|
---|
557 |
|
---|
558 | int s_domain = PF_INET;
|
---|
559 | #ifdef IPV6SUPPORT
|
---|
560 | s_domain = PF_INET6;
|
---|
561 | #endif
|
---|
562 | int s_type = (is_udp ? SOCK_DGRAM : SOCK_STREAM);
|
---|
563 | int s_proto = (is_udp ? IPPROTO_UDP : IPPROTO_TCP);
|
---|
564 |
|
---|
565 | if((port->fd = socket(s_domain, s_type, s_proto)) < 0)
|
---|
566 | {
|
---|
567 | cs_log("%s: Cannot create socket (errno=%d: %s)", module->desc, errno, strerror(errno));
|
---|
568 | #ifdef IPV6SUPPORT
|
---|
569 | cs_log("%s: Trying fallback to IPv4", module->desc);
|
---|
570 | s_domain = PF_INET;
|
---|
571 | if((port->fd = socket(s_domain, s_type, s_proto)) < 0)
|
---|
572 | {
|
---|
573 | cs_log("%s: Cannot create socket (errno=%d: %s)", module->desc, errno, strerror(errno));
|
---|
574 | return 0;
|
---|
575 | }
|
---|
576 | #else
|
---|
577 | return 0;
|
---|
578 | #endif
|
---|
579 | }
|
---|
580 |
|
---|
581 | #ifdef IPV6SUPPORT
|
---|
582 | // azbox toolchain do not have this define
|
---|
583 | #ifndef IPV6_V6ONLY
|
---|
584 | #define IPV6_V6ONLY 26
|
---|
585 | #endif
|
---|
586 | // set the server socket option to listen on IPv4 and IPv6 simultaneously
|
---|
587 | int val = 0;
|
---|
588 | if(setsockopt(port->fd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&val, sizeof(val)) < 0)
|
---|
589 | {
|
---|
590 | cs_log("%s: setsockopt(IPV6_V6ONLY) failed (errno=%d: %s)", module->desc, errno, strerror(errno));
|
---|
591 | }
|
---|
592 | #endif
|
---|
593 |
|
---|
594 | ov = 1;
|
---|
595 | if(setsockopt(port->fd, SOL_SOCKET, SO_REUSEADDR, (void *)&ov, sizeof(ov)) < 0)
|
---|
596 | {
|
---|
597 | cs_log("%s: setsockopt failed (errno=%d: %s)", module->desc, errno, strerror(errno));
|
---|
598 | close(port->fd);
|
---|
599 | port->fd = 0;
|
---|
600 | return 0;
|
---|
601 | }
|
---|
602 |
|
---|
603 | #ifdef SO_REUSEPORT
|
---|
604 | setsockopt(port->fd, SOL_SOCKET, SO_REUSEPORT, (void *)&ov, sizeof(ov));
|
---|
605 | #endif
|
---|
606 |
|
---|
607 | if(set_socket_priority(port->fd, cfg.netprio) > -1)
|
---|
608 | { snprintf(ptxt[1], sizeof(ptxt[1]), ", prio=%d", cfg.netprio); }
|
---|
609 |
|
---|
610 | if(!is_udp)
|
---|
611 | {
|
---|
612 | int32_t keep_alive = 1;
|
---|
613 | setsockopt(port->fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&keep_alive, sizeof(keep_alive));
|
---|
614 | }
|
---|
615 |
|
---|
616 | while(timeout-- && !exit_oscam)
|
---|
617 | {
|
---|
618 | if(bind(port->fd, (struct sockaddr *)&sad, sad_len) < 0)
|
---|
619 | {
|
---|
620 | if(timeout)
|
---|
621 | {
|
---|
622 | cs_log("%s: Bind request failed (%s), waiting another %d seconds",
|
---|
623 | module->desc, strerror(errno), timeout);
|
---|
624 | cs_sleepms(1000);
|
---|
625 | }
|
---|
626 | else
|
---|
627 | {
|
---|
628 | cs_log("%s: Bind request failed (%s), giving up", module->desc, strerror(errno));
|
---|
629 | close(port->fd);
|
---|
630 | port->fd = 0;
|
---|
631 | return 0;
|
---|
632 | }
|
---|
633 | }
|
---|
634 | else
|
---|
635 | {
|
---|
636 | timeout = 0;
|
---|
637 | }
|
---|
638 | }
|
---|
639 |
|
---|
640 | if(!is_udp)
|
---|
641 | {
|
---|
642 | if(listen(port->fd, CS_QLEN) < 0)
|
---|
643 | {
|
---|
644 | cs_log("%s: Cannot start listen mode (errno=%d: %s)", module->desc, errno, strerror(errno));
|
---|
645 | close(port->fd);
|
---|
646 | port->fd = 0;
|
---|
647 | return 0;
|
---|
648 | }
|
---|
649 | }
|
---|
650 |
|
---|
651 | cs_log("%s: initialized (fd=%d, port=%d%s%s)",
|
---|
652 | module->desc, port->fd,
|
---|
653 | port->s_port,
|
---|
654 | ptxt[0], ptxt[1]);
|
---|
655 |
|
---|
656 | for(i = 0; port->ncd && i < port->ncd->ncd_ftab.nfilts; i++)
|
---|
657 | {
|
---|
658 | int32_t j, pos = 0;
|
---|
659 | char buf[30 + (8 * port->ncd->ncd_ftab.filts[i].nprids)];
|
---|
660 | pos += snprintf(buf, sizeof(buf), "-> CAID: %04X PROVID: ", port->ncd->ncd_ftab.filts[i].caid);
|
---|
661 |
|
---|
662 | for(j = 0; j < port->ncd->ncd_ftab.filts[i].nprids; j++)
|
---|
663 | { pos += snprintf(buf + pos, sizeof(buf) - pos, "%06X, ", port->ncd->ncd_ftab.filts[i].prids[j]); }
|
---|
664 |
|
---|
665 | if(pos > 2 && j > 0)
|
---|
666 | { buf[pos - 2] = '\0'; }
|
---|
667 |
|
---|
668 | cs_log("%s", buf);
|
---|
669 | }
|
---|
670 |
|
---|
671 | return port->fd;
|
---|
672 | }
|
---|