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

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