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.

notify.c 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Exec external program to notify for an event
  3. * Copyright (C) 2011 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. // Needed for asprintf
  16. #define _GNU_SOURCE 1
  17. #include <stdlib.h>
  18. #include <stdarg.h>
  19. #include <unistd.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <ctype.h>
  23. #include <pthread.h>
  24. #include <sys/types.h>
  25. #include <sys/wait.h>
  26. #include <sys/mman.h>
  27. #include "libfuncs/queue.h"
  28. #include "notify.h"
  29. #include "util.h"
  30. struct npriv {
  31. char ident[512];
  32. char program[512];
  33. char msg_id[512];
  34. char text[512];
  35. char input[128];
  36. char output[128];
  37. int sync; /* Wait for message to be delivered */
  38. };
  39. static void *do_notify(void *in) {
  40. struct npriv *data = in;
  41. struct npriv *shared = mmap(NULL, sizeof(struct npriv), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
  42. if (!shared) {
  43. perror("mmap");
  44. goto OUT;
  45. }
  46. *shared = *data;
  47. pid_t pid = fork();
  48. if (pid==0) { // child process
  49. char *args[] = { shared->program, shared->ident, NULL };
  50. int e = 0;
  51. unsigned int i, r;
  52. char **env = calloc(32, sizeof(char *));
  53. if (asprintf(&env[e++], "_TS=%ld" , time(NULL)) < 0) exit(EXIT_FAILURE);
  54. if (asprintf(&env[e++], "_IDENT=%s" , shared->ident) < 0) exit(EXIT_FAILURE);
  55. if (asprintf(&env[e++], "_INPUT_ADDR=%s" , shared->input) < 0) exit(EXIT_FAILURE);
  56. if (asprintf(&env[e++], "_OUTPUT_ADDR=%s" , shared->output) < 0) exit(EXIT_FAILURE);
  57. if (asprintf(&env[e++], "_MESSAGE_ID=%s" , shared->msg_id) < 0) exit(EXIT_FAILURE);
  58. if (asprintf(&env[e++], "_MESSAGE_TEXT=%s" , shared->text) < 0) exit(EXIT_FAILURE);
  59. r = strlen(shared->msg_id);
  60. for (i=0; i<r; i++) {
  61. if (isalpha(shared->msg_id[i]))
  62. shared->msg_id[i] = tolower(shared->msg_id[i]);
  63. if (shared->msg_id[i] == '_')
  64. shared->msg_id[i] = ' ';
  65. }
  66. if (asprintf(&env[e++], "_MESSAGE_MSG=%s" , shared->msg_id) < 0) exit(EXIT_FAILURE);
  67. execve(args[0], args, env);
  68. // We reach here only if there is an error.
  69. fprintf(stderr, "execve('%s') failed: %s!\n", args[0], strerror(errno));
  70. do {
  71. free(env[e--]);
  72. } while (e);
  73. free(env);
  74. exit(EXIT_FAILURE);
  75. } else if (pid < 0) {
  76. fprintf(stderr, "fork() failed: %s\n", strerror(errno));
  77. } else {
  78. waitpid(pid, NULL, 0);
  79. }
  80. munmap(shared, sizeof(struct npriv));
  81. OUT:
  82. free(data);
  83. pthread_exit(EXIT_SUCCESS);
  84. }
  85. static void *notify_thread(void *data) {
  86. struct notify *n = data;
  87. set_thread_name("tsdec-notify");
  88. while (1) {
  89. struct npriv *np = queue_get(n->notifications); // Waits...
  90. if (!np)
  91. break;
  92. pthread_t notifier; // The notifier frees the data
  93. if (pthread_create(&notifier, NULL, &do_notify, np) != 0) {
  94. perror("pthread_create");
  95. free(np);
  96. } else {
  97. if (np->sync)
  98. pthread_join(notifier, NULL);
  99. else
  100. pthread_detach(notifier);
  101. }
  102. }
  103. pthread_exit(EXIT_SUCCESS);
  104. }
  105. /* ======================================================================== */
  106. struct notify *notify_alloc(struct ts *ts) {
  107. unsigned int i;
  108. if (!ts->ident || !ts->notify_program)
  109. return NULL;
  110. struct notify *n = calloc(1, sizeof(struct notify));
  111. n->notifications = queue_new();
  112. strncpy(n->ident, ts->ident, sizeof(n->ident) - 1);
  113. n->ident[sizeof(n->ident) - 1] = '\0';
  114. for (i=0; i<strlen(n->ident); i++) {
  115. if (n->ident[i] == '/')
  116. n->ident[i] = '-';
  117. }
  118. strncpy(n->program, ts->notify_program, sizeof(n->program) - 1);
  119. n->program[sizeof(n->program) - 1] = '\0';
  120. pthread_create(&n->thread, &ts->thread_attr , &notify_thread, n);
  121. return n;
  122. }
  123. static void npriv_init_defaults(struct notify *n, struct npriv *np) {
  124. strncpy(np->program, n->program, sizeof(np->program) - 1);
  125. strncpy(np->ident, n->ident, sizeof(np->ident) - 1);
  126. }
  127. static void notify_func(struct ts *ts, int sync_msg, char *msg_id, char *msg_text) {
  128. struct npriv *np;
  129. if (!ts->notify)
  130. return;
  131. np = calloc(1, sizeof(struct npriv));
  132. np->sync = sync_msg;
  133. if (ts->notify_wait)
  134. np->sync = 1;
  135. npriv_init_defaults(ts->notify, np);
  136. strncpy(np->msg_id, msg_id, sizeof(np->ident) - 1);
  137. np->msg_id[sizeof(np->ident) - 1] = 0;
  138. strncpy(np->text, msg_text, sizeof(np->text) - 1);
  139. np->text[sizeof(np->text) - 1] = 0;
  140. if (ts->input.type == NET_IO) {
  141. snprintf(np->input, sizeof(np->input), "%s:%s", ts->input.hostname, ts->input.service);
  142. } else if (ts->input.type == FILE_IO) {
  143. snprintf(np->input, sizeof(np->input), "%s", ts->input.fd == 0 ? "STDIN" : "FILE");
  144. }
  145. if (ts->output_stream) {
  146. if (ts->output.type == NET_IO) {
  147. snprintf(np->output, sizeof(np->output), "%s:%s", ts->output.hostname, ts->output.service);
  148. } else if (ts->output.type == FILE_IO) {
  149. snprintf(np->output, sizeof(np->output), "%s", ts->output.fd == 1 ? "STDOUT" : "FILE");
  150. }
  151. } else {
  152. snprintf(np->output, sizeof(np->output), "DISABLED");
  153. }
  154. queue_add(ts->notify->notifications, np);
  155. }
  156. #define MAX_MSG_TEXT 256
  157. void notify(struct ts *ts, char *msg_id, char *text_fmt, ...) {
  158. va_list args;
  159. char msg_text[MAX_MSG_TEXT];
  160. va_start(args, text_fmt);
  161. vsnprintf(msg_text, sizeof(msg_text) - 1, text_fmt, args);
  162. msg_text[sizeof(msg_text) - 1] = 0;
  163. va_end(args);
  164. notify_func(ts, 0, msg_id, msg_text);
  165. }
  166. void notify_sync(struct ts *ts, char *msg_id, char *text_fmt, ...) {
  167. va_list args;
  168. char msg_text[MAX_MSG_TEXT];
  169. va_start(args, text_fmt);
  170. vsnprintf(msg_text, sizeof(msg_text) - 1, text_fmt, args);
  171. msg_text[sizeof(msg_text) - 1] = 0;
  172. va_end(args);
  173. notify_func(ts, 1, msg_id, msg_text);
  174. }
  175. void notify_free(struct notify **pn) {
  176. struct notify *n = *pn;
  177. if (n) {
  178. queue_add(n->notifications, NULL);
  179. pthread_join(n->thread, NULL);
  180. queue_free(&n->notifications);
  181. FREE(*pn);
  182. }
  183. }