tcpsock.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2005 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: tcpsock.c,v $
00096  * Revision 1.22  2008/08/20 06:57:00  haraldkipp
00097  * Implemented IP demultiplexer.
00098  *
00099  * Revision 1.21  2008/08/11 07:00:32  haraldkipp
00100  * BSD types replaced by stdint types (feature request #1282721).
00101  *
00102  * Revision 1.20  2008/04/18 13:32:00  haraldkipp
00103  * Changed size parameter from u_short to int, which is easier to handle
00104  * for 32-bit targets. You need to recompile your ARM code. No impact on
00105  * AVR expected
00106  * I changed u_int to int at some places to avoid some warnings during
00107  * compilation of Nut/Net.
00108  * libs.
00109  *
00110  * Revision 1.19  2006/10/08 16:48:22  haraldkipp
00111  * Documentation fixed
00112  *
00113  * Revision 1.18  2006/03/21 21:22:20  drsung
00114  * Enhancement made to TCP state machine. Now TCP options
00115  * are read from peer and at least the maximum segment size is stored.
00116  *
00117  * Revision 1.17  2005/10/24 11:00:16  haraldkipp
00118  * Integer division hack for ARM without CRT removed again.
00119  *
00120  * Revision 1.16  2005/08/02 17:47:03  haraldkipp
00121  * Major API documentation update.
00122  *
00123  * Revision 1.15  2005/04/30 16:42:42  chaac
00124  * Fixed bug in handling of NUTDEBUG. Added include for cfg/os.h. If NUTDEBUG
00125  * is defined in NutConf, it will make effect where it is used.
00126  *
00127  * Revision 1.14  2005/04/08 14:40:36  freckle
00128  * added <sys/types.h> (__APPLE__) and <netinet/in.h> (__linux__) for htons
00129  *
00130  * Revision 1.13  2005/04/05 17:58:02  haraldkipp
00131  * Avoid integer division on ARM platform as long as we run without crtlib.
00132  *
00133  * Revision 1.12  2005/02/04 17:16:45  haraldkipp
00134  * Stop searching when listening socket found
00135  *
00136  * Revision 1.11  2005/01/03 08:44:15  haraldkipp
00137  * Simplyfied NutTcpSend().
00138  *
00139  * Revision 1.10  2004/10/14 16:43:07  drsung
00140  * Fixed compiler warning "comparison between signed and unsigned"
00141  *
00142  * Revision 1.9  2004/07/30 19:54:46  drsung
00143  * Some code of TCP stack redesigned. Round trip time calculation is now
00144  * supported. Fixed several bugs in TCP state machine. Now TCP connections
00145  * should be more reliable under heavy traffic or poor physical connections.
00146  *
00147  * Revision 1.8  2004/03/16 16:48:45  haraldkipp
00148  * Added Jan Dubiec's H8/300 port.
00149  *
00150  * Revision 1.7  2004/01/28 12:11:57  drsung
00151  * Bugfix for ICCAVR ... again
00152  *
00153  * Revision 1.6  2004/01/28 07:11:38  drsung
00154  * Bugfix for ICCAVR
00155  *
00156  * Revision 1.5  2004/01/14 19:34:08  drsung
00157  * New TCP output buffer handling
00158  *
00159  * Revision 1.4  2003/11/04 17:58:18  haraldkipp
00160  * Removed volatile attibute from socket list
00161  *
00162  * Revision 1.3  2003/08/14 15:08:36  haraldkipp
00163  * Bugfix, TCP may reuse socket ports because of wrong byte order during compare.
00164  *
00165  * Revision 1.2  2003/07/13 19:23:59  haraldkipp
00166  * TCP transfer speed increased by changing the character receive buffer
00167  * in TCPSOCKET to a NETBUF queue.
00168  *
00169  * Revision 1.1.1.1  2003/05/09 14:41:44  haraldkipp
00170  * Initial using 3.2.1
00171  *
00172  * Revision 1.18  2003/02/04 18:14:57  harald
00173  * Version 3 released
00174  *
00175  * Revision 1.17  2002/09/15 17:07:05  harald
00176  * Allow application to read buffered data during close wait
00177  *
00178  * Revision 1.16  2002/09/03 17:43:15  harald
00179  * Configurable receive buffer size
00180  *
00181  * Revision 1.15  2002/08/16 17:54:57  harald
00182  * Count out of sequence drops
00183  *
00184  * Revision 1.14  2002/08/11 12:26:21  harald
00185  * ICC mods
00186  *
00187  * Revision 1.13  2002/06/26 17:29:36  harald
00188  * First pre-release with 2.4 stack
00189  *
00190  */
00191 
00192 #include <cfg/os.h>
00193 #include <sys/types.h>
00194 #include <string.h>
00195 
00196 #include <sys/atom.h>
00197 #include <sys/heap.h>
00198 #include <sys/thread.h>
00199 #include <sys/event.h>
00200 #include <sys/timer.h>
00201 
00202 #include <net/errno.h>
00203 #include <net/route.h>
00204 #include <netinet/in.h>
00205 #include <netinet/ip.h>
00206 #include <netinet/icmp.h>
00207 #include <netinet/ip_icmp.h>
00208 #include <netinet/ipcsum.h>
00209 #include <sys/socket.h>
00210 #include <netinet/tcp.h>
00211 #include <stdio.h>
00212 #include <io.h>
00213 
00214 #ifdef NUTDEBUG
00215 #include <net/netdebug.h>
00216 #endif
00217 
00218 #define TICK_RATE   1
00219 
00224 
00225 TCPSOCKET *tcpSocketList = 0;   
00227 static volatile uint16_t last_local_port = 4096; /* Unassigned local port. */
00228 
00229 static uint_fast8_t registered;
00230 
00231 void NutTcpDiscardBuffers(TCPSOCKET * sock)
00232 {
00233     NETBUF *nb;
00234     while ((nb = sock->so_rx_buf) != 0) {
00235         sock->so_rx_buf = nb->nb_next;
00236         NutNetBufFree(nb);
00237     }
00238     while ((nb = sock->so_tx_nbq) != 0) {
00239         sock->so_tx_nbq = nb->nb_next;
00240         NutNetBufFree(nb);
00241     }
00242     while ((nb = sock->so_rx_nbq) != 0) {
00243         sock->so_rx_nbq = nb->nb_next;
00244         NutNetBufFree(nb);
00245     }
00246 }
00247 
00248 
00260 void NutTcpDestroySocket(TCPSOCKET * sock)
00261 {
00262     TCPSOCKET *sp;
00263     TCPSOCKET *volatile *spp;
00264     
00265     //@@@printf ("[%04X] Calling destroy.\n", (u_short) sock);
00266 
00267     /*
00268      * Remove socket from the list.
00269      */
00270     sp = tcpSocketList;
00271     spp = &tcpSocketList;
00272     while (sp) {
00273         if (sp == sock) {
00274             *spp = sp->so_next;
00275             break;
00276         }
00277         spp = &sp->so_next;
00278         sp = sp->so_next;
00279     }
00280 
00281     /*
00282      * Free all memory occupied by the socket.
00283      */
00284     if (sp) {
00285         NutTcpDiscardBuffers(sock);
00286         if (sock->so_devocnt)
00287         {
00288             NutHeapFree(sock->so_devobuf);
00289             sock->so_devocnt = 0;
00290         }
00291         memset(sock, 0, sizeof(TCPSOCKET));
00292         NutHeapFree(sock);
00293     }
00294 }
00295 
00310 TCPSOCKET *NutTcpFindSocket(uint16_t lport, uint16_t rport, uint32_t raddr)
00311 {
00312     TCPSOCKET *sp;
00313     TCPSOCKET *sock = 0;
00314 
00315     /*
00316      * Try to find an exact match for the remote
00317      * address and port first.
00318      */
00319     for (sp = tcpSocketList; sp; sp = sp->so_next) {
00320         if (sp->so_local_port == lport) {
00321             if (sp->so_remote_addr == raddr && sp->so_remote_port == rport && sp->so_state != TCPS_CLOSED) {
00322                 sock = sp;
00323                 break;
00324             }
00325         }
00326     }
00327 
00328     /*
00329      * If no exact match exists, try a listening socket.
00330      * This part had been totally wrong, because it
00331      * didn't check the local port number and accepted
00332      * incoming requests on any port. Thanks to
00333      * Alejandro Lopez, who pointed this out.
00334      */
00335     if (sock == 0) {
00336         for (sp = tcpSocketList; sp; sp = sp->so_next) {
00337             if (sp->so_state == TCPS_LISTEN && sp->so_local_port == lport) {
00338                 sock = sp;
00339                 break;
00340             }
00341         }
00342     }
00343 
00344     return sock;
00345 }
00346 
00347 
00362 TCPSOCKET *NutTcpCreateSocket(void)
00363 {
00364     TCPSOCKET *sock = 0;
00365 
00366     if (!registered) {
00367         if (NutRegisterIpHandler(IPPROTO_TCP, NutTcpInput) || NutTcpInitStateMachine()) {
00368             return NULL;
00369         }
00370         registered = 1;
00371     }
00372     if ((sock = NutHeapAllocClear(sizeof(TCPSOCKET))) != 0) {
00373         sock->so_state = TCPS_CLOSED;
00374 
00375         /*
00376             * Initialize the virtual device interface.
00377             */
00378         sock->so_devtype = IFTYP_TCPSOCK;
00379         sock->so_devread = NutTcpDeviceRead;
00380         sock->so_devwrite = NutTcpDeviceWrite;
00381 #ifdef __HARVARD_ARCH__
00382         sock->so_devwrite_P = NutTcpDeviceWrite_P;
00383 #endif
00384         sock->so_devioctl = NutTcpDeviceIOCtl;
00385 
00386         sock->so_tx_isn = NutGetTickCount();  /* Generate the ISN from the nut_ticks counter */
00387         sock->so_tx_una = sock->so_tx_isn;
00388         sock->so_tx_nxt = sock->so_tx_isn;
00389         sock->so_rx_bsz = sock->so_rx_win = TCP_WINSIZE;
00390 
00391         sock->so_mss = TCP_MSS;
00392         sock->so_rtto = 1000; /* Initial retransmission time out */
00393 
00394         sock->so_next = tcpSocketList;
00395 
00396         sock->so_devobsz = TCP_MSS; /* Default output buffer size is TCP_MSS bytes */
00397 
00398         tcpSocketList = sock;
00399     }
00400     return sock;
00401 }
00402 
00422 int NutTcpSetSockOpt(TCPSOCKET * sock, int optname, CONST void *optval, int optlen)
00423 {
00424     int rc = -1;
00425 
00426     if (sock == 0)
00427         return -1;
00428     switch (optname) {
00429 
00430     case TCP_MAXSEG:
00431         if (optval == 0 || optlen != sizeof(uint16_t))
00432             sock->so_last_error = EINVAL;
00433         else if (sock->so_state != TCPS_CLOSED) 
00434             sock->so_last_error = EISCONN;
00435         else {
00436             sock->so_mss = *((uint16_t *) optval);
00437             rc = 0;
00438         }
00439         break;
00440 
00441     case SO_RCVBUF:
00442         if (optval == 0 || optlen != sizeof(uint16_t))
00443             sock->so_last_error = EINVAL;
00444         else {
00445             sock->so_rx_bsz = *((uint16_t *) optval);
00446             sock->so_rx_win = sock->so_rx_bsz;
00447             rc = 0;
00448         }
00449         break;
00450 
00451     case SO_SNDTIMEO:
00452         if (optval == 0 || optlen != sizeof(uint32_t))
00453             sock->so_last_error = EINVAL;
00454         else {
00455             sock->so_write_to = *((uint32_t *) optval);
00456             rc = 0;
00457         }
00458         break;
00459 
00460     case SO_RCVTIMEO:
00461         if (optval == 0 || optlen != sizeof(uint32_t))
00462             sock->so_last_error = EINVAL;
00463         else {
00464             sock->so_read_to = *((uint32_t *) optval);
00465             rc = 0;
00466         }
00467         break;
00468 
00469     case SO_SNDBUF:
00470         if (optval == 0 || optlen != sizeof(uint16_t))
00471             sock->so_last_error = EINVAL;
00472         else {
00473             NutTcpDeviceWrite(sock, 0, 0);
00474             sock->so_devobsz = *((uint16_t *) optval);
00475             rc = 0;
00476         }
00477         break;
00478 
00479     default:
00480         sock->so_last_error = ENOPROTOOPT;
00481         break;
00482     }
00483     return rc;
00484 }
00485 
00505 int NutTcpGetSockOpt(TCPSOCKET * sock, int optname, void *optval, int optlen)
00506 {
00507     int rc = -1;
00508 
00509     if (sock == 0)
00510         return -1;
00511     switch (optname) {
00512 
00513     case TCP_MAXSEG:
00514         if (optval == 0 || optlen != sizeof(uint16_t))
00515             sock->so_last_error = EINVAL;
00516         else {
00517             *((uint16_t *) optval) = sock->so_mss;
00518             rc = 0;
00519         }
00520         break;
00521 
00522     case SO_RCVBUF:
00523         if (optval == 0 || optlen != sizeof(uint16_t))
00524             sock->so_last_error = EINVAL;
00525         else {
00526             *((uint16_t *) optval) = sock->so_rx_bsz;
00527             rc = 0;
00528         }
00529         break;
00530 
00531     case SO_SNDTIMEO:
00532         if (optval == 0 || optlen != sizeof(uint32_t))
00533             sock->so_last_error = EINVAL;
00534         else {
00535             *((uint32_t *) optval) = sock->so_write_to;
00536             rc = 0;
00537         }
00538         break;
00539 
00540     case SO_RCVTIMEO:
00541         if (optval == 0 || optlen != sizeof(uint32_t))
00542             sock->so_last_error = EINVAL;
00543         else {
00544             *((uint32_t *) optval) = sock->so_read_to;
00545             rc = 0;
00546         }
00547         break;
00548 
00549     case SO_SNDBUF:
00550         if (optval == 0 || optlen != sizeof(uint16_t))
00551             sock->so_last_error = EINVAL;
00552         else {
00553             *((uint16_t *) optval) = sock->so_devobsz;
00554             rc = 0;
00555         }
00556         break;
00557     default:
00558         sock->so_last_error = ENOPROTOOPT;
00559         break;
00560     }
00561     return rc;
00562 }
00563 
00582 int NutTcpConnect(TCPSOCKET * sock, uint32_t addr, uint16_t port)
00583 {
00584     TCPSOCKET *sp;
00585     NUTDEVICE *dev;
00586 
00587     if (sock == 0)
00588         return -1;
00589     /*
00590      * Despite RFC793 we do not allow a passive
00591      * open to become active.
00592      */
00593     if (sock->so_state == TCPS_LISTEN) {
00594         sock->so_last_error = EOPNOTSUPP;
00595         return -1;
00596     } else if (sock->so_state != TCPS_CLOSED) {
00597         sock->so_last_error = EISCONN;
00598         return -1;
00599     }
00600 
00601     /*
00602      * Find an unused local port.
00603      */
00604     do {
00605         if (++last_local_port == 0)
00606             last_local_port = 4096;
00607 
00608         sp = tcpSocketList;
00609         while (sp) {
00610             /* Thanks to Ralph Mason for fixing the byte order bug. */
00611             if (sp->so_local_port == htons(last_local_port))
00612                 break;
00613             sp = sp->so_next;
00614         }
00615     } while (sp);
00616 
00617     /*
00618      * OK - we've got a new port. Now fill
00619      * remaining parts of the socket structure.
00620      */
00621     sock->so_local_port = htons(last_local_port);
00622     sock->so_remote_port = htons(port);
00623     sock->so_remote_addr = addr;
00624 
00625     /*
00626      * Get local address for this destination.
00627      */
00628     if ((dev = NutIpRouteQuery(addr, 0)) != 0) {
00629         IFNET *nif = dev->dev_icb;
00630         sock->so_local_addr = nif->if_local_ip;
00631     } else {
00632         sock->so_last_error = EHOSTUNREACH;
00633         return -1;
00634     }
00635 
00636     /*
00637      * Trigger active open event for the state machine.
00638      * This will suspend the thread until synchronized.
00639      */
00640     return NutTcpStateActiveOpenEvent(sock);
00641 }
00642 
00658 int NutTcpAccept(TCPSOCKET * sock, uint16_t port)
00659 {
00660     sock->so_local_port = htons(port);
00661 
00662     return NutTcpStatePassiveOpenEvent(sock);
00663 }
00664 
00681 int NutTcpSend(TCPSOCKET * sock, CONST void *data, int len)
00682 {
00683     uint16_t unacked;
00684 
00685     /*
00686      * Check parameters.
00687      */
00688     NutThreadYield();
00689     
00690     if (sock == 0)
00691         return -1;
00692     if (data == 0 || len == 0)
00693         return 0;
00694 
00695     /*
00696      * Limit the transmission size to our maximum segment size.
00697      */
00698     if (len > sock->so_mss)
00699         len = sock->so_mss;
00700 
00701     for (;;) {
00702         /*
00703          * We can only send on an established connection.
00704          */
00705         if (sock->so_state != TCPS_ESTABLISHED) {
00706             sock->so_last_error = ENOTCONN;
00707             return -1;
00708         }
00709 
00710         /*
00711          * Limit the size of unacknowledged data to four full segments.
00712          * Also wait for peer's window open wide enough to take all our 
00713          * data. This also avoids silly window syndrome on our side.
00714          */
00715         unacked = sock->so_tx_nxt - sock->so_tx_una;
00716         if ((unacked >> 2) < sock->so_mss && len <= sock->so_tx_win - unacked) {
00717             break;
00718         }
00719         if (NutEventWait(&sock->so_tx_tq, sock->so_write_to)) {
00720             return 0;
00721         }
00722     }
00723     /*
00724      * The segment will be automatically retransmitted if not 
00725      * acknowledged in time. If this returns an error, it's a 
00726      * fatal one.
00727      */
00728     sock->so_tx_flags |= SO_ACK;
00729     if (NutTcpOutput(sock, data, len))
00730         return -1;
00731     return len;
00732 }
00733 
00751 int NutTcpReceive(TCPSOCKET * sock, void *data, int size)
00752 {
00753     int i;
00754 
00755     NutThreadYield();
00756     /*
00757      * Check parameters.
00758      */
00759     if (sock == 0)
00760         return -1;
00761     if (sock->so_state != TCPS_ESTABLISHED && sock->so_state != TCPS_CLOSE_WAIT) {
00762         sock->so_last_error = ENOTCONN;
00763         return -1;
00764     }
00765     if (data == 0 || size == 0)
00766         return 0;
00767 
00768     /*
00769      * Wait until any data arrived, a timeout occurs
00770      * or the connection terminates.
00771      */
00772     while (sock->so_rx_cnt - sock->so_rd_cnt == 0) {
00773         if (sock->so_state != TCPS_ESTABLISHED) {
00774             sock->so_last_error = ENOTCONN;
00775             return -1;
00776         }
00777         if (NutEventWait(&sock->so_rx_tq, sock->so_read_to))
00778             return 0;
00779     }
00780 
00781     if (size > sock->so_rx_cnt - sock->so_rd_cnt)
00782         size = sock->so_rx_cnt - sock->so_rd_cnt;
00783     if (size) {
00784         NETBUF *nb;
00785         uint16_t rd_cnt;         /* Bytes read from NETBUF. */
00786         uint16_t nb_cnt;         /* Bytes left in NETBUF. */
00787         uint16_t ab_cnt;         /* Total bytes in app buffer. */
00788         uint16_t mv_cnt;         /* Bytes to move to app buffer. */
00789 
00790         rd_cnt = sock->so_rd_cnt;
00791 
00792         ab_cnt = 0;
00793         while (ab_cnt < size) {
00794             nb = sock->so_rx_buf;
00795             nb_cnt = nb->nb_ap.sz - rd_cnt;
00796             mv_cnt = size - ab_cnt;
00797             if (mv_cnt > nb_cnt)
00798                 mv_cnt = nb_cnt;
00799             memcpy((char *) data + ab_cnt, (char *) (nb->nb_ap.vp) + rd_cnt, mv_cnt);
00800             ab_cnt += mv_cnt;
00801             rd_cnt += mv_cnt;
00802             if (mv_cnt >= nb_cnt) {
00803                 sock->so_rx_buf = nb->nb_next;
00804                 sock->so_rx_cnt -= rd_cnt;
00805                 NutNetBufFree(nb);
00806                 nb = sock->so_rx_buf;
00807                 rd_cnt = 0;
00808             }
00809         }
00810         sock->so_rd_cnt = rd_cnt;
00811 
00812         /*
00813          * Update our receive window.
00814          */
00815         if (sock->so_state == TCPS_ESTABLISHED) {
00816             i = sock->so_rx_win;
00817             if ((i += size) > sock->so_rx_bsz)
00818                 i = sock->so_rx_bsz;
00819 
00820             if (sock->so_rx_win <= sock->so_mss && i > sock->so_mss) {
00821                 sock->so_rx_win = i;
00822                 NutTcpStateWindowEvent(sock);
00823             } else {
00824                 sock->so_rx_win = i;
00825             }
00826         }
00827     }
00828     return size;
00829 }
00830 
00843 int NutTcpCloseSocket(TCPSOCKET * sock)
00844 {
00845     /* Flush buffer first */
00846     //@@@printf ("[%04X] Calling close\n", (u_short) sock);
00847     NutTcpDeviceWrite(sock, 0, 0);
00848     return NutTcpStateCloseEvent(sock);
00849 }
00850 
00899 int NutTcpError(TCPSOCKET * sock)
00900 {
00901     if (sock == 0)
00902         return ENOTSOCK;
00903     return sock->so_last_error;
00904 }
00905 
00925 int NutTcpDeviceRead(TCPSOCKET * sock, void *buffer, int size)
00926 {
00927     return NutTcpReceive(sock, buffer, size);
00928 }
00929 
00930 static int SendBuffer(TCPSOCKET * sock, CONST void *buffer, int size)
00931 {
00932     int rc;
00933     int bite;
00934 
00935     for (rc = 0; rc < size; rc += bite) {
00936         if ((bite = NutTcpSend(sock, (uint8_t *) buffer + rc, size - rc)) <= 0) {
00937             return -1;
00938         }
00939     }
00940     return rc;
00941 }
00942 
00965 int NutTcpDeviceWrite(TCPSOCKET * sock, CONST void *buf, int size)
00966 {
00967     int rc;
00968     uint16_t sz;
00969     /* hack alert for ICCAVR */
00970     uint8_t *buffer = (uint8_t*) buf;
00971 
00972     /*
00973      * Check parameters.
00974      */
00975     if (sock == 0)
00976         return -1;
00977     if (sock->so_state != TCPS_ESTABLISHED) {
00978         sock->so_last_error = ENOTCONN;
00979         return -1;
00980     }
00981 
00982     /* Flush buffer? */
00983     if (size == 0) {
00984         if (sock->so_devocnt) {
00985             if (SendBuffer(sock, sock->so_devobuf, sock->so_devocnt) < 0) {
00986                 NutHeapFree(sock->so_devobuf);
00987                 sock->so_devocnt = 0;
00988                 return -1;
00989             }
00990             NutHeapFree(sock->so_devobuf);
00991             sock->so_devocnt = 0;
00992         }
00993         return 0;
00994     }
00995 
00996     /* If we don't have a buffer so far... */
00997     if (sock->so_devocnt == 0) {
00998         /* If new data block is bigger or equal than buffer size
00999          * send first part of data to nic and store remaining
01000          * bytes in buffer
01001          */
01002         if ((uint16_t) size >= sock->so_devobsz) {
01003             rc = size % sock->so_devobsz;
01004             if (SendBuffer(sock, buffer, size - rc) < 0)
01005                 return -1;
01006             buffer += size - rc;
01007         } else
01008             rc = size;
01009 
01010         /* If there are some remainings bytes, allocate buffer
01011          * and store them
01012          */
01013         if (rc) {
01014             if (!(sock->so_devobuf = NutHeapAlloc(sock->so_devobsz)))
01015                 return -1;
01016             memcpy(sock->so_devobuf, buffer, rc);
01017             sock->so_devocnt = rc;
01018         }
01019         return size;
01020     }
01021 
01022     /* Check if new data fully fits in output buffer */
01023     if (sock->so_devocnt + size < sock->so_devobsz) {
01024         memcpy(sock->so_devobuf + sock->so_devocnt, buffer, size);
01025         sock->so_devocnt += size;
01026         return size;
01027     }
01028 
01029     /* Otherwise store first bytes of new data in buffer and flush
01030      * the buffer
01031      */
01032     sz = sock->so_devobsz - sock->so_devocnt;
01033     memcpy(sock->so_devobuf + sock->so_devocnt, buffer, sz);
01034     buffer += sz;
01035     if (SendBuffer(sock, sock->so_devobuf, sock->so_devobsz) < 0) {
01036         NutHeapFree(sock->so_devobuf);
01037         sock->so_devocnt = 0;
01038         return -1;
01039     }
01040 
01041     /* If remaining data is bigger or equal than buffer size
01042      * send first part of data to nic and later store remaining
01043      * bytes in buffer
01044      */
01045     sz = size - sz;
01046     if (sz >= sock->so_devobsz) {
01047         rc = size % sock->so_devobsz;
01048         if (SendBuffer(sock, buffer, sz - rc) < 0) {
01049             NutHeapFree(sock->so_devobuf);
01050             sock->so_devocnt = 0;
01051             return -1;
01052         }
01053         buffer += sz - rc;
01054     } else
01055         rc = sz;
01056 
01057     /* If there are some remainings bytes, store them in buffer
01058      */
01059     if (rc)
01060         memcpy(sock->so_devobuf, buffer, rc);
01061     else                        /* Otherwise free buffer */
01062         NutHeapFree(sock->so_devobuf);
01063     sock->so_devocnt = rc;
01064 
01065     return size;
01066 }
01067 
01092 #ifdef __HARVARD_ARCH__
01093 int NutTcpDeviceWrite_P(TCPSOCKET * sock, PGM_P buffer, int size)
01094 {
01095     int rc;
01096     char *rp = 0;
01097 
01098     /*
01099      * Hack alert. Neither do we handle out of memory correctly
01100      * nor do we pass the PGM pointer to lower levels.
01101      */
01102     if (size && (rp = NutHeapAlloc(size)) != 0)
01103         memcpy_P(rp, buffer, size);
01104     rc = NutTcpDeviceWrite(sock, rp, size);
01105     if (rp)
01106         NutHeapFree(rp);
01107 
01108     return rc;
01109 }
01110 #endif
01111 
01131 int NutTcpDeviceIOCtl(TCPSOCKET * sock, int cmd, void *param)
01132 {
01133     uint32_t *lvp = (uint32_t *) param;
01134     int rc = 0;
01135     
01136     switch (cmd) {
01137     case IOCTL_GETFILESIZE:
01138     case IOCTL_GETINBUFCOUNT:
01139         *lvp = (sock->so_rx_cnt - sock->so_rd_cnt);
01140         break;
01141     case IOCTL_GETOUTBUFCOUNT:
01142         *lvp = (sock->so_devocnt);
01143         break;
01144     default:
01145         rc = -1;
01146     }
01147     
01148     return rc;    
01149 }
01150 

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