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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. int ai_family = AF_UNSPEC;
  30. extern int keep_running;
  31. static uint8_t invalid_cw[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  32. int connect_client(int socktype, const char *hostname, const char *service) {
  33. struct addrinfo hints, *res;
  34. int n;
  35. memset(&hints, 0, sizeof(struct addrinfo));
  36. hints.ai_family = ai_family;
  37. hints.ai_socktype = socktype;
  38. ts_LOGf("CAM | Connecting to server %s port %s\n", hostname, service);
  39. n = getaddrinfo(hostname, service, &hints, &res);
  40. if (n < 0) {
  41. ts_LOGf("CAM | ERROR: getaddrinfo(%s): %s\n", hostname, gai_strerror(n));
  42. return -1;
  43. }
  44. int sockfd = -1;
  45. struct addrinfo *ressave = res;
  46. char str_addr[INET6_ADDRSTRLEN] = { 0 };
  47. while (res) {
  48. sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  49. if (sockfd > -1) {
  50. my_inet_ntop(res->ai_family, res->ai_addr, str_addr, sizeof(str_addr));
  51. if (do_connect(sockfd, res->ai_addr, res->ai_addrlen, 1000) < 0) {
  52. ts_LOGf("CAM | Error connecting to server %s port %s (addr=%s) | %s\n",
  53. hostname, service, str_addr, strerror(errno));
  54. close(sockfd);
  55. sockfd = -1;
  56. } else {
  57. break; // connected
  58. }
  59. } else {
  60. ts_LOGf("CAM | Could not create socket: %s\n", strerror(errno));
  61. sleep(1);
  62. return -1;
  63. }
  64. res = res->ai_next;
  65. }
  66. freeaddrinfo(ressave);
  67. if (socktype == SOCK_STREAM) {
  68. int flag = 1;
  69. setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
  70. }
  71. ts_LOGf("CAM | Connected to server %s port %s (addr=%s fd=%d).\n",
  72. hostname, service, str_addr, sockfd);
  73. return sockfd;
  74. }
  75. static inline void camd_reconnect(struct camd *c) {
  76. if (!keep_running)
  77. return;
  78. if (c->no_reconnect)
  79. return;
  80. c->ops.reconnect(c);
  81. }
  82. void camd_set_cw(struct ts *ts, uint8_t *new_cw, int check_validity) {
  83. struct camd *c = &ts->camd;
  84. c->ecm_recv_errors = 0;
  85. gettimeofday(&c->key->ts_keyset, NULL);
  86. c->key->ts = c->key->ts_keyset.tv_sec;
  87. ts->cw_last_warn = c->key->ts;
  88. if (!check_validity || memcmp(new_cw, invalid_cw, 8) != 0)
  89. csa_set_even_cw(c->key->csakey, new_cw);
  90. if (!check_validity || memcmp(new_cw + 8, invalid_cw, 8) != 0)
  91. csa_set_odd_cw(c->key->csakey, new_cw + 8);
  92. }
  93. static int camd_recv_cw(struct ts *ts) {
  94. struct camd *c = &ts->camd;
  95. struct timeval tv1, tv2, last_ts_keyset;
  96. uint16_t ca_id = 0;
  97. uint16_t idx = 0;
  98. int ret;
  99. gettimeofday(&tv1, NULL);
  100. ret = c->ops.get_cw(c, &ca_id, &idx, c->key->cw);
  101. gettimeofday(&tv2, NULL);
  102. if (!keep_running)
  103. return 0;
  104. if (ret <= 0) {
  105. // get_cw returned error, lets try other ecm pids, we might be lucky...
  106. if (ts->n_ecm_pids > 1) {
  107. ts->ecm_pid_idx++;
  108. if (ts->ecm_pid_idx + 1 > ts->n_ecm_pids)
  109. ts->ecm_pid_idx = 0;
  110. ts->ecm_pid = ts->ecm_pids[ts->ecm_pid_idx];
  111. ts_LOGf("ECM | Switching ECM pid to 0x%04x (%d) idx:%d\n", ts->ecm_pid, ts->ecm_pid, ts->ecm_pid_idx);
  112. }
  113. if (ret == -1) { // Fatal error it is better to reconnect to server.
  114. ts_LOGf("ERR | No code word has been received (ret = %d)\n", ret);
  115. camd_reconnect(c);
  116. }
  117. c->ecm_recv_errors++;
  118. if (c->ecm_recv_errors >= ECM_RECV_ERRORS_LIMIT) {
  119. c->key->is_valid_cw = 0;
  120. memset(c->key->cw, 0, 16); // Invalid CW
  121. }
  122. usleep(10000);
  123. return 0;
  124. }
  125. char cw_dump[16 * 6];
  126. ts_hex_dump_buf(cw_dump, 16 * 6, c->key->cw, 16, 0);
  127. int valid_cw = memcmp(c->key->cw, invalid_cw, 16) != 0;
  128. if (!c->key->is_valid_cw && valid_cw) {
  129. ts_LOGf("CW | OK: Valid code word was received.\n");
  130. notify(ts, "CODE_WORD_OK", "Valid code word was received.");
  131. }
  132. c->key->is_valid_cw = valid_cw;
  133. // At first ts_keyset is not initialized
  134. last_ts_keyset = c->key->ts_keyset;
  135. if (c->key->is_valid_cw)
  136. camd_set_cw(ts, c->key->cw, 1);
  137. if (ts->ecm_cw_log) {
  138. ts_LOGf("CW | SID 0x%04x CAID: 0x%04x CW_recv: %5llu ms LastKey: %5llu ms Data: %s\n",
  139. ts->service_id,
  140. ca_id,
  141. timeval_diff_msec(&tv1, &tv2),
  142. timeval_diff_msec(&last_ts_keyset, &tv2),
  143. cw_dump );
  144. }
  145. return 1;
  146. }
  147. #undef ERR
  148. static int camd_send_ecm(struct ts *ts, struct camd_msg *msg) {
  149. struct camd *c = &ts->camd;
  150. int ret = c->ops.do_ecm(c, msg);
  151. if (ret <= 0) {
  152. ts_LOGf("ERR | Error sending ecm packet, reconnecting to camd.\n");
  153. ts->is_cw_error = 1;
  154. camd_reconnect(c);
  155. return ret;
  156. }
  157. ret = camd_recv_cw(ts);
  158. if (ret < 1) {
  159. time_t now = time(NULL);
  160. ts->is_cw_error = 1;
  161. if (ts->key.ts && now - ts->key.ts > KEY_VALID_TIME) {
  162. if (c->key->is_valid_cw) {
  163. if (!ts->stream_is_not_scrambled || !ts->have_valid_pmt || ts->no_input) {
  164. notify(ts, "NO_CODE_WORD", "No code word was set in %ld sec. Decryption is disabled.",
  165. now - ts->key.ts);
  166. ts_LOGf("CW | *ERR* No valid code word was received in %ld seconds. Decryption is disabled.\n",
  167. now - ts->key.ts);
  168. }
  169. ts->cw_last_warn = time(NULL);
  170. ts->cw_next_warn = ts->cw_last_warn + ts->cw_warn_sec;
  171. ts->cw_next_warn -= now - ts->key.ts;
  172. if (ts->cw_next_warn <= ts->cw_last_warn)
  173. ts->cw_next_warn = ts->cw_last_warn + ts->cw_warn_sec;
  174. }
  175. c->key->is_valid_cw = 0;
  176. }
  177. return 0;
  178. }
  179. return ret;
  180. }
  181. static int camd_send_emm(struct ts *ts, struct camd_msg *msg) {
  182. struct camd *c = &ts->camd;
  183. int ret = c->ops.do_emm(c, msg);
  184. if (ret < 1) {
  185. c->emm_recv_errors++;
  186. if (c->check_emm_errors || c->emm_recv_errors >= EMM_RECV_ERRORS_LIMIT) {
  187. ts_LOGf("ERR | Error sending emm packet, reconnecting to camd.\n");
  188. camd_reconnect(c);
  189. c->emm_recv_errors = 0;
  190. }
  191. } else {
  192. c->emm_recv_errors = 0;
  193. }
  194. return ret;
  195. }
  196. static void camd_do_msg(struct camd_msg *msg) {
  197. if (!keep_running)
  198. goto OUT;
  199. if (msg->type == EMM_MSG) {
  200. msg->ts->emm_seen_count++;
  201. if (camd_send_emm(msg->ts, msg) > 0)
  202. msg->ts->emm_processed_count++;
  203. }
  204. if (msg->type == ECM_MSG) {
  205. msg->ts->ecm_seen_count++;
  206. if (camd_send_ecm(msg->ts, msg) > 0)
  207. msg->ts->ecm_processed_count++;
  208. }
  209. OUT:
  210. camd_msg_free(&msg);
  211. }
  212. struct camd_msg *camd_msg_alloc(enum msg_type msg_type, uint16_t ca_id, uint16_t service_id, uint8_t *data, int data_len) {
  213. struct camd_msg *c = calloc(1, sizeof(struct camd_msg));
  214. if (data_len > (int)sizeof(c->data)) {
  215. // ts_LOGf("ERROR: Tried to allocate too big CAMD message: %d max: %lu\n", data_len, sizeof(c->data));
  216. return NULL;
  217. }
  218. c->type = msg_type;
  219. c->ca_id = ca_id;
  220. c->service_id = service_id;
  221. c->data_len = data_len;
  222. memcpy(c->data, data, data_len);
  223. return c;
  224. }
  225. void camd_msg_free(struct camd_msg **pmsg) {
  226. struct camd_msg *m = *pmsg;
  227. if (m) {
  228. FREE(*pmsg);
  229. }
  230. }
  231. static void *camd_thread(void *in_ts) {
  232. struct ts *ts = in_ts;
  233. set_thread_name("tsdec-camd");
  234. while (keep_running) {
  235. struct camd_msg *msg;
  236. void *req = queue_get(ts->camd.req_queue); // Waits...
  237. if (ts->camd_stop)
  238. break;
  239. if (!req)
  240. continue;
  241. msg = queue_get_nowait(ts->camd.ecm_queue);
  242. if (!msg)
  243. msg = queue_get_nowait(ts->camd.emm_queue);
  244. if (!msg)
  245. continue;
  246. camd_do_msg(msg);
  247. if (ts->camd.ecm_queue->items >= ECM_QUEUE_HARD_LIMIT) {
  248. ts_LOGf("WRN | Too many items (%d) in ECM queue, dropping the oldest.\n", ts->camd.ecm_queue->items);
  249. while(ts->camd.ecm_queue->items >= ECM_QUEUE_SOFT_LIMIT) {
  250. msg = queue_get_nowait(ts->camd.ecm_queue);
  251. camd_msg_free(&msg);
  252. }
  253. }
  254. if (ts->camd.emm_queue->items >= EMM_QUEUE_HARD_LIMIT) {
  255. ts_LOGf("WRN | Too many items (%d) in EMM queue, dropping the oldest.%s\n",
  256. ts->camd.emm_queue->items, ts->camd.ops.proto == CAMD_NEWCAMD ?
  257. " Consider switching to cs378x protocol!" : "");
  258. while(ts->camd.emm_queue->items >= EMM_QUEUE_SOFT_LIMIT) {
  259. msg = queue_get_nowait(ts->camd.emm_queue);
  260. camd_msg_free(&msg);
  261. }
  262. }
  263. // Flush request queue
  264. while(ts->camd.req_queue->items > ts->camd.emm_queue->items + ts->camd.ecm_queue->items) {
  265. queue_get_nowait(ts->camd.req_queue);
  266. }
  267. }
  268. // Flush ECM queue
  269. while (ts->camd.ecm_queue->items) {
  270. struct camd_msg *msg = queue_get_nowait(ts->camd.ecm_queue);
  271. camd_msg_free(&msg);
  272. }
  273. // Flush EMM queue
  274. while (ts->camd.emm_queue->items) {
  275. struct camd_msg *msg = queue_get_nowait(ts->camd.emm_queue);
  276. camd_msg_free(&msg);
  277. }
  278. pthread_exit(EXIT_SUCCESS);
  279. }
  280. void camd_process_packet(struct ts *ts, struct camd_msg *msg) {
  281. if (!msg)
  282. return;
  283. if (ts->camd.constant_codeword)
  284. return;
  285. msg->ts = ts;
  286. if (ts->camd.thread) {
  287. if (msg->type == EMM_MSG)
  288. queue_add(ts->camd.emm_queue, msg);
  289. if (msg->type == ECM_MSG)
  290. queue_add(ts->camd.ecm_queue, msg);
  291. queue_add(ts->camd.req_queue, msg);
  292. } else {
  293. camd_do_msg(msg);
  294. }
  295. }
  296. void camd_start(struct ts *ts) {
  297. struct camd *c = &ts->camd;
  298. if (c->constant_codeword)
  299. return;
  300. c->ops.connect(c);
  301. // The input is not file, process messages using async thread
  302. if (ts->threaded) {
  303. c->req_queue = queue_new();
  304. c->ecm_queue = queue_new();
  305. c->emm_queue = queue_new();
  306. pthread_create(&c->thread, &ts->thread_attr , &camd_thread, ts);
  307. }
  308. }
  309. void camd_stop(struct ts *ts) {
  310. struct camd *c = &ts->camd;
  311. if (c->constant_codeword)
  312. return;
  313. ts->camd_stop = 1;
  314. if (c->thread) {
  315. queue_add(c->req_queue, NULL);
  316. queue_wakeup(c->req_queue);
  317. pthread_join(c->thread, NULL);
  318. queue_free(&c->req_queue);
  319. queue_free(&c->ecm_queue);
  320. queue_free(&c->emm_queue);
  321. c->thread = 0;
  322. }
  323. c->ops.disconnect(c);
  324. }