📄 util.c
字号:
/* $Id: util.c,v 1.45 2001/09/30 13:53:38 jm Exp $ * Help routines for the agents * * 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. */#ifdef HAVE_CONFIG_H#include <config.h>#endif#include <stdio.h>#include <stdlib.h>#include <string.h>#include <getopt.h>#include <sys/types.h>#include <unistd.h>#include <assert.h>#include <syslog.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <netinet/ip.h>#ifdef DYN_TARGET_WINDOWS#include "windows_extra.h"#else#include <netinet/udp.h>#endif#include <sys/stat.h>#include <fcntl.h>#include "debug.h"#include "dyn_ip.h"#include "util.h"#define DEBUG_FLAG 'u'int opt_foreground = 0;char *opt_config = NULL;int opt_debug = 0;static struct option#ifndef DYN_TARGET_WINDOWSconst#endiflong_options[] ={ {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'v'}, {"fg", no_argument, &opt_foreground, 1}, {"debug", no_argument, &opt_debug, 1}, {"config", required_argument, NULL, 'c'}, {0, 0, 0, 0}};char *parse_long_options(int argc, char *argv[], const char *command_name, const char *package, const char *version, void (*usage)(int, char *)){ int c; int saved_opterr; char *program_name; if (strchr(argv[0], '/')) program_name = strrchr(argv[0], '/') + 1; else program_name = argv[0]; saved_opterr = opterr; /* do not print an error message for unrecognized options */ opterr = 0; while ((c = getopt_long(argc, argv, "+hv", long_options, NULL)) != EOF) { switch (c) { case 'h': (*usage) (0, program_name); break; case 'v': printf("%s (%s) %s\n", command_name, package, version); printf("Copyright (C) 1998-2001, Dynamics group\n" "This program comes with NO WARRANTY,\n" "to the extent permitted by law.\n" "You may redistribute copies of this program\n" "under the terms of the GNU General Public " "License.\n" "For more information about these matters,\n" "see the files named COPYING.\n"); exit(0); case 'c': opt_config = optarg; assert(opt_config != NULL); printf("Using configuration file '%s'\n", opt_config); break; default: /* skip other parameters */ break; } } opterr = saved_opterr; /* Reset the getopt internals if it is called again */ optind = 1; return program_name;}/* print usage information */voiddynamics_usage(int status, char *name){ if (status != 0) { fprintf(stderr, "Try `%s --help' for more information.\n", name); } else { printf("Usage: %s [OPTION]\n\n" " -h / --help display this help\n" " --fg do not fork the daemon\n" " --version output version information\n" " --debug print debug information " "(use with --fg)\n" " --config <file> use given file as a " "configuration file\n", name); /* each agent shows it own specific parameters after this */ } if (status != 0) exit(status);}intdynamics_write_pid_file(char *pid_file){ FILE *f; if (pid_file != NULL) { f = fopen(pid_file, "w"); if (f != NULL) { fprintf(f, "%u\n", (unsigned int) getpid()); fclose(f); } return 0; } return -1;}/* Forks a daemon process and exits the original process * Returns: * -1: fork failed * 0: fork succeeded, running in daemon process * 1: -fg used, running the same process */intdynamics_fork_daemon(void){ pid_t child; int s; /* fork daemon and exit */ child = fork(); if (child < 0) { fprintf(stderr, "fork failed\n"); return -1; } if (child > 0) { /* original process - exit return immediately * (no & needed in the end of the command line) */ exit(0); } /* point stdin, stdout, and stderr to /dev/null */ s = open("/dev/null", O_RDWR); if (s >= 0) { dup2(s, STDIN_FILENO); dup2(s, STDOUT_FILENO); dup2(s, STDERR_FILENO); if (s > 2) close(s); } else { close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); } return 0;}unsigned intget_rand32(void){ unsigned int data; static FILE *rand_file = NULL; if (rand_file == NULL) { rand_file = fopen("/dev/urandom", "rb"); if (rand_file == NULL) { DEBUG(DEBUG_FLAG, "Cannot open /dev/urandom - " "reverting to rand()\n"); return rand(); } } fread(&data, 1, 4, rand_file); return data;}/** * allow_ip_forwarding: * * Sets up kernel to allow IPv4 forwarding * * Returns: -1 on failure and 0 on success */intallow_ipv4_forwarding(void){ int ret; FILE *f; f = fopen("/proc/sys/net/ipv4/ip_forward", "w"); if (f == NULL) return -1; ret = fputs("1", f) > 0; fclose(f); return ret;}voidcheck_kernel_support(int options){ int ok = 1; if ((options & CHECK_KERNEL_ADV_ROUTING) && dyn_ip_check_adv_routing() < 0) { fprintf(stderr, "Kernel does not support advanced policy " "routing.\n" "You will need to recompile the kernel with that " "support\n" "(options CONFIG_IP_ADVANCED_ROUTER and " "CONFIG_IP_MULTIPLE_TABLES).\n"); ok = 0; } if ((options & CHECK_KERNEL_IPIP) && dyn_ip_check_ipip() < 0) { fprintf(stderr, "Kernel does not support IP-within-IP " "encapsulation (ipip.o module)\n" "Try to load it with 'modprobe ipip'.\n" "If that does not work, you will need to recompile " "the kernel with IPIP support (CONFIG_NET_IPIP).\n"); ok = 0; } if ((options & CHECK_KERNEL_GRE) && dyn_ip_check_gre() < 0) { fprintf(stderr, "Kernel does not support GRE " "encapsulation (ip_gre.o module)\n" "Try to load it with 'modprobe ip_gre'.\n" "If that does not work, you will need to recompile " "the kernel with GRE support (CONFIG_NET_IPGRE).\n"); ok = 0; } if ((options & CHECK_KERNEL_NETLINK) && dyn_ip_check_netlink() < 0) { fprintf(stderr, "Kernel does not support netlink socket and/or" " routing messages\n" "You will need to recompile the kernel with networking" " options CONFIG_NETLINK and CONFIG_RTNETLINK.\n"); ok = 0; } if (!ok) { fprintf(stderr, "At least one of the required kernel options " "was missing. Aborting.\n"); exit(1); }}void init_key_extension(struct msg_key *ext, __u16 sub_type, __u32 spi, __u8 keylen){ assert(ext != NULL && keylen <= (255-7)); ext->type = VENDOR_EXT_TYPE2; ext->length = keylen + MIN_KEY_EXT_LEN; ext->reserved = 0; ext->vendor_id = htonl(VENDOR_ID_DYNAMICS); ext->sub_type = htons(sub_type); ext->spi = spi;}#ifndef ETH_ALEN#define ETH_ALEN 6#endif/* Return a pointer to hardware address string */char* ether_hwtoa(unsigned char *addr){ static char ret[ETH_ALEN * 3 + 1]; int i; for (i = 0; i < ETH_ALEN; i++) sprintf(&ret[i * 3], "%02x:", addr[i]); ret[sizeof(ret) - 2] = '\0'; return ret;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -