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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. notify(ts, "NO_CODE_WORD", "No code word was set in %ld sec. Decryption is disabled.",
  156. now - ts->key.ts);
  157. ts_LOGf("CW | *ERR* No valid code word was received in %ld seconds. Decryption is disabled.\n",
  158. now - ts->key.ts);
  159. ts->cw_last_warn = time(NULL);
  160. ts->cw_next_warn = ts->cw_last_warn + ts->cw_warn_sec;
  161. ts->cw_next_warn -= now - ts->key.ts;
  162. if (ts->cw_next_warn <= ts->cw_last_warn)
  163. ts->cw_next_warn = ts->cw_last_warn + ts->cw_warn_sec;
  164. }
  165. c->key->is_valid_cw = 0;
  166. }
  167. return 0;
  168. }
  169. return ret;
  170. }
  171. static int camd_send_emm(struct ts *ts, struct camd_msg *msg) {
  172. struct camd *c = &ts->camd;
  173. int ret = c->ops.do_emm(c, msg);
  174. if (ret < 1) {
  175. c->emm_recv_errors++;
  176. if (c->check_emm_errors || c->emm_recv_errors >= EMM_RECV_ERRORS_LIMIT) {
  177. ts_LOGf("ERR | Error sending emm packet, reconnecting to camd.\n");
  178. camd_reconnect(c);
  179. c->emm_recv_errors = 0;
  180. }
  181. } else {
  182. c->emm_recv_errors = 0;
  183. }
  184. return ret;
  185. }
  186. static void camd_do_msg(struct camd_msg *msg) {
  187. if (!keep_running)
  188. goto OUT;
  189. if (msg->type == EMM_MSG) {
  190. msg->ts->emm_seen_count++;
  191. if (camd_send_emm(msg->ts, msg) > 0)
  192. msg->ts->emm_processed_count++;
  193. }
  194. if (msg->type == ECM_MSG) {
  195. msg->ts->ecm_seen_count++;
  196. if (camd_send_ecm(msg->ts, msg) > 0)
  197. msg->ts->ecm_processed_count++;
  198. }
  199. OUT:
  200. camd_msg_free(&msg);
  201. }
  202. 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) {
  203. struct camd_msg *c = calloc(1, sizeof(struct camd_msg));
  204. c->type = msg_type;
  205. c->ca_id = ca_id;
  206. c->service_id = service_id;
  207. c->data_len = data_len;
  208. memcpy(c->data, data, data_len);
  209. return c;
  210. }
  211. void camd_msg_free(struct camd_msg **pmsg) {
  212. struct camd_msg *m = *pmsg;
  213. if (m) {
  214. FREE(*pmsg);
  215. }
  216. }
  217. static void *camd_thread(void *in_ts) {
  218. struct ts *ts = in_ts;
  219. set_thread_name("tsdec-camd");
  220. while (keep_running) {
  221. struct camd_msg *msg;
  222. void *req = queue_get(ts->camd.req_queue); // Waits...
  223. if (ts->camd_stop)
  224. break;
  225. if (!req)
  226. continue;
  227. msg = queue_get_nowait(ts->camd.ecm_queue);
  228. if (!msg)
  229. msg = queue_get_nowait(ts->camd.emm_queue);
  230. if (!msg)
  231. continue;
  232. camd_do_msg(msg);
  233. if (ts->camd.ecm_queue->items >= ECM_QUEUE_HARD_LIMIT) {
  234. ts_LOGf("WRN | Too much items (%d) in ECM queue, dropping the oldest.\n", ts->camd.ecm_queue->items);
  235. while(ts->camd.ecm_queue->items >= ECM_QUEUE_SOFT_LIMIT) {
  236. msg = queue_get_nowait(ts->camd.ecm_queue);
  237. camd_msg_free(&msg);
  238. }
  239. }
  240. if (ts->camd.emm_queue->items >= EMM_QUEUE_HARD_LIMIT) {
  241. ts_LOGf("WRN | Too much items (%d) in EMM queue, dropping the oldest.%s\n",
  242. ts->camd.emm_queue->items, ts->camd.ops.proto == CAMD_NEWCAMD ?
  243. " Consider switching to cs378x protocol!" : "");
  244. while(ts->camd.emm_queue->items >= EMM_QUEUE_SOFT_LIMIT) {
  245. msg = queue_get_nowait(ts->camd.emm_queue);
  246. camd_msg_free(&msg);
  247. }
  248. }
  249. // Flush request queue
  250. while(ts->camd.req_queue->items > ts->camd.emm_queue->items + ts->camd.ecm_queue->items) {
  251. queue_get_nowait(ts->camd.req_queue);
  252. }
  253. }
  254. // Flush ECM queue
  255. while (ts->camd.ecm_queue->items) {
  256. struct camd_msg *msg = queue_get_nowait(ts->camd.ecm_queue);
  257. camd_msg_free(&msg);
  258. }
  259. // Flush EMM queue
  260. while (ts->camd.emm_queue->items) {
  261. struct camd_msg *msg = queue_get_nowait(ts->camd.emm_queue);
  262. camd_msg_free(&msg);
  263. }
  264. pthread_exit(EXIT_SUCCESS);
  265. }
  266. void camd_process_packet(struct ts *ts, struct camd_msg *msg) {
  267. if (!msg)
  268. return;
  269. if (ts->camd.constant_codeword)
  270. return;
  271. msg->ts = ts;
  272. if (ts->camd.thread) {
  273. if (msg->type == EMM_MSG)
  274. queue_add(ts->camd.emm_queue, msg);
  275. if (msg->type == ECM_MSG)
  276. queue_add(ts->camd.ecm_queue, msg);
  277. queue_add(ts->camd.req_queue, msg);
  278. } else {
  279. camd_do_msg(msg);
  280. }
  281. }
  282. void camd_start(struct ts *ts) {
  283. struct camd *c = &ts->camd;
  284. if (c->constant_codeword)
  285. return;
  286. c->ops.connect(c);
  287. // The input is not file, process messages using async thread
  288. if (ts->threaded) {
  289. c->req_queue = queue_new();
  290. c->ecm_queue = queue_new();
  291. c->emm_queue = queue_new();
  292. pthread_create(&c->thread, &ts->thread_attr , &camd_thread, ts);
  293. }
  294. }
  295. void camd_stop(struct ts *ts) {
  296. struct camd *c = &ts->camd;
  297. if (c->constant_codeword)
  298. return;
  299. ts->camd_stop = 1;
  300. if (c->thread) {
  301. queue_add(c->req_queue, NULL);
  302. queue_wakeup(c->req_queue);
  303. pthread_join(c->thread, NULL);
  304. queue_free(&c->req_queue);
  305. queue_free(&c->ecm_queue);
  306. queue_free(&c->emm_queue);
  307. c->thread = 0;
  308. }
  309. c->ops.disconnect(c);
  310. }