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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
|
/*
* common.c: EPG2VDR plugin for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
*/
#include <sys/time.h>
#include <sys/param.h>
#include <netinet/in_systm.h>
#include <netinet/ip_icmp.h>
#include <netdb.h>
#include <stdarg.h>
#include <string.h>
#include <syslog.h>
#include <vdr/thread.h>
#include "common.h"
#include "config.h"
cMutex logMutex;
//***************************************************************************
// Tell
//***************************************************************************
void tell(int eloquence, const char* format, ...)
{
if (cfg.loglevel < eloquence)
return ;
const int sizeBuffer = 100000;
char t[sizeBuffer+100]; *t = 0;
va_list ap;
cMutexLock lock(&logMutex);
va_start(ap, format);
snprintf(t, sizeBuffer, "SEDUATMO: ");
vsnprintf(t+strlen(t), sizeBuffer-strlen(t), format, ap);
syslog(LOG_ERR, "%s", t);
va_end(ap);
}
//***************************************************************************
// Error
//***************************************************************************
int error(const char* format, ...)
{
const int sizeBuffer = 100000;
char t[sizeBuffer+100]; *t = 0;
va_list ap;
cMutexLock lock(&logMutex);
va_start(ap, format);
snprintf(t, sizeBuffer, "SEDUATMO: ");
vsnprintf(t+strlen(t), sizeBuffer-strlen(t), format, ap);
syslog(LOG_ERR, "%s", t);
va_end(ap);
return fail;
}
//***************************************************************************
// is Empty
//***************************************************************************
int isEmpty(const char* str)
{
return !str || !*str;
}
//***************************************************************************
// msNow
//***************************************************************************
MsTime msNow()
{
timeval tv;
gettimeofday(&tv, 0);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
//***************************************************************************
// Misc. Functions
//***************************************************************************
int minMax(int x, int min, int max)
{
if (x < min)
return min;
if (max < x)
return max;
return x;
}
double min(double a, double b)
{
return a < b ? a : b;
}
double max(double a, double b)
{
return a >= b ? a : b;
}
int getrand(int min, int max)
{
srand(time(0));
return rand() % (max-min) + min;
}
//***************************************************************************
// Is Alive (fork ping therefore no root permissions needed)
//***************************************************************************
int isAlive(const char* address)
{
return ping(address) == success;
}
uint16_t cksum(uint16_t *addr, unsigned len)
{
uint16_t answer = 0;
uint32_t sum = 0;
while (len > 1)
{
sum += *addr++;
len -= 2;
}
if (len == 1)
{
*(unsigned char *)&answer = *(unsigned char *)addr ;
sum += answer;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
answer = ~sum;
return answer;
}
int ping(const char* target)
{
const size_t DEFDATALEN = (64-ICMP_MINLEN);
const size_t MAXIPLEN = 60;
const size_t MAXICMPLEN = 76;
const size_t MAXPACKET = (65536 - 60 - ICMP_MINLEN);
int i, cc, packlen, datalen = DEFDATALEN;
struct hostent *hp;
struct sockaddr_in to, from;
u_char *packet, outpack[MAXPACKET];
char hnamebuf[MAXHOSTNAMELEN];
std::string hostname;
struct icmp *icp;
int ret, fromlen, hlen;
fd_set rfds;
struct timeval tv;
to.sin_family = AF_INET;
to.sin_addr.s_addr = inet_addr(target);
if (to.sin_addr.s_addr != (u_int)-1)
hostname = target;
else
{
if (!(hp = gethostbyname(target)))
{
tell(0, "unknown host '%s'", target);
return fail;
}
to.sin_family = hp->h_addrtype;
bcopy(hp->h_addr, (caddr_t)&to.sin_addr, hp->h_length);
strncpy(hnamebuf, hp->h_name, sizeof(hnamebuf) - 1);
hostname = hnamebuf;
}
packlen = datalen + MAXIPLEN + MAXICMPLEN;
packet = (u_char*)malloc((u_int)packlen);
int sd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (sd < 0)
{
tell(0, "Error: sokett failed due to '%s'", strerror(errno));
return fail; // Needs to run as root ore set rights "setcap cap_net_raw+ep /usr/bin/vdr"
}
icp = (struct icmp*)outpack;
icp->icmp_type = ICMP_ECHO;
icp->icmp_code = 0;
icp->icmp_cksum = 0;
icp->icmp_seq = 12345;
icp->icmp_id = getpid();
cc = datalen + ICMP_MINLEN;
icp->icmp_cksum = cksum((unsigned short *)icp,cc);
i = sendto(sd, (char *)outpack, cc, 0, (struct sockaddr*)&to, (socklen_t)sizeof(struct sockaddr_in));
if (i < 0)
tell(0, "Error: Sendto '%s'", strerror(errno));
FD_ZERO(&rfds);
FD_SET(sd, &rfds);
tv.tv_sec = 0;
tv.tv_usec = 50000;
while (true)
{
int retval = select(sd+1, &rfds, NULL, NULL, &tv);
if (retval <= 0)
{
if (retval == -1)
tell(0, "Error: select() '%s'", strerror(errno));
close(sd);
return fail;
}
fromlen = sizeof(sockaddr_in);
if ((ret = recvfrom(sd, (char*)packet, packlen, 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) < 0)
{
tell(0, "Error: recvfrom '%s'", strerror(errno));
close(sd);
return fail;
}
// Check the IP header
hlen = sizeof(struct ip);
if (ret < (hlen + ICMP_MINLEN))
{
tell(0, "Packet too short (%d) bytes from '%s'", ret, hostname.c_str());
close(sd);
return fail;
}
// Now the ICMP part
icp = (struct icmp*)(packet + hlen);
if (icp->icmp_type == ICMP_ECHOREPLY)
{
if (icp->icmp_seq == 12345 && icp->icmp_id == getpid())
{
close(sd);
return success;
}
}
}
close(sd);
return fail;
}
|