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