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

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