Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals

The uIP TCP/IP stack

Author:
Adam Dunkels <adam@dunkels.com>
The uIP TCP/IP stack is intended to make it possible to communicate using the TCP/IP protocol suite even on small 8-bit micro-controllers. Despite being small and simple, uIP do not require their peers to have complex, full-size stacks, but can communicate with peers running a similarly light-weight stack. The code size is on the order of a few kilobytes and RAM usage can be configured to be as low as a few hundred bytes.

uIP TCP/IP ½ºÅÃÀº ÀÛÀº 8ºñÆ® ¸¶ÀÌÅ©·Î ÄÁÆ®·Ñ·¯¿¡¼­µµ TCP/IP ÇÁ·ÎÅäÄÝÀ» »ç¿ëÇØ¼­ Åë½ÅÇÒ ¼ö ÀÖµµ·Ï ÇÑ´Ù. uIP´Â »ó´ë ³ëµå(peer)°¡ º¹ÀâÇϰí Ç® »çÀÌÁî ½ºÅÃÀ» °¡Áú Çʿ䰡 ¾ø°í °æ·®ÀÇ ½ºÅÃÀ» °¡Áö°í Åë½ÅÀ» ÇÒ ¼ö ÀÖ´Ù. ÄÚµå »çÀÌÁî´Â ¸î ų·Î ¹ÙÀÌÆ®ÀÌ°í ·¥ »ç¿ë·®Àº ¸î ¹é ¹ÙÀÌÆ®·Î ±¸¼ºµÉ ¼ö ÀÖ´Ù.

uIP introduction

With the success of the Internet, the TCP/IP protocol suite has become a global standard for communication. TCP/IP is the underlying protocol used for web page transfers, e-mail transmissions, file transfers, and peer-to-peer networking over the Internet. For embedded systems, being able to run native TCP/IP makes it possible to connect the system directly to an intranet or even the global Internet. Embedded devices with full TCP/IP support will be first-class network citizens, thus being able to fully communicate with other hosts in the network.

ÀÎÅͳÝÀÇ ¼º°øÀ¸·Î TCP/IP ÇÁ·ÎÅäÄÝÀº Àü¼¼°èÀûÀÎ Åë½Å Ç¥ÁØÀÌ µÇ¾ú´Ù. TCP/IP´Â ÀÎÅͳÝÀ» ÅëÇÑ À¥ ÆäÀÌÁö Àü¼Û, ÀÌ ¸ÞÀÏ Àü¼Û, ÆÄÀÏ Àü¼Û, ÇǾîÅõÇÇ¾î ³×Æ®¿öÅ·(peer-to-peer networking)À» À§ÇØ »ç¿ëµÇ´Â ÇÁ·ÎÅäÄÝÀÇ ±â¹ÝÀÌ´Ù. ÀÓº£µðµå ½Ã½ºÅÛ¿¡¼­ ÀÎÆ®¶ó³Ý ¶Ç´Â ÀÎÅͳݿ¡ ½Ã½ºÅÛÀ» ¿¬°áÇÏ´Â °ÍÀÌ °¡´ÉÇÏ´Ù. ¿ÏÀüÇÑ TCP/IP¸¦ Áö¿øÇÏ´Â ÀÓº£µðµå µð¹ÙÀ̽ºµéÀº ù¹øÂ° Ŭ·¡½º ³×Æ®¿öÅ© ±¸¼ºÀÌ µÉ°ÍÀÌ´Ù. µû¶ó¼­ ³×Æ®¿öÅ©¿¡¼­ ´Ù¸¥ È£½ºÆ®¿Í Åë½ÅÇÏ´Â °ÍÀÌ °¡´ÉÇÏ´Ù.


Traditional TCP/IP implementations have required far too much resources both in terms of code size and memory usage to be useful in small 8 or 16-bit systems. Code size of a few hundred kilobytes and RAM requirements of several hundreds of kilobytes have made it impossible to fit the full TCP/IP stack into systems with a few tens of kilobytes of RAM and room for less than 100 kilobytes of code.

ÀüÅëÀûÀÎ TCP/IP ±¸ÇöÀº ÀÛÀº 8ºñÆ® ¶Ç´Â 16ºñÆ® ½Ã½ºÅÛ¿¡¼­ ÄÚµå»çÀÌÁî¿Í ¸Þ¸ð¸® »ç¿ë¿¡¼­ ³Ê¹« ¸¹Àº ÀÚ¿øÀ» ¿ä±¸ ÇØ¿Ô´Ù. ¸î ¹é ų·Î ¹ÙÀÌÆ® ÄÚµå»çÀÌÁî¿Í ·¥ÀÇ ¿ä±¸´Â ¼ö½Ê ų·Î¹ÙÀÌÆ® ·¥°ú 100ų·Î¹ÙÀÌÆ®º¸´Ù ÀÛÀº ¸Þ¸ð¸®¸¦ °¡Áø ½Ã½ºÅÛ¿¡¼­ Ç®»çÀÌÁîÀÇ TCP/IP½ºÅÃÀ» ±¸ÇöÇÏ´Â °ÍÀ» ºÒ°¡´É ÇÏ°Ô Çß´Ù.


The uIP implementation is designed to have only the absolute minimal set of features needed for a full TCP/IP stack. It can only handle a single network interface and contains only a rudimentary UDP implementation, but focuses on the IP, ICMP and TCP protocols. uIP is written in the C programming language.

uIP ±¸ÇöÀº Àüü TCP/IP½ºÅÿ¡¼­ ÇÊ¿äÇÑ ÃÖ¼ÒÇÑÀÇ ºÎºÐ¸¸ °¡Áö°í ÀÖµµ·Ï ¼³°èµÇ¾ú´Ù. uIP´Â ´ÜÀÏ ³×Æ®¿öÅ© ÀÎÅÍÆäÀ̽º¸¸ ´Ù·ê ¼ö ÀÖ°í UDP ±¸ÇöÀ» Æ÷ÇÔÇϰí ÀÖ°í IP, ICMP, TCP¿¡ ÃÊÁ¡ÀÌ ¸ÂÃß¾î Á® ÀÖ°í C¾ð¾î·Î ¸¸µé¾îÁ® ÀÖ´Ù.


Many other TCP/IP implementations for small systems assume that the embedded device always will communicate with a full-scale TCP/IP implementation running on a workstation-class machine. Under this assumption, it is possible to remove certain TCP/IP mechanisms that are very rarely used in such situations. Many of those mechanisms are essential, however, if the embedded device is to communicate with another equally limited device, e.g., when running distributed peer-to-peer services and protocols. uIP is designed to be RFC compliant in order to let the embedded devices to act as first-class network citizens. The uIP TCP/IP implementation that is not tailored for any specific application.

ÀÛÀº ½Ã½ºÅÛÀ» À§ÇÑ ´Ù¸¥ ¸¹Àº TCP/IP ±¸ÇöµéÀº ÀÓº£µðµå µð¹ÙÀ̽º°¡ Ç×»ó ¿÷Å©½ºÅ×ÀÌ¼Ç ¸Ó½Å¿¡¼­ µ¿ÀÛÇÏ´Â Ç®-½ºÄÉÀÏ TCP/IP ±¸ÇöÀ» °¡Áö°í Åë½ÅÇÒ °ÍÀ̶ó°í °¡Á¤ÇÑ´Ù. ÀÌ·± °¡Á¤ÇÏ¿¡, ±×·±»óȲ¿¡¼­ °ÅÀÇ »ç¿ëµÇÁö ¾Ê´Â TCP/IP ¸ÞÄ¿´ÏÁòÀ» Á¦°Å ÇÒ ¼ö ÀÖ´Ù. ÀÓº£µðµå µð¹ÙÀ̽º°¡ ¶È°°ÀÌ ´Ù¸¥ Á¦ÇÑÀûÀÎ µð¹ÙÀ̽º¿Í Åë½ÅÇÑ´Ù¸é ´Ù¸¥ ¸¹Àº ¸ÞÄ¿´ÏÁòµéÀ» ²À ÇÊ¿äÇÏ´Ù. uIP´Â RFC¿Í ȣȯµÇµµ·Ï ¼³°èµÇ¾ú´Ù.


TCP/IP communication

The full TCP/IP suite consists of numerous protocols, ranging from low level protocols such as ARP which translates IP addresses to MAC addresses, to application level protocols such as SMTP that is used to transfer e-mail. The uIP is mostly concerned with the TCP and IP protocols and upper layer protocols will be referred to as "the application". Lower layer protocols are often implemented in hardware or firmware and will be referred to as "the network device" that are controlled by the network device driver.

IP ÁÖ¼Ò¸¦ MAC ÁÖ¼Ò·Î ÀüȯÇÏ´Â ARP¿Í °°Àº ÇÏÀ§ ·¹º§ ÇÁ·ÎÅäÄÝ¿¡¼­ À̸ÞÀÏ Àü¼Û¿¡ »ç¿ëµÇ´Â SMTP¿Í °°Àº ÀÀ¿ë ·¹º§ ÇÁ·ÎÅäÄݱîÁö, Àüü TCP/IP´Â ¸¹Àº ÇÁ·ÎÅäÄÝ·Î ÀÌ·ç¾îÁ® ÀÖ´Ù. uIP´Â ´ëºÎºÐ TCP¿Í IP ÇÁ·ÎÅäÄÝ¿¡ ÁýÁߵǾî ÀÖ°í »óÀ§ °èÃþ ÇÁ·ÎÅäÄÝÀº ¾îÇø®ÄÉÀ̼ÇÀ¸·Î½á ÂüÁ¶µÈ´Ù. ÇÏÀ§ °èÃþ ÇÁ·ÎÅäÄݵéÀº Çϵå¿þ¾î³ª Æß¿þ¾î·Î ±¸ÇöµÇ°í ³×Æ®¿öÅ© µð¹ÙÀ̽º µå¶óÀ̹ö¿¡ÀÇÇØ Á¦¾îµÇ´Â ³×Æ®¿öÅ© µð¹ÙÀ̽º·Î½á ÂüÁ¶µÈ´Ù.


TCP provides a reliable byte stream to the upper layer protocols. It breaks the byte stream into appropriately sized segments and each segment is sent in its own IP packet. The IP packets are sent out on the network by the network device driver. If the destination is not on the physically connected network, the IP packet is forwarded onto another network by a router that is situated between the two networks. If the maximum packet size of the other network is smaller than the size of the IP packet, the packet is fragmented into smaller packets by the router. If possible, the size of the TCP segments are chosen so that fragmentation is minimized. The final recipient of the packet will have to reassemble any fragmented IP packets before they can be passed to higher layers.

TCP´Â »óÀ§ °èÃþ ÇÁ·ÎÅäÄÝ¿¡ ½Å·Ú¼º ÀÖ´Â ¹ÙÀÌÆ® ½ºÆ®¸²À» Á¦°øÇÑ´Ù. TCP´Â ¹ÙÀÌÆ® ½ºÆ®¸²À» Àû´çÇÑ »çÀÌÁîÀÇ ¼¼±×¸ÕÆ®·Î Âɰ³°í °¢ ¼¼±×¸ÕÆ®´Â IP ÆÐŶÀ¸·Î º¸³»Áø´Ù. IP ÆÐŶÀº ³×Æ®¿öÅ© µð¹ÙÀ̽º µå¶óÀ̹ö¿¡ ÀÇÇØ ³×Æ®¿öÅ©À» ÅëÇØ Àü¼ÛµÈ´Ù. ¸ñÀûÁö°¡ ¹°¸®ÀûÀ¸·Î ¿¬°áµÈ ³×Æ®¿öÅ©¿¡ ÀÖÁö ¾ÊÀ¸¸é ¶ó¿ìÅÍ¿¡ ÀÇÇØ ´Ù¸¥ ³×Æ®¿öÅ©À¸·Î Æ÷¿öµù µÈ´Ù. ¸¸¾à ´Ù¸¥ ³×Æ®¿öÅ©ÀÇ ÃÖ´ë ÆÐŶ »çÀÌÁî°¡ IPÆÐŶÀÇ »çÀÌÁ´Ù ÀÛ´Ù¸é, ±× ÆÐŶÀº ¶ó¿ìÅÍ¿¡ ÀÇÇØ ´õ ÀÛÀº ÆÐŶÀ¸·Î Àý´ÜµÈ´Ù. °¡´ÉÇϸé TCP ¼¼±×¸ÕÆ®ÀÇ »çÀÌÁî´Â ´ÜÆíÀÌ ÃÖ¼ÒÈ­ µÇµµ·Ï ¼±ÅõȴÙ. ÆÐŶÀÌ ¸¶Áö¸·À¸·Î ¼ö½ÅµÇ¸é »óÀ§ °èÃþÀ¸·Î Àü´ÞÇϱâ Àü¿¡ Àý´ÜµÈ IPÆÐŶÀ» Àç Á¶¸³ÇÑ´Ù.


The formal requirements for the protocols in the TCP/IP stack is specified in a number of RFC documents published by the Internet Engineering Task Force, IETF. Each of the protocols in the stack is defined in one more RFC documents and RFC1122 collects all requirements and updates the previous RFCs.

TCP/IP ½ºÅÿ¡¼­ ÇÁ·ÎÅäÄÝÀ» À§ÇÑ ÀϹÝÀûÀÎ ¿ä±¸»çÇ×Àº RFC ¹®¼­ ¹øÈ£·Î ±ÔÁ¤µÈ´Ù. ½ºÅÿ¡¼­ °¢ ÇÁ·ÎÅäÄÝÀº Çϳª ÀÌ»óÀÇ RFC ¹®¼­¿¡ Á¤ÀÇ µÇ¾î ÀÖ´Ù. RFC1122´Â ¸ðµç ¿ä±¸»çÇ×µéÀ» ¸ð¾Æ ³õ¾Ò°í ÀÌÀü RFC¸¦ ¾÷µ¥ÀÌÆ® Çß´Ù.


The RFC1122 requirements can be divided into two categories; those that deal with the host to host communication and those that deal with communication between the application and the networking stack. An example of the first kind is "A TCP MUST be able to receive a TCP option in any segment" and an example of the second kind is "There MUST be a mechanism for reporting soft TCP error conditions to the application." A TCP/IP implementation that violates requirements of the first kind may not be able to communicate with other TCP/IP implementations and may even lead to network failures. Violation of the second kind of requirements will only affect the communication within the system and will not affect host-to-host communication.

RFC1122 ¿ä±¸»çÇ×µéÀº µÎ°³ÀÇ Ä«Å×°í¸®·Î ³ª´¶´Ù. È£½ºÆ®¿Í È£½ºÆ®³¢¸®ÀÇ Åë½ÅÀ» ´Ù·ç´Â °Í°ú ¾îÇø®ÄÉÀ̼ǰú ³×Æ®¿öÅ© ½ºÅûçÀÌÀÇ Åë½ÅÀ» ´Ù·ç´Â °ÍÀÌ´Ù. ù ¹øÂ° Á¾·ùÀÇ ¿¹´Â ¡°TCP MUST´Â ¾î¶² ¼¼±×¸ÕÆ®¿¡ TCP ¿É¼ÇÀ» ¼ö½ÅÇÒ¼ö ÀÖ¾î¾ß ÇÑ´Ù.¡± ¿Í µÎ¹øÂ° ¿¹´Â ¡°MUST ´Â TCP ¿¡·¯ »óŸ¦ ¾îÇø®ÄÉÀ̼ǿ¡°Ô ¾Ë·ÁÁÙ¼ö ÀÖ´Â ¸ÞÄÉ´ÏÁòÀÌ ÀÖ´Ù.¡± ù ¹øÂ° ¿ä±¸»çÇ×À» ¾î±â´Â TCP/IP ±¸ÇöÀº ´Ù¸¥ TCP/IP ±¸ÇöºÎ¿Í Åë½ÅÇÒ ¼ö ¾ø°í ³×Æ®¿öÅ© ½ÇÆÐ¸¦ ÃÊ·¡ ÇÑ´Ù. µÎ¹øÂ° Á¾·ùÀÇ ¿ä±¸»çÇ×À» ¾î±â´Â °ÍÀº ½Ã½ºÅÛ ³»¿¡¼­ ´ÜÁö Åë½Å¿¡ ¿µÇâÀ» ¹ÌÄ¡°í È£½ºÆ®¿Í È£½ºÆ® »çÀÌ Åë½Å¿¡´Â ¿µÇâÀ» ¹ÌÄ¡Áö ¾Ê´Â´Ù.


In uIP, all RFC requirements that affect host-to-host communication are implemented. However, in order to reduce code size, we have removed certain mechanisms in the interface between the application and the stack, such as the soft error reporting mechanism and dynamically configurable type-of-service bits for TCP connections. Since there are only very few applications that make use of those features they can be removed without loss of generality.

uIP¿¡¼­ È£½ºÆ®¿Í È£½ºÆ® »çÀÌ Åë½Å¿¡ ¿µÇâÀ» ¹ÌÄ¡´Â RFC¿ä±¸»çÇ×µéÀº ±¸ÇöµÇ¾î ÀÖ´Ù. ±×·¯³ª ÄÚµå»çÀÌÁ ÁÙÀ̱â À§ÇØ, ¿¡·¯¸¦ ¾Ë¸®´Â ¸ÞÄ¿´ÏÁò°ú TCP¿¬°áÀ» À§ÇØ µ¿ÀûÀ¸·Î ±¸¼º°¡´ÉÇÑ ¼­ºñ½ºÅ¸ÀÔ ºñÆ®µé°ú °°Àº ¾îÇø®ÄÉÀ̼ǰú ½ºÅûçÀÌÀÇ ÀÎÅÍÆäÀ̽º¿¡ ƯÁ¤ ¸ÞÄ¿´ÏÁòÀº Á¦°Å ÇÏ¿´´Ù.



Memory management

In the architectures for which uIP is intended, RAM is the most scarce resource. With only a few kilobytes of RAM available for the TCP/IP stack to use, mechanisms used in traditional TCP/IP cannot be directly applied.

uIP¿¡¼­ Ãß±¸ÇÏ´Â ¾ÆÅ°ÅØÃÄ¿¡¼­ ·¥Àº ¸Å¿ì ±ÍÇÑ ÀÚ¿øÀÌ´Ù. ¸î ų·Î ¹ÙÀÌÆ®ÀÇ ·¥À¸·Îµµ TCP/IP ½ºÅà ±¸ÇöÀÌ °¡´ÉÇÏÁö¸¸, ÀüÅëÀûÀÎ TCP/IP¿¡¼­ÀÇ ¸ÞÄ¿´ÏÁò¿¡¼­´Â Á÷Á¢ Àû¿ëµÉ ¼ö ¾ø´Ù.


The uIP stack does not use explicit dynamic memory allocation. Instead, it uses a single global buffer for holding packets and has a fixed table for holding connection state. The global packet buffer is large enough to contain one packet of maximum size. When a packet arrives from the network, the device driver places it in the global buffer and calls the TCP/IP stack. If the packet contains data, the TCP/IP stack will notify the corresponding application. Because the data in the buffer will be overwritten by the next incoming packet, the application will either have to act immediately on the data or copy the data into a secondary buffer for later processing. The packet buffer will not be overwritten by new packets before the application has processed the data. Packets that arrive when the application is processing the data must be queued, either by the network device or by the device driver. Most single-chip Ethernet controllers have on-chip buffers that are large enough to contain at least 4 maximum sized Ethernet frames. Devices that are handled by the processor, such as RS-232 ports, can copy incoming bytes to a separate buffer during application processing. If the buffers are full, the incoming packet is dropped. This will cause performance degradation, but only when multiple connections are running in parallel. This is because uIP advertises a very small receiver window, which means that only a single TCP segment will be in the network per connection.

uIP´Â ½ºÅÃÀº ¸í½ÃÀûÀ¸·Î µ¿Àû ¸Þ¸ð¸® ÇÒ´çÀ» »ç¿ëÇÏÁö ¾Ê´Â´Ù. ´ë½Å, ÆÐŶÀ» ÀúÀåÇϱâ À§ÇØ Àü¿ª ¹öÆÛ¸¦ »ç¿ëÇÏ°í ¿¬°á »óŸ¦ ÀúÀåÇϱâ À§ÇØ °íÁ¤µÈ Å×À̺íÀ» °¡Áø´Ù. Àü¿ª ÆÐŶ ¹öÆÛ´Â ÇϳªÀÇ ÃÖ´ë »çÀÌÁîÀÇ ÆÐŶÀ» ´ã±â¿¡ ÃæºÐÈ÷ Å©´Ù. ÆÐŶÀÌ ³×Æ®¿öÅ©À¸·ÎºÎÅÍ ¼ö½ÅµÇ¾úÀ» ¶§ µð¹ÙÀ̽º µå¶óÀ̺ê´Â ±× ÆÐŶÀ» Àü¿ª ¹öÆÛ¿¡ ³Ö°í TCP/IP½ºÅÃÀ» È£ÃâÇÑ´Ù. ÆÐŶÀÌ µ¥ÀÌÅ͸¦ °¡Áö°í ÀÖ´Ù¸é, TCP/IP ½ºÅÃÀº ÇØ´ç ¾îÇø®ÄÉÀ̼ǿ¡°Ô ¾Ë¸°´Ù. ¹öÆÛ¿¡ ÀÖ´Â µ¥ÀÌÅÍ´Â ´ÙÀ½¿¡ ¿À´Â ÆÐŶ¿¡ ÀÇÇØ µ¤¾î ½áÁú ¼ö Àֱ⠶§¹®¿¡, ¾îÇø®ÄÉÀ̼ÇÀº Áï½Ã µ¥ÀÌÅ͸¦ Áï½Ã ó¸®Çϰųª ³ªÁß¿¡ ó¸®Çϱâ À§ÇØ µ¥ÀÌÅ͸¦ ´Ù¸¥ ¹öÆÛ¿¡ º¹»çÇØ µÎ¾î¾ß ÇÑ´Ù. ÆÐŶ ¹öÆÛ´Â ¾îÇø®ÄÉÀ̼ÇÀÌ µ¥ÀÌÅ͸¦ ó¸®Çϱâ Àü¿¡ »õ·Î¿î ÆÐŶ¿¡ ÀÇÇØ µ¤¾î½á ÁöÁö ¾ÊÀ» °ÍÀÌ´Ù. ¾îÇø®ÄÉÀ̼ÇÀÌ µ¥ÀÌÅ͸¦ ó¸®Çϰí ÀÖ´Â Áß¿¡ µµÂøÇÑ ÆÐŶÀº ³×Æ®¿öÅ© µð¹ÙÀ̽º³ª µð¹ÙÀ̽º µå¶óÀ̹ö¿¡ ÀÇÇØ Å¥À× µÇ¾î¾ß ÇÑ´Ù. ´ëºÎºÐÀÇ ½Ì±Û-Ĩ ÀÌ´õ³Ý ÄÁÆ®·Ñ·¯´Â ÃÖ¼Ò 4°³ÀÇ ÃÖ´ë ±æÀÌÀÇ ÇÁ·¹ÀÓÀ» ´ãÀ» ¼ö ÀÖ´Â ¿Â-Ĩ(On-Chip) ¹öÆÛ¸¦ °¡Áö°í ÀÖ´Ù. RS-232 Æ÷Æ®¿Í °°ÀÌ ÇÁ·Î¼¼¼­°¡ ´Ù·ç´Â µð¹ÙÀ̽º´Â ¾îÇø®ÄÉÀ̼ÇÀÌ Ã³¸®Çϰí ÀÖ´Â Áß¿¡, ÀԷµǴ ¹ÙÀÌÆ®¸¦ µ¶¸³µÈ ¹öÆÛ¿¡ º¹»çÇØ µÐ´Ù. ¸¸¾à ¹öÆÛ°¡ ²Ë ã´Ù¸é, ¼ö½ÅµÈ ÆÐŶÀº ¹ö·ÁÁø´Ù. À̰ÍÀº ´ÙÁß ¿¬°áÀÌ º´·Ä·Î µ¿ÀÛ µÉ ¶§ ¼º´ÉÀ» ÀúÇϽÃŰ°Ô µÈ´Ù. À̰ÍÀº uIP°¡ ÀÛÀº ¼ö½Å À©µµ¿ì¸¦ °¡Áö°í Àֱ⠶§¹®ÀÌ°í ³×Æ®¿öÅ© ¿¬°á´ç ÇϳªÀÇ TCP¼¼±×¸ÕÆ®°¡ ÀÖ¾î¾ß µÈ´Ù´Â °ÍÀ» ÀǹÌÇÑ´Ù.


In uIP, the same global packet buffer that is used for incoming packets is also used for the TCP/IP headers of outgoing data. If the application sends dynamic data, it may use the parts of the global packet buffer that are not used for headers as a temporary storage buffer. To send the data, the application passes a pointer to the data as well as the length of the data to the stack. The TCP/IP headers are written into the global buffer and once the headers have been produced, the device driver sends the headers and the application data out on the network. The data is not queued for retransmissions. Instead, the application will have to reproduce the data if a retransmission is necessary.

uIP¿¡¼­, ¼ö½Å ÆÐŶÀ» À§ÇØ »ç¿ëµÇ´Â °°Àº Àü¿ª ÆÐŶ ¹öÆÛ´Â ¼Û½Å µ¥ÀÌÅÍÀÇ TCP/IP Çì´õ¸¦ À§ÇØ »ç¿ëµÈ´Ù. ¾îÇø®ÄÉÀ̼ÇÀÌ µ¿Àû µ¥ÀÌÅ͸¦ ¼Û½ÅÇÑ´Ù¸é, ¾îÇø®ÄÉÀ̼ÇÀº Àӽà ÀúÀå ¹öÆÛ·Î½á Çì´õ¸¦ À§ÇØ »ç¿ëµÇÁö ¾Ê´Â Àü¿ª ÆÐŶ ¹öÆÛÀÇ ÀϺθ¦ »ç¿ëÇÑ´Ù. µ¥ÀÌÅ͸¦ ¼Û½ÅÇϱâ À§ÇØ, ¾îÇø®ÄÉÀ̼ÇÀº µ¥ÀÌÅÍ Æ÷ÀÎÆ®¿Í µ¥ÀÌÅÍÀÇ ±æÀ̸¦ ½ºÅÿ¡ Àü´Þ ÇÑ´Ù. Àü¿ª ¹öÆÛ¿¡ TCP/IP Çì´õ¸¦ ÀúÀåÇϰí Çì´õ°¡ ¸¸µé¾î Áö¸é µð¹ÙÀ̽º µå¶óÀ̹ö´Â Çì´õ¿Í ¾îÇø®ÄÉÀÌ¼Ç µ¥ÀÌÅ͸¦ ³×Æ®¿öÅ©À¸·Î Àü¼ÛÇÑ´Ù. µ¥ÀÌÅÍ´Â ÀçÀü¼ÛÀ» À§ÇØ Å¥À×µÇÁö ¾Ê´Â´Ù. ´ë½Å, ¾îÇø®ÄÉÀ̼ÇÀº ÀçÀü¼ÛÀÌ ÇÊ¿äÇÏ´Ù¸é µ¥ÀÌÅ͸¦ Àç»ý¼º ÇÑ´Ù.


The total amount of memory usage for uIP depends heavily on the applications of the particular device in which the implementations are to be run. The memory configuration determines both the amount of traffic the system should be able to handle and the maximum amount of simultaneous connections. A device that will be sending large e-mails while at the same time running a web server with highly dynamic web pages and multiple simultaneous clients, will require more RAM than a simple Telnet server. It is possible to run the uIP implementation with as little as 200 bytes of RAM, but such a configuration will provide extremely low throughput and will only allow a small number of simultaneous connections.

uIPÀÇ Àüü ¸Þ¸ð¸® »ç¿ë·®Àº ±¸ÇöºÎ°¡ µ¹¾Æ°¡°Ô µÇ´Â µð¹ÙÀ̽º ¾îÇø®ÄÉÀ̼ǿ¡ ÀÇÁ¸ÀûÀÌ´Ù. ¸Þ¸ð¸® ±¸¼ºÀº ½Ã½ºÅÛÀÌ ´Ù·ê ¼ö ÀÖ´Â Æ®·¡ÇÈ(traffic) ¾ç°ú ÃÖ´ë·Î µ¿½Ã¿¡ ¿¬°á ÇÒ ¼ö ÀÖ´Â ¾çÀ» °áÁ¤ Áþ´Â´Ù. Å« ÀÌ ¸ÞÀÏÀ» º¸³»°í µ¿½Ã¿¡ µ¿Àû À¥ ÆäÀÌÁö¸¦ °¡Áö°í ÀÖ´Â À¥ ¼­¹ö°¡ µ¿ÀÛÇϰí ÀÖ´Â µð¹ÙÀ̽º´Â °£´ÜÇÑ ÅÚ³Ý ¼­¹öº¸´Ù ¸¹Àº ·¥ÀÌ ¿ä±¸µÈ´Ù. uIP´Â 200¹ÙÀÌÆ® ·¥À¸·Îµµ µ¿ÀÛµÉ ¼ö ÀÖ´Ù. ±×·¯³ª ±×·¯ÇÑ ±¸¼ºÀº ³·Àº 󸮷®À» Á¦°øÇÏ°í µ¿½Ã Á¢¼Ó¼ö¸¦ ÀÛ°Ô ÇÑ´Ù.



Application program interface (API)

The Application Program Interface (API) defines the way the application program interacts with the TCP/IP stack. The most commonly used API for TCP/IP is the BSD socket API which is used in most Unix systems and has heavily influenced the Microsoft Windows WinSock API. Because the socket API uses stop-and-wait semantics, it requires support from an underlying multitasking operating system. Since the overhead of task management, context switching and allocation of stack space for the tasks might be too high in the intended uIP target architectures, the BSD socket interface is not suitable for our purposes.

ÀÀ¿ë ÇÁ·Î±×·¥ ÀÎÅÍÆäÀ̽º´Â ÀÀ¿ë ÇÁ·Î±×·¥ÀÌ TCP/IP ½ºÅðú »óÈ£ ÀÛ¿ëÇÏ´Â ¹æ¹ýÀ» Á¤ÀÇÇϰí ÀÖ´Ù. TCP/IP¿¡¼­ »ç¿ëµÇ´Â ´ëºÎºÐÀº À¯´Ð½º ½Ã½ºÅÛ¿¡¼­ »ç¿ëµÇ´Â BSD ¼ÒÄÏÀÌ°í ¸¶ÀÌÅ©·Î ¼ÒÇÁÆ® À©µµ¿ìÀÇ WinSock API¿¡ ¿µÇâÀ» ÁÖ¾ú´Ù. ¼ÒÄÏ API´Â stop-and-wait ¹æ½ÄÀ» »ç¿ëÇϱ⠶§¹®¿¡ ¸ÖƼŸ½ºÅ· ¿ÀÆÛ·¹ÀÌ¼Ç ½Ã½ºÅÛ¿¡¼­ Áö¿øµÇ´Â °ÍÀÌ ¿ä±¸µÈ´Ù. Ÿ½ºÅ© °ü¸®ÀÇ ¿À¹öÇìµå ¶§¹®¿¡, Ÿ½ºÅ©¸¦ À§ÇÑ ¹®¸Æ ±³È¯°ú ½ºÅà °ø°£ ÇÒ´çÀº uIP ŸÄÏ ¾ÆÅ°ÅØÃÄ¿¡¼­´Â ¿À¹öÇìµå°¡ Å©´Ù. BSD ¼ÒÄÏ ÀÎÅÍÆäÀ̽º´Â ¿ì¸®ÀÇ ¸ñÀû¿¡´Â Àû´çÇÏÁö ¾Ê´Ù.


Instead, uIP uses an event driven interface where the application is invoked in response to certain events. An application running on top of uIP is implemented as a C function that is called by uIP in response to certain events. uIP calls the application when data is received, when data has been successfully delivered to the other end of the connection, when a new connection has been set up, or when data has to be retransmitted. The application is also periodically polled for new data. The application program provides only one callback function; it is up to the application to deal with mapping different network services to different ports and connections. Because the application is able to act on incoming data and connection requests as soon as the TCP/IP stack receives the packet, low response times can be achieved even in low-end systems.

´ë½Å, uIP´Â ¾îÇø®ÄÉÀ̼ÇÀÌ ¾î¶°ÇÑ À̺¥Æ®¿¡ ÀÀ´äÀ¸·Î È£ÃâµÇ´Â À̺¥Æ® µå¸®ºì(event driven) ÀÎÅÍÆäÀ̽º¸¦ »ç¿ëÇÑ´Ù. uIP À§¿¡¼­ µ¿À۵Ǵ ¾îÇø®ÄÉÀ̼ÇÀº ¾î¶°ÇÑ À̺¥Æ® ÀÀ´äÀ¸·Î uIP¿¡ÀÇÇØ È£ÃâµÇ´Â CÇÔ¼ö·Î ±¸ÇöµÇ¾î ÀÖ´Ù. uIP´Â µ¥ÀÌÅͰ¡ ¼ö½ÅµÇ¾úÀ» ¶§, µ¥ÀÌÅͰ¡ ¿¬°áµÈ ¹Ý´ëÆí¿¡ ¼º°øÀûÀ¸·Î Àü´Þ µÇ¾úÀ» ¶§, »õ·Î¿î ¿¬°áÀÌ ¼Â¾÷ µÇ¾úÀ» ¶§, µ¥ÀÌÅͰ¡ ÀçÀü¼Û µÇ¾úÀ» ¶§, ¾îÇø®ÄÉÀ̼ÇÀ» È£ÃâÇÑ´Ù. ¾îÇø®ÄÉÀ̼ÇÀº µ¥ÀÌÅ͸¦ ÁÖ±âÀûÀ¸·Î Æú¸µÇÑ´Ù. ÀÀ¿ë ÇÁ·Î±×·¥Àº ´ÜÁö ÇϳªÀÇ Äݹé ÇÔ¼ö¸¦ Á¦°øÇÑ´Ù.


uIP is different from other TCP/IP stacks in that it requires help from the application when doing retransmissions. Other TCP/IP stacks buffer the transmitted data in memory until the data is known to be successfully delivered to the remote end of the connection. If the data needs to be retransmitted, the stack takes care of the retransmission without notifying the application. With this approach, the data has to be buffered in memory while waiting for an acknowledgment even if the application might be able to quickly regenerate the data if a retransmission has to be made.

uIP´Â ÀçÀü¼Û ÇÒ ¶§ ¾îÇø®ÄÉÀ̼ǿ¡°Ô µµ¿òÀ» ¿äûÇÏ´Â Á¡¿¡¼­ ´Ù¸¥ ½ºÅõé°ú Â÷ÀÌÁ¡ÀÌ ÀÖ´Ù. ´Ù¸¥ TCP/IP½ºÅÃÀº µ¥ÀÌŸ°¡ ¹Ý´ëÆí¿¡ ¼º°øÀûÀ¸·Î Àü´ÞµÇ¾ú´Ù°í ÆÇ´Ü µÉ¶§ ±îÁö µ¥ÀÌŸ¸¦ ¸Þ¸ð¸®¿¡ ´ã°í ÀÖ´Ù. ¸¸¾à µ¥ÀÌŸ°¡ ÀçÀü¼Û µÇ¾ßÇÑ´Ù¸é ½ºÅÃÀº ¾îÇø®ÄÉÀ̼ǰú »ó°ü¾øÀÌ ÀçÀü¼ÛÀ» ¼öÇàÇÑ´Ù. µû¶ó¼­ ¾îÇø®ÄÉÀ̼ÇÀÌ ±ÞÈ÷ ÀçÀü¼ÛÀ» ¼öÇàÇÏ·ÁÇØµµ ¸Þ¸ð¸®¾È¿¡ ACK¸Þ½ÃÁö¸¦ ±â´Ù¸®´Â µ¥ÀÌÅÍ ¶§¹®¿¡ ÀçÀü¼ÛÀ» ¼öÇàÇÏÁö ¸øÇÏ´Â °æ¿ì°¡ »ý±â°Ô µÈ´Ù.


In order to reduce memory usage, uIP utilizes the fact that the application may be able to regenerate sent data and lets the application take part in retransmissions. uIP does not keep track of packet contents after they have been sent by the device driver, and uIP requires that the application takes an active part in performing the retransmission. When uIP decides that a segment should be retransmitted, it calls the application with a flag set indicating that a retransmission is required. The application checks the retransmission flag and produces the same data that was previously sent. From the application's standpoint, performing a retransmission is not different from how the data originally was sent. Therefore the application can be written in such a way that the same code is used both for sending data and retransmitting data. Also, it is important to note that even though the actual retransmission operation is carried out by the application, it is the responsibility of the stack to know when the retransmission should be made. Thus the complexity of the application does not necessarily increase because it takes an active part in doing retransmissions.

¸Þ¸ð¸® »ç¿ëÀ» ÁÙÀ̱â À§Çؼ­, uIP´Â ¾îÇø®ÄÉÀ̼ǰú ÇÔ²² ÀçÀü¼ÛÀ» ¼öÇàÇÑ´Ù. uIP°¡ ÀçÀü¼ÛÇÏ·Á°í ÇÒ¶§, ÀçÀü¼Û ¿äû Ç÷¡±×¿Í ÇÔ²² ¾îÇø®ÄÉÀ̼ÇÀ» È£ÃâÇÑ´Ù.. ¾îÇø®ÄÉÀ̼ÇÀº ÀçÀü¼Û Ç÷¡±×¸¦ º¸°í ¾Õ¼­º¸³½ µ¥ÀÌÅÍ¿Í °°Àº µ¥ÀÌÅ͸¦ »ý¼ºÇÑ´Ù. ¾îÇø®ÄÉÀ̼ÇÀÇ ÀÔÀå¿¡¼­´Â ÀçÀü¼ÛÀ» ¼öÇàÇÏ´Â °ÍÀº ¿ø·¡ µ¥ÀÌÅÍ Àü¼ÛÇϴ°Ͱú ´Ù¸£Áö ¾Ê°Ô ¿©±â±â ¶§¹®¿¡ µ¥ÀÌÅÍÀü¼Û°ú ÀçÀü¼ÛÀÇ Äڵ尡 °°´Ù. µû¶ó¼­ uIP½ºÅÃÀÌ ÀçÀü¼Û ¿äû¸¸ ÇØÁشٸé, ¾îÇø®ÄÉÀ̼ÇÀº º°´Ù¸¥ ÄÚµå º¹À⼺¾øÀÌ ÀçÀü¼ÛÀ» ¼öÇàÇÏ°Ô µÈ´Ù.


Application events

The application must be implemented as a C function, UIP_APPCALL(), that uIP calls whenever an event occurs. Each event has a corresponding test function that is used to distinguish between different events. The functions are implemented as C macros that will evaluate to either zero or non-zero. Note that certain events can happen in conjunction with each other (i.e., new data can arrive at the same time as data is acknowledged).

uIP´Â À̺¥Æ®°¡ ¹ß»ýµÉ¶§¸¶´Ù ¾îÇø®ÄÉÀ̼ÇÀÇ UIP_APPCALL()À» È£ÃâÇÑ´Ù. °¢°¢ÀÇ À̺¥Æ®µé ¸¶´Ù Å×½ºÆ®ÇÔ¼ö¸¦ µÖ¼­ À̺¥Æ®µéÀ» ±¸ºÐÇÑ´Ù. ÀÌ ÇÔ¼ö´Â C¸ÞÅ©·Î ó·³ ±¸ÇöµÇ¾îÁ® zore ¶Ç´Â non-zero¸¦ ¸®ÅÏÇÑ´Ù. ÁÖÀÇÇØ¾ß ÇÒ °æ¿ì´Â ¾î¶² À̺¥Æ®µéÀº µ¿½Ã ´Ù¹ßÀûÀ¸·Î ÀϾ ¼ö ÀÖ´Ù´Â °ÍÀÌ´Ù.(¿¹¸¦ µé¾î,»õ·Î¿î µ¥ÀÌŸ°¡ ACK¸Þ½ÃÁö¿Í µ¿½Ã¿¡ µµÂøÇÑ´Ù.)


The connection pointer

When the application is called by uIP, the global variable uip_conn is set to point to the uip_conn structure for the connection that currently is handled, and is called the "current connection". The fields in the uip_conn structure for the current connection can be used, e.g., to distinguish between different services, or to check to which IP address the connection is connected. One typical use would be to inspect the uip_conn->lport (the local TCP port number) to decide which service the connection should provide. For instance, an application might decide to act as an HTTP server if the value of uip_conn->lport is equal to 80 and act as a TELNET server if the value is 23.

¾îÇø®ÄÉÀ̼ÇÀÌ uIP¿¡ ÀÇÇØ È£ÃâµÉ¶§, Àü¿ªº¯¼ö uip_conn´Â ÇöÀç ¿¬°áÀ» ÇÚµéÇϱâÀ§ÇØ ±¸Á¶Ã¼ uip_conn¸¦ Æ÷ÀÎÆ®ÇÑ´Ù. À̸¦ "current connection"À̶ó°í ºÎ¸¥´Ù. uip_conn¿¡ ÀÖ´Â ÇʵåµéÀº ÇöÀç Á¢¼ÓÀÌ »ç¿ëµÇ¾î Áú¼öÀÖµµ·Ï, ¼­ºñ½ºµéÀ» ±¸º°Çϱâ À§Çؼ­,¶Ç´Â ¿¬°áµÈ Á¢¼ÓÀÇ IPÀÇ ÁÖ¼Ò¸¦ üũÇÒ ¿ëµµ·Î »ç¿ëµÇ¾îÁø´Ù. ÀüÇüÀûÀ¸·Î uip_conn->lport (local TCP Æ÷Æ®³Ñ¹ö)´Â ¾î´À ¼­ºñ½º°¡ Á¦°øµÇ´ÂÁö ÁöÁ¤ÇÑ´Ù. ¿¹¸¦ µé¾î uip_conn->lport°¡ 80À¸·Î ÁöÁ¤µÇÀÖ´Ù¸é ¾îÇø®ÄÉÀ̼ÇÀº HTTP ¼­ºñ½º¸¦ ÇÒ°ÍÀ̰í, 23À¸·Î ÁöÁ¤µÇÀÖ´Ù¸é TELNET¼­ºñ½º¸¦ ÇÒ °ÍÀÌ´Ù.


Receiving data

If the uIP test function uip_newdata() is non-zero, the remote host of the connection has sent new data. The uip_appdata pointer point to the actual data. The size of the data is obtained through the uIP function uip_datalen(). The data is not buffered by uIP, but will be overwritten after the application function returns, and the application will therefor have to either act directly on the incoming data, or by itself copy the incoming data into a buffer for later processing.

¸¸¾à uIPÀÇ Å×½ºÆ® ÇÔ¼ö uip_newdata()ÀÌ ³ÍÁ¦·Î¸é, ¿ø°ÝÁöÀÇ È£½ºÆ®°¡ »õ·Î¿î µ¥ÀÌÅ͸¦ Àü¼ÛÇÑ °ÍÀÌ´Ù. uip_appdata Æ÷ÀÎÅÍ´Â ½ÇÁ¦ µ¥ÀÌÅ͸¦ Æ÷ÀÎÆ®ÇÑ´Ù. µ¥ÀÌÅÍÀÇ Å©±â´Â uIP ÇÔ¼öuip_datalen()¸¦ ÅëÇØ ¾ò´Â´Ù. ±× µ¥ÀÌÅÍ´Â uIP¿¡ ÀÇÇØ ÀúÀåµÇÁö´Â ¾ÊÁö¸¸ ¾îÇø®ÄÉÀ̼ÇÀÌ µé¾î¿Â µ¥ÀÌÅÍ¿¡ Á÷Á¢ ¹ÝÀÀÇϰųª ÃßÈÄ¿¡ ó¸®ÇϱâÀ§ÇØ º¹»çÇÑ ÈÄ¿¡ µ¤¾î¾º¾î Áø´Ù.


Sending data

When sending data, uIP adjusts the length of the data sent by the application according to the available buffer space and the current TCP window advertised by the receiver. The amount of buffer space is dictated by the memory configuration. It is therefore possible that all data sent from the application does not arrive at the receiver, and the application may use the uip_mss() function to see how much data that actually will be sent by the stack.

µ¥ÀÌÅ͸¦ º¸³¾¶§ uIP´Â ¾îÇø®ÄÉÀ̼ǿ¡ ÀÇÇØ °è»êµÈ µ¥ÀÌÅÍÀÇ Å©±â¸¦ Àû¿ëÇÑ´Ù. ¹öÆÛÅ©±â´Â ¸Þ¸ð¸® ¼³Á¤¿¡ ÀÇÇØ °áÁ¤µÇ±â ¶§¹®¿¡ º¸³»´Â ÂÊÀÇ ¸ðµç µ¥ÀÌÅÍ´Â ¹Þ´ÂÂÊ¿¡¼­ µµÂøÇÏÁö ¾ÊÀ» ¼ö°¡ ÀÖ´Ù. ±×¸®°í uip_mss()ÇÔ¼ö¸¦ »ç¿ëÇØ¼­ º¸³¾ ¼ö ÀÖ´Â µ¥ÀÌÅÍÀÇ Å©±â°¡ ¾ó¸¶ÀÎÁö ¾Ë ¼öÀÖ´Ù.


The application sends data by using the uIP function uip_send(). The uip_send() function takes two arguments; a pointer to the data to be sent and the length of the data. If the application needs RAMspace for producing the actual data that should be sent, the packet buffer (pointed to by the uip_appdata pointer) can beused for this purpose.

¾îÇø®ÄÉÀ̼ÇÀº uip_send()ÇÔ¼ö¸¦ »ç¿ëÇØ¼­ µ¥ÀÌÅ͸¦ Àü¼ÛÇÑ´Ù. ÀÌ ÇÔ¼ö´Â µÎ°³ÀÇ ¸Å°³º¯¼ö¸¦ °®´Âµ¥ Çϳª´Â µ¥ÀÌÅÍÀÇ Æ÷ÀÎÅÍÀ̰í, ´Ù¸¥ Çϳª´Â µ¥ÀÌÅÍÀÇ Å©±âÀÌ´Ù. ¾îÇø®ÄÉÀ̼ÇÀÌ µ¥ÀÌÅ͸¦ Àü¼ÛÇϱâÀ§ÇÑ RAM°ø°£ÀÌ ÇÊ¿äÇÏ´Ù¸é ÆÐŶ¹öÆÛ(uip_appdataÆ÷ÀÎÅͰ¡ Æ÷ÀÎÆ®ÇÏ´Â °ø°£)°¡ ÀÌ ¿ëµµ·Î »ç¿ëµÇ¾îÁø´Ù.


The application can send only one chunk of data at a time on a connection and it is not possible to call uip_send() more than once per application invocation; only the data from the last call will be sent.

¾îÇø®ÄÉÀ̼ÇÀº ÇϳªÀÇ ¿¬°á¿¡ ÇѰ³ÀÇ data chunk¸¦ Àü¼ÛÇÒ¼öÀÖ´Ù. ¾îÇø®ÄÉÀÌ¼Ç ´ç Çѹø ÀÌ»ó uip_send()ÇÔ¼ö¸¦ È£Ãâ ÇÒ ¼ö ¾ø´Ù.


Retransmitting data

Retransmissions are driven by the periodic TCP timer. Every time the periodic timer is invoked, the retransmission timer for each connection is decremented. If the timer reaches zero, a retransmission should be made. As uIP does not keep track of packet contents after they have been sent by the device driver, uIP requires that the application takes an active part in performing the retransmission. When uIP decides that a segment should be retransmitted, the application function is called with the uip_rexmit() flag set, indicating that a retransmission is required.

ÀçÀü¼ÛÀº ÁÖ±âÀûÀÎ TCPŸÀ̸ӿ¡ ÀÇÇØ µ¿ÀÛÇÑ´Ù. Àü¼ÛÇÒ¶§ ¸¶´Ù ÁÖ±âÀû ŸÀ̸Ӱ¡ È£ÃâµÇ°í, ÀçÀü¼ÛŸÀ̸Ӱ¡ ÁÙ¾îµé±â ½ÃÀÛÇÑ´Ù. ÀçÀü¼Û ŸÀ̸Ӱ¡ 0À̵Ǹé ÀçÀü¼Û·çƾÀÌ ¼öÇàµÈ´Ù. uIP´Â µð¹ÙÀ̽º µå¶óÀ̹ö¸¦ ÅëÇØ Àü¼ÛµÈ ÈÄÀÇ ÆÐŶÀº °¡Áö°í ÀÖÁö ¾Ê´Â ´ë½Å¿¡ ¾îÇø®ÄÉÀ̼ǿ¡°Ô ÀçÀü¼ÛÀ» ¿äûÇϴµ¥, uip_rexmit()ÇÔ¼ö¸¦ ÅëÇØ¼­ ÀçÀü¼Û ¿äû Ç÷¡±×¸¦ ¼¼ÆÃÇÑ´Ù.


The application must check the uip_rexmit() flag and produce the same data that was previously sent. From the application's standpoint, performing a retransmission is not different from how the data originally was sent. Therefor, the application can be written in such a way that the same code is used both for sending data and retransmitting data. Also, it is important to note that even though the actual retransmission operation is carried out by the application, it is the responsibility of the stack to know when the retransmission should be made. Thus the complexity of the application does not necessarily increase because it takes an active part in doing retransmissions.

¾îÇø®ÄÉÀ̼ÇÀº uip_rexmit()·Î Ç÷¡±×¸¦ üũÇϰí ÀÌÀü¿¡ Àü¼ÛÇÑ µ¥ÀÌÅÍ¿Í ¶È°°Àº µ¥ÀÌÅ͸¦ ¸¸µç´Ù. ¾îÇø®ÄÉÀ̼ÇÀÇ °üÁ¡¿¡¼­ º¸¸é,ÀçÀü¼ÛÀ» ¼öÇàÇϴ°ÍÀº µ¥ÀÌÅ͸¦ Àü¼ÛÇÏ´Â ·çƾ°ú Â÷À̸¦ µÎÁö ¾Ê±â ¶§¹®¿¡ °°Àº Äڵ带 »ç¿ëÇÑ´Ù. ¶ÇÇÑ Áß¿äÇѰÍÀº ÀçÀü¼Û ¿©ºÎÀÇ ÆÇ´ÜÀ» ½ºÅÿ¡¼­ Çϱ⶧¹®¿¡ ¾îÇø®ÄɽüÇÀÇ º¹À⼺(complexity)Àº Áõ°¡ µÇÁö ¾Ê´Â´Ù.


Closing connections

The application closes the current connection by calling the uip_close() during an application call. This will cause the connection to be cleanly closed. In order to indicate a fatal error, the application might want to abort the connection and does so by calling the uip_abort() function. If the connection has been closed by the remote end, the test function uip_closed() is true. The application may then do any necessary cleanups.

¾îÇø®ÄÉÀ̼ÇÀº ÇöÀçÀÇ ¿¬°áÀ» uip_close()ÇÔ¼ö¸¦ È£ÃâÇÏ¹Ç·Î½á ¿¬°áÀ» ±ú²ýÇÏ°Ô closeÇÑ´Ù. ¿¡·¯¸¦ º¸°íÇϱâ À§Çؼ­´Â ¾îÇø®ÄÉÀ̼ÇÀº uip_abort()ÇÔ¼ö·Î ¿¬°áÀ» Ãë¼ÒÇÑ´Ù. ¸¸¾à »ó´ëÆí¿¡ ÀÇÇØ¼­ ¿¬°áÀÌ Á¾·áµÇ¾ú´Ù¸é , uip_closed() Å×½ºÆ® ÇÔ¼öÀÇ ¸®ÅϰªÀº 1(true)°¡ µÈ´Ù. À̰æ¿ì ¾îÇø®ÄÉÀ̼ǿ¡¼­´Â ¿¬°áÁ¾·á¸¦ µû·Î ó¸®ÇÒ Çʿ䰡 ¾ø´Ù.


Reporting errors

There are two fatal errors that can happen to a connection, either that the connection was aborted by the remote host, or that the connection retransmitted the last data too many times and has been aborted. uIP reports this by calling the application function. The application can use the two test functions uip_aborted() and uip_timedout() to test for those error conditions.

¿¬°á¿¡¼­ ¹ß»ý µÉ ¼ö ÀÖ´Â Ä¡¸íÀûÀÎ ¿¡·¯°¡ µÎ°³ ÀÖ´Ù. »ó´ëÆí È£½ºÆ®¿¡ ÀÇÇØ Ãë¼ÒµÈ ¿¬°áÀ̰ųª, ÀçÀü¼Û ÇÒ ¶§ ¸¶Áö¸· µ¥ÀÌÅÍÀÇ Å¸À̸Ӱ¡ 0ÀÌ µÇ¼­ Ãë¼ÒµÈ °æ¿ìÀÌ´Ù. uIP´Â ¾îÇø®ÄÉÀ̼ÇÀ» È£ÃâÇϹǷνá ÀÌ ¿¡·¯µéÀ» º¸°íÇÑ´Ù. ¾îÇø®ÄÉÀ̼ÇÀº uip_aborted(), uip_timedout() ÀÌ µÎ°¡Áö Å×½ºÆ® ÇÔ¼ö¸¦ »ç¿ëÇØ¼­ ¿¡·¯¿©ºÎ¸¦ Å×½ºÆ®ÇÑ´Ù.


Polling

When a connection is idle, uIP polls the application every time the periodic timer fires. The application uses the test function uip_poll() to check if it is being polled by uIP. The polling event has two purposes. The first is to let the application periodically know that a connection is idle, which allows the application to close connections that have been idle for too long. The other purpose is to let the application send new data that has been produced. The application can only send data when invoked by uIP, and therefore the poll event is the only way to send data on an otherwise idle connection.

¿¬°áÀÌ ÇѰ¡ÇÒ¶§, uIP´Â ÁÖ±âÀû ŸÀ̸Ӹ¦ »ç¿ëÇØ¼­ ¾îÇø®ÄÉÀ̼ÇÀ» °Ë»ç(poll)ÇÑ´Ù. ±× ¾îÇø®ÄÉÀ̼ÇÀº uip_poll()À̶õ Å×½ºÆ® ÇÔ¼ö¸¦ »ç¿ëÇØ¼­ uIP°¡ ¾îÇø®ÄÉÀ̼ÇÀ» °Ë»çÁßÀÎÁö ¾Æ´ÑÁö¸¦ Å×½ºÆ®ÇÑ´Ù. Æú¸µ¿¡´Â µÎ°¡Áö ¸ñÀûÀ» °®´Â´Ù. ù¹øÂ°´Â ¾îÇø®ÄÉÀ̼ÇÀÌ ÁÖ±âÀûÀ¸·Î ¿¬°áÀÇ ÇѰ¡ÇÔÀ» ¾Ë°ÔÇÔÀ¸·Î¼­, ³Ê¹« ¸¹ÀÌ ½¬°íÀÖ´Â ¿¬°áµéÀ» close½Ã۱â À§ÇÔÀÌ´Ù. ´Ù¸¥ ¸ñÀûÀº ¾îÇø®ÄÉÀ̼ÇÀÌ »õ·Î¿î µ¥ÀÌÅ͸¦ Àü¼ÛÇÏ°Ô ÇÔ¿¡ ÀÖ´Ù. ¾îÇø®ÄÉÀ̼ÇÀº ¿ÀÁ÷ uIP¿¡ ÀÇÇØ È£ÃâµÉ¶§ ¸¸ µ¥ÀÌÅ͸¦ Àü¼ÛÇÒ¼öÀֱ⠶§¹®¿¡ °Ë»ç½ÅÈ£(poll event)´Â ¿¬°áÀÌ idle»óŸ¦ ±ú¿ö¼­ µ¥ÀÌÅ͸¦ Àü¼ÛÇÏ°Ô ÇÒ¼öÀÖ´Â À¯ÀÏÇÑ ¹æ¹ýÀ̵ȴÙ.


Listening ports

uIP maintains a list of listening TCP ports. A new port is opened for listening with the uip_listen() function. When a connection request arrives on a listening port, uIP creates a new connection and calls the application function. The test function uip_connected() is true if the application was invoked because a new connection was created. The application can check the lport field in the uip_conn structure to check to which port the new connection was connected.

uIP´Â ¸®½º´× TCPÆ÷Æ®ÀÇ ¸®½ºÆ®¸¦ °¡Áö°íÀÖ´Ù. uip_listen()ÇÔ¼ö¸¦ ÅëÇØ¼­ »õ·Î¿î Æ÷Æ®¸¦ ¿­ ¼ö ÀÖ´Ù. ¸®½º´× Æ÷Æ®·Î ¿¬°á¿äûÀÌ µé¾î¿ÔÀ»¶§, uIP´Â »õ·Î¿î ¿¬°áÀ» ¼³Á¤ÇÏ°í ¾îÇø®ÄÉÀÌ¼Ç ÇÔ¼ö¸¦ È£ÃâÇÑ´Ù. ¾îÇø®ÄÉÀ̼ÇÀº Å×½ºÆ® ÇÔ¼öuip_connected()¸¦ ÅëÇØ¼­ ¿¬°á¿äû ¿©ºÎ¸¦ Å×½ºÆ®ÇÑ´Ù. true¸é »õ·Î¿î ¿¬°áÀÌ ¸¸µé¾î Áø°ÍÀÌ´Ù. ¾îÇø®ÄÉÀ̼ÇÀº uip_conn±¸Á¶Ã¼ÀÇ Çʵ尪 lport¸¦ üũÇÔÀ¸·Î¼­ »õ·Î¿î ¿¬°á¿¡ »ç¿ëµÇ´Â Æ÷Æ®°¡ ¸î¹øÀÎÁö ¾Ë¼öÀÖ´Ù.


Opening connections

New connections can be opened from within uIP by the function uip_connect(). This function allocates a new connection and sets a flag in the connection state which will open a TCP connection to the specified IP address and port the next time the connection is polled by uIP. The uip_connect() function returns a pointer to the uip_conn structure for the new connection. If there are no free connection slots, the function returns NULL.

»õ·Î¿î ¿¬°áµéÀº uIP°¡ uip_connect()ÇÔ¼ö¸¦ È£ÃâÇÔÀ¸·Î¼­ ¿¬°áµÈ´Ù. ÀÌ ÇÔ¼ö´Â »õ·Î¿î ¿¬°áÀ» ÇÒ´çÇÏ°í ¿¬°á»óÅ Ç÷¡±×¸¦ ¼¼ÆÃÇÑ´Ù. ipÁÖ¼Ò³ª Æ÷Æ®µîÀ» ÀúÀåÇØ¼­ ´ÙÀ½ ¿¬°á½Ã¿¡´Â uIP¿¡ ÀÇÇØ °Ë»çµÈ´Ù. uip_connect()ÇÔ¼ö´Â uip_conn±¸Á¶Ã¼¸¦ ¸®ÅÏÇϴµ¥ ¸¸¾à ¿©À¯ÀÖ´Â ¿¬°á½½·ÔÀÌ ¾ø´Ù¸é NULLÀ» ¸®ÅÏÇÑ´Ù.


The function uip_ipaddr() may be used to pack an IP address into the two element 16-bit array used by uIP to represent IP addresses. Two examples of usage are shown below. The first example shows how to open a connection to TCP port 8080 of the remote end of the current connection. If there are not enough TCP connection slots to allow a new connection to be opened, the uip_connect() function returns NULL and the current connection is aborted by uip_abort().

uIP_connect()ÇÔ¼ö´Â IPÁÖ¼Ò¸¦ Ç¥ÇöÇϱâÀ§ÇØ uip_ipaddr()ÇÔ¼ö¸¦ »ç¿ëÇϴµ¥ 16ºñÆ® Å©±âÀÇ µÎ°³ÀÇ ¿¤¸®¸ÕÆ®·Î ±¸¼ºµÈ ¹è¿­¿¡ ´ã¾Æ Ç¥ÇöÇÑ´Ù. ´ÙÀ½ µÎ°¡Áö ¿¹Á¦¸¦ »ìÆìº¸ÀÚ. ù¹øÂ° ¿¹Á¦´Â »õ·Î¿î ¿¬°áÀÌ TCP Æ÷Æ® 8080¿¡ ÇÒ´ç µÇ´Â ¿¹Á¦ÀÌ´Ù. ¸¸¾à TCP¿¬°á ½½·Ô¿¡ ¿©À¯°ø°£ÀÌ ¾ø´Ù¸é uip_connect()ÇÔ¼ö´Â NULLÀ» ¸®ÅÏÇϰí uip_abort()ÇÔ¼ö·Î ¿¬°áÀ» ÇØÁöÇÒ °ÍÀÌ´Ù.


void connect_example1_app(void) {

   if(uip_connect(uip_conn->ripaddr, HTONS(8080)) == NULL) {

      uip_abort();

   }

}   

The second example shows how to open a new connection to a specific IP address. No error checks are made in this example.

µÎ¹øÂ° ¿¹Á¦´Â IP¸¦ ÁöÁ¤ÇÏ´Â ¿¹Á¦ÀÌ´Ù. ÀÌ ¿¹Á¦¿¡¼­´Â ¿¡·¯Ã¼Å©¸¦ ÇÏÁö¾Ê´Â´Ù.

void connect_example2(void) {

   u16_t ipaddr[2];



   uip_ipaddr(ipaddr, 192,168,0,1);

   uip_connect(ipaddr, HTONS(8080));

}

uIP device drivers

From the network device driver's standpoint, uIP consists of two C functions: uip_input() and uip_periodic(). The uip_input() function should be called by the device driver when an IP packet has been received and put into the uip_buf packet buffer. The uip_input() function will process the packet, and when it returns an outbound packet may have been placed in the same uip_buf packet buffer (indicated by the uip_len variable being non-zero). The device driver should then send out this packet onto the network.

³×Æ®¿öÅ© µð¹ÙÀ̽º µå¶óÀ̹öÀÇ °üÁ¡¿¡¼­ º»´Ù¸é uIP´Â uip_input()ÇÔ¼ö¿Í uip_periodic()ÇÔ¼ö·Î ±¸¼ºµÈ´Ù. uip_input()ÇÔ¼ö´Â IPÆÐŶÀ» ¹Þ¾Æ uip_buf ÆÐŶ¹öÆÛ¿¡ ³ÖÀ»¶§ µð¹ÙÀ̽º µå¶óÀ̹ö¿¡ ÀÇÇØ È£ÃâµÈ´Ù. ÀÌ ÇÔ¼ö´Â ÆÐŶÀ» ó¸®ÇÏ°í ±× µ¥ÀÌÅ͸¦ ¸®ÅÏÇÑ´Ù. (uip_lenº¯¼ö°¡ ³ÍÁ¦·Î¸é ÆÐŶÀ» ó¸®Çؼ­ Àü¼ÛÇÒ¼öÀÖ´Â ÆÐŶÀÓÀ» ¸»ÇÑ´Ù.) µð¹ÙÀ̽º µå¶óÀ̹ö´Â uip_input()ÀÌ ¸®ÅÏÇÑ µ¥ÀÌÅ͸¦ ³×Æ®¿öÅ©·Î ³»º¸³½´Ù.


The uip_periodic() function should be invoked periodically once per connection by the device driver, typically one per second. This function is used by uIP to drive protocol timers and retransmissions, and when it returns it may have placed an outbound packet in the uip_buf buffer.

uip_periodic()ÇÔ¼ö´Â µð¹ÙÀ̽º µå¶óÀ̹ö¿¡ ÀÇÇØ ÁÖ±âÀûÀ¸·Î È£ÃâµÇ´Âµ¥ ÁÖ·Î ÀÏÃÊ¿¡ Çѹø È£ÃâµÈ´Ù. ÀÌ ÇÔ¼ö´Â uIP°¡ ÇÁ·ÎÅäÄÝÀ» ŸÀÌ¸Ó¿Í ÀçÀü¼ÛÀÛ¾÷À» ÄÁÆ®·Ñ ÇÒ¶§ »ç¿ëÇÏ´Â ÇÔ¼öÀÌ´Ù. ±×¸®°í uip_periodic()ÇÔ¼ö°¡ ¸®ÅÏ ÇÒ ¶§´Â uip_buf¿¡ ¹ÛÀ¸·Î ³ª°¥¼öÀÖ´Â ÆÐŶ(outbound packet)ÀÌ ¸¸µé¾îÁ® ÀÖÀ» °ÍÀÌ´Ù.


Architecture specific functions

uIP requires a few functions to be implemented specifically for the architecture on which uIP is intended to run. These functions should be hand-tuned for the particular architecture, but generic C implementations are given as part of the uIP distribution.

uIP¸¦ »ç¿ëÇÏ´Â ¾ÆÅ°ÅØÃÄ´Â uIPÀÇ ¸î°¡Áö ±â´ÉµéÀ» ÀÚ¼¼È÷ ±¸ÇöÇØ¾ßÇÑ´Ù. ÀÌ ÀÛ¾÷Àº ¼ÕÀÌ ¸¹À̰¡´Â ÀÛ¾÷ÀÌÁö¸¸ uIP¹èÆ÷º»¿¡¼­ C·Î ±¸ÇöÇÑ Äڵ带 ¾òÀ» ¼öÀÖ´Ù.


Checksum calculation

The TCP and IP protocols implement a checksum that covers the data and header portions of the TCP and IP packets. Since the calculation of this checksum is made over all bytes in every packet being sent and received it is important that the function that calculates the checksum is efficient. Most often, this means that the checksum calculation must be fine-tuned for the particular architecture on which the uIP stack runs.

TCP¿Í IPÇÁ·ÎÅäÄÝÀº °¢°¢ÀÇ Çì´õºÎºÐ¿¡ üũ¼¶ Çʵ带 µÎ¾î üũ¼¶À» ¼öÇàÇϴµ¥ ÀÌ·± üũ¼¶ °è»êÀº ÆÐŶÀ» º¸³¾¶§¿Í ¹ÞÀ»¶§ ¸Å¹ø °è»êµÇ¾îÁø´Ù. uIP½ºÅÃÀÌ µ¹°íÀÖ´Â ¾ÆÅ°ÅØÃĵµ üũ¼¶°è»êÀÌ ÇÊ¿äÇѵ¥ üũ¼¶ °è»êÀ» ¾ó¸¶³ª È¿À²ÀûÀ¸·Î ÇÏ´À³Ä°¡ Áß¿äÇÏ´Ù.


Because of this, uIP does not implement a generic checksum function, but leaves this to the architecture specific files which must implement the two functions uip_ipchksum() and uip_tcpchksum(). The checksum calculations in those functions can be written in highly optimized assembler rather than generic C code. An example C implementation of the checksum function is provided in the uIP distribution.

ÀÌ·¸±â ¶§¹®¿¡ uIP´Â ÀϹÝÀûÀΠüũ¼¶ ÇÔ¼ö¸¦ ¾²Áö¾Ê´Â´Ù. ÇÏÁö¸¸ uip_ipchksum()ÇÔ¼ö¿Í uip_tcpchksum()ÇÔ¼ö¸¦ »ç¿ëÇØ¼­ üũ¼¶À» ±¸ÇöÇÒ¼öÀÖ´Ù. ÀÌ µÎ ÇÔ¼öµéÀº ¾î¼Àºí¸®¾î·Î ±¸ÇöµÇ¾îÀÖ´Ù. C·Î ±¸ÇöµÈ üũ¼¶ ¿¹Á¦°¡ uIP ¹èÆ÷ÆÇ¿¡ Á¦°øµÈ´Ù.


32-bit arithmetic

The TCP protocol uses 32-bit sequence numbers, and a TCP implementation will have to do a number of 32-bit additions as part of the normal protocol processing. Since 32-bit arithmetic is not natively available on many of the platforms for which uIP is intended, uIP leaves the 32-bit additions to be implemented by the architecture specific module and does not make use of any 32-bit arithmetic in the main code base. The architecture specific code must implement a function uip_add32() which does a 32-bit addition and stores the result in a global variable uip_acc32.

TCPÇÁ·ÎÅäÄÝÀº 32ºñÆ® ¿¬»êÀ» ÇÑ´Ù. ÇÏÁö¸¸ uIP¸¦ ¾²´Â ´ëºÎºÐÀÇ ½Ã½ºÅÛÀº 32ºñÆ® ¿¬»êÀ» ÇÏÁö¾Ê´Â´Ù. uIP´Â 32ºñÆ® ¿¬»êÀ» main Äڵ忡¼­ ÇÏÁö¾Ê°í ƯÁ¤ ¸ðµâÀ» ÅëÇØ¼­ ±¸ÇöµÈ´Ù. ±× ƯÁ¤ ¸ðµâÀº uip_add32()ÇÔ¼ö¸¦ ±¸ÇöÇØ¾ß ÇÑ´Ù. ÀÌ ÇÔ¼ö´Â Àü¿ªº¯¼ö uip_acc32¿¡ 32ºñÆ® ¿¬»êÀÇ °á°ú¸¦ ÀúÀåÇÑ´Ù.


Examples

This section presents a number of very simple uIP applications. The uIP code distribution contains several more complex applications.

ÀÌ ¼½¼Ç¿¡¼­´Â ¾ÆÁÖ °£´ÜÇÑ uIP¿¹Á¦¸¦ ¼³¸íÇÑ´Ù. uIP¹èÆ÷ÆÇ¿¡´Â Á»´õ ÀÚ¼¼ÇÏ°í º¹ÀâÇÑ ¾îÇø®ÄÉÀ̼ǵéÀÌ Á¦°øµÈ´Ù.


A very simple application

This first example shows a very simple application. The application listens for incoming connections on port 1234. When a connection has been established, the application replies to all data sent to it by saying "ok"

ù¹øÂ° ¿¹Á¦´Â ¾ÆÁÖ °£´ÜÇÑ ¿¹Á¦ÀÔ´Ï´Ù. ¾îÇø®ÄÉÀ̼ÇÀÌ 1234Æ÷Æ®·Î ¸®½º´×À» ÇÑ´Ù. ¿¬°áÀÌ µÇ¸é ¾îÇø®ÄÉÀ̼ÇÀº ÀÀ´äÀ¸·Î "ok"¸¦ Àü¼ÛÇÑ´Ù.


The implementation of this application is shown below. The application is initialized with the function called example1_init() and the uIP callback function is called example1_app(). For this application, the configuration variable UIP_APPCALL should be defined to be example1_app().

¹ØÀÇ ¼Ò½º¿¡¼­Ã³·³ ¾îÇø®ÄÉÀ̼ÇÀº example1_init()·Î ÃʱâÈ­Çϰí example1_app()ÄݹéÇÔ¼ö¸¦ È£ÃâÇÑ´Ù. ¼³Á¤º¯¼öÀÎ UIP_APPCALL¿¡ example1_app()°¡ ¿¬°á µÇ¾ß ÇÑ´Ù.


void example1_init(void) {

   uip_listen(HTONS(1234));

}



void example1_app(void) {

   if(uip_newdata() || uip_rexmit()) {

      uip_send("ok\n", 3);

   }

}

The initialization function calls the uIP function uip_listen() to register a listening port. The actual application function example1_app() uses the test functions uip_newdata() and uip_rexmit() to determine why it was called. If the application was called because the remote end has sent it data, it responds with an "ok". If the application function was called because data was lost in the network and has to be retransmitted, it also sends an "ok". Note that this example actually shows a complete uIP application. It is not required for an application to deal with all types of events such as uip_connected() or uip_timedout().

ÃʱâÈ­ ÇÔ¼ö´Â ¸®½º´× Æ÷Æ®¸¦ µî·ÏÇϱâ À§Çؼ­ uIPÇÔ¼ö uip_listen()À» È£ÃâÇÑ´Ù. ½ÇÁ¦ ¾îÇø®ÄÉÀÌ¼Ç ÇÔ¼ö example1_app()´Â uip_newdata()¿Í uip_rexmit()¸¦ Å×½ºÆ®ÇÑ´Ù. »õ·Î¿î µ¥ÀÌÅ͸¦ ¹ÞÀº°æ¿ì,³×Æ®¿öÅ©»ó¿¡¼­ ÆÐŶÀÌ ºÐ½ÇµÇ ÀçÀü¼Û ¿äûÀÎ °æ¿ì¿¡ "ok"¸¦ º¸³½´Ù. ÀÌ ¿¹Á¦¿¡´Â ¾îÇø®ÄÉÀ̼ÇÀÇ Æ²¸¸ º¸¿©ÁÙ »Ó uip_connected()³ª uip_timedout()°ú °°Àº À̺¥Æ®µéÀÇ ¸ðµç ŸÀÔµéÀº ´Ù·çÁö ¾Ê´Â´Ù.


A more advanced application

This second example is slightly more advanced than the previous one, and shows how the application state field in the uip_conn structure is used.

µÎ¹øÂ° ¿¹Á¦¿¡¼­´Â ¾Õ¼­ »ìÆìº» ¿¹Á¦º¸´Ù Á»´õ Áøº¸µÈ ¿¹Á¦¸¦ »ìÆìº»´Ù. uip_conn±¸Á¶Ã¼¸¦ »ç¿ëÇØ¼­ °¢ ÇʵåµéÀ» »ìÆì º¼ °ÍÀÌ´Ù.


This application is similar to the first application in that it listens to a port for incoming connections and responds to data sent to it with a single "ok". The big difference is that this application prints out a welcoming "Welcome!" message when the connection has been established.

ÀÌ ¾îÇø®ÄÉÀ̼ÇÀº ¸®½º´× Æ÷Æ®¸¦ ¿­¾î ¿¬°áÀ» ±â´Ù¸®°í µ¥ÀÌÅ͸¦ ¹ÞÀ¸¸é "ok"·Î ÀÀ´ä ¸Þ½ÃÁö¸¦ ³¯·ÁÁִµ¥¼­ ºñ½ÁÇÏÁö¸¸, Áøº¸½ÃÄѼ­ ¿¬°á½Ã¿¡ "Welcome!"À̶õ ¸Þ½ÃÁö¸¦ ÂïÀ» °ÍÀÌ´Ù.


This seemingly small change of operation makes a big difference in how the application is implemented. The reason for the increase in complexity is that if data should be lost in the network, the application must know what data to retransmit. If the "Welcome!" message was lost, the application must retransmit the welcome and if one of the "ok" messages is lost, the application must send a new "ok".

¾ÕÀÇ ¿¹Á¦¿¡ ºñÇØ ±â´É»óÀÇ Â÷ÀÌ´Â º°·Î ¾øÁö¸¸ ¾îÇø®ÄÉÀ̼ÇÀÇ ±¸ÇöÄÚµå´Â ¸¹ÀÌ ´Ù¸£´Ù. ¿Ö³Ä ÇÏ¸é ³×Æ®¿öÅ©»ó¿¡¼­ ¼Õ½ÇµÈ µ¥ÀÌÅ͸¦ ó¸®ÇØÁÖ±â À§ÇÑ ÀçÀü¼Û ·çƾÀÌ Ãß°¡µÇ±â ¶§¹®ÀÌ´Ù. ¸¸¾à "welcome!"¸Þ½ÃÁö°¡ À¯½ÇµÈ´Ù¸é ¾îÇø®ÄÉÀ̼ÇÀº "welcome!" ¸Þ½ÃÁö¸¦ ÀçÀü¼ÛÇØ¾ßÇϰí, "ok"¸Þ½ÃÁö°¡ À¯½ÇµÇ¸é ¾îÇø®ÄÉÀ̼ÇÀº »õ·Î¿î "ok" ¸Þ½ÃÁö¸¦ ´Ù½Ã¸¸µé¾î Àü¼ÛÇØ¾ß ÇÑ´Ù.


The application knows that as long as the "Welcome!" message has not been acknowledged by the remote host, it might have been dropped in the network. But once the remote host has sent an acknowledgment back, the application can be sure that the welcome has been received and knows that any lost data must be an "ok" message. Thus the application can be in either of two states: either in the WELCOME-SENT state where the "Welcome!" has been sent but not acknowledged, or in the WELCOME-ACKED state where the "Welcome!" has been acknowledged.

¾îÇø®ÄÉÀ̼ÇÀº "welcome!"¸Þ½ÃÁö¿¡ ´ëÇÑ »ó´ëÆí È£½ºÆ®ÀÇ ÀÀ´äÀÌ ¾ø´Ù¸é ³×Æ®¿öÅ©»ó¿¡¼­ ÆÐŶÀÌ À¯½ÇµÇ¾ú´Ù°í ÆÇ´ÜÇÑ´Ù. ±×·¯³ª »ó´ëÆí È£½ºÆ®¿¡¼­ ÀÀ´ä¸Þ½ÃÁö¸¦ ÁÖ¸é, ¾îÇø®ÄÉÀ̼ÇÀº À£ÄÄ ¸Þ½ÃÁöÀÇ ÀÀ´äÀÎÁö ÀçÀü¼ÛÇÑ "ok"¸Þ½ÃÁöÀÇ ÀÀ´äÀÎÁö¸¦ ÆÇ´ÜÇØ¾ß ÇÑ´Ù. ±×·¡¼­ ¾îÇø®ÄÉÀ̼ÇÀº µÎ°¡Áö »óŸ¦ °¡Áú¼ö ÀÖ´Ù. : "welcome!"¸Þ½ÃÁö¸¦ Àü¼ÛÇØ³õ°í ÀÀ´ä¸ÞÁö½Ã¸¦ ¹ÞÁö ¸øÇѰæ(WELCOME-SENT)¿ì, "Welcome!"¸Þ½ÃÁö¸¦ º¸³»°í È®Àθ޽ÃÁö¸¦ ¹ÞÀº °æ¿ì(WELCOME-ACKED).


When a remote host connects to the application, the application sends the "Welcome!" message and sets it's state to WELCOME-SENT. When the welcome message is acknowledged, the application moves to the WELCOME-ACKED state. If the application receives any new data from the remote host, it responds by sending an "ok" back.

¿ø°ÝÁö È£½ºÆ®°¡ ¾îÇø®ÄÉÀ̼ǿ¡ ¿¬°áÀ» ½ÃµµÇÒ¶§ ¾îÇø®ÄÉÀ̼ÇÀº "welcome!"¸Þ½ÃÁö¸¦ º¸³»°í »óŸ¦ WELCOME-SENT·Î ¼¼ÆÃÇÑ´Ù. ±×ÈÄ¿¡ ÀÀ´ä¸Þ½ÃÁö¸¦ ¹Þ¾Ò´Ù¸é ¾îÇø®ÄÉÀ̼ÇÀÇ »óÅ´ WELCOME-ACKED»óÅ·Π¹Ù²î°Ô µÈ´Ù. ¸¸¾à ¾îÇø®ÄÉÀ̼ÇÀÌ ¿ø°ÝÁö È£½ºÆ®·Î ºÎÅÍ ¾Æ¹« ¸Þ½ÃÁöµµ ¹ÞÁö ¸øÇß´Ù¸é ÀçÀü¼Û·çƾ¿¡ ÀÇÇØ "ok"¸Þ½ÃÁö°¡ Àü¼ÛµÈ´Ù. ¸¸¾à ¾îÇø®ÄÉÀ̼ǿ¡°Ô ÀçÀü¼ÛÀ» ¿äûÇÑ´Ù¸é, ¾îÇø®ÄÉÀ̼ÇÀº »óŸ¦ È®ÀÎÇÑÈÄ WELCOME-SENT´Ù¸é "welcome!"¸Þ½ÃÁö¸¦ Àü¼ÛÇϰí "WELCOME-ACKED"»óÅÂÀ̸é "ok"¸Þ½ÃÁö¸¦ Àü¼ÛÇÑ´Ù.


If the application is requested to retransmit the last message, it looks at in which state the application is. If the application is in the WELCOME-SENT state, it sends a "Welcome!" message since it knows that the previous welcome message hasn't been acknowledged. If the application is in the WELCOME-ACKED state, it knows that the last message was an "ok" message and sends such a message.

¾îÇø®ÄÉÀ̼ÇÀÌ ¸¶Áö¸· ¸Þ½ÃÁö¿¡ ´ëÇØ ÀçÀü¼Û¿äûÀ» ¹Þ¾Ò´Ù¸é, Á¦ÀϸÕÀú ¾îÇø®ÄÉÀ̼ÇÀÇ »óŸ¦ »ìÆìº»´Ù. WELCOME-SENT»óŸé À£ÄÄ ¸Þ½ÃÁö¿¡ ´ëÇÑ ACK°¡ ¾ø´Â »óÅÂÀ̹ǷΠ"welcome!"¸Þ½ÃÁö¸¦ Àü¼ÛÇϰí, WELCOME-ACKED»óŸé "ok"¸Þ½ÃÁö¸¦ Àü¼ÛÇÑ´Ù.


The implementation of this application is seen below. This configuration settings for the application is follows after its implementation.

struct example2_state {

   enum {WELCOME_SENT, WELCOME_ACKED} state;

};



void example2_init(void) {

   uip_listen(HTONS(2345));

}



void example2_app(void) {

   struct example2_state *s;



   s = (struct example2_state *)uip_conn->appstate;

   

   if(uip_connected()) {

      s->state = WELCOME_SENT;

      uip_send("Welcome!\n", 9);

      return;

   } 



   if(uip_acked() && s->state == WELCOME_SENT) {

      s->state = WELCOME_ACKED;

   }



   if(uip_newdata()) {

      uip_send("ok\n", 3);

   }



   if(uip_rexmit()) {

      switch(s->state) {

      case WELCOME_SENT:

         uip_send("Welcome!\n", 9);

         break;

      case WELCOME_ACKED:

         uip_send("ok\n", 3);

         break;

      }

   }

}

The configuration for the application:

#define UIP_APPCALL       example2_app

#define UIP_APPSTATE_SIZE sizeof(struct example2_state)

Differentiating between applications

If the system should run multiple applications, one technique to differentiate between them is to use the TCP port number of either the remote end or the local end of the connection. The example below shows how the two examples above can be combined into one application.

¸¸¾à ÇϳªÀÇ ¾îÇø®ÄÉÀÌ¼Ç ¾È¿¡ ¿©·¯°³ÀÇ Æ÷Æ®°¡ ¿­·Á ´ÙÁß±â´ÉÀ» ¼öÇàÇØ¾ßÇÏ´Â °æ¿ì´Â ¹ØÀÇ ¼Ò½ºÃ³·³ ¿¬°áº°·Î Æ÷Æ®µéÀ» ÁöÁ¤ÇØÁÖ¾î¾ß ÇÑ´Ù.


void example3_init(void) {

   example1_init();

   example2_init();

}



void example3_app(void) {

   switch(uip_conn->lport) {

   case HTONS(1234):

      example1_app();

      break;

   case HTONS(2345):

      example2_app();

      break;

   }

}

Utilizing TCP flow control

This example shows a simple application that connects to a host, sends an HTTP request for a file and downloads it to a slow device such a disk drive. This shows how to use the flow control functions of uIP.

ÀÌ ¿¹Á¦´Â ¼öÇà¼Óµµ°¡ ´À¸° µð¹ÙÀ̽ºµé°úÀÇ ¿¬µ¿¿¡¼­ uIP°¡ ¾î¶»°Ô ÄÁÆ®·Ñ ÇÏ´ÂÁö¸¦ º¸¿©ÁØ´Ù. ¿¹¸¦ µé¸é, À¥ÆäÀÌÁö¸¦ ±Ü¾î¼­ ÆÄÀÏÀúÀåÇÒ¶§ ÀúÀåÇÏ´Â µ¿¾È¿¡ »õ·Î¿î µ¥ÀÌÅ͸¦ À̾ ÆÄÀÏ¿¡ ÀúÀåÇØ¾ßÇÏ´Â °æ¿ì.


void example4_init(void) {

   u16_t ipaddr[2];

   uip_ipaddr(ipaddr, 192,168,0,1);

   uip_connect(ipaddr, HTONS(80));

}



void example4_app(void) {

   if(uip_connected() || uip_rexmit()) {

      uip_send("GET /file HTTP/1.0\r\nServer:192.186.0.1\r\n\r\n",

               48);

      return;

   }



   if(uip_newdata()) {

      device_enqueue(uip_appdata, uip_datalen());

      if(device_queue_full()) {

         uip_stop();

      }

   }



   if(uip_poll() && uip_stopped()) {

      if(!device_queue_full()) {

         uip_restart();

      }

   }

}

When the connection has been established, an HTTP request is sent to the server. Since this is the only data that is sent, the application knows that if it needs to retransmit any data, it is that request that should be retransmitted. It is therefore possible to combine these two events as is done in the example.

¿¬°á ¿Ï·áµÇ¸é, ¼­¹ö¿¡°Ô HTTP¿äûÀ» º¸³½´Ù.


When the application receives new data from the remote host, it sends this data to the device by using the function device_enqueue(). It is important to note that this example assumes that this function copies the data into its own buffers. The data in the uip_appdata buffer will be overwritten by the next incoming packet.

¿ø°ÝÁö È£½ºÆ®·ÎºÎÅÍ µ¥ÀÌÅ͸¦ ¹Þ¾ÒÀ»¶§, ÀÌ µ¥ÀÌÅÍ´Â device_enqueue()ÇÔ¼ö¸¦ ÅëÇØ µð¹ÙÀ̽º¿¡°Ô Àü´ÞµÈ´Ù. Áß¿äÇѰÍÀº µð¹ÙÀ̽º ³»ºÎ ¹öÆÛ¿¡ Ä«ÇÇÇØ°£´Ù´Â »ç½Ç, µû¶ó¼­ ÇöÀçÀÇ ÆÐŶÀº ´ÙÀ½¿¡ µé¾î¿À´Â ÆÐŶ¿¡ ÀÇÇØ µ¤¾î¾²¿©Áø´Ù.


If the device's queue is full, the application stops the data from the remote host by calling the uIP function uip_stop(). The application can then be sure that it will not receive any new data until uip_restart() is called. The application polling event is used to check if the device's queue is no longer full and if so, the data flow is restarted with uip_restart().

¸¸¾à µð¹ÙÀ̽ºÀÇ Å¥°¡ °¡µæ Â÷ÀÖ´Ù¸é, ¾îÇø®ÄÉÀ̼ÇÀº uip_stop()ÇÔ¼ö¸¦ ÅëÇØ ¿ø°ÝÁö·ÎºÎÅÍ µ¥ÀÌŸ¸¦ ¹Þ´Â ÀÏÀ» ÁßÁöÇÑ´Ù. ±×¸®°í uip_restart()ÇÔ¼ö°¡ È£ÃâµÉ¶§±îÁö ¾îÇø®ÄÉÀ̼ÇÀº ¾î¶² µ¥ÀÌÅ͵µ ¹ÞÁö ¾Ê´Â´Ù. ¾îÇø®ÄÉÀ̼ÇÀº µð¹ÙÀ̽º Å¥¸¦ °è¼Ó °Ë»çÇϰí ÀÖ´Ù°¡, ¿©À¯°¡ »ý±â¸é ¹Ù·Î uip_restart()ÇÔ¼ö¸¦ È£ÃâÇÑ´Ù.


A simple web server

This example shows a very simple file server application that listens to two ports and uses the port number to determine which file to send. If the files are properly formatted, this simple application can be used as a web server with static pages. The implementation follows.

ÀÌ ¿¹Á¦´Â ¾ÆÁÖ °£´ÜÇÑ ÆÄÀÏ ¼­¹ö ¾îÇø®ÄÉÀ̼ÇÀÌ´Ù. µÎ°³ÀÇ Æ÷Æ®¸¦ ¿­°í¼­ µÎ°³ÀÇ ÆÄÀÏÀ» º¸³»´Âµ¥ ¾î´À ÆÄÀÏÀ» º¸³¾Áö °áÁ¤ÇÏ´Â ±â´ÉÀÌ´Ù. ¸¸¾à ÆÄÀÏ Çü½ÄÀÌ ¸Â´Ù¸é ÀÌ ¿¹Á¦´Â À¥¼­¹ö·Î µ¿ÀÛÇÒ ¼öÀÖ´Ù.


struct example5_state {

   char *dataptr;

   unsigned int dataleft;

};



void example5_init(void) {

   uip_listen(HTONS(80));

   uip_listen(HTONS(81));

}



void example5_app(void) {

   struct example5_state *s;

   s = (struct example5_state)uip_conn->appstate;

   

   if(uip_connected()) {

      switch(uip_conn->lport) {

      case HTONS(80):

         s->dataptr = data_port_80;

         s->dataleft = datalen_port_80;

         break;

      case HTONS(81):

         s->dataptr = data_port_81;

         s->dataleft = datalen_port_81;

         break;

      }

      uip_send(s->dataptr, s->dataleft);

      return;      

   }



   if(uip_acked()) {

      if(s->dataleft < uip_mss()) {

         uip_close();

         return;

      }

      s->dataptr += uip_conn->len;

      s->dataleft -= uip_conn->len;

      uip_send(s->dataptr, s->dataleft);      

   }

}

The application state consists of a pointer to the data that should be sent and the size of the data that is left to send. When a remote host connects to the application, the local port number is used to determine which file to send. The first chunk of data is sent using uip_send(). uIP makes sure that no more than MSS bytes of data is actually sent, even though s->dataleft may be larger than the MSS.

¾îÇø®ÄÉÀÌ¼Ç »óÅ´ µÎ°¡Áö Æ÷ÀÎÅÍ·Î ±¸¼ºµÈ´Ù. º¸³¾ µ¥ÀÌÅÍÀÇ Æ÷ÀÎÅÍ¿Í, ¾ÕÀ¸·Î º¸³¾ µ¥ÀÌÅÍÀÇ Å©±â. ¿ø°ÝÁö È£½ºÆ®°¡ ¾îÇø®ÄÉÀ̼ǿ¡ ¿¬°áµÉ¶§ ·ÎÄà Æ÷Æ®¹øÈ£´Â ¾î´À ÆÄÀÏÀ» º¸³¾Áö °áÁ¤ÇÑ´Ù. uip_send()ÇÔ¼ö·Î ù¹øÂ° µ¥ÀÌÅÍ chunk¸¦ Àü¼ÛÇÑ´Ù. uIP´Â Àü¼ÛÇÒ µ¥ÀÌÅÍÅ©±â°¡ MSS¸¦ ³ÑÁö ¾Ê°Ô º¸ÀåÇØ Áà¾ß ÇÑ´Ù.


The application is driven by incoming acknowledgments. When data has been acknowledged, new data can be sent. If there is no more data to send, the connection is closed using uip_close(). ¾îÇø®ÄÉÀ̼ÇÀº È®Àθ޽ÃÁö¸¦ ¹Þ°í ±×°Í¿¡ µû¶ó µ¿ÀÛÇÑ´Ù. ÀÌÀü¿¡ º¸³½ µ¥ÀÌÅÍ¿¡ ´ëÇÑ ACK¸Þ½ÃÁö¸¦ ¹Þ¾Ò´Ù¸é »õ·Î¿î µ¥ÀÌÅ͸¦ Àü¼ÛÇÑ´Ù. º¸³¾ µ¥ÀÌÅͰ¡ ´õÀÌ»ó ¾ø´Ù¸é ¿¬°áÀº uip_close()¿¡ ÀÇÇØ Á¾·áµÈ´Ù.


Structured application program design

When writing larger programs using uIP it is useful to be able to utilize the uIP API in a structured way. The following example provides a structured design that has showed itself to be useful for writing larger protocol implementations than the previous examples showed here. The program is divided into an uIP event handler function that calls seven application handler functions that process new data, act on acknowledged data, send new data, deal with connection establishment or closure events and handle errors. The functions are called newdata(), acked(), senddata(), connected(), closed(), aborted(), and timedout(), and needs to be written specifically for the protocol that is being implemented.

uIP¸¦ »ç¿ëÇØ¼­ Å« ÇÁ·Î±×·¥À» ÀÛ¼³ÇÒ¶§´Â ±¸Á¶È­µÈ ¹æ¹ýÀ¸·Î uIP API¸¦ »ç¿ëÇÒ ¼öÀÖ´Ù. ¾ÕÀ¸·Î º¸¿©ÁÙ ¿¹Á¦´Â À§¿¡¼­ º¸¿©Áá´ø ¿¹Á¦º¸´Ù ´õ Å« ÇÁ·ÎÅäÄÝÀ» ±¸ÇöÇÒ ¶§ À¯¿ëÇÏ°Ô »ç¿ëµÇ´Â ±¸Á¶È­µÈ ¹æ¹ýÀ» ¼Ò°³ÇÑ´Ù. ÀÌ ÇÁ·Î±×·¥Àº uIP À̺¥Æ® Çڵ鷯¿¡ ÀÇÇØ µ¿ÀÛÇÑ´Ù. ÃÑ 7°¡ÁöÀÇ Çڵ鷯°¡ È£Ã⠵ȴÙ. newdata(), acked(), senddata(), connected(), closed(), aborted(), timedout().


The uIP event handler function is shown below.

void example6_app(void) {

  if(uip_aborted()) {

    aborted();

  }

  if(uip_timedout()) {

    timedout();

  }

  if(uip_closed()) {

    closed();

  }

  if(uip_connected()) {

    connected();

  }

  if(uip_acked()) {

    acked();

  }

  if(uip_newdata()) {

    newdata();

  }

  if(uip_rexmit() ||

     uip_newdata() ||

     uip_acked() ||

     uip_connected() ||

     uip_poll()) {

    senddata();

  }

}

The function starts with dealing with any error conditions that might have happened by checking if uip_aborted() or uip_timedout() are true. If so, the appropriate error function is called. Also, if the connection has been closed, the closed() function is called to the it deal with the event.

uip_aborted()³ª uip_timedout()ÇÔ¼ö°¡ trueÀÌ¸é ¿¡·¯ ÇÔ¼ö¸¦ È£ÃâÇϰí, ¿¬°áÀ» ÇØÁ¦ÇÑ´Ù. closed()ÇÔ¼ö´Â uip_closed()ÇÔ¼ö°¡ ÂüÀÌ¸é ½ÇÇàµÈ´Ù.


Next, the function checks if the connection has just been established by checking if uip_connected() is true. The connected() function is called and is supposed to do whatever needs to be done when the connection is established, such as intializing the application state for the connection. Since it may be the case that data should be sent out, the senddata() function is called to deal with the outgoing data.

´ÙÀ½Àº, uip_connected()À» üũÇÔÀ¸·Î¼­ ¿¬°á ¿©ºÎ¸¦ È®ÀÎ ÇÑ´Ù. ¿¬°áÀÌ µÇ¾ú´Ù¸é connected()ÇÔ¼ö¸¦ È£ÃâÇØ¼­ ¿¬°á ¼³Á¤ ¹× ÃʱâÈ­ °úÁ¤À» °ÅÄ£´Ù. ±×¸®°í µ¥ÀÌÅ͸¦ Àü¼ÛÇØ¾ßµÇ´Â °æ¿ì¿¡´Â senddata()ÇÔ¼ö¸¦ »ç¿ëÇØ¼­ ¹ÛÀ¸·Î Àü¼ÛÇÑ´Ù.


The following very simple application serves as an example of how the application handler functions might look. This application simply waits for any data to arrive on the connection, and responds to the data by sending out the message "Hello world!". To illustrate how to develop an application state machine, this message is sent in two parts, first the "Hello" part and then the "world!" part.

¹ØÀÇ ¿¹Á¦´Â ¾îÇø®ÄÉÀ̼ÇÀÇ Çîµé·¯ÇÔ¼öµéÀÌ ¾î¶»°Ô µ¿ÀÛÇÏ´ÂÁö º¸¿©ÁÖ´Â ¿¹Á¦ÀÌ´Ù. ÀÌ ¾îÇø®ÄÉÀ̼ÇÀº ¿¬°áÀ» ±â´Ù¸®´Ù°¡ ¿¬°á¿äû ¸Þ½ÃÁö°¡ ¿À¸é "Hello world!"¸Þ½ÃÁö¸¦ º¸³¾°ÍÀÌ´Ù. ¾îÇø®ÄÉÀÌ¼Ç »óÅ¿¡ µû¶ó ¾î¶»°Ô µ¿À۵ǴÂÁö º¸¿©ÁÖ±â À§ÇØ ¸Þ½ÃÁö¸¦ µÎ ºÎºÐÀ¸·Î ³ª´©¾î¼­ Àü¼ÛÇß´Ù. "hello"ºÎºÐ°ú "world!".


#define STATE_WAITING 0

#define STATE_HELLO   1

#define STATE_WORLD   2



struct example6_state {

  u8_t state;

  char *textptr;

  int  textlen;

};



static void aborted(void) {}

static void timedout(void) {}

static void closed(void) {}



static void connected(void) {

  struct example6_state *s = (struct example6_state *)uip_conn->appstate;



  s->state   = STATE_WAITING;

  s->textlen = 0;

}



static void newdata(void) {

  struct example6_state *s = (struct example6_state *)uip_conn->appstate;



  if(s->state == STATE_WAITING) {

    s->state   = STATE_HELLO;

    s->textptr = "Hello ";

    s->textlen = 6;

  }

}



static void acked(void) {

  struct example6_state *s = (struct example6_state *)uip_conn->appstate;

  

  s->textlen -= uip_conn->len;

  s->textptr += uip_conn->len;

  if(s->textlen == 0) {

    switch(s->state) {

    case STATE_HELLO:

      s->state   = STATE_WORLD;

      s->textptr = "world!\n";

      s->textlen = 7;

      break;

    case STATE_WORLD:

      uip_close();

      break;

    }

  }

}



static void senddata(void) {

  struct example6_state *s = (struct example6_state *)uip_conn->appstate;



  if(s->textlen > 0) {

    uip_send(s->textptr, s->textlen);

  }

}

The application state consists of a "state" variable, a "textptr" pointer to a text message and the "textlen" length of the text message. The "state" variable can be either "STATE_WAITING", meaning that the application is waiting for data to arrive from the network, "STATE_HELLO", in which the application is sending the "Hello" part of the message, or "STATE_WORLD", in which the application is sending the "world!" message.

¾îÇø®ÄÉÀ̼ÇÀÇ »óÅ´ "stateº¯¼ö¿Í "textptr"ÅØ½ºÆ®Æ÷ÀÎÅÍ¿Í "textlen"ÅØ½ºÆ®¸Þ½ÃÁöÀÇ ±æÀÌ·Î ±¸¼ºµÈ´Ù. "state"º¯¼ö´Â ¿¬°áÀÌ µÈÈÄ µ¥ÀÌÅ͸¦ ±â´Ù¸®´Â »óÅÂÀÎ "STATE_WAITING", ¾îÇø®ÄÉÀ̼ÇÀÌ "helo"ºÎºÐÀ» Àü¼ÛÇÑ "STATE_HELLO"»óÅÂ, "world!"¸Þ½ÃÁö¸¦ Àü¼Û ÇϰíÀÖ´Â "STATE_WORDL"»óŰ¡ ÀÖ´Ù.


The application does not handle errors or connection closing events, and therefore the aborted(), timedout() and closed() functions are implemented as empty functions.

¾îÇø®ÄÉÀ̼ÇÀº aborted(), timedout(), closed() ÇÔ¼ö¿¡ ÀÇÇÑ ¿¡·¯Çڵ鸵À̳ª ¿¬°áÇØÁ¦¸¦ ÇÏÁö¾Ê´Â´Ù. ±× ÇÔ¼öµéÀº ±¸ÇöºÎ°¡ ºñ¾îÀÖ´Ù.


The connected() function will be called when a connection has been established, and in this case sets the "state" variable to be "STATE_WAITING" and the "textlen" variable to be zero, indicating that there is no message to be sent out.

connected()ÇÔ¼ö´Â ¿¬°á ¼³Á¤µÉ¶§ È£ÃâµÇ°í »óꝼö¸¦ "STATE_WATING","textlen"À» 0À¸·Î ÃʱâÈ­ ÇÑ´Ù.


When new data arrives from the network, the newdata() function will be called by the event handler function. The newdata() function will check if the connection is in the "STATE_WAITING" state, and if so switches to the "STATE_HELLO" state and registers a 6 byte long "Hello " message with the connection. This message will later be sent out by the senddata() function.

³×Æ®¿öÅ©·Î ºÎÅÍ »õ·Î¿î µ¥ÀÌÅͰ¡ µµÂøÇϸé À̺¥Æ® Çîµé·¯´Â newdata()ÇÔ¼ö¸¦ È£ÃâÇÑ´Ù. ÀÌ ÇÔ¼ö´Â "STATE_WAITING"»óÅÂ¸é ¾îÇø®ÄÉÀ̼ÇÀº Áö±Ý µµÂøÇÑ »ó´ë·ÎºÎÅÍ µ¥ÀÌÅ͸¦ ±â´Ù¸®°í ÀÖ´ÂÁßÀ̶ó´Â ÀǹÌÀÎ "STATE_HELLO"·Î »óŸ¦ ¹Ù²Ù°í, 6¹ÙÀÌÆ®ÀÇ "hello"ÆÄÆ®¸¦ ÀúÀåÇÑ´Ù. ÀÌ ¸Þ½ÃÁö´Â senddata()¿¡ ÀÇÇØ¼­ ³ªÁß¿¡ ³×Æ®¿öÅ©·Î º¸³»Áú °ÍÀÌ´Ù.


The acked() function is called whenever data that previously was sent has been acknowleged by the receiving host. This acked() function first reduces the amount of data that is left to send, by subtracting the length of the previously sent data (obtained from "uip_conn->len") from the "textlen" variable, and also adjusts the "textptr" pointer accordingly. It then checks if the "textlen" variable now is zero, which indicates that all data now has been successfully received, and if so changes application state. If the application was in the "STATE_HELLO" state, it switches state to "STATE_WORLD" and sets up a 7 byte "world!\n" message to be sent. If the application was in the "STATE_WORLD" state, it closes the connection.

acked()ÇÔ¼ö´Â µ¥ÀÌÅ͸¦ º¸³¾¶§ ¸¶´Ù »ó´ëÆí È£½ºÆ®·Î ºÎÅÍ È£ÃâµÈ´Ù. ÀÌ ÇÔ¼ö´Â º¸³¾ µ¥ÀÌÅÍÅ©±â¿¡¼­ º¸³½ µ¥ÀÌÅÍ Å©±â¸¦ »©ÁÖ°í ´ÙÀ½ Àü¼ÛÇÒ ºÎºÐÀ» "textptr"¿¡ ÁöÁ¤ÇÑ´Ù. ¸¸¾à "textlen"º¯¼ö°¡ 0À̸é, Áö±Ý ¸ðµç µ¥ÀÌÅÍ´Â Àß ¹Þ¾Ò´Ù´Â °ÍÀ» ¸»ÇϹǷΠ¾îÇø®ÄÉÀ̼ÇÀÇ »óŸ¦ "STATE_WORLD"·Î ¹Ù²Ù°í 7¹ÙÀÌÆ®¸Þ½ÃÁö "world\n"¸¦ º¸³½´Ù. ¹Ý¸é ¾îÇø®ÄÉÀ̼ÇÀÇ »óŰ¡ "STATE_WORLD"¿´´Ù¸é, ¿¬°áÀ» ÇØÁ¦ÇÑ´Ù.


Finally, the senddata() function takes care of actually sending the data that is to be sent. It is called by the event handler function when new data has been received, when data has been acknowledged, when a new connection has been established, when the connection is polled because of inactivity, or when a retransmission should be made. The purpose of the senddata() function is to optionally format the data that is to be sent, and to call the uip_send() function to actually send out the data. In this particular example, the function simply calls uip_send() with the appropriate arguments if data is to be sent, after checking if data should be sent out or not as indicated by the "textlen" variable.

¸¶Áö¸·À¸·Î senddata() ÇÔ¼ö´Â µ¥ÀÌÅ͸¦ ³×Æ®¿öÅ© »ó¿¡ ³»º¸³»´Â ÇÔ¼öÀÌ´Ù. ÀÌ ÇÔ¼ö´Â »õ·Î¿î µ¥ÀÌÅ͸¦ ¹Þ¾ÒÀ»¶§, ACK¸Þ½ÃÁö¸¦ »óÅÂÀ϶§,»õ·Î¿î ¿¬°áÀÌ ¿äû ?À»¶§, ÀçÀü¼Û ÇÒ¶§ÀÇ °æ¿ì¿¡ À̺¥Æ® Çڵ鷯¿¡ ÀÇÇØ¼­ È£ÃâµÈ´Ù. senddata()ÇÔ¼öÀÇ ¸ñÀûÀº º¸³¾ µ¥ÀÌÅÍÀÇ ºÎ°¡ÀûÀÎ ¿É¼ÇÀ» ¼¼ÆÃÇϴµ¥ ÀÖ´Ù. º¸³¾ µ¥ÀÌÅÍÀÇ Á¤º¸¸¦ ÀúÀåÇÑ ÈÄ uip_send()¸¦ È£ÃâÇØ¼­ µ¥ÀÌÅ͸¦ ¹ÛÀ¸·Î ³»º¸³½´Ù. ½ÇÁ¦¿¹¿¡¼­ ÀÌ ÇÔ¼ö´Â uip_send() ÇÔ¼ö¸¦ È£ÃâÇϱâÀü¿¡ "textlen" º¯¼ö·Î µ¥ÀÌÅ͸¦ º¸³¾Áö¸»Áö¸¦ °£´ÜÇÏ°Ô Å×½ºÆ®ÇÑ´Ù.

It is important to note that the senddata() function never should affect the application state; this should only be done in the acked() and newdata() functions.

senddata() ÇÔ¼ö´Â ¾îÇø®ÄÉÀÌ¼Ç »óŸ¦ º¯°æÇϰųª ¿µÇâÀ» ÁÖÁö ¾Ê´Â´Ù. ÀÌ¹Ì acked()³ª newdata() ÇÔ¼ö¿¡¼­ º¯°æÇß´Ù.



Generated on Tue Oct 7 15:51:00 2003 for uIP 0.9 by doxygen 1.3.3