Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

uip.h 31KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /**
  2. * \addtogroup uip
  3. * @{
  4. */
  5. /**
  6. * \file
  7. * Header file for the uIP TCP/IP stack.
  8. * \author Adam Dunkels <adam@dunkels.com>
  9. *
  10. * The uIP TCP/IP stack header file contains definitions for a number
  11. * of C macros that are used by uIP programs as well as internal uIP
  12. * structures, TCP/IP header structures and function declarations.
  13. *
  14. */
  15. /*
  16. * Copyright (c) 2001-2003, Adam Dunkels.
  17. * All rights reserved.
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions
  21. * are met:
  22. * 1. Redistributions of source code must retain the above copyright
  23. * notice, this list of conditions and the following disclaimer.
  24. * 2. Redistributions in binary form must reproduce the above copyright
  25. * notice, this list of conditions and the following disclaimer in the
  26. * documentation and/or other materials provided with the distribution.
  27. * 3. The name of the author may not be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  32. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  33. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  35. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  36. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  37. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  38. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  39. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  40. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  41. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. * This file is part of the uIP TCP/IP stack.
  44. *
  45. * $Id$
  46. *
  47. */
  48. #ifndef __UIP_H__
  49. #define __UIP_H__
  50. #include "uipopt.h"
  51. /*-----------------------------------------------------------------------------------*/
  52. /* First, the functions that should be called from the
  53. * system. Initialization, the periodic timer and incoming packets are
  54. * handled by the following three functions.
  55. */
  56. /**
  57. * \defgroup uipconffunc uIP configuration functions
  58. * @{
  59. *
  60. * The uIP configuration functions are used for setting run-time
  61. * parameters in uIP such as IP addresses.
  62. */
  63. /**
  64. * Set the IP address of this host.
  65. *
  66. * The IP address is represented as a 4-byte array where the first
  67. * octet of the IP address is put in the first member of the 4-byte
  68. * array.
  69. *
  70. * \param addr A pointer to a 4-byte representation of the IP address.
  71. *
  72. * \hideinitializer
  73. */
  74. #define uip_sethostaddr(addr) do { uip_hostaddr[0] = addr[0]; \
  75. uip_hostaddr[1] = addr[1]; } while(0)
  76. /**
  77. * Get the IP address of this host.
  78. *
  79. * The IP address is represented as a 4-byte array where the first
  80. * octet of the IP address is put in the first member of the 4-byte
  81. * array.
  82. *
  83. * \param addr A pointer to a 4-byte array that will be filled in with
  84. * the currently configured IP address.
  85. *
  86. * \hideinitializer
  87. */
  88. #define uip_gethostaddr(addr) do { addr[0] = uip_hostaddr[0]; \
  89. addr[1] = uip_hostaddr[1]; } while(0)
  90. /** @} */
  91. /**
  92. * \defgroup uipinit uIP initialization functions
  93. * @{
  94. *
  95. * The uIP initialization functions are used for booting uIP.
  96. */
  97. /**
  98. * uIP initialization function.
  99. *
  100. * This function should be called at boot up to initilize the uIP
  101. * TCP/IP stack.
  102. */
  103. void uip_init(void);
  104. /** @} */
  105. /**
  106. * \defgroup uipdevfunc uIP device driver functions
  107. * @{
  108. *
  109. * These functions are used by a network device driver for interacting
  110. * with uIP.
  111. */
  112. /**
  113. * Process an incoming packet.
  114. *
  115. * This function should be called when the device driver has received
  116. * a packet from the network. The packet from the device driver must
  117. * be present in the uip_buf buffer, and the length of the packet
  118. * should be placed in the uip_len variable.
  119. *
  120. * When the function returns, there may be an outbound packet placed
  121. * in the uip_buf packet buffer. If so, the uip_len variable is set to
  122. * the length of the packet. If no packet is to be sent out, the
  123. * uip_len variable is set to 0.
  124. *
  125. * The usual way of calling the function is presented by the source
  126. * code below.
  127. \code
  128. uip_len = devicedriver_poll();
  129. if(uip_len > 0) {
  130. uip_input();
  131. if(uip_len > 0) {
  132. devicedriver_send();
  133. }
  134. }
  135. \endcode
  136. *
  137. * \note If you are writing a uIP device driver that needs ARP
  138. * (Address Resolution Protocol), e.g., when running uIP over
  139. * Ethernet, you will need to call the uIP ARP code before calling
  140. * this function:
  141. \code
  142. #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
  143. uip_len = ethernet_devicedrver_poll();
  144. if(uip_len > 0) {
  145. if(BUF->type == HTONS(UIP_ETHTYPE_IP)) {
  146. uip_arp_ipin();
  147. uip_input();
  148. if(uip_len > 0) {
  149. uip_arp_out();
  150. ethernet_devicedriver_send();
  151. }
  152. } else if(BUF->type == HTONS(UIP_ETHTYPE_ARP)) {
  153. uip_arp_arpin();
  154. if(uip_len > 0) {
  155. ethernet_devicedriver_send();
  156. }
  157. }
  158. \endcode
  159. *
  160. * \hideinitializer
  161. */
  162. #define uip_input() uip_process(UIP_DATA)
  163. /**
  164. * Periodic processing for a connection identified by its number.
  165. *
  166. * This function does the necessary periodic processing (timers,
  167. * polling) for a uIP TCP conneciton, and should be called when the
  168. * periodic uIP timer goes off. It should be called for every
  169. * connection, regardless of whether they are open of closed.
  170. *
  171. * When the function returns, it may have an outbound packet waiting
  172. * for service in the uIP packet buffer, and if so the uip_len
  173. * variable is set to a value larger than zero. The device driver
  174. * should be called to send out the packet.
  175. *
  176. * The ususal way of calling the function is through a for() loop like
  177. * this:
  178. \code
  179. for(i = 0; i < UIP_CONNS; ++i) {
  180. uip_periodic(i);
  181. if(uip_len > 0) {
  182. devicedriver_send();
  183. }
  184. }
  185. \endcode
  186. *
  187. * \note If you are writing a uIP device driver that needs ARP
  188. * (Address Resolution Protocol), e.g., when running uIP over
  189. * Ethernet, you will need to call the uip_arp_out() function before
  190. * calling the device driver:
  191. \code
  192. for(i = 0; i < UIP_CONNS; ++i) {
  193. uip_periodic(i);
  194. if(uip_len > 0) {
  195. uip_arp_out();
  196. ethernet_devicedriver_send();
  197. }
  198. }
  199. \endcode
  200. *
  201. * \param conn The number of the connection which is to be periodically polled.
  202. *
  203. * \hideinitializer
  204. */
  205. #define uip_periodic(conn) do { uip_conn = &uip_conns[conn]; \
  206. uip_process(UIP_TIMER); } while (0)
  207. /**
  208. * Periodic processing for a connection identified by a pointer to its structure.
  209. *
  210. * Same as uip_periodic() but takes a pointer to the actual uip_conn
  211. * struct instead of an integer as its argument. This function can be
  212. * used to force periodic processing of a specific connection.
  213. *
  214. * \param conn A pointer to the uip_conn struct for the connection to
  215. * be processed.
  216. *
  217. * \hideinitializer
  218. */
  219. #define uip_periodic_conn(conn) do { uip_conn = conn; \
  220. uip_process(UIP_TIMER); } while (0)
  221. #if UIP_UDP
  222. /**
  223. * Periodic processing for a UDP connection identified by its number.
  224. *
  225. * This function is essentially the same as uip_prerioic(), but for
  226. * UDP connections. It is called in a similar fashion as the
  227. * uip_periodic() function:
  228. \code
  229. for(i = 0; i < UIP_UDP_CONNS; i++) {
  230. uip_udp_periodic(i);
  231. if(uip_len > 0) {
  232. devicedriver_send();
  233. }
  234. }
  235. \endcode
  236. *
  237. * \note As for the uip_periodic() function, special care has to be
  238. * taken when using uIP together with ARP and Ethernet:
  239. \code
  240. for(i = 0; i < UIP_UDP_CONNS; i++) {
  241. uip_udp_periodic(i);
  242. if(uip_len > 0) {
  243. uip_arp_out();
  244. ethernet_devicedriver_send();
  245. }
  246. }
  247. \endcode
  248. *
  249. * \param conn The number of the UDP connection to be processed.
  250. *
  251. * \hideinitializer
  252. */
  253. #define uip_udp_periodic(conn) do { uip_udp_conn = &uip_udp_conns[conn]; \
  254. uip_process(UIP_UDP_TIMER); } while (0)
  255. /**
  256. * Periodic processing for a UDP connection identified by a pointer to
  257. * its structure.
  258. *
  259. * Same as uip_udp_periodic() but takes a pointer to the actual
  260. * uip_conn struct instead of an integer as its argument. This
  261. * function can be used to force periodic processing of a specific
  262. * connection.
  263. *
  264. * \param conn A pointer to the uip_udp_conn struct for the connection
  265. * to be processed.
  266. *
  267. * \hideinitializer
  268. */
  269. #define uip_udp_periodic_conn(conn) do { uip_udp_conn = conn; \
  270. uip_process(UIP_UDP_TIMER); } while (0)
  271. #endif /* UIP_UDP */
  272. /**
  273. * The uIP packet buffer.
  274. *
  275. * The uip_buf array is used to hold incoming and outgoing
  276. * packets. The device driver should place incoming data into this
  277. * buffer. When sending data, the device driver should read the link
  278. * level headers and the TCP/IP headers from this buffer. The size of
  279. * the link level headers is configured by the UIP_LLH_LEN define.
  280. *
  281. * \note The application data need not be placed in this buffer, so
  282. * the device driver must read it from the place pointed to by the
  283. * uip_appdata pointer as illustrated by the following example:
  284. \code
  285. void
  286. devicedriver_send(void)
  287. {
  288. hwsend(&uip_buf[0], UIP_LLH_LEN);
  289. hwsend(&uip_buf[UIP_LLH_LEN], 40);
  290. hwsend(uip_appdata, uip_len - 40 - UIP_LLH_LEN);
  291. }
  292. \endcode
  293. */
  294. extern u8_t uip_buf[UIP_BUFSIZE+2];
  295. /** @} */
  296. /*-----------------------------------------------------------------------------------*/
  297. /* Functions that are used by the uIP application program. Opening and
  298. * closing connections, sending and receiving data, etc. is all
  299. * handled by the functions below.
  300. */
  301. /**
  302. * \defgroup uipappfunc uIP application functions
  303. * @{
  304. *
  305. * Functions used by an application running of top of uIP.
  306. */
  307. /**
  308. * Start listening to the specified port.
  309. *
  310. * \note Since this function expects the port number in network byte
  311. * order, a conversion using HTONS() or htons() is necessary.
  312. *
  313. \code
  314. uip_listen(HTONS(80));
  315. \endcode
  316. *
  317. * \param port A 16-bit port number in network byte order.
  318. */
  319. void uip_listen(u16_t port);
  320. /**
  321. * Stop listening to the specified port.
  322. *
  323. * \note Since this function expects the port number in network byte
  324. * order, a conversion using HTONS() or htons() is necessary.
  325. *
  326. \code
  327. uip_unlisten(HTONS(80));
  328. \endcode
  329. *
  330. * \param port A 16-bit port number in network byte order.
  331. */
  332. void uip_unlisten(u16_t port);
  333. /**
  334. * Connect to a remote host using TCP.
  335. *
  336. * This function is used to start a new connection to the specified
  337. * port on the specied host. It allocates a new connection identifier,
  338. * sets the connection to the SYN_SENT state and sets the
  339. * retransmission timer to 0. This will cause a TCP SYN segment to be
  340. * sent out the next time this connection is periodically processed,
  341. * which usually is done within 0.5 seconds after the call to
  342. * uip_connect().
  343. *
  344. * \note This function is avaliable only if support for active open
  345. * has been configured by defining UIP_ACTIVE_OPEN to 1 in uipopt.h.
  346. *
  347. * \note Since this function requires the port number to be in network
  348. * byte order, a convertion using HTONS() or htons() is necessary.
  349. *
  350. \code
  351. u16_t ipaddr[2];
  352. uip_ipaddr(ipaddr, 192,168,1,2);
  353. uip_connect(ipaddr, HTONS(80));
  354. \endcode
  355. *
  356. * \param ripaddr A pointer to a 4-byte array representing the IP
  357. * address of the remote hot.
  358. *
  359. * \param port A 16-bit port number in network byte order.
  360. *
  361. * \return A pointer to the uIP connection identifier for the new connection,
  362. * or NULL if no connection could be allocated.
  363. *
  364. */
  365. struct uip_conn *uip_connect(u16_t *ripaddr, u16_t port);
  366. /**
  367. * \internal
  368. *
  369. * Check if a connection has outstanding (i.e., unacknowledged) data.
  370. *
  371. * \param conn A pointer to the uip_conn structure for the connection.
  372. *
  373. * \hideinitializer
  374. */
  375. #define uip_outstanding(conn) ((conn)->len)
  376. /**
  377. * Send data on the current connection.
  378. *
  379. * This function is used to send out a single segment of TCP
  380. * data. Only applications that have been invoked by uIP for event
  381. * processing can send data.
  382. *
  383. * The amount of data that actually is sent out after a call to this
  384. * funcion is determined by the maximum amount of data TCP allows. uIP
  385. * will automatically crop the data so that only the appropriate
  386. * amount of data is sent. The function uip_mss() can be used to query
  387. * uIP for the amount of data that actually will be sent.
  388. *
  389. * \note This function does not guarantee that the sent data will
  390. * arrive at the destination. If the data is lost in the network, the
  391. * application will be invoked with the uip_rexmit() event being
  392. * set. The application will then have to resend the data using this
  393. * function.
  394. *
  395. * \param data A pointer to the data which is to be sent.
  396. *
  397. * \param len The maximum amount of data bytes to be sent.
  398. *
  399. * \hideinitializer
  400. */
  401. #define uip_send(data, len) do { uip_sappdata = (data); uip_slen = (len);} while(0)
  402. /**
  403. * The length of any incoming data that is currently avaliable (if avaliable)
  404. * in the uip_appdata buffer.
  405. *
  406. * The test function uip_data() must first be used to check if there
  407. * is any data available at all.
  408. *
  409. * \hideinitializer
  410. */
  411. #define uip_datalen() uip_len
  412. /**
  413. * The length of any out-of-band data (urgent data) that has arrived
  414. * on the connection.
  415. *
  416. * \note The configuration parameter UIP_URGDATA must be set for this
  417. * function to be enabled.
  418. *
  419. * \hideinitializer
  420. */
  421. #define uip_urgdatalen() uip_urglen
  422. /**
  423. * Close the current connection.
  424. *
  425. * This function will close the current connection in a nice way.
  426. *
  427. * \hideinitializer
  428. */
  429. #define uip_close() (uip_flags = UIP_CLOSE)
  430. /**
  431. * Abort the current connection.
  432. *
  433. * This function will abort (reset) the current connection, and is
  434. * usually used when an error has occured that prevents using the
  435. * uip_close() function.
  436. *
  437. * \hideinitializer
  438. */
  439. #define uip_abort() (uip_flags = UIP_ABORT)
  440. /**
  441. * Tell the sending host to stop sending data.
  442. *
  443. * This function will close our receiver's window so that we stop
  444. * receiving data for the current connection.
  445. *
  446. * \hideinitializer
  447. */
  448. #define uip_stop() (uip_conn->tcpstateflags |= UIP_STOPPED)
  449. /**
  450. * Find out if the current connection has been previously stopped with
  451. * uip_stop().
  452. *
  453. * \hideinitializer
  454. */
  455. #define uip_stopped(conn) ((conn)->tcpstateflags & UIP_STOPPED)
  456. /**
  457. * Restart the current connection, if is has previously been stopped
  458. * with uip_stop().
  459. *
  460. * This function will open the receiver's window again so that we
  461. * start receiving data for the current connection.
  462. *
  463. * \hideinitializer
  464. */
  465. #define uip_restart() do { uip_flags |= UIP_NEWDATA; \
  466. uip_conn->tcpstateflags &= ~UIP_STOPPED; \
  467. } while(0)
  468. /* uIP tests that can be made to determine in what state the current
  469. connection is, and what the application function should do. */
  470. /**
  471. * Is new incoming data available?
  472. *
  473. * Will reduce to non-zero if there is new data for the application
  474. * present at the uip_appdata pointer. The size of the data is
  475. * avaliable through the uip_len variable.
  476. *
  477. * \hideinitializer
  478. */
  479. #define uip_newdata() (uip_flags & UIP_NEWDATA)
  480. /**
  481. * Has previously sent data been acknowledged?
  482. *
  483. * Will reduce to non-zero if the previously sent data has been
  484. * acknowledged by the remote host. This means that the application
  485. * can send new data.
  486. *
  487. * \hideinitializer
  488. */
  489. #define uip_acked() (uip_flags & UIP_ACKDATA)
  490. /**
  491. * Has the connection just been connected?
  492. *
  493. * Reduces to non-zero if the current connection has been connected to
  494. * a remote host. This will happen both if the connection has been
  495. * actively opened (with uip_connect()) or passively opened (with
  496. * uip_listen()).
  497. *
  498. * \hideinitializer
  499. */
  500. #define uip_connected() (uip_flags & UIP_CONNECTED)
  501. /**
  502. * Has the connection been closed by the other end?
  503. *
  504. * Is non-zero if the connection has been closed by the remote
  505. * host. The application may then do the necessary clean-ups.
  506. *
  507. * \hideinitializer
  508. */
  509. #define uip_closed() (uip_flags & UIP_CLOSE)
  510. /**
  511. * Has the connection been aborted by the other end?
  512. *
  513. * Non-zero if the current connection has been aborted (reset) by the
  514. * remote host.
  515. *
  516. * \hideinitializer
  517. */
  518. #define uip_aborted() (uip_flags & UIP_ABORT)
  519. /**
  520. * Has the connection timed out?
  521. *
  522. * Non-zero if the current connection has been aborted due to too many
  523. * retransmissions.
  524. *
  525. * \hideinitializer
  526. */
  527. #define uip_timedout() (uip_flags & UIP_TIMEDOUT)
  528. /**
  529. * Do we need to retransmit previously data?
  530. *
  531. * Reduces to non-zero if the previously sent data has been lost in
  532. * the network, and the application should retransmit it. The
  533. * application should send the exact same data as it did the last
  534. * time, using the uip_send() function.
  535. *
  536. * \hideinitializer
  537. */
  538. #define uip_rexmit() (uip_flags & UIP_REXMIT)
  539. /**
  540. * Is the connection being polled by uIP?
  541. *
  542. * Is non-zero if the reason the application is invoked is that the
  543. * current connection has been idle for a while and should be
  544. * polled.
  545. *
  546. * The polling event can be used for sending data without having to
  547. * wait for the remote host to send data.
  548. *
  549. * \hideinitializer
  550. */
  551. #define uip_poll() (uip_flags & UIP_POLL)
  552. /**
  553. * Get the initial maxium segment size (MSS) of the current
  554. * connection.
  555. *
  556. * \hideinitializer
  557. */
  558. #define uip_initialmss() (uip_conn->initialmss)
  559. /**
  560. * Get the current maxium segment size that can be sent on the current
  561. * connection.
  562. *
  563. * The current maxiumum segment size that can be sent on the
  564. * connection is computed from the receiver's window and the MSS of
  565. * the connection (which also is available by calling
  566. * uip_initialmss()).
  567. *
  568. * \hideinitializer
  569. */
  570. #define uip_mss() (uip_conn->mss)
  571. /**
  572. * Set up a new UDP connection.
  573. *
  574. * \param ripaddr A pointer to a 4-byte structure representing the IP
  575. * address of the remote host.
  576. *
  577. * \param rport The remote port number in network byte order.
  578. *
  579. * \return The uip_udp_conn structure for the new connection or NULL
  580. * if no connection could be allocated.
  581. */
  582. struct uip_udp_conn *uip_udp_new(u16_t *ripaddr, u16_t rport);
  583. /**
  584. * Removed a UDP connection.
  585. *
  586. * \param conn A pointer to the uip_udp_conn structure for the connection.
  587. *
  588. * \hideinitializer
  589. */
  590. #define uip_udp_remove(conn) (conn)->lport = 0
  591. /**
  592. * Send a UDP datagram of length len on the current connection.
  593. *
  594. * This function can only be called in response to a UDP event (poll
  595. * or newdata). The data must be present in the uip_buf buffer, at the
  596. * place pointed to by the uip_appdata pointer.
  597. *
  598. * \param len The length of the data in the uip_buf buffer.
  599. *
  600. * \hideinitializer
  601. */
  602. #define uip_udp_send(len) uip_slen = (len)
  603. /** @} */
  604. /* uIP convenience and converting functions. */
  605. /**
  606. * \defgroup uipconvfunc uIP conversion functions
  607. * @{
  608. *
  609. * These functions can be used for converting between different data
  610. * formats used by uIP.
  611. */
  612. /**
  613. * Pack an IP address into a 4-byte array which is used by uIP to
  614. * represent IP addresses.
  615. *
  616. * Example:
  617. \code
  618. u16_t ipaddr[2];
  619. uip_ipaddr(&ipaddr, 192,168,1,2);
  620. \endcode
  621. *
  622. * \param addr A pointer to a 4-byte array that will be filled in with
  623. * the IP addres.
  624. * \param addr0 The first octet of the IP address.
  625. * \param addr1 The second octet of the IP address.
  626. * \param addr2 The third octet of the IP address.
  627. * \param addr3 The forth octet of the IP address.
  628. *
  629. * \hideinitializer
  630. */
  631. #define uip_ipaddr(addr, addr0,addr1,addr2,addr3) do { \
  632. (addr)[0] = HTONS(((addr0) << 8) | (addr1)); \
  633. (addr)[1] = HTONS(((addr2) << 8) | (addr3)); \
  634. } while(0)
  635. /**
  636. * Convert 16-bit quantity from host byte order to network byte order.
  637. *
  638. * This macro is primarily used for converting constants from host
  639. * byte order to network byte order. For converting variables to
  640. * network byte order, use the htons() function instead.
  641. *
  642. * \hideinitializer
  643. */
  644. #ifndef HTONS
  645. # if BYTE_ORDER == BIG_ENDIAN
  646. # define HTONS(n) (n)
  647. # else /* BYTE_ORDER == BIG_ENDIAN */
  648. # define HTONS(n) ((((u16_t)((n) & 0xff)) << 8) | (((n) & 0xff00) >> 8))
  649. # endif /* BYTE_ORDER == BIG_ENDIAN */
  650. #endif /* HTONS */
  651. /**
  652. * Convert 16-bit quantity from host byte order to network byte order.
  653. *
  654. * This function is primarily used for converting variables from host
  655. * byte order to network byte order. For converting constants to
  656. * network byte order, use the HTONS() macro instead.
  657. */
  658. #ifndef htons
  659. u16_t htons(u16_t val);
  660. #endif /* htons */
  661. /** @} */
  662. /**
  663. * Pointer to the application data in the packet buffer.
  664. *
  665. * This pointer points to the application data when the application is
  666. * called. If the application wishes to send data, the application may
  667. * use this space to write the data into before calling uip_send().
  668. */
  669. extern volatile u8_t *uip_appdata;
  670. extern volatile u8_t *uip_sappdata;
  671. #if UIP_URGDATA > 0
  672. /* u8_t *uip_urgdata:
  673. *
  674. * This pointer points to any urgent data that has been received. Only
  675. * present if compiled with support for urgent data (UIP_URGDATA).
  676. */
  677. extern volatile u8_t *uip_urgdata;
  678. #endif /* UIP_URGDATA > 0 */
  679. /* u[8|16]_t uip_len:
  680. *
  681. * When the application is called, uip_len contains the length of any
  682. * new data that has been received from the remote host. The
  683. * application should set this variable to the size of any data that
  684. * the application wishes to send. When the network device driver
  685. * output function is called, uip_len should contain the length of the
  686. * outgoing packet.
  687. */
  688. extern volatile u16_t uip_len, uip_slen;
  689. #if UIP_URGDATA > 0
  690. extern volatile u8_t uip_urglen, uip_surglen;
  691. #endif /* UIP_URGDATA > 0 */
  692. /**
  693. * Representation of a uIP TCP connection.
  694. *
  695. * The uip_conn structure is used for identifying a connection. All
  696. * but one field in the structure are to be considered read-only by an
  697. * application. The only exception is the appstate field whos purpose
  698. * is to let the application store application-specific state (e.g.,
  699. * file pointers) for the connection. The size of this field is
  700. * configured in the "uipopt.h" header file.
  701. */
  702. struct uip_conn {
  703. u16_t ripaddr[2]; /**< The IP address of the remote host. */
  704. u16_t lport; /**< The local TCP port, in network byte order. */
  705. u16_t rport; /**< The local remote TCP port, in network byte
  706. order. */
  707. u8_t rcv_nxt[4]; /**< The sequence number that we expect to
  708. receive next. */
  709. u8_t snd_nxt[4]; /**< The sequence number that was last sent by
  710. us. */
  711. u16_t len; /**< Length of the data that was previously sent. */
  712. u16_t mss; /**< Current maximum segment size for the
  713. connection. */
  714. u16_t initialmss; /**< Initial maximum segment size for the
  715. connection. */
  716. u8_t sa; /**< Retransmission time-out calculation state
  717. variable. */
  718. u8_t sv; /**< Retransmission time-out calculation state
  719. variable. */
  720. u8_t rto; /**< Retransmission time-out. */
  721. u8_t tcpstateflags; /**< TCP state and flags. */
  722. u8_t timer; /**< The retransmission timer. */
  723. u8_t nrtx; /**< The number of retransmissions for the last
  724. segment sent. */
  725. /** The application state. */
  726. u8_t appstate[UIP_APPSTATE_SIZE];
  727. };
  728. /* Pointer to the current connection. */
  729. extern struct uip_conn *uip_conn;
  730. /* The array containing all uIP connections. */
  731. extern struct uip_conn uip_conns[UIP_CONNS];
  732. /**
  733. * \addtogroup uiparch
  734. * @{
  735. */
  736. /**
  737. * 4-byte array used for the 32-bit sequence number calculations.
  738. */
  739. extern volatile u8_t uip_acc32[4];
  740. /** @} */
  741. #if UIP_UDP
  742. /**
  743. * Representation of a uIP UDP connection.
  744. */
  745. struct uip_udp_conn {
  746. u16_t ripaddr[2]; /**< The IP address of the remote peer. */
  747. u16_t lport; /**< The local port number in network byte order. */
  748. u16_t rport; /**< The remote port number in network byte order. */
  749. };
  750. extern struct uip_udp_conn *uip_udp_conn;
  751. extern struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS];
  752. #endif /* UIP_UDP */
  753. /**
  754. * The structure holding the TCP/IP statistics that are gathered if
  755. * UIP_STATISTICS is set to 1.
  756. *
  757. */
  758. struct uip_stats {
  759. struct {
  760. uip_stats_t drop; /**< Number of dropped packets at the IP
  761. layer. */
  762. uip_stats_t recv; /**< Number of received packets at the IP
  763. layer. */
  764. uip_stats_t sent; /**< Number of sent packets at the IP
  765. layer. */
  766. uip_stats_t vhlerr; /**< Number of packets dropped due to wrong
  767. IP version or header length. */
  768. uip_stats_t hblenerr; /**< Number of packets dropped due to wrong
  769. IP length, high byte. */
  770. uip_stats_t lblenerr; /**< Number of packets dropped due to wrong
  771. IP length, low byte. */
  772. uip_stats_t fragerr; /**< Number of packets dropped since they
  773. were IP fragments. */
  774. uip_stats_t chkerr; /**< Number of packets dropped due to IP
  775. checksum errors. */
  776. uip_stats_t protoerr; /**< Number of packets dropped since they
  777. were neither ICMP, UDP nor TCP. */
  778. } ip; /**< IP statistics. */
  779. struct {
  780. uip_stats_t drop; /**< Number of dropped ICMP packets. */
  781. uip_stats_t recv; /**< Number of received ICMP packets. */
  782. uip_stats_t sent; /**< Number of sent ICMP packets. */
  783. uip_stats_t typeerr; /**< Number of ICMP packets with a wrong
  784. type. */
  785. } icmp; /**< ICMP statistics. */
  786. struct {
  787. uip_stats_t drop; /**< Number of dropped TCP segments. */
  788. uip_stats_t recv; /**< Number of recived TCP segments. */
  789. uip_stats_t sent; /**< Number of sent TCP segments. */
  790. uip_stats_t chkerr; /**< Number of TCP segments with a bad
  791. checksum. */
  792. uip_stats_t ackerr; /**< Number of TCP segments with a bad ACK
  793. number. */
  794. uip_stats_t rst; /**< Number of recevied TCP RST (reset) segments. */
  795. uip_stats_t rexmit; /**< Number of retransmitted TCP segments. */
  796. uip_stats_t syndrop; /**< Number of dropped SYNs due to too few
  797. connections was avaliable. */
  798. uip_stats_t synrst; /**< Number of SYNs for closed ports,
  799. triggering a RST. */
  800. } tcp; /**< TCP statistics. */
  801. };
  802. /**
  803. * The uIP TCP/IP statistics.
  804. *
  805. * This is the variable in which the uIP TCP/IP statistics are gathered.
  806. */
  807. extern struct uip_stats uip_stat;
  808. /*-----------------------------------------------------------------------------------*/
  809. /* All the stuff below this point is internal to uIP and should not be
  810. * used directly by an application or by a device driver.
  811. */
  812. /*-----------------------------------------------------------------------------------*/
  813. /* u8_t uip_flags:
  814. *
  815. * When the application is called, uip_flags will contain the flags
  816. * that are defined in this file. Please read below for more
  817. * infomation.
  818. */
  819. extern volatile u8_t uip_flags;
  820. /* The following flags may be set in the global variable uip_flags
  821. before calling the application callback. The UIP_ACKDATA and
  822. UIP_NEWDATA flags may both be set at the same time, whereas the
  823. others are mutualy exclusive. Note that these flags should *NOT* be
  824. accessed directly, but through the uIP functions/macros. */
  825. #define UIP_ACKDATA 1 /* Signifies that the outstanding data was
  826. acked and the application should send
  827. out new data instead of retransmitting
  828. the last data. */
  829. #define UIP_NEWDATA 2 /* Flags the fact that the peer has sent
  830. us new data. */
  831. #define UIP_REXMIT 4 /* Tells the application to retransmit the
  832. data that was last sent. */
  833. #define UIP_POLL 8 /* Used for polling the application, to
  834. check if the application has data that
  835. it wants to send. */
  836. #define UIP_CLOSE 16 /* The remote host has closed the
  837. connection, thus the connection has
  838. gone away. Or the application signals
  839. that it wants to close the
  840. connection. */
  841. #define UIP_ABORT 32 /* The remote host has aborted the
  842. connection, thus the connection has
  843. gone away. Or the application signals
  844. that it wants to abort the
  845. connection. */
  846. #define UIP_CONNECTED 64 /* We have got a connection from a remote
  847. host and have set up a new connection
  848. for it, or an active connection has
  849. been successfully established. */
  850. #define UIP_TIMEDOUT 128 /* The connection has been aborted due to
  851. too many retransmissions. */
  852. /* uip_process(flag):
  853. *
  854. * The actual uIP function which does all the work.
  855. */
  856. void uip_process(u8_t flag);
  857. /* The following flags are passed as an argument to the uip_process()
  858. function. They are used to distinguish between the two cases where
  859. uip_process() is called. It can be called either because we have
  860. incoming data that should be processed, or because the periodic
  861. timer has fired. */
  862. #define UIP_DATA 1 /* Tells uIP that there is incoming data in
  863. the uip_buf buffer. The length of the
  864. data is stored in the global variable
  865. uip_len. */
  866. #define UIP_TIMER 2 /* Tells uIP that the periodic timer has
  867. fired. */
  868. #if UIP_UDP
  869. #define UIP_UDP_TIMER 3
  870. #endif /* UIP_UDP */
  871. /* The TCP states used in the uip_conn->tcpstateflags. */
  872. #define CLOSED 0
  873. #define SYN_RCVD 1
  874. #define SYN_SENT 2
  875. #define ESTABLISHED 3
  876. #define FIN_WAIT_1 4
  877. #define FIN_WAIT_2 5
  878. #define CLOSING 6
  879. #define TIME_WAIT 7
  880. #define LAST_ACK 8
  881. #define TS_MASK 15
  882. #define UIP_STOPPED 16
  883. #define UIP_TCPIP_HLEN 40
  884. /* The TCP and IP headers. */
  885. typedef struct {
  886. /* IP header. */
  887. u8_t vhl,
  888. tos,
  889. len[2],
  890. ipid[2],
  891. ipoffset[2],
  892. ttl,
  893. proto;
  894. u16_t ipchksum;
  895. u16_t srcipaddr[2],
  896. destipaddr[2];
  897. /* TCP header. */
  898. u16_t srcport,
  899. destport;
  900. u8_t seqno[4],
  901. ackno[4],
  902. tcpoffset,
  903. flags,
  904. wnd[2];
  905. u16_t tcpchksum;
  906. u8_t urgp[2];
  907. u8_t optdata[4];
  908. } uip_tcpip_hdr;
  909. /* The ICMP and IP headers. */
  910. typedef struct {
  911. /* IP header. */
  912. u8_t vhl,
  913. tos,
  914. len[2],
  915. ipid[2],
  916. ipoffset[2],
  917. ttl,
  918. proto;
  919. u16_t ipchksum;
  920. u16_t srcipaddr[2],
  921. destipaddr[2];
  922. /* ICMP (echo) header. */
  923. u8_t type, icode;
  924. u16_t icmpchksum;
  925. u16_t id, seqno;
  926. } uip_icmpip_hdr;
  927. /* The UDP and IP headers. */
  928. typedef struct {
  929. /* IP header. */
  930. u8_t vhl,
  931. tos,
  932. len[2],
  933. ipid[2],
  934. ipoffset[2],
  935. ttl,
  936. proto;
  937. u16_t ipchksum;
  938. u16_t srcipaddr[2],
  939. destipaddr[2];
  940. /* UDP header. */
  941. u16_t srcport,
  942. destport;
  943. u16_t udplen;
  944. u16_t udpchksum;
  945. } uip_udpip_hdr;
  946. #define UIP_PROTO_ICMP 1
  947. #define UIP_PROTO_TCP 6
  948. #define UIP_PROTO_UDP 17
  949. #if UIP_FIXEDADDR
  950. extern const u16_t uip_hostaddr[2];
  951. #else /* UIP_FIXEDADDR */
  952. extern u16_t uip_hostaddr[2];
  953. #endif /* UIP_FIXEDADDR */
  954. #endif /* __UIP_H__ */
  955. /** @} */