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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. void data_init(struct ts *ts) {
  22. memset(ts, 0, sizeof(struct ts));
  23. // Stream
  24. ts->pat = ts_pat_alloc();
  25. ts->curpat = ts_pat_alloc();
  26. ts->genpat = ts_pat_alloc();
  27. ts->cat = ts_cat_alloc();
  28. ts->curcat = ts_cat_alloc();
  29. ts->pmt = ts_pmt_alloc();
  30. ts->curpmt = ts_pmt_alloc();
  31. ts->sdt = ts_sdt_alloc();
  32. ts->cursdt = ts_sdt_alloc();
  33. ts->emm = ts_privsec_alloc();
  34. ts->last_emm = ts_privsec_alloc();
  35. ts->tmp_emm = ts_privsec_alloc();
  36. ts->ecm = ts_privsec_alloc();
  37. ts->last_ecm = ts_privsec_alloc();
  38. ts->tmp_ecm = ts_privsec_alloc();
  39. pidmap_clear(&ts->pidmap);
  40. pidmap_clear(&ts->cc);
  41. pidmap_clear(&ts->pid_seen);
  42. // Key
  43. memset(&ts->key, 0, sizeof(ts->key));
  44. ts->key.csakey = csa_key_alloc();
  45. gettimeofday(&ts->key.ts_keyset, NULL);
  46. // CAMD
  47. memset(&ts->camd, 0, sizeof(ts->camd));
  48. ts->camd.server_fd = -1;
  49. ts->camd.service = "2233";
  50. ts->camd.key = &ts->key;
  51. ts->camd.user = "user";
  52. ts->camd.pass = "pass";
  53. strcpy(ts->camd.newcamd.hex_des_key, "0102030405060708091011121314");
  54. camd_proto_cs378x(&ts->camd.ops);
  55. // Config
  56. ts->syslog_port = 514;
  57. ts->ts_discont = 1;
  58. ts->ecm_cw_log = 1;
  59. ts->debug_level = 0;
  60. ts->req_CA_sys = CA_CONAX;
  61. ts->process_ecm = 1;
  62. ts->process_emm = 0;
  63. ts->output_stream = 1;
  64. ts->pid_filter = 1;
  65. ts->emm_report_interval = 60;
  66. ts->emm_last_report = time(NULL);
  67. ts->ecm_report_interval = 60;
  68. ts->ecm_last_report = time(NULL);
  69. ts->irdeto_ecm_idx = 0;
  70. ts->irdeto_ecm_filter_type = IRDETO_FILTER_IDX;
  71. ts->cw_warn_sec = 60;
  72. ts->cw_last_warn= time(NULL);
  73. ts->cw_last_warn= ts->cw_last_warn + ts->cw_warn_sec;
  74. ts->key.ts = time(NULL);
  75. ts->input.fd = 0; // STDIN
  76. ts->input.type = FILE_IO;
  77. ts->output.fd = 1; // STDOUT
  78. ts->output.type = FILE_IO;
  79. ts->output.ttl = 1;
  80. ts->output.tos = -1;
  81. ts->output.v6_if_index = -1;
  82. ts->decode_buf = cbuf_init((7 * csa_get_batch_size() * 188) * 16, "decode"); // ~658Kb
  83. ts->write_buf = cbuf_init((7 * csa_get_batch_size() * 188) * 8, "write"); // ~324Kb
  84. ts->input_buffer= list_new("input");
  85. pthread_attr_init(&ts->thread_attr);
  86. size_t stack_size;
  87. pthread_attr_getstacksize(&ts->thread_attr, &stack_size);
  88. if (stack_size > THREAD_STACK_SIZE)
  89. pthread_attr_setstacksize(&ts->thread_attr, THREAD_STACK_SIZE);
  90. }
  91. void data_free(struct ts *ts) {
  92. ts_pat_free(&ts->pat);
  93. ts_pat_free(&ts->curpat);
  94. ts_pat_free(&ts->genpat);
  95. ts_cat_free(&ts->cat);
  96. ts_cat_free(&ts->curcat);
  97. ts_pmt_free(&ts->pmt);
  98. ts_pmt_free(&ts->curpmt);
  99. ts_sdt_free(&ts->sdt);
  100. ts_sdt_free(&ts->cursdt);
  101. ts_privsec_free(&ts->emm);
  102. ts_privsec_free(&ts->last_emm);
  103. ts_privsec_free(&ts->tmp_emm);
  104. ts_privsec_free(&ts->ecm);
  105. ts_privsec_free(&ts->last_ecm);
  106. ts_privsec_free(&ts->tmp_ecm);
  107. csa_key_free(&ts->key.csakey);
  108. cbuf_free(&ts->decode_buf);
  109. cbuf_free(&ts->write_buf);
  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. }