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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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(1000 * 1000); // 1 ms * 1000 == 1 sec
  123. // drain ecm queue
  124. while(c->ecm_queue->items > 1) {
  125. struct camd_msg *msg = queue_get_nowait(c->ecm_queue);
  126. camd_msg_free(&msg);
  127. }
  128. return 0;
  129. }
  130. char cw_dump[16 * 6];
  131. ts_hex_dump_buf(cw_dump, 16 * 6, c->key->cw, 16, 0);
  132. int valid_cw = memcmp(c->key->cw, invalid_cw, 16) != 0;
  133. if (!c->key->is_valid_cw && valid_cw) {
  134. ts_LOGf("CW | OK: Valid code word was received.\n");
  135. notify(ts, "CODE_WORD_OK", "Valid code word was received.");
  136. }
  137. c->key->is_valid_cw = valid_cw;
  138. // At first ts_keyset is not initialized
  139. last_ts_keyset = c->key->ts_keyset;
  140. if (c->key->is_valid_cw)
  141. camd_set_cw(ts, c->key->cw, 1);
  142. if (ts->ecm_cw_log) {
  143. ts_LOGf("CW | SID 0x%04x CAID: 0x%04x CW_recv: %5llu ms LastKey: %5llu ms Data: %s\n",
  144. ts->service_id,
  145. ca_id,
  146. timeval_diff_msec(&tv1, &tv2),
  147. timeval_diff_msec(&last_ts_keyset, &tv2),
  148. cw_dump );
  149. }
  150. return 1;
  151. }
  152. #undef ERR
  153. static int camd_send_ecm(struct ts *ts, struct camd_msg *msg) {
  154. struct camd *c = &ts->camd;
  155. int ret = c->ops.do_ecm(c, msg);
  156. if (ret <= 0) {
  157. ts_LOGf("ERR | Error sending ecm packet, reconnecting to camd.\n");
  158. ts->is_cw_error = 1;
  159. camd_reconnect(c);
  160. return ret;
  161. }
  162. ret = camd_recv_cw(ts);
  163. if (ret < 1) {
  164. time_t now = time(NULL);
  165. ts->is_cw_error = 1;
  166. if (ts->key.ts && now - ts->key.ts > KEY_VALID_TIME) {
  167. if (c->key->is_valid_cw) {
  168. if (!ts->stream_is_not_scrambled || !ts->have_valid_pmt || ts->no_input) {
  169. notify(ts, "NO_CODE_WORD", "No code word was set in %ld sec. Decryption is disabled.",
  170. now - ts->key.ts);
  171. ts_LOGf("CW | *ERR* No valid code word was received in %ld seconds. Decryption is disabled.\n",
  172. now - ts->key.ts);
  173. }
  174. ts->cw_last_warn = time(NULL);
  175. ts->cw_next_warn = ts->cw_last_warn + ts->cw_warn_sec;
  176. ts->cw_next_warn -= now - ts->key.ts;
  177. if (ts->cw_next_warn <= ts->cw_last_warn)
  178. ts->cw_next_warn = ts->cw_last_warn + ts->cw_warn_sec;
  179. }
  180. c->key->is_valid_cw = 0;
  181. }
  182. return 0;
  183. }
  184. return ret;
  185. }
  186. static int camd_send_emm(struct ts *ts, struct camd_msg *msg) {
  187. struct camd *c = &ts->camd;
  188. int ret = c->ops.do_emm(c, msg);
  189. if (ret < 1) {
  190. c->emm_recv_errors++;
  191. if (c->check_emm_errors || c->emm_recv_errors >= EMM_RECV_ERRORS_LIMIT) {
  192. ts_LOGf("ERR | Error sending emm packet, reconnecting to camd.\n");
  193. camd_reconnect(c);
  194. c->emm_recv_errors = 0;
  195. }
  196. } else {
  197. c->emm_recv_errors = 0;
  198. }
  199. return ret;
  200. }
  201. static void camd_do_msg(struct camd_msg *msg) {
  202. if (!keep_running)
  203. goto OUT;
  204. if (msg->type == EMM_MSG) {
  205. msg->ts->emm_seen_count++;
  206. if (camd_send_emm(msg->ts, msg) > 0)
  207. msg->ts->emm_processed_count++;
  208. }
  209. if (msg->type == ECM_MSG) {
  210. msg->ts->ecm_seen_count++;
  211. if (camd_send_ecm(msg->ts, msg) > 0)
  212. msg->ts->ecm_processed_count++;
  213. }
  214. OUT:
  215. camd_msg_free(&msg);
  216. }
  217. 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) {
  218. struct camd_msg *c = calloc(1, sizeof(struct camd_msg));
  219. if (data_len > (int)sizeof(c->data)) {
  220. // ts_LOGf("ERROR: Tried to allocate too big CAMD message: %d max: %lu\n", data_len, sizeof(c->data));
  221. return NULL;
  222. }
  223. c->type = msg_type;
  224. c->ca_id = ca_id;
  225. c->service_id = service_id;
  226. c->data_len = data_len;
  227. memcpy(c->data, data, data_len);
  228. return c;
  229. }
  230. void camd_msg_free(struct camd_msg **pmsg) {
  231. struct camd_msg *m = *pmsg;
  232. if (m) {
  233. FREE(*pmsg);
  234. }
  235. }
  236. static void *camd_thread(void *in_ts) {
  237. struct ts *ts = in_ts;
  238. set_thread_name("tsdec-camd");
  239. while (keep_running) {
  240. struct camd_msg *msg;
  241. void *req = queue_get(ts->camd.req_queue); // Waits...
  242. if (ts->camd_stop)
  243. break;
  244. if (!req)
  245. continue;
  246. msg = queue_get_nowait(ts->camd.ecm_queue);
  247. if (!msg)
  248. msg = queue_get_nowait(ts->camd.emm_queue);
  249. if (!msg)
  250. continue;
  251. camd_do_msg(msg);
  252. if (ts->camd.ecm_queue->items >= ECM_QUEUE_HARD_LIMIT) {
  253. ts_LOGf("WRN | Too many items (%d) in ECM queue, dropping the oldest.\n", ts->camd.ecm_queue->items);
  254. while(ts->camd.ecm_queue->items >= ECM_QUEUE_SOFT_LIMIT) {
  255. msg = queue_get_nowait(ts->camd.ecm_queue);
  256. camd_msg_free(&msg);
  257. }
  258. }
  259. if (ts->camd.emm_queue->items >= EMM_QUEUE_HARD_LIMIT) {
  260. ts_LOGf("WRN | Too many items (%d) in EMM queue, dropping the oldest.%s\n",
  261. ts->camd.emm_queue->items, ts->camd.ops.proto == CAMD_NEWCAMD ?
  262. " Consider switching to cs378x protocol!" : "");
  263. while(ts->camd.emm_queue->items >= EMM_QUEUE_SOFT_LIMIT) {
  264. msg = queue_get_nowait(ts->camd.emm_queue);
  265. camd_msg_free(&msg);
  266. }
  267. }
  268. // Flush request queue
  269. while(ts->camd.req_queue->items > ts->camd.emm_queue->items + ts->camd.ecm_queue->items) {
  270. queue_get_nowait(ts->camd.req_queue);
  271. }
  272. }
  273. // Flush ECM queue
  274. while (ts->camd.ecm_queue->items) {
  275. struct camd_msg *msg = queue_get_nowait(ts->camd.ecm_queue);
  276. camd_msg_free(&msg);
  277. }
  278. // Flush EMM queue
  279. while (ts->camd.emm_queue->items) {
  280. struct camd_msg *msg = queue_get_nowait(ts->camd.emm_queue);
  281. camd_msg_free(&msg);
  282. }
  283. pthread_exit(EXIT_SUCCESS);
  284. }
  285. void camd_process_packet(struct ts *ts, struct camd_msg *msg) {
  286. if (!msg)
  287. return;
  288. if (ts->camd.constant_codeword)
  289. return;
  290. msg->ts = ts;
  291. if (ts->camd.thread) {
  292. if (msg->type == EMM_MSG)
  293. queue_add(ts->camd.emm_queue, msg);
  294. if (msg->type == ECM_MSG)
  295. queue_add(ts->camd.ecm_queue, msg);
  296. queue_add(ts->camd.req_queue, msg);
  297. } else {
  298. camd_do_msg(msg);
  299. }
  300. }
  301. void camd_start(struct ts *ts) {
  302. struct camd *c = &ts->camd;
  303. if (c->constant_codeword)
  304. return;
  305. c->ops.connect(c);
  306. // The input is not file, process messages using async thread
  307. if (ts->threaded) {
  308. c->req_queue = queue_new();
  309. c->ecm_queue = queue_new();
  310. c->emm_queue = queue_new();
  311. pthread_create(&c->thread, &ts->thread_attr , &camd_thread, ts);
  312. }
  313. }
  314. void camd_stop(struct ts *ts) {
  315. struct camd *c = &ts->camd;
  316. if (c->constant_codeword)
  317. return;
  318. ts->camd_stop = 1;
  319. if (c->thread) {
  320. queue_add(c->req_queue, NULL);
  321. queue_wakeup(c->req_queue);
  322. pthread_join(c->thread, NULL);
  323. queue_free(&c->req_queue);
  324. queue_free(&c->ecm_queue);
  325. queue_free(&c->emm_queue);
  326. c->thread = 0;
  327. }
  328. c->ops.disconnect(c);
  329. }