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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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 (COPYING file) for more details.
  13. *
  14. */
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <getopt.h>
  18. #include <string.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <signal.h>
  22. #include <fcntl.h>
  23. #include <errno.h>
  24. #include <syslog.h>
  25. #include <sys/resource.h>
  26. #include <openssl/rand.h>
  27. #include "libfuncs/libfuncs.h"
  28. #include "data.h"
  29. #include "util.h"
  30. #include "csa.h"
  31. #include "camd.h"
  32. #include "process.h"
  33. #include "udp.h"
  34. #include "notify.h"
  35. #include "filter.h"
  36. #include "fixups.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 " " DLIB ")";
  40. static int keep_running = 1;
  41. static FILE *log_file = NULL;
  42. static char *log_filename = NULL;
  43. static int local_syslog = 0;
  44. static int remote_syslog = 0;
  45. static int packet_from_file = 0;
  46. static int packet_buflen;
  47. static uint8_t packet_buf[256];
  48. static enum msg_type packet_type = ECM_MSG;
  49. extern int ai_family;
  50. static void do_log(FILE *f, time_t now, const char *msg) {
  51. char date[64];
  52. struct tm tm;
  53. // There is no need to show timestamps when debug options are used
  54. if (packet_from_file) {
  55. fprintf(f, "%s", msg);
  56. return;
  57. }
  58. localtime_r(&now, &tm);
  59. strftime(date, sizeof(date), "%F %H:%M:%S", localtime_r(&now, &tm));
  60. fprintf(f, "%s | %s", date, msg);
  61. }
  62. static void LOG_func(const char *msg) {
  63. time_t now = time(NULL);
  64. do_log(stderr, now, msg);
  65. if (log_file)
  66. do_log(log_file, now, msg);
  67. if (local_syslog)
  68. syslog(LOG_INFO, msg, strlen(msg));
  69. if (remote_syslog)
  70. LOG(msg);
  71. }
  72. static const char short_options[] = "i:d:N:Sl:L:F:I:RzM:T:W:O:o:t:rk:g:upwxyc:C:Y:Q:A:s:U:P:B:46eZ:Ef:a:X:H:G:KJ:D:jbhVn:m:q";
  73. // Unused short options: av01235789
  74. static const struct option long_options[] = {
  75. { "ident", required_argument, NULL, 'i' },
  76. { "daemon", required_argument, NULL, 'd' },
  77. { "syslog", no_argument, NULL, 'S' },
  78. { "syslog-host", required_argument, NULL, 'l' },
  79. { "syslog-port", required_argument, NULL, 'L' },
  80. { "log-file", required_argument, NULL, 'F' },
  81. { "notify-program", required_argument, NULL, 'N' },
  82. { "input", required_argument, NULL, 'I' },
  83. { "input-rtp", no_argument, NULL, 'R' },
  84. { "input-ignore-disc", no_argument, NULL, 'z' },
  85. { "input-service", required_argument, NULL, 'M' },
  86. { "input-buffer", required_argument, NULL, 'T' },
  87. { "input-dump", required_argument, NULL, 'W' },
  88. { "output", required_argument, NULL, 'O' },
  89. { "output-intf", required_argument, NULL, 'o' },
  90. { "output-ttl", required_argument, NULL, 't' },
  91. { "output-rtp", no_argument, NULL, 'r' },
  92. { "output-rtp-ssrc", required_argument, NULL, 'k' },
  93. { "output-tos", required_argument, NULL, 'g' },
  94. { "no-output-on-error", no_argument, NULL, 'u' },
  95. { "no-output-filter", no_argument, NULL, 'p' },
  96. { "output-nit-pass", no_argument, NULL, 'y' },
  97. { "output-eit-pass", no_argument, NULL, 'w' },
  98. { "output-tdt-pass", no_argument, NULL, 'x' },
  99. { "ca-system", required_argument, NULL, 'c' },
  100. { "caid", required_argument, NULL, 'C' },
  101. { "const-cw", required_argument, NULL, 'Y' },
  102. { "biss-key", required_argument, NULL, 'Q' },
  103. { "camd-proto", required_argument, NULL, 'A' },
  104. { "camd-server", required_argument, NULL, 's' },
  105. { "camd-user", required_argument, NULL, 'U' },
  106. { "camd-pass", required_argument, NULL, 'P' },
  107. { "camd-des-key", required_argument, NULL, 'B' },
  108. { "ipv4", no_argument, NULL, '4' },
  109. { "ipv6", no_argument, NULL, '6' },
  110. { "emm", no_argument, NULL, 'e' },
  111. { "emm-pid", required_argument, NULL, 'Z' },
  112. { "emm-only", no_argument, NULL, 'E' },
  113. { "emm-report-time", required_argument, NULL, 'f' },
  114. { "emm-filter", required_argument, NULL, 'a' },
  115. { "no-emm-fixups", no_argument, NULL, 'q' },
  116. { "ecm-pid", required_argument, NULL, 'X' },
  117. { "ecm-report-time", required_argument, NULL, 'H' },
  118. { "ecm-irdeto-type", required_argument, NULL, 'G' },
  119. { "ecm-no-log", no_argument , NULL, 'K' },
  120. { "cw-warn-time", required_argument, NULL, 'J' },
  121. { "debug", required_argument, NULL, 'D' },
  122. { "pid-report", no_argument, NULL, 'j' },
  123. { "bench", no_argument, NULL, 'b' },
  124. { "help", no_argument, NULL, 'h' },
  125. { "version", no_argument, NULL, 'V' },
  126. { "ecm-file", required_argument, NULL, 'n' },
  127. { "emm-file", required_argument, NULL, 'm' },
  128. { 0, 0, 0, 0 }
  129. };
  130. static void show_help(struct ts *ts) {
  131. printf("%s\n", program_id);
  132. printf("Copyright (C) 2011-2012 Unix Solutions Ltd.\n");
  133. printf("\n");
  134. printf(" Usage: " PROGRAM_NAME " [opts]\n");
  135. printf("\n");
  136. printf("Main options:\n");
  137. printf(" -i --ident <server> | Format PROVIDER/CHANNEL. Default: empty\n");
  138. printf(" -d --daemon <pidfile> | Daemonize program and write pid file.\n");
  139. printf(" -N --notify-program <prg> | Execute <prg> to report events. Default: empty\n");
  140. printf("\n");
  141. printf("Input options:\n");
  142. printf(" -I --input <source> | Where to read from. File or multicast address.\n");
  143. printf(" . -I 224.0.0.1:5000 (v4 multicast)\n");
  144. printf(" . -I [ff01::1111]:5000 (v6 multicast)\n");
  145. printf(" . -I file://in.ts (read from file)\n");
  146. printf(" . By default the input is stdin.\n");
  147. printf(" -R --input-rtp | Enable RTP input\n");
  148. printf(" -z --input-ignore-disc | Do not report discontinuty errors in input.\n");
  149. printf(" -M --input-service <srvid> | Choose service id when input is MPTS.\n");
  150. printf(" -T --input-buffer <ms> | Set input buffer time in ms. Default: %u\n", ts->input_buffer_time);
  151. printf(" -W --input-dump <filename> | Save input stream in file.\n");
  152. printf("\n");
  153. printf("Output options:\n");
  154. printf(" -O --output <dest> | Where to send output. File or multicast address.\n");
  155. printf(" . -O 239.0.0.1:5000 (v4 multicast)\n");
  156. printf(" . -O [ff01::2222]:5000 (v6 multicast)\n");
  157. printf(" . -O file://out.ts (write to file)\n");
  158. printf(" . By default the output is stdout.\n");
  159. printf(" -o --output-intf <value> | Set multicast output interface.\n");
  160. printf(" . Default for IPv4: 0.0.0.0 (intf addr)\n");
  161. printf(" . Default for IPv6: -1 (intf number)\n");
  162. printf(" -t --output-ttl <ttl> | Set multicast ttl. Default: %d\n", ts->output.ttl);
  163. printf(" -r --output-rtp | Enable RTP output.\n");
  164. printf(" -k --output-rtp-ssrc <id> | Set RTP SSRC. Default: %u\n", ts->rtp_ssrc);
  165. printf(" -g --output-tos <tos> | Set TOS value of output packets. Default: none\n");
  166. printf(" -u --no-output-on-error | Do not output data when the code word is missing.\n");
  167. printf(" -p --no-output-filter | Disable output filtering. Default: %s\n", ts->pid_filter ? "enabled" : "disabled");
  168. printf(" -y --output-nit-pass | Pass through NIT.\n");
  169. printf(" -w --output-eit-pass | Pass through EIT (EPG).\n");
  170. printf(" -x --output-tdt-pass | Pass through TDT/TOT.\n");
  171. printf("\n");
  172. printf("CA options:\n");
  173. printf(" -c --ca-system <ca_sys> | Process input EMM/ECM from <ca_sys>.\n");
  174. printf(" | Valid systems are: CONAX (default), CRYPTOWORKS,\n");
  175. printf(" . IRDETO, SECA (MEDIAGUARD), VIACCESS,\n");
  176. printf(" . VIDEOGUARD (NDS), NAGRA, DRECRYPT and BULCRYPT.\n");
  177. printf(" -C --caid <caid> | Set CAID. Default: Taken from --ca-system.\n");
  178. printf(" -Y --const-cw <codeword> | Set constant code word for decryption.\n");
  179. printf(" . Example cw: a1a2a3a4a5a6a7a8b1b2b3b4b5b6b7b8\n");
  180. printf(" -Q --biss-key <biss-key> | Set BISS key for decryption.\n");
  181. printf(" . Example key: 112233445566\n");
  182. printf("\n");
  183. printf("CAMD server options:\n");
  184. printf(" -A --camd-proto <proto> | Set CAMD network protocol.\n");
  185. printf(" . Valid protocols are: CS378X (default) and NEWCAMD\n");
  186. printf(" -s --camd-server <host> | Set CAMD server address. Default port: 2233\n");
  187. printf(" . Example IPv4 addr and port: 127.0.0.1:2233\n");
  188. printf(" . Example IPv6 addr and port: [2a00::1014]:2233\n");
  189. printf(" . Example hostname : example.com\n");
  190. printf(" . Example hostname and port : example.com:2233\n");
  191. printf(" . Example IPv4 hostname : ipv4.google.com\n");
  192. printf(" . Example IPv6 hostname : ipv6.google.com\n");
  193. printf(" -U --camd-user <user> | Set CAMD server user. Default: %s\n", ts->camd.user);
  194. printf(" -P --camd-pass <pass> | Set CAMD server password. Default: %s\n", ts->camd.pass);
  195. printf(" -B --camd-des-key <key> | Set DES key for newcamd protocol.\n");
  196. printf(" . Default: %s\n", ts->camd.newcamd.hex_des_key);
  197. printf(" -4 --ipv4 | Use only IPv4 addresses of the camd server.\n");
  198. printf(" -6 --ipv6 | Use only IPv6 addresses of the camd server.\n");
  199. printf("\n");
  200. printf("EMM options:\n");
  201. printf(" -e --emm | Enable sending EMM's to CAMD. Default: %s\n", ts->emm_send ? "enabled" : "disabled");
  202. printf(" -E --emm-only | Send only EMMs to CAMD, skipping ECMs and without\n");
  203. printf(" . decoding the input stream.\n");
  204. printf(" -Z --emm-pid <pid> | Force EMM pid. Default: none\n");
  205. printf(" -f --emm-report-time <sec> | Report each <sec> seconds how much EMMs have been\n");
  206. printf(" . received/processed. Set <sec> to 0 to disable\n");
  207. printf(" . the reports. Default: %d sec\n", ts->emm_report_interval);
  208. printf(" -a --emm-filter <filter> | Add EMM filter defined by <filter>.\n");
  209. printf(" . This option can be used multiple times (max:%u).\n", MAX_FILTERS);
  210. printf(" . See FILTERING file for more info.\n");
  211. printf(" -q --no-emm-fixups | Disable EMM fixups for CRYPTOWORKS and VIACCESS.");
  212. printf("\n");
  213. printf("ECM options:\n");
  214. printf(" -X --ecm-pid <pid> | Force ECM pid. Default: none\n");
  215. printf(" -H --ecm-report-time <sec> | Report each <sec> how much ECMs and CWs have been\n");
  216. printf(" . processed/skipped. Set <sec> to 0 to disable\n");
  217. printf(" . the reports. Default: %d sec\n", ts->ecm_report_interval);
  218. printf(" -G --ecm-irdeto-type <int> | Process IRDETO ECMs with type X /0-3/. Default: %d\n", ts->irdeto_ecm);
  219. printf(" -K --ecm-no-log | Disable ECM and code words logging.\n");
  220. printf(" -J --cw-warn-time <sec> | Warn if no valid code word has been received.\n");
  221. printf(" . Set <sec> to 0 to disable. Default: %d sec\n", ts->cw_warn_sec);
  222. printf("\n");
  223. printf("Logging options:\n");
  224. printf(" -S --syslog | Log messages using syslog.\n");
  225. printf(" -l --syslog-host <host> | Syslog server address. Default: disabled\n");
  226. printf(" -L --syslog-port <port> | Syslog server port. Default: %d\n", ts->syslog_port);
  227. printf(" -F --log-file <filename> | Log to file <filename>.\n");
  228. printf(" -D --debug <level> | Message debug level.\n");
  229. printf(" . 0 = default messages\n");
  230. printf(" . 1 = show PSI tables\n");
  231. printf(" . 2 = show EMMs\n");
  232. printf(" . 3 = show duplicate ECMs\n");
  233. printf(" . 4 = packet debug\n");
  234. printf(" . 5 = packet debug + packet dump\n");
  235. printf("\n");
  236. printf("Debugging options:\n");
  237. printf(" -n --ecm-file <file.txt> | Read ECM from text file.\n");
  238. printf(" -m --emm-file <file.txt> | Read EMM from text file.\n");
  239. printf("\n");
  240. printf("Misc options:\n");
  241. printf(" -j --pid-report | Report how much packets were received.\n");
  242. printf(" -b --bench | Benchmark decrypton.\n");
  243. printf(" -h --help | Show help screen.\n");
  244. printf(" -V --version | Show program version.\n");
  245. printf("\n");
  246. }
  247. static int parse_io_param(struct io *io, char *opt, int open_flags, mode_t open_mode) {
  248. int port_set = 0, host_set;
  249. io->type = WTF_IO;
  250. if (strstr(opt, "file://") == opt) {
  251. io->fname = opt + 7; // strlen("file://")
  252. io->type = FILE_IO;
  253. } else if (strchr(opt, '/')) {
  254. io->fname = opt;
  255. io->type = FILE_IO;
  256. }
  257. if (io->type == FILE_IO) {
  258. io->fd = open(io->fname, open_flags, open_mode);
  259. if (io->fd < 0) {
  260. fprintf(stderr, "ERROR: Can not open file (%s): %s\n", io->fname, strerror(errno));
  261. exit(EXIT_FAILURE);
  262. }
  263. return 1;
  264. }
  265. io->type = NET_IO;
  266. host_set = parse_host_and_port(opt, &io->hostname, &io->service, &port_set);
  267. return !(!port_set || !host_set);
  268. }
  269. extern char *optarg;
  270. extern int optind, opterr, optopt;
  271. static void parse_options(struct ts *ts, int argc, char **argv) {
  272. int j, i, ca_err = 0, server_err = 1, input_addr_err = 0, output_addr_err = 0, ident_err = 0, port_set = 0;
  273. opterr = 0; // Prevent printing of error messages for unknown options in getopt()
  274. while ((j = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
  275. if (j == '?') {
  276. fprintf(stderr, "ERROR: Unknown parameter '%s'.\n", argv[optind - 1]);
  277. exit(EXIT_FAILURE);
  278. }
  279. switch (j) {
  280. case 'i': // -- ident
  281. ts->ident = optarg;
  282. break;
  283. case 'd': // --daemon
  284. ts->pidfile = optarg;
  285. break;
  286. case 'N': // --notify-program
  287. ts->notify_program = optarg;
  288. break;
  289. case 'S': // --syslog
  290. ts->syslog_active = 1;
  291. ts->syslog_remote = 0;
  292. break;
  293. case 'l': // --syslog-host
  294. ts->syslog_host = optarg;
  295. ts->syslog_active = 1;
  296. ts->syslog_remote = 1;
  297. break;
  298. case 'L': // --syslog-port
  299. ts->syslog_port = atoi(optarg);
  300. break;
  301. case 'F': // --log-file
  302. log_filename = optarg;
  303. break;
  304. case 'I': // --input
  305. input_addr_err = !parse_io_param(&ts->input, optarg, O_RDONLY, 0);
  306. break;
  307. case 'R': // --input-rtp
  308. ts->rtp_input = !ts->rtp_input;
  309. break;
  310. case 'z': // --input-ignore-disc
  311. ts->ts_discont = !ts->ts_discont;
  312. break;
  313. case 'M': // --input-service
  314. ts->forced_service_id = strtoul(optarg, NULL, 0) & 0xffff;
  315. break;
  316. case 'T': // --input-buffer
  317. ts->input_buffer_time = strtoul(optarg, NULL, 0);
  318. break;
  319. case 'W': // --input-dump
  320. ts->input_dump_filename = optarg;
  321. break;
  322. case 'O': // --output
  323. output_addr_err = !parse_io_param(&ts->output, optarg,
  324. O_CREAT | O_WRONLY | O_TRUNC,
  325. S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  326. break;
  327. case 'o': // --output-intf
  328. if (strchr(optarg, '.'))
  329. inet_aton(optarg, &ts->output.intf);
  330. else
  331. ts->output.v6_if_index = atoi(optarg);
  332. break;
  333. case 't': // --output-ttl
  334. ts->output.ttl = atoi(optarg);
  335. break;
  336. case 'r': // --output-rtp
  337. ts->rtp_output = 1;
  338. break;
  339. case 'k': // --output-rtp-ssrc
  340. ts->rtp_ssrc = strtoul(optarg, NULL, 0);
  341. break;
  342. case 'g': // --output-tos
  343. ts->output.tos = (uint8_t)strtol(optarg, NULL, 0);
  344. break;
  345. case 'u': // --no-output-on-error
  346. ts->no_output_on_error = !ts->no_output_on_error;
  347. break;
  348. case 'p': // --no-output-filter
  349. ts->pid_filter = !ts->pid_filter;
  350. break;
  351. case 'y': // --output-nit-pass
  352. ts->nit_passthrough = !ts->nit_passthrough;
  353. break;
  354. case 'w': // --output-eit-pass
  355. ts->eit_passthrough = !ts->eit_passthrough;
  356. break;
  357. case 'x': // --output-tdt-pass
  358. ts->tdt_passthrough = !ts->tdt_passthrough;
  359. break;
  360. case 'c': // --ca-system
  361. if (strcasecmp("IRDETO", optarg) == 0)
  362. ts->req_CA_sys = CA_IRDETO;
  363. else if (strcasecmp("CONNAX", optarg) == 0 || strcasecmp("CONAX", optarg) == 0)
  364. ts->req_CA_sys = CA_CONAX;
  365. else if (strcasecmp("CRYPTOWORKS", optarg) == 0)
  366. ts->req_CA_sys = CA_CRYPTOWORKS;
  367. else if (strcasecmp("SECA", optarg) == 0 || strcasecmp("MEDIAGUARD", optarg) == 0)
  368. ts->req_CA_sys = CA_SECA;
  369. else if (strcasecmp("VIACCESS", optarg) == 0)
  370. ts->req_CA_sys = CA_VIACCESS;
  371. else if (strcasecmp("VIDEOGUARD", optarg) == 0 || strcasecmp("NDS", optarg) == 0)
  372. ts->req_CA_sys = CA_VIDEOGUARD;
  373. else if (strcasecmp("NAGRA", optarg) == 0)
  374. ts->req_CA_sys = CA_NAGRA;
  375. else if (strcasecmp("DRE-CRYPT", optarg) == 0 || strcasecmp("DRECRYPT", optarg) == 0)
  376. ts->req_CA_sys = CA_DRECRYPT;
  377. else if (strcasecmp("BULCRYPT", optarg) == 0)
  378. ts->req_CA_sys = CA_BULCRYPT;
  379. else
  380. ca_err = 1;
  381. break;
  382. case 'C': // --caid
  383. ts->forced_caid = strtoul(optarg, NULL, 0) & 0xffff;
  384. break;
  385. case 'Y': // --const-cw
  386. ts->camd.constant_codeword = 1;
  387. if (strlen(optarg) > 2 && optarg[0] == '0' && optarg[1] == 'x')
  388. optarg += 2;
  389. if (strlen(optarg) != CODEWORD_LENGTH * 2) {
  390. fprintf(stderr, "ERROR: Constant code word should be %u characters long.\n", CODEWORD_LENGTH * 2);
  391. exit(EXIT_FAILURE);
  392. }
  393. if (decode_hex_string(optarg, ts->camd.key->cw, strlen(optarg)) < 0) {
  394. fprintf(stderr, "ERROR: Invalid hex string for constant code word: %s\n", optarg);
  395. exit(EXIT_FAILURE);
  396. }
  397. camd_set_cw(ts, ts->camd.key->cw, 0);
  398. ts->camd.key->is_valid_cw = 1;
  399. break;
  400. case 'Q': // --biss-key
  401. ts->camd.constant_codeword = 1;
  402. if (strlen(optarg) > 2 && optarg[0] == '0' && optarg[1] == 'x')
  403. optarg += 2;
  404. uint8_t *key = ts->camd.key->cw;
  405. // Sometimes the BISS keys are entered with their checksums already calculated (16 symbols, 8 bytes)
  406. // This is the same as constant cw with the same key for even and odd
  407. if (strlen(optarg) == (BISSKEY_LENGTH + 2) * 2) {
  408. if (decode_hex_string(optarg, key, strlen(optarg)) < 0) {
  409. fprintf(stderr, "ERROR: Invalid hex string for BISS key: %s\n", optarg);
  410. exit(EXIT_FAILURE);
  411. }
  412. } else {
  413. // BISS key without checksum (12 symbols, 6 bytes)
  414. if (strlen(optarg) != BISSKEY_LENGTH * 2) {
  415. fprintf(stderr, "ERROR: BISS key should be %u characters long.\n", BISSKEY_LENGTH * 2);
  416. exit(EXIT_FAILURE);
  417. }
  418. if (decode_hex_string(optarg, key, strlen(optarg)) < 0) {
  419. fprintf(stderr, "ERROR: Invalid hex string for BISS key: %s\n", optarg);
  420. exit(EXIT_FAILURE);
  421. }
  422. // Calculate BISS KEY crc
  423. memmove(key + 4, key + 3, 3);
  424. key[3] = (uint8_t)(key[0] + key[1] + key[2]);
  425. key[7] = (uint8_t)(key[4] + key[5] + key[6]);
  426. }
  427. // Even and odd keys are the same
  428. memcpy(key + 8, key, 8);
  429. camd_set_cw(ts, ts->camd.key->cw, 0);
  430. ts->camd.key->is_valid_cw = 1;
  431. break;
  432. case 'A': // --camd-proto
  433. if (strcasecmp(optarg, "cs378x") == 0) {
  434. camd_proto_cs378x(&ts->camd.ops);
  435. } else if (strcasecmp(optarg, "newcamd") == 0) {
  436. camd_proto_newcamd(&ts->camd.ops);
  437. } else {
  438. fprintf(stderr, "Unknown CAMD protocol: %s\n", optarg);
  439. exit(EXIT_FAILURE);
  440. }
  441. break;
  442. case 's': // --camd-server
  443. server_err = !parse_host_and_port(optarg, &ts->camd.hostname, &ts->camd.service, &port_set);
  444. break;
  445. case 'U': // --camd-user
  446. if (strlen(optarg) < 64)
  447. ts->camd.user = optarg;
  448. break;
  449. case 'P': // --camd-pass
  450. ts->camd.pass = optarg;
  451. break;
  452. case 'B': // --camd-des-key
  453. if (strlen(optarg) > 2 && optarg[0] == '0' && optarg[1] == 'x')
  454. optarg += 2;
  455. if (strlen(optarg) != DESKEY_LENGTH) {
  456. fprintf(stderr, "ERROR: des key should be %u characters long.\n", DESKEY_LENGTH);
  457. exit(EXIT_FAILURE);
  458. }
  459. strncpy(ts->camd.newcamd.hex_des_key, optarg, sizeof(ts->camd.newcamd.hex_des_key) - 1);
  460. ts->camd.newcamd.hex_des_key[sizeof(ts->camd.newcamd.hex_des_key) - 1] = 0;
  461. break;
  462. case '4': // --ipv4
  463. ai_family = AF_INET;
  464. break;
  465. case '6': // --ipv6
  466. ai_family = AF_INET6;
  467. break;
  468. case 'e': // --emm
  469. ts->emm_send = !ts->emm_send;
  470. break;
  471. case 'Z': // --emm-pid
  472. ts->forced_emm_pid = strtoul(optarg, NULL, 0) & 0x1fff;
  473. break;
  474. case 'E': // --emm-only
  475. ts->emm_only = 1;
  476. ts->emm_send = 1;
  477. break;
  478. case 'f': // --emm-report-time
  479. ts->emm_report_interval = strtoul(optarg, NULL, 10);
  480. if (ts->emm_report_interval > 86400)
  481. ts->emm_report_interval = 86400;
  482. break;
  483. case 'a': // --emm-filter
  484. if (ts->emm_filters_num + 1 > MAX_FILTERS) {
  485. fprintf(stderr, "ERROR: Maximum allowed filters are %d.\n", MAX_FILTERS);
  486. exit(EXIT_FAILURE);
  487. }
  488. if (filter_parse(optarg, &ts->emm_filters[ts->emm_filters_num])) {
  489. ts->emm_filters_num++;
  490. } else {
  491. fprintf(stderr, "ERROR: Can't parse EMM filter: %s\n", optarg);
  492. exit(EXIT_FAILURE);
  493. }
  494. break;
  495. case 'q': // --no-emm-fixups
  496. ts->emm_fixups = !ts->emm_fixups;
  497. break;
  498. case 'X': // --ecm-pid
  499. ts->forced_ecm_pid = strtoul(optarg, NULL, 0) & 0x1fff;
  500. break;
  501. case 'H': // --ecm-report-time
  502. ts->ecm_report_interval = strtoul(optarg, NULL, 10);
  503. if (ts->ecm_report_interval > 86400)
  504. ts->ecm_report_interval = 86400;
  505. break;
  506. case 'G': // --ecm-irdeto-type
  507. ts->irdeto_ecm = atoi(optarg);
  508. break;
  509. case 'K': // --ecm-no-log
  510. ts->ecm_cw_log = !ts->ecm_cw_log;
  511. break;
  512. case 'J': // --cw-warn-time
  513. ts->cw_warn_sec = strtoul(optarg, NULL, 10);
  514. if (ts->cw_warn_sec > 86400)
  515. ts->cw_warn_sec = 86400;
  516. ts->cw_last_warn= ts->cw_last_warn + ts->cw_warn_sec;
  517. break;
  518. case 'D': // --debug
  519. ts->debug_level = atoi(optarg);
  520. if (ts->debug_level > 0)
  521. ts->pid_report = 1;
  522. break;
  523. case 'j': // --pid-report
  524. ts->pid_report = 1;
  525. break;
  526. case 'b': // --bench
  527. csa_benchmark();
  528. exit(EXIT_SUCCESS);
  529. case 'n': // --ecm-file
  530. case 'm': // --emm-file
  531. packet_from_file = 1;
  532. packet_buflen = file_hex2buf(optarg, packet_buf, sizeof(packet_buf));
  533. if (!packet_buflen) {
  534. fprintf(stderr, "ERROR: Can't init packet from file.\n");
  535. exit(1);
  536. }
  537. packet_type = j == 'n' ? ECM_MSG : EMM_MSG;
  538. break;
  539. case 'h': // --help
  540. show_help(ts);
  541. exit(EXIT_SUCCESS);
  542. case 'V': // --version
  543. printf("%s\n", program_id);
  544. exit(EXIT_SUCCESS);
  545. }
  546. }
  547. if (!ts->ident) {
  548. if (ts->syslog_active || ts->notify_program)
  549. ident_err = 1;
  550. }
  551. if (packet_from_file) {
  552. int err = 0;
  553. if (!ts->forced_caid) {
  554. fprintf(stderr, "ERROR: CAID was not set. Use --caid option.\n");
  555. err++;
  556. }
  557. if (!ts->forced_service_id) {
  558. fprintf(stderr, "ERROR: Service id was not set. Use --input-service option.\n");
  559. err++;
  560. }
  561. if (err)
  562. exit(EXIT_FAILURE);
  563. ts->threaded = 0;
  564. input_addr_err = 0;
  565. output_addr_err = 0;
  566. ts->input.type = FILE_IO;
  567. ts->input.fd = 0;
  568. ts->output.type = FILE_IO;
  569. ts->output.fd = 1;
  570. ts->pid_filter = 0;
  571. ts->emm_send = 1;
  572. ts->emm_report_interval = 0;
  573. ts->ecm_report_interval = 0;
  574. ts->cw_warn_sec = 0;
  575. ts->camd.no_reconnect = 1;
  576. ts->camd.check_emm_errors = 1;
  577. ts->emm_filters_num = 0;
  578. }
  579. // Constant codeword is special. Disable conflicting options
  580. if (ts->camd.constant_codeword) {
  581. server_err = 0; // No server settings are required
  582. ts->forced_caid = 0;
  583. ts->forced_ecm_pid = 0;
  584. ts->forced_emm_pid = 0;
  585. ts->emm_send = 0;
  586. ts->emm_report_interval = 0;
  587. ts->ecm_report_interval = 0;
  588. ts->cw_warn_sec = 0;
  589. }
  590. if (ident_err || ca_err || server_err || input_addr_err || output_addr_err || ts->input.type == WTF_IO || ts->output.type == WTF_IO) {
  591. show_help(ts);
  592. if (ident_err)
  593. fprintf(stderr, "ERROR: Ident is not set, please use --ident option.\n");
  594. if (ca_err)
  595. fprintf(stderr, "ERROR: Requested CA system is unsupported.\n");
  596. if (server_err)
  597. fprintf(stderr, "ERROR: CAMD server address is not set or it is invalid.\n");
  598. if (input_addr_err)
  599. fprintf(stderr, "ERROR: Input address is invalid.\n");
  600. if (output_addr_err)
  601. fprintf(stderr, "ERROR: Output address is invalid.\n");
  602. exit(EXIT_FAILURE);
  603. }
  604. if (decode_hex_string(ts->camd.newcamd.hex_des_key, ts->camd.newcamd.bin_des_key, DESKEY_LENGTH) < 0) {
  605. fprintf(stderr, "ERROR: Invalid hex string for des key: %s\n", ts->camd.newcamd.hex_des_key);
  606. exit(EXIT_FAILURE);
  607. }
  608. if (ts->camd.ops.proto == CAMD_NEWCAMD && !port_set) {
  609. fprintf(stderr, "ERROR: CAMD server port is not set. Use --camd-server %s:xxxx to set the port.\n", ts->camd.hostname);
  610. exit(EXIT_FAILURE);
  611. }
  612. if (log_filename) {
  613. log_file = fopen(log_filename, "a");
  614. if (!log_file) {
  615. fprintf(stderr, "ERROR: Can't open log file %s: %s\n", log_filename, strerror(errno));
  616. exit(EXIT_FAILURE);
  617. }
  618. }
  619. if (ts->ident)
  620. ts_LOGf("Ident : %s\n", ts->ident);
  621. if (ts->notify_program)
  622. ts_LOGf("Notify prg : %s\n", ts->notify_program);
  623. if (ts->pidfile)
  624. ts_LOGf("Daemonize : %s pid file.\n", ts->pidfile);
  625. if (ts->syslog_active) {
  626. if (ts->syslog_remote)
  627. ts_LOGf("Syslog : %s:%d\n", ts->syslog_host, ts->syslog_port);
  628. else
  629. ts_LOGf("Syslog : enabled\n");
  630. } else {
  631. if (!packet_from_file)
  632. ts_LOGf("Syslog : disabled\n");
  633. }
  634. if (!ts->camd.constant_codeword) {
  635. if (ts->forced_caid)
  636. ts->req_CA_sys = ts_get_CA_sys(ts->forced_caid);
  637. if (!ts->forced_caid)
  638. ts_LOGf("CA System : %s\n", ts_get_CA_sys_txt(ts->req_CA_sys));
  639. else
  640. ts_LOGf("CA System : %s | CAID: 0x%04x (%d)\n",
  641. ts_get_CA_sys_txt(ts->req_CA_sys),
  642. ts->forced_caid, ts->forced_caid);
  643. } else {
  644. char cw_even[64], cw_odd[64];
  645. ts_hex_dump_buf(cw_even, sizeof(cw_even), ts->key.cw , 8, 0);
  646. ts_hex_dump_buf(cw_odd , sizeof(cw_odd ), ts->key.cw + 8, 8, 0);
  647. ts_LOGf("Constant CW: even = %s\n", cw_even);
  648. ts_LOGf("Constant CW: odd = %s\n", cw_odd);
  649. }
  650. if (ts->emm_fixups) {
  651. switch (ts->req_CA_sys) {
  652. case CA_CRYPTOWORKS:
  653. ts->emm_fixup_func = cryptoworks_reassemble_emm;
  654. break;
  655. case CA_VIACCESS:
  656. ts->emm_fixup_func = viaccess_reassemble_emm;
  657. break;
  658. default:
  659. ts->emm_fixups = 0;
  660. }
  661. }
  662. if (ts->input.type == NET_IO) {
  663. ts_LOGf("Input addr : %s://%s:%s/\n",
  664. ts->rtp_input ? "rtp" : "udp",
  665. ts->input.hostname, ts->input.service);
  666. if (ts->input_buffer_time) {
  667. ts_LOGf("Input buff : %u ms\n", ts->input_buffer_time);
  668. }
  669. } else if (ts->input.type == FILE_IO) {
  670. if (!packet_from_file)
  671. ts_LOGf("Input file : %s\n", ts->input.fd == 0 ? "STDIN" : ts->input.fname);
  672. }
  673. if (ts->input_dump_filename) {
  674. ts->input_dump_file = fopen(ts->input_dump_filename, "w");
  675. if (ts->input_dump_file)
  676. ts_LOGf("Input dump : %s\n", ts->input_dump_filename);
  677. else
  678. ts_LOGf("Input dump : %s | ERROR: %s\n", ts->input_dump_filename, strerror(errno));
  679. }
  680. if (ts->forced_service_id)
  681. ts_LOGf("Service id : 0x%04x (%d)\n",
  682. ts->forced_service_id, ts->forced_service_id);
  683. if (ts->req_CA_sys == CA_IRDETO)
  684. ts_LOGf("Irdeto ECM : %d\n", ts->irdeto_ecm);
  685. if (!ts->emm_only)
  686. {
  687. if (ts->output.type == NET_IO) {
  688. ts_LOGf("Output addr: %s://%s:%s/\n",
  689. ts->rtp_output ? "rtp" : "udp",
  690. ts->output.hostname, ts->output.service);
  691. ts_LOGf("Output intf: %s (IPv6 intf index:%d)\n",
  692. inet_ntoa(ts->output.intf), ts->output.v6_if_index);
  693. ts_LOGf("Output ttl : %d\n", ts->output.ttl);
  694. if (ts->output.tos > -1)
  695. ts_LOGf("Output TOS : %u (0x%02x)\n", ts->output.tos, ts->output.tos);
  696. if (ts->rtp_output) {
  697. ts_LOGf("RTP SSRC : %u (0x%04x)\n",
  698. ts->rtp_ssrc, ts->rtp_ssrc);
  699. // It is recommended that RTP seqnum starts with random number
  700. RAND_bytes((unsigned char *)&(ts->rtp_seqnum), 2);
  701. }
  702. } else if (ts->output.type == FILE_IO) {
  703. if (!packet_from_file)
  704. ts_LOGf("Output file: %s\n", ts->output.fd == 1 ? "STDOUT" : ts->output.fname);
  705. }
  706. if (!packet_from_file) {
  707. ts_LOGf("Out filter : %s (%s)%s\n",
  708. ts->pid_filter ? "enabled" : "disabled",
  709. ts->pid_filter ? "output only service related PIDs" : "output everything",
  710. ts->no_output_on_error ? " (No output on CW error)" : ""
  711. );
  712. }
  713. if (ts->pid_filter) {
  714. if (ts->nit_passthrough)
  715. ts_LOGf("Out filter : Pass through NIT.\n");
  716. if (ts->eit_passthrough)
  717. ts_LOGf("Out filter : Pass through EIT (EPG).\n");
  718. if (ts->tdt_passthrough)
  719. ts_LOGf("Out filter : Pass through TDT/TOT.\n");
  720. }
  721. }
  722. if (!ts->camd.constant_codeword) {
  723. ts_LOGf("CAMD proto : %s\n", ts->camd.ops.ident);
  724. ts_LOGf("CAMD addr : %s:%s%s\n", ts->camd.hostname, ts->camd.service,
  725. ai_family == AF_INET ? " (IPv4 only)" :
  726. ai_family == AF_INET6 ? " (IPv6 only)" :
  727. " (IPv4/IPv6)"
  728. );
  729. ts_LOGf("CAMD user : %s\n", ts->camd.user);
  730. ts_LOGf("CAMD pass : %s\n", ts->camd.pass);
  731. if (ts->camd.ops.proto == CAMD_NEWCAMD)
  732. ts_LOGf("CAMD deskey: %s\n", ts->camd.newcamd.hex_des_key);
  733. }
  734. if (!packet_from_file) {
  735. ts_LOGf("TS discont : %s\n", ts->ts_discont ? "report" : "ignore");
  736. ts->threaded = !(ts->input.type == FILE_IO && ts->input.fd != 0);
  737. }
  738. if (!packet_from_file && !ts->emm_only) {
  739. ts_LOGf("Decoding : %s\n", ts->threaded ? "threaded" : "single thread");
  740. }
  741. if (!packet_from_file) {
  742. if (ts->emm_send && ts->emm_report_interval)
  743. ts_LOGf("EMM report : %d sec\n", ts->emm_report_interval);
  744. if (ts->emm_send && ts->emm_report_interval == 0)
  745. ts_LOGf("EMM report : disabled\n");
  746. if (ts->forced_emm_pid)
  747. ts_LOGf("EMM pid : 0x%04x (%d)\n", ts->forced_emm_pid, ts->forced_emm_pid);
  748. }
  749. if (ts->emm_only) {
  750. ts_LOGf("EMM only : %s%s\n",
  751. ts->emm_only ? "yes" : "no",
  752. ts->emm_fixups ? " +fixups" : "");
  753. } else {
  754. if (!packet_from_file) {
  755. if (!ts->camd.constant_codeword)
  756. ts_LOGf("EMM sending: %s%s\n",
  757. ts->emm_send ? "yes" : "no",
  758. ts->emm_send && ts->emm_fixups ? " +fixups" : "");
  759. ts_LOGf("Decoding : %s\n", ts->threaded ? "threaded" : "single thread");
  760. }
  761. }
  762. if (ts->emm_send) {
  763. for (i = 0; i < ts->emm_filters_num; i++) {
  764. char tmp[512];
  765. filter_dump(&ts->emm_filters[i], tmp, sizeof(tmp));
  766. ts_LOGf("EMM filter : [%2d] %s\n", i + 1, tmp);
  767. }
  768. }
  769. if (!packet_from_file) {
  770. if (!ts->emm_only && ts->ecm_report_interval)
  771. ts_LOGf("ECM report : %d sec\n", ts->emm_report_interval);
  772. if (!ts->emm_only && ts->ecm_report_interval == 0 && !ts->camd.constant_codeword)
  773. ts_LOGf("ECM report : disabled\n");
  774. if (ts->forced_ecm_pid)
  775. ts_LOGf("ECM pid : 0x%04x (%d)\n", ts->forced_ecm_pid, ts->forced_ecm_pid);
  776. if (!ts->emm_only && ts->cw_warn_sec)
  777. ts_LOGf("CW warning : %d sec\n", ts->cw_warn_sec);
  778. if (!ts->emm_only && ts->cw_warn_sec && !ts->camd.constant_codeword)
  779. ts_LOGf("CW warning : disabled\n");
  780. if (!ts->ecm_cw_log && !ts->camd.constant_codeword)
  781. ts_LOGf("ECM/CW log : disabled\n");
  782. }
  783. if (ts->ident) {
  784. int len = strlen(ts->ident);
  785. for (i = 0; i < len; i++) {
  786. if (ts->ident[i] == '/')
  787. ts->ident[i] = '-';
  788. }
  789. }
  790. }
  791. static void report_emms(struct ts *ts, time_t now) {
  792. ts_LOGf("EMM | Received %u, Skipped %u, Sent %u, Processed %u in %lu seconds.\n",
  793. ts->emm_input_count,
  794. ts->emm_skipped_count,
  795. ts->emm_seen_count,
  796. ts->emm_processed_count,
  797. now - ts->emm_last_report);
  798. if (ts->emm_seen_count == 0) {
  799. notify(ts, "NO_EMM_RECEIVED", "No EMMs were received in last %lu seconds.",
  800. now - ts->emm_last_report);
  801. }
  802. ts->emm_last_report = now;
  803. ts->emm_input_count = 0;
  804. ts->emm_seen_count = 0;
  805. ts->emm_skipped_count = 0;
  806. ts->emm_processed_count = 0;
  807. }
  808. static void report_ecms(struct ts *ts, time_t now) {
  809. ts_LOGf("ECM | Received %u (%u dup) and processed %u in %lu seconds.\n",
  810. ts->ecm_seen_count,
  811. ts->ecm_duplicate_count,
  812. ts->ecm_processed_count,
  813. now - ts->ecm_last_report);
  814. ts->ecm_last_report = now;
  815. ts->ecm_seen_count = 0;
  816. ts->ecm_duplicate_count = 0;
  817. ts->ecm_processed_count = 0;
  818. }
  819. static void report_cw_warn(struct ts *ts, time_t now) {
  820. if (now - ts->key.ts > 1) {
  821. notify(ts, "NO_CODE_WORD", "No valid code word was received in %ld sec.",
  822. now - ts->key.ts);
  823. ts_LOGf("CW | *ERR* No valid code word was received for %ld seconds!\n",
  824. now - ts->key.ts);
  825. }
  826. ts->cw_last_warn = now;
  827. ts->cw_next_warn = now + ts->cw_warn_sec;
  828. }
  829. static void do_reports(struct ts *ts) {
  830. static int first_emm_report = 1;
  831. static int first_ecm_report = 1;
  832. time_t now = time(NULL);
  833. if (ts->emm_send && ts->emm_report_interval) {
  834. if (first_emm_report && now >= ts->emm_last_report) {
  835. first_emm_report = 0;
  836. ts->emm_last_report -= FIRST_REPORT_SEC;
  837. report_emms(ts, now);
  838. } else if ((time_t)(ts->emm_last_report + ts->emm_report_interval) <= now) {
  839. report_emms(ts, now);
  840. }
  841. }
  842. if (!ts->emm_only && ts->ecm_report_interval) {
  843. if (first_ecm_report && now >= ts->ecm_last_report) {
  844. first_ecm_report = 0;
  845. ts->ecm_last_report -= FIRST_REPORT_SEC;
  846. report_ecms(ts, now);
  847. } else if ((time_t)(ts->ecm_last_report + ts->ecm_report_interval) <= now) {
  848. report_ecms(ts, now);
  849. }
  850. }
  851. if (!ts->emm_only && !ts->key.is_valid_cw) {
  852. if (ts->cw_warn_sec && now >= ts->cw_next_warn) {
  853. report_cw_warn(ts, now);
  854. }
  855. }
  856. }
  857. void signal_quit(int sig) {
  858. if (!keep_running)
  859. raise(sig);
  860. keep_running = 0;
  861. ts_LOGf("Killed %s with signal %d\n", program_id, sig);
  862. signal(sig, SIG_DFL);
  863. }
  864. #define RTP_HDR_SZ 12
  865. static uint8_t ts_packet[FRAME_SIZE + RTP_HDR_SZ];
  866. static uint8_t rtp_hdr[2][RTP_HDR_SZ];
  867. static struct ts ts;
  868. int main(int argc, char **argv) {
  869. ssize_t readen;
  870. int have_data = 1;
  871. int ntimeouts = 0;
  872. time_t timeout_start = time(NULL);
  873. int rtp_hdr_pos = 0, num_packets = 0;
  874. struct rlimit rl;
  875. if (getrlimit(RLIMIT_STACK, &rl) == 0) {
  876. if (rl.rlim_cur > THREAD_STACK_SIZE) {
  877. rl.rlim_cur = THREAD_STACK_SIZE;
  878. setrlimit(RLIMIT_STACK, &rl);
  879. }
  880. }
  881. memset(rtp_hdr[0], 0, RTP_HDR_SZ);
  882. memset(rtp_hdr[1], 0, RTP_HDR_SZ);
  883. data_init(&ts);
  884. ts_set_log_func(LOG_func);
  885. parse_options(&ts, argc, argv);
  886. if (ts.pidfile)
  887. daemonize(ts.pidfile);
  888. if (ts.syslog_active) {
  889. if (ts.syslog_remote) {
  890. log_init(ts.ident, 1, 1, ts.syslog_host, ts.syslog_port);
  891. remote_syslog = 1;
  892. } else {
  893. openlog(ts.ident, LOG_NDELAY | LOG_PID, LOG_USER);
  894. local_syslog = 1;
  895. }
  896. }
  897. ts.notify = notify_alloc(&ts);
  898. ts_LOGf("Start %s\n", program_id);
  899. notify(&ts, "START", "Starting %s", program_id);
  900. if (ts.input.type == NET_IO && udp_connect_input(&ts.input) < 1)
  901. goto EXIT;
  902. if (ts.output.type == NET_IO && udp_connect_output(&ts.output) < 1)
  903. goto EXIT;
  904. signal(SIGCHLD, SIG_IGN);
  905. signal(SIGPIPE, SIG_IGN);
  906. signal(SIGINT , signal_quit);
  907. signal(SIGTERM, signal_quit);
  908. if (ts.threaded) {
  909. pthread_create(&ts.decode_thread, &ts.thread_attr, &decode_thread, &ts);
  910. pthread_create(&ts.write_thread , &ts.thread_attr, &write_thread , &ts);
  911. }
  912. ts.emm_last_report = time(NULL) + FIRST_REPORT_SEC;
  913. ts.ecm_last_report = time(NULL) + FIRST_REPORT_SEC;
  914. camd_start(&ts);
  915. if (packet_from_file) {
  916. uint8_t tmp[2048];
  917. ts_hex_dump_buf((char *)tmp, sizeof(tmp), packet_buf, packet_buflen, 16);
  918. ts_LOGf("%s | Processing packet with CAID 0x%04x\n", packet_type == ECM_MSG ? "ECM" : "EMM", ts.forced_caid);
  919. ts_LOGf("%s | Packet dump:\n%s\n", packet_type == ECM_MSG ? "ECM" : "EMM", tmp);
  920. camd_process_packet(&ts, camd_msg_alloc(packet_type, ts.forced_caid, ts.forced_service_id, packet_buf, packet_buflen));
  921. goto EXIT;
  922. }
  923. do {
  924. if (!ts.camd.constant_codeword)
  925. do_reports(&ts);
  926. if (ts.input.type == NET_IO) {
  927. set_log_io_errors(0);
  928. if (!ts.rtp_input) {
  929. readen = fdread_ex(ts.input.fd, (char *)ts_packet, FRAME_SIZE, 250, 4, 1);
  930. } else {
  931. readen = fdread_ex(ts.input.fd, (char *)ts_packet, FRAME_SIZE + RTP_HDR_SZ, 250, 4, 1);
  932. if (readen > RTP_HDR_SZ) {
  933. memcpy(rtp_hdr[rtp_hdr_pos], ts_packet, RTP_HDR_SZ);
  934. memmove(ts_packet, ts_packet + RTP_HDR_SZ, FRAME_SIZE);
  935. readen -= RTP_HDR_SZ;
  936. uint16_t ssrc = (rtp_hdr[rtp_hdr_pos][2] << 8) | rtp_hdr[rtp_hdr_pos][3];
  937. uint16_t pssrc = (rtp_hdr[!rtp_hdr_pos][2] << 8) | rtp_hdr[!rtp_hdr_pos][3];
  938. rtp_hdr_pos = !rtp_hdr_pos;
  939. if (pssrc + 1 != ssrc && (ssrc != 0 && pssrc != 0xffff) && num_packets > 2)
  940. if (ts.ts_discont)
  941. ts_LOGf("--- | RTP discontinuity last_ssrc %5d, curr_ssrc %5d, lost %d packet\n",
  942. pssrc, ssrc, ((ssrc - pssrc)-1) & 0xffff);
  943. num_packets++;
  944. }
  945. }
  946. set_log_io_errors(1);
  947. if (readen < 0) {
  948. ts_LOGf("--- | Input read timeout.\n");
  949. if (!ntimeouts) {
  950. timeout_start = time(NULL);
  951. notify(&ts, "INPUT_TIMEOUT", "Read timeout on input %s://%s:%s/",
  952. ts.rtp_input ? "rtp" : "udp",
  953. ts.input.hostname, ts.input.service);
  954. }
  955. ntimeouts++;
  956. } else {
  957. if (ntimeouts && readen > 0) {
  958. notify(&ts, "INPUT_OK", "Data is available on input %s://%s:%s/ after %ld seconds timeout.",
  959. ts.rtp_input ? "rtp" : "udp",
  960. ts.input.hostname, ts.input.service,
  961. (time(NULL) - timeout_start) + 2); // Timeout is detected when ~2 seconds there is no incoming data
  962. ntimeouts = 0;
  963. }
  964. }
  965. } else {
  966. readen = read(ts.input.fd, ts_packet, FRAME_SIZE);
  967. have_data = !(readen <= 0);
  968. }
  969. if (readen > 0) {
  970. if (ts.input_dump_file)
  971. fwrite(ts_packet, readen, 1, ts.input_dump_file);
  972. process_packets(&ts, ts_packet, readen);
  973. }
  974. if (!keep_running)
  975. break;
  976. } while (have_data);
  977. EXIT:
  978. camd_stop(&ts);
  979. if (ts.threaded) {
  980. ts.decode_stop = 1;
  981. ts.write_stop = 1;
  982. if (ts.decode_thread)
  983. pthread_join(ts.decode_thread, NULL);
  984. if (ts.write_thread)
  985. pthread_join(ts.write_thread, NULL);
  986. }
  987. show_pid_report(&ts);
  988. notify_sync(&ts, "STOP", "Stopping %s", program_id);
  989. ts_LOGf("Stop %s\n", program_id);
  990. if (ts.syslog_active) {
  991. if (ts.syslog_remote)
  992. log_close();
  993. else
  994. closelog();
  995. }
  996. if (ts.input_dump_file)
  997. fclose(ts.input_dump_file);
  998. if (ts.pidfile)
  999. unlink(ts.pidfile);
  1000. if (log_file)
  1001. fclose(log_file);
  1002. notify_free(&ts.notify);
  1003. data_free(&ts);
  1004. exit(EXIT_SUCCESS);
  1005. }