tsdecrypt reads and decrypts CSA encrypted incoming mpeg transport stream over UDP/RTP using code words obtained from OSCAM or similar CAM server. tsdecrypt communicates with CAM server using cs378x (camd35 over tcp) protocol or newcamd protocol. https://georgi.unixsol.org/programs/tsdecrypt/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

udp.c 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * UDP functions
  3. * Copyright (C) 2011 Unix Solutions Ltd.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License (COPYING file) for more details.
  13. *
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24. #include <arpa/inet.h>
  25. #include <errno.h>
  26. #include "util.h"
  27. #include "udp.h"
  28. #ifndef IPV6_ADD_MEMBERSHIP
  29. #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
  30. #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
  31. #endif
  32. static int is_multicast(struct sockaddr_storage *addr) {
  33. int ret = 0;
  34. switch (addr->ss_family) {
  35. case AF_INET: {
  36. struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
  37. ret = IN_MULTICAST(ntohl(addr4->sin_addr.s_addr));
  38. break;
  39. }
  40. case AF_INET6: {
  41. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
  42. ret = IN6_IS_ADDR_MULTICAST(&addr6->sin6_addr);
  43. break;
  44. } }
  45. return ret;
  46. }
  47. extern int ai_family;
  48. static int bind_addr(const char *hostname, const char *service, int socktype, struct sockaddr_storage *addr, int *addrlen, int *sock) {
  49. struct addrinfo hints, *res, *ressave;
  50. int n, ret = -1;
  51. memset(&hints, 0, sizeof(struct addrinfo));
  52. hints.ai_family = ai_family;
  53. hints.ai_socktype = socktype;
  54. n = getaddrinfo(hostname, service, &hints, &res);
  55. if (n < 0) {
  56. ts_LOGf("ERROR: getaddrinfo(%s): %s\n", hostname, gai_strerror(n));
  57. return ret;
  58. }
  59. ressave = res;
  60. while (res) {
  61. *sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  62. if (*sock > -1) {
  63. if (bind(*sock, res->ai_addr, res->ai_addrlen) == 0) {
  64. memcpy(addr, res->ai_addr, sizeof(*addr));
  65. *addrlen = res->ai_addrlen;
  66. ret = 0;
  67. goto OUT;
  68. } else {
  69. char str_addr[INET6_ADDRSTRLEN];
  70. my_inet_ntop(res->ai_family, res->ai_addr, str_addr, sizeof(str_addr));
  71. ts_LOGf("ERROR: bind: %s:%s (%s): %s\n",
  72. hostname, service, str_addr, strerror(errno));
  73. }
  74. close(*sock);
  75. *sock = -1;
  76. }
  77. res = res->ai_next;
  78. }
  79. OUT:
  80. freeaddrinfo(ressave);
  81. if (*sock > -1) {
  82. int on = 1;
  83. setsockopt(*sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  84. set_sock_nonblock(*sock);
  85. }
  86. return ret;
  87. }
  88. static int join_multicast_group(int sock, int ttl, struct sockaddr_storage *addr) {
  89. switch (addr->ss_family) {
  90. case AF_INET: {
  91. struct ip_mreq mreq;
  92. mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  93. mreq.imr_interface.s_addr = INADDR_ANY;
  94. if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
  95. ts_LOGf("ERROR: setsockopt(IP_ADD_MEMBERSHIP): %s\n", strerror(errno));
  96. return -1;
  97. }
  98. if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) {
  99. ts_LOGf("ERROR: setsockopt(IP_MULTICAST_TTL %d): %s\n", ttl, strerror(errno));
  100. }
  101. break;
  102. }
  103. case AF_INET6: {
  104. struct ipv6_mreq mreq6;
  105. memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
  106. mreq6.ipv6mr_interface = 0; // interface index, will be set later
  107. if (setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
  108. ts_LOGf("ERROR: setsockopt(IPV6_ADD_MEMBERSHIP): %s\n", strerror(errno));
  109. return -1;
  110. }
  111. if (setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
  112. ts_LOGf("ERROR: setsockopt(IPV6_MULTICAST_HOPS %d): %s\n", ttl, strerror(errno));
  113. }
  114. break;
  115. }
  116. }
  117. return 0;
  118. }
  119. int udp_connect_input(struct io *io) {
  120. struct sockaddr_storage addr;
  121. int addrlen = sizeof(addr);
  122. int sock = -1;
  123. memset(&addr, 0, sizeof(addr));
  124. ts_LOGf("Connecting input to %s port %s\n", io->hostname, io->service);
  125. if (bind_addr(io->hostname, io->service, SOCK_DGRAM, &addr, &addrlen, &sock) < 0)
  126. return -1;
  127. /* Set receive buffer size to ~2.0MB */
  128. int bufsize = (2000000 / 1316) * 1316;
  129. setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (void *)&bufsize, sizeof(bufsize));
  130. if (is_multicast(&addr)) {
  131. if (join_multicast_group(sock, io->ttl, &addr) < 0) {
  132. close(sock);
  133. return -1;
  134. }
  135. }
  136. io->fd = sock;
  137. ts_LOGf("Input connected to fd:%d\n", io->fd);
  138. return 1;
  139. }
  140. int udp_connect_output(struct io *io) {
  141. struct sockaddr_storage addr;
  142. int addrlen = sizeof(addr);
  143. int sock = -1;
  144. memset(&addr, 0, sizeof(addr));
  145. ts_LOGf("Connecting output to %s port %s ttl: %d\n",
  146. io->hostname, io->service, io->ttl);
  147. if (bind_addr(io->hostname, io->service, SOCK_DGRAM, &addr, &addrlen, &sock) < 0)
  148. return -1;
  149. /* Set send buffer size to ~2.0MB */
  150. int bufsize = (2000000 / 1316) * 1316;
  151. setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (void *)&bufsize, sizeof(bufsize));
  152. if (is_multicast(&addr)) {
  153. if (join_multicast_group(sock, io->ttl, &addr) < 0) {
  154. close(sock);
  155. return -1;
  156. } else {
  157. if (addr.ss_family == AF_INET) {
  158. if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, &io->intf, sizeof(io->intf)) < 0) {
  159. ts_LOGf("ERROR: setsockopt(IP_MUTICAST_IF %s): %s\n", inet_ntoa(io->intf), strerror(errno));
  160. close(sock);
  161. return -1;
  162. }
  163. }
  164. if (addr.ss_family == AF_INET6 && io->v6_if_index > -1) {
  165. if (setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_IF, (void *)&io->v6_if_index, sizeof(io->v6_if_index)) < 0) {
  166. ts_LOGf("ERROR: setsockopt(IPV6_MUTICAST_IF %d): %s\n", io->v6_if_index, strerror(errno));
  167. close(sock);
  168. return -1;
  169. }
  170. }
  171. }
  172. }
  173. if (addr.ss_family == AF_INET && io->tos > -1) {
  174. if (setsockopt(sock, IPPROTO_IP, IP_TOS, &io->tos, sizeof(io->tos)) < 0) {
  175. ts_LOGf("ERROR: setsockopt(IP_TOS 0x%02x): %s\n", io->tos, strerror(errno));
  176. }
  177. }
  178. if (connect(sock, (struct sockaddr *)&addr, addrlen) < 0) {
  179. ts_LOGf("ERROR: udp_connect(): %s\n", strerror(errno));
  180. close(sock);
  181. return -1;
  182. }
  183. io->fd = sock;
  184. ts_LOGf("Output connected to fd:%d\n", io->fd);
  185. return 1;
  186. }