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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  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 (COPYING file) for more details.
  13. *
  14. */
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <getopt.h>
  18. #include <string.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <signal.h>
  22. #include <fcntl.h>
  23. #include <errno.h>
  24. #include <syslog.h>
  25. #include <sys/resource.h>
  26. #include "libfuncs/libfuncs.h"
  27. #include "data.h"
  28. #include "util.h"
  29. #include "csa.h"
  30. #include "camd.h"
  31. #include "process.h"
  32. #include "udp.h"
  33. #include "notify.h"
  34. #include "filter.h"
  35. #define FIRST_REPORT_SEC 3
  36. #define PROGRAM_NAME "tsdecrypt"
  37. static const char *program_id = PROGRAM_NAME " v" VERSION " (" GIT_VER ", " DLIB ")";
  38. int keep_running = 1;
  39. static FILE *log_file = NULL;
  40. static char *log_filename = NULL;
  41. static int local_syslog = 0;
  42. static int remote_syslog = 0;
  43. static int packet_from_file = 0;
  44. static int packet_buflen;
  45. static uint8_t packet_buf[256];
  46. static enum msg_type packet_type = ECM_MSG;
  47. extern int ai_family;
  48. static void do_log(FILE *f, time_t now, const char *msg) {
  49. char date[64];
  50. struct tm tm;
  51. // There is no need to show timestamps when debug options are used
  52. if (packet_from_file) {
  53. fprintf(f, "%s", msg);
  54. return;
  55. }
  56. localtime_r(&now, &tm);
  57. strftime(date, sizeof(date), "%F %H:%M:%S", localtime_r(&now, &tm));
  58. fprintf(f, "%s | %s", date, msg);
  59. }
  60. static void LOG_func(const char *msg) {
  61. time_t now = time(NULL);
  62. do_log(stderr, now, msg);
  63. if (log_file)
  64. do_log(log_file, now, msg);
  65. if (local_syslog)
  66. syslog(LOG_INFO, msg, strlen(msg));
  67. if (remote_syslog)
  68. LOG(msg);
  69. }
  70. static const char short_options[] = "i:d:N:90:Sl:L:F:I:1:RzM:T:W:O:o:t:rk:g:upwxyc:C:Y:Q:A:s:U:P:B:46eZ:Ef:a:X:vqH:G:2:KJ:D:jbhVn:m:";
  71. // Unused short options: 3578
  72. static const struct option long_options[] = {
  73. { "ident", required_argument, NULL, 'i' },
  74. { "daemon", required_argument, NULL, 'd' },
  75. { "syslog", no_argument, NULL, 'S' },
  76. { "syslog-host", required_argument, NULL, 'l' },
  77. { "syslog-port", required_argument, NULL, 'L' },
  78. { "log-file", required_argument, NULL, 'F' },
  79. { "notify-program", required_argument, NULL, 'N' },
  80. { "notify-wait", no_argument, NULL, '9' },
  81. { "status-file", required_argument, NULL, '0' },
  82. { "input", required_argument, NULL, 'I' },
  83. { "input-source", required_argument, NULL, '1' },
  84. { "input-rtp", no_argument, NULL, 'R' },
  85. { "input-ignore-disc", no_argument, NULL, 'z' },
  86. { "input-service", required_argument, NULL, 'M' },
  87. { "input-buffer", required_argument, NULL, 'T' },
  88. { "input-dump", required_argument, NULL, 'W' },
  89. { "output", required_argument, NULL, 'O' },
  90. { "output-intf", required_argument, NULL, 'o' },
  91. { "output-ttl", required_argument, NULL, 't' },
  92. { "output-rtp", no_argument, NULL, 'r' },
  93. { "output-rtp-ssrc", required_argument, NULL, 'k' },
  94. { "output-tos", required_argument, NULL, 'g' },
  95. { "no-output-on-error", no_argument, NULL, 'u' },
  96. { "no-output-filter", no_argument, NULL, 'p' },
  97. { "output-nit-pass", no_argument, NULL, 'y' },
  98. { "output-eit-pass", no_argument, NULL, 'w' },
  99. { "output-tdt-pass", no_argument, NULL, 'x' },
  100. { "ca-system", required_argument, NULL, 'c' },
  101. { "caid", required_argument, NULL, 'C' },
  102. { "const-cw", required_argument, NULL, 'Y' },
  103. { "biss-key", required_argument, NULL, 'Q' },
  104. { "camd-proto", required_argument, NULL, 'A' },
  105. { "camd-server", required_argument, NULL, 's' },
  106. { "camd-user", required_argument, NULL, 'U' },
  107. { "camd-pass", required_argument, NULL, 'P' },
  108. { "camd-des-key", required_argument, NULL, 'B' },
  109. { "ipv4", no_argument, NULL, '4' },
  110. { "ipv6", no_argument, NULL, '6' },
  111. { "emm", no_argument, NULL, 'e' },
  112. { "emm-pid", required_argument, NULL, 'Z' },
  113. { "emm-only", no_argument, NULL, 'E' },
  114. { "emm-report-time", required_argument, NULL, 'f' },
  115. { "emm-filter", required_argument, NULL, 'a' },
  116. { "ecm-pid", required_argument, NULL, 'X' },
  117. { "ecm-only", no_argument, NULL, 'v' },
  118. { "ecm-report-time", required_argument, NULL, 'H' },
  119. { "ecm-irdeto-type", required_argument, NULL, 'G' },
  120. { "ecm-irdeto-chid", required_argument, NULL, '2' },
  121. { "ecm-no-log", no_argument , NULL, 'K' },
  122. { "cw-warn-time", required_argument, NULL, 'J' },
  123. { "ecm-and-emm-only", no_argument, NULL, 'q' },
  124. { "debug", required_argument, NULL, 'D' },
  125. { "pid-report", no_argument, NULL, 'j' },
  126. { "bench", no_argument, NULL, 'b' },
  127. { "help", no_argument, NULL, 'h' },
  128. { "version", no_argument, NULL, 'V' },
  129. { "ecm-file", required_argument, NULL, 'n' },
  130. { "emm-file", required_argument, NULL, 'm' },
  131. { 0, 0, 0, 0 }
  132. };
  133. static void show_help(struct ts *ts) {
  134. printf("%s\n", program_id);
  135. printf("Copyright (C) 2011-2012 Unix Solutions Ltd.\n");
  136. printf("\n");
  137. printf(" Usage: " PROGRAM_NAME " [opts]\n");
  138. printf("\n");
  139. printf("Main options:\n");
  140. printf(" -i --ident <server> | Format PROVIDER/CHANNEL. Default: empty\n");
  141. printf(" -d --daemon <pidfile> | Daemonize program and write pid file.\n");
  142. printf(" -N --notify-program <prg> | Execute <prg> to report events. Default: empty\n");
  143. printf(" -9 --notify-wait | Enable one by one notification delivery.\n");
  144. printf(" . Default: not set (async, deliver ASAP)\n");
  145. printf(" -0 --status-file <file> | Save current program status in file.\n");
  146. printf("\n");
  147. printf("Input options:\n");
  148. printf(" -I --input <source> | Where to read from. File or multicast address.\n");
  149. printf(" . -I 224.0.0.1:5000 (v4 multicast)\n");
  150. printf(" . -I [ff01::1111]:5000 (v6 multicast)\n");
  151. printf(" . -I file://in.ts (read from file)\n");
  152. printf(" . By default the input is stdin.\n");
  153. printf(" -1 --input-source <ipaddr> | Set multicast input source ip.\n");
  154. printf(" -R --input-rtp | Enable RTP input\n");
  155. printf(" -z --input-ignore-disc | Do not report discontinuty errors in input.\n");
  156. printf(" -M --input-service <srvid> | Choose service id when input is MPTS.\n");
  157. printf(" -T --input-buffer <ms> | Set input buffer time in ms. Default: %u\n", ts->input_buffer_time);
  158. printf(" -W --input-dump <filename> | Save input stream in file.\n");
  159. printf("\n");
  160. printf("Output options:\n");
  161. printf(" -O --output <dest> | Where to send output. File or multicast address.\n");
  162. printf(" . -O 239.0.0.1:5000 (v4 multicast)\n");
  163. printf(" . -O [ff01::2222]:5000 (v6 multicast)\n");
  164. printf(" . -O file://out.ts (write to file)\n");
  165. printf(" . By default the output is stdout.\n");
  166. printf(" -o --output-intf <value> | Set multicast output interface.\n");
  167. printf(" . Default for IPv4: 0.0.0.0 (intf addr)\n");
  168. printf(" . Default for IPv6: -1 (intf number)\n");
  169. printf(" -t --output-ttl <ttl> | Set multicast ttl. Default: %d\n", ts->output.ttl);
  170. printf(" -r --output-rtp | Enable RTP output.\n");
  171. printf(" -k --output-rtp-ssrc <id> | Set RTP SSRC. Default: %u\n", ts->rtp_ssrc);
  172. printf(" -g --output-tos <tos> | Set TOS value of output packets. Default: none\n");
  173. printf(" -u --no-output-on-error | Do not output data when the code word is missing.\n");
  174. printf(" -p --no-output-filter | Disable output filtering. Default: %s\n", ts->pid_filter ? "enabled" : "disabled");
  175. printf(" -y --output-nit-pass | Pass through NIT.\n");
  176. printf(" -w --output-eit-pass | Pass through EIT (EPG).\n");
  177. printf(" -x --output-tdt-pass | Pass through TDT/TOT.\n");
  178. printf("\n");
  179. printf("CA options:\n");
  180. printf(" -c --ca-system <ca_sys> | Process input EMM/ECM from <ca_sys>.\n");
  181. printf(" | Valid systems are: CONAX (default), CRYPTOWORKS,\n");
  182. printf(" . IRDETO, SECA (MEDIAGUARD), VIACCESS,\n");
  183. printf(" . VIDEOGUARD (NDS), NAGRA, DRECRYPT, BULCRYPT,\n");
  184. printf(" . GRIFFIN and DGCRYPT.\n");
  185. printf(" -C --caid <caid> | Set CAID. Default: Taken from --ca-system.\n");
  186. printf(" -Y --const-cw <codeword> | Set constant code word for decryption.\n");
  187. printf(" . Example cw: a1a2a3a4a5a6a7a8b1b2b3b4b5b6b7b8\n");
  188. printf(" -Q --biss-key <biss-key> | Set BISS key for decryption.\n");
  189. printf(" . Example key: 112233445566\n");
  190. printf("\n");
  191. printf("CAMD server options:\n");
  192. printf(" -A --camd-proto <proto> | Set CAMD network protocol.\n");
  193. printf(" . Valid protocols are: CS378X (default) and NEWCAMD\n");
  194. printf(" -s --camd-server <host> | Set CAMD server address. Default port: 2233\n");
  195. printf(" . Example IPv4 addr and port: 127.0.0.1:2233\n");
  196. printf(" . Example IPv6 addr and port: [2a00::1014]:2233\n");
  197. printf(" . Example hostname : example.com\n");
  198. printf(" . Example hostname and port : example.com:2233\n");
  199. printf(" . Example IPv4 hostname : ipv4.google.com\n");
  200. printf(" . Example IPv6 hostname : ipv6.google.com\n");
  201. printf(" -U --camd-user <user> | Set CAMD server user. Default: %s\n", ts->camd.user);
  202. printf(" -P --camd-pass <pass> | Set CAMD server password. Default: %s\n", ts->camd.pass);
  203. printf(" -B --camd-des-key <key> | Set DES key for newcamd protocol.\n");
  204. printf(" . Default: %s\n", ts->camd.newcamd.hex_des_key);
  205. printf(" -4 --ipv4 | Use only IPv4 addresses of the camd server.\n");
  206. printf(" -6 --ipv6 | Use only IPv6 addresses of the camd server.\n");
  207. printf("\n");
  208. printf("EMM options:\n");
  209. printf(" -e --emm | Enable sending EMM's to CAMD. Default: %s\n", ts->process_emm ? "enabled" : "disabled");
  210. printf(" -E --emm-only | Send only EMMs to CAMD, skipping ECMs and without\n");
  211. printf(" . decoding the input stream.\n");
  212. printf(" -Z --emm-pid <pid> | Force EMM pid. Default: none\n");
  213. printf(" -f --emm-report-time <sec> | Report each <sec> seconds how much EMMs have been\n");
  214. printf(" . received/processed. Set <sec> to 0 to disable\n");
  215. printf(" . the reports. Default: %d sec\n", ts->emm_report_interval);
  216. printf(" -a --emm-filter <filter> | Add EMM filter defined by <filter>.\n");
  217. printf(" . This option can be used multiple times (max:%u).\n", MAX_FILTERS);
  218. printf(" . See FILTERING file for more info.\n");
  219. printf("\n");
  220. printf("ECM options:\n");
  221. printf(" -X --ecm-pid <pid> | Force ECM pid. Default: none\n");
  222. printf(" -v --ecm-only | Send only ECMs to CAMD, skipping EMMs and without\n");
  223. printf(" . decoding the input stream.\n");
  224. printf(" -H --ecm-report-time <sec> | Report each <sec> how much ECMs and CWs have been\n");
  225. printf(" . processed/skipped. Set <sec> to 0 to disable\n");
  226. printf(" . the reports. Default: %d sec\n", ts->ecm_report_interval);
  227. printf(" -G --ecm-irdeto-type <int> | Process IRDETO ECMs with index X /0-255/\n");
  228. printf(" . It is better to use --ecm-irdeto-chid option!\n");
  229. printf(" -2 --ecm-irdeto-chid <int> | Set CHID to filter Irdeto ECMs (Default: 0x0000).\n");
  230. printf(" -K --ecm-no-log | Disable ECM and code words logging.\n");
  231. printf(" -J --cw-warn-time <sec> | Warn if no valid code word has been received.\n");
  232. printf(" . Set <sec> to 0 to disable. Default: %d sec\n", ts->cw_warn_sec);
  233. printf("\n");
  234. printf(" -q --ecm-and-emm-only | Send ECMs and EMMs to CAMD but do not decode\n");
  235. printf(" . the input stream.\n");
  236. printf("\n");
  237. printf("Logging options:\n");
  238. printf(" -S --syslog | Log messages using syslog.\n");
  239. printf(" -l --syslog-host <host> | Syslog server address. Default: disabled\n");
  240. printf(" -L --syslog-port <port> | Syslog server port. Default: %d\n", ts->syslog_port);
  241. printf(" -F --log-file <filename> | Log to file <filename>.\n");
  242. printf(" -D --debug <level> | Message debug level.\n");
  243. printf(" . 0 = default messages\n");
  244. printf(" . 1 = show PSI tables\n");
  245. printf(" . 2 = show EMMs\n");
  246. printf(" . 3 = show duplicate ECMs\n");
  247. printf(" . 4 = packet debug\n");
  248. printf(" . 5 = packet debug + packet dump\n");
  249. printf("\n");
  250. printf("Debugging options:\n");
  251. printf(" -n --ecm-file <file.txt> | Read ECM from text file.\n");
  252. printf(" -m --emm-file <file.txt> | Read EMM from text file.\n");
  253. printf("\n");
  254. printf("Misc options:\n");
  255. printf(" -j --pid-report | Report how much packets were received.\n");
  256. printf(" -b --bench | Benchmark decrypton.\n");
  257. printf(" -h --help | Show help screen.\n");
  258. printf(" -V --version | Show program version.\n");
  259. printf("\n");
  260. }
  261. static int parse_io_param(struct io *io, char *opt, int open_flags, mode_t open_mode) {
  262. int port_set = 0, host_set;
  263. io->type = WTF_IO;
  264. if (strstr(opt, "file://") == opt) {
  265. io->fname = opt + 7; // strlen("file://")
  266. io->type = FILE_IO;
  267. } else if (strchr(opt, '/')) {
  268. io->fname = opt;
  269. io->type = FILE_IO;
  270. }
  271. if (io->type == FILE_IO) {
  272. io->fd = open(io->fname, open_flags, open_mode);
  273. if (io->fd < 0) {
  274. fprintf(stderr, "ERROR: Can not open file (%s): %s\n", io->fname, strerror(errno));
  275. exit(EXIT_FAILURE);
  276. }
  277. return 1;
  278. }
  279. io->type = NET_IO;
  280. host_set = parse_host_and_port(opt, &io->hostname, &io->service, &port_set);
  281. return !(!port_set || !host_set);
  282. }
  283. static void parse_options(struct ts *ts, int argc, char **argv) {
  284. int j, i, ca_err = 0, server_err = 1, input_addr_err = 0, output_addr_err = 0, ident_err = 0, port_set = 0;
  285. while ((j = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
  286. if (j == '?')
  287. exit(EXIT_FAILURE);
  288. switch (j) {
  289. case 'i': // -- ident
  290. ts->ident = optarg;
  291. break;
  292. case 'd': // --daemon
  293. ts->pidfile = optarg;
  294. break;
  295. case 'N': // --notify-program
  296. ts->notify_program = optarg;
  297. break;
  298. case '9': // --notify-wait
  299. ts->notify_wait = !ts->notify_wait;
  300. break;
  301. case '0': // --status-file
  302. ts->status_file = optarg;
  303. ts->status_file_tmp = calloc(1, strlen(optarg) + 16);
  304. snprintf(ts->status_file_tmp, strlen(optarg) + 16, "%s.tmp", optarg);
  305. break;
  306. case 'S': // --syslog
  307. ts->syslog_active = 1;
  308. ts->syslog_remote = 0;
  309. break;
  310. case 'l': // --syslog-host
  311. ts->syslog_host = optarg;
  312. ts->syslog_active = 1;
  313. ts->syslog_remote = 1;
  314. break;
  315. case 'L': // --syslog-port
  316. ts->syslog_port = atoi(optarg);
  317. break;
  318. case 'F': // --log-file
  319. log_filename = optarg;
  320. break;
  321. case 'I': // --input
  322. input_addr_err = !parse_io_param(&ts->input, optarg, O_RDONLY, 0);
  323. break;
  324. case '1': // --input-source
  325. if (!inet_aton(optarg, &ts->input.isrc)) {
  326. fprintf(stderr, "ERROR: Can't parse input-source IP address: %s\n", optarg);
  327. exit(EXIT_FAILURE);
  328. }
  329. break;
  330. case 'R': // --input-rtp
  331. ts->rtp_input = !ts->rtp_input;
  332. break;
  333. case 'z': // --input-ignore-disc
  334. ts->ts_discont = !ts->ts_discont;
  335. break;
  336. case 'M': // --input-service
  337. ts->forced_service_id = strtoul(optarg, NULL, 0) & 0xffff;
  338. break;
  339. case 'T': // --input-buffer
  340. ts->input_buffer_time = strtoul(optarg, NULL, 0);
  341. break;
  342. case 'W': // --input-dump
  343. ts->input_dump_filename = optarg;
  344. break;
  345. case 'O': // --output
  346. output_addr_err = !parse_io_param(&ts->output, optarg,
  347. O_CREAT | O_WRONLY | O_TRUNC,
  348. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  349. break;
  350. case 'o': // --output-intf
  351. if (strchr(optarg, '.'))
  352. inet_aton(optarg, &ts->output.intf);
  353. else
  354. ts->output.v6_if_index = atoi(optarg);
  355. break;
  356. case 't': // --output-ttl
  357. ts->output.ttl = atoi(optarg);
  358. break;
  359. case 'r': // --output-rtp
  360. ts->rtp_output = 1;
  361. break;
  362. case 'k': // --output-rtp-ssrc
  363. ts->rtp_ssrc = strtoul(optarg, NULL, 0);
  364. break;
  365. case 'g': // --output-tos
  366. ts->output.tos = (uint8_t)strtol(optarg, NULL, 0);
  367. break;
  368. case 'u': // --no-output-on-error
  369. ts->no_output_on_error = !ts->no_output_on_error;
  370. break;
  371. case 'p': // --no-output-filter
  372. ts->pid_filter = !ts->pid_filter;
  373. break;
  374. case 'y': // --output-nit-pass
  375. ts->nit_passthrough = !ts->nit_passthrough;
  376. break;
  377. case 'w': // --output-eit-pass
  378. ts->eit_passthrough = !ts->eit_passthrough;
  379. break;
  380. case 'x': // --output-tdt-pass
  381. ts->tdt_passthrough = !ts->tdt_passthrough;
  382. break;
  383. case 'c': // --ca-system
  384. if (strcasecmp("IRDETO", optarg) == 0)
  385. ts->req_CA_sys = CA_IRDETO;
  386. else if (strcasecmp("CONNAX", optarg) == 0 || strcasecmp("CONAX", optarg) == 0)
  387. ts->req_CA_sys = CA_CONAX;
  388. else if (strcasecmp("CRYPTOWORKS", optarg) == 0)
  389. ts->req_CA_sys = CA_CRYPTOWORKS;
  390. else if (strcasecmp("SECA", optarg) == 0 || strcasecmp("MEDIAGUARD", optarg) == 0)
  391. ts->req_CA_sys = CA_SECA;
  392. else if (strcasecmp("VIACCESS", optarg) == 0)
  393. ts->req_CA_sys = CA_VIACCESS;
  394. else if (strcasecmp("VIDEOGUARD", optarg) == 0 || strcasecmp("NDS", optarg) == 0)
  395. ts->req_CA_sys = CA_VIDEOGUARD;
  396. else if (strcasecmp("NAGRA", optarg) == 0)
  397. ts->req_CA_sys = CA_NAGRA;
  398. else if (strcasecmp("DRE-CRYPT", optarg) == 0 || strcasecmp("DRECRYPT", optarg) == 0)
  399. ts->req_CA_sys = CA_DRECRYPT;
  400. else if (strcasecmp("BULCRYPT", optarg) == 0)
  401. ts->req_CA_sys = CA_BULCRYPT;
  402. else if (strcasecmp("GRIFFIN", optarg) == 0)
  403. ts->req_CA_sys = CA_GRIFFIN;
  404. else if (strcasecmp("DGCRYPT", optarg) == 0)
  405. ts->req_CA_sys = CA_DGCRYPT;
  406. else
  407. ca_err = 1;
  408. break;
  409. case 'C': // --caid
  410. ts->forced_caid = strtoul(optarg, NULL, 0) & 0xffff;
  411. break;
  412. case 'Y': // --const-cw
  413. ts->camd.constant_codeword = 1;
  414. if (strlen(optarg) > 2 && optarg[0] == '0' && optarg[1] == 'x')
  415. optarg += 2;
  416. if (strlen(optarg) != CODEWORD_LENGTH * 2) {
  417. fprintf(stderr, "ERROR: Constant code word should be %u characters long.\n", CODEWORD_LENGTH * 2);
  418. exit(EXIT_FAILURE);
  419. }
  420. if (decode_hex_string(optarg, ts->camd.key->cw, strlen(optarg)) < 0) {
  421. fprintf(stderr, "ERROR: Invalid hex string for constant code word: %s\n", optarg);
  422. exit(EXIT_FAILURE);
  423. }
  424. camd_set_cw(ts, ts->camd.key->cw, 0);
  425. ts->camd.key->is_valid_cw = 1;
  426. break;
  427. case 'Q': // --biss-key
  428. ts->camd.constant_codeword = 1;
  429. if (strlen(optarg) > 2 && optarg[0] == '0' && optarg[1] == 'x')
  430. optarg += 2;
  431. uint8_t *key = ts->camd.key->cw;
  432. // Sometimes the BISS keys are entered with their checksums already calculated (16 symbols, 8 bytes)
  433. // This is the same as constant cw with the same key for even and odd
  434. if (strlen(optarg) == (BISSKEY_LENGTH + 2) * 2) {
  435. if (decode_hex_string(optarg, key, strlen(optarg)) < 0) {
  436. fprintf(stderr, "ERROR: Invalid hex string for BISS key: %s\n", optarg);
  437. exit(EXIT_FAILURE);
  438. }
  439. } else {
  440. // BISS key without checksum (12 symbols, 6 bytes)
  441. if (strlen(optarg) != BISSKEY_LENGTH * 2) {
  442. fprintf(stderr, "ERROR: BISS key should be %u characters long.\n", BISSKEY_LENGTH * 2);
  443. exit(EXIT_FAILURE);
  444. }
  445. if (decode_hex_string(optarg, key, strlen(optarg)) < 0) {
  446. fprintf(stderr, "ERROR: Invalid hex string for BISS key: %s\n", optarg);
  447. exit(EXIT_FAILURE);
  448. }
  449. // Calculate BISS KEY crc
  450. memmove(key + 4, key + 3, 3);
  451. key[3] = (uint8_t)(key[0] + key[1] + key[2]);
  452. key[7] = (uint8_t)(key[4] + key[5] + key[6]);
  453. }
  454. // Even and odd keys are the same
  455. memcpy(key + 8, key, 8);
  456. camd_set_cw(ts, ts->camd.key->cw, 0);
  457. ts->camd.key->is_valid_cw = 1;
  458. break;
  459. case 'A': // --camd-proto
  460. if (strcasecmp(optarg, "cs378x") == 0) {
  461. camd_proto_cs378x(&ts->camd.ops);
  462. } else if (strcasecmp(optarg, "newcamd") == 0) {
  463. camd_proto_newcamd(&ts->camd.ops);
  464. } else {
  465. fprintf(stderr, "Unknown CAMD protocol: %s\n", optarg);
  466. exit(EXIT_FAILURE);
  467. }
  468. break;
  469. case 's': // --camd-server
  470. server_err = !parse_host_and_port(optarg, &ts->camd.hostname, &ts->camd.service, &port_set);
  471. break;
  472. case 'U': // --camd-user
  473. if (strlen(optarg) < 64)
  474. ts->camd.user = optarg;
  475. break;
  476. case 'P': // --camd-pass
  477. ts->camd.pass = optarg;
  478. break;
  479. case 'B': // --camd-des-key
  480. if (strlen(optarg) > 2 && optarg[0] == '0' && optarg[1] == 'x')
  481. optarg += 2;
  482. if (strlen(optarg) != DESKEY_LENGTH) {
  483. fprintf(stderr, "ERROR: des key should be %u characters long.\n", DESKEY_LENGTH);
  484. exit(EXIT_FAILURE);
  485. }
  486. strncpy(ts->camd.newcamd.hex_des_key, optarg, sizeof(ts->camd.newcamd.hex_des_key) - 1);
  487. ts->camd.newcamd.hex_des_key[sizeof(ts->camd.newcamd.hex_des_key) - 1] = 0;
  488. break;
  489. case '4': // --ipv4
  490. ai_family = AF_INET;
  491. break;
  492. case '6': // --ipv6
  493. ai_family = AF_INET6;
  494. break;
  495. case 'e': // --emm
  496. ts->process_emm = !ts->process_emm;
  497. break;
  498. case 'Z': // --emm-pid
  499. ts->forced_emm_pid = strtoul(optarg, NULL, 0) & 0x1fff;
  500. break;
  501. case 'E': // --emm-only
  502. ts->process_emm = 1;
  503. ts->process_ecm = 0;
  504. ts->output_stream = 0;
  505. break;
  506. case 'f': // --emm-report-time
  507. ts->emm_report_interval = strtoul(optarg, NULL, 10);
  508. if (ts->emm_report_interval > 86400)
  509. ts->emm_report_interval = 86400;
  510. break;
  511. case 'a': // --emm-filter
  512. if (ts->emm_filters_num + 1 > MAX_FILTERS) {
  513. fprintf(stderr, "ERROR: Maximum allowed filters are %d.\n", MAX_FILTERS);
  514. exit(EXIT_FAILURE);
  515. }
  516. if (filter_parse(optarg, &ts->emm_filters[ts->emm_filters_num])) {
  517. ts->emm_filters_num++;
  518. } else {
  519. fprintf(stderr, "ERROR: Can't parse EMM filter: %s\n", optarg);
  520. exit(EXIT_FAILURE);
  521. }
  522. break;
  523. case 'X': // --ecm-pid
  524. ts->forced_ecm_pid = strtoul(optarg, NULL, 0) & 0x1fff;
  525. break;
  526. case 'v': // --ecm-only
  527. ts->process_emm = 0;
  528. ts->process_ecm = 1;
  529. ts->output_stream = 0;
  530. break;
  531. case 'H': // --ecm-report-time
  532. ts->ecm_report_interval = strtoul(optarg, NULL, 10);
  533. if (ts->ecm_report_interval > 86400)
  534. ts->ecm_report_interval = 86400;
  535. break;
  536. case 'G': // --ecm-irdeto-type
  537. ts->irdeto_ecm_idx = strtoul(optarg, NULL, 0);
  538. ts->irdeto_ecm_filter_type = IRDETO_FILTER_IDX;
  539. break;
  540. case '2': // --ecm-irdeto-chid
  541. ts->irdeto_ecm_chid = strtoul(optarg, NULL, 0);
  542. ts->irdeto_ecm_filter_type = IRDETO_FILTER_CHID;
  543. break;
  544. case 'K': // --ecm-no-log
  545. ts->ecm_cw_log = !ts->ecm_cw_log;
  546. break;
  547. case 'J': // --cw-warn-time
  548. ts->cw_warn_sec = strtoul(optarg, NULL, 10);
  549. if (ts->cw_warn_sec > 86400)
  550. ts->cw_warn_sec = 86400;
  551. ts->cw_last_warn= ts->cw_last_warn + ts->cw_warn_sec;
  552. break;
  553. case 'q': // --ecm-and-emm-only
  554. ts->process_emm = 1;
  555. ts->process_ecm = 1;
  556. ts->output_stream = 0;
  557. break;
  558. case 'D': // --debug
  559. ts->debug_level = atoi(optarg);
  560. if (ts->debug_level > 0)
  561. ts->pid_report = 1;
  562. break;
  563. case 'j': // --pid-report
  564. ts->pid_report = 1;
  565. break;
  566. case 'b': // --bench
  567. csa_benchmark();
  568. exit(EXIT_SUCCESS);
  569. case 'n': // --ecm-file
  570. case 'm': // --emm-file
  571. packet_from_file = 1;
  572. packet_buflen = file_hex2buf(optarg, packet_buf, sizeof(packet_buf));
  573. if (!packet_buflen) {
  574. fprintf(stderr, "ERROR: Can't init packet from file.\n");
  575. exit(1);
  576. }
  577. packet_type = j == 'n' ? ECM_MSG : EMM_MSG;
  578. break;
  579. case 'h': // --help
  580. show_help(ts);
  581. exit(EXIT_SUCCESS);
  582. case 'V': // --version
  583. printf("%s\n", program_id);
  584. exit(EXIT_SUCCESS);
  585. }
  586. }
  587. if (!ts->ident) {
  588. if (ts->syslog_active || ts->notify_program || ts->status_file)
  589. ident_err = 1;
  590. }
  591. if (packet_from_file) {
  592. int err = 0;
  593. if (!ts->forced_caid) {
  594. fprintf(stderr, "ERROR: CAID was not set. Use --caid option.\n");
  595. err++;
  596. }
  597. if (!ts->forced_service_id) {
  598. fprintf(stderr, "ERROR: Service id was not set. Use --input-service option.\n");
  599. err++;
  600. }
  601. if (err)
  602. exit(EXIT_FAILURE);
  603. ts->threaded = 0;
  604. input_addr_err = 0;
  605. output_addr_err = 0;
  606. ts->input.type = FILE_IO;
  607. ts->input.fd = 0;
  608. ts->output.type = FILE_IO;
  609. ts->output.fd = 1;
  610. ts->pid_filter = 0;
  611. ts->process_ecm = 0;
  612. ts->process_emm = 0;
  613. ts->output_stream = 0;
  614. ts->camd.no_reconnect = 1;
  615. ts->camd.check_emm_errors = 1;
  616. ts->emm_filters_num = 0;
  617. }
  618. // Constant codeword is special. Disable conflicting options
  619. if (ts->camd.constant_codeword) {
  620. server_err = 0; // No server settings are required
  621. ts->process_ecm = 0;
  622. ts->process_emm = 0;
  623. ts->output_stream = 1;
  624. }
  625. if (ident_err || ca_err || server_err || input_addr_err || output_addr_err || ts->input.type == WTF_IO || ts->output.type == WTF_IO) {
  626. if (ident_err)
  627. fprintf(stderr, "ERROR: Ident is not set, please use --ident option.\n");
  628. if (ca_err)
  629. fprintf(stderr, "ERROR: Requested CA system is unsupported.\n");
  630. if (server_err)
  631. fprintf(stderr, "ERROR: CAMD server address is not set or it is invalid.\n");
  632. if (input_addr_err)
  633. fprintf(stderr, "ERROR: Input address is invalid.\n");
  634. if (output_addr_err)
  635. fprintf(stderr, "ERROR: Output address is invalid.\n");
  636. exit(EXIT_FAILURE);
  637. }
  638. if (decode_hex_string(ts->camd.newcamd.hex_des_key, ts->camd.newcamd.bin_des_key, DESKEY_LENGTH) < 0) {
  639. fprintf(stderr, "ERROR: Invalid hex string for des key: %s\n", ts->camd.newcamd.hex_des_key);
  640. exit(EXIT_FAILURE);
  641. }
  642. if (ts->camd.ops.proto == CAMD_NEWCAMD && !port_set) {
  643. fprintf(stderr, "ERROR: CAMD server port is not set. Use --camd-server %s:xxxx to set the port.\n", ts->camd.hostname);
  644. exit(EXIT_FAILURE);
  645. }
  646. if (log_filename) {
  647. log_file = fopen(log_filename, "a");
  648. if (!log_file) {
  649. fprintf(stderr, "ERROR: Can't open log file %s: %s\n", log_filename, strerror(errno));
  650. exit(EXIT_FAILURE);
  651. }
  652. }
  653. if (ts->ident)
  654. ts_LOGf("Ident : %s\n", ts->ident);
  655. if (ts->notify_program)
  656. ts_LOGf("Notify prg : %s (%s)\n", ts->notify_program, ts->notify_wait ? "sync" : "async");
  657. if (ts->status_file)
  658. ts_LOGf("Status file: %s\n", ts->status_file);
  659. if (ts->pidfile)
  660. ts_LOGf("Daemonize : %s pid file.\n", ts->pidfile);
  661. if (ts->syslog_active) {
  662. if (ts->syslog_remote)
  663. ts_LOGf("Syslog : %s:%d\n", ts->syslog_host, ts->syslog_port);
  664. else
  665. ts_LOGf("Syslog : enabled\n");
  666. } else {
  667. if (!packet_from_file)
  668. ts_LOGf("Syslog : disabled\n");
  669. }
  670. if (!ts->camd.constant_codeword) {
  671. if (ts->forced_caid)
  672. ts->req_CA_sys = ts_get_CA_sys(ts->forced_caid);
  673. if (!ts->forced_caid)
  674. ts_LOGf("CA System : %s\n", ts_get_CA_sys_txt(ts->req_CA_sys));
  675. else
  676. ts_LOGf("CA System : %s | CAID: 0x%04x (%d)\n",
  677. ts_get_CA_sys_txt(ts->req_CA_sys),
  678. ts->forced_caid, ts->forced_caid);
  679. } else {
  680. char cw_even[64], cw_odd[64];
  681. ts_hex_dump_buf(cw_even, sizeof(cw_even), ts->key.cw , 8, 0);
  682. ts_hex_dump_buf(cw_odd , sizeof(cw_odd ), ts->key.cw + 8, 8, 0);
  683. ts_LOGf("Constant CW: even = %s\n", cw_even);
  684. ts_LOGf("Constant CW: odd = %s\n", cw_odd);
  685. }
  686. if (ts->input.type == NET_IO) {
  687. ts_LOGf("Input addr : %s://%s:%s/\n",
  688. ts->rtp_input ? "rtp" : "udp",
  689. ts->input.hostname, ts->input.service);
  690. ts_LOGf("Input src : %s\n", inet_ntoa(ts->input.isrc));
  691. if (ts->input_buffer_time) {
  692. ts_LOGf("Input buff : %u ms\n", ts->input_buffer_time);
  693. }
  694. } else if (ts->input.type == FILE_IO) {
  695. if (!packet_from_file)
  696. ts_LOGf("Input file : %s\n", ts->input.fd == 0 ? "STDIN" : ts->input.fname);
  697. }
  698. if (ts->input_dump_filename) {
  699. ts->input_dump_file = fopen(ts->input_dump_filename, "w");
  700. if (ts->input_dump_file)
  701. ts_LOGf("Input dump : %s\n", ts->input_dump_filename);
  702. else
  703. ts_LOGf("Input dump : %s | ERROR: %s\n", ts->input_dump_filename, strerror(errno));
  704. }
  705. if (ts->forced_service_id)
  706. ts_LOGf("Service id : 0x%04x (%d)\n",
  707. ts->forced_service_id, ts->forced_service_id);
  708. if (ts->req_CA_sys == CA_IRDETO) {
  709. switch (ts->irdeto_ecm_filter_type) {
  710. case IRDETO_FILTER_IDX : ts_LOGf("Irdeto ECM : Index: 0x%02x (%d)\n", ts->irdeto_ecm_idx, ts->irdeto_ecm_idx); break;
  711. case IRDETO_FILTER_CHID: ts_LOGf("Irdeto ECM : CHID: 0x%04x (%d)\n", ts->irdeto_ecm_chid, ts->irdeto_ecm_chid); break;
  712. }
  713. }
  714. if (ts->output_stream) {
  715. if (ts->output.type == NET_IO) {
  716. ts_LOGf("Output addr: %s://%s:%s/\n",
  717. ts->rtp_output ? "rtp" : "udp",
  718. ts->output.hostname, ts->output.service);
  719. ts_LOGf("Output intf: %s (IPv6 intf index:%d)\n",
  720. inet_ntoa(ts->output.intf), ts->output.v6_if_index);
  721. ts_LOGf("Output ttl : %d\n", ts->output.ttl);
  722. if (ts->output.tos > -1)
  723. ts_LOGf("Output TOS : %u (0x%02x)\n", ts->output.tos, ts->output.tos);
  724. if (ts->rtp_output) {
  725. ts_LOGf("RTP SSRC : %u (0x%04x)\n",
  726. ts->rtp_ssrc, ts->rtp_ssrc);
  727. // It is recommended that RTP seqnum starts with random number
  728. RAND_bytes((unsigned char *)&(ts->rtp_seqnum), 2);
  729. }
  730. } else if (ts->output.type == FILE_IO) {
  731. ts_LOGf("Output file: %s\n", ts->output.fd == 1 ? "STDOUT" : ts->output.fname);
  732. }
  733. ts_LOGf("Out filter : %s (%s)%s\n",
  734. ts->pid_filter ? "enabled" : "disabled",
  735. ts->pid_filter ? "output only service related PIDs" : "output everything",
  736. ts->no_output_on_error ? " (No output on CW error)" : ""
  737. );
  738. if (ts->pid_filter) {
  739. if (ts->nit_passthrough)
  740. ts_LOGf("Out filter : Pass through NIT.\n");
  741. if (ts->eit_passthrough)
  742. ts_LOGf("Out filter : Pass through EIT (EPG).\n");
  743. if (ts->tdt_passthrough)
  744. ts_LOGf("Out filter : Pass through TDT/TOT.\n");
  745. }
  746. ts_LOGf("TS discont : %s\n", ts->ts_discont ? "report" : "ignore");
  747. ts->threaded = !(ts->input.type == FILE_IO && ts->input.fd != 0);
  748. ts_LOGf("Decoding : %s\n", ts->threaded ? "threaded" : "single thread");
  749. } else {
  750. ts_LOGf("Decoding : disabled\n");
  751. }
  752. if (!ts->camd.constant_codeword) {
  753. ts_LOGf("CAMD proto : %s\n", ts->camd.ops.ident);
  754. ts_LOGf("CAMD addr : %s:%s%s\n", ts->camd.hostname, ts->camd.service,
  755. ai_family == AF_INET ? " (IPv4 only)" :
  756. ai_family == AF_INET6 ? " (IPv6 only)" :
  757. " (IPv4/IPv6)"
  758. );
  759. ts_LOGf("CAMD user : %s\n", ts->camd.user);
  760. ts_LOGf("CAMD pass : %s\n", ts->camd.pass);
  761. if (ts->camd.ops.proto == CAMD_NEWCAMD)
  762. ts_LOGf("CAMD deskey: %s\n", ts->camd.newcamd.hex_des_key);
  763. }
  764. if (!packet_from_file)
  765. ts_LOGf("EMM process: %s\n", ts->process_emm ? "Yes" : "No");
  766. if (ts->process_emm) {
  767. if (ts->forced_emm_pid)
  768. ts_LOGf("EMM pid : 0x%04x (%d)\n", ts->forced_emm_pid, ts->forced_emm_pid);
  769. if (ts->emm_report_interval)
  770. ts_LOGf("EMM report : %d sec\n", ts->emm_report_interval);
  771. else
  772. ts_LOGf("EMM report : disabled\n");
  773. for (i = 0; i < ts->emm_filters_num; i++) {
  774. char tmp[512];
  775. filter_dump(&ts->emm_filters[i], tmp, sizeof(tmp));
  776. ts_LOGf("EMM filter : [%2d] %s\n", i + 1, tmp);
  777. }
  778. }
  779. if (!packet_from_file)
  780. ts_LOGf("ECM process: %s\n", ts->process_ecm ? "Yes" : "No");
  781. if (ts->process_ecm) {
  782. if (ts->forced_ecm_pid)
  783. ts_LOGf("ECM pid : 0x%04x (%d)\n", ts->forced_ecm_pid, ts->forced_ecm_pid);
  784. if (ts->ecm_report_interval)
  785. ts_LOGf("ECM report : %d sec\n", ts->emm_report_interval);
  786. else
  787. ts_LOGf("ECM report : disabled\n");
  788. if (ts->cw_warn_sec)
  789. ts_LOGf("CW warning : %d sec\n", ts->cw_warn_sec);
  790. else
  791. ts_LOGf("CW warning : disabled\n");
  792. if (!ts->ecm_cw_log)
  793. ts_LOGf("ECM/CW log : disabled\n");
  794. }
  795. if (ts->ident) {
  796. int len = strlen(ts->ident);
  797. for (i = 0; i < len; i++) {
  798. if (ts->ident[i] == '/')
  799. ts->ident[i] = '-';
  800. }
  801. }
  802. }
  803. static void report_emms(struct ts *ts, time_t now) {
  804. ts_LOGf("EMM | Received %u, Skipped %u, Sent %u, Processed %u in %lu seconds.\n",
  805. ts->emm_input_count,
  806. ts->emm_skipped_count,
  807. ts->emm_seen_count,
  808. ts->emm_processed_count,
  809. now - ts->emm_last_report);
  810. if (ts->emm_seen_count == 0) {
  811. notify(ts, "NO_EMM_RECEIVED", "No EMMs were received in last %lu seconds.",
  812. now - ts->emm_last_report);
  813. }
  814. ts->emm_last_report = now;
  815. ts->emm_input_count = 0;
  816. ts->emm_seen_count = 0;
  817. ts->emm_skipped_count = 0;
  818. ts->emm_processed_count = 0;
  819. }
  820. static void report_ecms(struct ts *ts, time_t now) {
  821. if ((ts->stream_is_not_scrambled || !ts->have_valid_pmt || ts->no_input) && ts->ecm_seen_count == 0)
  822. return;
  823. ts_LOGf("ECM | Received %u (%u dup) and processed %u in %lu seconds.\n",
  824. ts->ecm_seen_count,
  825. ts->ecm_duplicate_count,
  826. ts->ecm_processed_count,
  827. now - ts->ecm_last_report);
  828. ts->ecm_last_report = now;
  829. ts->ecm_seen_count = 0;
  830. ts->ecm_duplicate_count = 0;
  831. ts->ecm_processed_count = 0;
  832. }
  833. static void report_cw_warn(struct ts *ts, time_t now) {
  834. if (ts->stream_is_not_scrambled || !ts->have_valid_pmt || ts->no_input)
  835. return;
  836. if (now - ts->key.ts > 1) {
  837. notify(ts, "NO_CODE_WORD", "No valid code word was received in %ld sec.",
  838. now - ts->key.ts);
  839. ts_LOGf("CW | *ERR* No valid code word was received for %ld seconds!\n",
  840. now - ts->key.ts);
  841. }
  842. ts->cw_last_warn = now;
  843. ts->cw_next_warn = now + ts->cw_warn_sec;
  844. }
  845. static void do_reports(struct ts *ts) {
  846. static int first_emm_report = 1;
  847. static int first_ecm_report = 1;
  848. time_t now = time(NULL);
  849. if (ts->process_emm && ts->emm_report_interval) {
  850. if (first_emm_report && now >= ts->emm_last_report) {
  851. first_emm_report = 0;
  852. ts->emm_last_report -= FIRST_REPORT_SEC;
  853. report_emms(ts, now);
  854. } else if ((time_t)(ts->emm_last_report + ts->emm_report_interval) <= now) {
  855. report_emms(ts, now);
  856. }
  857. }
  858. if (ts->process_ecm && ts->ecm_report_interval) {
  859. if (first_ecm_report && now >= ts->ecm_last_report) {
  860. first_ecm_report = 0;
  861. ts->ecm_last_report -= FIRST_REPORT_SEC;
  862. report_ecms(ts, now);
  863. } else if ((time_t)(ts->ecm_last_report + ts->ecm_report_interval) <= now) {
  864. report_ecms(ts, now);
  865. }
  866. }
  867. if (ts->stream_is_not_scrambled && ts->last_not_scrambled_report_ts <= now - 60) {
  868. ts_LOGf("CLR | No encrypted packets in the last %ld seconds. Stream is clear.\n", now - ts->last_scrambled_packet_ts);
  869. notify(ts, "STREAM_CLEAR", "No encrypted packets in the last %ld seconds. Stream is clear.", now - ts->last_scrambled_packet_ts);
  870. ts->last_not_scrambled_report_ts = now;
  871. ts->key.is_valid_cw = 0;
  872. } else {
  873. if (ts->process_ecm && !ts->key.is_valid_cw) {
  874. if (ts->cw_warn_sec && now >= ts->cw_next_warn) {
  875. report_cw_warn(ts, now);
  876. }
  877. }
  878. }
  879. if (!ts->no_input) {
  880. if (ts->last_pmt_ts <= now - 3) {
  881. if (ts->have_valid_pmt) {
  882. ts_LOGf("MIS | There is no valid PMT in the input.\n");
  883. notify(ts, "NO_PROGRAM", "The input is missing valid program.");
  884. ts->have_valid_pmt = 0;
  885. ts->key.is_valid_cw = 0;
  886. }
  887. }
  888. }
  889. }
  890. static struct ts ts;
  891. void signal_quit(int sig) {
  892. if (!keep_running)
  893. raise(sig);
  894. keep_running = 0;
  895. ts_LOGf("Killed %s with signal %d\n", program_id, sig);
  896. if (ts.input.type == NET_IO)
  897. shutdown_fd(&ts.input.fd);
  898. if (ts.output.type == NET_IO)
  899. shutdown_fd(&ts.output.fd);
  900. signal(sig, SIG_DFL);
  901. }
  902. void signal_alarm(int sig) {
  903. (void)sig;
  904. raise(SIGINT);
  905. }
  906. #define RTP_HDR_SZ 12
  907. static uint8_t ts_packet[FRAME_SIZE + RTP_HDR_SZ];
  908. static uint8_t rtp_hdr[2][RTP_HDR_SZ];
  909. int main(int argc, char **argv) {
  910. ssize_t readen;
  911. int have_data = 1;
  912. int ntimeouts = 0;
  913. time_t timeout_start = time(NULL);
  914. int rtp_hdr_pos = 0, num_packets = 0;
  915. struct rlimit rl;
  916. if (getrlimit(RLIMIT_STACK, &rl) == 0) {
  917. if (rl.rlim_cur > THREAD_STACK_SIZE) {
  918. rl.rlim_cur = THREAD_STACK_SIZE;
  919. setrlimit(RLIMIT_STACK, &rl);
  920. }
  921. }
  922. memset(rtp_hdr[0], 0, RTP_HDR_SZ);
  923. memset(rtp_hdr[1], 0, RTP_HDR_SZ);
  924. data_init(&ts);
  925. ts_set_log_func(LOG_func);
  926. parse_options(&ts, argc, argv);
  927. if (ts.pidfile)
  928. daemonize(ts.pidfile);
  929. if (ts.syslog_active) {
  930. if (ts.syslog_remote) {
  931. log_init(ts.ident, 1, 1, ts.syslog_host, ts.syslog_port);
  932. remote_syslog = 1;
  933. } else {
  934. openlog(ts.ident, LOG_NDELAY | LOG_PID, LOG_USER);
  935. local_syslog = 1;
  936. }
  937. }
  938. ts.notify = notify_alloc(&ts);
  939. ts_LOGf("Start %s\n", program_id);
  940. notify(&ts, "START", "Starting %s", program_id);
  941. if (ts.input.type == NET_IO && udp_connect_input(&ts.input) < 1)
  942. goto EXIT;
  943. if (ts.output.type == NET_IO && udp_connect_output(&ts.output) < 1)
  944. goto EXIT;
  945. signal(SIGCHLD, SIG_IGN);
  946. signal(SIGPIPE, SIG_IGN);
  947. signal(SIGINT , signal_quit);
  948. signal(SIGTERM, signal_quit);
  949. if (ts.threaded) {
  950. pthread_create(&ts.decode_thread, &ts.thread_attr, &decode_thread, &ts);
  951. pthread_create(&ts.write_thread , &ts.thread_attr, &write_thread , &ts);
  952. }
  953. ts.emm_last_report = time(NULL) + FIRST_REPORT_SEC;
  954. ts.ecm_last_report = time(NULL) + FIRST_REPORT_SEC;
  955. camd_start(&ts);
  956. if (packet_from_file) {
  957. uint8_t tmp[2048];
  958. ts_hex_dump_buf((char *)tmp, sizeof(tmp), packet_buf, packet_buflen, 16);
  959. ts_LOGf("%s | Processing packet with CAID 0x%04x\n", packet_type == ECM_MSG ? "ECM" : "EMM", ts.forced_caid);
  960. ts_LOGf("%s | Packet dump:\n%s\n", packet_type == ECM_MSG ? "ECM" : "EMM", tmp);
  961. camd_process_packet(&ts, camd_msg_alloc(packet_type, ts.forced_caid, ts.forced_service_id, packet_buf, packet_buflen));
  962. goto EXIT;
  963. }
  964. do {
  965. if (!ts.camd.constant_codeword)
  966. do_reports(&ts);
  967. if (ts.input.type == NET_IO) {
  968. set_log_io_errors(0);
  969. if (!ts.rtp_input) {
  970. readen = fdread_ex(ts.input.fd, (char *)ts_packet, FRAME_SIZE, 250, 4, 1);
  971. } else {
  972. readen = fdread_ex(ts.input.fd, (char *)ts_packet, FRAME_SIZE + RTP_HDR_SZ, 250, 4, 1);
  973. if (readen > RTP_HDR_SZ) {
  974. memcpy(rtp_hdr[rtp_hdr_pos], ts_packet, RTP_HDR_SZ);
  975. memmove(ts_packet, ts_packet + RTP_HDR_SZ, FRAME_SIZE);
  976. readen -= RTP_HDR_SZ;
  977. uint16_t ssrc = (rtp_hdr[rtp_hdr_pos][2] << 8) | rtp_hdr[rtp_hdr_pos][3];
  978. uint16_t pssrc = (rtp_hdr[!rtp_hdr_pos][2] << 8) | rtp_hdr[!rtp_hdr_pos][3];
  979. rtp_hdr_pos = !rtp_hdr_pos;
  980. if (pssrc + 1 != ssrc && (ssrc != 0 && pssrc != 0xffff) && num_packets > 2)
  981. if (ts.ts_discont)
  982. ts_LOGf("--- | RTP discontinuity last_ssrc %5d, curr_ssrc %5d, lost %d packet\n",
  983. pssrc, ssrc, ((ssrc - pssrc)-1) & 0xffff);
  984. num_packets++;
  985. }
  986. }
  987. set_log_io_errors(1);
  988. if (!keep_running)
  989. break;
  990. if (readen < 0) {
  991. ts_LOGf("--- | Input read timeout.\n");
  992. if (!ntimeouts) {
  993. timeout_start = time(NULL);
  994. notify(&ts, "INPUT_TIMEOUT", "Read timeout on input %s://%s:%s/",
  995. ts.rtp_input ? "rtp" : "udp",
  996. ts.input.hostname, ts.input.service);
  997. }
  998. ts.no_input = 1;
  999. ntimeouts++;
  1000. } else {
  1001. if (ntimeouts && readen > 0) {
  1002. time_t now = time(NULL);
  1003. ts_LOGf("+++ | Input OK after %ld sec timeout.\n", (now - timeout_start) + 2);
  1004. notify(&ts, "INPUT_OK", "Data is available on input %s://%s:%s/ after %ld seconds timeout.",
  1005. ts.rtp_input ? "rtp" : "udp",
  1006. ts.input.hostname, ts.input.service,
  1007. (now - timeout_start) + 2); // Timeout is detected when ~2 seconds there is no incoming data
  1008. ts.no_input = 0;
  1009. ntimeouts = 0;
  1010. }
  1011. }
  1012. } else {
  1013. readen = read(ts.input.fd, ts_packet, FRAME_SIZE);
  1014. have_data = !(readen <= 0);
  1015. }
  1016. if (readen > 0) {
  1017. if (ts.input_dump_file)
  1018. fwrite(ts_packet, readen, 1, ts.input_dump_file);
  1019. process_packets(&ts, ts_packet, readen);
  1020. }
  1021. } while (have_data && keep_running);
  1022. EXIT:
  1023. camd_stop(&ts);
  1024. // If pthread_join failes make sure we exit...
  1025. signal(SIGINT , SIG_DFL);
  1026. signal(SIGTERM, SIG_DFL);
  1027. signal(SIGALRM, signal_alarm);
  1028. alarm(2);
  1029. if (ts.threaded) {
  1030. ts.decode_stop = 1;
  1031. ts.write_stop = 1;
  1032. if (ts.decode_thread)
  1033. pthread_join(ts.decode_thread, NULL);
  1034. if (ts.write_thread)
  1035. pthread_join(ts.write_thread, NULL);
  1036. }
  1037. show_pid_report(&ts);
  1038. notify_sync(&ts, "STOP", "Stopping %s", program_id);
  1039. ts_LOGf("Stop %s\n", program_id);
  1040. if (ts.syslog_active) {
  1041. if (ts.syslog_remote)
  1042. log_close();
  1043. else
  1044. closelog();
  1045. }
  1046. if (ts.input_dump_file)
  1047. fclose(ts.input_dump_file);
  1048. if (ts.pidfile)
  1049. unlink(ts.pidfile);
  1050. if (log_file)
  1051. fclose(log_file);
  1052. notify_free(&ts.notify);
  1053. data_free(&ts);
  1054. exit(EXIT_SUCCESS);
  1055. }