📄 olsrd_txtinfo.c
字号:
/* * The olsr.org Optimized Link-State Routing daemon(olsrd) * Copyright (c) 2004, Andreas T鴑nesen(andreto@olsr.org) * includes code by Bruno Randolf * includes code by Andreas Lopatic * includes code by Sven-Ola Tuecke * includes code by Lorenz Schori * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * Neither the name of olsr.org, olsrd nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * Visit http://www.olsr.org for more information. * * If you find this software useful feel free to make a donation * to the project. For more information see the website or contact * the copyright holders. * * $Id: olsrd_txtinfo.c,v 1.12 2007/10/14 14:11:11 bernd67 Exp $ *//* * Dynamic linked library for the olsr.org olsr daemon */ #include <sys/types.h>#include <sys/socket.h>#if !defined WIN32#include <sys/select.h>#endif#include <netinet/in.h>#include <arpa/inet.h>#include <sys/time.h>#include <time.h>#include <math.h>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <stdarg.h>#include <unistd.h>#include <errno.h>#include "olsr.h"#include "olsr_types.h"#include "neighbor_table.h"#include "two_hop_neighbor_table.h"#include "mpr_selector_set.h"#include "tc_set.h"#include "hna_set.h"#include "mid_set.h"#include "link_set.h"#include "socket_parser.h"#include "olsrd_txtinfo.h"#include "olsrd_plugin.h"#ifdef WIN32#define close(x) closesocket(x)#endifstatic int ipc_socket;static int ipc_open;static int ipc_connection;static int ipc_socket_up;/* IPC initialization function */static int plugin_ipc_init(void);static void send_info(int neighonly);static void ipc_action(int);static void ipc_print_neigh_link(void);static void ipc_print_routes(void);static void ipc_print_topology(void);static void ipc_print_hna(void);static void ipc_print_mid(void);#define TXT_IPC_BUFSIZE 256static int ipc_sendf(const char* format, ...) __attribute__((format(printf, 1, 2)));/** *Do initialization here * *This function is called by the my_init *function in uolsrd_plugin.c */intolsrd_plugin_init(void){ /* Initial IPC value */ ipc_open = 0; ipc_socket_up = 0; plugin_ipc_init(); return 1;}/** * destructor - called at unload */void olsr_plugin_exit(void){ if(ipc_open) close(ipc_socket);}static intplugin_ipc_init(void){ struct sockaddr_storage sst; struct sockaddr_in *sin; struct sockaddr_in6 *sin6; olsr_u32_t yes = 1; socklen_t addrlen; /* Init ipc socket */ if ((ipc_socket = socket(olsr_cnf->ip_version, SOCK_STREAM, 0)) == -1) {#ifndef NODEBUG olsr_printf(1, "(TXTINFO) socket()=%s\n", strerror(errno));#endif return 0; } else { if (setsockopt(ipc_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes)) < 0) {#ifndef NODEBUG olsr_printf(1, "(TXTINFO) setsockopt()=%s\n", strerror(errno));#endif return 0; }#if defined __FreeBSD__ && defined SO_NOSIGPIPE if (setsockopt(ipc_socket, SOL_SOCKET, SO_NOSIGPIPE, (char *)&yes, sizeof(yes)) < 0) { perror("SO_REUSEADDR failed"); return 0; }#endif /* Bind the socket */ /* complete the socket structure */ memset(&sst, 0, sizeof(sst)); if (olsr_cnf->ip_version == AF_INET) { sin = (struct sockaddr_in *)&sst; sin->sin_family = AF_INET; addrlen = sizeof(struct sockaddr_in);#ifdef SIN6_LEN sin->sin_len = addrlen;#endif sin->sin_addr.s_addr = INADDR_ANY; sin->sin_port = htons(ipc_port); } else { sin6 = (struct sockaddr_in6 *)&sst; sin6->sin6_family = AF_INET6; addrlen = sizeof(struct sockaddr_in6);#ifdef SIN6_LEN sin6->sin6_len = addrlen;#endif sin6->sin6_addr = in6addr_any; sin6->sin6_port = htons(ipc_port); } /* bind the socket to the port number */ if (bind(ipc_socket, (struct sockaddr *) &sst, addrlen) == -1) {#ifndef NODEBUG olsr_printf(1, "(TXTINFO) bind()=%s\n", strerror(errno));#endif return 0; } /* show that we are willing to listen */ if (listen(ipc_socket, 1) == -1) {#ifndef NODEBUG olsr_printf(1, "(TXTINFO) listen()=%s\n", strerror(errno));#endif return 0; } /* Register with olsrd */ add_olsr_socket(ipc_socket, &ipc_action); #ifndef NODEBUG olsr_printf(2, "(TXTINFO) listening on port %d\n",ipc_port);#endif ipc_socket_up = 1; } return 1;}static void ipc_action(int fd){ struct sockaddr_storage pin; struct sockaddr_in *sin4; struct sockaddr_in6 *sin6; char addr[INET6_ADDRSTRLEN]; fd_set rfds; struct timeval tv; int neighonly = 0; socklen_t addrlen = sizeof(struct sockaddr_storage); if(ipc_open) return; if ((ipc_connection = accept(fd, (struct sockaddr *) &pin, &addrlen)) == -1) {#ifndef NODEBUG olsr_printf(1, "(TXTINFO) accept()=%s\n", strerror(errno));#endif return; } tv.tv_sec = tv.tv_usec = 0; if (olsr_cnf->ip_version == AF_INET) { sin4 = (struct sockaddr_in *)&pin; if (inet_ntop(olsr_cnf->ip_version, &sin4->sin_addr, addr, INET6_ADDRSTRLEN) == NULL) addr[0] = '\0'; if (!COMP_IP(&sin4->sin_addr, &ipc_accept_ip.v4)) { olsr_printf(1, "(TXTINFO) From host(%s) not allowed!\n", addr); close(ipc_connection); return; } } else { sin6 = (struct sockaddr_in6 *)&pin; if (inet_ntop(olsr_cnf->ip_version, &sin6->sin6_addr, addr, INET6_ADDRSTRLEN) == NULL) addr[0] = '\0'; /* Use in6addr_any (::) in olsr.conf to allow anybody. */ if (!COMP_IP(&in6addr_any, &ipc_accept_ip.v6) && !COMP_IP(&sin6->sin6_addr, &ipc_accept_ip.v6)) { olsr_printf(1, "(TXTINFO) From host(%s) not allowed!\n", addr); close(ipc_connection); return; } } ipc_open = 1;#ifndef NODEBUG olsr_printf(2, "(TXTINFO) Connect from %s\n",addr);#endif /* purge read buffer to prevent blocking on linux*/ FD_ZERO(&rfds); FD_SET((unsigned int)ipc_connection, &rfds); /* Win32 needs the cast here */ if(0 <= select(ipc_connection+1, &rfds, NULL, NULL, &tv)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -