📄 riplib.c
字号:
/* ripLib.c - Routing Information Protocol (RIP) v1 and v2 library *//* Copyright 1984 - 1999 Wind River Systems, Inc. */#include "copyright_wrs.h"/* * 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. *//*modification history--------------------01x,16mar99,spm recovered orphaned code from tor1_0_1.sens1_1 (SPR #25770)01w,05oct98,spm made task parameters adjustable and optimized stack size (SPR #22422)01v,11sep98,spm general overhaul - moved expanded ripShutdown routine from ripTimer.c (SPR #22352); altered ripBuildPacket to allow class-based masks for internal supernets (SPR #22350)01u,01sep98,spm changed ripBuildPacket to include correct netmask for classless routing (SPR #22220 and #22065); added support for default next hop routers (SPR #21940)01t,26jun98,spm corrected ripBuildPacket to test version against MIB values (allowing RIPv1 updates) and added subnet test to use valid router in RIPv2 updates; moved semaphore creation before first use; changed RIP_MCAST_ADDR constant from string to value; added ripClearInterfaces routine needed to comply with ANVL RIP tests; removed compiler warnings01s,14dec97,jdi doc: cleanup.01r,21oct97,kbw made minor man page font fix01q,06oct97,gnn added sendHook routines and cleaned up warnings01p,04aug97,kbw fixed man page problems found in beta review01o,02jun97,gnn fixed SPR 8685 so that the timer task does not respawn.01n,16may97,gnn added code to implement leaking routes. modified ripSplitPacket to handle stupid packets (ANVL). renamed myHook to ripAuthHook.01m,08may97,gnn fixed an authentication bug.01l,05may97,rjc changed error return value to m2Lib stuff.01k,28apr97,gnn added some descriptive text.01j,20apr97,kbw fixed man page format, did spell check.01h,18apr97,gnn removed device specific code.01g,17apr97,gnn fixed errors pointed out by ANVL.01f,14apr97,gnn added authentication hook routines.01e,07apr97,gnn removed device specific code.01d,07apr97,gnn cleared up some of the more egregious warnings. added MIB-II interfaces and options.01c,12mar97,gnn added multicast support. added time variables.01b,24feb97,gnn added routines for version 2 support.01a,26nov96,gnn created from BSD4.4 routed main.c*//*DESCRIPTIONThis library implements versions 1 and 2 of the Routing Information Protocol (RIP). The protocol is intended to operate as an interior gateway protocolwithin a relatively small network with a longest path of 15 hops.HIGH-LEVEL INTERFACEThe ripLibInit() routine links this library into the VxWorks image and beginsa RIP session. This happens automatically if INCLUDE_RIP is defined at thetime the image is built. Once started, RIP will maintain the network routing table until deactivated by a call to the ripShutdown() routine, which will remove all route entries and disable the RIP library routines. All RIPrequests and responses are handled as defined in the RFC specifications.RFC 1058 defines the basic protocol operation and RFC 1723 details theextensions which implement version 2.When acting as a supplier, outgoing route updates are filtered using simplesplit horizon. Split horizon with poisoned reverse is not currently available.Additional route entries may be excluded from the periodic update with theripSendHookAdd() routine. If a RIP session is terminated, the networking subsystem may not function correctly until RIP is restarted with a new call to ripLibInit() unlessrouting information is provided by some other method.CONFIGURATION INTERFACEBy default, a RIP session only uses the network interfaces created before itstarted. The ripIfSearch() routine allows RIP to recognize any interfaces added to the system after that point. If the address or netmask of anexisting interface is changed during a RIP session, the ripIfReset() routine must be used to update the RIP configuration appropriately.The current RIP implementation also automatically performs the bordergateway filtering required by the RFC specification. Those restrictionsprovide correct operation in a mixed environment of RIP-1 and RIP-2 routers.The ripFilterDisable() routine will remove those limitations, and may producemore efficient routing for some topologies. That routine must not be usedif any version 1 routers are present. The ripFilterEnable() routine willrestore the default behavior. AUTHENTICATION INTERFACEBy default, authentication is disabled, but may be activated by an SNMPagent on an interface-specific basis. While authentication is disabled,any RIP-2 messages containing authentication entries are discarded. Whenenabled, all RIP-2 messages without authentication entries are automaticallyrejected. To fully support authentication, an authentication routine shouldbe specified with the ripAuthHookAdd() routine. The specified functionwill be called to screen every RIP-1 message and all unverified RIP-2 messages containing authentication entries. It may be removed with the ripAuthHookDelete() routine. All RIP-1 and unverified RIP-2 messages will be discarded while authentication is enabled unless a hook is present.OPTIONAL INTERFACEThe ripLeakHookAdd() routine allows the use of an alternative routingprotocol which uses RIP as a transport mechanism. The specified functioncan prevent the RIP session from creating any table entries from thereceived messages. The ripLeakHookDelete() routine will restore thedefault operation. DEBUGGING INTERFACEAs required by the RFC specification, the obsolete traceon and traceoff messages are not supported by this implementation. The ripRouteShow()routine will display the contents of the internal RIP routing table.Routines such as mRouteShow() to display the corresponding kernel routing table will also be available if INCLUDE_NET_SHOW is defined when the image is built. If additional information is required, the ripDebugLevelSet() routine will enable predefined debugging messages which will be sent to the standard output.INCLUDE FILES: ripLib.hSEE ALSO: RFC 1058, RFC 1723*//* * Routing Table Management Daemon */#include "vxWorks.h"#include "rip/defs.h"#include "m2Lib.h"#include "sys/ioctl.h"#include "sys/socket.h"#include "inetLib.h"#include "taskLib.h"#include "tickLib.h"#include "sockLib.h"#include "sysLib.h"#include "net/if.h"#include "errnoLib.h"#include "errno.h"#include "logLib.h"#include "wdLib.h"#include "semLib.h"#include "ioLib.h"/* defines */#define BUFSPACE 127*1024/* globals */RIP ripState;int routedDebug = 0;SEM_ID ripLockSem;BOOL ripFilterFlag = TRUE;/* * Settings for primary and timer tasks. For correct operation, the timer * task must run at a higher priority than the primary task. The stack * sizes are chosen based on the high-water mark measured on a Sparc target, * since the high use of registers in that architecture provides a likely * maximum. The actual values measured were 2524 bytes for the primary task * and 1824 bytes for the timer task. */int _ripTaskPriority = 100;int _ripTaskOptions = 0;int _ripTaskStackSize = 2750;int _ripTimerTaskPriority = 99;int _ripTimerTaskOptions = 0;int _ripTimerTaskStackSize = 2000;/* locals */LOCAL BOOL ripInitFlag = FALSE;IMPORT struct interface *ripIfNet;IMPORT struct interface **ifnext;/* forward declarations */ void rtdeleteall();FUNCPTR ripTimer();int ripTask();void timevaladd (struct timeval *t1, struct timeval *t2);void timevalsub (struct timeval *t1, struct timeval *t2);void ripTimeSet (struct timeval *pTimer);void process(int fd);void ripTimerArm (long timeout);void ripSetInterfaces(INT32 sock, UINT32 mcastAddr);void ripClearInterfaces(INT32 sock, UINT32 mcastAddr);int getsocket(int domain, int type, struct sockaddr_in *sin);void routedTableInit();void rtdefault();STATUS routedIfInit();void routedInput(struct sockaddr *from, register RIP_PKT *rip, int size);void toall(int (*f)(), int rtstate, struct interface *skipif);/******************************************************************************** ripLibInit - initialize the RIP routing library** This routine creates and initializes the global data structures used by * the RIP routing library and starts a RIP session to maintain routing * tables for a host. It must be called before using any other library * routines, and is invoked automatically if INCLUDE_RIP is defined at * the time the system is built.** The resulting RIP session will monitor all network interfaces which are * currently available for messages from other RIP routers. If the <supplier>* parameter is true, it will also respond to specific requests from other* routers and transmit route updates over every known interface at the * interval specified by <supplyInterval>.** Specifying a <gateway> setting of true establishes this router as a* gateway to the wider Internet, capable of routing packets anywhere within * the local networks. The final <multicast> flag indicates whether the* RIP messages are sent to the pre-defined multicast address of 224.0.0.9* (which requires a <version> setting of 2) or to the broadcast address of * the interfaces.** The <version> parameter determines the format used for outgoing RIP * messages, and also sets the initial settings of the MIB-II compatibility * switches in combination with the <multicast> flag. A <version> of 1 will * restrict all incoming traffic to that older message type. A <version> of * 2 will set the receive switch to accept either type unless <multicast> is * true, which limits reception to version 2 messages only. SNMP agents may * alter those settings on a per-interface basis once startup is complete.** The remaining parameters set various system timers used to maintain the* routing table. All of the values are expressed in seconds, and must be* greater than or equal to 1. The <timerRate> determines how often* the routing table is examined for changes and expired routes. The* <supplyInterval> must be an exact multiple of that value. The* <expire> parameter specifies the maximum time between updates before* a route is invalidated and removed from the kernel table. Expired routes * are then deleted from the internal RIP routing table if no update has* been received within the time set by the <garbage> parameter.** The defaults for all the parameter settings are given by the following* constants. The default timer values match the settings indicated in* the RFC specification.** .TS* tab(|);* lf3 lf3 lf3* l c l.* Parameter Name | Default Value | Symbolic Constant * _* <supplier> | 0 (FALSE) | RIP_SUPPLIER* <gateway> | 0 (FALSE) | RIP_GATEWAY* <multicast> | 0 (FALSE) | RIP_EXPIRE_TIME* <version> | 1 | RIP_SUPPLY_INTERVAL* <timerRate> | 1 | RIP_TIMER_RATE* <supplyInterval> | 30 | RIP_SUPPLY_INTERVAL* <expire> | 180 | RIP_EXPIRE_TIME* <garbage> | 300 | RIP_GARBAGE_TIME* .TE** INTERNAL* This routine creates two tasks, `tRip' and `tRipTimer'. The first is the * main loop of the routing task which monitors the routing port (520) for * updates and request messages. The second task uses a watchdog timer and* signalling semaphore to update the internal RIP routing table. The * ripLockSem blocking semaphore provides any necessary interlocking between * each of the tasks and the user routines which alter the RIP configuration.* * RETURNS: OK, or ERROR if configuration fails.** ERRNO: N/A*/STATUS ripLibInit ( BOOL supplier, /* operate in silent mode? */ BOOL gateway, /* act as gateway to the Internet? */ BOOL multicast, /* use multicast or broadcast addresses? */ int version, /* 1 or 2: selects format of outgoing messages */ int timerRate, /* update frequency for internal routing table */ int supplyInterval, /* update frequency for neighboring routers */ int expire, /* maximum interval for renewing learned routes */ int garbage /* elapsed time before deleting stale route */ ) { IMPORT m2RipInit(); if (ripInitFlag) return (OK); if (version < 1 || version > 2) return (ERROR); if ((version < 2) && (multicast == TRUE)) return (ERROR); if (timerRate < 1) return (ERROR); if (supplyInterval < 1) return (ERROR); if (expire < 1) return (ERROR); if (garbage < 1) return (ERROR); bzero ((char *)&ripState, sizeof(ripState)); /* Set the global state. */ ripState.version = version; ripState.multicast = multicast; ripState.timerRate = timerRate; ripState.supplyInterval = supplyInterval; ripState.expire = expire; ripState.garbage = garbage; ripFilterFlag = TRUE; /* Enable border gateway filtering. */ routedDebug = 0; /* Disable debugging messages by default. */ /* * This all has to do with RIP MIB II stuff. * * We set a global configuration that all interfaces will have * at startup time. SNMP agents can then modify these values * on a per interface basis. */ m2RipInit(); if (version == 1) ripState.ripConf.rip2IfConfSend = M2_rip2IfConfSend_ripVersion1; else if (multicast) ripState.ripConf.rip2IfConfSend = M2_rip2IfConfSend_ripVersion2; else ripState.ripConf.rip2IfConfSend = M2_rip2IfConfSend_rip1Compatible; if (version == 1) ripState.ripConf.rip2IfConfReceive = M2_rip2IfConfReceive_rip1; else if (multicast) ripState.ripConf.rip2IfConfReceive = M2_rip2IfConfReceive_rip2; else ripState.ripConf.rip2IfConfReceive = M2_rip2IfConfReceive_rip1OrRip2; ripState.ripConf.rip2IfConfAuthType = M2_rip2IfConfAuthType_noAuthentication; ripState.ripConf.rip2IfConfStatus = M2_rip2IfConfStatus_valid; /* Create the monitor task to receive and process RIP messages. */ ripState.ripTaskId = taskSpawn (RIP_TASK, _ripTaskPriority, _ripTaskOptions, _ripTaskStackSize, ripTask, supplier, gateway, multicast, 0, 0, 0, 0, 0, 0, 0); if (ripState.ripTaskId == ERROR) return (ERROR); ripInitFlag = TRUE; return (OK); }STATUS ripTask ( BOOL supplier, BOOL gateway, BOOL multicast ) { int n, nfd; struct timeval *pTimeout; struct timeval waittime; register RIP_PKT *query = (RIP_PKT *)ripState.packet; fd_set ibits;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -