📄 locupd2.c
字号:
/* $Id: locupd2.c,v 1.7 2001/09/08 14:29:40 jm Exp $ * Automatic location updates between two FAs using MN API * * Dynamic hierarchial IP tunnel * Copyright (C) 1998-2001, Dynamics group * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. See README and COPYING for * more details. *//* locupd2 can set the firewall entry to block traffic from a host if the * registration did not succeed in given time. Uncomment next line and edit * Makefile.am to compile this support. You will also need couple of files * from the ipchains package (libipfwc.h, ipfwc_kernel_headers.h, libipfwc.a) *//* #define FIREWALL */#include <stdio.h>#include <stdlib.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>#include <sys/time.h>#include <string.h>#include <errno.h>#include <ctype.h>#include <signal.h>#ifdef FIREWALL/* address to be blocked when the registration does not succeed */#define CN_ADDRESS "192.168.10.10"#include "asm/types.h"#include "libipfwc.h"#endif#include "dyn_mnlib.h"#include "util.h"#define DEFAULT_API_PATH "/var/run/dynamics_mn_admin"#define TIMEOUT 1#define MAX_FA 16FILE *f;unsigned long updates = 0;static int timerdiff(struct timeval *a, struct timeval *b){ return ((b->tv_sec - a->tv_sec) * 1000000 + b->tv_usec - a->tv_usec);}static void clean_up(int sig){ fprintf(f, "signal %i received - exiting\n", sig); fprintf(f, "%lu location updates\n", updates); fclose(f); exit(0);}static void locupd(int period, int fa_count, struct in_addr fa[]){ struct timeval goal, now; int diff, ret, i, pos;#ifdef FIREWALL int firewall = 0; struct ip_fwuser fw; struct in_addr addr; inet_aton(CN_ADDRESS, &addr); memset(&fw, 0, sizeof(fw)); dynamics_strlcpy(fw.label, "DENY", sizeof(fw.label)); fw.ipfw.fw_spts[1] = 65535; fw.ipfw.fw_dpts[1] = 65535; fw.ipfw.fw_tosand = 0xff; memcpy(&fw.ipfw.fw_src, &addr, 4); memset(&fw.ipfw.fw_smsk, 0xff, 4);#endif fprintf(f, "Location update every %i usec between FAs: ", period); for (i = 0; i < fa_count; i++) fprintf(f, "%s ", inet_ntoa(fa[i])); fprintf(f, "\n"); gettimeofday(&goal, NULL); pos = 0; updates = 0; for (;;) { ret = dynamics_mn_force_fa(&fa[pos], TIMEOUT); if (ret != API_SUCCESS) { fprintf(f, "dynamics_mn_force_fa: %i - %s\n", ret, dynamics_mn_get_error_string(ret)); } ret = dynamics_mn_update_location_dev_block(NULL, TIMEOUT); if (ret != API_SUCCESS) { fprintf(f, "dynamics_mn_update_location_dev_block: " "%i - %s\n", ret, dynamics_mn_get_error_string(ret)); } else updates++; goal.tv_usec += period; if (goal.tv_usec > 999999) { goal.tv_sec += goal.tv_usec / 1000000; goal.tv_usec %= 1000000; } gettimeofday(&now, NULL); diff = timerdiff(&now, &goal); if (diff < 0) { fprintf(f, "Locupd => %s did not succeed in given " "time period (%i usec).\n", inet_ntoa(fa[pos]), diff); gettimeofday(&goal, NULL);#ifdef FIREWALL if (!firewall) { fprintf(f, "\tadding firewall\n"); if (!ipfwc_append_entry("input", &fw)) fprintf(f, "ipfwc_append_entry failed" " - %s\n", ipfwc_strerror(errno)); firewall = 1; }#endif } else {#ifdef FIREWALL if (firewall) { fprintf(f, "\removing firewall\n"); if (!ipfwc_delete_entry("input", &fw)) fprintf(f, "ipfwc_delete_entry " "failed - %s\n", ipfwc_strerror(errno)); firewall = 0; }#endif /* printf("Sleeping %i usec\n", diff); */ usleep(diff); } pos++; if (pos >= fa_count) pos = 0; }}int main(int argc, char *argv[]){ int ret, period, i, path = 0, count = 0; struct in_addr fa[MAX_FA]; printf("Dynamics - Automatic location updates between two FAs using " "MN API\n\n"); if (argc < 4) { fprintf(stderr, "locupd2 <locupd period (usec)> <FA1 addr> <FA2 addr>" " [FA3 .. FAn] [API admin file name]\n" "e.g. locupd2 1000000 192.168.0.1 192.168.0.2\n" " locupd2 500000 192.168.0.1 192.168.0.2 " "/var/run/dynamics_mn_admin\n"); return -1; } if (argc - 1 > MAX_FA) { fprintf(stderr, "Too many FAs\n"); exit(1); } period = atoi(argv[1]); for (i = 2; i < argc; i++) { if (inet_aton(argv[i], &fa[i-2]) == 0) { if (i == argc - 1) { printf("Using admin file '%s'\n", argv[i]); path = 1; } else { fprintf(stderr, "Invalid IP address: %s\n", argv[i]); exit(1); } } else count++; } printf("%i FAs\n", count); ret = dynamics_mn_init(path ? argv[argc - 1] : DEFAULT_API_PATH); if (ret != API_SUCCESS) { fprintf(stderr, "dynamics_mn_init returned %i: %s\n", ret, dynamics_mn_get_error_string(ret)); return -1; } f = fopen("locupd2.log", "a"); if (f == NULL) { fprintf(stderr, "Could not open locupd2.log.\n"); exit(1); } if (dynamics_fork_daemon() != 0) { fprintf(stderr, "fork failed\n"); exit(1); } signal(SIGTERM, clean_up); signal(SIGINT, clean_up); locupd(period, count, fa); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -