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

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