Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uip.h 58KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130
  1. /**
  2. * \addtogroup uip
  3. * @{
  4. */
  5. /**
  6. * \file
  7. * Header file for the uIP TCP/IP stack.
  8. * \author Adam Dunkels <[email protected]>
  9. * \author Julien Abeille <[email protected]> (IPv6 related code)
  10. * \author Mathilde Durvy <[email protected]> (IPv6 related code)
  11. *
  12. * The uIP TCP/IP stack header file contains definitions for a number
  13. * of C macros that are used by uIP programs as well as internal uIP
  14. * structures, TCP/IP header structures and function declarations.
  15. *
  16. */
  17. /*
  18. * Copyright (c) 2001-2003, Adam Dunkels.
  19. * All rights reserved.
  20. *
  21. * Redistribution and use in source and binary forms, with or without
  22. * modification, are permitted provided that the following conditions
  23. * are met:
  24. * 1. Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * 2. Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in the
  28. * documentation and/or other materials provided with the distribution.
  29. * 3. The name of the author may not be used to endorse or promote
  30. * products derived from this software without specific prior
  31. * written permission.
  32. *
  33. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  34. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  35. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  37. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  38. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  39. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  40. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  41. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  42. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  43. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. *
  45. * This file is part of the uIP TCP/IP stack.
  46. *
  47. * $Id: uip.h,v 1.24 2009/04/06 13:18:50 nvt-se Exp $
  48. *
  49. */
  50. #ifndef __UIP_H__
  51. #define __UIP_H__
  52. #include "uipopt.h"
  53. /**
  54. * Representation of an IP address.
  55. *
  56. */
  57. #if UIP_CONF_IPV6
  58. typedef union uip_ip6addr_t {
  59. u8_t u8[16]; /* Initialiser, must come first!!! */
  60. u16_t u16[8];
  61. } uip_ip6addr_t;
  62. typedef uip_ip6addr_t uip_ipaddr_t;
  63. #else /* UIP_CONF_IPV6 */
  64. typedef union uip_ip4addr_t {
  65. u8_t u8[4]; /* Initialiser, must come first!!! */
  66. u16_t u16[2];
  67. #if 0
  68. u32_t u32;
  69. #endif
  70. } uip_ip4addr_t;
  71. typedef uip_ip4addr_t uip_ipaddr_t;
  72. #endif /* UIP_CONF_IPV6 */
  73. /*---------------------------------------------------------------------------*/
  74. /** \brief 16 bit 802.15.4 address */
  75. struct uip_802154_shortaddr {
  76. u8_t addr[2];
  77. };
  78. /** \brief 64 bit 802.15.4 address */
  79. struct uip_802154_longaddr {
  80. u8_t addr[8];
  81. };
  82. /** \brief 802.11 address */
  83. struct uip_80211_addr {
  84. u8_t addr[6];
  85. };
  86. /** \brief 802.3 address */
  87. struct uip_eth_addr {
  88. u8_t addr[6];
  89. };
  90. #if UIP_CONF_LL_802154
  91. /** \brief 802.15.4 address */
  92. typedef struct uip_802154_longaddr uip_lladdr_t;
  93. #define UIP_802154_SHORTADDR_LEN 2
  94. #define UIP_802154_LONGADDR_LEN 8
  95. #define UIP_LLADDR_LEN UIP_802154_LONGADDR_LEN
  96. #else /*UIP_CONF_LL_802154*/
  97. #if UIP_CONF_LL_80211
  98. /** \brief 802.11 address */
  99. typedef struct uip_80211_addr uip_lladdr_t;
  100. #define UIP_LLADDR_LEN 6
  101. #else /*UIP_CONF_LL_80211*/
  102. /** \brief Ethernet address */
  103. typedef struct uip_eth_addr uip_lladdr_t;
  104. #define UIP_LLADDR_LEN 6
  105. #endif /*UIP_CONF_LL_80211*/
  106. #endif /*UIP_CONF_LL_802154*/
  107. /*---------------------------------------------------------------------------*/
  108. /* First, the functions that should be called from the
  109. * system. Initialization, the periodic timer, and incoming packets are
  110. * handled by the following three functions.
  111. */
  112. /**
  113. * \defgroup uipconffunc uIP configuration functions
  114. * @{
  115. *
  116. * The uIP configuration functions are used for setting run-time
  117. * parameters in uIP such as IP addresses.
  118. */
  119. /**
  120. * Set the IP address of this host.
  121. *
  122. * The IP address is represented as a 4-byte array where the first
  123. * octet of the IP address is put in the first member of the 4-byte
  124. * array.
  125. *
  126. * Example:
  127. \code
  128. uip_ipaddr_t addr;
  129. uip_ipaddr(&addr, 192,168,1,2);
  130. uip_sethostaddr(&addr);
  131. \endcode
  132. * \param addr A pointer to an IP address of type uip_ipaddr_t;
  133. *
  134. * \sa uip_ipaddr()
  135. *
  136. * \hideinitializer
  137. */
  138. #define uip_sethostaddr(addr) uip_ipaddr_copy(&uip_hostaddr, (addr))
  139. /**
  140. * Get the IP address of this host.
  141. *
  142. * The IP address is represented as a 4-byte array where the first
  143. * octet of the IP address is put in the first member of the 4-byte
  144. * array.
  145. *
  146. * Example:
  147. \code
  148. uip_ipaddr_t hostaddr;
  149. uip_gethostaddr(&hostaddr);
  150. \endcode
  151. * \param addr A pointer to a uip_ipaddr_t variable that will be
  152. * filled in with the currently configured IP address.
  153. *
  154. * \hideinitializer
  155. */
  156. #define uip_gethostaddr(addr) uip_ipaddr_copy((addr), &uip_hostaddr)
  157. /**
  158. * Set the default router's IP address.
  159. *
  160. * \param addr A pointer to a uip_ipaddr_t variable containing the IP
  161. * address of the default router.
  162. *
  163. * \sa uip_ipaddr()
  164. *
  165. * \hideinitializer
  166. */
  167. #define uip_setdraddr(addr) uip_ipaddr_copy(&uip_draddr, (addr))
  168. /**
  169. * Set the netmask.
  170. *
  171. * \param addr A pointer to a uip_ipaddr_t variable containing the IP
  172. * address of the netmask.
  173. *
  174. * \sa uip_ipaddr()
  175. *
  176. * \hideinitializer
  177. */
  178. #define uip_setnetmask(addr) uip_ipaddr_copy(&uip_netmask, (addr))
  179. /**
  180. * Get the default router's IP address.
  181. *
  182. * \param addr A pointer to a uip_ipaddr_t variable that will be
  183. * filled in with the IP address of the default router.
  184. *
  185. * \hideinitializer
  186. */
  187. #define uip_getdraddr(addr) uip_ipaddr_copy((addr), &uip_draddr)
  188. /**
  189. * Get the netmask.
  190. *
  191. * \param addr A pointer to a uip_ipaddr_t variable that will be
  192. * filled in with the value of the netmask.
  193. *
  194. * \hideinitializer
  195. */
  196. #define uip_getnetmask(addr) uip_ipaddr_copy((addr), &uip_netmask)
  197. /** @} */
  198. /**
  199. * \defgroup uipinit uIP initialization functions
  200. * @{
  201. *
  202. * The uIP initialization functions are used for booting uIP.
  203. */
  204. /**
  205. * uIP initialization function.
  206. *
  207. * This function should be called at boot up to initialize the uIP
  208. * TCP/IP stack.
  209. */
  210. void uip_init(void);
  211. /**
  212. * uIP initialization function.
  213. *
  214. * This function may be used at boot time to set the initial ip_id.
  215. */
  216. void uip_setipid(u16_t id);
  217. /** @} */
  218. /**
  219. * \defgroup uipdevfunc uIP device driver functions
  220. * @{
  221. *
  222. * These functions are used by a network device driver for interacting
  223. * with uIP.
  224. */
  225. /**
  226. * Process an incoming packet.
  227. *
  228. * This function should be called when the device driver has received
  229. * a packet from the network. The packet from the device driver must
  230. * be present in the uip_buf buffer, and the length of the packet
  231. * should be placed in the uip_len variable.
  232. *
  233. * When the function returns, there may be an outbound packet placed
  234. * in the uip_buf packet buffer. If so, the uip_len variable is set to
  235. * the length of the packet. If no packet is to be sent out, the
  236. * uip_len variable is set to 0.
  237. *
  238. * The usual way of calling the function is presented by the source
  239. * code below.
  240. \code
  241. uip_len = devicedriver_poll();
  242. if(uip_len > 0) {
  243. uip_input();
  244. if(uip_len > 0) {
  245. devicedriver_send();
  246. }
  247. }
  248. \endcode
  249. *
  250. * \note If you are writing a uIP device driver that needs ARP
  251. * (Address Resolution Protocol), e.g., when running uIP over
  252. * Ethernet, you will need to call the uIP ARP code before calling
  253. * this function:
  254. \code
  255. #define BUF ((struct uip_eth_hdr *)&uip_buf[0])
  256. uip_len = ethernet_devicedrver_poll();
  257. if(uip_len > 0) {
  258. if(BUF->type == HTONS(UIP_ETHTYPE_IP)) {
  259. uip_arp_ipin();
  260. uip_input();
  261. if(uip_len > 0) {
  262. uip_arp_out();
  263. ethernet_devicedriver_send();
  264. }
  265. } else if(BUF->type == HTONS(UIP_ETHTYPE_ARP)) {
  266. uip_arp_arpin();
  267. if(uip_len > 0) {
  268. ethernet_devicedriver_send();
  269. }
  270. }
  271. \endcode
  272. *
  273. * \hideinitializer
  274. */
  275. #define uip_input() uip_process(UIP_DATA)
  276. /**
  277. * Periodic processing for a connection identified by its number.
  278. *
  279. * This function does the necessary periodic processing (timers,
  280. * polling) for a uIP TCP connection, and should be called when the
  281. * periodic uIP timer goes off. It should be called for every
  282. * connection, regardless of whether they are open of closed.
  283. *
  284. * When the function returns, it may have an outbound packet waiting
  285. * for service in the uIP packet buffer, and if so the uip_len
  286. * variable is set to a value larger than zero. The device driver
  287. * should be called to send out the packet.
  288. *
  289. * The usual way of calling the function is through a for() loop like
  290. * this:
  291. \code
  292. for(i = 0; i < UIP_CONNS; ++i) {
  293. uip_periodic(i);
  294. if(uip_len > 0) {
  295. devicedriver_send();
  296. }
  297. }
  298. \endcode
  299. *
  300. * \note If you are writing a uIP device driver that needs ARP
  301. * (Address Resolution Protocol), e.g., when running uIP over
  302. * Ethernet, you will need to call the uip_arp_out() function before
  303. * calling the device driver:
  304. \code
  305. for(i = 0; i < UIP_CONNS; ++i) {
  306. uip_periodic(i);
  307. if(uip_len > 0) {
  308. uip_arp_out();
  309. ethernet_devicedriver_send();
  310. }
  311. }
  312. \endcode
  313. *
  314. * \param conn The number of the connection which is to be periodically polled.
  315. *
  316. * \hideinitializer
  317. */
  318. #if UIP_TCP
  319. #define uip_periodic(conn) do { uip_conn = &uip_conns[conn]; \
  320. uip_process(UIP_TIMER); } while (0)
  321. /**
  322. *
  323. *
  324. */
  325. #define uip_conn_active(conn) (uip_conns[conn].tcpstateflags != UIP_CLOSED)
  326. /**
  327. * Perform periodic processing for a connection identified by a pointer
  328. * to its structure.
  329. *
  330. * Same as uip_periodic() but takes a pointer to the actual uip_conn
  331. * struct instead of an integer as its argument. This function can be
  332. * used to force periodic processing of a specific connection.
  333. *
  334. * \param conn A pointer to the uip_conn struct for the connection to
  335. * be processed.
  336. *
  337. * \hideinitializer
  338. */
  339. #define uip_periodic_conn(conn) do { uip_conn = conn; \
  340. uip_process(UIP_TIMER); } while (0)
  341. /**
  342. * Request that a particular connection should be polled.
  343. *
  344. * Similar to uip_periodic_conn() but does not perform any timer
  345. * processing. The application is polled for new data.
  346. *
  347. * \param conn A pointer to the uip_conn struct for the connection to
  348. * be processed.
  349. *
  350. * \hideinitializer
  351. */
  352. #define uip_poll_conn(conn) do { uip_conn = conn; \
  353. uip_process(UIP_POLL_REQUEST); } while (0)
  354. #endif /* UIP_TCP */
  355. #if UIP_UDP
  356. /**
  357. * Periodic processing for a UDP connection identified by its number.
  358. *
  359. * This function is essentially the same as uip_periodic(), but for
  360. * UDP connections. It is called in a similar fashion as the
  361. * uip_periodic() function:
  362. \code
  363. for(i = 0; i < UIP_UDP_CONNS; i++) {
  364. uip_udp_periodic(i);
  365. if(uip_len > 0) {
  366. devicedriver_send();
  367. }
  368. }
  369. \endcode
  370. *
  371. * \note As for the uip_periodic() function, special care has to be
  372. * taken when using uIP together with ARP and Ethernet:
  373. \code
  374. for(i = 0; i < UIP_UDP_CONNS; i++) {
  375. uip_udp_periodic(i);
  376. if(uip_len > 0) {
  377. uip_arp_out();
  378. ethernet_devicedriver_send();
  379. }
  380. }
  381. \endcode
  382. *
  383. * \param conn The number of the UDP connection to be processed.
  384. *
  385. * \hideinitializer
  386. */
  387. #define uip_udp_periodic(conn) do { uip_udp_conn = &uip_udp_conns[conn]; \
  388. uip_process(UIP_UDP_TIMER); } while(0)
  389. /**
  390. * Periodic processing for a UDP connection identified by a pointer to
  391. * its structure.
  392. *
  393. * Same as uip_udp_periodic() but takes a pointer to the actual
  394. * uip_conn struct instead of an integer as its argument. This
  395. * function can be used to force periodic processing of a specific
  396. * connection.
  397. *
  398. * \param conn A pointer to the uip_udp_conn struct for the connection
  399. * to be processed.
  400. *
  401. * \hideinitializer
  402. */
  403. #define uip_udp_periodic_conn(conn) do { uip_udp_conn = conn; \
  404. uip_process(UIP_UDP_TIMER); } while(0)
  405. #endif /* UIP_UDP */
  406. /** \brief Abandon the reassembly of the current packet */
  407. void uip_reass_over(void);
  408. /**
  409. * The uIP packet buffer.
  410. *
  411. * The uip_buf array is used to hold incoming and outgoing
  412. * packets. The device driver should place incoming data into this
  413. * buffer. When sending data, the device driver should read the link
  414. * level headers and the TCP/IP headers from this buffer. The size of
  415. * the link level headers is configured by the UIP_LLH_LEN define.
  416. *
  417. * \note The application data need not be placed in this buffer, so
  418. * the device driver must read it from the place pointed to by the
  419. * uip_appdata pointer as illustrated by the following example:
  420. \code
  421. void
  422. devicedriver_send(void)
  423. {
  424. hwsend(&uip_buf[0], UIP_LLH_LEN);
  425. if(uip_len <= UIP_LLH_LEN + UIP_TCPIP_HLEN) {
  426. hwsend(&uip_buf[UIP_LLH_LEN], uip_len - UIP_LLH_LEN);
  427. } else {
  428. hwsend(&uip_buf[UIP_LLH_LEN], UIP_TCPIP_HLEN);
  429. hwsend(uip_appdata, uip_len - UIP_TCPIP_HLEN - UIP_LLH_LEN);
  430. }
  431. }
  432. \endcode
  433. */
  434. extern u8_t uip_buf[UIP_BUFSIZE+2];
  435. /** @} */
  436. /*---------------------------------------------------------------------------*/
  437. /* Functions that are used by the uIP application program. Opening and
  438. * closing connections, sending and receiving data, etc. is all
  439. * handled by the functions below.
  440. */
  441. /**
  442. * \defgroup uipappfunc uIP application functions
  443. * @{
  444. *
  445. * Functions used by an application running of top of uIP.
  446. */
  447. /**
  448. * Start listening to the specified port.
  449. *
  450. * \note Since this function expects the port number in network byte
  451. * order, a conversion using HTONS() or htons() is necessary.
  452. *
  453. \code
  454. uip_listen(HTONS(80));
  455. \endcode
  456. *
  457. * \param port A 16-bit port number in network byte order.
  458. */
  459. void uip_listen(u16_t port);
  460. /**
  461. * Stop listening to the specified port.
  462. *
  463. * \note Since this function expects the port number in network byte
  464. * order, a conversion using HTONS() or htons() is necessary.
  465. *
  466. \code
  467. uip_unlisten(HTONS(80));
  468. \endcode
  469. *
  470. * \param port A 16-bit port number in network byte order.
  471. */
  472. void uip_unlisten(u16_t port);
  473. /**
  474. * Connect to a remote host using TCP.
  475. *
  476. * This function is used to start a new connection to the specified
  477. * port on the specified host. It allocates a new connection identifier,
  478. * sets the connection to the SYN_SENT state and sets the
  479. * retransmission timer to 0. This will cause a TCP SYN segment to be
  480. * sent out the next time this connection is periodically processed,
  481. * which usually is done within 0.5 seconds after the call to
  482. * uip_connect().
  483. *
  484. * \note This function is available only if support for active open
  485. * has been configured by defining UIP_ACTIVE_OPEN to 1 in uipopt.h.
  486. *
  487. * \note Since this function requires the port number to be in network
  488. * byte order, a conversion using HTONS() or htons() is necessary.
  489. *
  490. \code
  491. uip_ipaddr_t ipaddr;
  492. uip_ipaddr(&ipaddr, 192,168,1,2);
  493. uip_connect(&ipaddr, HTONS(80));
  494. \endcode
  495. *
  496. * \param ripaddr The IP address of the remote host.
  497. *
  498. * \param port A 16-bit port number in network byte order.
  499. *
  500. * \return A pointer to the uIP connection identifier for the new connection,
  501. * or NULL if no connection could be allocated.
  502. *
  503. */
  504. struct uip_conn *uip_connect(uip_ipaddr_t *ripaddr, u16_t port);
  505. /**
  506. * \internal
  507. *
  508. * Check if a connection has outstanding (i.e., unacknowledged) data.
  509. *
  510. * \param conn A pointer to the uip_conn structure for the connection.
  511. *
  512. * \hideinitializer
  513. */
  514. #define uip_outstanding(conn) ((conn)->len)
  515. /**
  516. * Send data on the current connection.
  517. *
  518. * This function is used to send out a single segment of TCP
  519. * data. Only applications that have been invoked by uIP for event
  520. * processing can send data.
  521. *
  522. * The amount of data that actually is sent out after a call to this
  523. * function is determined by the maximum amount of data TCP allows. uIP
  524. * will automatically crop the data so that only the appropriate
  525. * amount of data is sent. The function uip_mss() can be used to query
  526. * uIP for the amount of data that actually will be sent.
  527. *
  528. * \note This function does not guarantee that the sent data will
  529. * arrive at the destination. If the data is lost in the network, the
  530. * application will be invoked with the uip_rexmit() event being
  531. * set. The application will then have to resend the data using this
  532. * function.
  533. *
  534. * \param data A pointer to the data which is to be sent.
  535. *
  536. * \param len The maximum amount of data bytes to be sent.
  537. *
  538. * \hideinitializer
  539. */
  540. void uip_send(const void *data, int len);
  541. /**
  542. * The length of any incoming data that is currently available (if available)
  543. * in the uip_appdata buffer.
  544. *
  545. * The test function uip_data() must first be used to check if there
  546. * is any data available at all.
  547. *
  548. * \hideinitializer
  549. */
  550. /*void uip_datalen(void);*/
  551. #define uip_datalen() uip_len
  552. /**
  553. * The length of any out-of-band data (urgent data) that has arrived
  554. * on the connection.
  555. *
  556. * \note The configuration parameter UIP_URGDATA must be set for this
  557. * function to be enabled.
  558. *
  559. * \hideinitializer
  560. */
  561. #define uip_urgdatalen() uip_urglen
  562. /**
  563. * Close the current connection.
  564. *
  565. * This function will close the current connection in a nice way.
  566. *
  567. * \hideinitializer
  568. */
  569. #define uip_close() (uip_flags = UIP_CLOSE)
  570. /**
  571. * Abort the current connection.
  572. *
  573. * This function will abort (reset) the current connection, and is
  574. * usually used when an error has occurred that prevents using the
  575. * uip_close() function.
  576. *
  577. * \hideinitializer
  578. */
  579. #define uip_abort() (uip_flags = UIP_ABORT)
  580. /**
  581. * Tell the sending host to stop sending data.
  582. *
  583. * This function will close our receiver's window so that we stop
  584. * receiving data for the current connection.
  585. *
  586. * \hideinitializer
  587. */
  588. #define uip_stop() (uip_conn->tcpstateflags |= UIP_STOPPED)
  589. /**
  590. * Find out if the current connection has been previously stopped with
  591. * uip_stop().
  592. *
  593. * \hideinitializer
  594. */
  595. #define uip_stopped(conn) ((conn)->tcpstateflags & UIP_STOPPED)
  596. /**
  597. * Restart the current connection, if is has previously been stopped
  598. * with uip_stop().
  599. *
  600. * This function will open the receiver's window again so that we
  601. * start receiving data for the current connection.
  602. *
  603. * \hideinitializer
  604. */
  605. #define uip_restart() do { uip_flags |= UIP_NEWDATA; \
  606. uip_conn->tcpstateflags &= ~UIP_STOPPED; \
  607. } while(0)
  608. /* uIP tests that can be made to determine in what state the current
  609. connection is, and what the application function should do. */
  610. /**
  611. * Is the current connection a UDP connection?
  612. *
  613. * This function checks whether the current connection is a UDP connection.
  614. *
  615. * \hideinitializer
  616. *
  617. */
  618. #define uip_udpconnection() (uip_conn == NULL)
  619. /**
  620. * Is new incoming data available?
  621. *
  622. * Will reduce to non-zero if there is new data for the application
  623. * present at the uip_appdata pointer. The size of the data is
  624. * available through the uip_len variable.
  625. *
  626. * \hideinitializer
  627. */
  628. #define uip_newdata() (uip_flags & UIP_NEWDATA)
  629. /**
  630. * Has previously sent data been acknowledged?
  631. *
  632. * Will reduce to non-zero if the previously sent data has been
  633. * acknowledged by the remote host. This means that the application
  634. * can send new data.
  635. *
  636. * \hideinitializer
  637. */
  638. #define uip_acked() (uip_flags & UIP_ACKDATA)
  639. /**
  640. * Has the connection just been connected?
  641. *
  642. * Reduces to non-zero if the current connection has been connected to
  643. * a remote host. This will happen both if the connection has been
  644. * actively opened (with uip_connect()) or passively opened (with
  645. * uip_listen()).
  646. *
  647. * \hideinitializer
  648. */
  649. #define uip_connected() (uip_flags & UIP_CONNECTED)
  650. /**
  651. * Has the connection been closed by the other end?
  652. *
  653. * Is non-zero if the connection has been closed by the remote
  654. * host. The application may then do the necessary clean-ups.
  655. *
  656. * \hideinitializer
  657. */
  658. #define uip_closed() (uip_flags & UIP_CLOSE)
  659. /**
  660. * Has the connection been aborted by the other end?
  661. *
  662. * Non-zero if the current connection has been aborted (reset) by the
  663. * remote host.
  664. *
  665. * \hideinitializer
  666. */
  667. #define uip_aborted() (uip_flags & UIP_ABORT)
  668. /**
  669. * Has the connection timed out?
  670. *
  671. * Non-zero if the current connection has been aborted due to too many
  672. * retransmissions.
  673. *
  674. * \hideinitializer
  675. */
  676. #define uip_timedout() (uip_flags & UIP_TIMEDOUT)
  677. /**
  678. * Do we need to retransmit previously data?
  679. *
  680. * Reduces to non-zero if the previously sent data has been lost in
  681. * the network, and the application should retransmit it. The
  682. * application should send the exact same data as it did the last
  683. * time, using the uip_send() function.
  684. *
  685. * \hideinitializer
  686. */
  687. #define uip_rexmit() (uip_flags & UIP_REXMIT)
  688. /**
  689. * Is the connection being polled by uIP?
  690. *
  691. * Is non-zero if the reason the application is invoked is that the
  692. * current connection has been idle for a while and should be
  693. * polled.
  694. *
  695. * The polling event can be used for sending data without having to
  696. * wait for the remote host to send data.
  697. *
  698. * \hideinitializer
  699. */
  700. #define uip_poll() (uip_flags & UIP_POLL)
  701. /**
  702. * Get the initial maximum segment size (MSS) of the current
  703. * connection.
  704. *
  705. * \hideinitializer
  706. */
  707. #define uip_initialmss() (uip_conn->initialmss)
  708. /**
  709. * Get the current maximum segment size that can be sent on the current
  710. * connection.
  711. *
  712. * The current maximum segment size that can be sent on the
  713. * connection is computed from the receiver's window and the MSS of
  714. * the connection (which also is available by calling
  715. * uip_initialmss()).
  716. *
  717. * \hideinitializer
  718. */
  719. #define uip_mss() (uip_conn->mss)
  720. /**
  721. * Set up a new UDP connection.
  722. *
  723. * This function sets up a new UDP connection. The function will
  724. * automatically allocate an unused local port for the new
  725. * connection. However, another port can be chosen by using the
  726. * uip_udp_bind() call, after the uip_udp_new() function has been
  727. * called.
  728. *
  729. * Example:
  730. \code
  731. uip_ipaddr_t addr;
  732. struct uip_udp_conn *c;
  733. uip_ipaddr(&addr, 192,168,2,1);
  734. c = uip_udp_new(&addr, HTONS(12345));
  735. if(c != NULL) {
  736. uip_udp_bind(c, HTONS(12344));
  737. }
  738. \endcode
  739. * \param ripaddr The IP address of the remote host.
  740. *
  741. * \param rport The remote port number in network byte order.
  742. *
  743. * \return The uip_udp_conn structure for the new connection or NULL
  744. * if no connection could be allocated.
  745. */
  746. struct uip_udp_conn *uip_udp_new(const uip_ipaddr_t *ripaddr, u16_t rport);
  747. /**
  748. * Removed a UDP connection.
  749. *
  750. * \param conn A pointer to the uip_udp_conn structure for the connection.
  751. *
  752. * \hideinitializer
  753. */
  754. #define uip_udp_remove(conn) (conn)->lport = 0
  755. /**
  756. * Bind a UDP connection to a local port.
  757. *
  758. * \param conn A pointer to the uip_udp_conn structure for the
  759. * connection.
  760. *
  761. * \param port The local port number, in network byte order.
  762. *
  763. * \hideinitializer
  764. */
  765. #define uip_udp_bind(conn, port) (conn)->lport = port
  766. /**
  767. * Send a UDP datagram of length len on the current connection.
  768. *
  769. * This function can only be called in response to a UDP event (poll
  770. * or newdata). The data must be present in the uip_buf buffer, at the
  771. * place pointed to by the uip_appdata pointer.
  772. *
  773. * \param len The length of the data in the uip_buf buffer.
  774. *
  775. * \hideinitializer
  776. */
  777. #define uip_udp_send(len) uip_send((char *)uip_appdata, len)
  778. /** @} */
  779. /* uIP convenience and converting functions. */
  780. /**
  781. * \defgroup uipconvfunc uIP conversion functions
  782. * @{
  783. *
  784. * These functions can be used for converting between different data
  785. * formats used by uIP.
  786. */
  787. /**
  788. * Convert an IP address to four bytes separated by commas.
  789. *
  790. * Example:
  791. \code
  792. uip_ipaddr_t ipaddr;
  793. printf("ipaddr=%d.%d.%d.%d\n", uip_ipaddr_to_quad(&ipaddr));
  794. \endcode
  795. *
  796. * \param a A pointer to a uip_ipaddr_t.
  797. * \hideinitializer
  798. */
  799. #define uip_ipaddr_to_quad(a) (a)->u8[0],(a)->u8[1],(a)->u8[2],(a)->u8[3]
  800. /**
  801. * Construct an IP address from four bytes.
  802. *
  803. * This function constructs an IP address of the type that uIP handles
  804. * internally from four bytes. The function is handy for specifying IP
  805. * addresses to use with e.g. the uip_connect() function.
  806. *
  807. * Example:
  808. \code
  809. uip_ipaddr_t ipaddr;
  810. struct uip_conn *c;
  811. uip_ipaddr(&ipaddr, 192,168,1,2);
  812. c = uip_connect(&ipaddr, HTONS(80));
  813. \endcode
  814. *
  815. * \param addr A pointer to a uip_ipaddr_t variable that will be
  816. * filled in with the IP address.
  817. *
  818. * \param addr0 The first octet of the IP address.
  819. * \param addr1 The second octet of the IP address.
  820. * \param addr2 The third octet of the IP address.
  821. * \param addr3 The forth octet of the IP address.
  822. *
  823. * \hideinitializer
  824. */
  825. #define uip_ipaddr(addr, addr0,addr1,addr2,addr3) do { \
  826. (addr)->u8[0] = addr0; \
  827. (addr)->u8[1] = addr1; \
  828. (addr)->u8[2] = addr2; \
  829. (addr)->u8[3] = addr3; \
  830. } while(0)
  831. /**
  832. * Construct an IPv6 address from eight 16-bit words.
  833. *
  834. * This function constructs an IPv6 address.
  835. *
  836. * \hideinitializer
  837. */
  838. #define uip_ip6addr(addr, addr0,addr1,addr2,addr3,addr4,addr5,addr6,addr7) do { \
  839. (addr)->u16[0] = HTONS(addr0); \
  840. (addr)->u16[1] = HTONS(addr1); \
  841. (addr)->u16[2] = HTONS(addr2); \
  842. (addr)->u16[3] = HTONS(addr3); \
  843. (addr)->u16[4] = HTONS(addr4); \
  844. (addr)->u16[5] = HTONS(addr5); \
  845. (addr)->u16[6] = HTONS(addr6); \
  846. (addr)->u16[7] = HTONS(addr7); \
  847. } while(0)
  848. /**
  849. * Construct an IPv6 address from sixteen 8-bit words.
  850. *
  851. * This function constructs an IPv6 address.
  852. *
  853. * \hideinitializer
  854. */
  855. #define uip_ip6addr_u8(addr, addr0,addr1,addr2,addr3,addr4,addr5,addr6,addr7,addr8,addr9,addr10,addr11,addr12,addr13,addr14,addr15) do { \
  856. (addr)->u8[0] = addr0; \
  857. (addr)->u8[1] = addr1; \
  858. (addr)->u8[2] = addr2; \
  859. (addr)->u8[3] = addr3; \
  860. (addr)->u8[4] = addr4; \
  861. (addr)->u8[5] = addr5; \
  862. (addr)->u8[6] = addr6; \
  863. (addr)->u8[7] = addr7; \
  864. (addr)->u8[8] = addr8; \
  865. (addr)->u8[9] = addr9; \
  866. (addr)->u8[10] = addr10; \
  867. (addr)->u8[11] = addr11; \
  868. (addr)->u8[12] = addr12; \
  869. (addr)->u8[13] = addr13; \
  870. (addr)->u8[14] = addr14; \
  871. (addr)->u8[15] = addr15; \
  872. } while(0)
  873. /**
  874. * Copy an IP address to another IP address.
  875. *
  876. * Copies an IP address from one place to another.
  877. *
  878. * Example:
  879. \code
  880. uip_ipaddr_t ipaddr1, ipaddr2;
  881. uip_ipaddr(&ipaddr1, 192,16,1,2);
  882. uip_ipaddr_copy(&ipaddr2, &ipaddr1);
  883. \endcode
  884. *
  885. * \param dest The destination for the copy.
  886. * \param src The source from where to copy.
  887. *
  888. * \hideinitializer
  889. */
  890. #ifndef uip_ipaddr_copy
  891. #define uip_ipaddr_copy(dest, src) (*(dest) = *(src))
  892. #endif
  893. /**
  894. * Compare two IP addresses
  895. *
  896. * Compares two IP addresses.
  897. *
  898. * Example:
  899. \code
  900. uip_ipaddr_t ipaddr1, ipaddr2;
  901. uip_ipaddr(&ipaddr1, 192,16,1,2);
  902. if(uip_ipaddr_cmp(&ipaddr2, &ipaddr1)) {
  903. printf("They are the same");
  904. }
  905. \endcode
  906. *
  907. * \param addr1 The first IP address.
  908. * \param addr2 The second IP address.
  909. *
  910. * \hideinitializer
  911. */
  912. #if !UIP_CONF_IPV6
  913. #define uip_ipaddr_cmp(addr1, addr2) ((addr1)->u16[0] == (addr2)->u16[0] && \
  914. (addr1)->u16[1] == (addr2)->u16[1])
  915. #else /* !UIP_CONF_IPV6 */
  916. #define uip_ipaddr_cmp(addr1, addr2) (memcmp(addr1, addr2, sizeof(uip_ip6addr_t)) == 0)
  917. #endif /* !UIP_CONF_IPV6 */
  918. /**
  919. * Compare two IP addresses with netmasks
  920. *
  921. * Compares two IP addresses with netmasks. The masks are used to mask
  922. * out the bits that are to be compared.
  923. *
  924. * Example:
  925. \code
  926. uip_ipaddr_t ipaddr1, ipaddr2, mask;
  927. uip_ipaddr(&mask, 255,255,255,0);
  928. uip_ipaddr(&ipaddr1, 192,16,1,2);
  929. uip_ipaddr(&ipaddr2, 192,16,1,3);
  930. if(uip_ipaddr_maskcmp(&ipaddr1, &ipaddr2, &mask)) {
  931. printf("They are the same");
  932. }
  933. \endcode
  934. *
  935. * \param addr1 The first IP address.
  936. * \param addr2 The second IP address.
  937. * \param mask The netmask.
  938. *
  939. * \hideinitializer
  940. */
  941. #if !UIP_CONF_IPV6
  942. #define uip_ipaddr_maskcmp(addr1, addr2, mask) \
  943. (((((u16_t *)addr1)[0] & ((u16_t *)mask)[0]) == \
  944. (((u16_t *)addr2)[0] & ((u16_t *)mask)[0])) && \
  945. ((((u16_t *)addr1)[1] & ((u16_t *)mask)[1]) == \
  946. (((u16_t *)addr2)[1] & ((u16_t *)mask)[1])))
  947. #else
  948. #define uip_ipaddr_prefixcmp(addr1, addr2, length) (memcmp(addr1, addr2, length>>3) == 0)
  949. #endif
  950. /**
  951. * Check if an address is a broadcast address for a network.
  952. *
  953. * Checks if an address is the broadcast address for a network. The
  954. * network is defined by an IP address that is on the network and the
  955. * network's netmask.
  956. *
  957. * \param addr The IP address.
  958. * \param netaddr The network's IP address.
  959. * \param netmask The network's netmask.
  960. *
  961. * \hideinitializer
  962. */
  963. /*#define uip_ipaddr_isbroadcast(addr, netaddr, netmask)
  964. ((uip_ipaddr_t *)(addr)).u16 & ((uip_ipaddr_t *)(addr)).u16*/
  965. /**
  966. * Mask out the network part of an IP address.
  967. *
  968. * Masks out the network part of an IP address, given the address and
  969. * the netmask.
  970. *
  971. * Example:
  972. \code
  973. uip_ipaddr_t ipaddr1, ipaddr2, netmask;
  974. uip_ipaddr(&ipaddr1, 192,16,1,2);
  975. uip_ipaddr(&netmask, 255,255,255,0);
  976. uip_ipaddr_mask(&ipaddr2, &ipaddr1, &netmask);
  977. \endcode
  978. *
  979. * In the example above, the variable "ipaddr2" will contain the IP
  980. * address 192.168.1.0.
  981. *
  982. * \param dest Where the result is to be placed.
  983. * \param src The IP address.
  984. * \param mask The netmask.
  985. *
  986. * \hideinitializer
  987. */
  988. #define uip_ipaddr_mask(dest, src, mask) do { \
  989. ((u16_t *)dest)[0] = ((u16_t *)src)[0] & ((u16_t *)mask)[0]; \
  990. ((u16_t *)dest)[1] = ((u16_t *)src)[1] & ((u16_t *)mask)[1]; \
  991. } while(0)
  992. /**
  993. * Pick the first octet of an IP address.
  994. *
  995. * Picks out the first octet of an IP address.
  996. *
  997. * Example:
  998. \code
  999. uip_ipaddr_t ipaddr;
  1000. u8_t octet;
  1001. uip_ipaddr(&ipaddr, 1,2,3,4);
  1002. octet = uip_ipaddr1(&ipaddr);
  1003. \endcode
  1004. *
  1005. * In the example above, the variable "octet" will contain the value 1.
  1006. *
  1007. * \hideinitializer
  1008. */
  1009. #define uip_ipaddr1(addr) ((addr)->u8[0])
  1010. /**
  1011. * Pick the second octet of an IP address.
  1012. *
  1013. * Picks out the second octet of an IP address.
  1014. *
  1015. * Example:
  1016. \code
  1017. uip_ipaddr_t ipaddr;
  1018. u8_t octet;
  1019. uip_ipaddr(&ipaddr, 1,2,3,4);
  1020. octet = uip_ipaddr2(&ipaddr);
  1021. \endcode
  1022. *
  1023. * In the example above, the variable "octet" will contain the value 2.
  1024. *
  1025. * \hideinitializer
  1026. */
  1027. #define uip_ipaddr2(addr) ((addr)->u8[1])
  1028. /**
  1029. * Pick the third octet of an IP address.
  1030. *
  1031. * Picks out the third octet of an IP address.
  1032. *
  1033. * Example:
  1034. \code
  1035. uip_ipaddr_t ipaddr;
  1036. u8_t octet;
  1037. uip_ipaddr(&ipaddr, 1,2,3,4);
  1038. octet = uip_ipaddr3(&ipaddr);
  1039. \endcode
  1040. *
  1041. * In the example above, the variable "octet" will contain the value 3.
  1042. *
  1043. * \hideinitializer
  1044. */
  1045. #define uip_ipaddr3(addr) ((addr)->u8[2])
  1046. /**
  1047. * Pick the fourth octet of an IP address.
  1048. *
  1049. * Picks out the fourth octet of an IP address.
  1050. *
  1051. * Example:
  1052. \code
  1053. uip_ipaddr_t ipaddr;
  1054. u8_t octet;
  1055. uip_ipaddr(&ipaddr, 1,2,3,4);
  1056. octet = uip_ipaddr4(&ipaddr);
  1057. \endcode
  1058. *
  1059. * In the example above, the variable "octet" will contain the value 4.
  1060. *
  1061. * \hideinitializer
  1062. */
  1063. #define uip_ipaddr4(addr) ((addr)->u8[3])
  1064. /**
  1065. * Convert 16-bit quantity from host byte order to network byte order.
  1066. *
  1067. * This macro is primarily used for converting constants from host
  1068. * byte order to network byte order. For converting variables to
  1069. * network byte order, use the htons() function instead.
  1070. *
  1071. * \hideinitializer
  1072. */
  1073. #ifndef HTONS
  1074. # if UIP_BYTE_ORDER == UIP_BIG_ENDIAN
  1075. # define HTONS(n) (n)
  1076. # define HTONL(n) (n)
  1077. # else /* UIP_BYTE_ORDER == UIP_BIG_ENDIAN */
  1078. # define HTONS(n) (u16_t)((((u16_t) (n)) << 8) | (((u16_t) (n)) >> 8))
  1079. # define HTONL(n) (((u32_t)HTONS(n) << 16) | HTONS((u32_t)(n) >> 16))
  1080. # endif /* UIP_BYTE_ORDER == UIP_BIG_ENDIAN */
  1081. #else
  1082. #error "HTONS already defined!"
  1083. #endif /* HTONS */
  1084. /**
  1085. * Convert 16-bit quantity from host byte order to network byte order.
  1086. *
  1087. * This function is primarily used for converting variables from host
  1088. * byte order to network byte order. For converting constants to
  1089. * network byte order, use the HTONS() macro instead.
  1090. */
  1091. #ifndef htons
  1092. u16_t htons(u16_t val);
  1093. #endif /* htons */
  1094. #ifndef ntohs
  1095. #define ntohs htons
  1096. #endif
  1097. #ifndef htonl
  1098. u32_t htonl(u32_t val);
  1099. #endif /* htonl */
  1100. #ifndef ntohl
  1101. #define ntohl htonl
  1102. #endif
  1103. /** @} */
  1104. /**
  1105. * Pointer to the application data in the packet buffer.
  1106. *
  1107. * This pointer points to the application data when the application is
  1108. * called. If the application wishes to send data, the application may
  1109. * use this space to write the data into before calling uip_send().
  1110. */
  1111. extern void *uip_appdata;
  1112. #if UIP_URGDATA > 0
  1113. /* u8_t *uip_urgdata:
  1114. *
  1115. * This pointer points to any urgent data that has been received. Only
  1116. * present if compiled with support for urgent data (UIP_URGDATA).
  1117. */
  1118. extern void *uip_urgdata;
  1119. #endif /* UIP_URGDATA > 0 */
  1120. /**
  1121. * \defgroup uipdrivervars Variables used in uIP device drivers
  1122. * @{
  1123. *
  1124. * uIP has a few global variables that are used in device drivers for
  1125. * uIP.
  1126. */
  1127. /**
  1128. * The length of the packet in the uip_buf buffer.
  1129. *
  1130. * The global variable uip_len holds the length of the packet in the
  1131. * uip_buf buffer.
  1132. *
  1133. * When the network device driver calls the uIP input function,
  1134. * uip_len should be set to the length of the packet in the uip_buf
  1135. * buffer.
  1136. *
  1137. * When sending packets, the device driver should use the contents of
  1138. * the uip_len variable to determine the length of the outgoing
  1139. * packet.
  1140. *
  1141. */
  1142. extern u16_t uip_len;
  1143. /**
  1144. * The length of the extension headers
  1145. */
  1146. extern u8_t uip_ext_len;
  1147. /** @} */
  1148. #if UIP_URGDATA > 0
  1149. extern u16_t uip_urglen, uip_surglen;
  1150. #endif /* UIP_URGDATA > 0 */
  1151. /**
  1152. * Representation of a uIP TCP connection.
  1153. *
  1154. * The uip_conn structure is used for identifying a connection. All
  1155. * but one field in the structure are to be considered read-only by an
  1156. * application. The only exception is the appstate field whose purpose
  1157. * is to let the application store application-specific state (e.g.,
  1158. * file pointers) for the connection. The type of this field is
  1159. * configured in the "uipopt.h" header file.
  1160. */
  1161. struct uip_conn {
  1162. uip_ipaddr_t ripaddr; /**< The IP address of the remote host. */
  1163. u16_t lport; /**< The local TCP port, in network byte order. */
  1164. u16_t rport; /**< The local remote TCP port, in network byte
  1165. order. */
  1166. u8_t rcv_nxt[4]; /**< The sequence number that we expect to
  1167. receive next. */
  1168. u8_t snd_nxt[4]; /**< The sequence number that was last sent by
  1169. us. */
  1170. u16_t len; /**< Length of the data that was previously sent. */
  1171. u16_t mss; /**< Current maximum segment size for the
  1172. connection. */
  1173. u16_t initialmss; /**< Initial maximum segment size for the
  1174. connection. */
  1175. u8_t sa; /**< Retransmission time-out calculation state
  1176. variable. */
  1177. u8_t sv; /**< Retransmission time-out calculation state
  1178. variable. */
  1179. u8_t rto; /**< Retransmission time-out. */
  1180. u8_t tcpstateflags; /**< TCP state and flags. */
  1181. u8_t timer; /**< The retransmission timer. */
  1182. u8_t nrtx; /**< The number of retransmissions for the last
  1183. segment sent. */
  1184. /** The application state. */
  1185. uip_tcp_appstate_t appstate;
  1186. };
  1187. /**
  1188. * Pointer to the current TCP connection.
  1189. *
  1190. * The uip_conn pointer can be used to access the current TCP
  1191. * connection.
  1192. */
  1193. extern struct uip_conn *uip_conn;
  1194. #if UIP_TCP
  1195. /* The array containing all uIP connections. */
  1196. extern struct uip_conn uip_conns[UIP_CONNS];
  1197. #endif
  1198. /**
  1199. * \addtogroup uiparch
  1200. * @{
  1201. */
  1202. /**
  1203. * 4-byte array used for the 32-bit sequence number calculations.
  1204. */
  1205. extern u8_t uip_acc32[4];
  1206. /** @} */
  1207. /**
  1208. * Representation of a uIP UDP connection.
  1209. */
  1210. struct uip_udp_conn {
  1211. uip_ipaddr_t ripaddr; /**< The IP address of the remote peer. */
  1212. u16_t lport; /**< The local port number in network byte order. */
  1213. u16_t rport; /**< The remote port number in network byte order. */
  1214. u8_t ttl; /**< Default time-to-live. */
  1215. /** The application state. */
  1216. uip_udp_appstate_t appstate;
  1217. };
  1218. /**
  1219. * The current UDP connection.
  1220. */
  1221. extern struct uip_udp_conn *uip_udp_conn;
  1222. extern struct uip_udp_conn uip_udp_conns[UIP_UDP_CONNS];
  1223. struct uip_router {
  1224. int (*activate)(void);
  1225. int (*deactivate)(void);
  1226. uip_ipaddr_t *(*lookup)(uip_ipaddr_t *destipaddr, uip_ipaddr_t *nexthop);
  1227. };
  1228. #if UIP_CONF_ROUTER
  1229. extern const struct uip_router *uip_router;
  1230. /**
  1231. * uIP routing driver registration function.
  1232. */
  1233. void uip_router_register(const struct uip_router *router);
  1234. #endif /*UIP_CONF_ROUTER*/
  1235. #if UIP_CONF_ICMP6
  1236. struct uip_icmp6_conn {
  1237. uip_icmp6_appstate_t appstate;
  1238. };
  1239. extern struct uip_icmp6_conn uip_icmp6_conns;
  1240. #endif /*UIP_CONF_ICMP6*/
  1241. /**
  1242. * The uIP TCP/IP statistics.
  1243. *
  1244. * This is the variable in which the uIP TCP/IP statistics are gathered.
  1245. */
  1246. #if UIP_STATISTICS == 1
  1247. extern struct uip_stats uip_stat;
  1248. #define UIP_STAT(s) s
  1249. #else
  1250. #define UIP_STAT(s)
  1251. #endif /* UIP_STATISTICS == 1 */
  1252. /**
  1253. * The structure holding the TCP/IP statistics that are gathered if
  1254. * UIP_STATISTICS is set to 1.
  1255. *
  1256. */
  1257. struct uip_stats {
  1258. struct {
  1259. uip_stats_t recv; /**< Number of received packets at the IP
  1260. layer. */
  1261. uip_stats_t sent; /**< Number of sent packets at the IP
  1262. layer. */
  1263. uip_stats_t forwarded;/**< Number of forwarded packets at the IP
  1264. layer. */
  1265. uip_stats_t drop; /**< Number of dropped packets at the IP
  1266. layer. */
  1267. uip_stats_t vhlerr; /**< Number of packets dropped due to wrong
  1268. IP version or header length. */
  1269. uip_stats_t hblenerr; /**< Number of packets dropped due to wrong
  1270. IP length, high byte. */
  1271. uip_stats_t lblenerr; /**< Number of packets dropped due to wrong
  1272. IP length, low byte. */
  1273. uip_stats_t fragerr; /**< Number of packets dropped since they
  1274. were IP fragments. */
  1275. uip_stats_t chkerr; /**< Number of packets dropped due to IP
  1276. checksum errors. */
  1277. uip_stats_t protoerr; /**< Number of packets dropped since they
  1278. were neither ICMP, UDP nor TCP. */
  1279. } ip; /**< IP statistics. */
  1280. struct {
  1281. uip_stats_t recv; /**< Number of received ICMP packets. */
  1282. uip_stats_t sent; /**< Number of sent ICMP packets. */
  1283. uip_stats_t drop; /**< Number of dropped ICMP packets. */
  1284. uip_stats_t typeerr; /**< Number of ICMP packets with a wrong
  1285. type. */
  1286. uip_stats_t chkerr; /**< Number of ICMP packets with a bad
  1287. checksum. */
  1288. } icmp; /**< ICMP statistics. */
  1289. #if UIP_TCP
  1290. struct {
  1291. uip_stats_t recv; /**< Number of received TCP segments. */
  1292. uip_stats_t sent; /**< Number of sent TCP segments. */
  1293. uip_stats_t drop; /**< Number of dropped TCP segments. */
  1294. uip_stats_t chkerr; /**< Number of TCP segments with a bad
  1295. checksum. */
  1296. uip_stats_t ackerr; /**< Number of TCP segments with a bad ACK
  1297. number. */
  1298. uip_stats_t rst; /**< Number of received TCP RST (reset) segments. */
  1299. uip_stats_t rexmit; /**< Number of retransmitted TCP segments. */
  1300. uip_stats_t syndrop; /**< Number of dropped SYNs due to too few
  1301. connections was available. */
  1302. uip_stats_t synrst; /**< Number of SYNs for closed ports,
  1303. triggering a RST. */
  1304. } tcp; /**< TCP statistics. */
  1305. #endif
  1306. #if UIP_UDP
  1307. struct {
  1308. uip_stats_t drop; /**< Number of dropped UDP segments. */
  1309. uip_stats_t recv; /**< Number of received UDP segments. */
  1310. uip_stats_t sent; /**< Number of sent UDP segments. */
  1311. uip_stats_t chkerr; /**< Number of UDP segments with a bad
  1312. checksum. */
  1313. } udp; /**< UDP statistics. */
  1314. #endif /* UIP_UDP */
  1315. #if UIP_CONF_IPV6
  1316. struct {
  1317. uip_stats_t drop; /**< Number of dropped ND6 packets. */
  1318. uip_stats_t recv; /**< Number of received ND6 packets */
  1319. uip_stats_t sent; /**< Number of sent ND6 packets */
  1320. } nd6;
  1321. #endif /*UIP_CONF_IPV6*/
  1322. };
  1323. /*---------------------------------------------------------------------------*/
  1324. /* All the stuff below this point is internal to uIP and should not be
  1325. * used directly by an application or by a device driver.
  1326. */
  1327. /*---------------------------------------------------------------------------*/
  1328. /* u8_t uip_flags:
  1329. *
  1330. * When the application is called, uip_flags will contain the flags
  1331. * that are defined in this file. Please read below for more
  1332. * information.
  1333. */
  1334. extern u8_t uip_flags;
  1335. /* The following flags may be set in the global variable uip_flags
  1336. before calling the application callback. The UIP_ACKDATA,
  1337. UIP_NEWDATA, and UIP_CLOSE flags may both be set at the same time,
  1338. whereas the others are mutually exclusive. Note that these flags
  1339. should *NOT* be accessed directly, but only through the uIP
  1340. functions/macros. */
  1341. #define UIP_ACKDATA 1 /* Signifies that the outstanding data was
  1342. acked and the application should send
  1343. out new data instead of retransmitting
  1344. the last data. */
  1345. #define UIP_NEWDATA 2 /* Flags the fact that the peer has sent
  1346. us new data. */
  1347. #define UIP_REXMIT 4 /* Tells the application to retransmit the
  1348. data that was last sent. */
  1349. #define UIP_POLL 8 /* Used for polling the application, to
  1350. check if the application has data that
  1351. it wants to send. */
  1352. #define UIP_CLOSE 16 /* The remote host has closed the
  1353. connection, thus the connection has
  1354. gone away. Or the application signals
  1355. that it wants to close the
  1356. connection. */
  1357. #define UIP_ABORT 32 /* The remote host has aborted the
  1358. connection, thus the connection has
  1359. gone away. Or the application signals
  1360. that it wants to abort the
  1361. connection. */
  1362. #define UIP_CONNECTED 64 /* We have got a connection from a remote
  1363. host and have set up a new connection
  1364. for it, or an active connection has
  1365. been successfully established. */
  1366. #define UIP_TIMEDOUT 128 /* The connection has been aborted due to
  1367. too many retransmissions. */
  1368. /**
  1369. * \brief process the options within a hop by hop or destination option header
  1370. * \retval 0: nothing to send,
  1371. * \retval 1: drop pkt
  1372. * \retval 2: ICMP error message to send
  1373. */
  1374. /*static u8_t
  1375. uip_ext_hdr_options_process(); */
  1376. /* uip_process(flag):
  1377. *
  1378. * The actual uIP function which does all the work.
  1379. */
  1380. void uip_process(u8_t flag);
  1381. /* The following flags are passed as an argument to the uip_process()
  1382. function. They are used to distinguish between the two cases where
  1383. uip_process() is called. It can be called either because we have
  1384. incoming data that should be processed, or because the periodic
  1385. timer has fired. These values are never used directly, but only in
  1386. the macros defined in this file. */
  1387. #define UIP_DATA 1 /* Tells uIP that there is incoming
  1388. data in the uip_buf buffer. The
  1389. length of the data is stored in the
  1390. global variable uip_len. */
  1391. #define UIP_TIMER 2 /* Tells uIP that the periodic timer
  1392. has fired. */
  1393. #define UIP_POLL_REQUEST 3 /* Tells uIP that a connection should
  1394. be polled. */
  1395. #define UIP_UDP_SEND_CONN 4 /* Tells uIP that a UDP datagram
  1396. should be constructed in the
  1397. uip_buf buffer. */
  1398. #if UIP_UDP
  1399. #define UIP_UDP_TIMER 5
  1400. #endif /* UIP_UDP */
  1401. /* The TCP states used in the uip_conn->tcpstateflags. */
  1402. #define UIP_CLOSED 0
  1403. #define UIP_SYN_RCVD 1
  1404. #define UIP_SYN_SENT 2
  1405. #define UIP_ESTABLISHED 3
  1406. #define UIP_FIN_WAIT_1 4
  1407. #define UIP_FIN_WAIT_2 5
  1408. #define UIP_CLOSING 6
  1409. #define UIP_TIME_WAIT 7
  1410. #define UIP_LAST_ACK 8
  1411. #define UIP_TS_MASK 15
  1412. #define UIP_STOPPED 16
  1413. /* The TCP and IP headers. */
  1414. struct uip_tcpip_hdr {
  1415. #if UIP_CONF_IPV6
  1416. /* IPv6 header. */
  1417. u8_t vtc,
  1418. tcflow;
  1419. u16_t flow;
  1420. u8_t len[2];
  1421. u8_t proto, ttl;
  1422. uip_ip6addr_t srcipaddr, destipaddr;
  1423. #else /* UIP_CONF_IPV6 */
  1424. /* IPv4 header. */
  1425. u8_t vhl,
  1426. tos,
  1427. len[2],
  1428. ipid[2],
  1429. ipoffset[2],
  1430. ttl,
  1431. proto;
  1432. u16_t ipchksum;
  1433. uip_ipaddr_t srcipaddr, destipaddr;
  1434. #endif /* UIP_CONF_IPV6 */
  1435. /* TCP header. */
  1436. u16_t srcport,
  1437. destport;
  1438. u8_t seqno[4],
  1439. ackno[4],
  1440. tcpoffset,
  1441. flags,
  1442. wnd[2];
  1443. u16_t tcpchksum;
  1444. u8_t urgp[2];
  1445. u8_t optdata[4];
  1446. };
  1447. /* The ICMP and IP headers. */
  1448. struct uip_icmpip_hdr {
  1449. #if UIP_CONF_IPV6
  1450. /* IPv6 header. */
  1451. u8_t vtc,
  1452. tcf;
  1453. u16_t flow;
  1454. u8_t len[2];
  1455. u8_t proto, ttl;
  1456. uip_ip6addr_t srcipaddr, destipaddr;
  1457. #else /* UIP_CONF_IPV6 */
  1458. /* IPv4 header. */
  1459. u8_t vhl,
  1460. tos,
  1461. len[2],
  1462. ipid[2],
  1463. ipoffset[2],
  1464. ttl,
  1465. proto;
  1466. u16_t ipchksum;
  1467. uip_ipaddr_t srcipaddr, destipaddr;
  1468. #endif /* UIP_CONF_IPV6 */
  1469. /* ICMP header. */
  1470. u8_t type, icode;
  1471. u16_t icmpchksum;
  1472. #if !UIP_CONF_IPV6
  1473. u16_t id, seqno;
  1474. u8_t payload[1];
  1475. #endif /* !UIP_CONF_IPV6 */
  1476. };
  1477. /* The UDP and IP headers. */
  1478. struct uip_udpip_hdr {
  1479. #if UIP_CONF_IPV6
  1480. /* IPv6 header. */
  1481. u8_t vtc,
  1482. tcf;
  1483. u16_t flow;
  1484. u8_t len[2];
  1485. u8_t proto, ttl;
  1486. uip_ip6addr_t srcipaddr, destipaddr;
  1487. #else /* UIP_CONF_IPV6 */
  1488. /* IP header. */
  1489. u8_t vhl,
  1490. tos,
  1491. len[2],
  1492. ipid[2],
  1493. ipoffset[2],
  1494. ttl,
  1495. proto;
  1496. u16_t ipchksum;
  1497. uip_ipaddr_t srcipaddr, destipaddr;
  1498. #endif /* UIP_CONF_IPV6 */
  1499. /* UDP header. */
  1500. u16_t srcport,
  1501. destport;
  1502. u16_t udplen;
  1503. u16_t udpchksum;
  1504. };
  1505. /*
  1506. * In IPv6 the length of the L3 headers before the transport header is
  1507. * not fixed, due to the possibility to include extension option headers
  1508. * after the IP header. hence we split here L3 and L4 headers
  1509. */
  1510. /* The IP header */
  1511. struct uip_ip_hdr {
  1512. #if UIP_CONF_IPV6
  1513. /* IPV6 header */
  1514. u8_t vtc;
  1515. u8_t tcflow;
  1516. u16_t flow;
  1517. u8_t len[2];
  1518. u8_t proto, ttl;
  1519. uip_ip6addr_t srcipaddr, destipaddr;
  1520. #else /* UIP_CONF_IPV6 */
  1521. /* IPV4 header */
  1522. u8_t vhl,
  1523. tos,
  1524. len[2],
  1525. ipid[2],
  1526. ipoffset[2],
  1527. ttl,
  1528. proto;
  1529. u16_t ipchksum;
  1530. uip_ipaddr_t srcipaddr, destipaddr;
  1531. #endif /* UIP_CONF_IPV6 */
  1532. };
  1533. /*
  1534. * IPv6 extension option headers: we are able to process
  1535. * the 4 extension headers defined in RFC2460 (IPv6):
  1536. * - Hop by hop option header, destination option header:
  1537. * These two are not used by any core IPv6 protocol, hence
  1538. * we just read them and go to the next. They convey options,
  1539. * the options defined in RFC2460 are Pad1 and PadN, which do
  1540. * some padding, and that we do not need to read (the length
  1541. * field in the header is enough)
  1542. * - Routing header: this one is most notably used by MIPv6,
  1543. * which we do not implement, hence we just read it and go
  1544. * to the next
  1545. * - Fragmentation header: we read this header and are able to
  1546. * reassemble packets
  1547. *
  1548. * We do not offer any means to send packets with extension headers
  1549. *
  1550. * We do not implement Authentication and ESP headers, which are
  1551. * used in IPSec and defined in RFC4302,4303,4305,4385
  1552. */
  1553. /* common header part */
  1554. struct uip_ext_hdr {
  1555. u8_t next;
  1556. u8_t len;
  1557. };
  1558. /* Hop by Hop option header */
  1559. struct uip_hbho_hdr {
  1560. u8_t next;
  1561. u8_t len;
  1562. };
  1563. /* destination option header */
  1564. struct uip_desto_hdr {
  1565. u8_t next;
  1566. u8_t len;
  1567. };
  1568. /* We do not define structures for PAD1 and PADN options */
  1569. /*
  1570. * routing header
  1571. * the routing header as 4 common bytes, then routing header type
  1572. * specific data there are several types of routing header. Type 0 was
  1573. * deprecated as per RFC5095 most notable other type is 2, used in
  1574. * RFC3775 (MIPv6) here we do not implement MIPv6, so we just need to
  1575. * parse the 4 first bytes
  1576. */
  1577. struct uip_routing_hdr {
  1578. u8_t next;
  1579. u8_t len;
  1580. u8_t routing_type;
  1581. u8_t seg_left;
  1582. };
  1583. /* fragmentation header */
  1584. struct uip_frag_hdr {
  1585. u8_t next;
  1586. u8_t res;
  1587. u16_t offsetresmore;
  1588. u32_t id;
  1589. };
  1590. /*
  1591. * an option within the destination or hop by hop option headers
  1592. * it contains type an length, which is true for all options but PAD1
  1593. */
  1594. struct uip_ext_hdr_opt {
  1595. u8_t type;
  1596. u8_t len;
  1597. };
  1598. /* PADN option */
  1599. struct uip_ext_hdr_opt_padn {
  1600. u8_t opt_type;
  1601. u8_t opt_len;
  1602. };
  1603. /* TCP header */
  1604. struct uip_tcp_hdr {
  1605. u16_t srcport;
  1606. u16_t destport;
  1607. u8_t seqno[4];
  1608. u8_t ackno[4];
  1609. u8_t tcpoffset;
  1610. u8_t flags;
  1611. u8_t wnd[2];
  1612. u16_t tcpchksum;
  1613. u8_t urgp[2];
  1614. u8_t optdata[4];
  1615. };
  1616. /* The ICMP headers. */
  1617. struct uip_icmp_hdr {
  1618. u8_t type, icode;
  1619. u16_t icmpchksum;
  1620. #if !UIP_CONF_IPV6
  1621. u16_t id, seqno;
  1622. #endif /* !UIP_CONF_IPV6 */
  1623. };
  1624. /* The UDP headers. */
  1625. struct uip_udp_hdr {
  1626. u16_t srcport;
  1627. u16_t destport;
  1628. u16_t udplen;
  1629. u16_t udpchksum;
  1630. };
  1631. /**
  1632. * The buffer size available for user data in the \ref uip_buf buffer.
  1633. *
  1634. * This macro holds the available size for user data in the \ref
  1635. * uip_buf buffer. The macro is intended to be used for checking
  1636. * bounds of available user data.
  1637. *
  1638. * Example:
  1639. \code
  1640. snprintf(uip_appdata, UIP_APPDATA_SIZE, "%u\n", i);
  1641. \endcode
  1642. *
  1643. * \hideinitializer
  1644. */
  1645. #define UIP_APPDATA_SIZE (UIP_BUFSIZE - UIP_LLH_LEN - UIP_TCPIP_HLEN)
  1646. #define UIP_APPDATA_PTR (void *)&uip_buf[UIP_LLH_LEN + UIP_TCPIP_HLEN]
  1647. #define UIP_PROTO_ICMP 1
  1648. #define UIP_PROTO_TCP 6
  1649. #define UIP_PROTO_UDP 17
  1650. #define UIP_PROTO_ICMP6 58
  1651. #if UIP_CONF_IPV6
  1652. /** @{ */
  1653. /** \brief extension headers types */
  1654. #define UIP_PROTO_HBHO 0
  1655. #define UIP_PROTO_DESTO 60
  1656. #define UIP_PROTO_ROUTING 43
  1657. #define UIP_PROTO_FRAG 44
  1658. #define UIP_PROTO_NONE 59
  1659. /** @} */
  1660. /** @{ */
  1661. /** \brief Destination and Hop By Hop extension headers option types */
  1662. #define UIP_EXT_HDR_OPT_PAD1 0
  1663. #define UIP_EXT_HDR_OPT_PADN 1
  1664. /** @} */
  1665. /** @{ */
  1666. /**
  1667. * \brief Bitmaps for extension header processing
  1668. *
  1669. * When processing extension headers, we should record somehow which one we
  1670. * see, because you cannot have twice the same header, except for destination
  1671. * We store all this in one u8_t bitmap one bit for each header expected. The
  1672. * order in the bitmap is the order recommended in RFC2460
  1673. */
  1674. #define UIP_EXT_HDR_BITMAP_HBHO 0x01
  1675. #define UIP_EXT_HDR_BITMAP_DESTO1 0x02
  1676. #define UIP_EXT_HDR_BITMAP_ROUTING 0x04
  1677. #define UIP_EXT_HDR_BITMAP_FRAG 0x08
  1678. #define UIP_EXT_HDR_BITMAP_AH 0x10
  1679. #define UIP_EXT_HDR_BITMAP_ESP 0x20
  1680. #define UIP_EXT_HDR_BITMAP_DESTO2 0x40
  1681. /** @} */
  1682. #endif /* UIP_CONF_IPV6 */
  1683. /* Header sizes. */
  1684. #if UIP_CONF_IPV6
  1685. #define UIP_IPH_LEN 40
  1686. #define UIP_FRAGH_LEN 8
  1687. #else /* UIP_CONF_IPV6 */
  1688. #define UIP_IPH_LEN 20 /* Size of IP header */
  1689. #endif /* UIP_CONF_IPV6 */
  1690. #define UIP_UDPH_LEN 8 /* Size of UDP header */
  1691. #define UIP_TCPH_LEN 20 /* Size of TCP header */
  1692. #ifdef UIP_IPH_LEN
  1693. #define UIP_ICMPH_LEN 4 /* Size of ICMP header */
  1694. #endif
  1695. #define UIP_IPUDPH_LEN (UIP_UDPH_LEN + UIP_IPH_LEN) /* Size of IP +
  1696. * UDP
  1697. * header */
  1698. #define UIP_IPTCPH_LEN (UIP_TCPH_LEN + UIP_IPH_LEN) /* Size of IP +
  1699. * TCP
  1700. * header */
  1701. #define UIP_TCPIP_HLEN UIP_IPTCPH_LEN
  1702. #define UIP_IPICMPH_LEN (UIP_IPH_LEN + UIP_ICMPH_LEN) /* size of ICMP
  1703. + IP header */
  1704. #define UIP_LLIPH_LEN (UIP_LLH_LEN + UIP_IPH_LEN) /* size of L2
  1705. + IP header */
  1706. #if UIP_CONF_IPV6
  1707. /**
  1708. * The sums below are quite used in ND. When used for uip_buf, we
  1709. * include link layer length when used for uip_len, we do not, hence
  1710. * we need values with and without LLH_LEN we do not use capital
  1711. * letters as these values are variable
  1712. */
  1713. #define uip_l2_l3_hdr_len (UIP_LLH_LEN + UIP_IPH_LEN + uip_ext_len)
  1714. #define uip_l2_l3_icmp_hdr_len (UIP_LLH_LEN + UIP_IPH_LEN + uip_ext_len + UIP_ICMPH_LEN)
  1715. #define uip_l3_hdr_len (UIP_IPH_LEN + uip_ext_len)
  1716. #define uip_l3_icmp_hdr_len (UIP_IPH_LEN + uip_ext_len + UIP_ICMPH_LEN)
  1717. #endif /*UIP_CONF_IPV6*/
  1718. #if UIP_FIXEDADDR
  1719. extern const uip_ipaddr_t uip_hostaddr, uip_netmask, uip_draddr;
  1720. #else /* UIP_FIXEDADDR */
  1721. extern uip_ipaddr_t uip_hostaddr, uip_netmask, uip_draddr;
  1722. #endif /* UIP_FIXEDADDR */
  1723. extern const uip_ipaddr_t uip_broadcast_addr;
  1724. extern const uip_ipaddr_t uip_all_zeroes_addr;
  1725. #if UIP_FIXEDETHADDR
  1726. extern const uip_lladdr_t uip_lladdr;
  1727. #else
  1728. extern uip_lladdr_t uip_lladdr;
  1729. #endif
  1730. #ifdef UIP_CONF_IPV6
  1731. /**
  1732. * \brief Is IPv6 address a the unspecified address
  1733. * a is of type uip_ipaddr_t
  1734. */
  1735. #define uip_is_addr_unspecified(a) \
  1736. ((((a)->u16[0]) == 0) && \
  1737. (((a)->u16[1]) == 0) && \
  1738. (((a)->u16[2]) == 0) && \
  1739. (((a)->u16[3]) == 0) && \
  1740. (((a)->u16[4]) == 0) && \
  1741. (((a)->u16[5]) == 0) && \
  1742. (((a)->u16[6]) == 0) && \
  1743. (((a)->u16[7]) == 0))
  1744. /** \brief Is IPv6 address a the link local all-nodes multicast address */
  1745. #define uip_is_addr_linklocal_allnodes_mcast(a) \
  1746. ((((a)->u8[0]) == 0xff) && \
  1747. (((a)->u8[1]) == 0x02) && \
  1748. (((a)->u16[1]) == 0) && \
  1749. (((a)->u16[2]) == 0) && \
  1750. (((a)->u16[3]) == 0) && \
  1751. (((a)->u16[4]) == 0) && \
  1752. (((a)->u16[5]) == 0) && \
  1753. (((a)->u16[6]) == 0) && \
  1754. (((a)->u8[14]) == 0) && \
  1755. (((a)->u8[15]) == 0x01))
  1756. /** \brief set IP address a to unspecified */
  1757. #define uip_create_unspecified(a) uip_ip6addr(a, 0, 0, 0, 0, 0, 0, 0, 0)
  1758. /** \brief set IP address a to the link local all-nodes multicast address */
  1759. #define uip_create_linklocal_allnodes_mcast(a) uip_ip6addr(a, 0xff02, 0, 0, 0, 0, 0, 0, 0x0001)
  1760. /** \brief set IP address a to the link local all-routers multicast address */
  1761. #define uip_create_linklocal_allrouters_mcast(a) uip_ip6addr(a, 0xff02, 0, 0, 0, 0, 0, 0, 0x0002)
  1762. /**
  1763. * \brief is addr (a) a solicited node multicast address, see RFC3513
  1764. * a is of type uip_ipaddr_t*
  1765. */
  1766. #define uip_is_addr_solicited_node(a) \
  1767. ((((a)->u8[0]) == 0xFF) && \
  1768. (((a)->u8[1]) == 0x02) && \
  1769. (((a)->u16[1]) == 0) && \
  1770. (((a)->u16[2]) == 0) && \
  1771. (((a)->u16[3]) == 0) && \
  1772. (((a)->u16[4]) == 0) && \
  1773. (((a)->u16[5]) == 1) && \
  1774. (((a)->u8[12]) == 0xFF))
  1775. /**
  1776. * \briefput in b the solicited node address corresponding to address a
  1777. * both a and b are of type uip_ipaddr_t*
  1778. * */
  1779. #define uip_create_solicited_node(a, b) \
  1780. (((b)->u8[0]) = 0xFF); \
  1781. (((b)->u8[1]) = 0x02); \
  1782. (((b)->u16[1]) = 0); \
  1783. (((b)->u16[2]) = 0); \
  1784. (((b)->u16[3]) = 0); \
  1785. (((b)->u16[4]) = 0); \
  1786. (((b)->u8[10]) = 0); \
  1787. (((b)->u8[11]) = 0x01); \
  1788. (((b)->u8[12]) = 0xFF); \
  1789. (((b)->u8[13]) = ((a)->u8[13])); \
  1790. (((b)->u16[7]) = ((a)->u16[7]))
  1791. /**
  1792. * \brief is addr (a) a link local unicast address, see RFC3513
  1793. * i.e. is (a) on prefix FE80::/10
  1794. * a is of type uip_ipaddr_t*
  1795. */
  1796. #define uip_is_addr_link_local(a) \
  1797. ((((a)->u8[0]) == 0xFE) && \
  1798. (((a)->u8[1]) == 0x80))
  1799. /**
  1800. * \brief was addr (a) forged based on the mac address m
  1801. * a type is uip_ipaddr_t
  1802. * m type is uiplladdr_t
  1803. */
  1804. #if UIP_CONF_LL_802154
  1805. #define uip_is_addr_mac_addr_based(a, m) \
  1806. ((((a)->u8[8]) == (((m)->addr[0]) ^ 0x02)) && \
  1807. (((a)->u8[9]) == (m)->addr[1]) && \
  1808. (((a)->u8[10]) == (m)->addr[2]) && \
  1809. (((a)->u8[11]) == (m)->addr[3]) && \
  1810. (((a)->u8[12]) == (m)->addr[4]) && \
  1811. (((a)->u8[13]) == (m)->addr[5]) && \
  1812. (((a)->u8[14]) == (m)->addr[6]) && \
  1813. (((a)->u8[15]) == (m)->addr[7]))
  1814. #else
  1815. #define uip_is_addr_mac_addr_based(a, m) \
  1816. ((((a)->u8[8]) == (((m)->addr[0]) | 0x02)) && \
  1817. (((a)->u8[9]) == (m)->addr[1]) && \
  1818. (((a)->u8[10]) == (m)->addr[2]) && \
  1819. (((a)->u8[11]) == 0xff) && \
  1820. (((a)->u8[12]) == 0xfe) && \
  1821. (((a)->u8[13]) == (m)->addr[3]) && \
  1822. (((a)->u8[14]) == (m)->addr[4]) && \
  1823. (((a)->u8[15]) == (m)->addr[5]))
  1824. #endif /*UIP_CONF_LL_802154*/
  1825. /**
  1826. * \brief is address a multicast address, see RFC 3513
  1827. * a is of type uip_ipaddr_t*
  1828. * */
  1829. #define uip_is_addr_mcast(a) \
  1830. (((a)->u8[0]) == 0xFF)
  1831. /**
  1832. * \brief is group-id of multicast address a
  1833. * the all nodes group-id
  1834. */
  1835. #define uip_is_mcast_group_id_all_nodes(a) \
  1836. ((((a)->u16[1]) == 0) && \
  1837. (((a)->u16[2]) == 0) && \
  1838. (((a)->u16[3]) == 0) && \
  1839. (((a)->u16[4]) == 0) && \
  1840. (((a)->u16[5]) == 0) && \
  1841. (((a)->u16[6]) == 0) && \
  1842. (((a)->u8[14]) == 0) && \
  1843. (((a)->u8[15]) == 1))
  1844. /**
  1845. * \brief is group-id of multicast address a
  1846. * the all routers group-id
  1847. */
  1848. #define uip_is_mcast_group_id_all_routers(a) \
  1849. ((((a)->u16[1]) == 0) && \
  1850. (((a)->u16[2]) == 0) && \
  1851. (((a)->u16[3]) == 0) && \
  1852. (((a)->u16[4]) == 0) && \
  1853. (((a)->u16[5]) == 0) && \
  1854. (((a)->u16[6]) == 0) && \
  1855. (((a)->u8[14]) == 0) && \
  1856. (((a)->u8[15]) == 2))
  1857. #endif /*UIP_CONF_IPV6*/
  1858. /**
  1859. * Calculate the Internet checksum over a buffer.
  1860. *
  1861. * The Internet checksum is the one's complement of the one's
  1862. * complement sum of all 16-bit words in the buffer.
  1863. *
  1864. * See RFC1071.
  1865. *
  1866. * \param buf A pointer to the buffer over which the checksum is to be
  1867. * computed.
  1868. *
  1869. * \param len The length of the buffer over which the checksum is to
  1870. * be computed.
  1871. *
  1872. * \return The Internet checksum of the buffer.
  1873. */
  1874. u16_t uip_chksum(u16_t *buf, u16_t len);
  1875. /**
  1876. * Calculate the IP header checksum of the packet header in uip_buf.
  1877. *
  1878. * The IP header checksum is the Internet checksum of the 20 bytes of
  1879. * the IP header.
  1880. *
  1881. * \return The IP header checksum of the IP header in the uip_buf
  1882. * buffer.
  1883. */
  1884. u16_t uip_ipchksum(void);
  1885. /**
  1886. * Calculate the TCP checksum of the packet in uip_buf and uip_appdata.
  1887. *
  1888. * The TCP checksum is the Internet checksum of data contents of the
  1889. * TCP segment, and a pseudo-header as defined in RFC793.
  1890. *
  1891. * \return The TCP checksum of the TCP segment in uip_buf and pointed
  1892. * to by uip_appdata.
  1893. */
  1894. u16_t uip_tcpchksum(void);
  1895. /**
  1896. * Calculate the UDP checksum of the packet in uip_buf and uip_appdata.
  1897. *
  1898. * The UDP checksum is the Internet checksum of data contents of the
  1899. * UDP segment, and a pseudo-header as defined in RFC768.
  1900. *
  1901. * \return The UDP checksum of the UDP segment in uip_buf and pointed
  1902. * to by uip_appdata.
  1903. */
  1904. u16_t uip_udpchksum(void);
  1905. /**
  1906. * Calculate the ICMP checksum of the packet in uip_buf.
  1907. *
  1908. * \return The ICMP checksum of the ICMP packet in uip_buf
  1909. */
  1910. u16_t uip_icmp6chksum(void);
  1911. #endif /* __UIP_H__ */
  1912. /** @} */