📄 hastatus.c
字号:
/* $Id: hastatus.c,v 1.4 2000/04/06 07:26:53 jm Exp $ * Statistics information of FA in a TCP port * * Dynamic hierarchial IP tunnel * Copyright (C) 1998-2000, 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 <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>#include <string.h>#include <time.h>#include "dyn_halib.h"static char *default_path = "/var/run/dynamics_ha_read";static char *error_msg = "API call failed\n";#define MAX_TUNNELS 64void handle_request(int sock){ char buf[1024]; struct dynamics_tunnel_info tinfo; struct dynamics_ha_status status; dyn_tunnel_id tunnels[MAX_TUNNELS]; int r, c, i, len; read(sock, buf, sizeof(buf)); r = dynamics_ha_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 hastatus\r\n" "Connection: close\r\n" "Content-Type: text/html\r\n\r\n" "<HTML><HEAD><TITLE>Dynamics Home Agent Statistics</TITLE>" "</HEAD><BODY BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\">\n"); write(sock, buf, strlen(buf)); snprintf(buf, sizeof(buf), "<BIG>Dynamics Home Agent</BIG>\n" "version %s\n<P>Tunnels: %i<P>\n", status.version, status.tunnel_count); write(sock, buf, strlen(buf)); snprintf(buf, sizeof(buf), "<TABLE BORDER=1>\n" "<TR><TH>MN addr<TH>care-of addr" "<TH>created<TH>expires<TH>SPI\n"); write(sock, buf, strlen(buf)); c = MAX_TUNNELS; r = dynamics_ha_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_ha_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><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\n", tinfo.spi); 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, s, i, t; char *path; struct sockaddr_in addr, sa; if (argc < 2) { fprintf(stderr, "hastatus <TCP port> [API socket]\n"); exit(1); } port = atoi(argv[1]); if (port < 1) { fprintf(stderr, "Invalid port %i\n", port); exit(1); } if (argc > 2) path = argv[2]; else path = default_path; if (dynamics_ha_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); } listen(s, 5); for (;;) { i = sizeof sa; t = accept(s, (struct sockaddr *) &sa, (unsigned int*) &i); if (t < 0) { perror("accept"); exit(1); } printf("request from %s:%i\n", inet_ntoa(sa.sin_addr), ntohs(sa.sin_port)); handle_request(t); close(t); } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -