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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
|
static char *rcsId = "$Id: ibod.c,v 1.3 2001/02/15 10:31:37 datenwelt Exp $";
static char *rcsSymbol = "$Symbol$";
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <linux/isdn.h>
#include <syslog.h>
#include <errno.h>
#ifdef TCP_WRAPPERS
#include <arpa/inet.h>
#include <tcpd.h>
#endif
#include "ibod.h"
static int setattr(char *home);
static void reread(int sig);
static void pipehndl(int sig);
static void setinterval();
static void get_if_state();
static int bring_up_slave();
static int bring_down_slave();
static char *home;
static Conf cf;
static struct timeval timeout, tv_last, tv_up;
static int usageflags[ISDN_MAX_CHANNELS];
static char phone[ISDN_MAX_CHANNELS][20];
static Siobytes iobytes[ISDN_MAX_CHANNELS];
static unsigned long in_bytes_last, out_bytes_last;
static unsigned long in_bytes_per_sec, out_bytes_per_sec;
static unsigned long channels_last;
static int channels_now;
static int msgsock = -1; /* Communication socket descriptor */
static int limit;
int main(int argc, char *argv[])
{
fd_set readfds, /* writefds,*/ execptfds;
int s; /* Listening socket descriptor */
struct sockaddr_in server; /* Socket protocol descriptor */
char buf[MAX_MSG_LEN];
int max_fd, rc;
openlog("ibod", LOG_PID, LOG_DAEMON);
channels_last = -1;
/* Find out where to look for configuration file */
if ((home = getenv("IBOD_HOME")) == NULL)
home = IBOD_DEFAULT_DIR;
/* Setup initial attributes */
if (setattr(home) == -1) {
closelog();
exit(1);
}
setinterval();
/* Setup handlig of signal SIGHUP and SIGPIPE */
signal(SIGHUP, reread);
signal(SIGPIPE, pipehndl);
/* Create a socket for control panel communication */
if ((s = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
syslog(LOG_ERR, "creating socket: %s\n", sys_errlist[errno]);
closelog();
exit(1);
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(IBOD_PORT);
if (bind(s, (struct sockaddr *)&server, sizeof(struct sockaddr_in)) < 0) {
syslog(LOG_ERR, "binding stream socket: %s\n", sys_errlist[errno]);
closelog();
exit(1);
}
/* Start accepting connections. */
listen(s, 5);
FD_ZERO(&readfds);
FD_ZERO(&execptfds);
do {
setinterval();
FD_SET(s, &readfds);
FD_SET(s, &execptfds);
if (msgsock > 0) {
FD_SET(msgsock, &readfds);
FD_SET(msgsock, &execptfds);
}
max_fd = s;
if (msgsock > max_fd)
max_fd = msgsock;
if ((rc = select(max_fd + 1, &readfds, NULL, &execptfds, &timeout)) < 0) {
syslog(LOG_ERR, "select: %s\n", sys_errlist[errno]);
continue;
}
if (FD_ISSET(s, &readfds)) {
struct sockaddr_in client; /* Socket protocol descriptor */
int addrlen = sizeof(client);
msgsock = accept(s, (struct sockaddr *)&client, &addrlen);
if (msgsock == -1)
syslog(LOG_ERR, "accept: %s\n", sys_errlist[errno]);
#ifdef TCP_WRAPPERS
allow_severity=LOG_INFO;
deny_severity=LOG_WARNING;
if (hosts_ctl("ibod", "", inet_ntoa(client.sin_addr),"")==0) {
/* Access denied */
close(msgsock);
msgsock = -1;
syslog(deny_severity, "denied access from: %s\n",
inet_ntoa(client.sin_addr));
}
#endif
}
if (msgsock != -1 && FD_ISSET(msgsock, &readfds)) {
int rval;
memset(buf, 0, sizeof buf);
if ((rval = read(msgsock, buf, 1024)) < 0)
syslog(LOG_ERR, "reading stream message: %s\n",
sys_errlist[errno]);
else {
int cmd = *(int *)buf;
switch (cmd) {
case CMD_OPEN:
break;
case CMD_CLOSE:
close(msgsock);
msgsock = -1;
break;
case CMD_UP2:
if (channels_now == 1) {
if (bring_up_slave() == -1)
exit(1);
}
break;
case CMD_DOWN2:
if (channels_now == 2) {
if (bring_down_slave() == -1)
exit(1);
}
break;
case CMD_ENABLE:
cf.enable = 1;
break;
case CMD_DISABLE:
if (channels_now == 2) {
if (bring_down_slave() == -1)
exit(1);
}
cf.enable = 0;
}
}
}
if (FD_ISSET(s, &execptfds)) {
perror("Exception on s");
}
if (msgsock != -1 && FD_ISSET(msgsock, &execptfds)) {
perror("Exception on msgsock");
FD_SET(msgsock, &execptfds);
}
if (1) {
/* Gate state of interface */
get_if_state();
if (msgsock > 0) {
memcpy(&buf[LINK_STATUS_POS], &channels_now, POBJ_SIZE);
memcpy(&buf[INBOUND_RATE_POS], &in_bytes_per_sec, POBJ_SIZE);
memcpy(&buf[OUTBOUND_RATE_POS], &out_bytes_per_sec, POBJ_SIZE);
if (write(msgsock, buf, STATUS_FRAME_SIZE)==-1) {
/* error on write, close socket */
close(msgsock);
msgsock = -1;
syslog(LOG_ERR, "writing stream message: %s\n",
sys_errlist[errno]);
}
}
}
} while (1);
}
static int setattr(char *home)
{
FILE *fd;
char config_filename[MAX_STR_LEN];
char linebuf[MAX_STR_LEN];
char *key, *value;
int val;
strcpy(cf.dev, DEVICE);
cf.enable = ENABLE;
cf.interval = INTERVAL;
cf.filter = FILTER;
cf.limit = LIMIT;
cf.stayup = STAYUP;
cf.stayup_time = STAYUP_TIME;
cf.max_channels = MAX_CHANNELS;
/* Open config file */
sprintf(config_filename, "%s/ibod.cf", home);
if ((fd = fopen(config_filename, "r")) == NULL) {
syslog(LOG_ERR, "%s: %s\n", config_filename, strerror(errno));
return -1;
}
/* Loop over the config file to setup attributes */
while (fgets(linebuf, MAX_STR_LEN, fd) != NULL) {
if (*linebuf == '#') /* Ignore comments */
continue;
key = strtok(linebuf, " \t");
value = strtok(NULL, " \t\n");
if (strcmp(key, "DEVICE") == 0) {
if (strcmp(cf.dev, value) != 0)
syslog(LOG_NOTICE,
"Parameter DEVICE reconfigured to %s\n", value);
strcpy(cf.dev, value);
}
if (strcmp(key, "ENABLE") == 0) {
val = atoi(value);
if (cf.enable != val)
syslog(LOG_NOTICE,
"Parameter ENABLE reconfigured to %d\n", val);
cf.enable = val;
}
if (strcmp(key, "INTERVAL") == 0) {
val = atoi(value);
if (cf.interval != val)
syslog(LOG_NOTICE,
"Parameter INTERVAL reconfigured to %d\n", val);
cf.interval = atoi(value);
}
if (strcmp(key, "FILTER") == 0) {
val = atoi(value);
if (cf.filter != val)
syslog(LOG_NOTICE,
"Parameter FILTER reconfigured to %d\n", val);
cf.filter = atoi(value);
}
if (strcmp(key, "LIMIT") == 0) {
val = atoi(value);
if (cf.limit != val)
syslog(LOG_NOTICE,
"Parameter LIMIT reconfigured to %d\n", val);
cf.limit = atoi(value);
}
if (strcmp(key, "STAYUP") == 0) {
val = atoi(value);
if (cf.stayup != val)
syslog(LOG_NOTICE,
"Parameter STAYUP reconfigured to %d\n", val);
cf.stayup = atoi(value);
}
if (strcmp(key, "STAYUP_TIME") == 0) {
val = atoi(value);
if (cf.stayup_time != val)
syslog(LOG_NOTICE,
"Parameter STAYUP_TIME reconfigured to %d\n", val);
cf.stayup_time = atoi(value);
}
if (strcmp(key, "MAX_CHANNELS") == 0) {
val = atoi(value);
if (cf.max_channels != val)
syslog(LOG_NOTICE,
"Parameter MAX_CHANNELS reconfigured to %d\n", val);
cf.max_channels = atoi(value);
}
}
fclose(fd);
return 0;
}
static void setinterval()
{
timeout.tv_sec = cf.interval / 1000;
timeout.tv_usec = (cf.interval % 1000) * 1000;
}
static void reread(int sig)
{
(void) setattr(home);
setinterval();
signal(SIGHUP, reread);
}
static void pipehndl(int sig)
{
syslog(LOG_ERR, "caught SIGPIPE: %s\n", sys_errlist[errno]);
close(msgsock);
msgsock = -1;
signal(SIGPIPE, pipehndl);
}
static void get_if_state()
{
static char buf[4096];
struct timeval tv_now;
int ms_delta;
int in_bytes_now, out_bytes_now;
int fd;
int i;
/* Open the info device */
if ((fd = open(ISDN_INFO_DEV, O_RDONLY | O_NDELAY)) < 0) {
syslog(LOG_ERR, "%s: %s\n", ISDN_INFO_DEV, sys_errlist[errno]);
closelog();
exit(1);
}
/* Whats the time now */
gettimeofday(&tv_now, NULL);
ms_delta = (tv_now.tv_sec * 1000 + tv_now.tv_usec / 1000) -
(tv_last.tv_sec * 1000 + tv_last.tv_usec / 1000);
tv_last = tv_now;
/* Get info from interface */
if (read(fd, buf, sizeof(buf))> 0) {
sscanf(strstr(buf, "usage:"),
"usage: %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d",
&usageflags[0], &usageflags[1], &usageflags[2], &usageflags[3],
&usageflags[4], &usageflags[5], &usageflags[6], &usageflags[7],
&usageflags[8], &usageflags[9], &usageflags[10], &usageflags[11],
&usageflags[12], &usageflags[13], &usageflags[14], &usageflags[15]);
sscanf(strstr(buf, "phone:"),
"phone: %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s",
phone[0], phone[1], phone[2], phone[3],
phone[4], phone[5], phone[6], phone[7],
phone[8], phone[8], phone[10], phone[11],
phone[12], phone[13], phone[14], phone[15]);
}
in_bytes_now = 0;
out_bytes_now = 0;
channels_now = 0;
/* Get byte in/out for all channels */
if (ioctl(fd, IIOCGETCPS, &iobytes)) {
syslog(LOG_ERR, "%s: %s\n", "IIOCGETCPS", sys_errlist[errno]);
closelog();
exit(1);
}
close(fd);
/* Count number of open channes and total in/out bytes */
for (i = 0; i < cf.max_channels; i++) {
if (usageflags[i]) {
channels_now++;
in_bytes_now += iobytes[i].ibytes;
out_bytes_now += iobytes[i].obytes;
}
}
if (channels_last == -1 || channels_now < channels_last) {
channels_last = channels_now;
in_bytes_last = in_bytes_now;
out_bytes_last = out_bytes_now;
return;
}
/* Calculate the total through put in bytes/sec */
if (cf.filter < 1) {
in_bytes_per_sec =
(in_bytes_now - in_bytes_last) * 1000 / ms_delta;
out_bytes_per_sec =
(out_bytes_now - out_bytes_last) * 1000 / ms_delta;
}
else {
in_bytes_per_sec = (in_bytes_per_sec * (cf.filter - 1) +
(in_bytes_now - in_bytes_last) * 1000 / ms_delta) / cf.filter;
out_bytes_per_sec = (out_bytes_per_sec * (cf.filter - 1) +
(out_bytes_now - out_bytes_last) * 1000 / ms_delta) / cf.filter;
}
in_bytes_last = in_bytes_now;
out_bytes_last = out_bytes_now;
if (channels_now == 0) {
channels_last = channels_now;
return;
}
/* Take up or down slave channel */
if (cf.enable == 0) {
channels_last = channels_now;
return;
}
limit = (8 * 1024 * channels_now * cf.limit) / 100;
#ifdef DEBUG
syslog(LOG_NOTICE,"Set limit to %d bps - current in: %ld bps, current out: %ld bps\n",
limit, in_bytes_per_sec, out_bytes_per_sec);
#endif
if (channels_now < cf.max_channels &&
(in_bytes_per_sec > limit || out_bytes_per_sec > limit)) {
/* Bring up slave interface */
if (bring_up_slave() == -1)
exit(1);
/* Start stay up timer */
gettimeofday(&tv_up, NULL);
}
if ((channels_now > 1) &&
(in_bytes_per_sec <= limit) &&
(out_bytes_per_sec <= limit) &&
(cf.stayup == 0)) {
/* Check that the min stay up timer has expired */
gettimeofday(&tv_now, NULL);
if (tv_now.tv_sec - tv_up.tv_sec > cf.stayup_time) {
/* Bring down slave interface */
if (bring_down_slave() == -1)
exit(1);
}
}
channels_last = channels_now;
}
static int bring_up_slave()
{
int fd, rc;
if ((fd = open(ISDN_CTLR_DEV, O_RDWR)) < 0) {
syslog(LOG_ERR, "%s: %s\n", ISDN_CTLR_DEV, sys_errlist[errno]);
closelog();
return -1;
}
if ((rc = ioctl(fd, IIOCNETALN, cf.dev)) < 0) {
syslog(LOG_ERR, "%s: %s\n", cf.dev, sys_errlist[errno]);
closelog();
return -1;
}
close(fd);
if (rc)
syslog(LOG_NOTICE, "unable to attach additional link: %d\n", rc);
else {
channels_now++;
syslog(LOG_NOTICE, "added new link: %d\n", channels_now);
}
return 0;
}
static int bring_down_slave()
{
int fd, rc;
if ((fd = open(ISDN_CTLR_DEV, O_RDWR)) < 0) {
syslog(LOG_ERR, "%s: %s\n", ISDN_CTLR_DEV, sys_errlist[errno]);
closelog();
return -1;
}
if ((rc = ioctl(fd, IIOCNETDLN, cf.dev)) < 0) {
syslog(LOG_ERR, "%s: %s\n", cf.dev, sys_errlist[errno]);
closelog();
return -1;
}
close(fd);
if (rc)
syslog(LOG_ERR, "unable to remove additional link: %d\n", rc);
else {
syslog(LOG_NOTICE, "removed link: %d\n", channels_now);
}
return 0;
}
|