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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 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 <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "data.h"
  22. #include "csa.h"
  23. #include "camd.h"
  24. void data_init(struct ts *ts) {
  25. memset(ts, 0, sizeof(struct ts));
  26. // Stream
  27. ts->pat = ts_pat_alloc();
  28. ts->curpat = ts_pat_alloc();
  29. ts->genpat = ts_pat_alloc();
  30. ts->cat = ts_cat_alloc();
  31. ts->curcat = ts_cat_alloc();
  32. ts->pmt = ts_pmt_alloc();
  33. ts->curpmt = ts_pmt_alloc();
  34. ts->sdt = ts_sdt_alloc();
  35. ts->cursdt = ts_sdt_alloc();
  36. ts->emm = ts_privsec_alloc();
  37. ts->last_emm = ts_privsec_alloc();
  38. ts->tmp_emm = ts_privsec_alloc();
  39. ts->ecm = ts_privsec_alloc();
  40. ts->last_ecm = ts_privsec_alloc();
  41. ts->tmp_ecm = ts_privsec_alloc();
  42. pidmap_clear(&ts->pidmap);
  43. pidmap_clear(&ts->cc);
  44. pidmap_clear(&ts->pid_seen);
  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.server_port = 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->emm_send = 0;
  65. ts->pid_filter = 1;
  66. ts->emm_report_interval = 60;
  67. ts->emm_last_report = time(NULL);
  68. ts->ecm_report_interval = 60;
  69. ts->ecm_last_report = time(NULL);
  70. ts->cw_warn_sec = 60;
  71. ts->cw_last_warn= time(NULL);
  72. ts->cw_last_warn= ts->cw_last_warn + ts->cw_warn_sec;
  73. ts->key.ts = time(NULL);
  74. ts->input.fd = 0; // STDIN
  75. ts->input.type = FILE_IO;
  76. ts->output.fd = 1; // STDOUT
  77. ts->output.type = FILE_IO;
  78. ts->output.ttl = 1;
  79. ts->output.tos = -1;
  80. ts->decode_buf = cbuf_init((7 * csa_get_batch_size() * 188) * 16, "decode"); // ~658Kb
  81. ts->write_buf = cbuf_init((7 * csa_get_batch_size() * 188) * 8, "write"); // ~324Kb
  82. ts->input_buffer= list_new("input");
  83. pthread_attr_init(&ts->thread_attr);
  84. size_t stack_size;
  85. pthread_attr_getstacksize(&ts->thread_attr, &stack_size);
  86. if (stack_size > THREAD_STACK_SIZE)
  87. pthread_attr_setstacksize(&ts->thread_attr, THREAD_STACK_SIZE);
  88. }
  89. void data_free(struct ts *ts) {
  90. ts_pat_free(&ts->pat);
  91. ts_pat_free(&ts->curpat);
  92. ts_pat_free(&ts->genpat);
  93. ts_cat_free(&ts->cat);
  94. ts_cat_free(&ts->curcat);
  95. ts_pmt_free(&ts->pmt);
  96. ts_pmt_free(&ts->curpmt);
  97. ts_sdt_free(&ts->sdt);
  98. ts_sdt_free(&ts->cursdt);
  99. ts_privsec_free(&ts->emm);
  100. ts_privsec_free(&ts->last_emm);
  101. ts_privsec_free(&ts->tmp_emm);
  102. ts_privsec_free(&ts->ecm);
  103. ts_privsec_free(&ts->last_ecm);
  104. ts_privsec_free(&ts->tmp_ecm);
  105. csa_key_free(&ts->key.csakey);
  106. cbuf_free(&ts->decode_buf);
  107. cbuf_free(&ts->write_buf);
  108. FREE(ts->input.fname);
  109. FREE(ts->output.fname);
  110. list_free(&ts->input_buffer, free, NULL);
  111. // glibc's crypt function allocates static buffer on first crypt() call.
  112. // Since newcamd uses crypt(), the result is saved in c->newcamd.crypt_passwd
  113. // and in order to avoid leaking 43 bytes of memory on exit which makes valgrind
  114. // unhappy it is a good idea to free the memory (ONCE!).
  115. FREE(ts->camd.newcamd.crypt_passwd);
  116. pthread_attr_destroy(&ts->thread_attr);
  117. }