netbuf.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: netbuf.c,v $
00096  * Revision 1.3  2005/04/30 16:42:41  chaac
00097  * Fixed bug in handling of NUTDEBUG. Added include for cfg/os.h. If NUTDEBUG
00098  * is defined in NutConf, it will make effect where it is used.
00099  *
00100  * Revision 1.2  2004/01/16 07:51:43  drsung
00101  * Bugfix for reallocating smaller network buffers. Thx to Mike Cornelius.
00102  *
00103  * Revision 1.1.1.1  2003/05/09 14:40:46  haraldkipp
00104  * Initial using 3.2.1
00105  *
00106  * Revision 1.12  2003/03/31 14:53:07  harald
00107  * Prepare release 3.1
00108  *
00109  * Revision 1.11  2003/02/04 17:50:54  harald
00110  * Version 3 released
00111  *
00112  * Revision 1.10  2002/06/26 17:29:08  harald
00113  * First pre-release with 2.4 stack
00114  *
00115  */
00116 
00117 #include <cfg/os.h>
00118 #include <string.h>
00119 
00120 #include <sys/heap.h>
00121 #include <dev/netbuf.h>
00122 
00123 #ifdef NUTDEBUG
00124 #include <sys/osdebug.h>
00125 #endif
00126 
00131 
00132 static int NutNetBufAllocData(NBDATA * nbd, u_short size)
00133 {
00134     if ((nbd->vp = NutHeapAlloc(size)) == 0) {
00135         nbd->sz = 0;
00136         return -1;
00137     }
00138     nbd->sz = size;
00139     return 0;
00140 }
00141 
00142 static void NutNetBufFreeData(NBDATA * nbd)
00143 {
00144     NutHeapFree(nbd->vp);
00145     nbd->vp = 0;
00146     nbd->sz = 0;
00147 }
00148 
00168 NETBUF *NutNetBufAlloc(NETBUF * nb, u_char type, u_short size)
00169 {
00170     if (nb == 0) {
00171         nb = NutHeapAllocClear(sizeof(NETBUF));
00172     }
00173 
00174     if (nb && type) {
00175         if (size) {
00176             if (type & NBAF_DATALINK) {
00177                 if (nb->nb_flags & NBAF_DATALINK) {
00178                     if (nb->nb_dl.sz < size) {
00179                         NutNetBufFreeData(&nb->nb_dl);
00180                         if (NutNetBufAllocData(&nb->nb_dl, size)) {
00181                             NutNetBufFree(nb);
00182                             return 0;
00183                         }
00184                     } else
00185                         nb->nb_dl.sz = size;
00186                 } else if (NutNetBufAllocData(&nb->nb_dl, size)) {
00187                     NutNetBufFree(nb);
00188                     return 0;
00189                 }
00190             }
00191 
00192             if (type & NBAF_NETWORK) {
00193                 if (nb->nb_flags & NBAF_NETWORK) {
00194                     if (nb->nb_nw.sz < size) {
00195                         NutNetBufFreeData(&nb->nb_nw);
00196                         if (NutNetBufAllocData(&nb->nb_nw, size)) {
00197                             NutNetBufFree(nb);
00198                             return 0;
00199                         }
00200                     } else
00201                         nb->nb_nw.sz = size;
00202                 } else if (NutNetBufAllocData(&nb->nb_nw, size)) {
00203                     NutNetBufFree(nb);
00204                     return 0;
00205                 }
00206             }
00207 
00208             if (type & NBAF_TRANSPORT) {
00209                 if (nb->nb_flags & NBAF_TRANSPORT) {
00210                     if (nb->nb_tp.sz < size) {
00211                         NutNetBufFreeData(&nb->nb_tp);
00212                         if (NutNetBufAllocData(&nb->nb_tp, size)) {
00213                             NutNetBufFree(nb);
00214                             return 0;
00215                         }
00216                     } else
00217                         nb->nb_tp.sz = size;
00218                 } else if (NutNetBufAllocData(&nb->nb_tp, size)) {
00219                     NutNetBufFree(nb);
00220                     return 0;
00221                 }
00222             }
00223 
00224             if (type & NBAF_APPLICATION) {
00225                 if (nb->nb_flags & NBAF_APPLICATION) {
00226                     if (nb->nb_ap.sz < size) {
00227                         NutNetBufFreeData(&nb->nb_ap);
00228                         if (NutNetBufAllocData(&nb->nb_ap, size)) {
00229                             NutNetBufFree(nb);
00230                             return 0;
00231                         }
00232                     } else
00233                         nb->nb_ap.sz = size;
00234                 } else if (NutNetBufAllocData(&nb->nb_ap, size)) {
00235                     NutNetBufFree(nb);
00236                     return 0;
00237                 }
00238             }
00239             nb->nb_flags |= type;
00240         } else {
00241             type &= nb->nb_flags;
00242             if (type & NBAF_DATALINK)
00243                 NutNetBufFreeData(&nb->nb_dl);
00244             if (type & NBAF_NETWORK)
00245                 NutNetBufFreeData(&nb->nb_nw);
00246             if (type & NBAF_TRANSPORT)
00247                 NutNetBufFreeData(&nb->nb_tp);
00248             if (type & NBAF_APPLICATION)
00249                 NutNetBufFreeData(&nb->nb_ap);
00250             nb->nb_flags &= ~type;
00251         }
00252     }
00253     return nb;
00254 }
00255 
00266 NETBUF *NutNetBufClone(NETBUF * nb)
00267 {
00268     NETBUF *clone;
00269 
00270     if ((clone = NutHeapAllocClear(sizeof(NETBUF))) == 0)
00271         return 0;
00272 
00273     if (nb->nb_dl.sz) {
00274         if (NutNetBufAllocData(&clone->nb_dl, nb->nb_dl.sz)) {
00275             NutNetBufFree(clone);
00276             return 0;
00277         }
00278         memcpy(clone->nb_dl.vp, nb->nb_dl.vp, nb->nb_dl.sz);
00279         clone->nb_flags |= NBAF_DATALINK;
00280     }
00281     if (nb->nb_nw.sz) {
00282         if (NutNetBufAllocData(&clone->nb_nw, nb->nb_nw.sz)) {
00283             NutNetBufFree(clone);
00284             return 0;
00285         }
00286         memcpy(clone->nb_nw.vp, nb->nb_nw.vp, nb->nb_nw.sz);
00287         clone->nb_flags |= NBAF_NETWORK;
00288     }
00289     if (nb->nb_tp.sz) {
00290         if (NutNetBufAllocData(&clone->nb_tp, nb->nb_tp.sz)) {
00291             NutNetBufFree(clone);
00292             return 0;
00293         }
00294         memcpy(clone->nb_tp.vp, nb->nb_tp.vp, nb->nb_tp.sz);
00295         clone->nb_flags |= NBAF_TRANSPORT;
00296     }
00297     if (nb->nb_ap.sz) {
00298         if (NutNetBufAllocData(&clone->nb_ap, nb->nb_ap.sz)) {
00299             NutNetBufFree(clone);
00300             return 0;
00301         }
00302         memcpy(clone->nb_ap.vp, nb->nb_ap.vp, nb->nb_ap.sz);
00303         clone->nb_flags |= NBAF_APPLICATION;
00304     }
00305     return clone;
00306 }
00307 
00321 int NutNetBufFree(NETBUF * nb)
00322 {
00323     if(nb) {
00324         NutNetBufAlloc(nb, nb->nb_flags, 0);
00325         if (NutHeapFree(nb)) {
00326             return -1;
00327         }
00328     }
00329     return 0;
00330 }
00331 

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