- Add support of ORF P4 Irdeto mode
[oscam.git] / oscam-net.c
blobe997b3f68dcd27d235e85c0055f30b204a59cb5b
1 #define MODULE_LOG_PREFIX "net"
3 #include "globals.h"
4 #include "oscam-client.h"
5 #include "oscam-failban.h"
6 #include "oscam-lock.h"
7 #include "oscam-net.h"
8 #include "oscam-string.h"
9 #include "oscam-time.h"
10 #include "oscam-work.h"
12 extern CS_MUTEX_LOCK gethostbyname_lock;
13 extern int32_t exit_oscam;
15 #ifndef IPV6SUPPORT
16 static int32_t inet_byteorder = 0;
18 static in_addr_t cs_inet_order(in_addr_t n)
20 if(!inet_byteorder)
21 { inet_byteorder = (inet_addr("1.2.3.4") + 1 == inet_addr("1.2.3.5")) ? 1 : 2; }
22 switch(inet_byteorder)
24 case 1:
25 break;
26 case 2:
27 n = ((n & 0xff000000) >> 24) |
28 ((n & 0x00ff0000) >> 8) |
29 ((n & 0x0000ff00) << 8) |
30 ((n & 0x000000ff) << 24);
31 break;
33 return n;
35 #endif
37 char *cs_inet_ntoa(IN_ADDR_T addr)
39 #ifdef IPV6SUPPORT
40 static char buff[INET6_ADDRSTRLEN];
41 if(IN6_IS_ADDR_V4MAPPED(&addr) || IN6_IS_ADDR_V4COMPAT(&addr))
43 snprintf(buff, sizeof(buff), "%d.%d.%d.%d",
44 addr.s6_addr[12], addr.s6_addr[13], addr.s6_addr[14], addr.s6_addr[15]);
46 else
48 inet_ntop(AF_INET6, &(addr.s6_addr), buff, INET6_ADDRSTRLEN);
50 return buff;
51 #else
52 struct in_addr in;
53 in.s_addr = addr;
54 return (char *)inet_ntoa(in);
55 #endif
58 void cs_inet_addr(char *txt, IN_ADDR_T *out)
60 #ifdef IPV6SUPPORT
61 char buff[INET6_ADDRSTRLEN];
62 //trying as IPv6 address
63 if(inet_pton(AF_INET6, txt, out->s6_addr) == 0)
65 //now trying as mapped IPv4
66 snprintf(buff, sizeof(buff), "::ffff:%s", txt);
67 inet_pton(AF_INET6, buff, out->s6_addr);
69 #else
70 *out = inet_addr(txt);
71 #endif
74 void cs_resolve(const char *hostname, IN_ADDR_T *ip, struct SOCKADDR *sock, socklen_t *sa_len)
76 #ifdef IPV6SUPPORT
77 cs_getIPv6fromHost(hostname, ip, sock, sa_len);
78 #else
79 *ip = cs_getIPfromHost(hostname);
80 if(sa_len)
81 { *sa_len = sizeof(*sock); }
82 #endif
85 #ifdef IPV6SUPPORT
86 int32_t cs_in6addr_equal(struct in6_addr *a1, struct in6_addr *a2)
88 return memcmp(a1, a2, 16) == 0;
91 int32_t cs_in6addr_lt(struct in6_addr *a, struct in6_addr *b)
93 int i;
94 for(i = 0; i < 4; i++)
96 if((i == 2) && ((IN6_IS_ADDR_V4COMPAT(a) && IN6_IS_ADDR_V4MAPPED(b)) ||
97 (IN6_IS_ADDR_V4COMPAT(b) && IN6_IS_ADDR_V4MAPPED(a))))
98 { continue; } //skip comparing this part
100 if(a->s6_addr32[i] != b->s6_addr32[i])
101 { return ntohl(a->s6_addr32[i]) < ntohl(b->s6_addr32[i]); }
104 return 0;
107 int32_t cs_in6addr_isnull(struct in6_addr *addr)
109 int i;
110 for(i = 0; i < 16; i++)
111 if(addr->s6_addr[i])
112 { return 0; }
113 return 1;
116 void cs_in6addr_copy(struct in6_addr *dst, struct in6_addr *src)
118 memcpy(dst, src, 16);
121 void cs_in6addr_ipv4map(struct in6_addr *dst, in_addr_t src)
123 memset(dst->s6_addr, 0, 16);
124 dst->s6_addr[10] = 0xff;
125 dst->s6_addr[11] = 0xff;
126 memcpy(dst->s6_addr + 12, &src, 4);
128 #endif
130 IN_ADDR_T get_null_ip(void)
132 IN_ADDR_T ip;
133 #ifdef IPV6SUPPORT
134 cs_inet_addr("::", &ip);
135 #else
136 ip = 0;
137 #endif
138 return ip;
141 void set_null_ip(IN_ADDR_T *ip)
143 #ifdef IPV6SUPPORT
144 cs_inet_addr("::", ip);
145 #else
146 *ip = 0;
147 #endif
150 void set_localhost_ip(IN_ADDR_T *ip)
152 #ifdef IPV6SUPPORT
153 cs_inet_addr("::1", ip);
154 #else
155 cs_inet_addr("127.0.0.1", ip);
156 #endif
159 int32_t check_ip(struct s_ip *ip, IN_ADDR_T n)
161 struct s_ip *p_ip;
162 int32_t ok = 0;
163 #ifdef IPV6SUPPORT
164 for(p_ip = ip; (p_ip) && (!ok); p_ip = p_ip->next)
166 ok = cs_in6addr_lt(&n, &p_ip->ip[0]);
167 ok |= cs_in6addr_lt(&p_ip->ip[1], &n);
168 ok = !ok;
170 #else
171 for(p_ip = ip; (p_ip) && (!ok); p_ip = p_ip->next)
172 { ok = ((cs_inet_order(n) >= cs_inet_order(p_ip->ip[0])) && (cs_inet_order(n) <= cs_inet_order(p_ip->ip[1]))); }
173 #endif
174 return ok;
177 /* Returns the ip from the given hostname. If gethostbyname is configured in the config file, a lock
178 will be held until the ip has been resolved. */
179 uint32_t cs_getIPfromHost(const char *hostname)
181 uint32_t result = 0;
182 //Resolve with gethostbyname:
183 if(cfg.resolve_gethostbyname)
185 cs_writelock(__func__, &gethostbyname_lock);
186 struct hostent *rht = gethostbyname(hostname);
187 if(!rht)
188 { cs_log("can't resolve %s", hostname); }
189 else
190 { result = ((struct in_addr *)rht->h_addr)->s_addr; }
191 cs_writeunlock(__func__, &gethostbyname_lock);
193 else //Resolve with getaddrinfo:
195 struct addrinfo hints, *res = NULL;
196 memset(&hints, 0, sizeof(hints));
197 hints.ai_socktype = SOCK_STREAM;
198 hints.ai_family = AF_INET;
199 hints.ai_protocol = IPPROTO_TCP;
201 int32_t err = getaddrinfo(hostname, NULL, &hints, &res);
202 if(err != 0 || !res || !res->ai_addr)
204 cs_log("can't resolve %s, error: %s", hostname, err ? gai_strerror(err) : "unknown");
206 else
208 result = ((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr;
210 if(res) { freeaddrinfo(res); }
212 return result;
215 #ifdef IPV6SUPPORT
216 void cs_getIPv6fromHost(const char *hostname, struct in6_addr *addr, struct sockaddr_storage *sa, socklen_t *sa_len)
218 uint32_t ipv4addr = 0;
219 struct addrinfo hints, *res = NULL;
220 memset(&hints, 0, sizeof(hints));
221 hints.ai_socktype = SOCK_STREAM;
222 hints.ai_family = AF_UNSPEC;
223 hints.ai_protocol = IPPROTO_TCP;
224 int32_t err = getaddrinfo(hostname, NULL, &hints, &res);
225 if(err != 0 || !res || !res->ai_addr)
227 cs_log("can't resolve %s, error: %s", hostname, err ? gai_strerror(err) : "unknown");
229 else
231 ipv4addr = ((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr;
232 if(res->ai_family == AF_INET)
233 { cs_in6addr_ipv4map(addr, ipv4addr); }
234 else
235 { IP_ASSIGN(*addr, SIN_GET_ADDR(*res->ai_addr)); }
236 if(sa)
237 { memcpy(sa, res->ai_addr, res->ai_addrlen); }
238 if(sa_len)
239 { *sa_len = res->ai_addrlen; }
241 if(res)
242 { freeaddrinfo(res); }
244 #endif
246 int set_socket_priority(int fd, int priority)
248 #ifdef SO_PRIORITY
249 return priority ? setsockopt(fd, SOL_SOCKET, SO_PRIORITY, (void *)&priority, sizeof(int *)) : -1;
250 #else
251 (void)fd;
252 (void)priority;
253 return -1;
254 #endif
257 void setTCPTimeouts(int32_t sock)
259 int32_t flag = 1;
260 // this is not only for a real keepalive but also to detect closed connections so it should not be configurable
261 if(setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &flag, sizeof(flag)) && errno != EBADF)
263 cs_log("Setting SO_KEEPALIVE failed, errno=%d, %s", errno, strerror(errno));
265 #if defined(TCP_KEEPIDLE) && defined(TCP_KEEPCNT) && defined(TCP_KEEPINTVL)
266 flag = 10;
267 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)
269 cs_log("Setting TCP_KEEPIDLE failed, errno=%d, %s", errno, strerror(errno));
271 flag = 3;
272 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
274 cs_log("Setting TCP_KEEPCNT failed, errno=%d, %s", errno, strerror(errno));
276 flag = 1;
277 if(setsockopt(sock, IPPROTO_TCP, TCP_KEEPINTVL, &flag, sizeof(flag)) && errno != EBADF)
279 ; //send a keepalive packet out every second (until answer has been received or TCP_KEEPCNT has been reached)
280 cs_log("Setting TCP_KEEPINTVL failed, errno=%d, %s", errno, strerror(errno));
282 #endif
283 struct timeval tv;
284 tv.tv_sec = 60;
285 tv.tv_usec = 0;
286 if(setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(struct timeval)) && errno != EBADF)
289 cs_log("Setting SO_SNDTIMEO failed, errno=%d, %s", errno, strerror(errno));
291 tv.tv_sec = 600;
292 tv.tv_usec = 0;
293 if(setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval)) && errno != EBADF)
296 cs_log("Setting SO_RCVTIMEO failed, errno=%d, %s", errno, strerror(errno));
298 #if defined(TCP_USER_TIMEOUT)
299 int timeout = 60000; // RFC 5482 user timeout in milliseconds
300 setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT, (char *) &timeout, sizeof(timeout));
301 #endif
304 int set_nonblock(int32_t fd, bool nonblock)
306 int32_t flags = fcntl(fd, F_GETFL);
307 if (flags == -1)
308 return -1;
309 if (nonblock)
310 flags |= O_NONBLOCK;
311 else
312 flags &= (~O_NONBLOCK);
313 return fcntl(fd, F_SETFL, flags);
316 int8_t check_fd_for_data(int32_t fd)
318 int32_t rc;
319 struct pollfd pfd[1];
321 pfd[0].fd = fd;
322 pfd[0].events = (POLLIN | POLLPRI);
323 rc = poll(pfd, 1, 0);
325 if(rc == -1)
326 { cs_log("check_fd_for_data(fd=%d) failed: (errno=%d %s)", fd, errno, strerror(errno)); }
328 if(rc == -1 || rc == 0)
329 { return rc; }
331 if(pfd[0].revents & (POLLHUP | POLLNVAL | POLLERR))
332 { return -2; }
334 return 1;
337 int32_t recv_from_udpipe(uint8_t *buf)
339 uint16_t n;
340 if(buf[0] != 'U')
342 cs_log("INTERNAL PIPE-ERROR");
343 cs_exit(1);
345 memcpy(&n, buf + 1, 2);
346 memmove(buf, buf + 3, n);
347 return n;
350 int32_t process_input(uint8_t *buf, int32_t buflen, int32_t timeout)
352 int32_t rc, i, pfdcount;
353 int64_t polltime, timeoutms;
354 struct pollfd pfd[2];
355 struct s_client *cl = cur_client();
357 struct timeb starttime;
358 struct timeb currenttime;
359 timeoutms = 1000 * timeout;
360 cs_ftime(&starttime);
361 polltime = timeoutms; // initial polltime = timeoutms
362 while(1)
364 pfdcount = 0;
365 if(cl->pfd)
367 pfd[pfdcount].fd = cl->pfd;
368 pfd[pfdcount++].events = POLLIN | POLLPRI;
370 int32_t p_rc = poll(pfd, pfdcount, polltime);
372 cs_ftime(&currenttime);
373 int64_t gone = comp_timeb(&currenttime, &starttime);
374 polltime = timeoutms - gone; // calculate polltime left
375 if(polltime < 0)
377 polltime = 0;
379 if(p_rc < 0)
381 if(errno == EINTR)
382 { continue; }
383 else
384 { return 0; }
387 if((p_rc == 0) && (timeout != 0) && (gone >= timeoutms)) // client maxidle reached? timeout = 0, idle disconnect disabled
389 rc = -9;
390 break;
393 for(i = 0; i < pfdcount && p_rc > 0; i++)
395 if(pfd[i].revents & POLLHUP) // POLLHUP is only valid in revents so it doesn't need to be set above in events
397 return 0;
399 if(!(pfd[i].revents & (POLLIN | POLLPRI)))
400 { continue; }
402 if(pfd[i].fd == cl->pfd)
403 { return get_module(cl)->recv(cl, buf, buflen); }
406 return rc;
409 static struct s_client *find_client_by_ip(IN_ADDR_T ip, in_port_t port)
411 struct s_client *cl;
412 for(cl = first_client; cl; cl = cl->next)
414 if(!cl->kill &&
415 IP_EQUAL(cl->ip, ip) && cl->port == port &&
416 (cl->typ == 'c' || cl->typ == 'm'))
418 return cl;
421 return NULL;
424 int32_t accept_connection(struct s_module *module, int8_t module_idx, int8_t port_idx)
426 struct SOCKADDR cad;
427 int32_t scad = sizeof(cad), n;
428 struct s_client *cl;
429 struct s_port *port = &module->ptab.ports[port_idx];
431 memset(&cad, 0, sizeof(struct SOCKADDR));
432 if(module->type == MOD_CONN_UDP)
434 uchar *buf;
435 if(!cs_malloc(&buf, 1024))
436 { return -1; }
437 if((n = recvfrom(port->fd, buf + 3, 1024 - 3, 0, (struct sockaddr *)&cad, (socklen_t *)&scad)) > 0)
439 uint16_t rl;
440 cl = find_client_by_ip(SIN_GET_ADDR(cad), ntohs(SIN_GET_PORT(cad)));
441 rl = n;
442 buf[0] = 'U';
443 memcpy(buf + 1, &rl, 2);
445 if(cs_check_violation(SIN_GET_ADDR(cad), port->s_port))
447 NULLFREE(buf);
448 return 0;
451 cs_log_dbg(D_TRACE, "got %d bytes on port %d from ip %s:%d client %s",
452 n, port->s_port,
453 cs_inet_ntoa(SIN_GET_ADDR(cad)), SIN_GET_PORT(cad),
454 username(cl));
456 if(!cl)
458 cl = create_client(SIN_GET_ADDR(cad));
459 if(!cl) { return 0; }
461 cl->module_idx = module_idx;
462 cl->port_idx = port_idx;
463 cl->udp_fd = port->fd;
464 cl->udp_sa = cad;
465 cl->udp_sa_len = sizeof(cl->udp_sa);
467 cl->port = ntohs(SIN_GET_PORT(cad));
468 cl->typ = 'c';
470 add_job(cl, ACTION_CLIENT_INIT, NULL, 0);
472 add_job(cl, ACTION_CLIENT_UDP, buf, n + 3);
474 else
475 { NULLFREE(buf); }
477 else //TCP
479 int32_t pfd3;
480 if((pfd3 = accept(port->fd, (struct sockaddr *)&cad, (socklen_t *)&scad)) > 0)
483 if(cs_check_violation(SIN_GET_ADDR(cad), port->s_port))
485 close(pfd3);
486 return 0;
489 cl = create_client(SIN_GET_ADDR(cad));
490 if(cl == NULL)
492 close(pfd3);
493 return 0;
496 int32_t flag = 1;
497 setsockopt(pfd3, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag));
498 setTCPTimeouts(pfd3);
500 cl->module_idx = module_idx;
501 cl->udp_fd = pfd3;
502 cl->port_idx = port_idx;
504 cl->pfd = pfd3;
505 cl->port = ntohs(SIN_GET_PORT(cad));
506 cl->typ = 'c';
508 add_job(cl, ACTION_CLIENT_INIT, NULL, 0);
511 return 0;
514 void set_so_reuseport(int fd) {
515 #ifdef SO_REUSEPORT
516 // See: http://stackoverflow.com/questions/3261965/so-reuseport-on-linux
517 int32_t on = 1;
518 setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, (void *)&on, sizeof(on));
519 #else
520 fd = fd; // Do nothing
521 #endif
524 int32_t start_listener(struct s_module *module, struct s_port *port)
526 int32_t ov = 1, timeout, is_udp, i;
527 char ptxt[2][32];
528 struct SOCKADDR sad; // structure to hold server's address
529 socklen_t sad_len;
531 ptxt[0][0] = ptxt[1][0] = '\0';
532 if(!port->s_port)
534 cs_log_dbg(D_TRACE, "%s: disabled", module->desc);
535 return 0;
537 is_udp = (module->type == MOD_CONN_UDP);
539 memset(&sad, 0 , sizeof(sad));
540 #ifdef IPV6SUPPORT
541 SIN_GET_FAMILY(sad) = AF_INET6;
542 SIN_GET_ADDR(sad) = in6addr_any;
543 sad_len = sizeof(struct sockaddr_in6);
544 #else
545 sad.sin_family = AF_INET;
546 sad_len = sizeof(struct sockaddr);
547 if(!module->s_ip)
548 { module->s_ip = cfg.srvip; }
549 if(module->s_ip)
551 sad.sin_addr.s_addr = module->s_ip;
552 snprintf(ptxt[0], sizeof(ptxt[0]), ", ip=%s", inet_ntoa(sad.sin_addr));
554 else
556 sad.sin_addr.s_addr = INADDR_ANY;
558 #endif
559 timeout = cfg.bindwait;
560 port->fd = 0;
562 if(port->s_port > 0) // test for illegal value
564 SIN_GET_PORT(sad) = htons((uint16_t)port->s_port);
566 else
568 cs_log("%s: Bad port %d", module->desc, port->s_port);
569 return 0;
572 int s_type = (is_udp ? SOCK_DGRAM : SOCK_STREAM);
573 int s_proto = (is_udp ? IPPROTO_UDP : IPPROTO_TCP);
575 if((port->fd = socket(DEFAULT_AF, s_type, s_proto)) < 0)
577 cs_log("%s: Cannot create socket (errno=%d: %s)", module->desc, errno, strerror(errno));
578 #ifdef IPV6SUPPORT
579 cs_log("%s: Trying fallback to IPv4", module->desc);
580 if((port->fd = socket(AF_INET, s_type, s_proto)) < 0)
582 cs_log("%s: Cannot create socket (errno=%d: %s)", module->desc, errno, strerror(errno));
583 return 0;
585 #else
586 return 0;
587 #endif
590 #ifdef IPV6SUPPORT
591 // azbox toolchain do not have this define
592 #ifndef IPV6_V6ONLY
593 #define IPV6_V6ONLY 26
594 #endif
595 // set the server socket option to listen on IPv4 and IPv6 simultaneously
596 int val = 0;
597 if(setsockopt(port->fd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&val, sizeof(val)) < 0)
599 cs_log("%s: setsockopt(IPV6_V6ONLY) failed (errno=%d: %s)", module->desc, errno, strerror(errno));
601 #endif
603 ov = 1;
604 if(setsockopt(port->fd, SOL_SOCKET, SO_REUSEADDR, (void *)&ov, sizeof(ov)) < 0)
606 cs_log("%s: setsockopt failed (errno=%d: %s)", module->desc, errno, strerror(errno));
607 close(port->fd);
608 port->fd = 0;
609 return 0;
612 set_so_reuseport(port->fd);
614 if(set_socket_priority(port->fd, cfg.netprio) > -1)
615 { snprintf(ptxt[1], sizeof(ptxt[1]), ", prio=%d", cfg.netprio); }
617 if(!is_udp)
619 int32_t keep_alive = 1;
620 setsockopt(port->fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&keep_alive, sizeof(keep_alive));
623 while(timeout-- && !exit_oscam)
625 if(bind(port->fd, (struct sockaddr *)&sad, sad_len) < 0)
627 if(timeout)
629 cs_log("%s: Bind request failed (%s), waiting another %d seconds",
630 module->desc, strerror(errno), timeout);
631 cs_sleepms(1000);
633 else
635 cs_log("%s: Bind request failed (%s), giving up", module->desc, strerror(errno));
636 close(port->fd);
637 port->fd = 0;
638 return 0;
641 else
643 timeout = 0;
647 if(!is_udp)
649 if(listen(port->fd, CS_QLEN) < 0)
651 cs_log("%s: Cannot start listen mode (errno=%d: %s)", module->desc, errno, strerror(errno));
652 close(port->fd);
653 port->fd = 0;
654 return 0;
658 cs_log("%s: initialized (fd=%d, port=%d%s%s)",
659 module->desc, port->fd,
660 port->s_port,
661 ptxt[0], ptxt[1]);
663 for(i = 0; port->ncd && i < port->ncd->ncd_ftab.nfilts; i++)
665 int32_t j, pos = 0;
666 char buf[30 + (8 * port->ncd->ncd_ftab.filts[i].nprids)];
667 pos += snprintf(buf, sizeof(buf), "-> CAID: %04X PROVID: ", port->ncd->ncd_ftab.filts[i].caid);
669 for(j = 0; j < port->ncd->ncd_ftab.filts[i].nprids; j++)
670 { pos += snprintf(buf + pos, sizeof(buf) - pos, "%06X, ", port->ncd->ncd_ftab.filts[i].prids[j]); }
672 if(pos > 2 && j > 0)
673 { buf[pos - 2] = '\0'; }
675 cs_log("%s", buf);
678 return port->fd;
681 #ifdef __CYGWIN__
683 * Workaround missing MSG_WAITALL implementation under Cygwin.
685 ssize_t cygwin_recv(int sock, void *buf, int count, int tflags)
687 char *bp = buf;
688 int n = 0;
690 if ((n = recv(sock, bp, count, tflags)) < 0)
692 return(n);
695 if (n < count && (tflags & MSG_WAITALL))
697 cs_log_dbg(D_TRACE, "Cygwin socket read retry. Got %d expected %d", n, count);
699 int n2 = recv(sock, bp + n, count - n, tflags);
700 if (n2 < 0 || n + n2 != count)
702 cs_log_dbg(D_TRACE, "Cygwin socket read retry failed. Got %d", n2);
703 if (n2 < 0)
705 return(n2);
708 else
710 cs_log_dbg(D_TRACE, "Cygwin socket read retry success. Got %d - Total: %d", n2, n + n2);
713 n+= n2;
716 return n;
718 #endif /* __CYGWIN__ */