Nut/OS  4.10.3
API Reference
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$
00096  * Revision 1.25  2009/02/22 12:31:52  olereinhardt
00097  * Include "include/errno.h" instead of "include/net/errno.h"
00098  *
00099  * Revision 1.24  2009/02/13 14:52:05  haraldkipp
00100  * Include memdebug.h for heap management debugging support.
00101  *
00102  * Revision 1.23  2009/02/06 15:40:29  haraldkipp
00103  * Using newly available strdup() and calloc().
00104  * Replaced NutHeap routines by standard malloc/free.
00105  * Replaced pointer value 0 by NULL.
00106  *
00107  * Revision 1.22  2008/08/20 06:57:00  haraldkipp
00108  * Implemented IP demultiplexer.
00109  *
00110  * Revision 1.21  2008/08/11 07:00:32  haraldkipp
00111  * BSD types replaced by stdint types (feature request #1282721).
00112  *
00113  * Revision 1.20  2008/04/18 13:32:00  haraldkipp
00114  * Changed size parameter from u_short to int, which is easier to handle
00115  * for 32-bit targets. You need to recompile your ARM code. No impact on
00116  * AVR expected
00117  * I changed u_int to int at some places to avoid some warnings during
00118  * compilation of Nut/Net.
00119  * libs.
00120  *
00121  * Revision 1.19  2006/10/08 16:48:22  haraldkipp
00122  * Documentation fixed
00123  *
00124  * Revision 1.18  2006/03/21 21:22:20  drsung
00125  * Enhancement made to TCP state machine. Now TCP options
00126  * are read from peer and at least the maximum segment size is stored.
00127  *
00128  * Revision 1.17  2005/10/24 11:00:16  haraldkipp
00129  * Integer division hack for ARM without CRT removed again.
00130  *
00131  * Revision 1.16  2005/08/02 17:47:03  haraldkipp
00132  * Major API documentation update.
00133  *
00134  * Revision 1.15  2005/04/30 16:42:42  chaac
00135  * Fixed bug in handling of NUTDEBUG. Added include for cfg/os.h. If NUTDEBUG
00136  * is defined in NutConf, it will make effect where it is used.
00137  *
00138  * Revision 1.14  2005/04/08 14:40:36  freckle
00139  * added <sys/types.h> (__APPLE__) and <netinet/in.h> (__linux__) for htons
00140  *
00141  * Revision 1.13  2005/04/05 17:58:02  haraldkipp
00142  * Avoid integer division on ARM platform as long as we run without crtlib.
00143  *
00144  * Revision 1.12  2005/02/04 17:16:45  haraldkipp
00145  * Stop searching when listening socket found
00146  *
00147  * Revision 1.11  2005/01/03 08:44:15  haraldkipp
00148  * Simplyfied NutTcpSend().
00149  *
00150  * Revision 1.10  2004/10/14 16:43:07  drsung
00151  * Fixed compiler warning "comparison between signed and unsigned"
00152  *
00153  * Revision 1.9  2004/07/30 19:54:46  drsung
00154  * Some code of TCP stack redesigned. Round trip time calculation is now
00155  * supported. Fixed several bugs in TCP state machine. Now TCP connections
00156  * should be more reliable under heavy traffic or poor physical connections.
00157  *
00158  * Revision 1.8  2004/03/16 16:48:45  haraldkipp
00159  * Added Jan Dubiec's H8/300 port.
00160  *
00161  * Revision 1.7  2004/01/28 12:11:57  drsung
00162  * Bugfix for ICCAVR ... again
00163  *
00164  * Revision 1.6  2004/01/28 07:11:38  drsung
00165  * Bugfix for ICCAVR
00166  *
00167  * Revision 1.5  2004/01/14 19:34:08  drsung
00168  * New TCP output buffer handling
00169  *
00170  * Revision 1.4  2003/11/04 17:58:18  haraldkipp
00171  * Removed volatile attibute from socket list
00172  *
00173  * Revision 1.3  2003/08/14 15:08:36  haraldkipp
00174  * Bugfix, TCP may reuse socket ports because of wrong byte order during compare.
00175  *
00176  * Revision 1.2  2003/07/13 19:23:59  haraldkipp
00177  * TCP transfer speed increased by changing the character receive buffer
00178  * in TCPSOCKET to a NETBUF queue.
00179  *
00180  * Revision 1.1.1.1  2003/05/09 14:41:44  haraldkipp
00181  * Initial using 3.2.1
00182  *
00183  * Revision 1.18  2003/02/04 18:14:57  harald
00184  * Version 3 released
00185  *
00186  * Revision 1.17  2002/09/15 17:07:05  harald
00187  * Allow application to read buffered data during close wait
00188  *
00189  * Revision 1.16  2002/09/03 17:43:15  harald
00190  * Configurable receive buffer size
00191  *
00192  * Revision 1.15  2002/08/16 17:54:57  harald
00193  * Count out of sequence drops
00194  *
00195  * Revision 1.14  2002/08/11 12:26:21  harald
00196  * ICC mods
00197  *
00198  * Revision 1.13  2002/06/26 17:29:36  harald
00199  * First pre-release with 2.4 stack
00200  *
00201  */
00202 
00203 #include <cfg/os.h>
00204 #include <sys/types.h>
00205 #include <string.h>
00206 #include <stdlib.h>
00207 #include <errno.h>
00208 
00209 #include <sys/atom.h>
00210 #include <sys/heap.h>
00211 #include <sys/thread.h>
00212 #include <sys/event.h>
00213 #include <sys/timer.h>
00214 
00215 #include <net/route.h>
00216 #include <netinet/in.h>
00217 #include <netinet/ip.h>
00218 #include <netinet/icmp.h>
00219 #include <netinet/ip_icmp.h>
00220 #include <netinet/ipcsum.h>
00221 #include <sys/socket.h>
00222 #include <netinet/tcp.h>
00223 #include <stdio.h>
00224 #include <io.h>
00225 #include <memdebug.h>
00226 
00227 #ifdef NUTDEBUG
00228 #include <net/netdebug.h>
00229 #endif
00230 
00231 #define TICK_RATE   1
00232 
00237 
00238 TCPSOCKET *tcpSocketList = 0;   
00240 static volatile uint16_t last_local_port = 4096; /* Unassigned local port. */
00241 
00242 static uint_fast8_t registered;
00243 
00244 void NutTcpDiscardBuffers(TCPSOCKET * sock)
00245 {
00246     NETBUF *nb;
00247     while ((nb = sock->so_rx_buf) != 0) {
00248         sock->so_rx_buf = nb->nb_next;
00249         NutNetBufFree(nb);
00250     }
00251     while ((nb = sock->so_tx_nbq) != 0) {
00252         sock->so_tx_nbq = nb->nb_next;
00253         NutNetBufFree(nb);
00254     }
00255     while ((nb = sock->so_rx_nbq) != 0) {
00256         sock->so_rx_nbq = nb->nb_next;
00257         NutNetBufFree(nb);
00258     }
00259 }
00260 
00261 
00273 void NutTcpDestroySocket(TCPSOCKET * sock)
00274 {
00275     TCPSOCKET *sp;
00276     TCPSOCKET *volatile *spp;
00277     
00278     //@@@printf ("[%04X] Calling destroy.\n", (u_short) sock);
00279 
00280     /*
00281      * Remove socket from the list.
00282      */
00283     sp = tcpSocketList;
00284     spp = &tcpSocketList;
00285     while (sp) {
00286         if (sp == sock) {
00287             *spp = sp->so_next;
00288             break;
00289         }
00290         spp = &sp->so_next;
00291         sp = sp->so_next;
00292     }
00293 
00294     /*
00295      * Free all memory occupied by the socket.
00296      */
00297     if (sp) {
00298         NutTcpDiscardBuffers(sock);
00299         if (sock->so_devocnt)
00300         {
00301             free(sock->so_devobuf);
00302             sock->so_devocnt = 0;
00303         }
00304         memset(sock, 0, sizeof(TCPSOCKET));
00305         free(sock);
00306     }
00307 }
00308 
00323 TCPSOCKET *NutTcpFindSocket(uint16_t lport, uint16_t rport, uint32_t raddr)
00324 {
00325     TCPSOCKET *sp;
00326     TCPSOCKET *sock = 0;
00327 
00328     /*
00329      * Try to find an exact match for the remote
00330      * address and port first.
00331      */
00332     for (sp = tcpSocketList; sp; sp = sp->so_next) {
00333         if (sp->so_local_port == lport) {
00334             if (sp->so_remote_addr == raddr && sp->so_remote_port == rport && sp->so_state != TCPS_CLOSED) {
00335                 sock = sp;
00336                 break;
00337             }
00338         }
00339     }
00340 
00341     /*
00342      * If no exact match exists, try a listening socket.
00343      * This part had been totally wrong, because it
00344      * didn't check the local port number and accepted
00345      * incoming requests on any port. Thanks to
00346      * Alejandro Lopez, who pointed this out.
00347      */
00348     if (sock == 0) {
00349         for (sp = tcpSocketList; sp; sp = sp->so_next) {
00350             if (sp->so_state == TCPS_LISTEN && sp->so_local_port == lport) {
00351                 sock = sp;
00352                 break;
00353             }
00354         }
00355     }
00356 
00357     return sock;
00358 }
00359 
00360 
00373 TCPSOCKET *NutTcpCreateSocket(void)
00374 {
00375     TCPSOCKET *sock = 0;
00376 
00377     if (!registered) {
00378         if (NutRegisterIpHandler(IPPROTO_TCP, NutTcpInput) || NutTcpInitStateMachine()) {
00379             return NULL;
00380         }
00381         registered = 1;
00382     }
00383     if ((sock = calloc(1, sizeof(TCPSOCKET))) != 0) {
00384         sock->so_state = TCPS_CLOSED;
00385 
00386         /*
00387             * Initialize the virtual device interface.
00388             */
00389         sock->so_devtype = IFTYP_TCPSOCK;
00390         sock->so_devread = NutTcpDeviceRead;
00391         sock->so_devwrite = NutTcpDeviceWrite;
00392 #ifdef __HARVARD_ARCH__
00393         sock->so_devwrite_P = NutTcpDeviceWrite_P;
00394 #endif
00395         sock->so_devioctl = NutTcpDeviceIOCtl;
00396 
00397         sock->so_tx_isn = NutGetTickCount();  /* Generate the ISN from the nut_ticks counter */
00398         sock->so_tx_una = sock->so_tx_isn;
00399         sock->so_tx_nxt = sock->so_tx_isn;
00400         sock->so_rx_bsz = sock->so_rx_win = TCP_WINSIZE;
00401 
00402         sock->so_mss = TCP_MSS;
00403         sock->so_rtto = 1000; /* Initial retransmission time out */
00404 
00405         sock->so_next = tcpSocketList;
00406 
00407         sock->so_devobsz = TCP_MSS; /* Default output buffer size is TCP_MSS bytes */
00408 
00409         tcpSocketList = sock;
00410     }
00411     return sock;
00412 }
00413 
00433 int NutTcpSetSockOpt(TCPSOCKET * sock, int optname, CONST void *optval, int optlen)
00434 {
00435     int rc = -1;
00436 
00437     if (sock == 0)
00438         return -1;
00439     switch (optname) {
00440 
00441     case TCP_MAXSEG:
00442         if (optval == 0 || optlen != sizeof(uint16_t))
00443             sock->so_last_error = EINVAL;
00444         else if (sock->so_state != TCPS_CLOSED) 
00445                 sock->so_last_error = EISCONN;
00446         else {
00447             sock->so_mss = *((uint16_t *) optval);
00448             rc = 0;
00449         }
00450         break;
00451 
00452     case SO_RCVBUF:
00453         if (optval == 0 || optlen != sizeof(uint16_t))
00454             sock->so_last_error = EINVAL;
00455         else {
00456             sock->so_rx_bsz = *((uint16_t *) optval);
00457             sock->so_rx_win = sock->so_rx_bsz;
00458             rc = 0;
00459         }
00460         break;
00461 
00462     case SO_SNDTIMEO:
00463         if (optval == 0 || optlen != sizeof(uint32_t))
00464             sock->so_last_error = EINVAL;
00465         else {
00466             sock->so_write_to = *((uint32_t *) optval);
00467             rc = 0;
00468         }
00469         break;
00470 
00471     case SO_RCVTIMEO:
00472         if (optval == 0 || optlen != sizeof(uint32_t))
00473             sock->so_last_error = EINVAL;
00474         else {
00475             sock->so_read_to = *((uint32_t *) optval);
00476             rc = 0;
00477         }
00478         break;
00479 
00480     case SO_SNDBUF:
00481         if (optval == 0 || optlen != sizeof(uint16_t))
00482             sock->so_last_error = EINVAL;
00483         else {
00484             NutTcpDeviceWrite(sock, 0, 0);
00485             sock->so_devobsz = *((uint16_t *) optval);
00486             rc = 0;
00487         }
00488         break;
00489 
00490     default:
00491         sock->so_last_error = ENOPROTOOPT;
00492         break;
00493     }
00494     return rc;
00495 }
00496 
00516 int NutTcpGetSockOpt(TCPSOCKET * sock, int optname, void *optval, int optlen)
00517 {
00518     int rc = -1;
00519 
00520     if (sock == 0)
00521         return -1;
00522     switch (optname) {
00523 
00524     case TCP_MAXSEG:
00525         if (optval == 0 || optlen != sizeof(uint16_t))
00526             sock->so_last_error = EINVAL;
00527         else {
00528             *((uint16_t *) optval) = sock->so_mss;
00529             rc = 0;
00530         }
00531         break;
00532 
00533     case SO_RCVBUF:
00534         if (optval == 0 || optlen != sizeof(uint16_t))
00535             sock->so_last_error = EINVAL;
00536         else {
00537             *((uint16_t *) optval) = sock->so_rx_bsz;
00538             rc = 0;
00539         }
00540         break;
00541 
00542     case SO_SNDTIMEO:
00543         if (optval == 0 || optlen != sizeof(uint32_t))
00544             sock->so_last_error = EINVAL;
00545         else {
00546             *((uint32_t *) optval) = sock->so_write_to;
00547             rc = 0;
00548         }
00549         break;
00550 
00551     case SO_RCVTIMEO:
00552         if (optval == 0 || optlen != sizeof(uint32_t))
00553             sock->so_last_error = EINVAL;
00554         else {
00555             *((uint32_t *) optval) = sock->so_read_to;
00556             rc = 0;
00557         }
00558         break;
00559 
00560     case SO_SNDBUF:
00561         if (optval == 0 || optlen != sizeof(uint16_t))
00562             sock->so_last_error = EINVAL;
00563         else {
00564             *((uint16_t *) optval) = sock->so_devobsz;
00565             rc = 0;
00566         }
00567         break;
00568     default:
00569         sock->so_last_error = ENOPROTOOPT;
00570         break;
00571     }
00572     return rc;
00573 }
00574 
00593 int NutTcpConnect(TCPSOCKET * sock, uint32_t addr, uint16_t port)
00594 {
00595     TCPSOCKET *sp;
00596     NUTDEVICE *dev;
00597 
00598     if (sock == 0)
00599         return -1;
00600     /*
00601      * Despite RFC793 we do not allow a passive
00602      * open to become active.
00603      */
00604     if (sock->so_state == TCPS_LISTEN) {
00605         sock->so_last_error = EOPNOTSUPP;
00606         return -1;
00607     } else if (sock->so_state != TCPS_CLOSED) {
00608         sock->so_last_error = EISCONN;
00609         return -1;
00610     }
00611 
00612     /*
00613      * Find an unused local port.
00614      */
00615     do {
00616         if (++last_local_port == 0)
00617             last_local_port = 4096;
00618 
00619         sp = tcpSocketList;
00620         while (sp) {
00621             /* Thanks to Ralph Mason for fixing the byte order bug. */
00622             if (sp->so_local_port == htons(last_local_port))
00623                 break;
00624             sp = sp->so_next;
00625         }
00626     } while (sp);
00627 
00628     /*
00629      * OK - we've got a new port. Now fill
00630      * remaining parts of the socket structure.
00631      */
00632     sock->so_local_port = htons(last_local_port);
00633     sock->so_remote_port = htons(port);
00634     sock->so_remote_addr = addr;
00635 
00636     /*
00637      * Get local address for this destination.
00638      */
00639     if ((dev = NutIpRouteQuery(addr, 0)) != 0) {
00640         IFNET *nif = dev->dev_icb;
00641         sock->so_local_addr = nif->if_local_ip;
00642     } else {
00643         sock->so_last_error = EHOSTUNREACH;
00644         return -1;
00645     }
00646 
00647     /*
00648      * Trigger active open event for the state machine.
00649      * This will suspend the thread until synchronized.
00650      */
00651     return NutTcpStateActiveOpenEvent(sock);
00652 }
00653 
00669 int NutTcpAccept(TCPSOCKET * sock, uint16_t port)
00670 {
00671     sock->so_local_port = htons(port);
00672 
00673     return NutTcpStatePassiveOpenEvent(sock);
00674 }
00675 
00692 int NutTcpSend(TCPSOCKET * sock, CONST void *data, int len)
00693 {
00694     uint16_t unacked;
00695 
00696     /*
00697      * Check parameters.
00698      */
00699     NutThreadYield();
00700     
00701     if (sock == 0)
00702         return -1;
00703     if (data == 0 || len == 0)
00704         return 0;
00705 
00706     /*
00707      * Limit the transmission size to our maximum segment size.
00708      */
00709     if (len > sock->so_mss)
00710         len = sock->so_mss;
00711 
00712     for (;;) {
00713         /*
00714          * We can only send on an established connection.
00715          */
00716         if (sock->so_state != TCPS_ESTABLISHED) {
00717             sock->so_last_error = ENOTCONN;
00718             return -1;
00719         }
00720 
00721         /*
00722          * Limit the size of unacknowledged data to four full segments.
00723          * Also wait for peer's window open wide enough to take all our 
00724          * data. This also avoids silly window syndrome on our side.
00725          */
00726         unacked = sock->so_tx_nxt - sock->so_tx_una;
00727         if ((unacked >> 2) < sock->so_mss && len <= sock->so_tx_win - unacked) {
00728             break;
00729         }
00730         if (NutEventWait(&sock->so_tx_tq, sock->so_write_to)) {
00731             return 0;
00732         }
00733     }
00734     /*
00735      * The segment will be automatically retransmitted if not 
00736      * acknowledged in time. If this returns an error, it's a 
00737      * fatal one.
00738      */
00739     sock->so_tx_flags |= SO_ACK;
00740     if (NutTcpOutput(sock, data, len))
00741         return -1;
00742     return len;
00743 }
00744 
00762 int NutTcpReceive(TCPSOCKET * sock, void *data, int size)
00763 {
00764     int i;
00765 
00766     NutThreadYield();
00767     /*
00768      * Check parameters.
00769      */
00770     if (sock == 0)
00771         return -1;
00772     if (sock->so_state != TCPS_ESTABLISHED && sock->so_state != TCPS_CLOSE_WAIT) {
00773         sock->so_last_error = ENOTCONN;
00774         return -1;
00775     }
00776     if (data == 0 || size == 0)
00777         return 0;
00778 
00779     /*
00780      * Wait until any data arrived, a timeout occurs
00781      * or the connection terminates.
00782      */
00783     while (sock->so_rx_cnt - sock->so_rd_cnt == 0) {
00784         if (sock->so_state != TCPS_ESTABLISHED) {
00785             sock->so_last_error = ENOTCONN;
00786             return -1;
00787         }
00788         if (NutEventWait(&sock->so_rx_tq, sock->so_read_to))
00789             return 0;
00790     }
00791 
00792     if (size > sock->so_rx_cnt - sock->so_rd_cnt)
00793         size = sock->so_rx_cnt - sock->so_rd_cnt;
00794     if (size) {
00795         NETBUF *nb;
00796         uint16_t rd_cnt;         /* Bytes read from NETBUF. */
00797         uint16_t nb_cnt;         /* Bytes left in NETBUF. */
00798         uint16_t ab_cnt;         /* Total bytes in app buffer. */
00799         uint16_t mv_cnt;         /* Bytes to move to app buffer. */
00800 
00801         rd_cnt = sock->so_rd_cnt;
00802 
00803         ab_cnt = 0;
00804         while (ab_cnt < size) {
00805             nb = sock->so_rx_buf;
00806             nb_cnt = nb->nb_ap.sz - rd_cnt;
00807             mv_cnt = size - ab_cnt;
00808             if (mv_cnt > nb_cnt)
00809                 mv_cnt = nb_cnt;
00810             memcpy((char *) data + ab_cnt, (char *) (nb->nb_ap.vp) + rd_cnt, mv_cnt);
00811             ab_cnt += mv_cnt;
00812             rd_cnt += mv_cnt;
00813             if (mv_cnt >= nb_cnt) {
00814                 sock->so_rx_buf = nb->nb_next;
00815                 sock->so_rx_cnt -= rd_cnt;
00816                 NutNetBufFree(nb);
00817                 sock->so_rx_apc--;
00818                 nb = sock->so_rx_buf;
00819                 rd_cnt = 0;
00820             }
00821         }
00822         sock->so_rd_cnt = rd_cnt;
00823 
00824         /*
00825          * Update our receive window.
00826          */
00827         if (sock->so_state == TCPS_ESTABLISHED) {
00828             i = sock->so_rx_win;
00829             if ((i += size) > sock->so_rx_bsz)
00830                 i = sock->so_rx_bsz;
00831 
00832             if (sock->so_rx_win <= sock->so_mss && i > sock->so_mss) {
00833                 sock->so_rx_win = i;
00834                 NutTcpStateWindowEvent(sock);
00835             } else {
00836                 sock->so_rx_win = i;
00837             }
00838         }
00839     }
00840     return size;
00841 }
00842 
00855 int NutTcpCloseSocket(TCPSOCKET * sock)
00856 {
00857     /* Flush buffer first */
00858     //@@@printf ("[%04X] Calling close\n", (u_short) sock);
00859     NutTcpDeviceWrite(sock, 0, 0);
00860     return NutTcpStateCloseEvent(sock);
00861 }
00862 
00911 int NutTcpError(TCPSOCKET * sock)
00912 {
00913     if (sock == 0)
00914         return ENOTSOCK;
00915     return sock->so_last_error;
00916 }
00917 
00937 int NutTcpDeviceRead(TCPSOCKET * sock, void *buffer, int size)
00938 {
00939     return NutTcpReceive(sock, buffer, size);
00940 }
00941 
00942 static int SendBuffer(TCPSOCKET * sock, CONST void *buffer, int size)
00943 {
00944     int rc;
00945     int bite;
00946 
00947     for (rc = 0; rc < size; rc += bite) {
00948         if ((bite = NutTcpSend(sock, (uint8_t *) buffer + rc, size - rc)) <= 0) {
00949             return -1;
00950         }
00951     }
00952     return rc;
00953 }
00954 
00977 int NutTcpDeviceWrite(TCPSOCKET * sock, CONST void *buf, int size)
00978 {
00979     int rc;
00980     uint16_t sz;
00981     /* hack alert for ICCAVR */
00982     uint8_t *buffer = (uint8_t*) buf;
00983 
00984     /*
00985      * Check parameters.
00986      */
00987     if (sock == 0)
00988         return -1;
00989     if (sock->so_state != TCPS_ESTABLISHED) {
00990         sock->so_last_error = ENOTCONN;
00991         return -1;
00992     }
00993 
00994     /* Flush buffer? */
00995     if (size == 0) {
00996         if (sock->so_devocnt) {
00997             if (SendBuffer(sock, sock->so_devobuf, sock->so_devocnt) < 0) {
00998                 free(sock->so_devobuf);
00999                 sock->so_devocnt = 0;
01000                 return -1;
01001             }
01002             free(sock->so_devobuf);
01003             sock->so_devocnt = 0;
01004         }
01005         return 0;
01006     }
01007 
01008     /* If we don't have a buffer so far... */
01009     if (sock->so_devocnt == 0) {
01010         /* If new data block is bigger or equal than buffer size
01011          * send first part of data to nic and store remaining
01012          * bytes in buffer
01013          */
01014         if ((uint16_t) size >= sock->so_devobsz) {
01015             rc = size % sock->so_devobsz;
01016             if (SendBuffer(sock, buffer, size - rc) < 0)
01017                 return -1;
01018             buffer += size - rc;
01019         } else
01020             rc = size;
01021 
01022         /* If there are some remainings bytes, allocate buffer
01023          * and store them
01024          */
01025         if (rc) {
01026             if (!(sock->so_devobuf = malloc(sock->so_devobsz)))
01027                 return -1;
01028             memcpy(sock->so_devobuf, buffer, rc);
01029             sock->so_devocnt = rc;
01030         }
01031         return size;
01032     }
01033 
01034     /* Check if new data fully fits in output buffer */
01035     if (sock->so_devocnt + size < sock->so_devobsz) {
01036         memcpy(sock->so_devobuf + sock->so_devocnt, buffer, size);
01037         sock->so_devocnt += size;
01038         return size;
01039     }
01040 
01041     /* Otherwise store first bytes of new data in buffer and flush
01042      * the buffer
01043      */
01044     sz = sock->so_devobsz - sock->so_devocnt;
01045     memcpy(sock->so_devobuf + sock->so_devocnt, buffer, sz);
01046     buffer += sz;
01047     if (SendBuffer(sock, sock->so_devobuf, sock->so_devobsz) < 0) {
01048         free(sock->so_devobuf);
01049         sock->so_devocnt = 0;
01050         return -1;
01051     }
01052 
01053     /* If remaining data is bigger or equal than buffer size
01054      * send first part of data to nic and later store remaining
01055      * bytes in buffer
01056      */
01057     sz = size - sz;
01058     if (sz >= sock->so_devobsz) {
01059         rc = size % sock->so_devobsz;
01060         if (SendBuffer(sock, buffer, sz - rc) < 0) {
01061             free(sock->so_devobuf);
01062             sock->so_devocnt = 0;
01063             return -1;
01064         }
01065         buffer += sz - rc;
01066     } else
01067         rc = sz;
01068 
01069     /* If there are some remainings bytes, store them in buffer
01070      */
01071     if (rc)
01072         memcpy(sock->so_devobuf, buffer, rc);
01073     else                        /* Otherwise free buffer */
01074         free(sock->so_devobuf);
01075     sock->so_devocnt = rc;
01076 
01077     return size;
01078 }
01079 
01104 #ifdef __HARVARD_ARCH__
01105 int NutTcpDeviceWrite_P(TCPSOCKET * sock, PGM_P buffer, int size)
01106 {
01107     int rc;
01108     char *rp = 0;
01109 
01110     /*
01111      * Hack alert. Neither do we handle out of memory correctly
01112      * nor do we pass the PGM pointer to lower levels.
01113      */
01114     if (size && (rp = NutHeapAlloc(size)) != 0)
01115         memcpy_P(rp, buffer, size);
01116     rc = NutTcpDeviceWrite(sock, rp, size);
01117     if (rp)
01118         NutHeapFree(rp);
01119 
01120     return rc;
01121 }
01122 #endif
01123 
01143 int NutTcpDeviceIOCtl(TCPSOCKET * sock, int cmd, void *param)
01144 {
01145     uint32_t *lvp = (uint32_t *) param;
01146     int rc = 0;
01147     
01148     switch (cmd) {
01149     case IOCTL_GETFILESIZE:
01150     case IOCTL_GETINBUFCOUNT:
01151         *lvp = (sock->so_rx_cnt - sock->so_rd_cnt);
01152         break;
01153     case IOCTL_GETOUTBUFCOUNT:
01154         *lvp = (sock->so_devocnt);
01155         break;
01156     default:
01157         rc = -1;
01158     }
01159     
01160     return rc;    
01161 }
01162