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

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