LIBSSDP
 All Classes Files Functions Variables Typedefs Macros
main.c
Go to the documentation of this file.
1 
7 /* ______ ____ __ __ ____ ____ ____
8  * /\ _ \/\ _`\ /\ \/\ \/\ _`\ /\ _`\ /\ _`\
9  * \ \ \L\ \ \ \L\ \\ \ \ \ \ \,\L\_\ \ \L\_\ \ \/\ \
10  * \ \ __ \ \ _ <'\ \ \ \ \/_\__ \\ \ _\L\ \ \ \ \
11  * \ \ \/\ \ \ \L\ \\ \ \_\ \/\ \L\ \ \ \L\ \ \ \_\ \
12  * \ \_\ \_\ \____/ \ \_____\ `\____\ \____/\ \____/
13  * \/_/\/_/\/___/ \/_____/\/_____/\/___/ \/___/
14  *
15  *
16  * [A]bused is [B]anks [U]ber-[S]exy-[E]dition [D]aemon
17  *
18  * Copyright(C) 2014-2017 by Andreas Bank, andreas.mikael.bank@gmail.com
19  */
20 
21 /*
22  * ████████╗ ██████╗ ██████╗ ██████╗
23  * ╚══██╔══╝██╔═══██╗██╔══██╗██╔═══██╗██╗
24  * ██║ ██║ ██║██║ ██║██║ ██║╚═╝
25  * ██║ ██║ ██║██║ ██║██║ ██║██╗
26  * ██║ ╚██████╔╝██████╔╝╚██████╔╝╚═╝
27  * ╚═╝ ╚═════╝ ╚═════╝ ╚═════╝
28  * 01010100 01001111 01000100 01001111 00111010
29  *
30  * - Write JSON converter
31  * - Fix IPv6!
32  */
33 
34 /* Uncomment the line below to enable simulating SSDP notifications */
35 //#define DEBUG_MSG___
36 
38 #define DEBUG_MSG_FREQ___ 0.5
39 
47 #define DEBUG_MSG_LOCATION_HEADER "http://127.0.0.1:80/udhisapi.xml"
48 
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 
54 #include <errno.h>
55 #include <signal.h>
56 
57 #include "configuration.h"
58 #include "common_definitions.h"
59 #include "daemon.h"
60 #include "log.h"
61 #include "ssdp_common.h"
62 #include "ssdp_listener.h"
63 #include "ssdp_prober.h"
64 #include "string_utils.h"
65 
68 
71 
74 
78 static void cleanup(void) {
79  ssdp_listener_close(&ssdp_listener);
80  ssdp_prober_close(&ssdp_prober);
81  PRINT_DEBUG("Cleaning up and exiting...\n");
82 }
83 
89 static void exit_sig(int param) {
90  ssdp_listener_stop(&ssdp_listener);
91 }
92 
100  if(conf->run_as_daemon &&
101  !(conf->run_as_server ||
102  (conf->listen_for_upnp_notif && conf->forward_address) ||
103  conf->scan_for_upnp_devices)) {
104  /* If missconfigured, stop and notify the user */
105 
106  PRINT_ERROR("Cannot start as daemon.\nUse -d in combination with "
107  "-S or -a.\n");
108  exit(EXIT_FAILURE);
109 
110  }
111  else if(conf->run_as_daemon) {
112  /* If run as a daemon */
113  daemonize();
114  }
115 
116  /* If set to listen for UPnP notifications then
117  fork() and live a separate life */
118  if (conf->listen_for_upnp_notif &&
119  ((conf->run_as_daemon && conf->forward_address) ||
120  conf->run_as_server)) {
121  if (fork() != 0) {
122  /* listen_for_upnp_notif went to the forked process,
123  so it is set to false in parent so it doesn't run twice'*/
125  }
126  else {
127  PRINT_DEBUG("Created a process for listening of UPnP notifications");
128  /* scan_for_upnp_devices is set to false
129  so it doesn't run in this child (if it will be run at all) */
131  }
132  }
133 
134  /* If set to scan for UPnP-enabled devices then
135  fork() and live a separate life */
136  if(conf->scan_for_upnp_devices &&
137  (conf->run_as_daemon || conf->run_as_server)) {
138  if (fork() != 0) {
139  /* scan_for_upnp_devices went to the forked process,
140  so it set to false in parent so it doesn't run twice */
142  }
143  else {
144  PRINT_DEBUG("Created a process for scanning UPnP devices");
145  /* listen_for_upnp_notif is set to false
146  so it doesn't run in this child (if it will be run at all) */
148  }
149  }
150 }
151 
152 int main(int argc, char **argv) {
153  int ret = 0;
154 
155  signal(SIGTERM, &exit_sig);
156  signal(SIGABRT, &exit_sig);
157  signal(SIGINT, &exit_sig);
158 
159  #ifdef DEBUG___
160  log_start_args(argc, argv);
161  PRINT_DEBUG("%sDevelopment color%s", ANSI_COLOR_PURPLE, ANSI_COLOR_RESET);
162  PRINT_DEBUG("%sDebug color%s", ANSI_COLOR_GRAY, ANSI_COLOR_RESET);
163  PRINT_DEBUG("%sWarning color%s", ANSI_COLOR_YELLOW, ANSI_COLOR_RESET);
164  PRINT_DEBUG("%sError color%s", ANSI_COLOR_RED, ANSI_COLOR_RESET);
165  #endif
166 
168  if (parse_args(argc, argv, &conf)) {
169  cleanup();
170  exit(EXIT_FAILURE);
171  }
172 
173  verify_running_states(&conf);
174 
175  if (conf.listen_for_upnp_notif) {
176  /* If set to listen for devices notifications then
177  start listening for notifications but never continue
178  to do any other work the parent should be doing */
179 
180  /* init socket */
181  if ((ret = ssdp_passive_listener_init(&ssdp_listener, &conf))) {
182  PRINT_ERROR("Could not create SSDP listener");
183  return ret;
184  }
185 
186  /* Display forwarding info */
187  print_forwarder(&conf, &ssdp_listener.forwarder);
188 
189  if (ssdp_listener_start(&ssdp_listener, &conf)) {
190  PRINT_ERROR("%s", strerror(errno));
191  return errno;
192  }
193 
194  } else if(conf.scan_for_upnp_devices) {
195  /* If set to scan for devices then
196  start scanning but never continue
197  to do any other work the parent should be doing */
198 
199  if ((ret = ssdp_prober_init(&ssdp_prober, &conf))) {
200  PRINT_ERROR("Could not create SSDP prober");
201  return ret;
202  }
203 
204  /* Display forwarding info */
205  print_forwarder(&conf, &ssdp_prober.forwarder);
206 
207  if (ssdp_prober_start(&ssdp_prober, &conf)) {
208  PRINT_ERROR("%s", strerror(errno));
209  }
210 
211  } else {
212  usage();
213  }
214 
215  cleanup();
216  exit(EXIT_SUCCESS);
217 }
218 
static void cleanup(void)
Definition: main.c:78
static ssdp_prober_s ssdp_prober
Definition: main.c:70
void set_default_configuration(configuration_s *conf)
Definition: configuration.c:18
void daemonize(void)
Definition: daemon.c:21
void usage(void)
Definition: configuration.c:44
int ssdp_prober_init(ssdp_prober_s *prober, configuration_s *conf)
Definition: ssdp_prober.c:42
int parse_args(int argc, char *const *argv, configuration_s *conf)
Definition: configuration.c:78
#define FALSE
#define ANSI_COLOR_YELLOW
Definition: log.h:28
void ssdp_listener_stop(ssdp_listener_s *listener)
void ssdp_listener_close(ssdp_listener_s *listener)
#define ANSI_COLOR_RED
Definition: log.h:30
void log_start_args(int argc, char **argv)
Definition: log.c:219
static void exit_sig(int param)
Definition: main.c:89
int ssdp_prober_start(ssdp_prober_s *prober, configuration_s *conf)
Definition: ssdp_prober.c:94
struct sockaddr_storage forwarder
Definition: ssdp_listener.h:20
static void verify_running_states(configuration_s *conf)
Definition: main.c:99
int ssdp_passive_listener_init(ssdp_listener_s *listener, configuration_s *conf)
static configuration_s conf
Definition: main.c:73
#define ANSI_COLOR_RESET
Definition: log.h:32
void print_forwarder(configuration_s *conf, struct sockaddr_storage *forwarder)
Definition: ssdp_common.c:14
#define ANSI_COLOR_GRAY
Definition: log.h:22
int ssdp_listener_start(ssdp_listener_s *listener, configuration_s *conf)
static ssdp_listener_s ssdp_listener
Definition: main.c:67
void ssdp_prober_close(ssdp_prober_s *prober)
Definition: ssdp_prober.c:85
#define ANSI_COLOR_PURPLE
Definition: log.h:20