tsdecrypt reads and decrypts CSA encrypted incoming mpeg transport stream over UDP/RTP using code words obtained from OSCAM or similar CAM server. tsdecrypt communicates with CAM server using cs378x (camd35 over tcp) protocol or newcamd protocol. https://georgi.unixsol.org/programs/tsdecrypt/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tsdecrypt.c 38KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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. ts_LOGf("ECM | Received %u (%u dup) and processed %u in %lu seconds.\n",
  808. ts->ecm_seen_count,
  809. ts->ecm_duplicate_count,
  810. ts->ecm_processed_count,
  811. now - ts->ecm_last_report);
  812. ts->ecm_last_report = now;
  813. ts->ecm_seen_count = 0;
  814. ts->ecm_duplicate_count = 0;
  815. ts->ecm_processed_count = 0;
  816. }
  817. static void report_cw_warn(struct ts *ts, time_t now) {
  818. if (now - ts->key.ts > 1) {
  819. notify(ts, "NO_CODE_WORD", "No valid code word was received in %ld sec.",
  820. now - ts->key.ts);
  821. ts_LOGf("CW | *ERR* No valid code word was received for %ld seconds!\n",
  822. now - ts->key.ts);
  823. }
  824. ts->cw_last_warn = now;
  825. ts->cw_next_warn = now + ts->cw_warn_sec;
  826. }
  827. static void do_reports(struct ts *ts) {
  828. static int first_emm_report = 1;
  829. static int first_ecm_report = 1;
  830. time_t now = time(NULL);
  831. if (ts->process_emm && ts->emm_report_interval) {
  832. if (first_emm_report && now >= ts->emm_last_report) {
  833. first_emm_report = 0;
  834. ts->emm_last_report -= FIRST_REPORT_SEC;
  835. report_emms(ts, now);
  836. } else if ((time_t)(ts->emm_last_report + ts->emm_report_interval) <= now) {
  837. report_emms(ts, now);
  838. }
  839. }
  840. if (ts->process_ecm && ts->ecm_report_interval) {
  841. if (first_ecm_report && now >= ts->ecm_last_report) {
  842. first_ecm_report = 0;
  843. ts->ecm_last_report -= FIRST_REPORT_SEC;
  844. report_ecms(ts, now);
  845. } else if ((time_t)(ts->ecm_last_report + ts->ecm_report_interval) <= now) {
  846. report_ecms(ts, now);
  847. }
  848. }
  849. if (ts->process_ecm && !ts->key.is_valid_cw) {
  850. if (ts->cw_warn_sec && now >= ts->cw_next_warn) {
  851. report_cw_warn(ts, now);
  852. }
  853. }
  854. }
  855. static struct ts ts;
  856. void signal_quit(int sig) {
  857. if (!keep_running)
  858. raise(sig);
  859. keep_running = 0;
  860. ts_LOGf("Killed %s with signal %d\n", program_id, sig);
  861. if (ts.input.type == NET_IO)
  862. shutdown_fd(&ts.input.fd);
  863. if (ts.output.type == NET_IO)
  864. shutdown_fd(&ts.output.fd);
  865. signal(sig, SIG_DFL);
  866. }
  867. void signal_alarm(int sig) {
  868. (void)sig;
  869. raise(SIGINT);
  870. }
  871. #define RTP_HDR_SZ 12
  872. static uint8_t ts_packet[FRAME_SIZE + RTP_HDR_SZ];
  873. static uint8_t rtp_hdr[2][RTP_HDR_SZ];
  874. int main(int argc, char **argv) {
  875. ssize_t readen;
  876. int have_data = 1;
  877. int ntimeouts = 0;
  878. time_t timeout_start = time(NULL);
  879. int rtp_hdr_pos = 0, num_packets = 0;
  880. struct rlimit rl;
  881. if (getrlimit(RLIMIT_STACK, &rl) == 0) {
  882. if (rl.rlim_cur > THREAD_STACK_SIZE) {
  883. rl.rlim_cur = THREAD_STACK_SIZE;
  884. setrlimit(RLIMIT_STACK, &rl);
  885. }
  886. }
  887. memset(rtp_hdr[0], 0, RTP_HDR_SZ);
  888. memset(rtp_hdr[1], 0, RTP_HDR_SZ);
  889. data_init(&ts);
  890. ts_set_log_func(LOG_func);
  891. parse_options(&ts, argc, argv);
  892. if (ts.pidfile)
  893. daemonize(ts.pidfile);
  894. if (ts.syslog_active) {
  895. if (ts.syslog_remote) {
  896. log_init(ts.ident, 1, 1, ts.syslog_host, ts.syslog_port);
  897. remote_syslog = 1;
  898. } else {
  899. openlog(ts.ident, LOG_NDELAY | LOG_PID, LOG_USER);
  900. local_syslog = 1;
  901. }
  902. }
  903. ts.notify = notify_alloc(&ts);
  904. ts_LOGf("Start %s\n", program_id);
  905. notify(&ts, "START", "Starting %s", program_id);
  906. if (ts.input.type == NET_IO && udp_connect_input(&ts.input) < 1)
  907. goto EXIT;
  908. if (ts.output.type == NET_IO && udp_connect_output(&ts.output) < 1)
  909. goto EXIT;
  910. signal(SIGCHLD, SIG_IGN);
  911. signal(SIGPIPE, SIG_IGN);
  912. signal(SIGINT , signal_quit);
  913. signal(SIGTERM, signal_quit);
  914. if (ts.threaded) {
  915. pthread_create(&ts.decode_thread, &ts.thread_attr, &decode_thread, &ts);
  916. pthread_create(&ts.write_thread , &ts.thread_attr, &write_thread , &ts);
  917. }
  918. ts.emm_last_report = time(NULL) + FIRST_REPORT_SEC;
  919. ts.ecm_last_report = time(NULL) + FIRST_REPORT_SEC;
  920. camd_start(&ts);
  921. if (packet_from_file) {
  922. uint8_t tmp[2048];
  923. ts_hex_dump_buf((char *)tmp, sizeof(tmp), packet_buf, packet_buflen, 16);
  924. ts_LOGf("%s | Processing packet with CAID 0x%04x\n", packet_type == ECM_MSG ? "ECM" : "EMM", ts.forced_caid);
  925. ts_LOGf("%s | Packet dump:\n%s\n", packet_type == ECM_MSG ? "ECM" : "EMM", tmp);
  926. camd_process_packet(&ts, camd_msg_alloc(packet_type, ts.forced_caid, ts.forced_service_id, packet_buf, packet_buflen));
  927. goto EXIT;
  928. }
  929. do {
  930. if (!ts.camd.constant_codeword)
  931. do_reports(&ts);
  932. if (ts.input.type == NET_IO) {
  933. set_log_io_errors(0);
  934. if (!ts.rtp_input) {
  935. readen = fdread_ex(ts.input.fd, (char *)ts_packet, FRAME_SIZE, 250, 4, 1);
  936. } else {
  937. readen = fdread_ex(ts.input.fd, (char *)ts_packet, FRAME_SIZE + RTP_HDR_SZ, 250, 4, 1);
  938. if (readen > RTP_HDR_SZ) {
  939. memcpy(rtp_hdr[rtp_hdr_pos], ts_packet, RTP_HDR_SZ);
  940. memmove(ts_packet, ts_packet + RTP_HDR_SZ, FRAME_SIZE);
  941. readen -= RTP_HDR_SZ;
  942. uint16_t ssrc = (rtp_hdr[rtp_hdr_pos][2] << 8) | rtp_hdr[rtp_hdr_pos][3];
  943. uint16_t pssrc = (rtp_hdr[!rtp_hdr_pos][2] << 8) | rtp_hdr[!rtp_hdr_pos][3];
  944. rtp_hdr_pos = !rtp_hdr_pos;
  945. if (pssrc + 1 != ssrc && (ssrc != 0 && pssrc != 0xffff) && num_packets > 2)
  946. if (ts.ts_discont)
  947. ts_LOGf("--- | RTP discontinuity last_ssrc %5d, curr_ssrc %5d, lost %d packet\n",
  948. pssrc, ssrc, ((ssrc - pssrc)-1) & 0xffff);
  949. num_packets++;
  950. }
  951. }
  952. set_log_io_errors(1);
  953. if (!keep_running)
  954. break;
  955. if (readen < 0) {
  956. ts_LOGf("--- | Input read timeout.\n");
  957. if (!ntimeouts) {
  958. timeout_start = time(NULL);
  959. notify(&ts, "INPUT_TIMEOUT", "Read timeout on input %s://%s:%s/",
  960. ts.rtp_input ? "rtp" : "udp",
  961. ts.input.hostname, ts.input.service);
  962. }
  963. ntimeouts++;
  964. } else {
  965. if (ntimeouts && readen > 0) {
  966. notify(&ts, "INPUT_OK", "Data is available on input %s://%s:%s/ after %ld seconds timeout.",
  967. ts.rtp_input ? "rtp" : "udp",
  968. ts.input.hostname, ts.input.service,
  969. (time(NULL) - timeout_start) + 2); // Timeout is detected when ~2 seconds there is no incoming data
  970. ntimeouts = 0;
  971. }
  972. }
  973. } else {
  974. readen = read(ts.input.fd, ts_packet, FRAME_SIZE);
  975. have_data = !(readen <= 0);
  976. }
  977. if (readen > 0) {
  978. if (ts.input_dump_file)
  979. fwrite(ts_packet, readen, 1, ts.input_dump_file);
  980. process_packets(&ts, ts_packet, readen);
  981. }
  982. } while (have_data && keep_running);
  983. EXIT:
  984. camd_stop(&ts);
  985. // If pthread_join failes make sure we exit...
  986. signal(SIGINT , SIG_DFL);
  987. signal(SIGTERM, SIG_DFL);
  988. signal(SIGALRM, signal_alarm);
  989. alarm(2);
  990. if (ts.threaded) {
  991. ts.decode_stop = 1;
  992. ts.write_stop = 1;
  993. if (ts.decode_thread)
  994. pthread_join(ts.decode_thread, NULL);
  995. if (ts.write_thread)
  996. pthread_join(ts.write_thread, NULL);
  997. }
  998. show_pid_report(&ts);
  999. notify_sync(&ts, "STOP", "Stopping %s", program_id);
  1000. ts_LOGf("Stop %s\n", program_id);
  1001. if (ts.syslog_active) {
  1002. if (ts.syslog_remote)
  1003. log_close();
  1004. else
  1005. closelog();
  1006. }
  1007. if (ts.input_dump_file)
  1008. fclose(ts.input_dump_file);
  1009. if (ts.pidfile)
  1010. unlink(ts.pidfile);
  1011. if (log_file)
  1012. fclose(log_file);
  1013. notify_free(&ts.notify);
  1014. data_free(&ts);
  1015. exit(EXIT_SUCCESS);
  1016. }