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.

data.h 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Data definitions
  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 for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  17. */
  18. #ifndef DATA_H
  19. #define DATA_H
  20. #include <pthread.h>
  21. #include <limits.h>
  22. #include <openssl/aes.h>
  23. #include <openssl/md5.h>
  24. #include <dvbcsa/dvbcsa.h>
  25. #include "libfuncs/libfuncs.h"
  26. #include "libtsfuncs/tsfuncs.h"
  27. // 7 * 188
  28. #define FRAME_SIZE 1316
  29. // How much seconds to assume the key is valid
  30. #define KEY_VALID_TIME 10
  31. struct notify {
  32. pthread_t thread; /* Thread handle */
  33. QUEUE *notifications; /* Notification queue */
  34. char ident[512]; /* tsdecrypt ident (set by -i) */
  35. char program[512]; /* What program to exec */
  36. };
  37. struct key {
  38. uint8_t cw[16];
  39. int is_valid_cw;
  40. struct dvbcsa_key_s *csakey[2];
  41. struct dvbcsa_bs_key_s *bs_csakey[2];
  42. time_t ts; // At what time the key is set
  43. struct timeval ts_keyset; // At what time the key is set
  44. };
  45. // 4 auth header, 20 header size, 256 max data size, 16 potential padding
  46. #define CAMD35_HDR_LEN (20)
  47. #define CAMD35_BUF_LEN (4 + CAMD35_HDR_LEN + 256 + 16)
  48. // When this limit is reached invalid_cw flag is set.
  49. #define ECM_RECV_ERRORS_LIMIT 10
  50. // When this limit is reached camd_reconnect is called.
  51. #define EMM_RECV_ERRORS_LIMIT 100
  52. struct camd35 {
  53. uint8_t buf[CAMD35_BUF_LEN];
  54. int server_fd;
  55. struct in_addr server_addr;
  56. unsigned int server_port;
  57. char user[64];
  58. char pass[64];
  59. AES_KEY aes_encrypt_key;
  60. AES_KEY aes_decrypt_key;
  61. unsigned int ecm_recv_errors; // Error counter, reset on successful send/recv
  62. unsigned int emm_recv_errors; // Error counter, reset on successful send/recv
  63. uint32_t auth_token;
  64. struct key *key;
  65. pthread_t thread;
  66. QUEUE *req_queue;
  67. QUEUE *ecm_queue;
  68. QUEUE *emm_queue;
  69. };
  70. enum io_type {
  71. FILE_IO,
  72. NET_IO,
  73. WTF_IO
  74. };
  75. struct io {
  76. int fd;
  77. enum io_type type;
  78. char *fname;
  79. struct in_addr addr;
  80. unsigned int port;
  81. // Used only for output
  82. int ttl;
  83. struct in_addr intf;
  84. };
  85. struct ts {
  86. // Stream handling
  87. struct ts_pat *pat, *curpat;
  88. struct ts_cat *cat, *curcat;
  89. struct ts_pmt *pmt, *curpmt;
  90. struct ts_privsec *emm, *last_emm;
  91. struct ts_privsec *ecm, *last_ecm;
  92. struct ts_privsec *tmp_emm;
  93. struct ts_privsec *tmp_ecm;
  94. uint16_t pmt_pid;
  95. uint16_t service_id;
  96. uint16_t emm_caid, emm_pid;
  97. uint16_t ecm_caid, ecm_pid;
  98. uint16_t forced_caid;
  99. uint16_t forced_emm_pid;
  100. uint16_t forced_ecm_pid;
  101. uint16_t ecm_counter;
  102. pidmap_t pidmap;
  103. pidmap_t cc; // Continuity counters
  104. pidmap_t pid_seen;
  105. // Stats
  106. unsigned int emm_seen_count;
  107. unsigned int emm_processed_count;
  108. unsigned int emm_report_interval;
  109. time_t emm_last_report;
  110. unsigned int ecm_seen_count;
  111. unsigned int ecm_processed_count;
  112. unsigned int ecm_duplicate_count;
  113. unsigned int ecm_report_interval;
  114. time_t ecm_last_report;
  115. unsigned int cw_warn_sec;
  116. time_t cw_last_warn;
  117. // CAMD handling
  118. struct key key;
  119. struct camd35 camd35;
  120. // Config
  121. char ident[128];
  122. char syslog_host[128];
  123. int syslog_port;
  124. int syslog_active;
  125. int syslog_remote;
  126. int daemonize;
  127. char pidfile[PATH_MAX];
  128. enum CA_system req_CA_sys;
  129. int emm_send;
  130. int emm_only;
  131. int pid_filter;
  132. uint8_t irdeto_ecm;
  133. int ecm_cw_log;
  134. int rtp_input;
  135. struct io input;
  136. struct io output;
  137. int debug_level;
  138. int ts_discont;
  139. int camd_stop;
  140. int is_cw_error;
  141. int threaded;
  142. int packet_delay;
  143. int decode_stop;
  144. pthread_t decode_thread;
  145. CBUF *decode_buf;
  146. int write_stop;
  147. pthread_t write_thread;
  148. CBUF *write_buf;
  149. struct notify *notify;
  150. char notify_program[512];
  151. };
  152. enum msg_type { EMM_MSG, ECM_MSG };
  153. struct camd_msg {
  154. enum msg_type type;
  155. uint16_t idx;
  156. uint16_t ca_id;
  157. uint16_t service_id;
  158. uint8_t data_len;
  159. uint8_t data[255];
  160. struct ts *ts;
  161. };
  162. void data_init(struct ts *ts);
  163. void data_free(struct ts *ts);
  164. #endif