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

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