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

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