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

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