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

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