00001 /*
00002 * Copyright (C) 2001-2002 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. All advertising materials mentioning features or use of this
00014 * software must display the following acknowledgement:
00015 *
00016 * This product includes software developed by egnite Software GmbH
00017 * and its contributors.
00018 *
00019 * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH 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 EGNITE
00023 * SOFTWARE GMBH 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 * -
00035 * Portions Copyright (c) 1983, 1993 by
00036 * The Regents of the University of California. All rights reserved.
00037 *
00038 * Redistribution and use in source and binary forms, with or without
00039 * modification, are permitted provided that the following conditions
00040 * are met:
00041 * 1. Redistributions of source code must retain the above copyright
00042 * notice, this list of conditions and the following disclaimer.
00043 * 2. Redistributions in binary form must reproduce the above copyright
00044 * notice, this list of conditions and the following disclaimer in the
00045 * documentation and/or other materials provided with the distribution.
00046 * 3. All advertising materials mentioning features or use of this software
00047 * must display the following acknowledgement:
00048 * This product includes software developed by the University of
00049 * California, Berkeley and its contributors.
00050 * 4. Neither the name of the University nor the names of its contributors
00051 * may be used to endorse or promote products derived from this software
00052 * without specific prior written permission.
00053 *
00054 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00055 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00056 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00057 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00058 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00059 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00060 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00061 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00062 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00063 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00064 * SUCH DAMAGE.
00065 * -
00066 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
00067 *
00068 * Permission to use, copy, modify, and distribute this software for any
00069 * purpose with or without fee is hereby granted, provided that the above
00070 * copyright notice and this permission notice appear in all copies, and that
00071 * the name of Digital Equipment Corporation not be used in advertising or
00072 * publicity pertaining to distribution of the document or software without
00073 * specific, written prior permission.
00074 *
00075 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
00076 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
00077 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
00078 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
00079 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
00080 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
00081 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
00082 * SOFTWARE.
00083 */
00084
00085 /*
00086 * $Log: udp.c,v $
00087 * Revision 1.1 2002/08/01 17:34:30 harald
00088 * First check in
00089 *
00090 */
00091
00092 #include "eboot.h"
00093
00094 /*!
00095 * \addtogroup xgStack
00096 */
00097 /*@{*/
00098
00099 /*!
00100 * \brief Receive an UDP datagram on a specified port.
00101 *
00102 * This function calls IpInput(). Any incoming Ethernet
00103 * frame, which is not an UDP datagram to the specified
00104 * port will be discarded.
00105 *
00106 * \param port UDP port to listen to.
00107 * \param tms Return with timeout after the specified
00108 * number of waiting loops. On a 14 Mhz ATmega
00109 * this value represents approximately the number
00110 * of milliseconds to wait.
00111 *
00112 * \return The number of bytes received, 0 on timeout
00113 * or -1 in case of a failure.
00114 */
00115 int UdpInput(u_short port, u_short tms)
00116 {
00117 int rc = 0;
00118 UDPHDR *uh = &rframe.udp_hdr;
00119
00120 for(;;) {
00121 if((rc = IpInput(IPPROTO_UDP, tms)) == 0) {
00122 break;
00123 }
00124
00125 /*
00126 * Check the requested port.
00127 */
00128 if(port == ntohs(uh->uh_dport)) {
00129 rc = ntohs(uh->uh_ulen) - sizeof(UDPHDR);
00130 break;
00131 }
00132 }
00133 return rc;
00134 }
00135
00136 /*!
00137 * \brief Send an UDP datagram.
00138 *
00139 * This function fills the UDP header of the global send frame
00140 * and calls IpOutput().
00141 *
00142 * \param dip Destination IP address in network byte order.
00143 * \param dport Destination port.
00144 * \param sport Source port.
00145 * \param len Number of data bytes to transmit.
00146 *
00147 * \return 0 on success, -1 otherwise.
00148 */
00149 int UdpOutput(u_long dip, u_short dport, u_short sport, u_short len)
00150 {
00151 UDPHDR *up = &sframe.udp_hdr;
00152
00153 up->uh_sport = htons(sport);
00154 up->uh_dport = htons(dport);
00155 up->uh_ulen = htons(sizeof(UDPHDR) + len);
00156
00157 return IpOutput(dip, IPPROTO_UDP, sizeof(UDPHDR) + len);
00158 }
00159
00160 /*@}*/