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.

data.c 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Data functions
  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 <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "data.h"
  19. #include "csa.h"
  20. #include "camd.h"
  21. #include "util.h"
  22. void data_init(struct ts *ts) {
  23. memset(ts, 0, sizeof(struct ts));
  24. // Stream
  25. ts->pat = ts_pat_alloc();
  26. ts->curpat = ts_pat_alloc();
  27. ts->genpat = ts_pat_alloc();
  28. ts->cat = ts_cat_alloc();
  29. ts->curcat = ts_cat_alloc();
  30. ts->pmt = ts_pmt_alloc();
  31. ts->curpmt = ts_pmt_alloc();
  32. ts->sdt = ts_sdt_alloc();
  33. ts->cursdt = ts_sdt_alloc();
  34. ts->emm = ts_privsec_alloc();
  35. ts->last_emm = ts_privsec_alloc();
  36. ts->tmp_emm = ts_privsec_alloc();
  37. ts->ecm = ts_privsec_alloc();
  38. ts->last_ecm = ts_privsec_alloc();
  39. ts->tmp_ecm = ts_privsec_alloc();
  40. pidmap_clear(&ts->pidmap);
  41. pidmap_clear(&ts->cc);
  42. pidmap_clear(&ts->pid_seen);
  43. ts->have_valid_pmt = 1;
  44. ts->last_pmt_ts = time(NULL);
  45. // Key
  46. memset(&ts->key, 0, sizeof(ts->key));
  47. ts->key.csakey = csa_key_alloc();
  48. gettimeofday(&ts->key.ts_keyset, NULL);
  49. // CAMD
  50. memset(&ts->camd, 0, sizeof(ts->camd));
  51. ts->camd.server_fd = -1;
  52. ts->camd.service = "2233";
  53. ts->camd.key = &ts->key;
  54. ts->camd.user = "user";
  55. ts->camd.pass = "pass";
  56. strcpy(ts->camd.newcamd.hex_des_key, "0102030405060708091011121314");
  57. camd_proto_cs378x(&ts->camd.ops);
  58. // Config
  59. ts->syslog_port = 514;
  60. ts->ts_discont = 1;
  61. ts->ecm_cw_log = 1;
  62. ts->debug_level = 0;
  63. ts->req_CA_sys = CA_CONAX;
  64. ts->process_ecm = 1;
  65. ts->process_emm = 0;
  66. ts->output_stream = 1;
  67. ts->pid_filter = 1;
  68. ts->emm_report_interval = 60;
  69. ts->emm_last_report = time(NULL);
  70. ts->ecm_report_interval = 60;
  71. ts->ecm_last_report = time(NULL);
  72. ts->last_scrambled_packet_ts = time(NULL);
  73. ts->last_not_scrambled_packet_ts = time(NULL);
  74. ts->irdeto_ecm_idx = 0;
  75. ts->irdeto_ecm_filter_type = IRDETO_FILTER_IDX;
  76. ts->cw_warn_sec = 60;
  77. ts->cw_last_warn= time(NULL);
  78. ts->cw_last_warn= ts->cw_last_warn + ts->cw_warn_sec;
  79. ts->key.ts = time(NULL);
  80. ts->allow_encrypted_output = 0;
  81. ts->output_is_encrypted = 0;
  82. ts->last_encrypted_output_ts = get_time();
  83. ts->last_decrypted_output_ts = get_time();
  84. ts->input.fd = 0; // STDIN
  85. ts->input.type = FILE_IO;
  86. ts->output.fd = 1; // STDOUT
  87. ts->output.type = FILE_IO;
  88. ts->output.ttl = 1;
  89. ts->output.tos = -1;
  90. ts->output.v6_if_index = -1;
  91. ts->decode_buf = cbuf_init((7 * csa_get_batch_size() * 188) * 16, "decode"); // ~658Kb
  92. ts->write_buf = cbuf_init((7 * csa_get_batch_size() * 188) * 8, "write"); // ~324Kb
  93. ts->input_buffer= list_new("input");
  94. pthread_attr_init(&ts->thread_attr);
  95. size_t stack_size;
  96. pthread_attr_getstacksize(&ts->thread_attr, &stack_size);
  97. if (stack_size > THREAD_STACK_SIZE)
  98. pthread_attr_setstacksize(&ts->thread_attr, THREAD_STACK_SIZE);
  99. }
  100. void data_free(struct ts *ts) {
  101. ts_pat_free(&ts->pat);
  102. ts_pat_free(&ts->curpat);
  103. ts_pat_free(&ts->genpat);
  104. ts_cat_free(&ts->cat);
  105. ts_cat_free(&ts->curcat);
  106. ts_pmt_free(&ts->pmt);
  107. ts_pmt_free(&ts->curpmt);
  108. ts_sdt_free(&ts->sdt);
  109. ts_sdt_free(&ts->cursdt);
  110. ts_privsec_free(&ts->emm);
  111. ts_privsec_free(&ts->last_emm);
  112. ts_privsec_free(&ts->tmp_emm);
  113. ts_privsec_free(&ts->ecm);
  114. ts_privsec_free(&ts->last_ecm);
  115. ts_privsec_free(&ts->tmp_ecm);
  116. csa_key_free(&ts->key.csakey);
  117. cbuf_free(&ts->decode_buf);
  118. cbuf_free(&ts->write_buf);
  119. list_free(&ts->input_buffer, free, NULL);
  120. // glibc's crypt function allocates static buffer on first crypt() call.
  121. // Since newcamd uses crypt(), the result is saved in c->newcamd.crypt_passwd
  122. // and in order to avoid leaking 43 bytes of memory on exit which makes valgrind
  123. // unhappy it is a good idea to free the memory (ONCE!).
  124. FREE(ts->camd.newcamd.crypt_passwd);
  125. ts->status_file = NULL;
  126. FREE(ts->status_file_tmp);
  127. pthread_attr_destroy(&ts->thread_attr);
  128. }