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

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