Time connection_down_time;
} Monitor;
+typedef struct
+{
+ Monitor *monitor;
+ Time delay;
+ Time interval;
+ const char *command;
+ Time next;
+ pid_t child_pid;
+} Trigger;
+
typedef struct
{
const char *target_name;
int monitor_init(Monitor *);
void monitor_check(Monitor *, Time);
void capture_handler(uint8_t *, const struct pcap_pkthdr *, const uint8_t *);
+int trigger_init(Trigger *);
+void trigger_check(Trigger *, Time);
int get_inet_scope(uint32_t, const Address *);
int get_inet6_scope(const struct in6_addr *, const Address *);
int pinger_init(Pinger *);
int main(int argc, char **argv)
{
Monitor monitor;
+ Trigger trigger;
Pinger pinger;
int o;
char *endp;
Time stats_interval = 1800000000ULL;
- Time trigger_delay = 60000000ULL;
- Time trigger_interval = 600000000ULL;
- const char *trigger_cmd = NULL;
no_daemon = 0;
verbose = 0;
monitor.interface_name = NULL;
+ trigger.monitor = &monitor;
+ trigger.delay = 60000000ULL;
+ trigger.interval = 600000000ULL;
pinger.target_name = NULL;
/* Parse options */
stats_interval = strtoul(optarg, NULL, 10)*1000000;
break;
case 't':
- trigger_delay = strtoul(optarg, &endp, 10)*1000000;
+ trigger.delay = strtoul(optarg, &endp, 10)*1000000;
if(*endp==',')
- trigger_interval = strtoul(endp+1, NULL, 10)*1000000;
+ trigger.interval = strtoul(endp+1, NULL, 10)*1000000;
else
- trigger_interval = trigger_delay;
+ trigger.interval = trigger.delay;
break;
case 'c':
- trigger_cmd = optarg;
+ trigger.command = optarg;
break;
case 'i':
monitor.interface_name = strdup(optarg);
}
if(monitor.interface_name)
+ {
monitor_init(&monitor);
+ if(trigger.command)
+ trigger_init(&trigger);
+ }
+
if(pinger.target_name)
pinger_init(&pinger);
openlog("netmon", 0, LOG_LOCAL7);
Time next_stats = current_time()+stats_interval;
- Time next_trigger = trigger_delay;
- pid_t child_pid = 0;
while(1)
{
Time time = current_time();
{
monitor_check(&monitor, time);
- if(monitor.connection_status)
- next_trigger = trigger_delay;
- else if(time>=monitor.last_receive+next_trigger)
- {
- if(no_daemon)
- printf("Running %s\n", trigger_cmd);
- else
- syslog(LOG_INFO, "Running %s", trigger_cmd);
- child_pid = run_command(trigger_cmd);
- next_trigger += trigger_interval;
- }
+ if(trigger.command)
+ trigger_check(&trigger, time);
}
if(pinger.target_name)
next_stats += stats_interval;
}
}
-
- /* Reap any finished child process */
- if(child_pid)
- {
- if(waitpid(child_pid, NULL, WNOHANG)==child_pid)
- child_pid = 0;
- }
}
closelog();
monitor->last_transmit = timestamp;
}
+int trigger_init(Trigger *trigger)
+{
+ trigger->next = trigger->delay;
+ trigger->child_pid = 0;
+
+ return 0;
+}
+
+void trigger_check(Trigger *trigger, Time time)
+{
+ Monitor *monitor = trigger->monitor;
+
+ if(monitor->connection_status)
+ trigger->next = trigger->delay;
+ else if(time>=monitor->last_receive+trigger->next)
+ {
+ if(no_daemon)
+ printf("Running %s\n", trigger->command);
+ else
+ syslog(LOG_INFO, "Running %s", trigger->command);
+ trigger->child_pid = run_command(trigger->command);
+ trigger->next += trigger->interval;
+ }
+
+ /* Reap any finished child process */
+ if(trigger->child_pid)
+ {
+ if(waitpid(trigger->child_pid, NULL, WNOHANG)==trigger->child_pid)
+ trigger->child_pid = 0;
+ }
+}
+
int get_inet_scope(uint32_t addr, const Address *local_addr)
{
uint32_t diff = addr^((struct sockaddr_in *)&local_addr->address)->sin_addr.s_addr;