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 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 get_socket(const char *hostname, const char *service, int socktype, struct sockaddr_storage *addr, int *addrlen, int *sock, bool is_output) {
  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. int on = 1;
  64. setsockopt(*sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  65. set_sock_nonblock(*sock);
  66. if (is_output) {
  67. memcpy(addr, res->ai_addr, res->ai_addrlen);
  68. *addrlen = res->ai_addrlen;
  69. ret = 0;
  70. break;
  71. }
  72. if (bind(*sock, res->ai_addr, res->ai_addrlen) == 0) {
  73. memcpy(addr, res->ai_addr, res->ai_addrlen);
  74. *addrlen = res->ai_addrlen;
  75. ret = 0;
  76. break;
  77. } else {
  78. char str_addr[INET6_ADDRSTRLEN];
  79. my_inet_ntop(res->ai_family, res->ai_addr, str_addr, sizeof(str_addr));
  80. ts_LOGf("ERROR: bind: %s:%s (%s): %s\n",
  81. hostname, service, str_addr, strerror(errno));
  82. }
  83. close(*sock);
  84. *sock = -1;
  85. }
  86. res = res->ai_next;
  87. }
  88. freeaddrinfo(ressave);
  89. return ret;
  90. }
  91. static int get_input_socket(const char *hostname, const char *service, int socktype, struct sockaddr_storage *addr, int *addrlen, int *sock) {
  92. return get_socket(hostname, service, socktype, addr, addrlen, sock, false);
  93. }
  94. static int get_output_socket(const char *hostname, const char *service, int socktype, struct sockaddr_storage *addr, int *addrlen, int *sock) {
  95. return get_socket(hostname, service, socktype, addr, addrlen, sock, true);
  96. }
  97. static int join_multicast_group(int sock, int ttl, struct sockaddr_storage *addr) {
  98. switch (addr->ss_family) {
  99. case AF_INET: {
  100. struct ip_mreq mreq;
  101. mreq.imr_multiaddr.s_addr = ((struct sockaddr_in *)addr)->sin_addr.s_addr;
  102. mreq.imr_interface.s_addr = INADDR_ANY;
  103. if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq, sizeof(mreq)) < 0) {
  104. ts_LOGf("ERROR: setsockopt(IP_ADD_MEMBERSHIP): %s\n", strerror(errno));
  105. return -1;
  106. }
  107. if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) {
  108. ts_LOGf("ERROR: setsockopt(IP_MULTICAST_TTL %d): %s\n", ttl, strerror(errno));
  109. }
  110. break;
  111. }
  112. case AF_INET6: {
  113. struct ipv6_mreq mreq6;
  114. memcpy(&mreq6.ipv6mr_multiaddr, &(((struct sockaddr_in6 *)addr)->sin6_addr), sizeof(struct in6_addr));
  115. mreq6.ipv6mr_interface = 0; // interface index, will be set later
  116. if (setsockopt(sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
  117. ts_LOGf("ERROR: setsockopt(IPV6_ADD_MEMBERSHIP): %s\n", strerror(errno));
  118. return -1;
  119. }
  120. if (setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
  121. ts_LOGf("ERROR: setsockopt(IPV6_MULTICAST_HOPS %d): %s\n", ttl, strerror(errno));
  122. }
  123. break;
  124. }
  125. }
  126. return 0;
  127. }
  128. int udp_connect_input(struct io *io) {
  129. struct sockaddr_storage addr;
  130. int addrlen = sizeof(addr);
  131. int sock = -1;
  132. memset(&addr, 0, sizeof(addr));
  133. if (!io->isrc.s_addr)
  134. ts_LOGf("Connecting input to %s port %s\n", io->hostname, io->service);
  135. else
  136. ts_LOGf("Connecting input to %s port %s source %s\n", io->hostname, io->service, inet_ntoa(io->isrc));
  137. if (get_input_socket(io->hostname, io->service, SOCK_DGRAM, &addr, &addrlen, &sock) < 0)
  138. return -1;
  139. /* Set receive buffer size to ~2.0MB */
  140. int bufsize = (2000000 / 1316) * 1316;
  141. setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (void *)&bufsize, sizeof(bufsize));
  142. if (is_multicast(&addr)) {
  143. if (join_multicast_group(sock, io->ttl, &addr) < 0) {
  144. close(sock);
  145. return -1;
  146. } else {
  147. #ifdef IP_ADD_SOURCE_MEMBERSHIP
  148. if (io->isrc.s_addr && addr.ss_family == AF_INET) {
  149. /* Source-specific multicast */
  150. struct sockaddr_in *src = (struct sockaddr_in *)&addr;
  151. struct ip_mreq_source imr;
  152. memset(&imr, 0, sizeof(imr));
  153. imr.imr_multiaddr = src->sin_addr;
  154. imr.imr_sourceaddr = io->isrc;
  155. if (setsockopt(sock, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP,
  156. (char *)&imr, sizeof(struct ip_mreq_source)) < 0)
  157. {
  158. char str_addr[INET6_ADDRSTRLEN];
  159. my_inet_ntop(addr.ss_family, (struct sockaddr *)&addr, str_addr, sizeof(str_addr));
  160. ts_LOGf("ERROR: Can't set multicast group %s source %s: %s\n",
  161. str_addr, inet_ntoa(io->isrc), strerror(errno));
  162. }
  163. }
  164. #endif
  165. }
  166. }
  167. io->fd = sock;
  168. ts_LOGf("Input connected to fd:%d\n", io->fd);
  169. return 1;
  170. }
  171. int udp_connect_output(struct io *io) {
  172. struct sockaddr_storage addr;
  173. int addrlen = sizeof(addr);
  174. int sock = -1;
  175. memset(&addr, 0, sizeof(addr));
  176. ts_LOGf("Connecting output to %s port %s ttl %d\n",
  177. io->hostname, io->service, io->ttl);
  178. if (get_output_socket(io->hostname, io->service, SOCK_DGRAM, &addr, &addrlen, &sock) < 0)
  179. return -1;
  180. /* Set send buffer size to ~2.0MB */
  181. int bufsize = (2000000 / 1316) * 1316;
  182. setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (void *)&bufsize, sizeof(bufsize));
  183. if (is_multicast(&addr)) {
  184. if (join_multicast_group(sock, io->ttl, &addr) < 0) {
  185. close(sock);
  186. return -1;
  187. } else {
  188. if (addr.ss_family == AF_INET) {
  189. if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, &io->intf, sizeof(io->intf)) < 0) {
  190. ts_LOGf("ERROR: setsockopt(IP_MUTICAST_IF %s): %s\n", inet_ntoa(io->intf), strerror(errno));
  191. close(sock);
  192. return -1;
  193. }
  194. }
  195. if (addr.ss_family == AF_INET6 && io->v6_if_index > -1) {
  196. if (setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_IF, (void *)&io->v6_if_index, sizeof(io->v6_if_index)) < 0) {
  197. ts_LOGf("ERROR: setsockopt(IPV6_MUTICAST_IF %d): %s\n", io->v6_if_index, strerror(errno));
  198. close(sock);
  199. return -1;
  200. }
  201. }
  202. }
  203. }
  204. if (addr.ss_family == AF_INET && io->tos > -1) {
  205. if (setsockopt(sock, IPPROTO_IP, IP_TOS, &io->tos, sizeof(io->tos)) < 0) {
  206. ts_LOGf("ERROR: setsockopt(IP_TOS 0x%02x): %s\n", io->tos, strerror(errno));
  207. }
  208. }
  209. if (connect(sock, (struct sockaddr *)&addr, addrlen) < 0) {
  210. ts_LOGf("ERROR: udp_connect(): %s\n", strerror(errno));
  211. close(sock);
  212. return -1;
  213. }
  214. io->fd = sock;
  215. ts_LOGf("Output connected to fd:%d\n", io->fd);
  216. return 1;
  217. }