This repository has been archived by the owner on Oct 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
device.cpp
191 lines (167 loc) · 5.07 KB
/
device.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* lapdog - take actions on devices (dis)appearance on the LAN
* Copyright (C) 2014-2018 Salvo Tomaselli <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sys/types.h>
#include <fstream>
#include <iostream>
#include <pwd.h>
#include <sstream>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <syslog.h>
#include "advsystem.h"
#include "device.h"
#include "split.h"
using namespace std;
/**
* Creates a new device object starting
* from a configuration file.
**/
device::device(const char* conffile, const char* name) {
this->name = string(name);
this->count=0;
this->max_misses = 10;
this->uid = 0;
this->gid = 0;
this->gateway = false;
this->config = configuration::getconfig();
std::filebuf fb;
if (not fb.open(conffile,std::ios::in)) {
//TODO failed
}
std::istream is(&fb);
vector<string> lines = split(is,'\n',0);
for (size_t i=0; i<lines.size(); i++) {
std::string line = lines[i];
if (line[0] == '#')
continue;
else if (line.size() == 0)
continue;
vector<string> key_value = split(line,'=',2);
string key = key_value[0];
string value = key_value[1];
if (key=="hw") {
this->hw_addr.set_hwaddr(value);
} else if (key=="on_appear_action") {
this->on_appear_action = value;
} else if (key=="on_disappear_action") {
this->on_disappear_action= value;
} else if (key=="initial_count") {
this->count = strtol(value.c_str(),NULL,10);
} else if (key=="max_misses") {
this->max_misses = strtol(value.c_str(),NULL,10);
} else if (key=="uid") {
this->uid = strtol(value.c_str(),NULL,10);
} else if (key=="gid") {
this->gid = strtol(value.c_str(),NULL,10);
} else if (key=="user") {
struct passwd * p = getpwnam(value.c_str());
if (p==NULL) {
syslog(LOG_ERR,"Can't find uid for user %s", value.c_str());
exit(1);
}
this->uid = p->pw_uid;
this->gid = p->pw_gid;
} else if (key=="gateway") {
this->gateway = value == "true";
} else {
syslog(LOG_ERR, "Unrecognized key \"%s\" in configuration file" , key.c_str());
exit(1);
}
}
//check that hw_addr was set
if (!this->hw_addr.is_set()) {
syslog(LOG_ERR,"hw key missing");
exit(1);
}
}
bool device::is_gateway() {
return this->gateway;
}
/**
* Return the hw_address associated
**/
const hwaddr device::get_hw_addr() {
return this->hw_addr;
}
/**
* The device just missed a ping
**/
void device::missed() {
if (this->count == this->max_misses) {
syslog(LOG_INFO, "%s (%s) disappeared, running action", name.c_str(), hw_addr.strrepr().c_str());
advsystem(uid, gid, on_disappear_action.c_str());
this->count ++;
} else if (this->count < this->max_misses) {
this->count ++;
}
}
/**
* The device responded to a ping
**/
void device::responded() {
if (this->count == this->max_misses+1) {
syslog(LOG_INFO, "%s (%s) appeared, running action", name.c_str(), hw_addr.strrepr().c_str());
advsystem(uid, gid, on_appear_action.c_str());
}
this->count = 0;
}
/**
* Returns true if the device is present.
*
* It uses the latest available data, will not
* generate new pings.
**/
bool device::present() {
return this->count < this->max_misses;
}
/**
* Returns the name assigned to the device in the configuration
*
* The name is assigned by the name of the file
**/
string device::device_name() {
return this->name;
}
/**
* Prints the status on the given file descriptor
**/
void device::dump(int fd) {
dprintf(fd,
"%s\t%s\t%s\tmissed: %d\tmax misses: %d\t%c\tARP entry: %s\n",
this->name.c_str(),
this->hw_addr.strrepr().c_str(),
this->present() ? "present" : "absent",
this->count,
this->max_misses,
this->gateway ? 'G' : 'D',
this->is_in_arp_table ? "yes": "no"
);
}
/**
* @brief Marks the device as being present or not in the ARP table
*
* Some devices do not repspond to ping, but their presence in the ARP
* table indicates that they are online.
*
* This information is only used for the status.
* @param value
*/
void device::in_arp(bool value) {
this->is_in_arp_table = value;
}