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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  17. */
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <string.h>
  21. #include <sys/errno.h>
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24. #include <netinet/tcp.h>
  25. #include <arpa/inet.h>
  26. #include <dvbcsa/dvbcsa.h>
  27. #include "libfuncs/libfuncs.h"
  28. #include "data.h"
  29. #include "util.h"
  30. #include "camd.h"
  31. #include "notify.h"
  32. int camd_tcp_connect(struct in_addr ip, int port) {
  33. ts_LOGf("CAM | Connecting to server %s:%d\n", inet_ntoa(ip), port);
  34. int fd = socket(PF_INET, SOCK_STREAM, 0);
  35. if (fd < 0) {
  36. ts_LOGf("CAM | Could not create socket | %s\n", strerror(errno));
  37. return -1;
  38. }
  39. struct sockaddr_in sock;
  40. sock.sin_family = AF_INET;
  41. sock.sin_port = htons(port);
  42. sock.sin_addr = ip;
  43. if (do_connect(fd, (struct sockaddr *)&sock, sizeof(sock), 1000) < 0) {
  44. ts_LOGf("CAM | Could not connect to server %s:%d | %s\n", inet_ntoa(ip), port, strerror(errno));
  45. close(fd);
  46. sleep(1);
  47. return -1;
  48. }
  49. int flag = 1;
  50. setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
  51. ts_LOGf("CAM | Connected to fd:%d\n", fd);
  52. return fd;
  53. }
  54. static int camd_recv_cw(struct ts *ts) {
  55. struct camd *c = &ts->camd;
  56. struct timeval tv1, tv2, last_ts_keyset;
  57. static uint8_t invalid_cw[16] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  58. uint16_t ca_id = 0;
  59. uint16_t idx = 0;
  60. int ret;
  61. gettimeofday(&tv1, NULL);
  62. ret = c->ops.get_cw(c, &ca_id, &idx, c->key->cw);
  63. gettimeofday(&tv2, NULL);
  64. if (ret <= 0) {
  65. if (ret == -1) { // Fatal error it is better to reconnect to server.
  66. ts_LOGf("ERR | No code word has been received (ret = %d)\n", ret);
  67. c->ops.reconnect(c);
  68. }
  69. c->ecm_recv_errors++;
  70. if (c->ecm_recv_errors >= ECM_RECV_ERRORS_LIMIT) {
  71. c->key->is_valid_cw = 0;
  72. memset(c->key->cw, 0, 16); // Invalid CW
  73. }
  74. usleep(10000);
  75. return 0;
  76. }
  77. char cw_dump[16 * 6];
  78. ts_hex_dump_buf(cw_dump, 16 * 6, c->key->cw, 16, 0);
  79. int valid_cw = memcmp(c->key->cw, invalid_cw, 16) != 0;
  80. if (!c->key->is_valid_cw && valid_cw) {
  81. ts_LOGf("CW | OK: Valid code word was received.\n");
  82. notify(ts, "CODE_WORD_OK", "Valid code word was received.");
  83. }
  84. c->key->is_valid_cw = valid_cw;
  85. // At first ts_keyset is not initialized
  86. last_ts_keyset = c->key->ts_keyset;
  87. if (c->key->is_valid_cw) {
  88. c->ecm_recv_errors = 0;
  89. gettimeofday(&c->key->ts_keyset, NULL);
  90. c->key->ts = c->key->ts_keyset.tv_sec;
  91. ts->cw_last_warn = c->key->ts;
  92. if (memcmp(c->key->cw, invalid_cw, 8) != 0) {
  93. dvbcsa_key_set (c->key->cw, c->key->csakey[0]);
  94. dvbcsa_bs_key_set(c->key->cw, c->key->bs_csakey[0]);
  95. }
  96. if (memcmp(c->key->cw + 8, invalid_cw, 8) != 0) {
  97. dvbcsa_key_set(c->key->cw + 8, c->key->csakey[1]);
  98. dvbcsa_bs_key_set(c->key->cw + 8, c->key->bs_csakey[1]);
  99. }
  100. }
  101. if (ts->ecm_cw_log) {
  102. ts_LOGf("CW | CAID: 0x%04x [ %5llu ms ] ( %6llu ms ) ------ IDX: 0x%04x Data: %s\n",
  103. ca_id, timeval_diff_msec(&tv1, &tv2),
  104. timeval_diff_msec(&last_ts_keyset, &tv2),
  105. idx, cw_dump );
  106. }
  107. return 1;
  108. }
  109. #undef ERR
  110. static int camd_send_ecm(struct ts *ts, struct camd_msg *msg) {
  111. struct camd *c = &ts->camd;
  112. int ret = c->ops.do_ecm(c, msg);
  113. if (ret <= 0) {
  114. ts_LOGf("ERR | Error sending ecm packet, reconnecting to camd.\n");
  115. ts->is_cw_error = 1;
  116. c->ops.reconnect(c);
  117. return ret;
  118. }
  119. ret = camd_recv_cw(ts);
  120. if (ret < 1) {
  121. time_t now = time(NULL);
  122. ts->is_cw_error = 1;
  123. if (ts->key.ts && now - ts->key.ts > KEY_VALID_TIME) {
  124. if (c->key->is_valid_cw) {
  125. notify(ts, "NO_CODE_WORD", "No code word was set in %ld sec. Decryption is disabled.",
  126. now - ts->key.ts);
  127. ts_LOGf("CW | *ERR* No valid code word was received in %ld seconds. Decryption is disabled.\n",
  128. now - ts->key.ts);
  129. ts->cw_last_warn = time(NULL);
  130. ts->cw_next_warn = ts->cw_last_warn + ts->cw_warn_sec;
  131. ts->cw_next_warn -= now - ts->key.ts;
  132. if (ts->cw_next_warn <= ts->cw_last_warn)
  133. ts->cw_next_warn = ts->cw_last_warn + ts->cw_warn_sec;
  134. }
  135. c->key->is_valid_cw = 0;
  136. }
  137. return 0;
  138. }
  139. return ret;
  140. }
  141. static int camd_send_emm(struct ts *ts, struct camd_msg *msg) {
  142. struct camd *c = &ts->camd;
  143. int ret = c->ops.do_emm(c, msg);
  144. if (ret < 1) {
  145. c->emm_recv_errors++;
  146. if (c->emm_recv_errors >= EMM_RECV_ERRORS_LIMIT) {
  147. ts_LOGf("ERR | Error sending emm packet, reconnecting to camd.\n");
  148. c->ops.reconnect(c);
  149. c->emm_recv_errors = 0;
  150. }
  151. } else {
  152. c->emm_recv_errors = 0;
  153. }
  154. return ret;
  155. }
  156. static void camd_do_msg(struct camd_msg *msg) {
  157. if (msg->type == EMM_MSG) {
  158. msg->ts->emm_seen_count++;
  159. if (camd_send_emm(msg->ts, msg) > 0)
  160. msg->ts->emm_processed_count++;
  161. }
  162. if (msg->type == ECM_MSG) {
  163. msg->ts->ecm_seen_count++;
  164. if (camd_send_ecm(msg->ts, msg) > 0)
  165. msg->ts->ecm_processed_count++;
  166. }
  167. camd_msg_free(&msg);
  168. }
  169. 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) {
  170. struct camd_msg *c = calloc(1, sizeof(struct camd_msg));
  171. c->type = msg_type;
  172. c->ca_id = ca_id;
  173. c->service_id = service_id;
  174. c->data_len = data_len;
  175. memcpy(c->data, data, data_len);
  176. return c;
  177. }
  178. void camd_msg_free(struct camd_msg **pmsg) {
  179. struct camd_msg *m = *pmsg;
  180. if (m) {
  181. FREE(*pmsg);
  182. }
  183. }
  184. static void *camd_thread(void *in_ts) {
  185. struct ts *ts = in_ts;
  186. set_thread_name("tsdec-camd");
  187. while (1) {
  188. struct camd_msg *msg;
  189. void *req = queue_get(ts->camd.req_queue); // Waits...
  190. if (ts->camd_stop)
  191. break;
  192. if (!req)
  193. continue;
  194. msg = queue_get_nowait(ts->camd.ecm_queue);
  195. if (!msg)
  196. msg = queue_get_nowait(ts->camd.emm_queue);
  197. if (!msg)
  198. continue;
  199. camd_do_msg(msg);
  200. if (ts->camd.ecm_queue->items >= ECM_QUEUE_HARD_LIMIT) {
  201. ts_LOGf("WRN | Too much items (%d) in ECM queue, dropping the oldest.\n", ts->camd.ecm_queue->items);
  202. while(ts->camd.ecm_queue->items >= ECM_QUEUE_SOFT_LIMIT) {
  203. msg = queue_get_nowait(ts->camd.ecm_queue);
  204. camd_msg_free(&msg);
  205. }
  206. }
  207. if (ts->camd.emm_queue->items >= EMM_QUEUE_HARD_LIMIT) {
  208. ts_LOGf("WRN | Too much items (%d) in EMM queue, dropping the oldest.%s\n",
  209. ts->camd.emm_queue->items, ts->camd.ops.proto == CAMD_NEWCAMD ?
  210. " Consider switching to cs378x protocol!" : "");
  211. while(ts->camd.emm_queue->items >= EMM_QUEUE_SOFT_LIMIT) {
  212. msg = queue_get_nowait(ts->camd.emm_queue);
  213. camd_msg_free(&msg);
  214. }
  215. }
  216. }
  217. // Flush ECM queue
  218. while (ts->camd.ecm_queue->items) {
  219. struct camd_msg *msg = queue_get_nowait(ts->camd.ecm_queue);
  220. camd_msg_free(&msg);
  221. }
  222. // Flush EMM queue
  223. while (ts->camd.emm_queue->items) {
  224. struct camd_msg *msg = queue_get_nowait(ts->camd.emm_queue);
  225. camd_msg_free(&msg);
  226. }
  227. pthread_exit(EXIT_SUCCESS);
  228. }
  229. void camd_process_packet(struct ts *ts, struct camd_msg *msg) {
  230. if (!msg)
  231. return;
  232. msg->ts = ts;
  233. if (ts->camd.thread) {
  234. if (msg->type == EMM_MSG)
  235. queue_add(ts->camd.emm_queue, msg);
  236. if (msg->type == ECM_MSG)
  237. queue_add(ts->camd.ecm_queue, msg);
  238. queue_add(ts->camd.req_queue, msg);
  239. } else {
  240. camd_do_msg(msg);
  241. }
  242. }
  243. void camd_start(struct ts *ts) {
  244. struct camd *c = &ts->camd;
  245. c->ops.connect(c);
  246. // The input is not file, process messages using async thread
  247. if (ts->threaded) {
  248. c->req_queue = queue_new();
  249. c->ecm_queue = queue_new();
  250. c->emm_queue = queue_new();
  251. pthread_create(&c->thread, NULL , &camd_thread, ts);
  252. }
  253. }
  254. void camd_stop(struct ts *ts) {
  255. struct camd *c = &ts->camd;
  256. ts->camd_stop = 1;
  257. if (c->thread) {
  258. queue_add(c->req_queue, NULL);
  259. queue_wakeup(c->req_queue);
  260. pthread_join(c->thread, NULL);
  261. queue_free(&c->req_queue);
  262. queue_free(&c->ecm_queue);
  263. queue_free(&c->emm_queue);
  264. c->thread = 0;
  265. }
  266. c->ops.disconnect(c);
  267. }