📄 trace.c
字号:
/* * Copyright (c) 2004 Ying Ge, Communication Research Center Canada. * * Copyright (c) 2002, 2003 Maoyu Wang, Communication Research Center Canada. * * By Ying Ge: * 1. Change the OLSR packet format and message processing procedure based * on the OLSR RFC. * 2. Add support of multiple interfaces to OLSR, including MID message * creating and processing procedure as specified in the OLSR RFC. * 3. Add QoS Support to OLSR * * By Maoyu Wang: * 1. Ported OLSR from IPv4 to IPv6. * 2. Added the Host and Network Association (HNA) functionality into OLSR. * 3. Added the default gateway functionality into OLSR by extending the HNA * message usage. The default gateway functionality supported the mobility * by cooperating with Mobile IPv6 for a mobile node as well as supported * Internet access for MANET nodes. * * DISTRIBUTED WITH NO WARRANTY, EXPRESS OR IMPLIED. * See the GNU Library General Public License (file COPYING in the distribution) * for conditions of use and redistribution *//* * This Copyright notice is in French. An English summary is given * but the referee text is the French one. * * Copyright (c) 2000, 2001 Adokoe.Plakoo@inria.fr, INRIA Rocquencourt, * Anis.Laouiti@inria.fr, INRIA Rocquencourt. * * Ce logiciel informatique est disponible aux conditions * usuelles dans la recherche, c'est-à-dire qu'il peut * être utilisé, copié, modifié, distribué à l'unique * condition que ce texte soit conservé afin que * l'origine de ce logiciel soit reconnue. * Le nom de l'Institut National de Recherche en Informatique * et en Automatique (INRIA), ou d'une personne morale * ou physique ayant participé à l'élaboration de ce logiciel ne peut * être utilisé sans son accord préalable explicite. * * Ce logiciel est fourni tel quel sans aucune garantie, * support ou responsabilité d'aucune sorte. * Certaines parties de ce logiciel sont dérivées de sources developpees par * University of California, Berkeley et ses contributeurs couvertes * par des copyrights. * This software is available with usual "research" terms * with the aim of retain credits of the software. * Permission to use, copy, modify and distribute this software for any * purpose and without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies, * and the name of INRIA, or any contributor not be used in advertising * or publicity pertaining to this material without the prior explicit * permission. The software is provided "as is" without any * warranties, support or liabilities of any kind. * This product includes software developed by the University of * California, Berkeley and its contributors protected by copyrights. *//* * Copyright (c) 1983, 1988, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. 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. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University 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 REGENTS 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 REGENTS 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. *//* * From: @(#)trace.c 5.11 (Berkeley) 2/28/91 * From: @(#)trace.c 8.1 (Berkeley) 6/5/93 */char trace_rcsid[] = "$Id: trace.c,v 1.3 2000/12/08 15:26:07 prima Exp $";/* * Routing Table Management Daemon */#define OLSRTYPES#include "defs.h"#include <sys/stat.h>#include <signal.h>#include <fcntl.h>#include <syslog.h>#include <errno.h>#include <paths.h>#define NRECORDS 50 /* size of circular trace buffer */FILE *ftrace = NULL /*stdout*/; /* output trace file */int traceactions=0; /* on/off */int tracepackets; /* watch packets as they go by */int tracehistory; /* on/off */static int tracecontents; /* watch packet contents as they go by */static struct timeval lastlog;static char *savetracename;static int iftraceinit(struct interface *, struct ifdebug *);void dumpif(FILE *, struct interface *);void dumptrace(FILE *, char *, struct ifdebug *);void traceinit(struct interface *ifp){ if (iftraceinit(ifp, &ifp->int_input) && iftraceinit(ifp, &ifp->int_output)) return; tracehistory = 0; fprintf(stderr, "traceinit: can't init %s\n", ifp->int_name);}static int iftraceinit(struct interface *ifp, struct ifdebug *ifd){ struct iftrace *t; ifd->ifd_records = (struct iftrace *)malloc(NRECORDS * sizeof (struct iftrace)); if (ifd->ifd_records == 0) return (0); ifd->ifd_front = ifd->ifd_records; ifd->ifd_count = 0; for (t = ifd->ifd_records; t < ifd->ifd_records + NRECORDS; t++) { t->ift_size = 0; t->ift_packet = 0; } ifd->ifd_if = ifp; return (1);}void traceon(char *file){ struct stat stbuf; int fd; if (ftrace != NULL) return; fd=open(file, O_CREAT|O_WRONLY|O_APPEND|O_NDELAY, 0600); if (fd==-1 || (fstat(fd, &stbuf) >= 0 && !S_ISREG(stbuf.st_mode))) { syslog(LOG_ERR, "Cannot syslog to %s: error %s\n", file, errno); return; } fcntl(fd, F_SETFL, 0); /* Turn NDELAY off */ savetracename = file; (void) gettimeofday(&now, (struct timezone *)NULL); ftrace = fdopen(fd, "a"); if (ftrace == NULL) return; dup2(fileno(ftrace), 1); dup2(fileno(ftrace), 2); traceactions = 1; fprintf(ftrace, "Tracing enabled %s\n", ctime((time_t *)&now.tv_sec));}void traceoff(void){ if (!traceactions) return; if (ftrace != NULL) { int fd = open(_PATH_DEVNULL, O_RDWR); fprintf(ftrace, "Tracing disabled %s\n",ctime((time_t *)&now.tv_sec)); fflush(ftrace); (void) dup2(fd, 1); (void) dup2(fd, 2); (void) close(fd); fclose(ftrace); ftrace = NULL; } traceactions = 0; tracehistory = 0; tracepackets = 0; tracecontents = 0;}void sigtrace(int s){ if (s == SIGUSR2) traceoff(); else if (ftrace == NULL && savetracename) traceon(savetracename); else bumploglevel();}/* * Move to next higher level of tracing when -t option processed or * SIGUSR1 is received. Successive levels are: * traceactions * traceactions + tracepackets * traceactions + tracehistory (packets and contents after change) * traceactions + tracepackets + tracecontents */void bumploglevel(void){ (void) gettimeofday(&now, (struct timezone *)NULL); if (traceactions == 0) { traceactions++; if (ftrace) fprintf(ftrace, "Tracing actions started %s\n", ctime((time_t *)&now.tv_sec)); } else if (tracepackets == 0) { tracepackets++; tracehistory = 0; tracecontents = 0; if (ftrace) fprintf(ftrace, "Tracing packets started %s\n",ctime((time_t *)&now.tv_sec)); } else if (tracehistory == 0) { tracehistory++; if (ftrace) fprintf(ftrace, "Tracing history started %s\n",ctime((time_t *)&now.tv_sec)); } else { tracepackets++; tracecontents++; tracehistory = 0; if (ftrace) fprintf(ftrace, "Tracing packet contents started %s\n",ctime((time_t *)&now.tv_sec)); } if (ftrace) fflush(ftrace);}void trace(struct ifdebug *ifd, struct sockaddr *who, char *p, int len, int m){ struct iftrace *t; if (ifd->ifd_records == 0) return; t = ifd->ifd_front++; if (ifd->ifd_front >= ifd->ifd_records + NRECORDS) ifd->ifd_front = ifd->ifd_records; if (ifd->ifd_count < NRECORDS) ifd->ifd_count++; if (t->ift_size > 0 && t->ift_size < len && t->ift_packet) { free(t->ift_packet); t->ift_packet = 0; } t->ift_stamp = now; //v4->v6??? t->ift_who = *who; if (len > 0 && t->ift_packet == 0) { t->ift_packet = (char *)malloc(len); if (t->ift_packet == 0) len = 0; } if (len > 0) memcpy(t->ift_packet, p, len); t->ift_size = len; t->ift_metric = m; }void traceaction(FILE *fd, char *action, struct olsr_ip_addr *addr, struct interface *ifp, int timer){ int first; char *cp; if (fd == NULL) return; if (lastlog.tv_sec != now.tv_sec || lastlog.tv_usec != now.tv_usec) { fprintf(fd, "\n%.19s:\n", ctime((time_t *)&now.tv_sec));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -