route.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2003 by egnite Software GmbH. All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00021  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  *
00032  * -
00033  * Portions Copyright (C) 2000 David J. Hudson <dave@humbug.demon.co.uk>
00034  *
00035  * This file is distributed in the hope that it will be useful, but WITHOUT
00036  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00037  * FITNESS FOR A PARTICULAR PURPOSE.
00038  *
00039  * You can redistribute this file and/or modify it under the terms of the GNU
00040  * General Public License (GPL) as published by the Free Software Foundation;
00041  * either version 2 of the License, or (at your discretion) any later version.
00042  * See the accompanying file "copying-gpl.txt" for more details.
00043  *
00044  * As a special exception to the GPL, permission is granted for additional
00045  * uses of the text contained in this file.  See the accompanying file
00046  * "copying-liquorice.txt" for details.
00047  * -
00048  * Portions Copyright (c) 1983, 1993 by
00049  *  The Regents of the University of California.  All rights reserved.
00050  *
00051  * Redistribution and use in source and binary forms, with or without
00052  * modification, are permitted provided that the following conditions
00053  * are met:
00054  * 1. Redistributions of source code must retain the above copyright
00055  *    notice, this list of conditions and the following disclaimer.
00056  * 2. Redistributions in binary form must reproduce the above copyright
00057  *    notice, this list of conditions and the following disclaimer in the
00058  *    documentation and/or other materials provided with the distribution.
00059  * 3. Neither the name of the University nor the names of its contributors
00060  *    may be used to endorse or promote products derived from this software
00061  *    without specific prior written permission.
00062  *
00063  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00064  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00065  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00066  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00067  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00068  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00069  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00070  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00071  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00072  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00073  * SUCH DAMAGE.
00074  * -
00075  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
00076  *
00077  * Permission to use, copy, modify, and distribute this software for any
00078  * purpose with or without fee is hereby granted, provided that the above
00079  * copyright notice and this permission notice appear in all copies, and that
00080  * the name of Digital Equipment Corporation not be used in advertising or
00081  * publicity pertaining to distribution of the document or software without
00082  * specific, written prior permission.
00083  * 
00084  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
00085  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
00086  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
00087  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00088  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00089  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00090  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00091  * SOFTWARE.
00092  */
00093 
00094 /*
00095  * $Log: route.c,v $
00096  * Revision 1.5  2005/07/23 14:30:40  haraldkipp
00097  * Removed unnecessary critical sections and atomic inc-/decrements.
00098  * Fixed a bug in NutIpRouteList(), which filled the first entry only.
00099  * Corrected the comment about unavailabilty of removing routes.
00100  *
00101  * Revision 1.4  2005/04/30 16:42:42  chaac
00102  * Fixed bug in handling of NUTDEBUG. Added include for cfg/os.h. If NUTDEBUG
00103  * is defined in NutConf, it will make effect where it is used.
00104  *
00105  * Revision 1.3  2004/12/17 15:27:19  haraldkipp
00106  * Added Adam Pierce's routing management functions.
00107  *
00108  * Revision 1.2  2004/10/10 16:37:03  drsung
00109  * Detection of directed broadcasts to local network added.
00110  *
00111  * Revision 1.1.1.1  2003/05/09 14:41:38  haraldkipp
00112  * Initial using 3.2.1
00113  *
00114  * Revision 1.12  2003/03/31 14:53:28  harald
00115  * Prepare release 3.1
00116  *
00117  * Revision 1.11  2003/02/04 18:14:57  harald
00118  * Version 3 released
00119  *
00120  * Revision 1.10  2002/06/26 17:29:36  harald
00121  * First pre-release with 2.4 stack
00122  *
00123  */
00124 
00125 #include <cfg/os.h>
00126 #include <sys/heap.h>
00127 
00128 #include <net/if_var.h>
00129 #include <net/route.h>
00130 
00131 #include <string.h>
00132 
00133 #ifdef NUTDEBUG
00134 #include <sys/osdebug.h>
00135 #endif
00136 
00141 
00142 RTENTRY *rteList;           
00161 int NutIpRouteAdd(u_long ip, u_long mask, u_long gate, NUTDEVICE * dev)
00162 {
00163     int rc = -1;
00164     RTENTRY *rte;
00165     RTENTRY *rtp;
00166     RTENTRY **rtpp;
00167 
00168     /*
00169      * Insert a new entry into the list, which is
00170      * sorted based on the mask. Host routes are
00171      * in front, default routes at the end and
00172      * networks in between.
00173      */
00174     rtp = rteList;
00175     rtpp = &rteList;
00176     while (rtp && rtp->rt_mask > mask) {
00177         rtpp = &rtp->rt_next;
00178         rtp = rtp->rt_next;
00179     }
00180     if ((rte = NutHeapAlloc(sizeof(RTENTRY))) != 0) {
00181         rte->rt_ip = ip & mask;
00182         rte->rt_mask = mask;
00183         rte->rt_gateway = gate;
00184         rte->rt_dev = dev;
00185         rte->rt_next = rtp;
00186         *rtpp = rte;
00187         rc = 0;
00188     }
00189     return rc;
00190 }
00191 
00199 int NutIpRouteDelAll(NUTDEVICE * dev)
00200 {
00201     RTENTRY **rtpp;
00202     RTENTRY *rte;
00203 
00204     rtpp = &rteList;
00205 
00206     while (*rtpp) {
00207         rte = *rtpp;
00208 
00209         if (rte->rt_dev == dev || dev == 0) {
00210             *rtpp = rte->rt_next;
00211             NutHeapFree(rte);
00212         } else
00213             *rtpp = (*rtpp)->rt_next;
00214     }
00215     return 0;
00216 }
00217 
00218 
00232 int NutIpRouteDel(u_long ip, u_long mask, u_long gate, NUTDEVICE * dev)
00233 {
00234     int rc = -1;
00235     RTENTRY **rtpp;
00236     RTENTRY *rte;
00237 
00238     for (rtpp = &rteList; *rtpp; *rtpp = (*rtpp)->rt_next) {
00239         rte = *rtpp;
00240 
00241         if (rte->rt_ip == ip && rte->rt_mask == mask && rte->rt_gateway == gate && rte->rt_dev == dev) {
00242             *rtpp = rte->rt_next;
00243             NutHeapFree(rte);
00244             rc = 0;
00245         }
00246     }
00247     return rc;
00248 }
00249 
00259 RTENTRY *NutIpRouteList(int *numEntries)
00260 {
00261     RTENTRY *rc;
00262     RTENTRY *rte;
00263 
00264     /* First count the number of entries. */
00265     *numEntries = 0;
00266     for (rte = rteList; rte; rte = rte->rt_next) {
00267         (*numEntries)++;
00268     }
00269 
00270     /* Allocate space for the result. */
00271     rc = (RTENTRY *) NutHeapAlloc(sizeof(RTENTRY) * (*numEntries));
00272 
00273     /* Fill in the result. */
00274     if (rc) {
00275         for (rte = rteList; rte; rc++, rte = rte->rt_next) {
00276             memcpy(rc, rte, sizeof(RTENTRY));
00277         }
00278     }
00279     else {
00280         *numEntries = 0;
00281     }
00282     return rc;
00283 }
00284 
00299 static RTENTRY *NutIpRouteRecQuery(u_long ip, u_long * gate, u_char level)
00300 {
00301     RTENTRY *rte = 0;
00302 
00303     if (level < 4) {
00304         for (rte = rteList; rte; rte = rte->rt_next) {
00305             if ((ip & rte->rt_mask) == rte->rt_ip)
00306                 break;
00307         }
00308         if (rte) {
00309             if (gate && rte->rt_gateway)
00310                 *gate = rte->rt_gateway;
00311             if (rte->rt_dev == 0)
00312                 rte = NutIpRouteRecQuery(rte->rt_gateway, gate, level + 1);
00313         }
00314     }
00315     return rte;
00316 }
00317 
00334 NUTDEVICE *NutIpRouteQuery(u_long ip, u_long * gate)
00335 {
00336     RTENTRY *rte;
00337 
00338     if (gate)
00339         *gate = 0;
00340     /* Return the first interface if the IP is broadcast. This solves the
00341        long existing problem with bad checksums on UDP broadcasts. Many
00342        thanks to Nicolas Moreau for this patch. */
00343     if (ip == 0xFFFFFFFF)
00344         rte = rteList;
00345     else
00346         rte = NutIpRouteRecQuery(ip, gate, 0);
00347 
00348     return rte ? rte->rt_dev : 0;
00349 }
00350 

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/