1*10465441SEvalZero 2*10465441SEvalZero 3*10465441SEvalZero/** 4*10465441SEvalZero\mainpage The uIP TCP/IP stack 5*10465441SEvalZero\author Adam Dunkels, http://www.sics.se/~adam/ 6*10465441SEvalZero 7*10465441SEvalZeroThe uIP TCP/IP stack is intended to make it possible to communicate 8*10465441SEvalZerousing the TCP/IP protocol suite even on small 8-bit 9*10465441SEvalZeromicro-controllers. Despite being small and simple, uIP do not require 10*10465441SEvalZerotheir peers to have complex, full-size stacks, but can communicate 11*10465441SEvalZerowith peers running a similarly light-weight stack. The code size is on 12*10465441SEvalZerothe order of a few kilobytes and RAM usage can be configured to be as 13*10465441SEvalZerolow as a few hundred bytes. 14*10465441SEvalZero 15*10465441SEvalZerouIP can be found at the uIP web page: http://www.sics.se/~adam/uip/ 16*10465441SEvalZero 17*10465441SEvalZero\sa \ref apps "Application programs" 18*10465441SEvalZero\sa \ref uipopt "Compile-time configuration options" 19*10465441SEvalZero\sa \ref uipconffunc "Run-time configuration functions" 20*10465441SEvalZero\sa \ref uipinit "Initialization functions" 21*10465441SEvalZero\sa \ref uipdevfunc "Device driver interface" and 22*10465441SEvalZero \ref uipdrivervars "variables used by device drivers" 23*10465441SEvalZero\sa \ref uipappfunc "uIP functions called from application programs" 24*10465441SEvalZero(see below) and the \ref psock "protosockets API" and their underlying 25*10465441SEvalZero\ref pt "protothreads" 26*10465441SEvalZero 27*10465441SEvalZero\section uIPIntroduction Introduction 28*10465441SEvalZero 29*10465441SEvalZeroWith the success of the Internet, the TCP/IP protocol suite has become 30*10465441SEvalZeroa global standard for communication. TCP/IP is the underlying protocol 31*10465441SEvalZeroused for web page transfers, e-mail transmissions, file transfers, and 32*10465441SEvalZeropeer-to-peer networking over the Internet. For embedded systems, being 33*10465441SEvalZeroable to run native TCP/IP makes it possible to connect the system 34*10465441SEvalZerodirectly to an intranet or even the global Internet. Embedded devices 35*10465441SEvalZerowith full TCP/IP support will be first-class network citizens, thus 36*10465441SEvalZerobeing able to fully communicate with other hosts in the network. 37*10465441SEvalZero 38*10465441SEvalZeroTraditional TCP/IP implementations have required far too much 39*10465441SEvalZeroresources both in terms of code size and memory usage to be useful in 40*10465441SEvalZerosmall 8 or 16-bit systems. Code size of a few hundred kilobytes and 41*10465441SEvalZeroRAM requirements of several hundreds of kilobytes have made it 42*10465441SEvalZeroimpossible to fit the full TCP/IP stack into systems with a few tens 43*10465441SEvalZeroof kilobytes of RAM and room for less than 100 kilobytes of 44*10465441SEvalZerocode. 45*10465441SEvalZero 46*10465441SEvalZeroThe uIP implementation is designed to have only the absolute minimal 47*10465441SEvalZeroset of features needed for a full TCP/IP stack. It can only handle a 48*10465441SEvalZerosingle network interface and contains the IP, ICMP, UDP and TCP 49*10465441SEvalZeroprotocols. uIP is written in the C programming language. 50*10465441SEvalZero 51*10465441SEvalZeroMany other TCP/IP implementations for small systems assume that the 52*10465441SEvalZeroembedded device always will communicate with a full-scale TCP/IP 53*10465441SEvalZeroimplementation running on a workstation-class machine. Under this 54*10465441SEvalZeroassumption, it is possible to remove certain TCP/IP mechanisms that 55*10465441SEvalZeroare very rarely used in such situations. Many of those mechanisms are 56*10465441SEvalZeroessential, however, if the embedded device is to communicate with 57*10465441SEvalZeroanother equally limited device, e.g., when running distributed 58*10465441SEvalZeropeer-to-peer services and protocols. uIP is designed to be RFC 59*10465441SEvalZerocompliant in order to let the embedded devices to act as first-class 60*10465441SEvalZeronetwork citizens. The uIP TCP/IP implementation that is not tailored 61*10465441SEvalZerofor any specific application. 62*10465441SEvalZero 63*10465441SEvalZero 64*10465441SEvalZero\section tcpip TCP/IP Communication 65*10465441SEvalZero 66*10465441SEvalZeroThe full TCP/IP suite consists of numerous protocols, ranging from low 67*10465441SEvalZerolevel protocols such as ARP which translates IP addresses to MAC 68*10465441SEvalZeroaddresses, to application level protocols such as SMTP that is used to 69*10465441SEvalZerotransfer e-mail. The uIP is mostly concerned with the TCP and IP 70*10465441SEvalZeroprotocols and upper layer protocols will be referred to as "the 71*10465441SEvalZeroapplication". Lower layer protocols are often implemented in hardware 72*10465441SEvalZeroor firmware and will be referred to as "the network device" that are 73*10465441SEvalZerocontrolled by the network device driver. 74*10465441SEvalZero 75*10465441SEvalZeroTCP provides a reliable byte stream to the upper layer protocols. It 76*10465441SEvalZerobreaks the byte stream into appropriately sized segments and each 77*10465441SEvalZerosegment is sent in its own IP packet. The IP packets are sent out on 78*10465441SEvalZerothe network by the network device driver. If the destination is not on 79*10465441SEvalZerothe physically connected network, the IP packet is forwarded onto 80*10465441SEvalZeroanother network by a router that is situated between the two 81*10465441SEvalZeronetworks. If the maximum packet size of the other network is smaller 82*10465441SEvalZerothan the size of the IP packet, the packet is fragmented into smaller 83*10465441SEvalZeropackets by the router. If possible, the size of the TCP segments are 84*10465441SEvalZerochosen so that fragmentation is minimized. The final recipient of the 85*10465441SEvalZeropacket will have to reassemble any fragmented IP packets before they 86*10465441SEvalZerocan be passed to higher layers. 87*10465441SEvalZero 88*10465441SEvalZeroThe formal requirements for the protocols in the TCP/IP stack is 89*10465441SEvalZerospecified in a number of RFC documents published by the Internet 90*10465441SEvalZeroEngineering Task Force, IETF. Each of the protocols in the stack is 91*10465441SEvalZerodefined in one more RFC documents and RFC1122 collects 92*10465441SEvalZeroall requirements and updates the previous RFCs. 93*10465441SEvalZero 94*10465441SEvalZeroThe RFC1122 requirements can be divided into two categories; those 95*10465441SEvalZerothat deal with the host to host communication and those that deal with 96*10465441SEvalZerocommunication between the application and the networking stack. An 97*10465441SEvalZeroexample of the first kind is "A TCP MUST be able to receive a TCP 98*10465441SEvalZerooption in any segment" and an example of the second kind is "There 99*10465441SEvalZeroMUST be a mechanism for reporting soft TCP error conditions to the 100*10465441SEvalZeroapplication." A TCP/IP implementation that violates requirements of 101*10465441SEvalZerothe first kind may not be able to communicate with other TCP/IP 102*10465441SEvalZeroimplementations and may even lead to network failures. Violation of 103*10465441SEvalZerothe second kind of requirements will only affect the communication 104*10465441SEvalZerowithin the system and will not affect host-to-host communication. 105*10465441SEvalZero 106*10465441SEvalZeroIn uIP, all RFC requirements that affect host-to-host communication 107*10465441SEvalZeroare implemented. However, in order to reduce code size, we have 108*10465441SEvalZeroremoved certain mechanisms in the interface between the application 109*10465441SEvalZeroand the stack, such as the soft error reporting mechanism and 110*10465441SEvalZerodynamically configurable type-of-service bits for TCP 111*10465441SEvalZeroconnections. Since there are only very few applications that make use 112*10465441SEvalZeroof those features they can be removed without loss of generality. 113*10465441SEvalZero 114*10465441SEvalZero\section mainloop Main Control Loop 115*10465441SEvalZero 116*10465441SEvalZeroThe uIP stack can be run either as a task in a multitasking system, or 117*10465441SEvalZeroas the main program in a singletasking system. In both cases, the main 118*10465441SEvalZerocontrol loop does two things repeatedly: 119*10465441SEvalZero 120*10465441SEvalZero - Check if a packet has arrived from the network. 121*10465441SEvalZero - Check if a periodic timeout has occurred. 122*10465441SEvalZero 123*10465441SEvalZeroIf a packet has arrived, the input handler function, uip_input(), 124*10465441SEvalZeroshould be invoked by the main control loop. The input handler function 125*10465441SEvalZerowill never block, but will return at once. When it returns, the stack 126*10465441SEvalZeroor the application for which the incoming packet was intended may have 127*10465441SEvalZeroproduced one or more reply packets which should be sent out. If so, 128*10465441SEvalZerothe network device driver should be called to send out these packets. 129*10465441SEvalZero 130*10465441SEvalZeroPeriodic timeouts are used to drive TCP mechanisms that depend on 131*10465441SEvalZerotimers, such as delayed acknowledgments, retransmissions and 132*10465441SEvalZeroround-trip time estimations. When the main control loop infers that 133*10465441SEvalZerothe periodic timer should fire, it should invoke the timer handler 134*10465441SEvalZerofunction uip_periodic(). Because the TCP/IP stack may perform 135*10465441SEvalZeroretransmissions when dealing with a timer event, the network device 136*10465441SEvalZerodriver should called to send out the packets that may have been produced. 137*10465441SEvalZero 138*10465441SEvalZero\section arch Architecture Specific Functions 139*10465441SEvalZero 140*10465441SEvalZerouIP requires a few functions to be implemented specifically for the 141*10465441SEvalZeroarchitecture on which uIP is intended to run. These functions should 142*10465441SEvalZerobe hand-tuned for the particular architecture, but generic C 143*10465441SEvalZeroimplementations are given as part of the uIP distribution. 144*10465441SEvalZero 145*10465441SEvalZero\subsection checksums Checksum Calculation 146*10465441SEvalZero 147*10465441SEvalZeroThe TCP and IP protocols implement a checksum that covers the data and 148*10465441SEvalZeroheader portions of the TCP and IP packets. Since the calculation of 149*10465441SEvalZerothis checksum is made over all bytes in every packet being sent and 150*10465441SEvalZeroreceived it is important that the function that calculates the 151*10465441SEvalZerochecksum is efficient. Most often, this means that the checksum 152*10465441SEvalZerocalculation must be fine-tuned for the particular architecture on 153*10465441SEvalZerowhich the uIP stack runs. 154*10465441SEvalZero 155*10465441SEvalZeroWhile uIP includes a generic checksum function, it also leaves it open 156*10465441SEvalZerofor an architecture specific implementation of the two functions 157*10465441SEvalZerouip_ipchksum() and uip_tcpchksum(). The checksum calculations in those 158*10465441SEvalZerofunctions can be written in highly optimized assembler rather than 159*10465441SEvalZerogeneric C code. 160*10465441SEvalZero 161*10465441SEvalZero\subsection longarith 32-bit Arithmetic 162*10465441SEvalZero 163*10465441SEvalZeroThe TCP protocol uses 32-bit sequence numbers, and a TCP 164*10465441SEvalZeroimplementation will have to do a number of 32-bit additions as part of 165*10465441SEvalZerothe normal protocol processing. Since 32-bit arithmetic is not 166*10465441SEvalZeronatively available on many of the platforms for which uIP is intended, 167*10465441SEvalZerouIP leaves the 32-bit additions to be implemented by the architecture 168*10465441SEvalZerospecific module and does not make use of any 32-bit arithmetic in the 169*10465441SEvalZeromain code base. 170*10465441SEvalZero 171*10465441SEvalZeroWhile uIP implements a generic 32-bit addition, there is support for 172*10465441SEvalZerohaving an architecture specific implementation of the uip_add32() 173*10465441SEvalZerofunction. 174*10465441SEvalZero 175*10465441SEvalZero 176*10465441SEvalZero\section memory Memory Management 177*10465441SEvalZero 178*10465441SEvalZeroIn the architectures for which uIP is intended, RAM is the most 179*10465441SEvalZeroscarce resource. With only a few kilobytes of RAM available for the 180*10465441SEvalZeroTCP/IP stack to use, mechanisms used in traditional TCP/IP cannot be 181*10465441SEvalZerodirectly applied. 182*10465441SEvalZero 183*10465441SEvalZero 184*10465441SEvalZeroThe uIP stack does not use explicit dynamic memory 185*10465441SEvalZeroallocation. Instead, it uses a single global buffer for holding 186*10465441SEvalZeropackets and has a fixed table for holding connection state. The global 187*10465441SEvalZeropacket buffer is large enough to contain one packet of maximum 188*10465441SEvalZerosize. When a packet arrives from the network, the device driver places 189*10465441SEvalZeroit in the global buffer and calls the TCP/IP stack. If the packet 190*10465441SEvalZerocontains data, the TCP/IP stack will notify the corresponding 191*10465441SEvalZeroapplication. Because the data in the buffer will be overwritten by the 192*10465441SEvalZeronext incoming packet, the application will either have to act 193*10465441SEvalZeroimmediately on the data or copy the data into a secondary buffer for 194*10465441SEvalZerolater processing. The packet buffer will not be overwritten by new 195*10465441SEvalZeropackets before the application has processed the data. Packets that 196*10465441SEvalZeroarrive when the application is processing the data must be queued, 197*10465441SEvalZeroeither by the network device or by the device driver. Most single-chip 198*10465441SEvalZeroEthernet controllers have on-chip buffers that are large enough to 199*10465441SEvalZerocontain at least 4 maximum sized Ethernet frames. Devices that are 200*10465441SEvalZerohandled by the processor, such as RS-232 ports, can copy incoming 201*10465441SEvalZerobytes to a separate buffer during application processing. If the 202*10465441SEvalZerobuffers are full, the incoming packet is dropped. This will cause 203*10465441SEvalZeroperformance degradation, but only when multiple connections are 204*10465441SEvalZerorunning in parallel. This is because uIP advertises a very small 205*10465441SEvalZeroreceiver window, which means that only a single TCP segment will be in 206*10465441SEvalZerothe network per connection. 207*10465441SEvalZero 208*10465441SEvalZeroIn uIP, the same global packet buffer that is used for incoming 209*10465441SEvalZeropackets is also used for the TCP/IP headers of outgoing data. If the 210*10465441SEvalZeroapplication sends dynamic data, it may use the parts of the global 211*10465441SEvalZeropacket buffer that are not used for headers as a temporary storage 212*10465441SEvalZerobuffer. To send the data, the application passes a pointer to the data 213*10465441SEvalZeroas well as the length of the data to the stack. The TCP/IP headers are 214*10465441SEvalZerowritten into the global buffer and once the headers have been 215*10465441SEvalZeroproduced, the device driver sends the headers and the application data 216*10465441SEvalZeroout on the network. The data is not queued for 217*10465441SEvalZeroretransmissions. Instead, the application will have to reproduce the 218*10465441SEvalZerodata if a retransmission is necessary. 219*10465441SEvalZero 220*10465441SEvalZeroThe total amount of memory usage for uIP depends heavily on the 221*10465441SEvalZeroapplications of the particular device in which the implementations are 222*10465441SEvalZeroto be run. The memory configuration determines both the amount of 223*10465441SEvalZerotraffic the system should be able to handle and the maximum amount of 224*10465441SEvalZerosimultaneous connections. A device that will be sending large e-mails 225*10465441SEvalZerowhile at the same time running a web server with highly dynamic web 226*10465441SEvalZeropages and multiple simultaneous clients, will require more RAM than a 227*10465441SEvalZerosimple Telnet server. It is possible to run the uIP implementation 228*10465441SEvalZerowith as little as 200 bytes of RAM, but such a configuration will 229*10465441SEvalZeroprovide extremely low throughput and will only allow a small number of 230*10465441SEvalZerosimultaneous connections. 231*10465441SEvalZero 232*10465441SEvalZero\section api Application Program Interface (API) 233*10465441SEvalZero 234*10465441SEvalZero 235*10465441SEvalZeroThe Application Program Interface (API) defines the way the 236*10465441SEvalZeroapplication program interacts with the TCP/IP stack. The most commonly 237*10465441SEvalZeroused API for TCP/IP is the BSD socket API which is used in most Unix 238*10465441SEvalZerosystems and has heavily influenced the Microsoft Windows WinSock 239*10465441SEvalZeroAPI. Because the socket API uses stop-and-wait semantics, it requires 240*10465441SEvalZerosupport from an underlying multitasking operating system. Since the 241*10465441SEvalZerooverhead of task management, context switching and allocation of stack 242*10465441SEvalZerospace for the tasks might be too high in the intended uIP target 243*10465441SEvalZeroarchitectures, the BSD socket interface is not suitable for our 244*10465441SEvalZeropurposes. 245*10465441SEvalZero 246*10465441SEvalZerouIP provides two APIs to programmers: protosockets, a BSD socket-like 247*10465441SEvalZeroAPI without the overhead of full multi-threading, and a "raw" 248*10465441SEvalZeroevent-based API that is nore low-level than protosockets but uses less 249*10465441SEvalZeromemory. 250*10465441SEvalZero 251*10465441SEvalZero\sa \ref psock 252*10465441SEvalZero\sa \ref pt 253*10465441SEvalZero 254*10465441SEvalZero 255*10465441SEvalZero\subsection rawapi The uIP raw API 256*10465441SEvalZero 257*10465441SEvalZeroThe "raw" uIP API uses an event driven interface where the application is 258*10465441SEvalZeroinvoked in response to certain events. An application running on top 259*10465441SEvalZeroof uIP is implemented as a C function that is called by uIP in 260*10465441SEvalZeroresponse to certain events. uIP calls the application when data is 261*10465441SEvalZeroreceived, when data has been successfully delivered to the other end 262*10465441SEvalZeroof the connection, when a new connection has been set up, or when data 263*10465441SEvalZerohas to be retransmitted. The application is also periodically polled 264*10465441SEvalZerofor new data. The application program provides only one callback 265*10465441SEvalZerofunction; it is up to the application to deal with mapping different 266*10465441SEvalZeronetwork services to different ports and connections. Because the 267*10465441SEvalZeroapplication is able to act on incoming data and connection requests as 268*10465441SEvalZerosoon as the TCP/IP stack receives the packet, low response times can 269*10465441SEvalZerobe achieved even in low-end systems. 270*10465441SEvalZero 271*10465441SEvalZerouIP is different from other TCP/IP stacks in that it requires help 272*10465441SEvalZerofrom the application when doing retransmissions. Other TCP/IP stacks 273*10465441SEvalZerobuffer the transmitted data in memory until the data is known to be 274*10465441SEvalZerosuccessfully delivered to the remote end of the connection. If the 275*10465441SEvalZerodata needs to be retransmitted, the stack takes care of the 276*10465441SEvalZeroretransmission without notifying the application. With this approach, 277*10465441SEvalZerothe data has to be buffered in memory while waiting for an 278*10465441SEvalZeroacknowledgment even if the application might be able to quickly 279*10465441SEvalZeroregenerate the data if a retransmission has to be made. 280*10465441SEvalZero 281*10465441SEvalZeroIn order to reduce memory usage, uIP utilizes the fact that the 282*10465441SEvalZeroapplication may be able to regenerate sent data and lets the 283*10465441SEvalZeroapplication take part in retransmissions. uIP does not keep track of 284*10465441SEvalZeropacket contents after they have been sent by the device driver, and 285*10465441SEvalZerouIP requires that the application takes an active part in performing 286*10465441SEvalZerothe retransmission. When uIP decides that a segment should be 287*10465441SEvalZeroretransmitted, it calls the application with a flag set indicating 288*10465441SEvalZerothat a retransmission is required. The application checks the 289*10465441SEvalZeroretransmission flag and produces the same data that was previously 290*10465441SEvalZerosent. From the application's standpoint, performing a retransmission 291*10465441SEvalZerois not different from how the data originally was sent. Therefore the 292*10465441SEvalZeroapplication can be written in such a way that the same code is used 293*10465441SEvalZeroboth for sending data and retransmitting data. Also, it is important 294*10465441SEvalZeroto note that even though the actual retransmission operation is 295*10465441SEvalZerocarried out by the application, it is the responsibility of the stack 296*10465441SEvalZeroto know when the retransmission should be made. Thus the complexity of 297*10465441SEvalZerothe application does not necessarily increase because it takes an 298*10465441SEvalZeroactive part in doing retransmissions. 299*10465441SEvalZero 300*10465441SEvalZero\subsubsection appevents Application Events 301*10465441SEvalZero 302*10465441SEvalZeroThe application must be implemented as a C function, UIP_APPCALL(), 303*10465441SEvalZerothat uIP calls whenever an event occurs. Each event has a corresponding 304*10465441SEvalZerotest function that is used to distinguish between different 305*10465441SEvalZeroevents. The functions are implemented as C macros that will evaluate 306*10465441SEvalZeroto either zero or non-zero. Note that certain events can happen in 307*10465441SEvalZeroconjunction with each other (i.e., new data can arrive at the same 308*10465441SEvalZerotime as data is acknowledged). 309*10465441SEvalZero 310*10465441SEvalZero\subsubsection connstate The Connection Pointer 311*10465441SEvalZero 312*10465441SEvalZeroWhen the application is called by uIP, the global variable uip_conn is 313*10465441SEvalZeroset to point to the uip_conn structure for the connection that 314*10465441SEvalZerocurrently is handled, and is called the "current connection". The 315*10465441SEvalZerofields in the uip_conn structure for the current connection can be 316*10465441SEvalZeroused, e.g., to distinguish between different services, or to check to 317*10465441SEvalZerowhich IP address the connection is connected. One typical use would be 318*10465441SEvalZeroto inspect the uip_conn->lport (the local TCP port number) to decide 319*10465441SEvalZerowhich service the connection should provide. For instance, an 320*10465441SEvalZeroapplication might decide to act as an HTTP server if the value of 321*10465441SEvalZerouip_conn->lport is equal to 80 and act as a TELNET server if the value 322*10465441SEvalZerois 23. 323*10465441SEvalZero 324*10465441SEvalZero\subsubsection recvdata Receiving Data 325*10465441SEvalZero 326*10465441SEvalZeroIf the uIP test function uip_newdata() is non-zero, the remote host of 327*10465441SEvalZerothe connection has sent new data. The uip_appdata pointer point to the 328*10465441SEvalZeroactual data. The size of the data is obtained through the uIP function 329*10465441SEvalZerouip_datalen(). The data is not buffered by uIP, but will be 330*10465441SEvalZerooverwritten after the application function returns, and the 331*10465441SEvalZeroapplication will therefor have to either act directly on the incoming 332*10465441SEvalZerodata, or by itself copy the incoming data into a buffer for later 333*10465441SEvalZeroprocessing. 334*10465441SEvalZero 335*10465441SEvalZero\subsubsection senddata Sending Data 336*10465441SEvalZero 337*10465441SEvalZeroWhen sending data, uIP adjusts the length of the data sent by the 338*10465441SEvalZeroapplication according to the available buffer space and the current 339*10465441SEvalZeroTCP window advertised by the receiver. The amount of buffer space is 340*10465441SEvalZerodictated by the memory configuration. It is therefore possible that 341*10465441SEvalZeroall data sent from the application does not arrive at the receiver, 342*10465441SEvalZeroand the application may use the uip_mss() function to see how much 343*10465441SEvalZerodata that actually will be sent by the stack. 344*10465441SEvalZero 345*10465441SEvalZeroThe application sends data by using the uIP function uip_send(). The 346*10465441SEvalZerouip_send() function takes two arguments; a pointer to the data to be 347*10465441SEvalZerosent and the length of the data. If the application needs RAM space 348*10465441SEvalZerofor producing the actual data that should be sent, the packet buffer 349*10465441SEvalZero(pointed to by the uip_appdata pointer) can be used for this purpose. 350*10465441SEvalZero 351*10465441SEvalZeroThe application can send only one chunk of data at a time on a 352*10465441SEvalZeroconnection and it is not possible to call uip_send() more than once 353*10465441SEvalZeroper application invocation; only the data from the last call will be 354*10465441SEvalZerosent. 355*10465441SEvalZero 356*10465441SEvalZero\subsubsection rexmitdata Retransmitting Data 357*10465441SEvalZero 358*10465441SEvalZeroRetransmissions are driven by the periodic TCP timer. Every time the 359*10465441SEvalZeroperiodic timer is invoked, the retransmission timer for each 360*10465441SEvalZeroconnection is decremented. If the timer reaches zero, a retransmission 361*10465441SEvalZeroshould be made. As uIP does not keep track of packet contents after they have 362*10465441SEvalZerobeen sent by the device driver, uIP requires that the 363*10465441SEvalZeroapplication takes an active part in performing the 364*10465441SEvalZeroretransmission. When uIP decides that a segment should be 365*10465441SEvalZeroretransmitted, the application function is called with the 366*10465441SEvalZerouip_rexmit() flag set, indicating that a retransmission is 367*10465441SEvalZerorequired. 368*10465441SEvalZero 369*10465441SEvalZeroThe application must check the uip_rexmit() flag and produce the same 370*10465441SEvalZerodata that was previously sent. From the application's standpoint, 371*10465441SEvalZeroperforming a retransmission is not different from how the data 372*10465441SEvalZerooriginally was sent. Therefor, the application can be written in such 373*10465441SEvalZeroa way that the same code is used both for sending data and 374*10465441SEvalZeroretransmitting data. Also, it is important to note that even though 375*10465441SEvalZerothe actual retransmission operation is carried out by the application, 376*10465441SEvalZeroit is the responsibility of the stack to know when the retransmission 377*10465441SEvalZeroshould be made. Thus the complexity of the application does not 378*10465441SEvalZeronecessarily increase because it takes an active part in doing 379*10465441SEvalZeroretransmissions. 380*10465441SEvalZero 381*10465441SEvalZero\subsubsection closing Closing Connections 382*10465441SEvalZero 383*10465441SEvalZeroThe application closes the current connection by calling the 384*10465441SEvalZerouip_close() during an application call. This will cause the connection 385*10465441SEvalZeroto be cleanly closed. In order to indicate a fatal error, the 386*10465441SEvalZeroapplication might want to abort the connection and does so by calling 387*10465441SEvalZerothe uip_abort() function. 388*10465441SEvalZero 389*10465441SEvalZeroIf the connection has been closed by the remote end, the test function 390*10465441SEvalZerouip_closed() is true. The application may then do any necessary 391*10465441SEvalZerocleanups. 392*10465441SEvalZero 393*10465441SEvalZero\subsubsection errors Reporting Errors 394*10465441SEvalZero 395*10465441SEvalZeroThere are two fatal errors that can happen to a connection, either 396*10465441SEvalZerothat the connection was aborted by the remote host, or that the 397*10465441SEvalZeroconnection retransmitted the last data too many times and has been 398*10465441SEvalZeroaborted. uIP reports this by calling the application function. The 399*10465441SEvalZeroapplication can use the two test functions uip_aborted() and 400*10465441SEvalZerouip_timedout() to test for those error conditions. 401*10465441SEvalZero 402*10465441SEvalZero\subsubsection polling Polling 403*10465441SEvalZero 404*10465441SEvalZeroWhen a connection is idle, uIP polls the application every time the 405*10465441SEvalZeroperiodic timer fires. The application uses the test function 406*10465441SEvalZerouip_poll() to check if it is being polled by uIP. 407*10465441SEvalZero 408*10465441SEvalZeroThe polling event has two purposes. The first is to let the 409*10465441SEvalZeroapplication periodically know that a connection is idle, which allows 410*10465441SEvalZerothe application to close connections that have been idle for too 411*10465441SEvalZerolong. The other purpose is to let the application send new data that 412*10465441SEvalZerohas been produced. The application can only send data when invoked by 413*10465441SEvalZerouIP, and therefore the poll event is the only way to send data on an 414*10465441SEvalZerootherwise idle connection. 415*10465441SEvalZero 416*10465441SEvalZero\subsubsection listen Listening Ports 417*10465441SEvalZero 418*10465441SEvalZerouIP maintains a list of listening TCP ports. A new port is opened for 419*10465441SEvalZerolistening with the uip_listen() function. When a connection request 420*10465441SEvalZeroarrives on a listening port, uIP creates a new connection and calls 421*10465441SEvalZerothe application function. The test function uip_connected() is true if 422*10465441SEvalZerothe application was invoked because a new connection was created. 423*10465441SEvalZero 424*10465441SEvalZeroThe application can check the lport field in the uip_conn structure to 425*10465441SEvalZerocheck to which port the new connection was connected. 426*10465441SEvalZero 427*10465441SEvalZero\subsubsection connect Opening Connections 428*10465441SEvalZero 429*10465441SEvalZeroNew connections can be opened from within 430*10465441SEvalZerouIP by the function uip_connect(). This function 431*10465441SEvalZeroallocates a new connection and sets a flag in the connection state 432*10465441SEvalZerowhich will open a TCP connection to the specified IP address and port 433*10465441SEvalZerothe next time the connection is polled by uIP. The uip_connect() 434*10465441SEvalZerofunction returns 435*10465441SEvalZeroa pointer to the uip_conn structure for the new 436*10465441SEvalZeroconnection. If there are no free connection slots, the function 437*10465441SEvalZeroreturns NULL. 438*10465441SEvalZero 439*10465441SEvalZeroThe function uip_ipaddr() may be used to pack an IP address into the 440*10465441SEvalZerotwo element 16-bit array used by uIP to represent IP addresses. 441*10465441SEvalZero 442*10465441SEvalZeroTwo examples of usage are shown below. The first example shows how to 443*10465441SEvalZeroopen a connection to TCP port 8080 of the remote end of the current 444*10465441SEvalZeroconnection. If there are not enough TCP connection slots to allow a 445*10465441SEvalZeronew connection to be opened, the uip_connect() function returns NULL 446*10465441SEvalZeroand the current connection is aborted by uip_abort(). 447*10465441SEvalZero 448*10465441SEvalZero\code 449*10465441SEvalZerovoid connect_example1_app(void) { 450*10465441SEvalZero if(uip_connect(uip_conn->ripaddr, HTONS(8080)) == NULL) { 451*10465441SEvalZero uip_abort(); 452*10465441SEvalZero } 453*10465441SEvalZero} 454*10465441SEvalZero\endcode 455*10465441SEvalZero 456*10465441SEvalZeroThe second example shows how to open a new connection to a specific IP 457*10465441SEvalZeroaddress. No error checks are made in this example. 458*10465441SEvalZero 459*10465441SEvalZero\code 460*10465441SEvalZerovoid connect_example2(void) { 461*10465441SEvalZero u16_t ipaddr[2]; 462*10465441SEvalZero 463*10465441SEvalZero uip_ipaddr(ipaddr, 192,168,0,1); 464*10465441SEvalZero uip_connect(ipaddr, HTONS(8080)); 465*10465441SEvalZero} 466*10465441SEvalZero\endcode 467*10465441SEvalZero 468*10465441SEvalZero\section examples Examples 469*10465441SEvalZero 470*10465441SEvalZeroThis section presents a number of very simple uIP applications. The 471*10465441SEvalZerouIP code distribution contains several more complex applications. 472*10465441SEvalZero 473*10465441SEvalZero\subsection example1 A Very Simple Application 474*10465441SEvalZero 475*10465441SEvalZeroThis first example shows a very simple application. The application 476*10465441SEvalZerolistens for incoming connections on port 1234. When a connection has 477*10465441SEvalZerobeen established, the application replies to all data sent to it by 478*10465441SEvalZerosaying "ok" 479*10465441SEvalZero 480*10465441SEvalZeroThe implementation of this application is shown below. The application 481*10465441SEvalZerois initialized with the function called example1_init() and the uIP 482*10465441SEvalZerocallback function is called example1_app(). For this application, the 483*10465441SEvalZeroconfiguration variable UIP_APPCALL should be defined to be 484*10465441SEvalZeroexample1_app(). 485*10465441SEvalZero 486*10465441SEvalZero\code 487*10465441SEvalZerovoid example1_init(void) { 488*10465441SEvalZero uip_listen(HTONS(1234)); 489*10465441SEvalZero} 490*10465441SEvalZero 491*10465441SEvalZerovoid example1_app(void) { 492*10465441SEvalZero if(uip_newdata() || uip_rexmit()) { 493*10465441SEvalZero uip_send("ok\n", 3); 494*10465441SEvalZero } 495*10465441SEvalZero} 496*10465441SEvalZero\endcode 497*10465441SEvalZero 498*10465441SEvalZeroThe initialization function calls the uIP function uip_listen() to 499*10465441SEvalZeroregister a listening port. The actual application function 500*10465441SEvalZeroexample1_app() uses the test functions uip_newdata() and uip_rexmit() 501*10465441SEvalZeroto determine why it was called. If the application was called because 502*10465441SEvalZerothe remote end has sent it data, it responds with an "ok". If the 503*10465441SEvalZeroapplication function was called because data was lost in the network 504*10465441SEvalZeroand has to be retransmitted, it also sends an "ok". Note that this 505*10465441SEvalZeroexample actually shows a complete uIP application. It is not required 506*10465441SEvalZerofor an application to deal with all types of events such as 507*10465441SEvalZerouip_connected() or uip_timedout(). 508*10465441SEvalZero 509*10465441SEvalZero\subsection example2 A More Advanced Application 510*10465441SEvalZero 511*10465441SEvalZeroThis second example is slightly more advanced than the previous one, 512*10465441SEvalZeroand shows how the application state field in the uip_conn structure is 513*10465441SEvalZeroused. 514*10465441SEvalZero 515*10465441SEvalZeroThis application is similar to the first application in that it 516*10465441SEvalZerolistens to a port for incoming connections and responds to data sent 517*10465441SEvalZeroto it with a single "ok". The big difference is that this application 518*10465441SEvalZeroprints out a welcoming "Welcome!" message when the connection has been 519*10465441SEvalZeroestablished. 520*10465441SEvalZero 521*10465441SEvalZeroThis seemingly small change of operation makes a big difference in how 522*10465441SEvalZerothe application is implemented. The reason for the increase in 523*10465441SEvalZerocomplexity is that if data should be lost in the network, the 524*10465441SEvalZeroapplication must know what data to retransmit. If the "Welcome!" 525*10465441SEvalZeromessage was lost, the application must retransmit the welcome and if 526*10465441SEvalZeroone of the "ok" messages is lost, the application must send a new 527*10465441SEvalZero"ok". 528*10465441SEvalZero 529*10465441SEvalZeroThe application knows that as long as the "Welcome!" message has not 530*10465441SEvalZerobeen acknowledged by the remote host, it might have been dropped in 531*10465441SEvalZerothe network. But once the remote host has sent an acknowledgment 532*10465441SEvalZeroback, the application can be sure that the welcome has been received 533*10465441SEvalZeroand knows that any lost data must be an "ok" message. Thus the 534*10465441SEvalZeroapplication can be in either of two states: either in the WELCOME-SENT 535*10465441SEvalZerostate where the "Welcome!" has been sent but not acknowledged, or in 536*10465441SEvalZerothe WELCOME-ACKED state where the "Welcome!" has been acknowledged. 537*10465441SEvalZero 538*10465441SEvalZeroWhen a remote host connects to the application, the application sends 539*10465441SEvalZerothe "Welcome!" message and sets it's state to WELCOME-SENT. When the 540*10465441SEvalZerowelcome message is acknowledged, the application moves to the 541*10465441SEvalZeroWELCOME-ACKED state. If the application receives any new data from the 542*10465441SEvalZeroremote host, it responds by sending an "ok" back. 543*10465441SEvalZero 544*10465441SEvalZeroIf the application is requested to retransmit the last message, it 545*10465441SEvalZerolooks at in which state the application is. If the application is in 546*10465441SEvalZerothe WELCOME-SENT state, it sends a "Welcome!" message since it 547*10465441SEvalZeroknows that the previous welcome message hasn't been acknowledged. If 548*10465441SEvalZerothe application is in the WELCOME-ACKED state, it knows that the last 549*10465441SEvalZeromessage was an "ok" message and sends such a message. 550*10465441SEvalZero 551*10465441SEvalZeroThe implementation of this application is seen below. This 552*10465441SEvalZeroconfiguration settings for the application is follows after its 553*10465441SEvalZeroimplementation. 554*10465441SEvalZero 555*10465441SEvalZero\code 556*10465441SEvalZerostruct example2_state { 557*10465441SEvalZero enum {WELCOME_SENT, WELCOME_ACKED} state; 558*10465441SEvalZero}; 559*10465441SEvalZero 560*10465441SEvalZerovoid example2_init(void) { 561*10465441SEvalZero uip_listen(HTONS(2345)); 562*10465441SEvalZero} 563*10465441SEvalZero 564*10465441SEvalZerovoid example2_app(void) { 565*10465441SEvalZero struct example2_state *s; 566*10465441SEvalZero 567*10465441SEvalZero s = (struct example2_state *)uip_conn->appstate; 568*10465441SEvalZero 569*10465441SEvalZero if(uip_connected()) { 570*10465441SEvalZero s->state = WELCOME_SENT; 571*10465441SEvalZero uip_send("Welcome!\n", 9); 572*10465441SEvalZero return; 573*10465441SEvalZero } 574*10465441SEvalZero 575*10465441SEvalZero if(uip_acked() && s->state == WELCOME_SENT) { 576*10465441SEvalZero s->state = WELCOME_ACKED; 577*10465441SEvalZero } 578*10465441SEvalZero 579*10465441SEvalZero if(uip_newdata()) { 580*10465441SEvalZero uip_send("ok\n", 3); 581*10465441SEvalZero } 582*10465441SEvalZero 583*10465441SEvalZero if(uip_rexmit()) { 584*10465441SEvalZero switch(s->state) { 585*10465441SEvalZero case WELCOME_SENT: 586*10465441SEvalZero uip_send("Welcome!\n", 9); 587*10465441SEvalZero break; 588*10465441SEvalZero case WELCOME_ACKED: 589*10465441SEvalZero uip_send("ok\n", 3); 590*10465441SEvalZero break; 591*10465441SEvalZero } 592*10465441SEvalZero } 593*10465441SEvalZero} 594*10465441SEvalZero\endcode 595*10465441SEvalZero 596*10465441SEvalZeroThe configuration for the application: 597*10465441SEvalZero 598*10465441SEvalZero\code 599*10465441SEvalZero#define UIP_APPCALL example2_app 600*10465441SEvalZero#define UIP_APPSTATE_SIZE sizeof(struct example2_state) 601*10465441SEvalZero\endcode 602*10465441SEvalZero 603*10465441SEvalZero\subsection example3 Differentiating Between Applications 604*10465441SEvalZero 605*10465441SEvalZeroIf the system should run multiple applications, one technique to 606*10465441SEvalZerodifferentiate between them is to use the TCP port number of either the 607*10465441SEvalZeroremote end or the local end of the connection. The example below shows 608*10465441SEvalZerohow the two examples above can be combined into one application. 609*10465441SEvalZero 610*10465441SEvalZero\code 611*10465441SEvalZerovoid example3_init(void) { 612*10465441SEvalZero example1_init(); 613*10465441SEvalZero example2_init(); 614*10465441SEvalZero} 615*10465441SEvalZero 616*10465441SEvalZerovoid example3_app(void) { 617*10465441SEvalZero switch(uip_conn->lport) { 618*10465441SEvalZero case HTONS(1234): 619*10465441SEvalZero example1_app(); 620*10465441SEvalZero break; 621*10465441SEvalZero case HTONS(2345): 622*10465441SEvalZero example2_app(); 623*10465441SEvalZero break; 624*10465441SEvalZero } 625*10465441SEvalZero} 626*10465441SEvalZero\endcode 627*10465441SEvalZero 628*10465441SEvalZero\subsection example4 Utilizing TCP Flow Control 629*10465441SEvalZero 630*10465441SEvalZeroThis example shows a simple application that connects to a host, sends 631*10465441SEvalZeroan HTTP request for a file and downloads it to a slow device such a 632*10465441SEvalZerodisk drive. This shows how to use the flow control functions of uIP. 633*10465441SEvalZero 634*10465441SEvalZero\code 635*10465441SEvalZerovoid example4_init(void) { 636*10465441SEvalZero u16_t ipaddr[2]; 637*10465441SEvalZero uip_ipaddr(ipaddr, 192,168,0,1); 638*10465441SEvalZero uip_connect(ipaddr, HTONS(80)); 639*10465441SEvalZero} 640*10465441SEvalZero 641*10465441SEvalZerovoid example4_app(void) { 642*10465441SEvalZero if(uip_connected() || uip_rexmit()) { 643*10465441SEvalZero uip_send("GET /file HTTP/1.0\r\nServer:192.186.0.1\r\n\r\n", 644*10465441SEvalZero 48); 645*10465441SEvalZero return; 646*10465441SEvalZero } 647*10465441SEvalZero 648*10465441SEvalZero if(uip_newdata()) { 649*10465441SEvalZero device_enqueue(uip_appdata, uip_datalen()); 650*10465441SEvalZero if(device_queue_full()) { 651*10465441SEvalZero uip_stop(); 652*10465441SEvalZero } 653*10465441SEvalZero } 654*10465441SEvalZero 655*10465441SEvalZero if(uip_poll() && uip_stopped()) { 656*10465441SEvalZero if(!device_queue_full()) { 657*10465441SEvalZero uip_restart(); 658*10465441SEvalZero } 659*10465441SEvalZero } 660*10465441SEvalZero} 661*10465441SEvalZero\endcode 662*10465441SEvalZero 663*10465441SEvalZeroWhen the connection has been established, an HTTP request is sent to 664*10465441SEvalZerothe server. Since this is the only data that is sent, the application 665*10465441SEvalZeroknows that if it needs to retransmit any data, it is that request that 666*10465441SEvalZeroshould be retransmitted. It is therefore possible to combine these two 667*10465441SEvalZeroevents as is done in the example. 668*10465441SEvalZero 669*10465441SEvalZeroWhen the application receives new data from the remote host, it sends 670*10465441SEvalZerothis data to the device by using the function device_enqueue(). It is 671*10465441SEvalZeroimportant to note that this example assumes that this function copies 672*10465441SEvalZerothe data into its own buffers. The data in the uip_appdata buffer will 673*10465441SEvalZerobe overwritten by the next incoming packet. 674*10465441SEvalZero 675*10465441SEvalZeroIf the device's queue is full, the application stops the data from the 676*10465441SEvalZeroremote host by calling the uIP function uip_stop(). The application 677*10465441SEvalZerocan then be sure that it will not receive any new data until 678*10465441SEvalZerouip_restart() is called. The application polling event is used to 679*10465441SEvalZerocheck if the device's queue is no longer full and if so, the data flow 680*10465441SEvalZerois restarted with uip_restart(). 681*10465441SEvalZero 682*10465441SEvalZero\subsection example5 A Simple Web Server 683*10465441SEvalZero 684*10465441SEvalZeroThis example shows a very simple file server application that listens 685*10465441SEvalZeroto two ports and uses the port number to determine which file to 686*10465441SEvalZerosend. If the files are properly formatted, this simple application can 687*10465441SEvalZerobe used as a web server with static pages. The implementation follows. 688*10465441SEvalZero 689*10465441SEvalZero\code 690*10465441SEvalZerostruct example5_state { 691*10465441SEvalZero char *dataptr; 692*10465441SEvalZero unsigned int dataleft; 693*10465441SEvalZero}; 694*10465441SEvalZero 695*10465441SEvalZerovoid example5_init(void) { 696*10465441SEvalZero uip_listen(HTONS(80)); 697*10465441SEvalZero uip_listen(HTONS(81)); 698*10465441SEvalZero} 699*10465441SEvalZero 700*10465441SEvalZerovoid example5_app(void) { 701*10465441SEvalZero struct example5_state *s; 702*10465441SEvalZero s = (struct example5_state)uip_conn->appstate; 703*10465441SEvalZero 704*10465441SEvalZero if(uip_connected()) { 705*10465441SEvalZero switch(uip_conn->lport) { 706*10465441SEvalZero case HTONS(80): 707*10465441SEvalZero s->dataptr = data_port_80; 708*10465441SEvalZero s->dataleft = datalen_port_80; 709*10465441SEvalZero break; 710*10465441SEvalZero case HTONS(81): 711*10465441SEvalZero s->dataptr = data_port_81; 712*10465441SEvalZero s->dataleft = datalen_port_81; 713*10465441SEvalZero break; 714*10465441SEvalZero } 715*10465441SEvalZero uip_send(s->dataptr, s->dataleft); 716*10465441SEvalZero return; 717*10465441SEvalZero } 718*10465441SEvalZero 719*10465441SEvalZero if(uip_acked()) { 720*10465441SEvalZero if(s->dataleft < uip_mss()) { 721*10465441SEvalZero uip_close(); 722*10465441SEvalZero return; 723*10465441SEvalZero } 724*10465441SEvalZero s->dataptr += uip_conn->len; 725*10465441SEvalZero s->dataleft -= uip_conn->len; 726*10465441SEvalZero uip_send(s->dataptr, s->dataleft); 727*10465441SEvalZero } 728*10465441SEvalZero} 729*10465441SEvalZero\endcode 730*10465441SEvalZero 731*10465441SEvalZeroThe application state consists of a pointer to the data that should be 732*10465441SEvalZerosent and the size of the data that is left to send. When a remote host 733*10465441SEvalZeroconnects to the application, the local port number is used to 734*10465441SEvalZerodetermine which file to send. The first chunk of data is sent using 735*10465441SEvalZerouip_send(). uIP makes sure that no more than MSS bytes of data is 736*10465441SEvalZeroactually sent, even though s->dataleft may be larger than the MSS. 737*10465441SEvalZero 738*10465441SEvalZeroThe application is driven by incoming acknowledgments. When data has 739*10465441SEvalZerobeen acknowledged, new data can be sent. If there is no more data to 740*10465441SEvalZerosend, the connection is closed using uip_close(). 741*10465441SEvalZero 742*10465441SEvalZero\subsection example6 Structured Application Program Design 743*10465441SEvalZero 744*10465441SEvalZeroWhen writing larger programs using uIP it is useful to be able to 745*10465441SEvalZeroutilize the uIP API in a structured way. The following example 746*10465441SEvalZeroprovides a structured design that has showed itself to be useful for 747*10465441SEvalZerowriting larger protocol implementations than the previous examples 748*10465441SEvalZeroshowed here. The program is divided into an uIP event handler function 749*10465441SEvalZerothat calls seven application handler functions that process new data, 750*10465441SEvalZeroact on acknowledged data, send new data, deal with connection 751*10465441SEvalZeroestablishment or closure events and handle errors. The functions are 752*10465441SEvalZerocalled newdata(), acked(), senddata(), connected(), closed(), 753*10465441SEvalZeroaborted(), and timedout(), and needs to be written specifically for 754*10465441SEvalZerothe protocol that is being implemented. 755*10465441SEvalZero 756*10465441SEvalZeroThe uIP event handler function is shown below. 757*10465441SEvalZero 758*10465441SEvalZero\code 759*10465441SEvalZerovoid example6_app(void) { 760*10465441SEvalZero if(uip_aborted()) { 761*10465441SEvalZero aborted(); 762*10465441SEvalZero } 763*10465441SEvalZero if(uip_timedout()) { 764*10465441SEvalZero timedout(); 765*10465441SEvalZero } 766*10465441SEvalZero if(uip_closed()) { 767*10465441SEvalZero closed(); 768*10465441SEvalZero } 769*10465441SEvalZero if(uip_connected()) { 770*10465441SEvalZero connected(); 771*10465441SEvalZero } 772*10465441SEvalZero if(uip_acked()) { 773*10465441SEvalZero acked(); 774*10465441SEvalZero } 775*10465441SEvalZero if(uip_newdata()) { 776*10465441SEvalZero newdata(); 777*10465441SEvalZero } 778*10465441SEvalZero if(uip_rexmit() || 779*10465441SEvalZero uip_newdata() || 780*10465441SEvalZero uip_acked() || 781*10465441SEvalZero uip_connected() || 782*10465441SEvalZero uip_poll()) { 783*10465441SEvalZero senddata(); 784*10465441SEvalZero } 785*10465441SEvalZero} 786*10465441SEvalZero\endcode 787*10465441SEvalZero 788*10465441SEvalZeroThe function starts with dealing with any error conditions that might 789*10465441SEvalZerohave happened by checking if uip_aborted() or uip_timedout() are 790*10465441SEvalZerotrue. If so, the appropriate error function is called. Also, if the 791*10465441SEvalZeroconnection has been closed, the closed() function is called to the it 792*10465441SEvalZerodeal with the event. 793*10465441SEvalZero 794*10465441SEvalZeroNext, the function checks if the connection has just been established 795*10465441SEvalZeroby checking if uip_connected() is true. The connected() function is 796*10465441SEvalZerocalled and is supposed to do whatever needs to be done when the 797*10465441SEvalZeroconnection is established, such as intializing the application state 798*10465441SEvalZerofor the connection. Since it may be the case that data should be sent 799*10465441SEvalZeroout, the senddata() function is called to deal with the outgoing data. 800*10465441SEvalZero 801*10465441SEvalZeroThe following very simple application serves as an example of how the 802*10465441SEvalZeroapplication handler functions might look. This application simply 803*10465441SEvalZerowaits for any data to arrive on the connection, and responds to the 804*10465441SEvalZerodata by sending out the message "Hello world!". To illustrate how to 805*10465441SEvalZerodevelop an application state machine, this message is sent in two 806*10465441SEvalZeroparts, first the "Hello" part and then the "world!" part. 807*10465441SEvalZero 808*10465441SEvalZero\code 809*10465441SEvalZero#define STATE_WAITING 0 810*10465441SEvalZero#define STATE_HELLO 1 811*10465441SEvalZero#define STATE_WORLD 2 812*10465441SEvalZero 813*10465441SEvalZerostruct example6_state { 814*10465441SEvalZero u8_t state; 815*10465441SEvalZero char *textptr; 816*10465441SEvalZero int textlen; 817*10465441SEvalZero}; 818*10465441SEvalZero 819*10465441SEvalZerostatic void aborted(void) {} 820*10465441SEvalZerostatic void timedout(void) {} 821*10465441SEvalZerostatic void closed(void) {} 822*10465441SEvalZero 823*10465441SEvalZerostatic void connected(void) { 824*10465441SEvalZero struct example6_state *s = (struct example6_state *)uip_conn->appstate; 825*10465441SEvalZero 826*10465441SEvalZero s->state = STATE_WAITING; 827*10465441SEvalZero s->textlen = 0; 828*10465441SEvalZero} 829*10465441SEvalZero 830*10465441SEvalZerostatic void newdata(void) { 831*10465441SEvalZero struct example6_state *s = (struct example6_state *)uip_conn->appstate; 832*10465441SEvalZero 833*10465441SEvalZero if(s->state == STATE_WAITING) { 834*10465441SEvalZero s->state = STATE_HELLO; 835*10465441SEvalZero s->textptr = "Hello "; 836*10465441SEvalZero s->textlen = 6; 837*10465441SEvalZero } 838*10465441SEvalZero} 839*10465441SEvalZero 840*10465441SEvalZerostatic void acked(void) { 841*10465441SEvalZero struct example6_state *s = (struct example6_state *)uip_conn->appstate; 842*10465441SEvalZero 843*10465441SEvalZero s->textlen -= uip_conn->len; 844*10465441SEvalZero s->textptr += uip_conn->len; 845*10465441SEvalZero if(s->textlen == 0) { 846*10465441SEvalZero switch(s->state) { 847*10465441SEvalZero case STATE_HELLO: 848*10465441SEvalZero s->state = STATE_WORLD; 849*10465441SEvalZero s->textptr = "world!\n"; 850*10465441SEvalZero s->textlen = 7; 851*10465441SEvalZero break; 852*10465441SEvalZero case STATE_WORLD: 853*10465441SEvalZero uip_close(); 854*10465441SEvalZero break; 855*10465441SEvalZero } 856*10465441SEvalZero } 857*10465441SEvalZero} 858*10465441SEvalZero 859*10465441SEvalZerostatic void senddata(void) { 860*10465441SEvalZero struct example6_state *s = (struct example6_state *)uip_conn->appstate; 861*10465441SEvalZero 862*10465441SEvalZero if(s->textlen > 0) { 863*10465441SEvalZero uip_send(s->textptr, s->textlen); 864*10465441SEvalZero } 865*10465441SEvalZero} 866*10465441SEvalZero\endcode 867*10465441SEvalZero 868*10465441SEvalZeroThe application state consists of a "state" variable, a "textptr" 869*10465441SEvalZeropointer to a text message and the "textlen" length of the text 870*10465441SEvalZeromessage. The "state" variable can be either "STATE_WAITING", meaning 871*10465441SEvalZerothat the application is waiting for data to arrive from the network, 872*10465441SEvalZero"STATE_HELLO", in which the application is sending the "Hello" part of 873*10465441SEvalZerothe message, or "STATE_WORLD", in which the application is sending the 874*10465441SEvalZero"world!" message. 875*10465441SEvalZero 876*10465441SEvalZeroThe application does not handle errors or connection closing events, 877*10465441SEvalZeroand therefore the aborted(), timedout() and closed() functions are 878*10465441SEvalZeroimplemented as empty functions. 879*10465441SEvalZero 880*10465441SEvalZeroThe connected() function will be called when a connection has been 881*10465441SEvalZeroestablished, and in this case sets the "state" variable to be 882*10465441SEvalZero"STATE_WAITING" and the "textlen" variable to be zero, indicating that 883*10465441SEvalZerothere is no message to be sent out. 884*10465441SEvalZero 885*10465441SEvalZeroWhen new data arrives from the network, the newdata() function will be 886*10465441SEvalZerocalled by the event handler function. The newdata() function will 887*10465441SEvalZerocheck if the connection is in the "STATE_WAITING" state, and if so 888*10465441SEvalZeroswitches to the "STATE_HELLO" state and registers a 6 byte long "Hello 889*10465441SEvalZero" message with the connection. This message will later be sent out by 890*10465441SEvalZerothe senddata() function. 891*10465441SEvalZero 892*10465441SEvalZeroThe acked() function is called whenever data that previously was sent 893*10465441SEvalZerohas been acknowleged by the receiving host. This acked() function 894*10465441SEvalZerofirst reduces the amount of data that is left to send, by subtracting 895*10465441SEvalZerothe length of the previously sent data (obtained from "uip_conn->len") 896*10465441SEvalZerofrom the "textlen" variable, and also adjusts the "textptr" pointer 897*10465441SEvalZeroaccordingly. It then checks if the "textlen" variable now is zero, 898*10465441SEvalZerowhich indicates that all data now has been successfully received, and 899*10465441SEvalZeroif so changes application state. If the application was in the 900*10465441SEvalZero"STATE_HELLO" state, it switches state to "STATE_WORLD" and sets up a 901*10465441SEvalZero7 byte "world!\n" message to be sent. If the application was in the 902*10465441SEvalZero"STATE_WORLD" state, it closes the connection. 903*10465441SEvalZero 904*10465441SEvalZeroFinally, the senddata() function takes care of actually sending the 905*10465441SEvalZerodata that is to be sent. It is called by the event handler function 906*10465441SEvalZerowhen new data has been received, when data has been acknowledged, when 907*10465441SEvalZeroa new connection has been established, when the connection is polled 908*10465441SEvalZerobecause of inactivity, or when a retransmission should be made. The 909*10465441SEvalZeropurpose of the senddata() function is to optionally format the data 910*10465441SEvalZerothat is to be sent, and to call the uip_send() function to actually 911*10465441SEvalZerosend out the data. In this particular example, the function simply 912*10465441SEvalZerocalls uip_send() with the appropriate arguments if data is to be sent, 913*10465441SEvalZeroafter checking if data should be sent out or not as indicated by the 914*10465441SEvalZero"textlen" variable. 915*10465441SEvalZero 916*10465441SEvalZeroIt is important to note that the senddata() function never should 917*10465441SEvalZeroaffect the application state; this should only be done in the acked() 918*10465441SEvalZeroand newdata() functions. 919*10465441SEvalZero 920*10465441SEvalZero\section protoimpl Protocol Implementations 921*10465441SEvalZero 922*10465441SEvalZeroThe protocols in the TCP/IP protocol suite are designed in a layered 923*10465441SEvalZerofashion where each protocol performs a specific function and the 924*10465441SEvalZerointeractions between the protocol layers are strictly defined. While 925*10465441SEvalZerothe layered approach is a good way to design protocols, it is not 926*10465441SEvalZeroalways the best way to implement them. In uIP, the protocol 927*10465441SEvalZeroimplementations are tightly coupled in order to save code space. 928*10465441SEvalZero 929*10465441SEvalZeroThis section gives detailed information on the specific protocol 930*10465441SEvalZeroimplementations in uIP. 931*10465441SEvalZero 932*10465441SEvalZero\subsection ip IP --- Internet Protocol 933*10465441SEvalZero 934*10465441SEvalZeroWhen incoming packets are processed by uIP, the IP layer is the first 935*10465441SEvalZeroprotocol that examines the packet. The IP layer does a few simple 936*10465441SEvalZerochecks such as if the destination IP address of the incoming packet 937*10465441SEvalZeromatches any of the local IP address and verifies the IP header 938*10465441SEvalZerochecksum. Since there are no IP options that are strictly required and 939*10465441SEvalZerobecause they are very uncommon, any IP options in received packets are 940*10465441SEvalZerodropped. 941*10465441SEvalZero 942*10465441SEvalZero\subsubsection ipreass IP Fragment Reassembly 943*10465441SEvalZero 944*10465441SEvalZeroIP fragment reassembly is implemented using a separate buffer that 945*10465441SEvalZeroholds the packet to be reassembled. An incoming fragment is copied 946*10465441SEvalZerointo the right place in the buffer and a bit map is used to keep track 947*10465441SEvalZeroof which fragments have been received. Because the first byte of an IP 948*10465441SEvalZerofragment is aligned on an 8-byte boundary, the bit map requires a 949*10465441SEvalZerosmall amount of memory. When all fragments have been reassembled, the 950*10465441SEvalZeroresulting IP packet is passed to the transport layer. If all fragments 951*10465441SEvalZerohave not been received within a specified time frame, the packet is 952*10465441SEvalZerodropped. 953*10465441SEvalZero 954*10465441SEvalZeroThe current implementation only has a single buffer for holding 955*10465441SEvalZeropackets to be reassembled, and therefore does not support simultaneous 956*10465441SEvalZeroreassembly of more than one packet. Since fragmented packets are 957*10465441SEvalZerouncommon, this ought to be a reasonable decision. Extending the 958*10465441SEvalZeroimplementation to support multiple buffers would be straightforward, 959*10465441SEvalZerohowever. 960*10465441SEvalZero 961*10465441SEvalZero\subsubsection ipbroadcast Broadcasts and Multicasts 962*10465441SEvalZero 963*10465441SEvalZeroIP has the ability to broadcast and multicast packets on the local 964*10465441SEvalZeronetwork. Such packets are addressed to special broadcast and multicast 965*10465441SEvalZeroaddresses. Broadcast is used heavily in many UDP based protocols such 966*10465441SEvalZeroas the Microsoft Windows file-sharing SMB protocol. Multicast is 967*10465441SEvalZeroprimarily used in protocols used for multimedia distribution such as 968*10465441SEvalZeroRTP. TCP is a point-to-point protocol and does not use broadcast or 969*10465441SEvalZeromulticast packets. uIP current supports broadcast packets as well as 970*10465441SEvalZerosending multicast packets. Joining multicast groups (IGMP) and 971*10465441SEvalZeroreceiving non-local multicast packets is not currently supported. 972*10465441SEvalZero 973*10465441SEvalZero\subsection icmp ICMP --- Internet Control Message Protocol 974*10465441SEvalZero 975*10465441SEvalZeroThe ICMP protocol is used for reporting soft error conditions and for 976*10465441SEvalZeroquerying host parameters. Its main use is, however, the echo mechanism 977*10465441SEvalZerowhich is used by the "ping" program. 978*10465441SEvalZero 979*10465441SEvalZeroThe ICMP implementation in uIP is very simple as itis restricted to 980*10465441SEvalZeroonly implement ICMP echo messages. Replies to echo messages are 981*10465441SEvalZeroconstructed by simply swapping the source and destination IP addresses 982*10465441SEvalZeroof incoming echo requests and rewriting the ICMP header with the 983*10465441SEvalZeroEcho-Reply message type. The ICMP checksum is adjusted using standard 984*10465441SEvalZerotechniques (see RFC1624). 985*10465441SEvalZero 986*10465441SEvalZeroSince only the ICMP echo message is implemented, there is no support 987*10465441SEvalZerofor Path MTU discovery or ICMP redirect messages. Neither of these is 988*10465441SEvalZerostrictly required for interoperability; they are performance 989*10465441SEvalZeroenhancement mechanisms. 990*10465441SEvalZero 991*10465441SEvalZero\subsection tcp TCP --- Transmission Control Protocol 992*10465441SEvalZero 993*10465441SEvalZeroThe TCP implementation in uIP is driven by incoming packets and timer 994*10465441SEvalZeroevents. Incoming packets are parsed by TCP and if the packet contains 995*10465441SEvalZerodata that is to be delivered to the application, the application is 996*10465441SEvalZeroinvoked by the means of the application function call. If the incoming 997*10465441SEvalZeropacket acknowledges previously sent data, the connection state is 998*10465441SEvalZeroupdated and the application is informed, allowing it to send out new 999*10465441SEvalZerodata. 1000*10465441SEvalZero 1001*10465441SEvalZero\subsubsection listeb Listening Connections 1002*10465441SEvalZero 1003*10465441SEvalZeroTCP allows a connection to listen for incoming connection requests. In 1004*10465441SEvalZerouIP, a listening connection is identified by the 16-bit port number 1005*10465441SEvalZeroand incoming connection requests are checked against the list of 1006*10465441SEvalZerolistening connections. This list of listening connections is dynamic 1007*10465441SEvalZeroand can be altered by the applications in the system. 1008*10465441SEvalZero 1009*10465441SEvalZero\subsubsection slidingwindow Sliding Window 1010*10465441SEvalZero 1011*10465441SEvalZeroMost TCP implementations use a sliding window mechanism for sending 1012*10465441SEvalZerodata. Multiple data segments are sent in succession without waiting 1013*10465441SEvalZerofor an acknowledgment for each segment. 1014*10465441SEvalZero 1015*10465441SEvalZeroThe sliding window algorithm uses a lot of 32-bit operations and 1016*10465441SEvalZerobecause 32-bit arithmetic is fairly expensive on most 8-bit CPUs, uIP 1017*10465441SEvalZerodoes not implement it. Also, uIP does not buffer sent packets and a 1018*10465441SEvalZerosliding window implementation that does not buffer sent packets will have 1019*10465441SEvalZeroto be supported by a complex application layer. Instead, uIP allows 1020*10465441SEvalZeroonly a single TCP segment per connection to be unacknowledged at any 1021*10465441SEvalZerogiven time. 1022*10465441SEvalZero 1023*10465441SEvalZeroIt is important to note that even though most TCP implementations use 1024*10465441SEvalZerothe sliding window algorithm, it is not required by the TCP 1025*10465441SEvalZerospecifications. Removing the sliding window mechanism does not affect 1026*10465441SEvalZerointeroperability in any way. 1027*10465441SEvalZero 1028*10465441SEvalZero\subsubsection rttest Round-Trip Time Estimation 1029*10465441SEvalZero 1030*10465441SEvalZeroTCP continuously estimates the current Round-Trip Time (RTT) of every 1031*10465441SEvalZeroactive connection in order to find a suitable value for the 1032*10465441SEvalZeroretransmission time-out. 1033*10465441SEvalZero 1034*10465441SEvalZeroThe RTT estimation in uIP is implemented using TCP's periodic 1035*10465441SEvalZerotimer. Each time the periodic timer fires, it increments a counter for 1036*10465441SEvalZeroeach connection that has unacknowledged data in the network. When an 1037*10465441SEvalZeroacknowledgment is received, the current value of the counter is used 1038*10465441SEvalZeroas a sample of the RTT. The sample is used together with Van 1039*10465441SEvalZeroJacobson's standard TCP RTT estimation function to calculate an 1040*10465441SEvalZeroestimate of the RTT. Karn's algorithm is used to ensure that 1041*10465441SEvalZeroretransmissions do not skew the estimates. 1042*10465441SEvalZero 1043*10465441SEvalZero\subsubsection rexmit Retransmissions 1044*10465441SEvalZero 1045*10465441SEvalZeroRetransmissions are driven by the periodic TCP timer. Every time the 1046*10465441SEvalZeroperiodic timer is invoked, the retransmission timer for each 1047*10465441SEvalZeroconnection is decremented. If the timer reaches zero, a retransmission 1048*10465441SEvalZeroshould be made. 1049*10465441SEvalZero 1050*10465441SEvalZeroAs uIP does not keep track of packet contents after they have 1051*10465441SEvalZerobeen sent by the device driver, uIP requires that the 1052*10465441SEvalZeroapplication takes an active part in performing the 1053*10465441SEvalZeroretransmission. When uIP decides that a segment should be 1054*10465441SEvalZeroretransmitted, it calls the application with a flag set indicating 1055*10465441SEvalZerothat a retransmission is required. The application checks the 1056*10465441SEvalZeroretransmission flag and produces the same data that was previously 1057*10465441SEvalZerosent. From the application's standpoint, performing a retransmission 1058*10465441SEvalZerois not different from how the data originally was sent. Therefore the 1059*10465441SEvalZeroapplication can be written in such a way that the same code is used 1060*10465441SEvalZeroboth for sending data and retransmitting data. Also, it is important 1061*10465441SEvalZeroto note that even though the actual retransmission operation is 1062*10465441SEvalZerocarried out by the application, it is the responsibility of the stack 1063*10465441SEvalZeroto know when the retransmission should be made. Thus the complexity of 1064*10465441SEvalZerothe application does not necessarily increase because it takes an 1065*10465441SEvalZeroactive part in doing retransmissions. 1066*10465441SEvalZero 1067*10465441SEvalZero\subsubsection flowcontrol Flow Control 1068*10465441SEvalZero 1069*10465441SEvalZeroThe purpose of TCP's flow control mechanisms is to allow communication 1070*10465441SEvalZerobetween hosts with wildly varying memory dimensions. In each TCP 1071*10465441SEvalZerosegment, the sender of the segment indicates its available buffer 1072*10465441SEvalZerospace. A TCP sender must not send more data than the buffer space 1073*10465441SEvalZeroindicated by the receiver. 1074*10465441SEvalZero 1075*10465441SEvalZeroIn uIP, the application cannot send more data than the receiving host 1076*10465441SEvalZerocan buffer. And application cannot send more data than the amount of 1077*10465441SEvalZerobytes it is allowed to send by the receiving host. If the remote host 1078*10465441SEvalZerocannot accept any data at all, the stack initiates the zero window 1079*10465441SEvalZeroprobing mechanism. 1080*10465441SEvalZero 1081*10465441SEvalZero\subsubsection congestioncontrol Congestion Control 1082*10465441SEvalZero 1083*10465441SEvalZeroThe congestion control mechanisms limit the number of simultaneous TCP 1084*10465441SEvalZerosegments in the network. The algorithms used for congestion control 1085*10465441SEvalZeroare designed to be simple to implement and require only a few lines of 1086*10465441SEvalZerocode. 1087*10465441SEvalZero 1088*10465441SEvalZeroSince uIP only handles one in-flight TCP segment per connection, 1089*10465441SEvalZerothe amount of simultaneous segments cannot be further limited, thus 1090*10465441SEvalZerothe congestion control mechanisms are not needed. 1091*10465441SEvalZero 1092*10465441SEvalZero\subsubsection urgdata Urgent Data 1093*10465441SEvalZero 1094*10465441SEvalZeroTCP's urgent data mechanism provides an application-to-application 1095*10465441SEvalZeronotification mechanism, which can be used by an application to mark 1096*10465441SEvalZeroparts of the data stream as being more urgent than the normal 1097*10465441SEvalZerostream. It is up to the receiving application to interpret the meaning 1098*10465441SEvalZeroof the urgent data. 1099*10465441SEvalZero 1100*10465441SEvalZeroIn many TCP implementations, including the BSD implementation, the 1101*10465441SEvalZerourgent data feature increases the complexity of the implementation 1102*10465441SEvalZerobecause it requires an asynchronous notification mechanism in an 1103*10465441SEvalZerootherwise synchronous API. As uIP already use an asynchronous event 1104*10465441SEvalZerobased API, the implementation of the urgent data feature does not lead 1105*10465441SEvalZeroto increased complexity. 1106*10465441SEvalZero 1107*10465441SEvalZero\section performance Performance 1108*10465441SEvalZero 1109*10465441SEvalZeroIn TCP/IP implementations for high-end systems, processing time is 1110*10465441SEvalZerodominated by the checksum calculation loop, the operation of copying 1111*10465441SEvalZeropacket data and context switching. Operating systems for high-end 1112*10465441SEvalZerosystems often have multiple protection domains for protecting kernel 1113*10465441SEvalZerodata from user processes and user processes from each other. Because 1114*10465441SEvalZerothe TCP/IP stack is run in the kernel, data has to be copied between 1115*10465441SEvalZerothe kernel space and the address space of the user processes and a 1116*10465441SEvalZerocontext switch has to be performed once the data has been 1117*10465441SEvalZerocopied. Performance can be enhanced by combining the copy operation 1118*10465441SEvalZerowith the checksum calculation. Because high-end systems usually have 1119*10465441SEvalZeronumerous active connections, packet demultiplexing is also an 1120*10465441SEvalZeroexpensive operation. 1121*10465441SEvalZero 1122*10465441SEvalZeroA small embedded device does not have the necessary processing power 1123*10465441SEvalZeroto have multiple protection domains and the power to run a 1124*10465441SEvalZeromultitasking operating system. Therefore there is no need to copy 1125*10465441SEvalZerodata between the TCP/IP stack and the application program. With an 1126*10465441SEvalZeroevent based API there is no context switch between the TCP/IP stack 1127*10465441SEvalZeroand the applications. 1128*10465441SEvalZero 1129*10465441SEvalZeroIn such limited systems, the TCP/IP processing overhead is dominated 1130*10465441SEvalZeroby the copying of packet data from the network device to host memory, 1131*10465441SEvalZeroand checksum calculation. Apart from the checksum calculation and 1132*10465441SEvalZerocopying, the TCP processing done for an incoming packet involves only 1133*10465441SEvalZeroupdating a few counters and flags before handing the data over to the 1134*10465441SEvalZeroapplication. Thus an estimate of the CPU overhead of our TCP/IP 1135*10465441SEvalZeroimplementations can be obtained by calculating the amount of CPU 1136*10465441SEvalZerocycles needed for the checksum calculation and copying of a maximum 1137*10465441SEvalZerosized packet. 1138*10465441SEvalZero 1139*10465441SEvalZero\subsection delack The Impact of Delayed Acknowledgments 1140*10465441SEvalZero 1141*10465441SEvalZeroMost TCP receivers implement the delayed acknowledgment algorithm for 1142*10465441SEvalZeroreducing the number of pure acknowledgment packets sent. A TCP 1143*10465441SEvalZeroreceiver using this algorithm will only send acknowledgments for every 1144*10465441SEvalZeroother received segment. If no segment is received within a specific 1145*10465441SEvalZerotime-frame, an acknowledgment is sent. The time-frame can be as high 1146*10465441SEvalZeroas 500 ms but typically is 200 ms. 1147*10465441SEvalZero 1148*10465441SEvalZeroA TCP sender such as uIP that only handles a single outstanding TCP 1149*10465441SEvalZerosegment will interact poorly with the delayed acknowledgment 1150*10465441SEvalZeroalgorithm. Because the receiver only receives a single segment at a 1151*10465441SEvalZerotime, it will wait as much as 500 ms before an acknowledgment is 1152*10465441SEvalZerosent. This means that the maximum possible throughput is severely 1153*10465441SEvalZerolimited by the 500 ms idle time. 1154*10465441SEvalZero 1155*10465441SEvalZeroThus the maximum throughput equation when sending data from uIP will 1156*10465441SEvalZerobe $p = s / (t + t_d)$ where $s$ is the segment size and $t_d$ is the 1157*10465441SEvalZerodelayed acknowledgment timeout, which typically is between 200 and 1158*10465441SEvalZero500 ms. With a segment size of 1000 bytes, a round-trip time of 40 ms 1159*10465441SEvalZeroand a delayed acknowledgment timeout of 200 ms, the maximum 1160*10465441SEvalZerothroughput will be 4166 bytes per second. With the delayed acknowledgment 1161*10465441SEvalZeroalgorithm disabled at the receiver, the maximum throughput would be 1162*10465441SEvalZero25000 bytes per second. 1163*10465441SEvalZero 1164*10465441SEvalZeroIt should be noted, however, that since small systems running uIP are 1165*10465441SEvalZeronot very likely to have large amounts of data to send, the delayed 1166*10465441SEvalZeroacknowledgmen t throughput degradation of uIP need not be very 1167*10465441SEvalZerosevere. Small amounts of data sent by such a system will not span more 1168*10465441SEvalZerothan a single TCP segment, and would therefore not be affected by the 1169*10465441SEvalZerothroughput degradation anyway. 1170*10465441SEvalZero 1171*10465441SEvalZeroThe maximum throughput when uIP acts as a receiver is not affected by 1172*10465441SEvalZerothe delayed acknowledgment throughput degradation. 1173*10465441SEvalZero 1174*10465441SEvalZero 1175*10465441SEvalZero 1176*10465441SEvalZero*/ 1177*10465441SEvalZero 1178*10465441SEvalZero 1179*10465441SEvalZero/** @} */ 1180*10465441SEvalZero 1181