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: udpin.c,v $ 00096 * Revision 1.7 2008/04/18 13:13:11 haraldkipp 00097 * Using fast ints. 00098 * 00099 * Revision 1.6 2006/10/08 16:48:22 haraldkipp 00100 * Documentation fixed 00101 * 00102 * Revision 1.5 2005/06/05 16:48:32 haraldkipp 00103 * Additional parameter enables NutUdpInput() to avoid responding to UDP 00104 * broadcasts with ICMP unreachable messages. Fixes bug #1215192. 00105 * 00106 * Revision 1.4 2005/05/26 11:47:24 drsung 00107 * ICMP unreachable will be sent on incoming udp packets with no local peer port. 00108 * 00109 * Revision 1.3 2005/02/02 16:22:35 haraldkipp 00110 * Do not wake up waiting threads if the incoming datagram 00111 * doesn't fit in the buffer. 00112 * 00113 * Revision 1.2 2003/11/24 21:01:04 drsung 00114 * Packet queue added for UDP sockets. 00115 * 00116 * Revision 1.1.1.1 2003/05/09 14:41:45 haraldkipp 00117 * Initial using 3.2.1 00118 * 00119 * Revision 1.10 2003/02/04 18:14:57 harald 00120 * Version 3 released 00121 * 00122 * Revision 1.9 2002/06/26 17:29:36 harald 00123 * First pre-release with 2.4 stack 00124 * 00125 */ 00126 00127 #include <sys/event.h> 00128 00129 #include <netinet/udp.h> 00130 #include <sys/socket.h> 00131 #include <netinet/ip_icmp.h> 00132 #include <netinet/icmp.h> 00133 00138 00149 /* @@@ 2003-10-24: modified by OS for udp packet queue */ 00150 void NutUdpInput(NETBUF * nb, uint_fast8_t bcast) 00151 { 00152 UDPHDR *uh; 00153 UDPSOCKET *sock; 00154 00155 uh = (UDPHDR *) nb->nb_tp.vp; 00156 00157 nb->nb_ap.vp = uh + 1; 00158 nb->nb_ap.sz = nb->nb_tp.sz - sizeof(UDPHDR); 00159 nb->nb_tp.sz = sizeof(UDPHDR); 00160 00161 /* 00162 * Find a port. If none exists and if this datagram hasn't been 00163 * broadcasted, return an ICMP unreachable. 00164 */ 00165 if ((sock = NutUdpFindSocket(uh->uh_dport)) == 0) { 00166 if (bcast || NutIcmpResponse(ICMP_UNREACH, ICMP_UNREACH_PORT, 0, nb) == 0) { 00167 NutNetBufFree(nb); 00168 } 00169 return; 00170 } 00171 00172 /* if buffer size is defined, use packet queue */ 00173 if (sock->so_rx_bsz) { 00174 /* New packet fits into the buffer? */ 00175 if (sock->so_rx_cnt + nb->nb_ap.sz > sock->so_rx_bsz) { 00176 /* No, so discard it */ 00177 NutNetBufFree(nb); 00178 return; 00179 } else { 00180 /* if a first packet is already in the queue, find the end 00181 * and add the new packet */ 00182 if (sock->so_rx_nb) { 00183 NETBUF *snb; 00184 for (snb = sock->so_rx_nb; snb->nb_next != 0; snb = snb->nb_next); 00185 snb->nb_next = nb; 00186 } else 00187 sock->so_rx_nb = nb; 00188 00189 /* increment input buffer count */ 00190 sock->so_rx_cnt += nb->nb_ap.sz; 00191 }; 00192 } else { /* no packet queue */ 00193 /* if a packet is still buffered, discard it */ 00194 if (sock->so_rx_nb) { 00195 NutNetBufFree(sock->so_rx_nb); 00196 } 00197 sock->so_rx_nb = nb; 00198 sock->so_rx_cnt = nb->nb_ap.sz; /* set input buffer count to size of new packet */ 00199 }; 00200 00201 /* post the event only, if one thread is waiting */ 00202 if (sock->so_rx_rdy) 00203 NutEventPost(&sock->so_rx_rdy); 00204 } 00205