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

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