ipdemux.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008 by egnite GmbH.
00003  *
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  *
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  * 2. Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in the
00014  *    documentation and/or other materials provided with the distribution.
00015  * 3. Neither the name of the copyright holders nor the names of
00016  *    contributors may be used to endorse or promote products derived
00017  *    from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00022  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00023  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00024  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00025  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00026  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00027  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00028  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00029  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00030  * SUCH DAMAGE.
00031  *
00032  * For additional information see http://www.ethernut.de/
00033  */
00034 
00044 #include <sys/types.h>
00045 #include <sys/heap.h>
00046 #include <netinet/if_ether.h>
00047 #include <netinet/ip.h>
00048 
00053 
00057 typedef struct _INET_PROTOCOLS INET_PROTOCOLS;
00058 
00059 struct _INET_PROTOCOLS {
00060     INET_PROTOCOLS *inet_next;
00061     uint8_t inet_prot;
00062     int (*inet_input)(NUTDEVICE *, NETBUF *);
00063 };
00064 
00065 static INET_PROTOCOLS *in_prots;
00066 
00077 static int NutIpDemux(NUTDEVICE * dev, NETBUF * nb)
00078 {
00079     INET_PROTOCOLS *inetp;
00080     uint8_t prot = ((IPHDR *)nb->nb_nw.vp)->ip_p;
00081 
00082     for (inetp = in_prots; inetp; inetp = inetp->inet_next) {
00083         if (prot == inetp->inet_prot && inetp->inet_input) {
00084             if ((*inetp->inet_input) (dev, nb) == 0) {
00085                 return 0;
00086             }
00087         }
00088     }
00089     return -1;
00090 }
00091 
00117 int NutRegisterIpHandler(uint8_t prot, int (*hdlr)(NUTDEVICE *, NETBUF *))
00118 {
00119     INET_PROTOCOLS *inetp;
00120 
00121     /* Check existing registrations. */
00122     for (inetp = in_prots; inetp; inetp = inetp->inet_next) {
00123         if (inetp->inet_prot == prot) {
00124             /* Found one. */
00125             break;
00126         }
00127     }
00128 
00129     if (inetp == NULL) {
00130         /* No existing entry. Allocate a new one. */
00131         inetp = NutHeapAllocClear(sizeof(INET_PROTOCOLS));
00132         if (inetp == NULL) {
00133             return -1;
00134         }
00135         /* Set protocol type of our new entry. */
00136         inetp->inet_prot = prot;
00137         if (in_prots == NULL) {
00138             /* This is the first registration. Set the list root ... */
00139             in_prots = inetp;
00140             /* ... and enable our demultiplexer. */
00141             ip_demux = NutIpDemux;
00142         } else {
00143             /* Not the first registration. Insert new handler at the top. */
00144             inetp->inet_next = in_prots;
00145             in_prots = inetp;
00146         }
00147     }
00148     /* Finally set the handler function pointer of the new or existing 
00149     ** entry. */
00150     inetp->inet_input = hdlr;
00151 
00152     return 0;
00153 }
00154 

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