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.2KB

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