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

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