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

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