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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <sys/socket.h>
  9. #include <netinet/in.h>
  10. #include <arpa/inet.h>
  11. #include <errno.h>
  12. #include "udp.h"
  13. int udp_connect_input(struct io *io) {
  14. int sock = socket(AF_INET, SOCK_DGRAM, 0);
  15. if (sock < 0) {
  16. ts_LOGf("socket(SOCK_DGRAM): %s", strerror(errno));
  17. return -1;
  18. }
  19. ts_LOGf("Connecting input to udp://%s:%d/\n", inet_ntoa(io->addr), io->port);
  20. int on = 1;
  21. setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  22. /* Set receive buffer size to ~2.3MB or 6Mb/s for half a second */
  23. int bufsize = (6000000 / 1316) / 2;
  24. setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (void *)&bufsize, sizeof(bufsize));
  25. // subscribe to multicast group
  26. if (IN_MULTICAST(ntohl(io->addr.s_addr))) {
  27. struct ip_mreq mreq;
  28. memcpy(&mreq.imr_multiaddr, &io->addr, sizeof(struct in_addr));
  29. mreq.imr_interface.s_addr = htonl(INADDR_ANY);
  30. if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
  31. ts_LOGf("setsockopt(IP_ADD_MEMBERSHIP %s): %s", inet_ntoa(io->addr), strerror(errno));
  32. return -1;
  33. }
  34. }
  35. // bind to the socket so data can be read
  36. struct sockaddr_in receiving_from;
  37. memset(&receiving_from, 0, sizeof(receiving_from));
  38. receiving_from.sin_family = AF_INET;
  39. receiving_from.sin_addr = io->addr;
  40. receiving_from.sin_port = htons(io->port);
  41. if (bind(sock, (struct sockaddr *) &receiving_from, sizeof(receiving_from)) < 0) {
  42. ts_LOGf("bind(): %s", strerror(errno));
  43. return -1;
  44. }
  45. io->fd = sock;
  46. ts_LOGf("Input connected to fd:%d\n", io->fd);
  47. return 1;
  48. }
  49. int udp_connect_output(struct io *io) {
  50. int sock = socket(AF_INET, SOCK_DGRAM, 0);
  51. if (sock < 0) {
  52. ts_LOGf("socket(SOCK_DGRAM): %s\n", strerror(errno));
  53. return -1;
  54. }
  55. ts_LOGf("Connecting output to udp://%s:%d ttl:%d\n",
  56. inet_ntoa(io->addr), io->port, io->ttl);
  57. int on = 1;
  58. setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  59. set_sock_nonblock(sock);
  60. /* Set receive buffer size to ~2.3MB or 6Mb/s for half a second */
  61. int bufsize = (6000000 / 1316) / 2;
  62. setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (void *)&bufsize, sizeof(bufsize));
  63. // subscribe to multicast group
  64. if (IN_MULTICAST(ntohl(io->addr.s_addr))) {
  65. int ttl = io->ttl;
  66. if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) {
  67. ts_LOGf("setsockopt(IP_MUTICAST_TTL): %s\n", strerror(errno));
  68. close(sock);
  69. return -1;
  70. }
  71. if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF, &io->intf, sizeof(io->intf)) < 0) {
  72. ts_LOGf("setsockopt(IP_MUTICAST_IF %s): %s\n", inet_ntoa(io->intf), strerror(errno));
  73. close(sock);
  74. return -1;
  75. }
  76. }
  77. struct sockaddr_in sockaddr;
  78. memset(&sockaddr, 0, sizeof(sockaddr));
  79. sockaddr.sin_family = AF_INET;
  80. sockaddr.sin_addr.s_addr = io->addr.s_addr;
  81. sockaddr.sin_port = htons(io->port);
  82. if (connect(sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr))) {
  83. ts_LOGf("udp_connect() error: %s\n", strerror(errno));
  84. close(sock);
  85. return -1;
  86. }
  87. io->fd = sock;
  88. ts_LOGf("Output connected to fd:%d\n", io->fd);
  89. return 1;
  90. }