Nut/OS  4.10.3
API Reference
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 
00049 #include <stdlib.h>
00050 #include <memdebug.h>
00051 
00056 
00060 typedef struct _INET_PROTOCOLS INET_PROTOCOLS;
00061 
00062 struct _INET_PROTOCOLS {
00063     INET_PROTOCOLS *inet_next;
00064     uint8_t inet_prot;
00065     int (*inet_input)(NUTDEVICE *, NETBUF *);
00066 };
00067 
00068 static INET_PROTOCOLS *in_prots;
00069 
00080 static int NutIpDemux(NUTDEVICE * dev, NETBUF * nb)
00081 {
00082     INET_PROTOCOLS *inetp;
00083     uint8_t prot = ((IPHDR *)nb->nb_nw.vp)->ip_p;
00084 
00085     for (inetp = in_prots; inetp; inetp = inetp->inet_next) {
00086         if (prot == inetp->inet_prot && inetp->inet_input) {
00087             if ((*inetp->inet_input) (dev, nb) == 0) {
00088                 return 0;
00089             }
00090         }
00091     }
00092     return -1;
00093 }
00094 
00120 int NutRegisterIpHandler(uint8_t prot, int (*hdlr)(NUTDEVICE *, NETBUF *))
00121 {
00122     INET_PROTOCOLS *inetp;
00123 
00124     /* Check existing registrations. */
00125     for (inetp = in_prots; inetp; inetp = inetp->inet_next) {
00126         if (inetp->inet_prot == prot) {
00127             /* Found one. */
00128             break;
00129         }
00130     }
00131 
00132     if (inetp == NULL) {
00133         /* No existing entry. Allocate a new one. */
00134         inetp = calloc(1, sizeof(INET_PROTOCOLS));
00135         if (inetp == NULL) {
00136             return -1;
00137         }
00138         /* Set protocol type of our new entry. */
00139         inetp->inet_prot = prot;
00140         if (in_prots == NULL) {
00141             /* This is the first registration. Set the list root ... */
00142             in_prots = inetp;
00143             /* ... and enable our demultiplexer. */
00144             ip_demux = NutIpDemux;
00145         } else {
00146             /* Not the first registration. Insert new handler at the top. */
00147             inetp->inet_next = in_prots;
00148             in_prots = inetp;
00149         }
00150     }
00151     /* Finally set the handler function pointer of the new or existing 
00152     ** entry. */
00153     inetp->inet_input = hdlr;
00154 
00155     return 0;
00156 }
00157