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.

camd.c 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * CAMD communications
  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. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <string.h>
  21. #include <sys/errno.h>
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24. #include <netinet/tcp.h>
  25. #include <arpa/inet.h>
  26. #include "libfuncs/libfuncs.h"
  27. #include "data.h"
  28. #include "csa.h"
  29. #include "util.h"
  30. #include "camd.h"
  31. #include "notify.h"
  32. static uint8_t invalid_cw[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  33. int camd_tcp_connect(struct in_addr ip, int port) {
  34. ts_LOGf("CAM | Connecting to server %s:%d\n", inet_ntoa(ip), port);
  35. int fd = socket(PF_INET, SOCK_STREAM, 0);
  36. if (fd < 0) {
  37. ts_LOGf("CAM | Could not create socket | %s\n", strerror(errno));
  38. return -1;
  39. }
  40. struct sockaddr_in sock;
  41. sock.sin_family = AF_INET;
  42. sock.sin_port = htons(port);
  43. sock.sin_addr = ip;
  44. if (do_connect(fd, (struct sockaddr *)&sock, sizeof(sock), 1000) < 0) {
  45. ts_LOGf("CAM | Could not connect to server %s:%d | %s\n", inet_ntoa(ip), port, strerror(errno));
  46. close(fd);
  47. sleep(1);
  48. return -1;
  49. }
  50. int flag = 1;
  51. setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
  52. ts_LOGf("CAM | Connected to fd:%d\n", fd);
  53. return fd;
  54. }
  55. void camd_set_cw(struct ts *ts, uint8_t *new_cw, int check_validity) {
  56. struct camd *c = &ts->camd;
  57. c->ecm_recv_errors = 0;
  58. gettimeofday(&c->key->ts_keyset, NULL);
  59. c->key->ts = c->key->ts_keyset.tv_sec;
  60. ts->cw_last_warn = c->key->ts;
  61. if (!check_validity || memcmp(new_cw, invalid_cw, 8) != 0)
  62. csa_set_even_cw(c->key->csakey, new_cw);
  63. if (!check_validity || memcmp(new_cw + 8, invalid_cw, 8) != 0)
  64. csa_set_odd_cw(c->key->csakey, new_cw + 8);
  65. }
  66. static int camd_recv_cw(struct ts *ts) {
  67. struct camd *c = &ts->camd;
  68. struct timeval tv1, tv2, last_ts_keyset;
  69. uint16_t ca_id = 0;
  70. uint16_t idx = 0;
  71. int ret;
  72. gettimeofday(&tv1, NULL);
  73. ret = c->ops.get_cw(c, &ca_id, &idx, c->key->cw);
  74. gettimeofday(&tv2, NULL);
  75. if (ret <= 0) {
  76. if (ret == -1) { // Fatal error it is better to reconnect to server.
  77. ts_LOGf("ERR | No code word has been received (ret = %d)\n", ret);
  78. c->ops.reconnect(c);
  79. }
  80. c->ecm_recv_errors++;
  81. if (c->ecm_recv_errors >= ECM_RECV_ERRORS_LIMIT) {
  82. c->key->is_valid_cw = 0;
  83. memset(c->key->cw, 0, 16); // Invalid CW
  84. }
  85. usleep(10000);
  86. return 0;
  87. }
  88. char cw_dump[16 * 6];
  89. ts_hex_dump_buf(cw_dump, 16 * 6, c->key->cw, 16, 0);
  90. int valid_cw = memcmp(c->key->cw, invalid_cw, 16) != 0;
  91. if (!c->key->is_valid_cw && valid_cw) {
  92. ts_LOGf("CW | OK: Valid code word was received.\n");
  93. notify(ts, "CODE_WORD_OK", "Valid code word was received.");
  94. }
  95. c->key->is_valid_cw = valid_cw;
  96. // At first ts_keyset is not initialized
  97. last_ts_keyset = c->key->ts_keyset;
  98. if (c->key->is_valid_cw)
  99. camd_set_cw(ts, c->key->cw, 1);
  100. if (ts->ecm_cw_log) {
  101. ts_LOGf("CW | SID 0x%04x CAID: 0x%04x CW_recv: %5llu ms LastKey: %5llu ms Data: %s\n",
  102. ts->service_id,
  103. ca_id,
  104. timeval_diff_msec(&tv1, &tv2),
  105. timeval_diff_msec(&last_ts_keyset, &tv2),
  106. cw_dump );
  107. }
  108. return 1;
  109. }
  110. #undef ERR
  111. static int camd_send_ecm(struct ts *ts, struct camd_msg *msg) {
  112. struct camd *c = &ts->camd;
  113. int ret = c->ops.do_ecm(c, msg);
  114. if (ret <= 0) {
  115. ts_LOGf("ERR | Error sending ecm packet, reconnecting to camd.\n");
  116. ts->is_cw_error = 1;
  117. c->ops.reconnect(c);
  118. return ret;
  119. }
  120. ret = camd_recv_cw(ts);
  121. if (ret < 1) {
  122. time_t now = time(NULL);
  123. ts->is_cw_error = 1;
  124. if (ts->key.ts && now - ts->key.ts > KEY_VALID_TIME) {
  125. if (c->key->is_valid_cw) {
  126. notify(ts, "NO_CODE_WORD", "No code word was set in %ld sec. Decryption is disabled.",
  127. now - ts->key.ts);
  128. ts_LOGf("CW | *ERR* No valid code word was received in %ld seconds. Decryption is disabled.\n",
  129. now - ts->key.ts);
  130. ts->cw_last_warn = time(NULL);
  131. ts->cw_next_warn = ts->cw_last_warn + ts->cw_warn_sec;
  132. ts->cw_next_warn -= now - ts->key.ts;
  133. if (ts->cw_next_warn <= ts->cw_last_warn)
  134. ts->cw_next_warn = ts->cw_last_warn + ts->cw_warn_sec;
  135. }
  136. c->key->is_valid_cw = 0;
  137. }
  138. return 0;
  139. }
  140. return ret;
  141. }
  142. static int camd_send_emm(struct ts *ts, struct camd_msg *msg) {
  143. struct camd *c = &ts->camd;
  144. int ret = c->ops.do_emm(c, msg);
  145. if (ret < 1) {
  146. c->emm_recv_errors++;
  147. if (c->emm_recv_errors >= EMM_RECV_ERRORS_LIMIT) {
  148. ts_LOGf("ERR | Error sending emm packet, reconnecting to camd.\n");
  149. c->ops.reconnect(c);
  150. c->emm_recv_errors = 0;
  151. }
  152. } else {
  153. c->emm_recv_errors = 0;
  154. }
  155. return ret;
  156. }
  157. static void camd_do_msg(struct camd_msg *msg) {
  158. if (msg->type == EMM_MSG) {
  159. msg->ts->emm_seen_count++;
  160. if (camd_send_emm(msg->ts, msg) > 0)
  161. msg->ts->emm_processed_count++;
  162. }
  163. if (msg->type == ECM_MSG) {
  164. msg->ts->ecm_seen_count++;
  165. if (camd_send_ecm(msg->ts, msg) > 0)
  166. msg->ts->ecm_processed_count++;
  167. }
  168. camd_msg_free(&msg);
  169. }
  170. struct camd_msg *camd_msg_alloc(enum msg_type msg_type, uint16_t ca_id, uint16_t service_id, uint8_t *data, uint8_t data_len) {
  171. struct camd_msg *c = calloc(1, sizeof(struct camd_msg));
  172. c->type = msg_type;
  173. c->ca_id = ca_id;
  174. c->service_id = service_id;
  175. c->data_len = data_len;
  176. memcpy(c->data, data, data_len);
  177. return c;
  178. }
  179. void camd_msg_free(struct camd_msg **pmsg) {
  180. struct camd_msg *m = *pmsg;
  181. if (m) {
  182. FREE(*pmsg);
  183. }
  184. }
  185. static void *camd_thread(void *in_ts) {
  186. struct ts *ts = in_ts;
  187. set_thread_name("tsdec-camd");
  188. while (1) {
  189. struct camd_msg *msg;
  190. void *req = queue_get(ts->camd.req_queue); // Waits...
  191. if (ts->camd_stop)
  192. break;
  193. if (!req)
  194. continue;
  195. msg = queue_get_nowait(ts->camd.ecm_queue);
  196. if (!msg)
  197. msg = queue_get_nowait(ts->camd.emm_queue);
  198. if (!msg)
  199. continue;
  200. camd_do_msg(msg);
  201. if (ts->camd.ecm_queue->items >= ECM_QUEUE_HARD_LIMIT) {
  202. ts_LOGf("WRN | Too much items (%d) in ECM queue, dropping the oldest.\n", ts->camd.ecm_queue->items);
  203. while(ts->camd.ecm_queue->items >= ECM_QUEUE_SOFT_LIMIT) {
  204. msg = queue_get_nowait(ts->camd.ecm_queue);
  205. camd_msg_free(&msg);
  206. }
  207. }
  208. if (ts->camd.emm_queue->items >= EMM_QUEUE_HARD_LIMIT) {
  209. ts_LOGf("WRN | Too much items (%d) in EMM queue, dropping the oldest.%s\n",
  210. ts->camd.emm_queue->items, ts->camd.ops.proto == CAMD_NEWCAMD ?
  211. " Consider switching to cs378x protocol!" : "");
  212. while(ts->camd.emm_queue->items >= EMM_QUEUE_SOFT_LIMIT) {
  213. msg = queue_get_nowait(ts->camd.emm_queue);
  214. camd_msg_free(&msg);
  215. }
  216. }
  217. }
  218. // Flush ECM queue
  219. while (ts->camd.ecm_queue->items) {
  220. struct camd_msg *msg = queue_get_nowait(ts->camd.ecm_queue);
  221. camd_msg_free(&msg);
  222. }
  223. // Flush EMM queue
  224. while (ts->camd.emm_queue->items) {
  225. struct camd_msg *msg = queue_get_nowait(ts->camd.emm_queue);
  226. camd_msg_free(&msg);
  227. }
  228. pthread_exit(EXIT_SUCCESS);
  229. }
  230. void camd_process_packet(struct ts *ts, struct camd_msg *msg) {
  231. if (!msg)
  232. return;
  233. if (ts->camd.constant_codeword)
  234. return;
  235. msg->ts = ts;
  236. if (ts->camd.thread) {
  237. if (msg->type == EMM_MSG)
  238. queue_add(ts->camd.emm_queue, msg);
  239. if (msg->type == ECM_MSG)
  240. queue_add(ts->camd.ecm_queue, msg);
  241. queue_add(ts->camd.req_queue, msg);
  242. } else {
  243. camd_do_msg(msg);
  244. }
  245. }
  246. void camd_start(struct ts *ts) {
  247. struct camd *c = &ts->camd;
  248. if (c->constant_codeword)
  249. return;
  250. c->ops.connect(c);
  251. // The input is not file, process messages using async thread
  252. if (ts->threaded) {
  253. c->req_queue = queue_new();
  254. c->ecm_queue = queue_new();
  255. c->emm_queue = queue_new();
  256. pthread_create(&c->thread, &ts->thread_attr , &camd_thread, ts);
  257. }
  258. }
  259. void camd_stop(struct ts *ts) {
  260. struct camd *c = &ts->camd;
  261. if (c->constant_codeword)
  262. return;
  263. ts->camd_stop = 1;
  264. if (c->thread) {
  265. queue_add(c->req_queue, NULL);
  266. queue_wakeup(c->req_queue);
  267. pthread_join(c->thread, NULL);
  268. queue_free(&c->req_queue);
  269. queue_free(&c->ecm_queue);
  270. queue_free(&c->emm_queue);
  271. c->thread = 0;
  272. }
  273. c->ops.disconnect(c);
  274. }