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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. ts->is_cw_error = 1;
  122. if (ts->key.ts && time(NULL) - ts->key.ts > KEY_VALID_TIME) {
  123. if (c->key->is_valid_cw)
  124. notify(ts, "NO_CODE_WORD", "No code word was set in %ld sec. Decryption is disabled.",
  125. time(NULL) - ts->key.ts);
  126. c->key->is_valid_cw = 0;
  127. }
  128. return 0;
  129. }
  130. return ret;
  131. }
  132. static int camd_send_emm(struct ts *ts, struct camd_msg *msg) {
  133. struct camd *c = &ts->camd;
  134. int ret = c->ops.do_emm(c, msg);
  135. if (ret < 0) {
  136. c->emm_recv_errors++;
  137. if (c->emm_recv_errors >= EMM_RECV_ERRORS_LIMIT) {
  138. ts_LOGf("ERR | Error sending emm packet, reconnecting to camd.\n");
  139. c->ops.reconnect(c);
  140. c->emm_recv_errors = 0;
  141. }
  142. } else {
  143. c->emm_recv_errors = 0;
  144. }
  145. return ret;
  146. }
  147. static void camd_do_msg(struct camd_msg *msg) {
  148. if (msg->type == EMM_MSG) {
  149. msg->ts->emm_seen_count++;
  150. if (camd_send_emm(msg->ts, msg) > 0)
  151. msg->ts->emm_processed_count++;
  152. }
  153. if (msg->type == ECM_MSG) {
  154. msg->ts->ecm_seen_count++;
  155. if (camd_send_ecm(msg->ts, msg) > 0)
  156. msg->ts->ecm_processed_count++;
  157. }
  158. camd_msg_free(&msg);
  159. }
  160. 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) {
  161. struct camd_msg *c = calloc(1, sizeof(struct camd_msg));
  162. c->type = msg_type;
  163. c->ca_id = ca_id;
  164. c->service_id = service_id;
  165. c->data_len = data_len;
  166. memcpy(c->data, data, data_len);
  167. return c;
  168. }
  169. void camd_msg_free(struct camd_msg **pmsg) {
  170. struct camd_msg *m = *pmsg;
  171. if (m) {
  172. FREE(*pmsg);
  173. }
  174. }
  175. static void *camd_thread(void *in_ts) {
  176. struct ts *ts = in_ts;
  177. set_thread_name("tsdec-camd");
  178. while (1) {
  179. struct camd_msg *msg;
  180. void *req = queue_get(ts->camd.req_queue); // Waits...
  181. if (!req || ts->camd_stop)
  182. break;
  183. msg = queue_get_nowait(ts->camd.ecm_queue);
  184. if (!msg)
  185. msg = queue_get_nowait(ts->camd.emm_queue);
  186. if (!msg)
  187. break;
  188. camd_do_msg(msg);
  189. }
  190. pthread_exit(EXIT_SUCCESS);
  191. }
  192. void camd_process_packet(struct ts *ts, struct camd_msg *msg) {
  193. msg->ts = ts;
  194. if (ts->camd.thread) {
  195. if (msg->type == EMM_MSG)
  196. queue_add(ts->camd.emm_queue, msg);
  197. if (msg->type == ECM_MSG)
  198. queue_add(ts->camd.ecm_queue, msg);
  199. queue_add(ts->camd.req_queue, msg);
  200. } else {
  201. camd_do_msg(msg);
  202. }
  203. }
  204. void camd_start(struct ts *ts) {
  205. struct camd *c = &ts->camd;
  206. c->ops.connect(c);
  207. // The input is not file, process messages using async thread
  208. if (!(ts->input.type == FILE_IO && ts->input.fd != 0)) {
  209. c->req_queue = queue_new();
  210. c->ecm_queue = queue_new();
  211. c->emm_queue = queue_new();
  212. pthread_create(&c->thread, NULL , &camd_thread, ts);
  213. }
  214. }
  215. void camd_stop(struct ts *ts) {
  216. struct camd *c = &ts->camd;
  217. ts->camd_stop = 1;
  218. if (c->thread) {
  219. queue_wakeup(c->req_queue);
  220. pthread_join(c->thread, NULL);
  221. queue_free(&c->req_queue);
  222. queue_free(&c->ecm_queue);
  223. queue_free(&c->emm_queue);
  224. c->thread = 0;
  225. }
  226. c->ops.disconnect(c);
  227. }