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.

tsdecrypt.c 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. /*
  2. * tsdecrypt
  3. * Copyright (C) 2011-2012 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 <getopt.h>
  21. #include <string.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <signal.h>
  25. #include <fcntl.h>
  26. #include <errno.h>
  27. #include <syslog.h>
  28. #include <sys/resource.h>
  29. #include <openssl/rand.h>
  30. #include "libfuncs/libfuncs.h"
  31. #include "data.h"
  32. #include "util.h"
  33. #include "csa.h"
  34. #include "camd.h"
  35. #include "process.h"
  36. #include "udp.h"
  37. #include "notify.h"
  38. #define FIRST_REPORT_SEC 3
  39. #define PROGRAM_NAME "tsdecrypt"
  40. static const char *program_id = PROGRAM_NAME " v" VERSION " (" GIT_VER ", build " BUILD_ID " " DLIB ")";
  41. static int keep_running = 1;
  42. static FILE *log_file = NULL;
  43. static char *log_filename = NULL;
  44. static int local_syslog = 0;
  45. static int remote_syslog = 0;
  46. static void do_log(FILE *f, time_t now, const char *msg) {
  47. char date[64];
  48. struct tm tm;
  49. localtime_r(&now, &tm);
  50. strftime(date, sizeof(date), "%F %H:%M:%S", localtime_r(&now, &tm));
  51. fprintf(f, "%s | %s", date, msg);
  52. }
  53. static void LOG_func(const char *msg) {
  54. time_t now = time(NULL);
  55. do_log(stderr, now, msg);
  56. if (log_file)
  57. do_log(log_file, now, msg);
  58. if (local_syslog)
  59. syslog(LOG_INFO, msg, strlen(msg));
  60. if (remote_syslog)
  61. LOG(msg);
  62. }
  63. static const char short_options[] = "i:d:N:Sl:L:F:I:RzM:T:W:O:o:t:rk:g:pwxyc:C:Y:Q:A:s:U:P:B:eZ:Ef:X:H:G:KJ:D:jbhV";
  64. // Unused short options: amnquv0123456789
  65. static const struct option long_options[] = {
  66. { "ident", required_argument, NULL, 'i' },
  67. { "daemon", required_argument, NULL, 'd' },
  68. { "syslog", no_argument, NULL, 'S' },
  69. { "syslog-host", required_argument, NULL, 'l' },
  70. { "syslog-port", required_argument, NULL, 'L' },
  71. { "log-file", required_argument, NULL, 'F' },
  72. { "notify-program", required_argument, NULL, 'N' },
  73. { "input", required_argument, NULL, 'I' },
  74. { "input-rtp", no_argument, NULL, 'R' },
  75. { "input-ignore-disc", no_argument, NULL, 'z' },
  76. { "input-service", required_argument, NULL, 'M' },
  77. { "input-buffer", required_argument, NULL, 'T' },
  78. { "input-dump", required_argument, NULL, 'W' },
  79. { "output", required_argument, NULL, 'O' },
  80. { "output-intf", required_argument, NULL, 'o' },
  81. { "output-ttl", required_argument, NULL, 't' },
  82. { "output-rtp", no_argument, NULL, 'r' },
  83. { "output-rtp-ssrc", required_argument, NULL, 'k' },
  84. { "output-tos", required_argument, NULL, 'g' },
  85. { "output-filter", no_argument, NULL, 'p' },
  86. { "no-output-filter", no_argument, NULL, 'p' },
  87. { "output-nit-pass", no_argument, NULL, 'y' },
  88. { "output-eit-pass", no_argument, NULL, 'w' },
  89. { "output-tdt-pass", no_argument, NULL, 'x' },
  90. { "ca-system", required_argument, NULL, 'c' },
  91. { "caid", required_argument, NULL, 'C' },
  92. { "const-cw", required_argument, NULL, 'Y' },
  93. { "biss-key", required_argument, NULL, 'Q' },
  94. { "camd-proto", required_argument, NULL, 'A' },
  95. { "camd-server", required_argument, NULL, 's' },
  96. { "camd-user", required_argument, NULL, 'U' },
  97. { "camd-pass", required_argument, NULL, 'P' },
  98. { "camd-des-key", required_argument, NULL, 'B' },
  99. { "emm", no_argument, NULL, 'e' },
  100. { "emm-pid", required_argument, NULL, 'Z' },
  101. { "emm-only", no_argument, NULL, 'E' },
  102. { "emm-report-time", required_argument, NULL, 'f' },
  103. { "ecm-pid", required_argument, NULL, 'X' },
  104. { "ecm-report-time", required_argument, NULL, 'H' },
  105. { "ecm-irdeto-type", required_argument, NULL, 'G' },
  106. { "ecm-no-log", no_argument , NULL, 'K' },
  107. { "cw-warn-time", required_argument, NULL, 'J' },
  108. { "debug", required_argument, NULL, 'D' },
  109. { "pid-report", no_argument, NULL, 'j' },
  110. { "bench", no_argument, NULL, 'b' },
  111. { "help", no_argument, NULL, 'h' },
  112. { "version", no_argument, NULL, 'V' },
  113. { 0, 0, 0, 0 }
  114. };
  115. static void show_help(struct ts *ts) {
  116. printf("%s\n", program_id);
  117. printf("Copyright (C) 2011-2012 Unix Solutions Ltd.\n");
  118. printf("\n");
  119. printf(" Usage: " PROGRAM_NAME " [opts]\n");
  120. printf("\n");
  121. printf("Main options:\n");
  122. printf(" -i --ident <server> | Format PROVIDER/CHANNEL. Default: empty\n");
  123. printf(" -d --daemon <pidfile> | Daemonize program and write pid file.\n");
  124. printf(" -N --notify-program <prg> | Execute <prg> to report events. Default: empty\n");
  125. printf("\n");
  126. printf("Input options:\n");
  127. printf(" -I --input <source> | Where to read from. File or multicast address.\n");
  128. printf(" . -I 224.0.0.1:5000 (multicast receive)\n");
  129. printf(" . -I file.ts (read from file)\n");
  130. printf(" . -I - (read from stdin) (default)\n");
  131. printf(" -R --input-rtp | Enable RTP input\n");
  132. printf(" -z --input-ignore-disc | Do not report discontinuty errors in input.\n");
  133. printf(" -M --input-service <srvid> | Choose service id when input is MPTS.\n");
  134. printf(" -T --input-buffer <ms> | Set input buffer time in ms. Default: %u\n", ts->input_buffer_time);
  135. printf(" -W --input-dump <filename> | Save input stream in file.\n");
  136. printf("\n");
  137. printf("Output options:\n");
  138. printf(" -O --output <dest> | Where to send output. File or multicast address.\n");
  139. printf(" . -O 239.0.0.1:5000 (multicast send)\n");
  140. printf(" . -O file.ts (write to file)\n");
  141. printf(" . -O - (write to stdout) (default)\n");
  142. printf(" -o --output-intf <addr> | Set multicast output interface. Default: %s\n", inet_ntoa(ts->output.intf));
  143. printf(" -t --output-ttl <ttl> | Set multicast ttl. Default: %d\n", ts->output.ttl);
  144. printf(" -r --output-rtp | Enable RTP output.\n");
  145. printf(" -k --output-rtp-ssrc <id> | Set RTP SSRC. Default: %u\n", ts->rtp_ssrc);
  146. printf(" -g --output-tos <tos> | Set TOS value of output packets. Default: none\n");
  147. printf(" -p --no-output-filter | Disable output filtering. Default: %s\n", ts->pid_filter ? "enabled" : "disabled");
  148. printf(" -y --output-nit-pass | Pass through NIT.\n");
  149. printf(" -w --output-eit-pass | Pass through EIT (EPG).\n");
  150. printf(" -x --output-tdt-pass | Pass through TDT/TOT.\n");
  151. printf("\n");
  152. printf("CA options:\n");
  153. printf(" -c --ca-system <ca_sys> | Process input EMM/ECM from <ca_sys>.\n");
  154. printf(" | Valid systems are: CONAX (default), CRYPTOWORKS,\n");
  155. printf(" . IRDETO, SECA (MEDIAGUARD), VIACCESS,\n");
  156. printf(" . VIDEOGUARD (NDS), NAGRA, DRECRYPT and BULCRYPT.\n");
  157. printf(" -C --caid <caid> | Set CAID. Default: Taken from --ca-system.\n");
  158. printf(" -Y --const-cw <codeword> | Set constant code word for decryption.\n");
  159. printf(" . Example cw: a1a2a3a4a5a6a7a8b1b2b3b4b5b6b7b8\n");
  160. printf(" -Q --biss-key <biss-key> | Set BISS key for decryption.\n");
  161. printf(" . Example key: 112233445566\n");
  162. printf("\n");
  163. printf("CAMD server options:\n");
  164. printf(" -A --camd-proto <proto> | Set CAMD network protocol.\n");
  165. printf(" . Valid protocols are: CS378X (default) and NEWCAMD\n");
  166. printf(" -s --camd-server <addr> | Set CAMD server ip_address:port (1.2.3.4:2233).\n");
  167. printf(" -U --camd-user <user> | Set CAMD server user. Default: %s\n", ts->camd.user);
  168. printf(" -P --camd-pass <pass> | Set CAMD server password. Default: %s\n", ts->camd.pass);
  169. printf(" -B --camd-des-key <key> | Set DES key for newcamd protocol.\n");
  170. printf(" . Default: %s\n", ts->camd.newcamd.hex_des_key);
  171. printf("\n");
  172. printf("EMM options:\n");
  173. printf(" -e --emm | Enable sending EMM's to CAMD. Default: %s\n", ts->emm_send ? "enabled" : "disabled");
  174. printf(" -E --emm-only | Send only EMMs to CAMD, skipping ECMs and without\n");
  175. printf(" . decoding the input stream.\n");
  176. printf(" -Z --emm-pid <pid> | Force EMM pid. Default: none\n");
  177. printf(" -f --emm-report-time <sec> | Report each <sec> seconds how much EMMs have been\n");
  178. printf(" . received/processed. Set <sec> to 0 to disable\n");
  179. printf(" . the reports. Default: %d sec\n", ts->emm_report_interval);
  180. printf("\n");
  181. printf("ECM options:\n");
  182. printf(" -X --ecm-pid <pid> | Force ECM pid. Default: none\n");
  183. printf(" -H --ecm-report-time <sec> | Report each <sec> how much ECMs and CWs have been\n");
  184. printf(" . processed/skipped. Set <sec> to 0 to disable\n");
  185. printf(" . the reports. Default: %d sec\n", ts->ecm_report_interval);
  186. printf(" -G --ecm-irdeto-type <int> | Process IRDETO ECMs with type X /0-3/. Default: %d\n", ts->irdeto_ecm);
  187. printf(" -K --ecm-no-log | Disable ECM and code words logging.\n");
  188. printf(" -J --cw-warn-time <sec> | Warn if no valid code word has been received.\n");
  189. printf(" . Set <sec> to 0 to disable. Default: %d sec\n", ts->cw_warn_sec);
  190. printf("\n");
  191. printf("Logging options:\n");
  192. printf(" -S --syslog | Log messages using syslog.\n");
  193. printf(" -l --syslog-host <host> | Syslog server address. Default: disabled\n");
  194. printf(" -L --syslog-port <port> | Syslog server port. Default: %d\n", ts->syslog_port);
  195. printf(" -F --log-file <filename> | Log to file <filename>.\n");
  196. printf(" -D --debug <level> | Message debug level.\n");
  197. printf(" . 0 = default messages\n");
  198. printf(" . 1 = show PSI tables\n");
  199. printf(" . 2 = show EMMs\n");
  200. printf(" . 3 = show duplicate ECMs\n");
  201. printf(" . 4 = packet debug\n");
  202. printf(" . 5 = packet debug + packet dump\n");
  203. printf("\n");
  204. printf("Misc options:\n");
  205. printf(" -j --pid-report | Report how much packets were received.\n");
  206. printf(" -b --bench | Benchmark decrypton.\n");
  207. printf(" -h --help | Show help screen.\n");
  208. printf(" -V --version | Show program version.\n");
  209. printf("\n");
  210. }
  211. static int parse_io_param(struct io *io, char *opt, int open_flags, mode_t open_mode) {
  212. io->type = WTF_IO;
  213. char *p = strrchr(opt, ':');
  214. if (!p) {
  215. io->type = FILE_IO;
  216. if (strcmp(opt, "-") != 0) {
  217. io->fd = open(opt, open_flags, open_mode);
  218. if (io->fd < 0) {
  219. fprintf(stderr, "ERROR: Can not open file (%s): %s\n", opt, strerror(errno));
  220. exit(EXIT_FAILURE);
  221. }
  222. }
  223. io->fname = strdup(opt);
  224. return 0;
  225. }
  226. *p = 0x00;
  227. io->type = NET_IO;
  228. io->port = atoi(p + 1);
  229. if (inet_aton(opt, &io->addr) == 0)
  230. return 1;
  231. return 0;
  232. }
  233. static void parse_options(struct ts *ts, int argc, char **argv) {
  234. int j, i, ca_err = 0, server_err = 1, input_addr_err = 0, output_addr_err = 0, output_intf_err = 0, ident_err = 0, port_set = 0;
  235. while ((j = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
  236. char *p = NULL;
  237. switch (j) {
  238. case 'i': // -- ident
  239. ts->ident = optarg;
  240. break;
  241. case 'd': // --daemon
  242. ts->pidfile = optarg;
  243. break;
  244. case 'N': // --notify-program
  245. ts->notify_program = optarg;
  246. break;
  247. case 'S': // --syslog
  248. ts->syslog_active = 1;
  249. ts->syslog_remote = 0;
  250. break;
  251. case 'l': // --syslog-host
  252. ts->syslog_host = optarg;
  253. ts->syslog_active = 1;
  254. ts->syslog_remote = 1;
  255. break;
  256. case 'L': // --syslog-port
  257. ts->syslog_port = atoi(optarg);
  258. break;
  259. case 'F': // --log-file
  260. log_filename = optarg;
  261. break;
  262. case 'I': // --input
  263. input_addr_err = parse_io_param(&ts->input, optarg, O_RDONLY, 0);
  264. break;
  265. case 'R': // --input-rtp
  266. ts->rtp_input = !ts->rtp_input;
  267. break;
  268. case 'z': // --input-ignore-disc
  269. ts->ts_discont = !ts->ts_discont;
  270. break;
  271. case 'M': // --input-service
  272. ts->forced_service_id = strtoul(optarg, NULL, 0) & 0xffff;
  273. break;
  274. case 'T': // --input-buffer
  275. ts->input_buffer_time = strtoul(optarg, NULL, 0);
  276. break;
  277. case 'W': // --input-dump
  278. ts->input_dump_filename = optarg;
  279. break;
  280. case 'O': // --output
  281. output_addr_err = parse_io_param(&ts->output, optarg,
  282. O_CREAT | O_WRONLY | O_TRUNC,
  283. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  284. break;
  285. case 'o': // --output-intf
  286. if (inet_aton(optarg, &ts->output.intf) == 0)
  287. output_intf_err = 1;
  288. break;
  289. case 't': // --output-ttl
  290. ts->output.ttl = atoi(optarg);
  291. break;
  292. case 'r': // --output-rtp
  293. ts->rtp_output = 1;
  294. break;
  295. case 'k': // --output-rtp-ssrc
  296. ts->rtp_ssrc = strtoul(optarg, NULL, 0);
  297. break;
  298. case 'g': // --output-tos
  299. ts->output.tos = (uint8_t)strtol(optarg, NULL, 0);
  300. break;
  301. case 'p': // --no-output-filter
  302. ts->pid_filter = 0;
  303. break;
  304. case 'y': // --output-nit-pass
  305. ts->nit_passthrough = !ts->nit_passthrough;
  306. break;
  307. case 'w': // --output-eit-pass
  308. ts->eit_passthrough = !ts->eit_passthrough;
  309. break;
  310. case 'x': // --output-tdt-pass
  311. ts->tdt_passthrough = !ts->tdt_passthrough;
  312. break;
  313. case 'c': // --ca-system
  314. if (strcasecmp("IRDETO", optarg) == 0)
  315. ts->req_CA_sys = CA_IRDETO;
  316. else if (strcasecmp("CONNAX", optarg) == 0 || strcasecmp("CONAX", optarg) == 0)
  317. ts->req_CA_sys = CA_CONAX;
  318. else if (strcasecmp("CRYPTOWORKS", optarg) == 0)
  319. ts->req_CA_sys = CA_CRYPTOWORKS;
  320. else if (strcasecmp("SECA", optarg) == 0 || strcasecmp("MEDIAGUARD", optarg) == 0)
  321. ts->req_CA_sys = CA_SECA;
  322. else if (strcasecmp("VIACCESS", optarg) == 0)
  323. ts->req_CA_sys = CA_VIACCESS;
  324. else if (strcasecmp("VIDEOGUARD", optarg) == 0 || strcasecmp("NDS", optarg) == 0)
  325. ts->req_CA_sys = CA_VIDEOGUARD;
  326. else if (strcasecmp("NAGRA", optarg) == 0)
  327. ts->req_CA_sys = CA_NAGRA;
  328. else if (strcasecmp("DRE-CRYPT", optarg) == 0 || strcasecmp("DRECRYPT", optarg) == 0)
  329. ts->req_CA_sys = CA_DRECRYPT;
  330. else if (strcasecmp("BULCRYPT", optarg) == 0)
  331. ts->req_CA_sys = CA_BULCRYPT;
  332. else
  333. ca_err = 1;
  334. break;
  335. case 'C': // --caid
  336. ts->forced_caid = strtoul(optarg, NULL, 0) & 0xffff;
  337. break;
  338. case 'Y': // --const-cw
  339. ts->camd.constant_codeword = 1;
  340. if (strlen(optarg) > 2 && optarg[0] == '0' && optarg[1] == 'x')
  341. optarg += 2;
  342. if (strlen(optarg) != CODEWORD_LENGTH * 2) {
  343. fprintf(stderr, "ERROR: Constant code word should be %u characters long.\n", CODEWORD_LENGTH * 2);
  344. exit(EXIT_FAILURE);
  345. }
  346. if (decode_hex_string(optarg, ts->camd.key->cw, strlen(optarg)) < 0) {
  347. fprintf(stderr, "ERROR: Invalid hex string for constant code word: %s\n", optarg);
  348. exit(EXIT_FAILURE);
  349. }
  350. camd_set_cw(ts, ts->camd.key->cw, 0);
  351. ts->camd.key->is_valid_cw = 1;
  352. break;
  353. case 'Q': // --biss-key
  354. ts->camd.constant_codeword = 1;
  355. if (strlen(optarg) > 2 && optarg[0] == '0' && optarg[1] == 'x')
  356. optarg += 2;
  357. uint8_t *key = ts->camd.key->cw;
  358. // Sometimes the BISS keys are entered with their checksums already calculated (16 symbols, 8 bytes)
  359. // This is the same as constant cw with the same key for even and odd
  360. if (strlen(optarg) == (BISSKEY_LENGTH + 2) * 2) {
  361. if (decode_hex_string(optarg, key, strlen(optarg)) < 0) {
  362. fprintf(stderr, "ERROR: Invalid hex string for BISS key: %s\n", optarg);
  363. exit(EXIT_FAILURE);
  364. }
  365. } else {
  366. // BISS key without checksum (12 symbols, 6 bytes)
  367. if (strlen(optarg) != BISSKEY_LENGTH * 2) {
  368. fprintf(stderr, "ERROR: BISS key should be %u characters long.\n", BISSKEY_LENGTH * 2);
  369. exit(EXIT_FAILURE);
  370. }
  371. if (decode_hex_string(optarg, key, strlen(optarg)) < 0) {
  372. fprintf(stderr, "ERROR: Invalid hex string for BISS key: %s\n", optarg);
  373. exit(EXIT_FAILURE);
  374. }
  375. // Calculate BISS KEY crc
  376. memmove(key + 4, key + 3, 3);
  377. key[3] = (uint8_t)(key[0] + key[1] + key[2]);
  378. key[7] = (uint8_t)(key[4] + key[5] + key[6]);
  379. }
  380. // Even and odd keys are the same
  381. memcpy(key + 8, key, 8);
  382. camd_set_cw(ts, ts->camd.key->cw, 0);
  383. ts->camd.key->is_valid_cw = 1;
  384. break;
  385. case 'A': // --camd-proto
  386. if (strcasecmp(optarg, "cs378x") == 0) {
  387. camd_proto_cs378x(&ts->camd.ops);
  388. } else if (strcasecmp(optarg, "newcamd") == 0) {
  389. camd_proto_newcamd(&ts->camd.ops);
  390. } else {
  391. fprintf(stderr, "Unknown CAMD protocol: %s\n", optarg);
  392. exit(EXIT_FAILURE);
  393. }
  394. break;
  395. case 's': // --camd-server
  396. p = strrchr(optarg, ':');
  397. if (p) {
  398. *p = 0x00;
  399. ts->camd.server_port = atoi(p + 1);
  400. port_set = 1;
  401. }
  402. if (inet_aton(optarg, &ts->camd.server_addr) == 0)
  403. server_err = 1;
  404. else
  405. server_err = 0;
  406. break;
  407. case 'U': // --camd-user
  408. if (strlen(optarg) < 64)
  409. ts->camd.user = optarg;
  410. break;
  411. case 'P': // --camd-pass
  412. ts->camd.pass = optarg;
  413. break;
  414. case 'B': // --camd-des-key
  415. if (strlen(optarg) > 2 && optarg[0] == '0' && optarg[1] == 'x')
  416. optarg += 2;
  417. if (strlen(optarg) != DESKEY_LENGTH) {
  418. fprintf(stderr, "ERROR: des key should be %u characters long.\n", DESKEY_LENGTH);
  419. exit(EXIT_FAILURE);
  420. }
  421. strncpy(ts->camd.newcamd.hex_des_key, optarg, sizeof(ts->camd.newcamd.hex_des_key) - 1);
  422. ts->camd.newcamd.hex_des_key[sizeof(ts->camd.newcamd.hex_des_key) - 1] = 0;
  423. break;
  424. case 'e': // --emm
  425. ts->emm_send = !ts->emm_send;
  426. break;
  427. case 'Z': // --emm-pid
  428. ts->forced_emm_pid = strtoul(optarg, NULL, 0) & 0x1fff;
  429. break;
  430. case 'E': // --emm-only
  431. ts->emm_only = 1;
  432. ts->emm_send = 1;
  433. break;
  434. case 'f': // --emm-report-time
  435. ts->emm_report_interval = strtoul(optarg, NULL, 10);
  436. if (ts->emm_report_interval > 86400)
  437. ts->emm_report_interval = 86400;
  438. break;
  439. case 'X': // --ecm-pid
  440. ts->forced_ecm_pid = strtoul(optarg, NULL, 0) & 0x1fff;
  441. break;
  442. case 'H': // --ecm-report-time
  443. ts->ecm_report_interval = strtoul(optarg, NULL, 10);
  444. if (ts->ecm_report_interval > 86400)
  445. ts->ecm_report_interval = 86400;
  446. break;
  447. case 'G': // --ecm-irdeto-type
  448. ts->irdeto_ecm = atoi(optarg);
  449. break;
  450. case 'K': // --ecm-no-log
  451. ts->ecm_cw_log = !ts->ecm_cw_log;
  452. break;
  453. case 'J': // --cw-warn-time
  454. ts->cw_warn_sec = strtoul(optarg, NULL, 10);
  455. if (ts->cw_warn_sec > 86400)
  456. ts->cw_warn_sec = 86400;
  457. ts->cw_last_warn= ts->cw_last_warn + ts->cw_warn_sec;
  458. break;
  459. case 'D': // --debug
  460. ts->debug_level = atoi(optarg);
  461. if (ts->debug_level > 0)
  462. ts->pid_report = 1;
  463. break;
  464. case 'j': // --pid-report
  465. ts->pid_report = 1;
  466. break;
  467. case 'b': // --bench
  468. csa_benchmark();
  469. exit(EXIT_SUCCESS);
  470. case 'h': // --help
  471. show_help(ts);
  472. exit(EXIT_SUCCESS);
  473. case 'V': // --version
  474. printf("%s\n", program_id);
  475. exit(EXIT_SUCCESS);
  476. }
  477. }
  478. if (!ts->ident) {
  479. if (ts->syslog_active || ts->notify_program)
  480. ident_err = 1;
  481. }
  482. // Constant codeword is special. Disable conflicting options
  483. if (ts->camd.constant_codeword) {
  484. server_err = 0; // No server settings are required
  485. ts->forced_caid = 0;
  486. ts->forced_ecm_pid = 0;
  487. ts->forced_emm_pid = 0;
  488. ts->emm_send = 0;
  489. ts->emm_report_interval = 0;
  490. ts->ecm_report_interval = 0;
  491. ts->cw_warn_sec = 0;
  492. }
  493. if (ident_err || ca_err || server_err || input_addr_err || output_addr_err || ts->input.type == WTF_IO || ts->output.type == WTF_IO) {
  494. show_help(ts);
  495. if (ident_err)
  496. fprintf(stderr, "ERROR: Ident is not set, please use --ident option.\n");
  497. if (ca_err)
  498. fprintf(stderr, "ERROR: Requested CA system is unsupported.\n");
  499. if (server_err)
  500. fprintf(stderr, "ERROR: CAMD server IP address is not set or it is invalid.\n");
  501. if (input_addr_err)
  502. fprintf(stderr, "ERROR: Input IP address is invalid.\n");
  503. if (output_addr_err)
  504. fprintf(stderr, "ERROR: Output IP address is invalid.\n");
  505. if (output_intf_err)
  506. fprintf(stderr, "ERROR: Output interface address is invalid.\n");
  507. exit(EXIT_FAILURE);
  508. }
  509. if (decode_hex_string(ts->camd.newcamd.hex_des_key, ts->camd.newcamd.bin_des_key, DESKEY_LENGTH) < 0) {
  510. fprintf(stderr, "ERROR: Invalid hex string for des key: %s\n", ts->camd.newcamd.hex_des_key);
  511. exit(EXIT_FAILURE);
  512. }
  513. if (ts->camd.ops.proto == CAMD_NEWCAMD && !port_set) {
  514. fprintf(stderr, "ERROR: CAMD server port is not set. Use --camd-server %s:xxxx to set the port.\n", inet_ntoa(ts->camd.server_addr));
  515. exit(EXIT_FAILURE);
  516. }
  517. if (log_filename) {
  518. log_file = fopen(log_filename, "a");
  519. if (!log_file) {
  520. fprintf(stderr, "ERROR: Can't open log file %s: %s\n", log_filename, strerror(errno));
  521. exit(EXIT_FAILURE);
  522. }
  523. }
  524. if (ts->ident)
  525. ts_LOGf("Ident : %s\n", ts->ident);
  526. if (ts->notify_program)
  527. ts_LOGf("Notify prg : %s\n", ts->notify_program);
  528. if (ts->pidfile)
  529. ts_LOGf("Daemonize : %s pid file.\n", ts->pidfile);
  530. if (ts->syslog_active) {
  531. if (ts->syslog_remote)
  532. ts_LOGf("Syslog : %s:%d\n", ts->syslog_host, ts->syslog_port);
  533. else
  534. ts_LOGf("Syslog : enabled\n");
  535. } else
  536. ts_LOGf("Syslog : disabled\n");
  537. if (!ts->camd.constant_codeword) {
  538. if (ts->forced_caid)
  539. ts->req_CA_sys = ts_get_CA_sys(ts->forced_caid);
  540. if (!ts->forced_caid)
  541. ts_LOGf("CA System : %s\n", ts_get_CA_sys_txt(ts->req_CA_sys));
  542. else
  543. ts_LOGf("CA System : %s | CAID: 0x%04x (%d)\n",
  544. ts_get_CA_sys_txt(ts->req_CA_sys),
  545. ts->forced_caid, ts->forced_caid);
  546. } else {
  547. char cw_even[64], cw_odd[64];
  548. ts_hex_dump_buf(cw_even, sizeof(cw_even), ts->key.cw , 8, 0);
  549. ts_hex_dump_buf(cw_odd , sizeof(cw_odd ), ts->key.cw + 8, 8, 0);
  550. ts_LOGf("Constant CW: even = %s\n", cw_even);
  551. ts_LOGf("Constant CW: odd = %s\n", cw_odd);
  552. }
  553. if (ts->input.type == NET_IO) {
  554. ts_LOGf("Input addr : %s://%s:%u/\n",
  555. ts->rtp_input ? "rtp" : "udp",
  556. inet_ntoa(ts->input.addr), ts->input.port);
  557. if (ts->input_buffer_time) {
  558. ts_LOGf("Input buff : %u ms\n", ts->input_buffer_time);
  559. }
  560. } else if (ts->input.type == FILE_IO) {
  561. ts_LOGf("Input file : %s\n", ts->input.fd == 0 ? "STDIN" : ts->input.fname);
  562. }
  563. if (ts->input_dump_filename) {
  564. ts->input_dump_file = fopen(ts->input_dump_filename, "w");
  565. if (ts->input_dump_file)
  566. ts_LOGf("Input dump : %s\n", ts->input_dump_filename);
  567. else
  568. ts_LOGf("Input dump : %s | ERROR: %s\n", ts->input_dump_filename, strerror(errno));
  569. }
  570. if (ts->forced_service_id)
  571. ts_LOGf("Service id : 0x%04x (%d)\n",
  572. ts->forced_service_id, ts->forced_service_id);
  573. if (ts->req_CA_sys == CA_IRDETO)
  574. ts_LOGf("Irdeto ECM : %d\n", ts->irdeto_ecm);
  575. if (!ts->emm_only)
  576. {
  577. if (ts->output.type == NET_IO) {
  578. ts_LOGf("Output addr: %s://%s:%u/\n",
  579. ts->rtp_output ? "rtp" : "udp",
  580. inet_ntoa(ts->output.addr), ts->output.port);
  581. ts_LOGf("Output intf: %s\n", inet_ntoa(ts->output.intf));
  582. ts_LOGf("Output ttl : %d\n", ts->output.ttl);
  583. if (ts->output.tos > -1)
  584. ts_LOGf("Output TOS : %u (0x%02x)\n", ts->output.tos, ts->output.tos);
  585. if (ts->rtp_output) {
  586. ts_LOGf("RTP SSRC : %u (0x%04x)\n",
  587. ts->rtp_ssrc, ts->rtp_ssrc);
  588. // It is recommended that RTP seqnum starts with random number
  589. RAND_bytes((unsigned char *)&(ts->rtp_seqnum), 2);
  590. }
  591. } else if (ts->output.type == FILE_IO) {
  592. ts_LOGf("Output file: %s\n", ts->output.fd == 1 ? "STDOUT" : ts->output.fname);
  593. }
  594. ts_LOGf("Out filter : %s (%s)\n",
  595. ts->pid_filter ? "enabled" : "disabled",
  596. ts->pid_filter ? "output only service related PIDs" : "output everything"
  597. );
  598. if (ts->pid_filter) {
  599. if (ts->nit_passthrough)
  600. ts_LOGf("Out filter : Pass through NIT.\n");
  601. if (ts->eit_passthrough)
  602. ts_LOGf("Out filter : Pass through EIT (EPG).\n");
  603. if (ts->tdt_passthrough)
  604. ts_LOGf("Out filter : Pass through TDT/TOT.\n");
  605. }
  606. }
  607. if (!ts->camd.constant_codeword) {
  608. ts_LOGf("CAMD proto : %s\n", ts->camd.ops.ident);
  609. ts_LOGf("CAMD addr : tcp://%s:%u/\n", inet_ntoa(ts->camd.server_addr), ts->camd.server_port);
  610. ts_LOGf("CAMD user : %s\n", ts->camd.user);
  611. ts_LOGf("CAMD pass : %s\n", ts->camd.pass);
  612. if (ts->camd.ops.proto == CAMD_NEWCAMD)
  613. ts_LOGf("CAMD deskey: %s\n", ts->camd.newcamd.hex_des_key);
  614. }
  615. ts_LOGf("TS discont : %s\n", ts->ts_discont ? "report" : "ignore");
  616. ts->threaded = !(ts->input.type == FILE_IO && ts->input.fd != 0);
  617. if (ts->emm_send && ts->emm_report_interval)
  618. ts_LOGf("EMM report : %d sec\n", ts->emm_report_interval);
  619. if (ts->emm_send && ts->emm_report_interval == 0)
  620. ts_LOGf("EMM report : disabled\n");
  621. if (ts->forced_emm_pid)
  622. ts_LOGf("EMM pid : 0x%04x (%d)\n", ts->forced_emm_pid, ts->forced_emm_pid);
  623. if (ts->emm_only) {
  624. ts_LOGf("EMM only : %s\n", ts->emm_only ? "yes" : "no");
  625. } else {
  626. if (!ts->camd.constant_codeword)
  627. ts_LOGf("EMM send : %s\n", ts->emm_send ? "enabled" : "disabled");
  628. ts_LOGf("Decoding : %s\n", ts->threaded ? "threaded" : "single thread");
  629. }
  630. if (!ts->emm_only && ts->ecm_report_interval)
  631. ts_LOGf("ECM report : %d sec\n", ts->emm_report_interval);
  632. if (!ts->emm_only && ts->ecm_report_interval == 0 && !ts->camd.constant_codeword)
  633. ts_LOGf("ECM report : disabled\n");
  634. if (ts->forced_ecm_pid)
  635. ts_LOGf("ECM pid : 0x%04x (%d)\n", ts->forced_ecm_pid, ts->forced_ecm_pid);
  636. if (!ts->emm_only && ts->cw_warn_sec)
  637. ts_LOGf("CW warning : %d sec\n", ts->cw_warn_sec);
  638. if (!ts->emm_only && ts->cw_warn_sec && !ts->camd.constant_codeword)
  639. ts_LOGf("CW warning : disabled\n");
  640. if (!ts->ecm_cw_log && !ts->camd.constant_codeword)
  641. ts_LOGf("ECM/CW log : disabled\n");
  642. if (ts->ident) {
  643. int len = strlen(ts->ident);
  644. for (i = 0; i < len; i++) {
  645. if (ts->ident[i] == '/')
  646. ts->ident[i] = '-';
  647. }
  648. }
  649. }
  650. static void report_emms(struct ts *ts, time_t now) {
  651. ts_LOGf("EMM | Received %u and processed %u in %lu seconds.\n",
  652. ts->emm_seen_count,
  653. ts->emm_processed_count,
  654. now - ts->emm_last_report);
  655. if (ts->emm_seen_count == 0) {
  656. notify(ts, "NO_EMM_RECEIVED", "No EMMs were received in last %lu seconds.",
  657. now - ts->emm_last_report);
  658. }
  659. ts->emm_last_report = now;
  660. ts->emm_seen_count = 0;
  661. ts->emm_processed_count = 0;
  662. }
  663. static void report_ecms(struct ts *ts, time_t now) {
  664. ts_LOGf("ECM | Received %u (%u dup) and processed %u in %lu seconds.\n",
  665. ts->ecm_seen_count,
  666. ts->ecm_duplicate_count,
  667. ts->ecm_processed_count,
  668. now - ts->ecm_last_report);
  669. ts->ecm_last_report = now;
  670. ts->ecm_seen_count = 0;
  671. ts->ecm_duplicate_count = 0;
  672. ts->ecm_processed_count = 0;
  673. }
  674. static void report_cw_warn(struct ts *ts, time_t now) {
  675. if (now - ts->key.ts > 1) {
  676. notify(ts, "NO_CODE_WORD", "No valid code word was received in %ld sec.",
  677. now - ts->key.ts);
  678. ts_LOGf("CW | *ERR* No valid code word was received for %ld seconds!\n",
  679. now - ts->key.ts);
  680. }
  681. ts->cw_last_warn = now;
  682. ts->cw_next_warn = now + ts->cw_warn_sec;
  683. }
  684. static void do_reports(struct ts *ts) {
  685. static int first_emm_report = 1;
  686. static int first_ecm_report = 1;
  687. time_t now = time(NULL);
  688. if (ts->emm_send && ts->emm_report_interval) {
  689. if (first_emm_report && now >= ts->emm_last_report) {
  690. first_emm_report = 0;
  691. ts->emm_last_report -= FIRST_REPORT_SEC;
  692. report_emms(ts, now);
  693. } else if ((time_t)(ts->emm_last_report + ts->emm_report_interval) <= now) {
  694. report_emms(ts, now);
  695. }
  696. }
  697. if (!ts->emm_only && ts->ecm_report_interval) {
  698. if (first_ecm_report && now >= ts->ecm_last_report) {
  699. first_ecm_report = 0;
  700. ts->ecm_last_report -= FIRST_REPORT_SEC;
  701. report_ecms(ts, now);
  702. } else if ((time_t)(ts->ecm_last_report + ts->ecm_report_interval) <= now) {
  703. report_ecms(ts, now);
  704. }
  705. }
  706. if (!ts->emm_only && !ts->key.is_valid_cw) {
  707. if (ts->cw_warn_sec && now >= ts->cw_next_warn) {
  708. report_cw_warn(ts, now);
  709. }
  710. }
  711. }
  712. void signal_quit(int sig) {
  713. if (!keep_running)
  714. raise(sig);
  715. keep_running = 0;
  716. ts_LOGf("Killed %s with signal %d\n", program_id, sig);
  717. signal(sig, SIG_DFL);
  718. }
  719. #define RTP_HDR_SZ 12
  720. static uint8_t ts_packet[FRAME_SIZE + RTP_HDR_SZ];
  721. static uint8_t rtp_hdr[2][RTP_HDR_SZ];
  722. static struct ts ts;
  723. int main(int argc, char **argv) {
  724. ssize_t readen;
  725. int have_data = 1;
  726. int ntimeouts = 0;
  727. time_t timeout_start = time(NULL);
  728. int rtp_hdr_pos = 0, num_packets = 0;
  729. struct rlimit rl;
  730. if (getrlimit(RLIMIT_STACK, &rl) == 0) {
  731. if (rl.rlim_cur > THREAD_STACK_SIZE) {
  732. rl.rlim_cur = THREAD_STACK_SIZE;
  733. setrlimit(RLIMIT_STACK, &rl);
  734. }
  735. }
  736. memset(rtp_hdr[0], 0, RTP_HDR_SZ);
  737. memset(rtp_hdr[1], 0, RTP_HDR_SZ);
  738. data_init(&ts);
  739. ts_set_log_func(LOG_func);
  740. parse_options(&ts, argc, argv);
  741. if (ts.pidfile)
  742. daemonize(ts.pidfile);
  743. if (ts.syslog_active) {
  744. if (ts.syslog_remote) {
  745. log_init(ts.ident, 1, 1, ts.syslog_host, ts.syslog_port);
  746. remote_syslog = 1;
  747. } else {
  748. openlog(ts.ident, LOG_NDELAY | LOG_PID, LOG_USER);
  749. local_syslog = 1;
  750. }
  751. }
  752. ts.notify = notify_alloc(&ts);
  753. ts_LOGf("Start %s\n", program_id);
  754. notify(&ts, "START", "Starting %s", program_id);
  755. if (ts.input.type == NET_IO && udp_connect_input(&ts.input) < 1)
  756. goto EXIT;
  757. if (ts.output.type == NET_IO && udp_connect_output(&ts.output) < 1)
  758. goto EXIT;
  759. signal(SIGCHLD, SIG_IGN);
  760. signal(SIGPIPE, SIG_IGN);
  761. signal(SIGINT , signal_quit);
  762. signal(SIGTERM, signal_quit);
  763. if (ts.threaded) {
  764. pthread_create(&ts.decode_thread, &ts.thread_attr, &decode_thread, &ts);
  765. pthread_create(&ts.write_thread , &ts.thread_attr, &write_thread , &ts);
  766. }
  767. ts.emm_last_report = time(NULL) + FIRST_REPORT_SEC;
  768. ts.ecm_last_report = time(NULL) + FIRST_REPORT_SEC;
  769. camd_start(&ts);
  770. do {
  771. if (!ts.camd.constant_codeword)
  772. do_reports(&ts);
  773. if (ts.input.type == NET_IO) {
  774. set_log_io_errors(0);
  775. if (!ts.rtp_input) {
  776. readen = fdread_ex(ts.input.fd, (char *)ts_packet, FRAME_SIZE, 250, 4, 1);
  777. } else {
  778. readen = fdread_ex(ts.input.fd, (char *)ts_packet, FRAME_SIZE + RTP_HDR_SZ, 250, 4, 1);
  779. if (readen > RTP_HDR_SZ) {
  780. memcpy(rtp_hdr[rtp_hdr_pos], ts_packet, RTP_HDR_SZ);
  781. memmove(ts_packet, ts_packet + RTP_HDR_SZ, FRAME_SIZE);
  782. readen -= RTP_HDR_SZ;
  783. uint16_t ssrc = (rtp_hdr[rtp_hdr_pos][2] << 8) | rtp_hdr[rtp_hdr_pos][3];
  784. uint16_t pssrc = (rtp_hdr[!rtp_hdr_pos][2] << 8) | rtp_hdr[!rtp_hdr_pos][3];
  785. rtp_hdr_pos = !rtp_hdr_pos;
  786. if (pssrc + 1 != ssrc && (ssrc != 0 && pssrc != 0xffff) && num_packets > 2)
  787. if (ts.ts_discont)
  788. ts_LOGf("--- | RTP discontinuity last_ssrc %5d, curr_ssrc %5d, lost %d packet\n",
  789. pssrc, ssrc, ((ssrc - pssrc)-1) & 0xffff);
  790. num_packets++;
  791. }
  792. }
  793. set_log_io_errors(1);
  794. if (readen < 0) {
  795. ts_LOGf("--- | Input read timeout.\n");
  796. if (!ntimeouts) {
  797. timeout_start = time(NULL);
  798. notify(&ts, "INPUT_TIMEOUT", "Read timeout on input %s://%s:%u/",
  799. ts.rtp_input ? "rtp" : "udp",
  800. inet_ntoa(ts.input.addr), ts.input.port);
  801. }
  802. ntimeouts++;
  803. } else {
  804. if (ntimeouts && readen > 0) {
  805. notify(&ts, "INPUT_OK", "Data is available on input %s://%s:%u/ after %ld seconds timeout.",
  806. ts.rtp_input ? "rtp" : "udp",
  807. inet_ntoa(ts.input.addr), ts.input.port,
  808. (time(NULL) - timeout_start) + 2); // Timeout is detected when ~2 seconds there is no incoming data
  809. ntimeouts = 0;
  810. }
  811. }
  812. } else {
  813. readen = read(ts.input.fd, ts_packet, FRAME_SIZE);
  814. have_data = !(readen <= 0);
  815. }
  816. if (readen > 0) {
  817. if (ts.input_dump_file)
  818. fwrite(ts_packet, readen, 1, ts.input_dump_file);
  819. process_packets(&ts, ts_packet, readen);
  820. }
  821. if (!keep_running)
  822. break;
  823. } while (have_data);
  824. EXIT:
  825. camd_stop(&ts);
  826. if (ts.threaded) {
  827. ts.decode_stop = 1;
  828. ts.write_stop = 1;
  829. if (ts.decode_thread)
  830. pthread_join(ts.decode_thread, NULL);
  831. if (ts.write_thread)
  832. pthread_join(ts.write_thread, NULL);
  833. }
  834. show_pid_report(&ts);
  835. notify_sync(&ts, "STOP", "Stopping %s", program_id);
  836. ts_LOGf("Stop %s\n", program_id);
  837. if (ts.syslog_active) {
  838. if (ts.syslog_remote)
  839. log_close();
  840. else
  841. closelog();
  842. }
  843. if (ts.input_dump_file)
  844. fclose(ts.input_dump_file);
  845. if (ts.pidfile)
  846. unlink(ts.pidfile);
  847. if (log_file)
  848. fclose(log_file);
  849. notify_free(&ts.notify);
  850. data_free(&ts);
  851. exit(EXIT_SUCCESS);
  852. }