📄 fastatus.c
字号:
/* $Id: fastatus.c,v 1.12 2001/09/08 14:29:40 jm Exp $ * Statistics information of FA in a TCP port * * 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. */#ifndef _GNU_SOURCE#define _GNU_SOURCE#endif#include <stdio.h>#include <stdlib.h>#include <sys/socket.h>#include <sys/param.h>#include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>#include <string.h>#include <errno.h>#include <time.h>#include <signal.h>#include <getopt.h>#include "util.h"#include "dyn_falib.h"static char *default_path = "/var/run/dynamics_fa_read";static char *error_msg = "API call failed\n";#define MAX_TUNNELS 64#define PID_FILE "/var/run/fastatus.pid"static void clean_up(int sig){ unlink(PID_FILE); exit(sig);}static void handle_request(int sock){ char buf[1024]; struct dynamics_tunnel_info tinfo; struct dynamics_fa_status status; dyn_tunnel_id tunnels[MAX_TUNNELS]; int r, c, i, len; read(sock, buf, sizeof(buf)); r = dynamics_fa_get_status(&status, 1); if (r != API_SUCCESS) { write(sock, error_msg, strlen(error_msg)); return; } snprintf(buf, sizeof(buf), "HTTP/1.1 200 OK\r\n" "Server: Dynamics fastatus\r\n" "Connection: close\r\n" "Content-Type: text/html\r\n\r\n" "<HTML><HEAD><TITLE>Dynamics Foreign Agent Statistics</TITLE>" "</HEAD><BODY BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\">\n"); write(sock, buf, strlen(buf)); snprintf(buf, sizeof(buf), "<BIG>Dynamics Foreign Agent</BIG>\n" "version %s\n<P><TABLE BORDER=1><TR><TD>" "tunnels %i\n<TD>requests accepted %li<TD>apicalls(admin) %li" "\n" "<TR><TD>advertisements sent %li<TD>requests rejected %li" "<TD>apicalls(read) %li\n</TABLE><P>\n", status.version, status.tunnel_count, status.req_accepted, status.apicalls_admin, status.adv_sent, status.req_rejected, status.apicalls_read); write(sock, buf, strlen(buf)); snprintf(buf, sizeof(buf), "<TABLE BORDER=1>\n" "<TR><TH>MN addr<TH>care-of addr<TH>HA addr" "<TH>created<TH>expires<TH>SPI<TH>conf.\n"); write(sock, buf, strlen(buf)); c = MAX_TUNNELS; r = dynamics_fa_get_tunnels(&c, tunnels, 1); if (r != API_SUCCESS) { printf("error=%i\n", r); write(sock, error_msg, strlen(error_msg)); return; } for (i = 0; i < c; i++) { len = sizeof(tinfo); r = dynamics_fa_get_tunnel_info(tunnels[i], &tinfo, &len, 1); if (r != API_SUCCESS || len != sizeof(tinfo)) { write(sock, error_msg, strlen(error_msg)); continue; } snprintf(buf, sizeof(buf), "<TR><TD>%s\n", inet_ntoa(tinfo.mn_addr)); write(sock, buf, strlen(buf)); snprintf(buf, sizeof(buf), "<TD>%s\n", inet_ntoa(tinfo.co_addr)); write(sock, buf, strlen(buf)); snprintf(buf, sizeof(buf), "<TD>%s\n", inet_ntoa(tinfo.ha_addr)); write(sock, buf, strlen(buf)); snprintf(buf, sizeof(buf), "<TD><SMALL>%s</SMALL>", ctime(&tinfo.creation_time)); write(sock, buf, strlen(buf)); snprintf(buf, sizeof(buf), "<TD><SMALL>%s</SMALL>", ctime(&tinfo.expiration_time)); write(sock, buf, strlen(buf)); snprintf(buf, sizeof(buf), "<TD>%i<TD>%i\n", tinfo.spi, tinfo.confirmed); write(sock, buf, strlen(buf)); } snprintf(buf, sizeof(buf), "</TABLE>\n</BODY></HTML>\n"); write(sock, buf, strlen(buf));}int main(int argc, char *argv[]){ int port = -1, s, i, t; char *path = NULL; struct sockaddr_in addr, sa; int foreground = 0,c; if (argc < 2) { fprintf(stderr, "fastatus -p <TCP port> [-s <API socket>] " "[--fg]\n"); exit(1); } while (1) { int option_index = 0; static struct option long_options[] = { {"fg", 0, 0, 0}, {0, 0, 0, 0} }; c = getopt_long (argc, argv, "p:s:", long_options, &option_index); if (c == -1) break; switch (c) { case 0: switch (option_index) { case 0: fprintf(stderr,"Option: %s\n", long_options[option_index].name); foreground = 1; } break; case 'p': port = atoi(optarg); if (port < 1) { fprintf(stderr, "Invalid port %i\n", port); exit(1); } printf("Port: %i\n", port); break; case 's': path = malloc(MAXPATHLEN); if (path == NULL) { fprintf(stderr, "malloc for path: %s\n", strerror(errno)); exit(1); } dynamics_strlcpy(path, optarg, MAXPATHLEN); break; case '?': fprintf(stderr, "fastatus -p <TCP port> -s [API " "socket] [--fg]\n"); exit(1); default: printf ("?? getopt returned character code " "0%o ??\n", c); } } if (path == NULL) path = default_path; if (dynamics_fa_init(path) != API_SUCCESS) { fprintf(stderr, "API initialization failed\n"); exit(1); } s = socket(AF_INET, SOCK_STREAM, 0); if (s < 0) { perror("socket"); exit(1); } i = 1; if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &i, sizeof(int)) < 0) { perror("setsockopt"); exit(1); } memset((char *) &addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; addr.sin_port = htons(port); if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { perror("bind"); exit(1); } if (!foreground && dynamics_fork_daemon() < 0) { fprintf(stderr, "fork failed\n"); exit(1); } dynamics_write_pid_file(PID_FILE); signal(SIGHUP, clean_up); signal(SIGTERM, clean_up); signal(SIGINT, clean_up); listen(s, 5); for (;;) { i = sizeof sa; t = accept(s, (struct sockaddr *) &sa, (unsigned int*) &i); if (t < 0) { perror("accept"); exit(1); }#if 0 printf("request from %s:%i\n", inet_ntoa(sa.sin_addr), ntohs(sa.sin_port));#endif handle_request(t); close(t); } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -