uip.h 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  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: uip.h,v 1.40 2006/06/08 07:12:07 adam Exp $
  46. *
  47. */
  48. #ifndef __UIP_H__
  49. #define __UIP_H__
  50. #include "uipopt.h"
  51. /**
  52. * Repressentation of an IP address.
  53. *
  54. */
  55. typedef u16_t uip_ip4addr_t[2];
  56. typedef u16_t uip_ip6addr_t[8];
  57. #if UIP_CONF_IPV6
  58. typedef uip_ip6addr_t uip_ipaddr_t;
  59. #else /* UIP_CONF_IPV6 */
  60. typedef uip_ip4addr_t uip_ipaddr_t;
  61. #endif /* UIP_CONF_IPV6 */
  62. /*---------------------------------------------------------------------------*/
  63. /* First, the functions that should be called from the
  64. * system. Initialization, the periodic timer and incoming packets are
  65. * handled by the following three functions.
  66. */
  67. /**
  68. * \defgroup uipconffunc uIP configuration functions
  69. * @{
  70. *
  71. * The uIP configuration functions are used for setting run-time
  72. * parameters in uIP such as IP addresses.
  73. */
  74. /**
  75. * Set the IP address of this host.
  76. *
  77. * The IP address is represented as a 4-byte array where the first
  78. * octet of the IP address is put in the first member of the 4-byte
  79. * array.
  80. *
  81. * Example:
  82. \code
  83. uip_ipaddr_t addr;
  84. uip_ipaddr(&addr, 192,168,1,2);
  85. uip_sethostaddr(&addr);
  86. \endcode
  87. * \param addr A pointer to an IP address of type uip_ipaddr_t;
  88. *
  89. * \sa uip_ipaddr()
  90. *
  91. * \hideinitializer
  92. */
  93. #define uip_sethostaddr(addr) uip_ipaddr_copy(uip_hostaddr, (addr))
  94. /**
  95. * Get the IP address of this host.
  96. *
  97. * The IP address is represented as a 4-byte array where the first
  98. * octet of the IP address is put in the first member of the 4-byte
  99. * array.
  100. *
  101. * Example:
  102. \code
  103. uip_ipaddr_t hostaddr;
  104. uip_gethostaddr(&hostaddr);
  105. \endcode
  106. * \param addr A pointer to a uip_ipaddr_t variable that will be
  107. * filled in with the currently configured IP address.
  108. *
  109. * \hideinitializer
  110. */
  111. #define uip_gethostaddr(addr) uip_ipaddr_copy((addr), uip_hostaddr)
  112. /**
  113. * Set the default router's IP address.
  114. *
  115. * \param addr A pointer to a uip_ipaddr_t variable containing the IP
  116. * address of the default router.
  117. *
  118. * \sa uip_ipaddr()
  119. *
  120. * \hideinitializer
  121. */
  122. #define uip_setdraddr(addr) uip_ipaddr_copy(uip_draddr, (addr))
  123. /**
  124. * Set the netmask.
  125. *
  126. * \param addr A pointer to a uip_ipaddr_t variable containing the IP
  127. * address of the netmask.
  128. *
  129. * \sa uip_ipaddr()
  130. *
  131. * \hideinitializer
  132. */
  133. #define uip_setnetmask(addr) uip_ipaddr_copy(uip_netmask, (addr))
  134. /**
  135. * Get the default router's IP address.
  136. *
  137. * \param addr A pointer to a uip_ipaddr_t variable that will be
  138. * filled in with the IP address of the default router.
  139. *
  140. * \hideinitializer
  141. */
  142. #define uip_getdraddr(addr) uip_ipaddr_copy((addr), uip_draddr)
  143. /**
  144. * Get the netmask.
  145. *
  146. * \param addr A pointer to a uip_ipaddr_t variable that will be
  147. * filled in with the value of the netmask.
  148. *
  149. * \hideinitializer
  150. */
  151. #define uip_getnetmask(addr) uip_ipaddr_copy((addr), uip_netmask)
  152. /** @} */
  153. /**
  154. * \defgroup uipinit uIP initialization functions
  155. * @{
  156. *
  157. * The uIP initialization functions are used for booting uIP.
  158. */
  159. /**
  160. * uIP initialization function.
  161. *
  162. * This function should be called at boot up to initilize the uIP
  163. * TCP/IP stack.
  164. */
  165. void uip_init(void);
  166. /**
  167. * uIP initialization function.
  168. *
  169. * This function may be used at boot time to set the initial ip_id.
  170. */
  171. void uip_setipid(u16_t id);
  172. /** @} */
  173. /**
  174. * \defgroup uipdevfunc uIP device driver functions
  175. * @{
  176. *
  177. * These functions are used by a network device driver for interacting
  178. * with uIP.
  179. */
  180. /**
  181. * Process an incoming packet.
  182. *
  183. * This function should be called when the device driver has received
  184. * a packet from the network. The packet from the device driver must
  185. * be present in the uip_buf buffer, and the length of the packet
  186. * should be placed in the uip_len variable.
  187. *
  188. * When the function returns, there may be an outbound packet placed
  189. * in the uip_buf packet buffer. If so, the uip_len variable is set to
  190. * the length of the packet. If no packet is to be sent out, the
  191. * uip_len variable is set to 0.
  192. *
  193. * The usual way of calling the function is presented by the source
  194. * code below.
  195. \code
  196. uip_len = devicedriver_poll();
  197. if(uip_len > 0) {
  198. uip_input();
  199. if(uip_len > 0) {
  200. devicedriver_send();
  201. }
  202. }
  203. \endcode
  204. *
  205. * \note If you are writing a uIP device driver that needs ARP
  206. * (Address Resolution Protocol), e.g., when running uIP over
  207. * Ethernet, you will need to call the uIP ARP code before calling
  208. * this function:
  209. \code
  210. #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
  211. uip_len = ethernet_devicedrver_poll();
  212. if(uip_len > 0) {
  213. if(BUF->type == HTONS(UIP_ETHTYPE_IP)) {
  214. uip_arp_ipin();
  215. uip_input();
  216. if(uip_len > 0) {
  217. uip_arp_out();
  218. ethernet_devicedriver_send();
  219. }
  220. } else if(BUF->type == HTONS(UIP_ETHTYPE_ARP)) {
  221. uip_arp_arpin();
  222. if(uip_len > 0) {
  223. ethernet_devicedriver_send();
  224. }
  225. }
  226. \endcode
  227. *
  228. * \hideinitializer
  229. */
  230. #define uip_input() uip_process(UIP_DATA)
  231. /**
  232. * Periodic processing for a connection identified by its number.
  233. *
  234. * This function does the necessary periodic processing (timers,
  235. * polling) for a uIP TCP conneciton, and should be called when the
  236. * periodic uIP timer goes off. It should be called for every
  237. * connection, regardless of whether they are open of closed.
  238. *
  239. * When the function returns, it may have an outbound packet waiting
  240. * for service in the uIP packet buffer, and if so the uip_len
  241. * variable is set to a value larger than zero. The device driver
  242. * should be called to send out the packet.
  243. *
  244. * The ususal way of calling the function is through a for() loop like
  245. * this:
  246. \code
  247. for(i = 0; i < UIP_CONNS; ++i) {
  248. uip_periodic(i);
  249. if(uip_len > 0) {
  250. devicedriver_send();
  251. }
  252. }
  253. \endcode
  254. *
  255. * \note If you are writing a uIP device driver that needs ARP
  256. * (Address Resolution Protocol), e.g., when running uIP over
  257. * Ethernet, you will need to call the uip_arp_out() function before
  258. * calling the device driver:
  259. \code
  260. for(i = 0; i < UIP_CONNS; ++i) {
  261. uip_periodic(i);
  262. if(uip_len > 0) {
  263. uip_arp_out();
  264. ethernet_devicedriver_send();
  265. }
  266. }
  267. \endcode
  268. *
  269. * \param conn The number of the connection which is to be periodically polled.
  270. *
  271. * \hideinitializer
  272. */
  273. #define uip_periodic(conn) do { uip_conn = &uip_conns[conn]; \
  274. uip_process(UIP_TIMER); } while (0)
  275. /**
  276. *
  277. *
  278. */
  279. #define uip_conn_active(conn) (uip_conns[conn].tcpstateflags != UIP_CLOSED)
  280. /**
  281. * Perform periodic processing for a connection identified by a pointer
  282. * to its structure.
  283. *
  284. * Same as uip_periodic() but takes a pointer to the actual uip_conn
  285. * struct instead of an integer as its argument. This function can be
  286. * used to force periodic processing of a specific connection.
  287. *
  288. * \param conn A pointer to the uip_conn struct for the connection to
  289. * be processed.
  290. *
  291. * \hideinitializer
  292. */
  293. #define uip_periodic_conn(conn) do { uip_conn = conn; \
  294. uip_process(UIP_TIMER); } while (0)
  295. /**
  296. * Reuqest that a particular connection should be polled.
  297. *
  298. * Similar to uip_periodic_conn() but does not perform any timer
  299. * processing. The application is polled for new data.
  300. *
  301. * \param conn A pointer to the uip_conn struct for the connection to
  302. * be processed.
  303. *
  304. * \hideinitializer
  305. */
  306. #define uip_poll_conn(conn) do { uip_conn = conn; \
  307. uip_process(UIP_POLL_REQUEST); } while (0)
  308. #if UIP_UDP
  309. /**
  310. * Periodic processing for a UDP connection identified by its number.
  311. *
  312. * This function is essentially the same as uip_periodic(), but for
  313. * UDP connections. It is called in a similar fashion as the
  314. * uip_periodic() function:
  315. \code
  316. for(i = 0; i < UIP_UDP_CONNS; i++) {
  317. uip_udp_periodic(i);
  318. if(uip_len > 0) {
  319. devicedriver_send();
  320. }
  321. }
  322. \endcode
  323. *
  324. * \note As for the uip_periodic() function, special care has to be
  325. * taken when using uIP together with ARP and Ethernet:
  326. \code
  327. for(i = 0; i < UIP_UDP_CONNS; i++) {
  328. uip_udp_periodic(i);
  329. if(uip_len > 0) {
  330. uip_arp_out();
  331. ethernet_devicedriver_send();
  332. }
  333. }
  334. \endcode
  335. *
  336. * \param conn The number of the UDP connection to be processed.
  337. *
  338. * \hideinitializer
  339. */
  340. #define uip_udp_periodic(conn) do { uip_udp_conn = &uip_udp_conns[conn]; \
  341. uip_process(UIP_UDP_TIMER); } while (0)
  342. /**
  343. * Periodic processing for a UDP connection identified by a pointer to
  344. * its structure.
  345. *
  346. * Same as uip_udp_periodic() but takes a pointer to the actual
  347. * uip_conn struct instead of an integer as its argument. This
  348. * function can be used to force periodic processing of a specific
  349. * connection.
  350. *
  351. * \param conn A pointer to the uip_udp_conn struct for the connection
  352. * to be processed.
  353. *
  354. * \hideinitializer
  355. */
  356. #define uip_udp_periodic_conn(conn) do { uip_udp_conn = conn; \
  357. uip_process(UIP_UDP_TIMER); } while (0)
  358. #endif /* UIP_UDP */
  359. /**
  360. * The uIP packet buffer.
  361. *
  362. * The uip_buf array is used to hold incoming and outgoing
  363. * packets. The device driver should place incoming data into this
  364. * buffer. When sending data, the device driver should read the link
  365. * level headers and the TCP/IP headers from this buffer. The size of
  366. * the link level headers is configured by the UIP_LLH_LEN define.
  367. *
  368. * \note The application data need not be placed in this buffer, so
  369. * the device driver must read it from the place pointed to by the
  370. * uip_appdata pointer as illustrated by the following example:
  371. \code
  372. void
  373. devicedriver_send(void)
  374. {
  375. hwsend(&uip_buf[0], UIP_LLH_LEN);
  376. if(uip_len <= UIP_LLH_LEN + UIP_TCPIP_HLEN) {
  377. hwsend(&uip_buf[UIP_LLH_LEN], uip_len - UIP_LLH_LEN);
  378. } else {
  379. hwsend(&uip_buf[UIP_LLH_LEN], UIP_TCPIP_HLEN);
  380. hwsend(uip_appdata, uip_len - UIP_TCPIP_HLEN - UIP_LLH_LEN);
  381. }
  382. }
  383. \endcode
  384. */
  385. extern u8_t uip_buf[UIP_BUFSIZE+2];
  386. /** @} */
  387. /*---------------------------------------------------------------------------*/
  388. /* Functions that are used by the uIP application program. Opening and
  389. * closing connections, sending and receiving data, etc. is all
  390. * handled by the functions below.
  391. */
  392. /**
  393. * \defgroup uipappfunc uIP application functions
  394. * @{
  395. *
  396. * Functions used by an application running of top of uIP.
  397. */
  398. /**
  399. * Start listening to the specified port.
  400. *
  401. * \note Since this function expects the port number in network byte
  402. * order, a conversion using HTONS() or uip_htons() is necessary.
  403. *
  404. \code
  405. uip_listen(HTONS(80));
  406. \endcode
  407. *
  408. * \param port A 16-bit port number in network byte order.
  409. */
  410. void uip_listen(u16_t port);
  411. /**
  412. * Stop listening to the specified port.
  413. *
  414. * \note Since this function expects the port number in network byte
  415. * order, a conversion using HTONS() or uip_htons() is necessary.
  416. *
  417. \code
  418. uip_unlisten(HTONS(80));
  419. \endcode
  420. *
  421. * \param port A 16-bit port number in network byte order.
  422. */
  423. void uip_unlisten(u16_t port);
  424. /**
  425. * Connect to a remote host using TCP.
  426. *
  427. * This function is used to start a new connection to the specified
  428. * port on the specied host. It allocates a new connection identifier,
  429. * sets the connection to the SYN_SENT state and sets the
  430. * retransmission timer to 0. This will cause a TCP SYN segment to be
  431. * sent out the next time this connection is periodically processed,
  432. * which usually is done within 0.5 seconds after the call to
  433. * uip_connect().
  434. *
  435. * \note This function is avaliable only if support for active open
  436. * has been configured by defining UIP_ACTIVE_OPEN to 1 in uipopt.h.
  437. *
  438. * \note Since this function requires the port number to be in network
  439. * byte order, a conversion using HTONS() or uip_htons() is necessary.
  440. *
  441. \code
  442. uip_ipaddr_t ipaddr;
  443. uip_ipaddr(&ipaddr, 192,168,1,2);
  444. uip_connect(&ipaddr, HTONS(80));
  445. \endcode
  446. *
  447. * \param ripaddr The IP address of the remote hot.
  448. *
  449. * \param port A 16-bit port number in network byte order.
  450. *
  451. * \return A pointer to the uIP connection identifier for the new connection,
  452. * or NULL if no connection could be allocated.
  453. *
  454. */
  455. struct uip_conn *uip_connect(uip_ipaddr_t *ripaddr, u16_t port);
  456. /**
  457. * \internal
  458. *
  459. * Check if a connection has outstanding (i.e., unacknowledged) data.
  460. *
  461. * \param conn A pointer to the uip_conn structure for the connection.
  462. *
  463. * \hideinitializer
  464. */
  465. #define uip_outstanding(conn) ((conn)->len)
  466. /**
  467. * Send data on the current connection.
  468. *
  469. * This function is used to send out a single segment of TCP
  470. * data. Only applications that have been invoked by uIP for event
  471. * processing can send data.
  472. *
  473. * The amount of data that actually is sent out after a call to this
  474. * funcion is determined by the maximum amount of data TCP allows. uIP
  475. * will automatically crop the data so that only the appropriate
  476. * amount of data is sent. The function uip_mss() can be used to query
  477. * uIP for the amount of data that actually will be sent.
  478. *
  479. * \note This function does not guarantee that the sent data will
  480. * arrive at the destination. If the data is lost in the network, the
  481. * application will be invoked with the uip_rexmit() event being
  482. * set. The application will then have to resend the data using this
  483. * function.
  484. *
  485. * \param data A pointer to the data which is to be sent.
  486. *
  487. * \param len The maximum amount of data bytes to be sent.
  488. *
  489. * \hideinitializer
  490. */
  491. void uip_send(const void *data, int len);
  492. /**
  493. * The length of any incoming data that is currently avaliable (if avaliable)
  494. * in the uip_appdata buffer.
  495. *
  496. * The test function uip_data() must first be used to check if there
  497. * is any data available at all.
  498. *
  499. * \hideinitializer
  500. */
  501. /*void uip_datalen(void);*/
  502. #define uip_datalen() uip_len
  503. /**
  504. * The length of any out-of-band data (urgent data) that has arrived
  505. * on the connection.
  506. *
  507. * \note The configuration parameter UIP_URGDATA must be set for this
  508. * function to be enabled.
  509. *
  510. * \hideinitializer
  511. */
  512. #define uip_urgdatalen() uip_urglen
  513. /**
  514. * Close the current connection.
  515. *
  516. * This function will close the current connection in a nice way.
  517. *
  518. * \hideinitializer
  519. */
  520. #define uip_close() (uip_flags = UIP_CLOSE)
  521. /**
  522. * Abort the current connection.
  523. *
  524. * This function will abort (reset) the current connection, and is
  525. * usually used when an error has occured that prevents using the
  526. * uip_close() function.
  527. *
  528. * \hideinitializer
  529. */
  530. #define uip_abort() (uip_flags = UIP_ABORT)
  531. /**
  532. * Tell the sending host to stop sending data.
  533. *
  534. * This function will close our receiver's window so that we stop
  535. * receiving data for the current connection.
  536. *
  537. * \hideinitializer
  538. */
  539. #define uip_stop() (uip_conn->tcpstateflags |= UIP_STOPPED)
  540. /**
  541. * Find out if the current connection has been previously stopped with
  542. * uip_stop().
  543. *
  544. * \hideinitializer
  545. */
  546. #define uip_stopped(conn) ((conn)->tcpstateflags & UIP_STOPPED)
  547. /**
  548. * Restart the current connection, if is has previously been stopped
  549. * with uip_stop().
  550. *
  551. * This function will open the receiver's window again so that we
  552. * start receiving data for the current connection.
  553. *
  554. * \hideinitializer
  555. */
  556. #define uip_restart() do { uip_flags |= UIP_NEWDATA; \
  557. uip_conn->tcpstateflags &= ~UIP_STOPPED; \
  558. } while(0)
  559. /* uIP tests that can be made to determine in what state the current
  560. connection is, and what the application function should do. */
  561. /**
  562. * Is the current connection a UDP connection?
  563. *
  564. * This function checks whether the current connection is a UDP connection.
  565. *
  566. * \hideinitializer
  567. *
  568. */
  569. #define uip_udpconnection() (uip_conn == NULL)
  570. /**
  571. * Is new incoming data available?
  572. *
  573. * Will reduce to non-zero if there is new data for the application
  574. * present at the uip_appdata pointer. The size of the data is
  575. * avaliable through the uip_len variable.
  576. *
  577. * \hideinitializer
  578. */
  579. #define uip_newdata() (uip_flags & UIP_NEWDATA)
  580. /**
  581. * Has previously sent data been acknowledged?
  582. *
  583. * Will reduce to non-zero if the previously sent data has been
  584. * acknowledged by the remote host. This means that the application
  585. * can send new data.
  586. *
  587. * \hideinitializer
  588. */
  589. #define uip_acked() (uip_flags & UIP_ACKDATA)
  590. /**
  591. * Has the connection just been connected?
  592. *
  593. * Reduces to non-zero if the current connection has been connected to
  594. * a remote host. This will happen both if the connection has been
  595. * actively opened (with uip_connect()) or passively opened (with
  596. * uip_listen()).
  597. *
  598. * \hideinitializer
  599. */
  600. #define uip_connected() (uip_flags & UIP_CONNECTED)
  601. /**
  602. * Has the connection been closed by the other end?
  603. *
  604. * Is non-zero if the connection has been closed by the remote
  605. * host. The application may then do the necessary clean-ups.
  606. *
  607. * \hideinitializer
  608. */
  609. #define uip_closed() (uip_flags & UIP_CLOSE)
  610. /**
  611. * Has the connection been aborted by the other end?
  612. *
  613. * Non-zero if the current connection has been aborted (reset) by the
  614. * remote host.
  615. *
  616. * \hideinitializer
  617. */
  618. #define uip_aborted() (uip_flags & UIP_ABORT)
  619. /**
  620. * Has the connection timed out?
  621. *
  622. * Non-zero if the current connection has been aborted due to too many
  623. * retransmissions.
  624. *
  625. * \hideinitializer
  626. */
  627. #define uip_timedout() (uip_flags & UIP_TIMEDOUT)
  628. /**
  629. * Do we need to retransmit previously data?
  630. *
  631. * Reduces to non-zero if the previously sent data has been lost in
  632. * the network, and the application should retransmit it. The
  633. * application should send the exact same data as it did the last
  634. * time, using the uip_send() function.
  635. *
  636. * \hideinitializer
  637. */
  638. #define uip_rexmit() (uip_flags & UIP_REXMIT)
  639. /**
  640. * Is the connection being polled by uIP?
  641. *
  642. * Is non-zero if the reason the application is invoked is that the
  643. * current connection has been idle for a while and should be
  644. * polled.
  645. *
  646. * The polling event can be used for sending data without having to
  647. * wait for the remote host to send data.
  648. *
  649. * \hideinitializer
  650. */
  651. #define uip_poll() (uip_flags & UIP_POLL)
  652. /**
  653. * Get the initial maxium segment size (MSS) of the current
  654. * connection.
  655. *
  656. * \hideinitializer
  657. */
  658. #define uip_initialmss() (uip_conn->initialmss)
  659. /**
  660. * Get the current maxium segment size that can be sent on the current
  661. * connection.
  662. *
  663. * The current maxiumum segment size that can be sent on the
  664. * connection is computed from the receiver's window and the MSS of
  665. * the connection (which also is available by calling
  666. * uip_initialmss()).
  667. *
  668. * \hideinitializer
  669. */
  670. #define uip_mss() (uip_conn->mss)
  671. /**
  672. * Set up a new UDP connection.
  673. *
  674. * This function sets up a new UDP connection. The function will
  675. * automatically allocate an unused local port for the new
  676. * connection. However, another port can be chosen by using the
  677. * uip_udp_bind() call, after the uip_udp_new() function has been
  678. * called.
  679. *
  680. * Example:
  681. \code
  682. uip_ipaddr_t addr;
  683. struct uip_udp_conn *c;
  684. uip_ipaddr(&addr, 192,168,2,1);
  685. c = uip_udp_new(&addr, HTONS(12345));
  686. if(c != NULL) {
  687. uip_udp_bind(c, HTONS(12344));
  688. }
  689. \endcode
  690. * \param ripaddr The IP address of the remote host.
  691. *
  692. * \param rport The remote port number in network byte order.
  693. *
  694. * \return The uip_udp_conn structure for the new connection or NULL
  695. * if no connection could be allocated.
  696. */
  697. struct uip_udp_conn *uip_udp_new(uip_ipaddr_t *ripaddr, u16_t rport);
  698. /**
  699. * Removed a UDP connection.
  700. *
  701. * \param conn A pointer to the uip_udp_conn structure for the connection.
  702. *
  703. * \hideinitializer
  704. */
  705. #define uip_udp_remove(conn) (conn)->lport = 0
  706. /**
  707. * Bind a UDP connection to a local port.
  708. *
  709. * \param conn A pointer to the uip_udp_conn structure for the
  710. * connection.
  711. *
  712. * \param port The local port number, in network byte order.
  713. *
  714. * \hideinitializer
  715. */
  716. #define uip_udp_bind(conn, port) (conn)->lport = port
  717. /**
  718. * Send a UDP datagram of length len on the current connection.
  719. *
  720. * This function can only be called in response to a UDP event (poll
  721. * or newdata). The data must be present in the uip_buf buffer, at the
  722. * place pointed to by the uip_appdata pointer.
  723. *
  724. * \param len The length of the data in the uip_buf buffer.
  725. *
  726. * \hideinitializer
  727. */
  728. #define uip_udp_send(len) uip_send((char *)uip_appdata, len)
  729. /** @} */
  730. /* uIP convenience and converting functions. */
  731. /**
  732. * \defgroup uipconvfunc uIP conversion functions
  733. * @{
  734. *
  735. * These functions can be used for converting between different data
  736. * formats used by uIP.
  737. */
  738. /**
  739. * Construct an IP address from four bytes.
  740. *
  741. * This function constructs an IP address of the type that uIP handles
  742. * internally from four bytes. The function is handy for specifying IP
  743. * addresses to use with e.g. the uip_connect() function.
  744. *
  745. * Example:
  746. \code
  747. uip_ipaddr_t ipaddr;
  748. struct uip_conn *c;
  749. uip_ipaddr(&ipaddr, 192,168,1,2);
  750. c = uip_connect(&ipaddr, HTONS(80));
  751. \endcode
  752. *
  753. * \param addr A pointer to a uip_ipaddr_t variable that will be
  754. * filled in with the IP address.
  755. *
  756. * \param addr0 The first octet of the IP address.
  757. * \param addr1 The second octet of the IP address.
  758. * \param addr2 The third octet of the IP address.
  759. * \param addr3 The forth octet of the IP address.
  760. *
  761. * \hideinitializer
  762. */
  763. #define uip_ipaddr(addr, addr0,addr1,addr2,addr3) do { \
  764. ((u16_t *)(addr))[0] = HTONS(((addr0) << 8) | (addr1)); \
  765. ((u16_t *)(addr))[1] = HTONS(((addr2) << 8) | (addr3)); \
  766. } while(0)
  767. /**
  768. * Construct an IPv6 address from eight 16-bit words.
  769. *
  770. * This function constructs an IPv6 address.
  771. *
  772. * \hideinitializer
  773. */
  774. #define uip_ip6addr(addr, addr0,addr1,addr2,addr3,addr4,addr5,addr6,addr7) do { \
  775. ((u16_t *)(addr))[0] = HTONS((addr0)); \
  776. ((u16_t *)(addr))[1] = HTONS((addr1)); \
  777. ((u16_t *)(addr))[2] = HTONS((addr2)); \
  778. ((u16_t *)(addr))[3] = HTONS((addr3)); \
  779. ((u16_t *)(addr))[4] = HTONS((addr4)); \
  780. ((u16_t *)(addr))[5] = HTONS((addr5)); \
  781. ((u16_t *)(addr))[6] = HTONS((addr6)); \
  782. ((u16_t *)(addr))[7] = HTONS((addr7)); \
  783. } while(0)
  784. /**
  785. * Copy an IP address to another IP address.
  786. *
  787. * Copies an IP address from one place to another.
  788. *
  789. * Example:
  790. \code
  791. uip_ipaddr_t ipaddr1, ipaddr2;
  792. uip_ipaddr(&ipaddr1, 192,16,1,2);
  793. uip_ipaddr_copy(&ipaddr2, &ipaddr1);
  794. \endcode
  795. *
  796. * \param dest The destination for the copy.
  797. * \param src The source from where to copy.
  798. *
  799. * \hideinitializer
  800. */
  801. #if !UIP_CONF_IPV6
  802. #define uip_ipaddr_copy(dest, src) do { \
  803. ((u16_t *)dest)[0] = ((u16_t *)src)[0]; \
  804. ((u16_t *)dest)[1] = ((u16_t *)src)[1]; \
  805. } while(0)
  806. #else /* !UIP_CONF_IPV6 */
  807. #define uip_ipaddr_copy(dest, src) memcpy(dest, src, sizeof(uip_ip6addr_t))
  808. #endif /* !UIP_CONF_IPV6 */
  809. /**
  810. * Compare two IP addresses
  811. *
  812. * Compares two IP addresses.
  813. *
  814. * Example:
  815. \code
  816. uip_ipaddr_t ipaddr1, ipaddr2;
  817. uip_ipaddr(&ipaddr1, 192,16,1,2);
  818. if(uip_ipaddr_cmp(&ipaddr2, &ipaddr1)) {
  819. printf("They are the same");
  820. }
  821. \endcode
  822. *
  823. * \param addr1 The first IP address.
  824. * \param addr2 The second IP address.
  825. *
  826. * \hideinitializer
  827. */
  828. #if !UIP_CONF_IPV6
  829. #define uip_ipaddr_cmp(addr1, addr2) (((u16_t *)addr1)[0] == ((u16_t *)addr2)[0] && \
  830. ((u16_t *)addr1)[1] == ((u16_t *)addr2)[1])
  831. #else /* !UIP_CONF_IPV6 */
  832. #define uip_ipaddr_cmp(addr1, addr2) (memcmp(addr1, addr2, sizeof(uip_ip6addr_t)) == 0)
  833. #endif /* !UIP_CONF_IPV6 */
  834. /**
  835. * Compare two IP addresses with netmasks
  836. *
  837. * Compares two IP addresses with netmasks. The masks are used to mask
  838. * out the bits that are to be compared.
  839. *
  840. * Example:
  841. \code
  842. uip_ipaddr_t ipaddr1, ipaddr2, mask;
  843. uip_ipaddr(&mask, 255,255,255,0);
  844. uip_ipaddr(&ipaddr1, 192,16,1,2);
  845. uip_ipaddr(&ipaddr2, 192,16,1,3);
  846. if(uip_ipaddr_maskcmp(&ipaddr1, &ipaddr2, &mask)) {
  847. printf("They are the same");
  848. }
  849. \endcode
  850. *
  851. * \param addr1 The first IP address.
  852. * \param addr2 The second IP address.
  853. * \param mask The netmask.
  854. *
  855. * \hideinitializer
  856. */
  857. #define uip_ipaddr_maskcmp(addr1, addr2, mask) \
  858. (((((u16_t *)addr1)[0] & ((u16_t *)mask)[0]) == \
  859. (((u16_t *)addr2)[0] & ((u16_t *)mask)[0])) && \
  860. ((((u16_t *)addr1)[1] & ((u16_t *)mask)[1]) == \
  861. (((u16_t *)addr2)[1] & ((u16_t *)mask)[1])))
  862. /**
  863. * Mask out the network part of an IP address.
  864. *
  865. * Masks out the network part of an IP address, given the address and
  866. * the netmask.
  867. *
  868. * Example:
  869. \code
  870. uip_ipaddr_t ipaddr1, ipaddr2, netmask;
  871. uip_ipaddr(&ipaddr1, 192,16,1,2);
  872. uip_ipaddr(&netmask, 255,255,255,0);
  873. uip_ipaddr_mask(&ipaddr2, &ipaddr1, &netmask);
  874. \endcode
  875. *
  876. * In the example above, the variable "ipaddr2" will contain the IP
  877. * address 192.168.1.0.
  878. *
  879. * \param dest Where the result is to be placed.
  880. * \param src The IP address.
  881. * \param mask The netmask.
  882. *
  883. * \hideinitializer
  884. */
  885. #define uip_ipaddr_mask(dest, src, mask) do { \
  886. ((u16_t *)dest)[0] = ((u16_t *)src)[0] & ((u16_t *)mask)[0]; \
  887. ((u16_t *)dest)[1] = ((u16_t *)src)[1] & ((u16_t *)mask)[1]; \
  888. } while(0)
  889. /**
  890. * Pick the first octet of an IP address.
  891. *
  892. * Picks out the first octet of an IP address.
  893. *
  894. * Example:
  895. \code
  896. uip_ipaddr_t ipaddr;
  897. u8_t octet;
  898. uip_ipaddr(&ipaddr, 1,2,3,4);
  899. octet = uip_ipaddr1(&ipaddr);
  900. \endcode
  901. *
  902. * In the example above, the variable "octet" will contain the value 1.
  903. *
  904. * \hideinitializer
  905. */
  906. #define uip_ipaddr1(addr) (uip_htons(((u16_t *)(addr))[0]) >> 8)
  907. /**
  908. * Pick the second octet of an IP address.
  909. *
  910. * Picks out the second octet of an IP address.
  911. *
  912. * Example:
  913. \code
  914. uip_ipaddr_t ipaddr;
  915. u8_t octet;
  916. uip_ipaddr(&ipaddr, 1,2,3,4);
  917. octet = uip_ipaddr2(&ipaddr);
  918. \endcode
  919. *
  920. * In the example above, the variable "octet" will contain the value 2.
  921. *
  922. * \hideinitializer
  923. */
  924. #define uip_ipaddr2(addr) (uip_htons(((u16_t *)(addr))[0]) & 0xff)
  925. /**
  926. * Pick the third octet of an IP address.
  927. *
  928. * Picks out the third octet of an IP address.
  929. *
  930. * Example:
  931. \code
  932. uip_ipaddr_t ipaddr;
  933. u8_t octet;
  934. uip_ipaddr(&ipaddr, 1,2,3,4);
  935. octet = uip_ipaddr3(&ipaddr);
  936. \endcode
  937. *
  938. * In the example above, the variable "octet" will contain the value 3.
  939. *
  940. * \hideinitializer
  941. */
  942. #define uip_ipaddr3(addr) (uip_htons(((u16_t *)(addr))[1]) >> 8)
  943. /**
  944. * Pick the fourth octet of an IP address.
  945. *
  946. * Picks out the fourth octet of an IP address.
  947. *
  948. * Example:
  949. \code
  950. uip_ipaddr_t ipaddr;
  951. u8_t octet;
  952. uip_ipaddr(&ipaddr, 1,2,3,4);
  953. octet = uip_ipaddr4(&ipaddr);
  954. \endcode
  955. *
  956. * In the example above, the variable "octet" will contain the value 4.
  957. *
  958. * \hideinitializer
  959. */
  960. #define uip_ipaddr4(addr) (uip_htons(((u16_t *)(addr))[1]) & 0xff)
  961. /**
  962. * Convert 16-bit quantity from host byte order to network byte order.
  963. *
  964. * This macro is primarily used for converting constants from host
  965. * byte order to network byte order. For converting variables to
  966. * network byte order, use the uip_htons() function instead.
  967. *
  968. * \hideinitializer
  969. */
  970. #ifndef HTONS
  971. # if UIP_BYTE_ORDER == UIP_BIG_ENDIAN
  972. # define HTONS(n) (n)
  973. # else /* UIP_BYTE_ORDER == UIP_BIG_ENDIAN */
  974. # define HTONS(n) (u16_t)((((u16_t) (n)) << 8) | (((u16_t) (n)) >> 8))
  975. # endif /* UIP_BYTE_ORDER == UIP_BIG_ENDIAN */
  976. #else
  977. #error "HTONS already defined!"
  978. #endif /* HTONS */
  979. /**
  980. * Convert 16-bit quantity from host byte order to network byte order.
  981. *
  982. * This function is primarily used for converting variables from host
  983. * byte order to network byte order. For converting constants to
  984. * network byte order, use the HTONS() macro instead.
  985. */
  986. #ifndef uip_htons
  987. u16_t uip_htons(u16_t val);
  988. #endif /* uip_htons */
  989. #ifndef ntohs
  990. #define ntohs uip_htons
  991. #endif
  992. /** @} */
  993. /**
  994. * Pointer to the application data in the packet buffer.
  995. *
  996. * This pointer points to the application data when the application is
  997. * called. If the application wishes to send data, the application may
  998. * use this space to write the data into before calling uip_send().
  999. */
  1000. extern volatile u8_t *uip_appdata;
  1001. #if UIP_URGDATA > 0
  1002. /* u8_t *uip_urgdata:
  1003. *
  1004. * This pointer points to any urgent data that has been received. Only
  1005. * present if compiled with support for urgent data (UIP_URGDATA).
  1006. */
  1007. extern void *uip_urgdata;
  1008. #endif /* UIP_URGDATA > 0 */
  1009. /**
  1010. * \defgroup uipdrivervars Variables used in uIP device drivers
  1011. * @{
  1012. *
  1013. * uIP has a few global variables that are used in device drivers for
  1014. * uIP.
  1015. */
  1016. /**
  1017. * The length of the packet in the uip_buf buffer.
  1018. *
  1019. * The global variable uip_len holds the length of the packet in the
  1020. * uip_buf buffer.
  1021. *
  1022. * When the network device driver calls the uIP input function,
  1023. * uip_len should be set to the length of the packet in the uip_buf
  1024. * buffer.
  1025. *
  1026. * When sending packets, the device driver should use the contents of
  1027. * the uip_len variable to determine the length of the outgoing
  1028. * packet.
  1029. *
  1030. */
  1031. extern u16_t uip_len;
  1032. /** @} */
  1033. #if UIP_URGDATA > 0
  1034. extern u16_t uip_urglen, uip_surglen;
  1035. #endif /* UIP_URGDATA > 0 */
  1036. /**
  1037. * Representation of a uIP TCP connection.
  1038. *
  1039. * The uip_conn structure is used for identifying a connection. All
  1040. * but one field in the structure are to be considered read-only by an
  1041. * application. The only exception is the appstate field whos purpose
  1042. * is to let the application store application-specific state (e.g.,
  1043. * file pointers) for the connection. The type of this field is
  1044. * configured in the "uipopt.h" header file.
  1045. */
  1046. struct uip_conn {
  1047. uip_ipaddr_t ripaddr; /**< The IP address of the remote host. */
  1048. u16_t lport; /**< The local TCP port, in network byte order. */
  1049. u16_t rport; /**< The local remote TCP port, in network byte
  1050. order. */
  1051. u8_t rcv_nxt[4]; /**< The sequence number that we expect to
  1052. receive next. */
  1053. u8_t snd_nxt[4]; /**< The sequence number that was last sent by
  1054. us. */
  1055. u16_t len; /**< Length of the data that was previously sent. */
  1056. u16_t mss; /**< Current maximum segment size for the
  1057. connection. */
  1058. u16_t initialmss; /**< Initial maximum segment size for the
  1059. connection. */
  1060. u8_t sa; /**< Retransmission time-out calculation state
  1061. variable. */
  1062. u8_t sv; /**< Retransmission time-out calculation state
  1063. variable. */
  1064. u8_t rto; /**< Retransmission time-out. */
  1065. u8_t tcpstateflags; /**< TCP state and flags. */
  1066. u8_t timer; /**< The retransmission timer. */
  1067. u8_t nrtx; /**< The number of retransmissions for the last
  1068. segment sent. */
  1069. /** The application state. */
  1070. uip_tcp_appstate_t appstate;
  1071. };
  1072. /**
  1073. * Pointer to the current TCP connection.
  1074. *
  1075. * The uip_conn pointer can be used to access the current TCP
  1076. * connection.
  1077. */
  1078. extern struct uip_conn *uip_conn;
  1079. /* The array containing all uIP connections. */
  1080. extern struct uip_conn uip_conns[UIP_CONNS];
  1081. /**
  1082. * \addtogroup uiparch
  1083. * @{
  1084. */
  1085. /**
  1086. * 4-byte array used for the 32-bit sequence number calculations.
  1087. */
  1088. extern u8_t uip_acc32[4];
  1089. /** @} */
  1090. #if UIP_UDP
  1091. /**
  1092. * Representation of a uIP UDP connection.
  1093. */
  1094. struct uip_udp_conn {
  1095. uip_ipaddr_t ripaddr; /**< The IP address of the remote peer. */
  1096. u16_t lport; /**< The local port number in network byte order. */
  1097. u16_t rport; /**< The remote port number in network byte order. */
  1098. u8_t ttl; /**< Default time-to-live. */
  1099. /** The application state. */
  1100. uip_udp_appstate_t appstate;
  1101. };
  1102. /**
  1103. * The current UDP connection.
  1104. */
  1105. extern struct uip_udp_conn *uip_udp_conn;
  1106. extern struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS];
  1107. #endif /* UIP_UDP */
  1108. /**
  1109. * The structure holding the TCP/IP statistics that are gathered if
  1110. * UIP_STATISTICS is set to 1.
  1111. *
  1112. */
  1113. struct uip_stats {
  1114. struct {
  1115. uip_stats_t drop; /**< Number of dropped packets at the IP
  1116. layer. */
  1117. uip_stats_t recv; /**< Number of received packets at the IP
  1118. layer. */
  1119. uip_stats_t sent; /**< Number of sent packets at the IP
  1120. layer. */
  1121. uip_stats_t vhlerr; /**< Number of packets dropped due to wrong
  1122. IP version or header length. */
  1123. uip_stats_t hblenerr; /**< Number of packets dropped due to wrong
  1124. IP length, high byte. */
  1125. uip_stats_t lblenerr; /**< Number of packets dropped due to wrong
  1126. IP length, low byte. */
  1127. uip_stats_t fragerr; /**< Number of packets dropped since they
  1128. were IP fragments. */
  1129. uip_stats_t chkerr; /**< Number of packets dropped due to IP
  1130. checksum errors. */
  1131. uip_stats_t protoerr; /**< Number of packets dropped since they
  1132. were neither ICMP, UDP nor TCP. */
  1133. } ip; /**< IP statistics. */
  1134. struct {
  1135. uip_stats_t drop; /**< Number of dropped ICMP packets. */
  1136. uip_stats_t recv; /**< Number of received ICMP packets. */
  1137. uip_stats_t sent; /**< Number of sent ICMP packets. */
  1138. uip_stats_t typeerr; /**< Number of ICMP packets with a wrong
  1139. type. */
  1140. } icmp; /**< ICMP statistics. */
  1141. struct {
  1142. uip_stats_t drop; /**< Number of dropped TCP segments. */
  1143. uip_stats_t recv; /**< Number of recived TCP segments. */
  1144. uip_stats_t sent; /**< Number of sent TCP segments. */
  1145. uip_stats_t chkerr; /**< Number of TCP segments with a bad
  1146. checksum. */
  1147. uip_stats_t ackerr; /**< Number of TCP segments with a bad ACK
  1148. number. */
  1149. uip_stats_t rst; /**< Number of recevied TCP RST (reset) segments. */
  1150. uip_stats_t rexmit; /**< Number of retransmitted TCP segments. */
  1151. uip_stats_t syndrop; /**< Number of dropped SYNs due to too few
  1152. connections was avaliable. */
  1153. uip_stats_t synrst; /**< Number of SYNs for closed ports,
  1154. triggering a RST. */
  1155. } tcp; /**< TCP statistics. */
  1156. #if UIP_UDP
  1157. struct {
  1158. uip_stats_t drop; /**< Number of dropped UDP segments. */
  1159. uip_stats_t recv; /**< Number of recived UDP segments. */
  1160. uip_stats_t sent; /**< Number of sent UDP segments. */
  1161. uip_stats_t chkerr; /**< Number of UDP segments with a bad
  1162. checksum. */
  1163. } udp; /**< UDP statistics. */
  1164. #endif /* UIP_UDP */
  1165. };
  1166. /**
  1167. * The uIP TCP/IP statistics.
  1168. *
  1169. * This is the variable in which the uIP TCP/IP statistics are gathered.
  1170. */
  1171. extern struct uip_stats uip_stat;
  1172. /*---------------------------------------------------------------------------*/
  1173. /* All the stuff below this point is internal to uIP and should not be
  1174. * used directly by an application or by a device driver.
  1175. */
  1176. /*---------------------------------------------------------------------------*/
  1177. /* u8_t uip_flags:
  1178. *
  1179. * When the application is called, uip_flags will contain the flags
  1180. * that are defined in this file. Please read below for more
  1181. * infomation.
  1182. */
  1183. extern u8_t uip_flags;
  1184. /* The following flags may be set in the global variable uip_flags
  1185. before calling the application callback. The UIP_ACKDATA,
  1186. UIP_NEWDATA, and UIP_CLOSE flags may both be set at the same time,
  1187. whereas the others are mutualy exclusive. Note that these flags
  1188. should *NOT* be accessed directly, but only through the uIP
  1189. functions/macros. */
  1190. #define UIP_ACKDATA 1 /* Signifies that the outstanding data was
  1191. acked and the application should send
  1192. out new data instead of retransmitting
  1193. the last data. */
  1194. #define UIP_NEWDATA 2 /* Flags the fact that the peer has sent
  1195. us new data. */
  1196. #define UIP_REXMIT 4 /* Tells the application to retransmit the
  1197. data that was last sent. */
  1198. #define UIP_POLL 8 /* Used for polling the application, to
  1199. check if the application has data that
  1200. it wants to send. */
  1201. #define UIP_CLOSE 16 /* The remote host has closed the
  1202. connection, thus the connection has
  1203. gone away. Or the application signals
  1204. that it wants to close the
  1205. connection. */
  1206. #define UIP_ABORT 32 /* The remote host has aborted the
  1207. connection, thus the connection has
  1208. gone away. Or the application signals
  1209. that it wants to abort the
  1210. connection. */
  1211. #define UIP_CONNECTED 64 /* We have got a connection from a remote
  1212. host and have set up a new connection
  1213. for it, or an active connection has
  1214. been successfully established. */
  1215. #define UIP_TIMEDOUT 128 /* The connection has been aborted due to
  1216. too many retransmissions. */
  1217. /* uip_process(flag):
  1218. *
  1219. * The actual uIP function which does all the work.
  1220. */
  1221. void uip_process(u8_t flag);
  1222. /* The following flags are passed as an argument to the uip_process()
  1223. function. They are used to distinguish between the two cases where
  1224. uip_process() is called. It can be called either because we have
  1225. incoming data that should be processed, or because the periodic
  1226. timer has fired. These values are never used directly, but only in
  1227. the macrose defined in this file. */
  1228. #define UIP_DATA 1 /* Tells uIP that there is incoming
  1229. data in the uip_buf buffer. The
  1230. length of the data is stored in the
  1231. global variable uip_len. */
  1232. #define UIP_TIMER 2 /* Tells uIP that the periodic timer
  1233. has fired. */
  1234. #define UIP_POLL_REQUEST 3 /* Tells uIP that a connection should
  1235. be polled. */
  1236. #define UIP_UDP_SEND_CONN 4 /* Tells uIP that a UDP datagram
  1237. should be constructed in the
  1238. uip_buf buffer. */
  1239. #if UIP_UDP
  1240. #define UIP_UDP_TIMER 5
  1241. #endif /* UIP_UDP */
  1242. /* The TCP states used in the uip_conn->tcpstateflags. */
  1243. #define UIP_CLOSED 0
  1244. #define UIP_SYN_RCVD 1
  1245. #define UIP_SYN_SENT 2
  1246. #define UIP_ESTABLISHED 3
  1247. #define UIP_FIN_WAIT_1 4
  1248. #define UIP_FIN_WAIT_2 5
  1249. #define UIP_CLOSING 6
  1250. #define UIP_TIME_WAIT 7
  1251. #define UIP_LAST_ACK 8
  1252. #define UIP_TS_MASK 15
  1253. #define UIP_STOPPED 16
  1254. /* The TCP and IP headers. */
  1255. struct uip_tcpip_hdr {
  1256. #if UIP_CONF_IPV6
  1257. /* IPv6 header. */
  1258. u8_t vtc,
  1259. tcflow;
  1260. u16_t flow;
  1261. u8_t len[2];
  1262. u8_t proto, ttl;
  1263. uip_ip6addr_t srcipaddr, destipaddr;
  1264. #else /* UIP_CONF_IPV6 */
  1265. /* IPv4 header. */
  1266. u8_t vhl,
  1267. tos,
  1268. len[2],
  1269. ipid[2],
  1270. ipoffset[2],
  1271. ttl,
  1272. proto;
  1273. u16_t ipchksum;
  1274. u16_t srcipaddr[2],
  1275. destipaddr[2];
  1276. #endif /* UIP_CONF_IPV6 */
  1277. /* TCP header. */
  1278. u16_t srcport,
  1279. destport;
  1280. u8_t seqno[4],
  1281. ackno[4],
  1282. tcpoffset,
  1283. flags,
  1284. wnd[2];
  1285. u16_t tcpchksum;
  1286. u8_t urgp[2];
  1287. u8_t optdata[4];
  1288. };
  1289. /* The ICMP and IP headers. */
  1290. struct uip_icmpip_hdr {
  1291. #if UIP_CONF_IPV6
  1292. /* IPv6 header. */
  1293. u8_t vtc,
  1294. tcf;
  1295. u16_t flow;
  1296. u8_t len[2];
  1297. u8_t proto, ttl;
  1298. uip_ip6addr_t srcipaddr, destipaddr;
  1299. #else /* UIP_CONF_IPV6 */
  1300. /* IPv4 header. */
  1301. u8_t vhl,
  1302. tos,
  1303. len[2],
  1304. ipid[2],
  1305. ipoffset[2],
  1306. ttl,
  1307. proto;
  1308. u16_t ipchksum;
  1309. u16_t srcipaddr[2],
  1310. destipaddr[2];
  1311. #endif /* UIP_CONF_IPV6 */
  1312. /* ICMP (echo) header. */
  1313. u8_t type, icode;
  1314. u16_t icmpchksum;
  1315. #if !UIP_CONF_IPV6
  1316. u16_t id, seqno;
  1317. #else /* !UIP_CONF_IPV6 */
  1318. u8_t flags, reserved1, reserved2, reserved3;
  1319. u8_t icmp6data[16];
  1320. u8_t options[1];
  1321. #endif /* !UIP_CONF_IPV6 */
  1322. };
  1323. /* The UDP and IP headers. */
  1324. struct uip_udpip_hdr {
  1325. #if UIP_CONF_IPV6
  1326. /* IPv6 header. */
  1327. u8_t vtc,
  1328. tcf;
  1329. u16_t flow;
  1330. u8_t len[2];
  1331. u8_t proto, ttl;
  1332. uip_ip6addr_t srcipaddr, destipaddr;
  1333. #else /* UIP_CONF_IPV6 */
  1334. /* IP header. */
  1335. u8_t vhl,
  1336. tos,
  1337. len[2],
  1338. ipid[2],
  1339. ipoffset[2],
  1340. ttl,
  1341. proto;
  1342. u16_t ipchksum;
  1343. u16_t srcipaddr[2],
  1344. destipaddr[2];
  1345. #endif /* UIP_CONF_IPV6 */
  1346. /* UDP header. */
  1347. u16_t srcport,
  1348. destport;
  1349. u16_t udplen;
  1350. u16_t udpchksum;
  1351. };
  1352. /**
  1353. * The buffer size available for user data in the \ref uip_buf buffer.
  1354. *
  1355. * This macro holds the available size for user data in the \ref
  1356. * uip_buf buffer. The macro is intended to be used for checking
  1357. * bounds of available user data.
  1358. *
  1359. * Example:
  1360. \code
  1361. snprintf(uip_appdata, UIP_APPDATA_SIZE, "%u\n", i);
  1362. \endcode
  1363. *
  1364. * \hideinitializer
  1365. */
  1366. #define UIP_APPDATA_SIZE (UIP_BUFSIZE - UIP_LLH_LEN - UIP_TCPIP_HLEN)
  1367. #define UIP_PROTO_ICMP 1
  1368. #define UIP_PROTO_TCP 6
  1369. #define UIP_PROTO_UDP 17
  1370. #define UIP_PROTO_ICMP6 58
  1371. /* Header sizes. */
  1372. #if UIP_CONF_IPV6
  1373. #define UIP_IPH_LEN 40
  1374. #else /* UIP_CONF_IPV6 */
  1375. #define UIP_IPH_LEN 20 /* Size of IP header */
  1376. #endif /* UIP_CONF_IPV6 */
  1377. #define UIP_UDPH_LEN 8 /* Size of UDP header */
  1378. #define UIP_TCPH_LEN 20 /* Size of TCP header */
  1379. #define UIP_IPUDPH_LEN (UIP_UDPH_LEN + UIP_IPH_LEN) /* Size of IP +
  1380. UDP
  1381. header */
  1382. #define UIP_IPTCPH_LEN (UIP_TCPH_LEN + UIP_IPH_LEN) /* Size of IP +
  1383. TCP
  1384. header */
  1385. #define UIP_TCPIP_HLEN UIP_IPTCPH_LEN
  1386. #if UIP_FIXEDADDR
  1387. extern const uip_ipaddr_t uip_hostaddr, uip_netmask, uip_draddr;
  1388. #else /* UIP_FIXEDADDR */
  1389. extern uip_ipaddr_t uip_hostaddr, uip_netmask, uip_draddr;
  1390. #endif /* UIP_FIXEDADDR */
  1391. /**
  1392. * Representation of a 48-bit Ethernet address.
  1393. */
  1394. struct uip_eth_addr {
  1395. u8_t addr[6];
  1396. };
  1397. /**
  1398. * Calculate the Internet checksum over a buffer.
  1399. *
  1400. * The Internet checksum is the one's complement of the one's
  1401. * complement sum of all 16-bit words in the buffer.
  1402. *
  1403. * See RFC1071.
  1404. *
  1405. * \param buf A pointer to the buffer over which the checksum is to be
  1406. * computed.
  1407. *
  1408. * \param len The length of the buffer over which the checksum is to
  1409. * be computed.
  1410. *
  1411. * \return The Internet checksum of the buffer.
  1412. */
  1413. u16_t uip_chksum(u16_t *buf, u16_t len);
  1414. /**
  1415. * Calculate the IP header checksum of the packet header in uip_buf.
  1416. *
  1417. * The IP header checksum is the Internet checksum of the 20 bytes of
  1418. * the IP header.
  1419. *
  1420. * \return The IP header checksum of the IP header in the uip_buf
  1421. * buffer.
  1422. */
  1423. u16_t uip_ipchksum(void);
  1424. /**
  1425. * Calculate the TCP checksum of the packet in uip_buf and uip_appdata.
  1426. *
  1427. * The TCP checksum is the Internet checksum of data contents of the
  1428. * TCP segment, and a pseudo-header as defined in RFC793.
  1429. *
  1430. * \return The TCP checksum of the TCP segment in uip_buf and pointed
  1431. * to by uip_appdata.
  1432. */
  1433. u16_t uip_tcpchksum(void);
  1434. /**
  1435. * Calculate the UDP checksum of the packet in uip_buf and uip_appdata.
  1436. *
  1437. * The UDP checksum is the Internet checksum of data contents of the
  1438. * UDP segment, and a pseudo-header as defined in RFC768.
  1439. *
  1440. * \return The UDP checksum of the UDP segment in uip_buf and pointed
  1441. * to by uip_appdata.
  1442. */
  1443. u16_t uip_udpchksum(void);
  1444. #endif /* __UIP_H__ */
  1445. /** @} */