Nut/OS  4.10.3
API Reference
udpsock.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$
00096  * Revision 1.13  2009/02/22 12:37:26  olereinhardt
00097  * Added NutUdpError and NutUdpSetSocketError to set and retrieve socket
00098  * errors. As udp sockets aren't connection oriented those errors will be
00099  * anounced asynchronously on the next NutUdpSend or NutUdpReceive
00100  *
00101  * Include "include/errno.h" instead of "include/net/errno.h"
00102  *
00103  * Revision 1.12  2009/02/13 14:52:05  haraldkipp
00104  * Include memdebug.h for heap management debugging support.
00105  *
00106  * Revision 1.11  2009/02/06 15:40:29  haraldkipp
00107  * Using newly available strdup() and calloc().
00108  * Replaced NutHeap routines by standard malloc/free.
00109  * Replaced pointer value 0 by NULL.
00110  *
00111  * Revision 1.10  2008/08/20 06:57:00  haraldkipp
00112  * Implemented IP demultiplexer.
00113  *
00114  * Revision 1.9  2008/08/11 07:00:33  haraldkipp
00115  * BSD types replaced by stdint types (feature request #1282721).
00116  *
00117  * Revision 1.8  2008/04/18 13:32:00  haraldkipp
00118  * Changed size parameter from u_short to int, which is easier to handle
00119  * for 32-bit targets. You need to recompile your ARM code. No impact on
00120  * AVR expected
00121  * I changed u_int to int at some places to avoid some warnings during
00122  * compilation of Nut/Net.
00123  * libs.
00124  *
00125  * Revision 1.7  2005/08/02 17:47:03  haraldkipp
00126  * Major API documentation update.
00127  *
00128  * Revision 1.6  2005/04/08 15:20:51  olereinhardt
00129  * added <sys/types.h> (__APPLE__) and <netinet/in.h> (__linux__)
00130  * for htons and simmilar.
00131  *
00132  * Revision 1.5  2003/11/26 12:55:12  drsung
00133  * Portability issues ... again
00134  *
00135  * Revision 1.4  2003/11/24 21:01:04  drsung
00136  * Packet queue added for UDP sockets.
00137  *
00138  * Revision 1.3  2003/08/14 15:07:18  haraldkipp
00139  * Optimization
00140  *
00141  * Revision 1.2  2003/07/20 16:01:01  haraldkipp
00142  * Bugfix: Send crashed on routing problems.
00143  *
00144  * Revision 1.1.1.1  2003/05/09 14:41:46  haraldkipp
00145  * Initial using 3.2.1
00146  *
00147  * Revision 1.13  2003/05/06 18:21:52  harald
00148  * Memory hole fixed
00149  *
00150  * Revision 1.12  2003/04/21 17:04:57  harald
00151  * Bugfix: Return port number in host byte order
00152  *
00153  * Revision 1.11  2003/02/04 18:14:57  harald
00154  * Version 3 released
00155  *
00156  * Revision 1.10  2002/06/26 17:29:36  harald
00157  * First pre-release with 2.4 stack
00158  *
00159  */
00160 
00161 #include <sys/heap.h>
00162 #include <sys/event.h>
00163 #include <sys/types.h>
00164 #include <netinet/ip.h>
00165 #include <netinet/udp.h>
00166 #include <netinet/in.h>
00167 #include <sys/socket.h>
00168 
00169 #include <errno.h>
00170 #include <stdlib.h>
00171 #include <string.h>
00172 #include <memdebug.h>
00173 
00178 
00179 
00180 UDPSOCKET *udpSocketList;       
00181 static uint16_t last_local_port = 4096;  /* Unassigned local port. */
00182 static uint_fast8_t registered;
00183 
00195 UDPSOCKET *NutUdpCreateSocket(uint16_t port)
00196 {
00197     UDPSOCKET *sock;
00198 
00199     if (!registered) {
00200         if (NutRegisterIpHandler(IPPROTO_UDP, NutUdpInput)) {
00201             return NULL;
00202         }
00203         registered = 1;
00204     }
00205     if (port == 0) {
00206         do {
00207             if (++last_local_port == 0)
00208                 last_local_port = 4096;
00209             port = htons(last_local_port);
00210             sock = udpSocketList;
00211             while (sock) {
00212                 if (sock->so_local_port == port)
00213                     break;
00214                 sock = sock->so_next;
00215             }
00216         } while (sock);
00217         port = last_local_port;
00218     }
00219     if ((sock = calloc(1, sizeof(UDPSOCKET))) != 0) {
00220         sock->so_local_port = htons(port);
00221         sock->so_next = udpSocketList;
00222         udpSocketList = sock;
00223     }
00224     return sock;
00225 }
00226 
00241 int NutUdpSendTo(UDPSOCKET * sock, uint32_t addr, uint16_t port, void *data, int len)
00242 {
00243     int rc;
00244     NETBUF *nb;
00245 
00246 #ifdef NUT_UDP_ICMP_SUPPORT    
00247     if (sock->so_last_error)
00248         return -1;
00249 #endif
00250     
00251     if ((nb = NutNetBufAlloc(0, NBAF_APPLICATION, len)) == 0) {
00252         sock->so_last_error = ENOMEM;
00253         return -1;
00254     }
00255     memcpy(nb->nb_ap.vp, data, len);
00256 
00257     /* Bugfix by Ralph Mason. We should not free the NETBUF in case of an error. */
00258     if ((rc = NutUdpOutput(sock, addr, port, nb)) == 0)
00259         NutNetBufFree(nb);
00260 
00261     return rc;
00262 }
00263 
00281  /* @@@ 2003-10-24: modified by OS for udp packet queue */
00282 int NutUdpReceiveFrom(UDPSOCKET * sock, uint32_t * addr, uint16_t * port, void *data, int size, uint32_t timeout)
00283 {
00284     IPHDR *ip;
00285     UDPHDR *uh;
00286     NETBUF *nb;
00287 
00288 #ifdef NUT_UDP_ICMP_SUPPORT    
00289     /* The ICMP handler might have set an error condition. */
00290     if (sock->so_last_error)
00291         return -1;
00292 #endif
00293     
00294     if (sock->so_rx_nb == 0)
00295         NutEventWait(&sock->so_rx_rdy, timeout);
00296 
00297 #ifdef NUT_UDP_ICMP_SUPPORT    
00298     /* An ICMP message might have posted the rx event. So check again */
00299     if (sock->so_last_error)
00300         return -1;
00301 #endif
00302     
00303     if ((nb = sock->so_rx_nb) == 0)
00304         return 0;
00305 
00306     /* forward the queue's head to the next packet */
00307     sock->so_rx_nb = nb->nb_next;
00308 
00309     ip = nb->nb_nw.vp;
00310     *addr = ip->ip_src;
00311 
00312     uh = nb->nb_tp.vp;
00313     *port = htons(uh->uh_sport);
00314 
00315     if (size > nb->nb_ap.sz)
00316         size = nb->nb_ap.sz;
00317 
00318     sock->so_rx_cnt -= nb->nb_ap.sz;    /* decrement input buffer count */
00319 
00320     memcpy(data, nb->nb_ap.vp, size);
00321     NutNetBufFree(nb);
00322 
00323     return size;
00324 }
00325 
00338  /* @@@ 2003-10-24: modified by OS for udp packet queue */
00339 int NutUdpDestroySocket(UDPSOCKET * sock)
00340 {
00341     UDPSOCKET *sp;
00342     UDPSOCKET **spp;
00343     int rc = -1;
00344     NETBUF *nb;
00345 
00346     spp = &udpSocketList;
00347     sp = udpSocketList;
00348 
00349     while (sp) {
00350         if (sp == sock) {
00351             *spp = sock->so_next;
00352             /* packets may have arrived that the application 
00353                did not read before closing the socket. */
00354             while ((nb = sock->so_rx_nb) != 0) {
00355                 sock->so_rx_nb = nb->nb_next;
00356                 NutNetBufFree(nb);
00357             }
00358             free(sock);
00359             rc = 0;
00360             break;
00361         }
00362         spp = &sp->so_next;
00363         sp = sp->so_next;
00364     }
00365     return rc;
00366 }
00367 
00391 int NutUdpError(UDPSOCKET * sock, uint32_t * addr, uint16_t * port)
00392 {
00393     int rc;
00394     
00395     if (sock == 0) {
00396         addr = 0;
00397         port = 0;
00398         return ENOTSOCK;
00399     }
00400     
00401     if (sock->so_last_error) {
00402         rc = sock->so_last_error;
00403         *port = ntohs(sock->so_remote_port);
00404         *addr = sock->so_remote_addr;
00405 
00406         sock->so_last_error  = 0;
00407         sock->so_remote_port = 0;
00408         sock->so_remote_addr = 0;
00409         return rc;
00410     }     
00411     return 0;
00412 }
00413 
00425 UDPSOCKET *NutUdpFindSocket(uint16_t port)
00426 {
00427     UDPSOCKET *sp;
00428     UDPSOCKET *sock = 0;
00429 
00430     for (sp = udpSocketList; sp; sp = sp->so_next) {
00431         if (sp->so_local_port == port) {
00432             sock = sp;
00433             break;
00434         }
00435     }
00436     return sock;
00437 }
00438 
00453 int NutUdpSetSockOpt(UDPSOCKET * sock, int optname, CONST void *optval, int optlen)
00454 {
00455     int rc = -1;
00456 
00457     if (sock == 0)
00458         return -1;
00459     switch (optname) {
00460 
00461     case SO_RCVBUF:
00462         if (optval != 0 && optlen == sizeof(uint16_t)) {
00463             sock->so_rx_bsz = *((uint16_t *) optval);
00464             rc = 0;
00465         }
00466         break;
00467 
00468     default:
00469         /* sock->so_last_error = ENOPROTOOPT; */
00470         break;
00471     }
00472     return rc;
00473 }
00474 
00490 int NutUdpGetSockOpt(UDPSOCKET * sock, int optname, void *optval, int optlen)
00491 {
00492     int rc = -1;
00493 
00494     if (sock == 0)
00495         return -1;
00496     switch (optname) {
00497 
00498     case SO_RCVBUF:
00499         if (optval != 0 && optlen == sizeof(uint16_t)) {
00500             *((uint16_t *) optval) = sock->so_rx_bsz;
00501             rc = 0;
00502         }
00503         break;
00504 
00505     default:
00506         /* sock->so_last_error = ENOPROTOOPT; */
00507         break;
00508     }
00509     return rc;
00510 }
00511 
00530 int NutUdpSetSocketError(UDPSOCKET * sock, uint32_t remote_addr, uint16_t remote_port, uint16_t error)
00531 {
00532     if (sock == 0)
00533         return -1;
00534     
00535     sock->so_remote_addr = remote_addr;
00536     sock->so_remote_port = remote_port;
00537     sock->so_last_error  = error;
00538 
00539     /* post the event only, if a thread is waiting */
00540     if (sock->so_rx_rdy)
00541         NutEventPost(&sock->so_rx_rdy);
00542 
00543     return 0;
00544 }
00545 
00546