sockets.c 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852
  1. /**
  2. * @file
  3. * Sockets BSD-Like API module
  4. *
  5. * @defgroup socket Socket API
  6. * @ingroup sequential_api
  7. * BSD-style socket API.\n
  8. * Thread-safe, to be called from non-TCPIP threads only.\n
  9. * Can be activated by defining @ref LWIP_SOCKET to 1.\n
  10. * Header is in posix/sys/socket.h\b
  11. */
  12. /*
  13. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without modification,
  17. * are permitted provided that the following conditions are met:
  18. *
  19. * 1. Redistributions of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. The name of the author may not be used to endorse or promote products
  25. * derived from this software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  28. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  29. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  30. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  31. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  32. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  35. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  36. * OF SUCH DAMAGE.
  37. *
  38. * This file is part of the lwIP TCP/IP stack.
  39. *
  40. * Author: Adam Dunkels <adam@sics.se>
  41. *
  42. * Improved by Marc Boucher <marc@mbsi.ca> and David Haas <dhaas@alum.rpi.edu>
  43. *
  44. */
  45. #include "lwip/opt.h"
  46. #if LWIP_SOCKET /* don't build if not configured for use in lwipopts.h */
  47. #include "lwip/sockets.h"
  48. #include "lwip/api.h"
  49. #include "lwip/sys.h"
  50. #include "lwip/igmp.h"
  51. #include "lwip/inet.h"
  52. #include "lwip/tcp.h"
  53. #include "lwip/raw.h"
  54. #include "lwip/udp.h"
  55. #include "lwip/memp.h"
  56. #include "lwip/pbuf.h"
  57. #include "lwip/priv/tcpip_priv.h"
  58. #if LWIP_CHECKSUM_ON_COPY
  59. #include "lwip/inet_chksum.h"
  60. #endif
  61. #include <string.h>
  62. /* If the netconn API is not required publicly, then we include the necessary
  63. files here to get the implementation */
  64. #if !LWIP_NETCONN
  65. #undef LWIP_NETCONN
  66. #define LWIP_NETCONN 1
  67. #include "api_msg.c"
  68. #include "api_lib.c"
  69. #include "netbuf.c"
  70. #undef LWIP_NETCONN
  71. #define LWIP_NETCONN 0
  72. #endif
  73. #if LWIP_IPV4
  74. #define IP4ADDR_PORT_TO_SOCKADDR(sin, ipaddr, port) do { \
  75. (sin)->sin_len = sizeof(struct sockaddr_in); \
  76. (sin)->sin_family = AF_INET; \
  77. (sin)->sin_port = lwip_htons((port)); \
  78. inet_addr_from_ip4addr(&(sin)->sin_addr, ipaddr); \
  79. memset((sin)->sin_zero, 0, SIN_ZERO_LEN); }while(0)
  80. #define SOCKADDR4_TO_IP4ADDR_PORT(sin, ipaddr, port) do { \
  81. inet_addr_to_ip4addr(ip_2_ip4(ipaddr), &((sin)->sin_addr)); \
  82. (port) = lwip_ntohs((sin)->sin_port); }while(0)
  83. #endif /* LWIP_IPV4 */
  84. #if LWIP_IPV6
  85. #define IP6ADDR_PORT_TO_SOCKADDR(sin6, ipaddr, port) do { \
  86. (sin6)->sin6_len = sizeof(struct sockaddr_in6); \
  87. (sin6)->sin6_family = AF_INET6; \
  88. (sin6)->sin6_port = lwip_htons((port)); \
  89. (sin6)->sin6_flowinfo = 0; \
  90. inet6_addr_from_ip6addr(&(sin6)->sin6_addr, ipaddr); \
  91. (sin6)->sin6_scope_id = 0; }while(0)
  92. #define SOCKADDR6_TO_IP6ADDR_PORT(sin6, ipaddr, port) do { \
  93. inet6_addr_to_ip6addr(ip_2_ip6(ipaddr), &((sin6)->sin6_addr)); \
  94. (port) = lwip_ntohs((sin6)->sin6_port); }while(0)
  95. #endif /* LWIP_IPV6 */
  96. #if LWIP_IPV4 && LWIP_IPV6
  97. static void sockaddr_to_ipaddr_port(const struct sockaddr* sockaddr, ip_addr_t* ipaddr, u16_t* port);
  98. #define IS_SOCK_ADDR_LEN_VALID(namelen) (((namelen) == sizeof(struct sockaddr_in)) || \
  99. ((namelen) == sizeof(struct sockaddr_in6)))
  100. #define IS_SOCK_ADDR_TYPE_VALID(name) (((name)->sa_family == AF_INET) || \
  101. ((name)->sa_family == AF_INET6))
  102. #define SOCK_ADDR_TYPE_MATCH(name, sock) \
  103. ((((name)->sa_family == AF_INET) && !(NETCONNTYPE_ISIPV6((sock)->conn->type))) || \
  104. (((name)->sa_family == AF_INET6) && (NETCONNTYPE_ISIPV6((sock)->conn->type))))
  105. #define IPADDR_PORT_TO_SOCKADDR(sockaddr, ipaddr, port) do { \
  106. if (IP_IS_V6(ipaddr)) { \
  107. IP6ADDR_PORT_TO_SOCKADDR((struct sockaddr_in6*)(void*)(sockaddr), ip_2_ip6(ipaddr), port); \
  108. } else { \
  109. IP4ADDR_PORT_TO_SOCKADDR((struct sockaddr_in*)(void*)(sockaddr), ip_2_ip4(ipaddr), port); \
  110. } } while(0)
  111. #define SOCKADDR_TO_IPADDR_PORT(sockaddr, ipaddr, port) sockaddr_to_ipaddr_port(sockaddr, ipaddr, &(port))
  112. #define DOMAIN_TO_NETCONN_TYPE(domain, type) (((domain) == AF_INET) ? \
  113. (type) : (enum netconn_type)((type) | NETCONN_TYPE_IPV6))
  114. #elif LWIP_IPV6 /* LWIP_IPV4 && LWIP_IPV6 */
  115. #define IS_SOCK_ADDR_LEN_VALID(namelen) ((namelen) == sizeof(struct sockaddr_in6))
  116. #define IS_SOCK_ADDR_TYPE_VALID(name) ((name)->sa_family == AF_INET6)
  117. #define SOCK_ADDR_TYPE_MATCH(name, sock) 1
  118. #define IPADDR_PORT_TO_SOCKADDR(sockaddr, ipaddr, port) \
  119. IP6ADDR_PORT_TO_SOCKADDR((struct sockaddr_in6*)(void*)(sockaddr), ip_2_ip6(ipaddr), port)
  120. #define SOCKADDR_TO_IPADDR_PORT(sockaddr, ipaddr, port) \
  121. SOCKADDR6_TO_IP6ADDR_PORT((const struct sockaddr_in6*)(const void*)(sockaddr), ipaddr, port)
  122. #define DOMAIN_TO_NETCONN_TYPE(domain, netconn_type) (netconn_type)
  123. #else /*-> LWIP_IPV4: LWIP_IPV4 && LWIP_IPV6 */
  124. #define IS_SOCK_ADDR_LEN_VALID(namelen) ((namelen) == sizeof(struct sockaddr_in))
  125. #define IS_SOCK_ADDR_TYPE_VALID(name) ((name)->sa_family == AF_INET)
  126. #define SOCK_ADDR_TYPE_MATCH(name, sock) 1
  127. #define IPADDR_PORT_TO_SOCKADDR(sockaddr, ipaddr, port) \
  128. IP4ADDR_PORT_TO_SOCKADDR((struct sockaddr_in*)(void*)(sockaddr), ip_2_ip4(ipaddr), port)
  129. #define SOCKADDR_TO_IPADDR_PORT(sockaddr, ipaddr, port) \
  130. SOCKADDR4_TO_IP4ADDR_PORT((const struct sockaddr_in*)(const void*)(sockaddr), ipaddr, port)
  131. #define DOMAIN_TO_NETCONN_TYPE(domain, netconn_type) (netconn_type)
  132. #endif /* LWIP_IPV6 */
  133. #define IS_SOCK_ADDR_TYPE_VALID_OR_UNSPEC(name) (((name)->sa_family == AF_UNSPEC) || \
  134. IS_SOCK_ADDR_TYPE_VALID(name))
  135. #define SOCK_ADDR_TYPE_MATCH_OR_UNSPEC(name, sock) (((name)->sa_family == AF_UNSPEC) || \
  136. SOCK_ADDR_TYPE_MATCH(name, sock))
  137. #define IS_SOCK_ADDR_ALIGNED(name) ((((mem_ptr_t)(name)) % 4) == 0)
  138. #define LWIP_SOCKOPT_CHECK_OPTLEN(optlen, opttype) do { if ((optlen) < sizeof(opttype)) { return EINVAL; }}while(0)
  139. #define LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, optlen, opttype) do { \
  140. LWIP_SOCKOPT_CHECK_OPTLEN(optlen, opttype); \
  141. if ((sock)->conn == NULL) { return EINVAL; } }while(0)
  142. #define LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, opttype) do { \
  143. LWIP_SOCKOPT_CHECK_OPTLEN(optlen, opttype); \
  144. if (((sock)->conn == NULL) || ((sock)->conn->pcb.tcp == NULL)) { return EINVAL; } }while(0)
  145. #define LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, opttype, netconntype) do { \
  146. LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, opttype); \
  147. if (NETCONNTYPE_GROUP(netconn_type((sock)->conn)) != netconntype) { return ENOPROTOOPT; } }while(0)
  148. #define LWIP_SETGETSOCKOPT_DATA_VAR_REF(name) API_VAR_REF(name)
  149. #define LWIP_SETGETSOCKOPT_DATA_VAR_DECLARE(name) API_VAR_DECLARE(struct lwip_setgetsockopt_data, name)
  150. #define LWIP_SETGETSOCKOPT_DATA_VAR_FREE(name) API_VAR_FREE(MEMP_SOCKET_SETGETSOCKOPT_DATA, name)
  151. #if LWIP_MPU_COMPATIBLE
  152. #define LWIP_SETGETSOCKOPT_DATA_VAR_ALLOC(name, sock) do { \
  153. name = (struct lwip_setgetsockopt_data *)memp_malloc(MEMP_SOCKET_SETGETSOCKOPT_DATA); \
  154. if (name == NULL) { \
  155. sock_set_errno(sock, ENOMEM); \
  156. return -1; \
  157. } }while(0)
  158. #else /* LWIP_MPU_COMPATIBLE */
  159. #define LWIP_SETGETSOCKOPT_DATA_VAR_ALLOC(name, sock)
  160. #endif /* LWIP_MPU_COMPATIBLE */
  161. #if LWIP_SO_SNDRCVTIMEO_NONSTANDARD
  162. #define LWIP_SO_SNDRCVTIMEO_OPTTYPE int
  163. #define LWIP_SO_SNDRCVTIMEO_SET(optval, val) (*(int *)(optval) = (val))
  164. #define LWIP_SO_SNDRCVTIMEO_GET_MS(optval) ((s32_t)*(const int*)(optval))
  165. #else
  166. #define LWIP_SO_SNDRCVTIMEO_OPTTYPE struct timeval
  167. #define LWIP_SO_SNDRCVTIMEO_SET(optval, val) do { \
  168. s32_t loc = (val); \
  169. ((struct timeval *)(optval))->tv_sec = (loc) / 1000U; \
  170. ((struct timeval *)(optval))->tv_usec = ((loc) % 1000U) * 1000U; }while(0)
  171. #define LWIP_SO_SNDRCVTIMEO_GET_MS(optval) ((((const struct timeval *)(optval))->tv_sec * 1000U) + (((const struct timeval *)(optval))->tv_usec / 1000U))
  172. #endif
  173. #define NUM_SOCKETS MEMP_NUM_NETCONN
  174. /** This is overridable for the rare case where more than 255 threads
  175. * select on the same socket...
  176. */
  177. #ifndef SELWAIT_T
  178. #define SELWAIT_T u8_t
  179. #endif
  180. #include <rtthread.h>
  181. #ifdef SAL_USING_POSIX
  182. #include <ipc/waitqueue.h>
  183. #endif
  184. /** Contains all internal pointers and states used for a socket */
  185. struct lwip_sock {
  186. /** sockets currently are built on netconns, each socket has one netconn */
  187. struct netconn *conn;
  188. /** data that was left from the previous read */
  189. void *lastdata;
  190. /** offset in the data that was left from the previous read */
  191. u16_t lastoffset;
  192. /** number of times data was received, set by event_callback(),
  193. tested by the receive and select functions */
  194. s16_t rcvevent;
  195. /** number of times data was ACKed (free send buffer), set by event_callback(),
  196. tested by select */
  197. u16_t sendevent;
  198. /** error happened for this socket, set by event_callback(), tested by select */
  199. u16_t errevent;
  200. /** last error that occurred on this socket (in fact, all our errnos fit into an u8_t) */
  201. u8_t err;
  202. /** counter of how many threads are waiting for this socket using select */
  203. SELWAIT_T select_waiting;
  204. #ifdef SAL_USING_POSIX
  205. rt_wqueue_t wait_head;
  206. #endif
  207. };
  208. #if LWIP_NETCONN_SEM_PER_THREAD
  209. #define SELECT_SEM_T sys_sem_t*
  210. #define SELECT_SEM_PTR(sem) (sem)
  211. #else /* LWIP_NETCONN_SEM_PER_THREAD */
  212. #define SELECT_SEM_T sys_sem_t
  213. #define SELECT_SEM_PTR(sem) (&(sem))
  214. #endif /* LWIP_NETCONN_SEM_PER_THREAD */
  215. /** Description for a task waiting in select */
  216. struct lwip_select_cb {
  217. /** Pointer to the next waiting task */
  218. struct lwip_select_cb *next;
  219. /** Pointer to the previous waiting task */
  220. struct lwip_select_cb *prev;
  221. /** readset passed to select */
  222. fd_set *readset;
  223. /** writeset passed to select */
  224. fd_set *writeset;
  225. /** unimplemented: exceptset passed to select */
  226. fd_set *exceptset;
  227. /** don't signal the same semaphore twice: set to 1 when signalled */
  228. int sem_signalled;
  229. /** semaphore to wake up a task waiting for select */
  230. SELECT_SEM_T sem;
  231. };
  232. /** A struct sockaddr replacement that has the same alignment as sockaddr_in/
  233. * sockaddr_in6 if instantiated.
  234. */
  235. union sockaddr_aligned {
  236. struct sockaddr sa;
  237. #if LWIP_IPV6
  238. struct sockaddr_in6 sin6;
  239. #endif /* LWIP_IPV6 */
  240. #if LWIP_IPV4
  241. struct sockaddr_in sin;
  242. #endif /* LWIP_IPV4 */
  243. };
  244. #if LWIP_IGMP
  245. /* Define the number of IPv4 multicast memberships, default is one per socket */
  246. #ifndef LWIP_SOCKET_MAX_MEMBERSHIPS
  247. #define LWIP_SOCKET_MAX_MEMBERSHIPS NUM_SOCKETS
  248. #endif
  249. /* This is to keep track of IP_ADD_MEMBERSHIP calls to drop the membership when
  250. a socket is closed */
  251. struct lwip_socket_multicast_pair {
  252. /** the socket */
  253. struct lwip_sock* sock;
  254. /** the interface address */
  255. ip4_addr_t if_addr;
  256. /** the group address */
  257. ip4_addr_t multi_addr;
  258. };
  259. struct lwip_socket_multicast_pair socket_ipv4_multicast_memberships[LWIP_SOCKET_MAX_MEMBERSHIPS];
  260. static int lwip_socket_register_membership(int s, const ip4_addr_t *if_addr, const ip4_addr_t *multi_addr);
  261. static void lwip_socket_unregister_membership(int s, const ip4_addr_t *if_addr, const ip4_addr_t *multi_addr);
  262. static void lwip_socket_drop_registered_memberships(int s);
  263. #endif /* LWIP_IGMP */
  264. /** The global array of available sockets */
  265. static struct lwip_sock sockets[NUM_SOCKETS];
  266. /** The global list of tasks waiting for select */
  267. static struct lwip_select_cb *select_cb_list;
  268. /** This counter is increased from lwip_select when the list is changed
  269. and checked in event_callback to see if it has changed. */
  270. static volatile int select_cb_ctr;
  271. #if LWIP_SOCKET_SET_ERRNO
  272. #ifndef set_errno
  273. #define set_errno(err) do { if (err) { errno = (err); } } while(0)
  274. #endif
  275. #else /* LWIP_SOCKET_SET_ERRNO */
  276. #define set_errno(err)
  277. #endif /* LWIP_SOCKET_SET_ERRNO */
  278. #define sock_set_errno(sk, e) do { \
  279. const int sockerr = (e); \
  280. sk->err = (u8_t)sockerr; \
  281. set_errno(sockerr); \
  282. } while (0)
  283. /* Forward declaration of some functions */
  284. static void event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len);
  285. #if !LWIP_TCPIP_CORE_LOCKING
  286. static void lwip_getsockopt_callback(void *arg);
  287. static void lwip_setsockopt_callback(void *arg);
  288. #endif
  289. static u8_t lwip_getsockopt_impl(int s, int level, int optname, void *optval, socklen_t *optlen);
  290. static u8_t lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_t optlen);
  291. #if LWIP_IPV4 && LWIP_IPV6
  292. static void
  293. sockaddr_to_ipaddr_port(const struct sockaddr* sockaddr, ip_addr_t* ipaddr, u16_t* port)
  294. {
  295. if ((sockaddr->sa_family) == AF_INET6) {
  296. SOCKADDR6_TO_IP6ADDR_PORT((const struct sockaddr_in6*)(const void*)(sockaddr), ipaddr, *port);
  297. ipaddr->type = IPADDR_TYPE_V6;
  298. } else {
  299. SOCKADDR4_TO_IP4ADDR_PORT((const struct sockaddr_in*)(const void*)(sockaddr), ipaddr, *port);
  300. ipaddr->type = IPADDR_TYPE_V4;
  301. }
  302. }
  303. #endif /* LWIP_IPV4 && LWIP_IPV6 */
  304. /** LWIP_NETCONN_SEM_PER_THREAD==1: initialize thread-local semaphore */
  305. void
  306. lwip_socket_thread_init(void)
  307. {
  308. netconn_thread_init();
  309. }
  310. /** LWIP_NETCONN_SEM_PER_THREAD==1: destroy thread-local semaphore */
  311. void
  312. lwip_socket_thread_cleanup(void)
  313. {
  314. netconn_thread_cleanup();
  315. }
  316. /**
  317. * Map a externally used socket index to the internal socket representation.
  318. *
  319. * @param s externally used socket index
  320. * @return struct lwip_sock for the socket or NULL if not found
  321. */
  322. static struct lwip_sock *
  323. get_socket(int s)
  324. {
  325. struct lwip_sock *sock;
  326. s -= LWIP_SOCKET_OFFSET;
  327. if ((s < 0) || (s >= NUM_SOCKETS)) {
  328. LWIP_DEBUGF(SOCKETS_DEBUG, ("get_socket(%d): invalid\n", s + LWIP_SOCKET_OFFSET));
  329. set_errno(EBADF);
  330. return NULL;
  331. }
  332. sock = &sockets[s];
  333. if (!sock->conn) {
  334. LWIP_DEBUGF(SOCKETS_DEBUG, ("get_socket(%d): not active\n", s + LWIP_SOCKET_OFFSET));
  335. set_errno(EBADF);
  336. return NULL;
  337. }
  338. return sock;
  339. }
  340. /**
  341. * Same as get_socket but doesn't set errno
  342. *
  343. * @param s externally used socket index
  344. * @return struct lwip_sock for the socket or NULL if not found
  345. */
  346. static struct lwip_sock *
  347. tryget_socket(int s)
  348. {
  349. s -= LWIP_SOCKET_OFFSET;
  350. if ((s < 0) || (s >= NUM_SOCKETS)) {
  351. return NULL;
  352. }
  353. if (!sockets[s].conn) {
  354. return NULL;
  355. }
  356. return &sockets[s];
  357. }
  358. /**
  359. * Same as tryget_socket but a global routine.
  360. *
  361. * @param s externally used socket index
  362. * @return struct lwip_sock for the socket or NULL if not found
  363. */
  364. struct lwip_sock *
  365. lwip_tryget_socket(int s)
  366. {
  367. return tryget_socket(s);
  368. }
  369. /**
  370. * Allocate a new socket for a given netconn.
  371. *
  372. * @param newconn the netconn for which to allocate a socket
  373. * @param accepted 1 if socket has been created by accept(),
  374. * 0 if socket has been created by socket()
  375. * @return the index of the new socket; -1 on error
  376. */
  377. static int
  378. alloc_socket(struct netconn *newconn, int accepted)
  379. {
  380. int i;
  381. SYS_ARCH_DECL_PROTECT(lev);
  382. /* allocate a new socket identifier */
  383. for (i = 0; i < NUM_SOCKETS; ++i) {
  384. /* Protect socket array */
  385. SYS_ARCH_PROTECT(lev);
  386. if (!sockets[i].conn && (sockets[i].select_waiting == 0)) {
  387. sockets[i].conn = newconn;
  388. /* The socket is not yet known to anyone, so no need to protect
  389. after having marked it as used. */
  390. SYS_ARCH_UNPROTECT(lev);
  391. sockets[i].lastdata = NULL;
  392. sockets[i].lastoffset = 0;
  393. sockets[i].rcvevent = 0;
  394. /* TCP sendbuf is empty, but the socket is not yet writable until connected
  395. * (unless it has been created by accept()). */
  396. sockets[i].sendevent = (NETCONNTYPE_GROUP(newconn->type) == NETCONN_TCP ? (accepted != 0) : 1);
  397. sockets[i].errevent = 0;
  398. sockets[i].err = 0;
  399. #ifdef SAL_USING_POSIX
  400. rt_wqueue_init(&sockets[i].wait_head);
  401. #endif
  402. return i + LWIP_SOCKET_OFFSET;
  403. }
  404. SYS_ARCH_UNPROTECT(lev);
  405. }
  406. return -1;
  407. }
  408. /** Free a socket. The socket's netconn must have been
  409. * delete before!
  410. *
  411. * @param sock the socket to free
  412. * @param is_tcp != 0 for TCP sockets, used to free lastdata
  413. */
  414. static void
  415. free_socket(struct lwip_sock *sock, int is_tcp)
  416. {
  417. void *lastdata;
  418. lastdata = sock->lastdata;
  419. sock->lastdata = NULL;
  420. sock->lastoffset = 0;
  421. sock->err = 0;
  422. /* Protect socket array */
  423. SYS_ARCH_SET(sock->conn, NULL);
  424. /* don't use 'sock' after this line, as another task might have allocated it */
  425. if (lastdata != NULL) {
  426. if (is_tcp) {
  427. pbuf_free((struct pbuf *)lastdata);
  428. } else {
  429. netbuf_delete((struct netbuf *)lastdata);
  430. }
  431. }
  432. }
  433. /* Below this, the well-known socket functions are implemented.
  434. * Use google.com or opengroup.org to get a good description :-)
  435. *
  436. * Exceptions are documented!
  437. */
  438. int
  439. lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
  440. {
  441. struct lwip_sock *sock, *nsock;
  442. struct netconn *newconn;
  443. ip_addr_t naddr;
  444. u16_t port = 0;
  445. int newsock;
  446. err_t err;
  447. SYS_ARCH_DECL_PROTECT(lev);
  448. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d)...\n", s));
  449. sock = get_socket(s);
  450. if (!sock) {
  451. return -1;
  452. }
  453. if (netconn_is_nonblocking(sock->conn) && (sock->rcvevent <= 0)) {
  454. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d): returning EWOULDBLOCK\n", s));
  455. set_errno(EWOULDBLOCK);
  456. return -1;
  457. }
  458. /* wait for a new connection */
  459. err = netconn_accept(sock->conn, &newconn);
  460. if (err != ERR_OK) {
  461. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d): netconn_acept failed, err=%d\n", s, err));
  462. if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_TCP) {
  463. sock_set_errno(sock, EOPNOTSUPP);
  464. } else if (err == ERR_CLSD) {
  465. sock_set_errno(sock, EINVAL);
  466. } else {
  467. sock_set_errno(sock, err_to_errno(err));
  468. }
  469. return -1;
  470. }
  471. LWIP_ASSERT("newconn != NULL", newconn != NULL);
  472. newsock = alloc_socket(newconn, 1);
  473. if (newsock == -1) {
  474. netconn_delete(newconn);
  475. sock_set_errno(sock, ENFILE);
  476. return -1;
  477. }
  478. LWIP_ASSERT("invalid socket index", (newsock >= LWIP_SOCKET_OFFSET) && (newsock < NUM_SOCKETS + LWIP_SOCKET_OFFSET));
  479. /* RT-Thread has changed callback when using BSD socket API, so remove this assert. */
  480. /* LWIP_ASSERT("newconn->callback == event_callback", newconn->callback == event_callback); */
  481. nsock = &sockets[newsock - LWIP_SOCKET_OFFSET];
  482. /* See event_callback: If data comes in right away after an accept, even
  483. * though the server task might not have created a new socket yet.
  484. * In that case, newconn->socket is counted down (newconn->socket--),
  485. * so nsock->rcvevent is >= 1 here!
  486. */
  487. SYS_ARCH_PROTECT(lev);
  488. nsock->rcvevent += (s16_t)(-1 - newconn->socket);
  489. newconn->socket = newsock;
  490. SYS_ARCH_UNPROTECT(lev);
  491. /* Note that POSIX only requires us to check addr is non-NULL. addrlen must
  492. * not be NULL if addr is valid.
  493. */
  494. if (addr != NULL) {
  495. union sockaddr_aligned tempaddr;
  496. /* get the IP address and port of the remote host */
  497. err = netconn_peer(newconn, &naddr, &port);
  498. if (err != ERR_OK) {
  499. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d): netconn_peer failed, err=%d\n", s, err));
  500. netconn_delete(newconn);
  501. free_socket(nsock, 1);
  502. sock_set_errno(sock, err_to_errno(err));
  503. return -1;
  504. }
  505. LWIP_ASSERT("addr valid but addrlen NULL", addrlen != NULL);
  506. IPADDR_PORT_TO_SOCKADDR(&tempaddr, &naddr, port);
  507. if (*addrlen > tempaddr.sa.sa_len) {
  508. *addrlen = tempaddr.sa.sa_len;
  509. }
  510. MEMCPY(addr, &tempaddr, *addrlen);
  511. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d) returning new sock=%d addr=", s, newsock));
  512. ip_addr_debug_print_val(SOCKETS_DEBUG, naddr);
  513. LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F"\n", port));
  514. } else {
  515. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d) returning new sock=%d", s, newsock));
  516. }
  517. sock_set_errno(sock, 0);
  518. return newsock;
  519. }
  520. int
  521. lwip_bind(int s, const struct sockaddr *name, socklen_t namelen)
  522. {
  523. struct lwip_sock *sock;
  524. ip_addr_t local_addr;
  525. u16_t local_port;
  526. err_t err;
  527. sock = get_socket(s);
  528. if (!sock) {
  529. return -1;
  530. }
  531. if (!SOCK_ADDR_TYPE_MATCH(name, sock)) {
  532. /* sockaddr does not match socket type (IPv4/IPv6) */
  533. sock_set_errno(sock, err_to_errno(ERR_VAL));
  534. return -1;
  535. }
  536. /* check size, family and alignment of 'name' */
  537. LWIP_ERROR("lwip_bind: invalid address", (IS_SOCK_ADDR_LEN_VALID(namelen) &&
  538. IS_SOCK_ADDR_TYPE_VALID(name) && IS_SOCK_ADDR_ALIGNED(name)),
  539. sock_set_errno(sock, err_to_errno(ERR_ARG)); return -1;);
  540. LWIP_UNUSED_ARG(namelen);
  541. SOCKADDR_TO_IPADDR_PORT(name, &local_addr, local_port);
  542. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d, addr=", s));
  543. ip_addr_debug_print_val(SOCKETS_DEBUG, local_addr);
  544. LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F")\n", local_port));
  545. #if LWIP_IPV4 && LWIP_IPV6
  546. /* Dual-stack: Unmap IPv4 mapped IPv6 addresses */
  547. if (IP_IS_V6_VAL(local_addr) && ip6_addr_isipv4mappedipv6(ip_2_ip6(&local_addr))) {
  548. unmap_ipv4_mapped_ipv6(ip_2_ip4(&local_addr), ip_2_ip6(&local_addr));
  549. IP_SET_TYPE_VAL(local_addr, IPADDR_TYPE_V4);
  550. }
  551. #endif /* LWIP_IPV4 && LWIP_IPV6 */
  552. err = netconn_bind(sock->conn, &local_addr, local_port);
  553. if (err != ERR_OK) {
  554. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d) failed, err=%d\n", s, err));
  555. sock_set_errno(sock, err_to_errno(err));
  556. return -1;
  557. }
  558. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d) succeeded\n", s));
  559. sock_set_errno(sock, 0);
  560. return 0;
  561. }
  562. int
  563. lwip_close(int s)
  564. {
  565. struct lwip_sock *sock;
  566. int is_tcp = 0;
  567. err_t err;
  568. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_close(%d)\n", s));
  569. sock = get_socket(s);
  570. if (!sock) {
  571. return -1;
  572. }
  573. if (sock->conn != NULL) {
  574. is_tcp = NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP;
  575. } else {
  576. LWIP_ASSERT("sock->lastdata == NULL", sock->lastdata == NULL);
  577. }
  578. #if LWIP_IGMP
  579. /* drop all possibly joined IGMP memberships */
  580. lwip_socket_drop_registered_memberships(s);
  581. #endif /* LWIP_IGMP */
  582. err = netconn_delete(sock->conn);
  583. if (err != ERR_OK) {
  584. sock_set_errno(sock, err_to_errno(err));
  585. return -1;
  586. }
  587. free_socket(sock, is_tcp);
  588. set_errno(0);
  589. return 0;
  590. }
  591. int
  592. lwip_connect(int s, const struct sockaddr *name, socklen_t namelen)
  593. {
  594. struct lwip_sock *sock;
  595. err_t err;
  596. sock = get_socket(s);
  597. if (!sock) {
  598. return -1;
  599. }
  600. if (!SOCK_ADDR_TYPE_MATCH_OR_UNSPEC(name, sock)) {
  601. /* sockaddr does not match socket type (IPv4/IPv6) */
  602. sock_set_errno(sock, err_to_errno(ERR_VAL));
  603. return -1;
  604. }
  605. LWIP_UNUSED_ARG(namelen);
  606. if (name->sa_family == AF_UNSPEC) {
  607. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, AF_UNSPEC)\n", s));
  608. err = netconn_disconnect(sock->conn);
  609. } else {
  610. ip_addr_t remote_addr;
  611. u16_t remote_port;
  612. /* check size, family and alignment of 'name' */
  613. LWIP_ERROR("lwip_connect: invalid address", IS_SOCK_ADDR_LEN_VALID(namelen) &&
  614. IS_SOCK_ADDR_TYPE_VALID_OR_UNSPEC(name) && IS_SOCK_ADDR_ALIGNED(name),
  615. sock_set_errno(sock, err_to_errno(ERR_ARG)); return -1;);
  616. SOCKADDR_TO_IPADDR_PORT(name, &remote_addr, remote_port);
  617. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, addr=", s));
  618. ip_addr_debug_print_val(SOCKETS_DEBUG, remote_addr);
  619. LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F")\n", remote_port));
  620. #if LWIP_IPV4 && LWIP_IPV6
  621. /* Dual-stack: Unmap IPv4 mapped IPv6 addresses */
  622. if (IP_IS_V6_VAL(remote_addr) && ip6_addr_isipv4mappedipv6(ip_2_ip6(&remote_addr))) {
  623. unmap_ipv4_mapped_ipv6(ip_2_ip4(&remote_addr), ip_2_ip6(&remote_addr));
  624. IP_SET_TYPE_VAL(remote_addr, IPADDR_TYPE_V4);
  625. }
  626. #endif /* LWIP_IPV4 && LWIP_IPV6 */
  627. err = netconn_connect(sock->conn, &remote_addr, remote_port);
  628. }
  629. if (err != ERR_OK) {
  630. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d) failed, err=%d\n", s, err));
  631. sock_set_errno(sock, err_to_errno(err));
  632. return -1;
  633. }
  634. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d) succeeded\n", s));
  635. sock_set_errno(sock, 0);
  636. return 0;
  637. }
  638. /**
  639. * Set a socket into listen mode.
  640. * The socket may not have been used for another connection previously.
  641. *
  642. * @param s the socket to set to listening mode
  643. * @param backlog (ATTENTION: needs TCP_LISTEN_BACKLOG=1)
  644. * @return 0 on success, non-zero on failure
  645. */
  646. int
  647. lwip_listen(int s, int backlog)
  648. {
  649. struct lwip_sock *sock;
  650. err_t err;
  651. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_listen(%d, backlog=%d)\n", s, backlog));
  652. sock = get_socket(s);
  653. if (!sock) {
  654. return -1;
  655. }
  656. /* limit the "backlog" parameter to fit in an u8_t */
  657. backlog = LWIP_MIN(LWIP_MAX(backlog, 0), 0xff);
  658. err = netconn_listen_with_backlog(sock->conn, (u8_t)backlog);
  659. if (err != ERR_OK) {
  660. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_listen(%d) failed, err=%d\n", s, err));
  661. if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_TCP) {
  662. sock_set_errno(sock, EOPNOTSUPP);
  663. return -1;
  664. }
  665. sock_set_errno(sock, err_to_errno(err));
  666. return -1;
  667. }
  668. sock_set_errno(sock, 0);
  669. return 0;
  670. }
  671. int
  672. lwip_recvfrom(int s, void *mem, size_t len, int flags,
  673. struct sockaddr *from, socklen_t *fromlen)
  674. {
  675. struct lwip_sock *sock;
  676. void *buf = NULL;
  677. struct pbuf *p;
  678. u16_t buflen, copylen;
  679. int off = 0;
  680. u8_t done = 0;
  681. err_t err;
  682. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d, %p, %"SZT_F", 0x%x, ..)\n", s, mem, len, flags));
  683. sock = get_socket(s);
  684. if (!sock) {
  685. return -1;
  686. }
  687. do {
  688. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: top while sock->lastdata=%p\n", sock->lastdata));
  689. /* Check if there is data left from the last recv operation. */
  690. if (sock->lastdata) {
  691. buf = sock->lastdata;
  692. } else {
  693. /* If this is non-blocking call, then check first */
  694. if (((flags & MSG_DONTWAIT) || netconn_is_nonblocking(sock->conn)) &&
  695. (sock->rcvevent <= 0)) {
  696. if (off > 0) {
  697. /* already received data, return that */
  698. sock_set_errno(sock, 0);
  699. return off;
  700. }
  701. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): returning EWOULDBLOCK\n", s));
  702. set_errno(EWOULDBLOCK);
  703. return -1;
  704. }
  705. /* No data was left from the previous operation, so we try to get
  706. some from the network. */
  707. if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP) {
  708. err = netconn_recv_tcp_pbuf(sock->conn, (struct pbuf **)&buf);
  709. } else {
  710. err = netconn_recv(sock->conn, (struct netbuf **)&buf);
  711. }
  712. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: netconn_recv err=%d, netbuf=%p\n",
  713. err, buf));
  714. if (err != ERR_OK) {
  715. if (off > 0) {
  716. if (err == ERR_CLSD) {
  717. /* closed but already received data, ensure select gets the FIN, too */
  718. event_callback(sock->conn, NETCONN_EVT_RCVPLUS, 0);
  719. }
  720. /* already received data, return that */
  721. sock_set_errno(sock, 0);
  722. return off;
  723. }
  724. /* We should really do some error checking here. */
  725. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): buf == NULL, error is \"%s\"!\n",
  726. s, lwip_strerr(err)));
  727. sock_set_errno(sock, err_to_errno(err));
  728. if (err == ERR_CLSD) {
  729. return 0;
  730. } else {
  731. return -1;
  732. }
  733. }
  734. LWIP_ASSERT("buf != NULL", buf != NULL);
  735. sock->lastdata = buf;
  736. }
  737. if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP) {
  738. p = (struct pbuf *)buf;
  739. } else {
  740. p = ((struct netbuf *)buf)->p;
  741. }
  742. buflen = p->tot_len;
  743. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: buflen=%"U16_F" len=%"SZT_F" off=%d sock->lastoffset=%"U16_F"\n",
  744. buflen, len, off, sock->lastoffset));
  745. buflen -= sock->lastoffset;
  746. if (len > buflen) {
  747. copylen = buflen;
  748. } else {
  749. copylen = (u16_t)len;
  750. }
  751. /* copy the contents of the received buffer into
  752. the supplied memory pointer mem */
  753. pbuf_copy_partial(p, (u8_t*)mem + off, copylen, sock->lastoffset);
  754. off += copylen;
  755. if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP) {
  756. LWIP_ASSERT("invalid copylen, len would underflow", len >= copylen);
  757. len -= copylen;
  758. if ((len <= 0) ||
  759. (p->flags & PBUF_FLAG_PUSH) ||
  760. (sock->rcvevent <= 0) ||
  761. ((flags & MSG_PEEK) != 0)) {
  762. done = 1;
  763. }
  764. } else {
  765. done = 1;
  766. }
  767. /* Check to see from where the data was.*/
  768. if (done) {
  769. #if !SOCKETS_DEBUG
  770. if (from && fromlen)
  771. #endif /* !SOCKETS_DEBUG */
  772. {
  773. u16_t port;
  774. ip_addr_t tmpaddr;
  775. ip_addr_t *fromaddr;
  776. union sockaddr_aligned saddr;
  777. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): addr=", s));
  778. if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP) {
  779. fromaddr = &tmpaddr;
  780. netconn_getaddr(sock->conn, fromaddr, &port, 0);
  781. } else {
  782. port = netbuf_fromport((struct netbuf *)buf);
  783. fromaddr = netbuf_fromaddr((struct netbuf *)buf);
  784. }
  785. #if LWIP_IPV4 && LWIP_IPV6
  786. /* Dual-stack: Map IPv4 addresses to IPv4 mapped IPv6 */
  787. if (NETCONNTYPE_ISIPV6(netconn_type(sock->conn)) && IP_IS_V4(fromaddr)) {
  788. ip4_2_ipv4_mapped_ipv6(ip_2_ip6(fromaddr), ip_2_ip4(fromaddr));
  789. IP_SET_TYPE(fromaddr, IPADDR_TYPE_V6);
  790. }
  791. #endif /* LWIP_IPV4 && LWIP_IPV6 */
  792. IPADDR_PORT_TO_SOCKADDR(&saddr, fromaddr, port);
  793. ip_addr_debug_print(SOCKETS_DEBUG, fromaddr);
  794. LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F" len=%d\n", port, off));
  795. #if SOCKETS_DEBUG
  796. if (from && fromlen)
  797. #endif /* SOCKETS_DEBUG */
  798. {
  799. if (*fromlen > saddr.sa.sa_len) {
  800. *fromlen = saddr.sa.sa_len;
  801. }
  802. MEMCPY(from, &saddr, *fromlen);
  803. }
  804. }
  805. }
  806. /* If we don't peek the incoming message... */
  807. if ((flags & MSG_PEEK) == 0) {
  808. /* If this is a TCP socket, check if there is data left in the
  809. buffer. If so, it should be saved in the sock structure for next
  810. time around. */
  811. if ((NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP) && (buflen - copylen > 0)) {
  812. sock->lastdata = buf;
  813. sock->lastoffset += copylen;
  814. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: lastdata now netbuf=%p\n", buf));
  815. } else {
  816. sock->lastdata = NULL;
  817. sock->lastoffset = 0;
  818. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: deleting netbuf=%p\n", buf));
  819. if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP) {
  820. pbuf_free((struct pbuf *)buf);
  821. } else {
  822. netbuf_delete((struct netbuf *)buf);
  823. }
  824. buf = NULL;
  825. }
  826. }
  827. } while (!done);
  828. sock_set_errno(sock, 0);
  829. return off;
  830. }
  831. int
  832. lwip_read(int s, void *mem, size_t len)
  833. {
  834. return lwip_recvfrom(s, mem, len, 0, NULL, NULL);
  835. }
  836. int
  837. lwip_recv(int s, void *mem, size_t len, int flags)
  838. {
  839. return lwip_recvfrom(s, mem, len, flags, NULL, NULL);
  840. }
  841. int
  842. lwip_send(int s, const void *data, size_t size, int flags)
  843. {
  844. struct lwip_sock *sock;
  845. err_t err;
  846. u8_t write_flags;
  847. size_t written;
  848. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d, data=%p, size=%"SZT_F", flags=0x%x)\n",
  849. s, data, size, flags));
  850. sock = get_socket(s);
  851. if (!sock) {
  852. return -1;
  853. }
  854. if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_TCP) {
  855. #if (LWIP_UDP || LWIP_RAW)
  856. return lwip_sendto(s, data, size, flags, NULL, 0);
  857. #else /* (LWIP_UDP || LWIP_RAW) */
  858. sock_set_errno(sock, err_to_errno(ERR_ARG));
  859. return -1;
  860. #endif /* (LWIP_UDP || LWIP_RAW) */
  861. }
  862. write_flags = NETCONN_COPY |
  863. ((flags & MSG_MORE) ? NETCONN_MORE : 0) |
  864. ((flags & MSG_DONTWAIT) ? NETCONN_DONTBLOCK : 0);
  865. written = 0;
  866. err = netconn_write_partly(sock->conn, data, size, write_flags, &written);
  867. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d) err=%d written=%"SZT_F"\n", s, err, written));
  868. sock_set_errno(sock, err_to_errno(err));
  869. return (err == ERR_OK ? (int)written : -1);
  870. }
  871. int
  872. lwip_sendmsg(int s, const struct msghdr *msg, int flags)
  873. {
  874. struct lwip_sock *sock;
  875. int i;
  876. #if LWIP_TCP
  877. u8_t write_flags;
  878. size_t written;
  879. #endif
  880. int size = 0;
  881. err_t err = ERR_OK;
  882. sock = get_socket(s);
  883. if (!sock) {
  884. return -1;
  885. }
  886. LWIP_ERROR("lwip_sendmsg: invalid msghdr", msg != NULL,
  887. sock_set_errno(sock, err_to_errno(ERR_ARG)); return -1;);
  888. LWIP_UNUSED_ARG(msg->msg_control);
  889. LWIP_UNUSED_ARG(msg->msg_controllen);
  890. LWIP_UNUSED_ARG(msg->msg_flags);
  891. LWIP_ERROR("lwip_sendmsg: invalid msghdr iov", (msg->msg_iov != NULL && msg->msg_iovlen != 0),
  892. sock_set_errno(sock, err_to_errno(ERR_ARG)); return -1;);
  893. if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP) {
  894. #if LWIP_TCP
  895. write_flags = NETCONN_COPY |
  896. ((flags & MSG_MORE) ? NETCONN_MORE : 0) |
  897. ((flags & MSG_DONTWAIT) ? NETCONN_DONTBLOCK : 0);
  898. for (i = 0; i < msg->msg_iovlen; i++) {
  899. u8_t apiflags = write_flags;
  900. if (i + 1 < msg->msg_iovlen) {
  901. apiflags |= NETCONN_MORE;
  902. }
  903. written = 0;
  904. err = netconn_write_partly(sock->conn, msg->msg_iov[i].iov_base, msg->msg_iov[i].iov_len, write_flags, &written);
  905. if (err == ERR_OK) {
  906. size += written;
  907. /* check that the entire IO vector was accepected, if not return a partial write */
  908. if (written != msg->msg_iov[i].iov_len)
  909. break;
  910. }
  911. /* none of this IO vector was accepted, but previous was, return partial write and conceal ERR_WOULDBLOCK */
  912. else if (err == ERR_WOULDBLOCK && size > 0) {
  913. err = ERR_OK;
  914. /* let ERR_WOULDBLOCK persist on the netconn since we are returning ERR_OK */
  915. break;
  916. } else {
  917. size = -1;
  918. break;
  919. }
  920. }
  921. sock_set_errno(sock, err_to_errno(err));
  922. return size;
  923. #else /* LWIP_TCP */
  924. sock_set_errno(sock, err_to_errno(ERR_ARG));
  925. return -1;
  926. #endif /* LWIP_TCP */
  927. }
  928. /* else, UDP and RAW NETCONNs */
  929. #if LWIP_UDP || LWIP_RAW
  930. {
  931. struct netbuf *chain_buf;
  932. LWIP_UNUSED_ARG(flags);
  933. LWIP_ERROR("lwip_sendmsg: invalid msghdr name", (((msg->msg_name == NULL) && (msg->msg_namelen == 0)) ||
  934. IS_SOCK_ADDR_LEN_VALID(msg->msg_namelen)) ,
  935. sock_set_errno(sock, err_to_errno(ERR_ARG)); return -1;);
  936. /* initialize chain buffer with destination */
  937. chain_buf = netbuf_new();
  938. if (!chain_buf) {
  939. sock_set_errno(sock, err_to_errno(ERR_MEM));
  940. return -1;
  941. }
  942. if (msg->msg_name) {
  943. u16_t remote_port;
  944. SOCKADDR_TO_IPADDR_PORT((const struct sockaddr *)msg->msg_name, &chain_buf->addr, remote_port);
  945. netbuf_fromport(chain_buf) = remote_port;
  946. }
  947. #if LWIP_NETIF_TX_SINGLE_PBUF
  948. for (i = 0; i < msg->msg_iovlen; i++) {
  949. size += msg->msg_iov[i].iov_len;
  950. }
  951. /* Allocate a new netbuf and copy the data into it. */
  952. if (netbuf_alloc(chain_buf, (u16_t)size) == NULL) {
  953. err = ERR_MEM;
  954. } else {
  955. /* flatten the IO vectors */
  956. size_t offset = 0;
  957. for (i = 0; i < msg->msg_iovlen; i++) {
  958. MEMCPY(&((u8_t*)chain_buf->p->payload)[offset], msg->msg_iov[i].iov_base, msg->msg_iov[i].iov_len);
  959. offset += msg->msg_iov[i].iov_len;
  960. }
  961. #if LWIP_CHECKSUM_ON_COPY
  962. {
  963. /* This can be improved by using LWIP_CHKSUM_COPY() and aggregating the checksum for each IO vector */
  964. u16_t chksum = ~inet_chksum_pbuf(chain_buf->p);
  965. netbuf_set_chksum(chain_buf, chksum);
  966. }
  967. #endif /* LWIP_CHECKSUM_ON_COPY */
  968. err = ERR_OK;
  969. }
  970. #else /* LWIP_NETIF_TX_SINGLE_PBUF */
  971. /* create a chained netbuf from the IO vectors. NOTE: we assemble a pbuf chain
  972. manually to avoid having to allocate, chain, and delete a netbuf for each iov */
  973. for (i = 0; i < msg->msg_iovlen; i++) {
  974. struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF);
  975. if (p == NULL) {
  976. err = ERR_MEM; /* let netbuf_delete() cleanup chain_buf */
  977. break;
  978. }
  979. p->payload = msg->msg_iov[i].iov_base;
  980. LWIP_ASSERT("iov_len < u16_t", msg->msg_iov[i].iov_len <= 0xFFFF);
  981. p->len = p->tot_len = (u16_t)msg->msg_iov[i].iov_len;
  982. /* netbuf empty, add new pbuf */
  983. if (chain_buf->p == NULL) {
  984. chain_buf->p = chain_buf->ptr = p;
  985. /* add pbuf to existing pbuf chain */
  986. } else {
  987. pbuf_cat(chain_buf->p, p);
  988. }
  989. }
  990. /* save size of total chain */
  991. if (err == ERR_OK) {
  992. size = netbuf_len(chain_buf);
  993. }
  994. #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
  995. if (err == ERR_OK) {
  996. #if LWIP_IPV4 && LWIP_IPV6
  997. /* Dual-stack: Unmap IPv4 mapped IPv6 addresses */
  998. if (IP_IS_V6_VAL(chain_buf->addr) && ip6_addr_isipv4mappedipv6(ip_2_ip6(&chain_buf->addr))) {
  999. unmap_ipv4_mapped_ipv6(ip_2_ip4(&chain_buf->addr), ip_2_ip6(&chain_buf->addr));
  1000. IP_SET_TYPE_VAL(chain_buf->addr, IPADDR_TYPE_V4);
  1001. }
  1002. #endif /* LWIP_IPV4 && LWIP_IPV6 */
  1003. /* send the data */
  1004. err = netconn_send(sock->conn, chain_buf);
  1005. }
  1006. /* deallocated the buffer */
  1007. netbuf_delete(chain_buf);
  1008. sock_set_errno(sock, err_to_errno(err));
  1009. return (err == ERR_OK ? size : -1);
  1010. }
  1011. #else /* LWIP_UDP || LWIP_RAW */
  1012. sock_set_errno(sock, err_to_errno(ERR_ARG));
  1013. return -1;
  1014. #endif /* LWIP_UDP || LWIP_RAW */
  1015. }
  1016. int
  1017. lwip_sendto(int s, const void *data, size_t size, int flags,
  1018. const struct sockaddr *to, socklen_t tolen)
  1019. {
  1020. struct lwip_sock *sock;
  1021. err_t err;
  1022. u16_t short_size;
  1023. u16_t remote_port;
  1024. struct netbuf buf;
  1025. sock = get_socket(s);
  1026. if (!sock) {
  1027. return -1;
  1028. }
  1029. if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP) {
  1030. #if LWIP_TCP
  1031. return lwip_send(s, data, size, flags);
  1032. #else /* LWIP_TCP */
  1033. LWIP_UNUSED_ARG(flags);
  1034. sock_set_errno(sock, err_to_errno(ERR_ARG));
  1035. return -1;
  1036. #endif /* LWIP_TCP */
  1037. }
  1038. /* @todo: split into multiple sendto's? */
  1039. LWIP_ASSERT("lwip_sendto: size must fit in u16_t", size <= 0xffff);
  1040. short_size = (u16_t)size;
  1041. LWIP_ERROR("lwip_sendto: invalid address", (((to == NULL) && (tolen == 0)) ||
  1042. (IS_SOCK_ADDR_LEN_VALID(tolen) &&
  1043. IS_SOCK_ADDR_TYPE_VALID(to) && IS_SOCK_ADDR_ALIGNED(to))),
  1044. sock_set_errno(sock, err_to_errno(ERR_ARG)); return -1;);
  1045. LWIP_UNUSED_ARG(tolen);
  1046. /* initialize a buffer */
  1047. buf.p = buf.ptr = NULL;
  1048. #if LWIP_CHECKSUM_ON_COPY
  1049. buf.flags = 0;
  1050. #endif /* LWIP_CHECKSUM_ON_COPY */
  1051. if (to) {
  1052. SOCKADDR_TO_IPADDR_PORT(to, &buf.addr, remote_port);
  1053. } else {
  1054. remote_port = 0;
  1055. ip_addr_set_any(NETCONNTYPE_ISIPV6(netconn_type(sock->conn)), &buf.addr);
  1056. }
  1057. netbuf_fromport(&buf) = remote_port;
  1058. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_sendto(%d, data=%p, short_size=%"U16_F", flags=0x%x to=",
  1059. s, data, short_size, flags));
  1060. ip_addr_debug_print(SOCKETS_DEBUG, &buf.addr);
  1061. LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F"\n", remote_port));
  1062. /* make the buffer point to the data that should be sent */
  1063. #if LWIP_NETIF_TX_SINGLE_PBUF
  1064. /* Allocate a new netbuf and copy the data into it. */
  1065. if (netbuf_alloc(&buf, short_size) == NULL) {
  1066. err = ERR_MEM;
  1067. } else {
  1068. #if LWIP_CHECKSUM_ON_COPY
  1069. if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_RAW) {
  1070. u16_t chksum = LWIP_CHKSUM_COPY(buf.p->payload, data, short_size);
  1071. netbuf_set_chksum(&buf, chksum);
  1072. } else
  1073. #endif /* LWIP_CHECKSUM_ON_COPY */
  1074. {
  1075. MEMCPY(buf.p->payload, data, short_size);
  1076. }
  1077. err = ERR_OK;
  1078. }
  1079. #else /* LWIP_NETIF_TX_SINGLE_PBUF */
  1080. err = netbuf_ref(&buf, data, short_size);
  1081. #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
  1082. if (err == ERR_OK) {
  1083. #if LWIP_IPV4 && LWIP_IPV6
  1084. /* Dual-stack: Unmap IPv4 mapped IPv6 addresses */
  1085. if (IP_IS_V6_VAL(buf.addr) && ip6_addr_isipv4mappedipv6(ip_2_ip6(&buf.addr))) {
  1086. unmap_ipv4_mapped_ipv6(ip_2_ip4(&buf.addr), ip_2_ip6(&buf.addr));
  1087. IP_SET_TYPE_VAL(buf.addr, IPADDR_TYPE_V4);
  1088. }
  1089. #endif /* LWIP_IPV4 && LWIP_IPV6 */
  1090. /* send the data */
  1091. err = netconn_send(sock->conn, &buf);
  1092. }
  1093. /* deallocated the buffer */
  1094. netbuf_free(&buf);
  1095. sock_set_errno(sock, err_to_errno(err));
  1096. return (err == ERR_OK ? short_size : -1);
  1097. }
  1098. int
  1099. lwip_socket(int domain, int type, int protocol)
  1100. {
  1101. struct netconn *conn;
  1102. int i;
  1103. LWIP_UNUSED_ARG(domain); /* @todo: check this */
  1104. /* create a netconn */
  1105. switch (type) {
  1106. case SOCK_RAW:
  1107. conn = netconn_new_with_proto_and_callback(DOMAIN_TO_NETCONN_TYPE(domain, NETCONN_RAW),
  1108. (u8_t)protocol, event_callback);
  1109. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_RAW, %d) = ",
  1110. domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
  1111. break;
  1112. case SOCK_DGRAM:
  1113. conn = netconn_new_with_callback(DOMAIN_TO_NETCONN_TYPE(domain,
  1114. ((protocol == IPPROTO_UDPLITE) ? NETCONN_UDPLITE : NETCONN_UDP)) ,
  1115. event_callback);
  1116. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_DGRAM, %d) = ",
  1117. domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
  1118. break;
  1119. case SOCK_STREAM:
  1120. conn = netconn_new_with_callback(DOMAIN_TO_NETCONN_TYPE(domain, NETCONN_TCP), event_callback);
  1121. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_STREAM, %d) = ",
  1122. domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
  1123. break;
  1124. default:
  1125. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%d, %d/UNKNOWN, %d) = -1\n",
  1126. domain, type, protocol));
  1127. set_errno(EINVAL);
  1128. return -1;
  1129. }
  1130. if (!conn) {
  1131. LWIP_DEBUGF(SOCKETS_DEBUG, ("-1 / ENOBUFS (could not create netconn)\n"));
  1132. set_errno(ENOBUFS);
  1133. return -1;
  1134. }
  1135. i = alloc_socket(conn, 0);
  1136. if (i == -1) {
  1137. netconn_delete(conn);
  1138. set_errno(ENFILE);
  1139. return -1;
  1140. }
  1141. conn->socket = i;
  1142. LWIP_DEBUGF(SOCKETS_DEBUG, ("%d\n", i));
  1143. set_errno(0);
  1144. return i;
  1145. }
  1146. int
  1147. lwip_write(int s, const void *data, size_t size)
  1148. {
  1149. return lwip_send(s, data, size, 0);
  1150. }
  1151. int
  1152. lwip_writev(int s, const struct iovec *iov, int iovcnt)
  1153. {
  1154. struct msghdr msg;
  1155. msg.msg_name = NULL;
  1156. msg.msg_namelen = 0;
  1157. /* Hack: we have to cast via number to cast from 'const' pointer to non-const.
  1158. Blame the opengroup standard for this inconsistency. */
  1159. msg.msg_iov = LWIP_CONST_CAST(struct iovec *, iov);
  1160. msg.msg_iovlen = iovcnt;
  1161. msg.msg_control = NULL;
  1162. msg.msg_controllen = 0;
  1163. msg.msg_flags = 0;
  1164. return lwip_sendmsg(s, &msg, 0);
  1165. }
  1166. /**
  1167. * Go through the readset and writeset lists and see which socket of the sockets
  1168. * set in the sets has events. On return, readset, writeset and exceptset have
  1169. * the sockets enabled that had events.
  1170. *
  1171. * @param maxfdp1 the highest socket index in the sets
  1172. * @param readset_in set of sockets to check for read events
  1173. * @param writeset_in set of sockets to check for write events
  1174. * @param exceptset_in set of sockets to check for error events
  1175. * @param readset_out set of sockets that had read events
  1176. * @param writeset_out set of sockets that had write events
  1177. * @param exceptset_out set os sockets that had error events
  1178. * @return number of sockets that had events (read/write/exception) (>= 0)
  1179. */
  1180. static int
  1181. lwip_selscan(int maxfdp1, fd_set *readset_in, fd_set *writeset_in, fd_set *exceptset_in,
  1182. fd_set *readset_out, fd_set *writeset_out, fd_set *exceptset_out)
  1183. {
  1184. int i, nready = 0;
  1185. fd_set lreadset, lwriteset, lexceptset;
  1186. struct lwip_sock *sock;
  1187. SYS_ARCH_DECL_PROTECT(lev);
  1188. FD_ZERO(&lreadset);
  1189. FD_ZERO(&lwriteset);
  1190. FD_ZERO(&lexceptset);
  1191. /* Go through each socket in each list to count number of sockets which
  1192. currently match */
  1193. for (i = LWIP_SOCKET_OFFSET; i < maxfdp1; i++) {
  1194. /* if this FD is not in the set, continue */
  1195. if (!(readset_in && FD_ISSET(i, readset_in)) &&
  1196. !(writeset_in && FD_ISSET(i, writeset_in)) &&
  1197. !(exceptset_in && FD_ISSET(i, exceptset_in))) {
  1198. continue;
  1199. }
  1200. /* First get the socket's status (protected)... */
  1201. SYS_ARCH_PROTECT(lev);
  1202. sock = tryget_socket(i);
  1203. if (sock != NULL) {
  1204. void* lastdata = sock->lastdata;
  1205. s16_t rcvevent = sock->rcvevent;
  1206. u16_t sendevent = sock->sendevent;
  1207. u16_t errevent = sock->errevent;
  1208. SYS_ARCH_UNPROTECT(lev);
  1209. /* ... then examine it: */
  1210. /* See if netconn of this socket is ready for read */
  1211. if (readset_in && FD_ISSET(i, readset_in) && ((lastdata != NULL) || (rcvevent > 0))) {
  1212. FD_SET(i, &lreadset);
  1213. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_selscan: fd=%d ready for reading\n", i));
  1214. nready++;
  1215. }
  1216. /* See if netconn of this socket is ready for write */
  1217. if (writeset_in && FD_ISSET(i, writeset_in) && (sendevent != 0)) {
  1218. FD_SET(i, &lwriteset);
  1219. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_selscan: fd=%d ready for writing\n", i));
  1220. nready++;
  1221. }
  1222. /* See if netconn of this socket had an error */
  1223. if (exceptset_in && FD_ISSET(i, exceptset_in) && (errevent != 0)) {
  1224. FD_SET(i, &lexceptset);
  1225. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_selscan: fd=%d ready for exception\n", i));
  1226. nready++;
  1227. }
  1228. } else {
  1229. SYS_ARCH_UNPROTECT(lev);
  1230. /* continue on to next FD in list */
  1231. }
  1232. }
  1233. /* copy local sets to the ones provided as arguments */
  1234. *readset_out = lreadset;
  1235. *writeset_out = lwriteset;
  1236. *exceptset_out = lexceptset;
  1237. LWIP_ASSERT("nready >= 0", nready >= 0);
  1238. return nready;
  1239. }
  1240. int
  1241. lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
  1242. struct timeval *timeout)
  1243. {
  1244. u32_t waitres = 0;
  1245. int nready;
  1246. fd_set lreadset, lwriteset, lexceptset;
  1247. u32_t msectimeout;
  1248. struct lwip_select_cb select_cb;
  1249. int i;
  1250. int maxfdp2;
  1251. #if LWIP_NETCONN_SEM_PER_THREAD
  1252. int waited = 0;
  1253. #endif
  1254. SYS_ARCH_DECL_PROTECT(lev);
  1255. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select(%d, %p, %p, %p, tvsec=%"S32_F" tvusec=%"S32_F")\n",
  1256. maxfdp1, (void *)readset, (void *) writeset, (void *) exceptset,
  1257. timeout ? (s32_t)timeout->tv_sec : (s32_t)-1,
  1258. timeout ? (s32_t)timeout->tv_usec : (s32_t)-1));
  1259. /* Go through each socket in each list to count number of sockets which
  1260. currently match */
  1261. nready = lwip_selscan(maxfdp1, readset, writeset, exceptset, &lreadset, &lwriteset, &lexceptset);
  1262. /* If we don't have any current events, then suspend if we are supposed to */
  1263. if (!nready) {
  1264. if (timeout && timeout->tv_sec == 0 && timeout->tv_usec == 0) {
  1265. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: no timeout, returning 0\n"));
  1266. /* This is OK as the local fdsets are empty and nready is zero,
  1267. or we would have returned earlier. */
  1268. goto return_copy_fdsets;
  1269. }
  1270. /* None ready: add our semaphore to list:
  1271. We don't actually need any dynamic memory. Our entry on the
  1272. list is only valid while we are in this function, so it's ok
  1273. to use local variables. */
  1274. select_cb.next = NULL;
  1275. select_cb.prev = NULL;
  1276. select_cb.readset = readset;
  1277. select_cb.writeset = writeset;
  1278. select_cb.exceptset = exceptset;
  1279. select_cb.sem_signalled = 0;
  1280. #if LWIP_NETCONN_SEM_PER_THREAD
  1281. select_cb.sem = LWIP_NETCONN_THREAD_SEM_GET();
  1282. #else /* LWIP_NETCONN_SEM_PER_THREAD */
  1283. if (sys_sem_new(&select_cb.sem, 0) != ERR_OK) {
  1284. /* failed to create semaphore */
  1285. set_errno(ENOMEM);
  1286. return -1;
  1287. }
  1288. #endif /* LWIP_NETCONN_SEM_PER_THREAD */
  1289. /* Protect the select_cb_list */
  1290. SYS_ARCH_PROTECT(lev);
  1291. /* Put this select_cb on top of list */
  1292. select_cb.next = select_cb_list;
  1293. if (select_cb_list != NULL) {
  1294. select_cb_list->prev = &select_cb;
  1295. }
  1296. select_cb_list = &select_cb;
  1297. /* Increasing this counter tells event_callback that the list has changed. */
  1298. select_cb_ctr++;
  1299. /* Now we can safely unprotect */
  1300. SYS_ARCH_UNPROTECT(lev);
  1301. /* Increase select_waiting for each socket we are interested in */
  1302. maxfdp2 = maxfdp1;
  1303. for (i = LWIP_SOCKET_OFFSET; i < maxfdp1; i++) {
  1304. if ((readset && FD_ISSET(i, readset)) ||
  1305. (writeset && FD_ISSET(i, writeset)) ||
  1306. (exceptset && FD_ISSET(i, exceptset))) {
  1307. struct lwip_sock *sock;
  1308. SYS_ARCH_PROTECT(lev);
  1309. sock = tryget_socket(i);
  1310. if (sock != NULL) {
  1311. sock->select_waiting++;
  1312. LWIP_ASSERT("sock->select_waiting > 0", sock->select_waiting > 0);
  1313. } else {
  1314. /* Not a valid socket */
  1315. nready = -1;
  1316. maxfdp2 = i;
  1317. SYS_ARCH_UNPROTECT(lev);
  1318. break;
  1319. }
  1320. SYS_ARCH_UNPROTECT(lev);
  1321. }
  1322. }
  1323. if (nready >= 0) {
  1324. /* Call lwip_selscan again: there could have been events between
  1325. the last scan (without us on the list) and putting us on the list! */
  1326. nready = lwip_selscan(maxfdp1, readset, writeset, exceptset, &lreadset, &lwriteset, &lexceptset);
  1327. if (!nready) {
  1328. /* Still none ready, just wait to be woken */
  1329. if (timeout == 0) {
  1330. /* Wait forever */
  1331. msectimeout = 0;
  1332. } else {
  1333. msectimeout = ((timeout->tv_sec * 1000) + ((timeout->tv_usec + 500)/1000));
  1334. if (msectimeout == 0) {
  1335. /* Wait 1ms at least (0 means wait forever) */
  1336. msectimeout = 1;
  1337. }
  1338. }
  1339. waitres = sys_arch_sem_wait(SELECT_SEM_PTR(select_cb.sem), msectimeout);
  1340. #if LWIP_NETCONN_SEM_PER_THREAD
  1341. waited = 1;
  1342. #endif
  1343. }
  1344. }
  1345. /* Decrease select_waiting for each socket we are interested in */
  1346. for (i = LWIP_SOCKET_OFFSET; i < maxfdp2; i++) {
  1347. if ((readset && FD_ISSET(i, readset)) ||
  1348. (writeset && FD_ISSET(i, writeset)) ||
  1349. (exceptset && FD_ISSET(i, exceptset))) {
  1350. struct lwip_sock *sock;
  1351. SYS_ARCH_PROTECT(lev);
  1352. sock = tryget_socket(i);
  1353. if (sock != NULL) {
  1354. /* for now, handle select_waiting==0... */
  1355. LWIP_ASSERT("sock->select_waiting > 0", sock->select_waiting > 0);
  1356. if (sock->select_waiting > 0) {
  1357. sock->select_waiting--;
  1358. }
  1359. } else {
  1360. /* Not a valid socket */
  1361. nready = -1;
  1362. }
  1363. SYS_ARCH_UNPROTECT(lev);
  1364. }
  1365. }
  1366. /* Take us off the list */
  1367. SYS_ARCH_PROTECT(lev);
  1368. if (select_cb.next != NULL) {
  1369. select_cb.next->prev = select_cb.prev;
  1370. }
  1371. if (select_cb_list == &select_cb) {
  1372. LWIP_ASSERT("select_cb.prev == NULL", select_cb.prev == NULL);
  1373. select_cb_list = select_cb.next;
  1374. } else {
  1375. LWIP_ASSERT("select_cb.prev != NULL", select_cb.prev != NULL);
  1376. select_cb.prev->next = select_cb.next;
  1377. }
  1378. /* Increasing this counter tells event_callback that the list has changed. */
  1379. select_cb_ctr++;
  1380. SYS_ARCH_UNPROTECT(lev);
  1381. #if LWIP_NETCONN_SEM_PER_THREAD
  1382. if (select_cb.sem_signalled && (!waited || (waitres == SYS_ARCH_TIMEOUT))) {
  1383. /* don't leave the thread-local semaphore signalled */
  1384. sys_arch_sem_wait(select_cb.sem, 1);
  1385. }
  1386. #else /* LWIP_NETCONN_SEM_PER_THREAD */
  1387. sys_sem_free(&select_cb.sem);
  1388. #endif /* LWIP_NETCONN_SEM_PER_THREAD */
  1389. if (nready < 0) {
  1390. /* This happens when a socket got closed while waiting */
  1391. set_errno(EBADF);
  1392. return -1;
  1393. }
  1394. if (waitres == SYS_ARCH_TIMEOUT) {
  1395. /* Timeout */
  1396. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: timeout expired\n"));
  1397. /* This is OK as the local fdsets are empty and nready is zero,
  1398. or we would have returned earlier. */
  1399. goto return_copy_fdsets;
  1400. }
  1401. /* See what's set */
  1402. nready = lwip_selscan(maxfdp1, readset, writeset, exceptset, &lreadset, &lwriteset, &lexceptset);
  1403. }
  1404. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: nready=%d\n", nready));
  1405. return_copy_fdsets:
  1406. set_errno(0);
  1407. if (readset) {
  1408. *readset = lreadset;
  1409. }
  1410. if (writeset) {
  1411. *writeset = lwriteset;
  1412. }
  1413. if (exceptset) {
  1414. *exceptset = lexceptset;
  1415. }
  1416. return nready;
  1417. }
  1418. /**
  1419. * Callback registered in the netconn layer for each socket-netconn.
  1420. * Processes recvevent (data available) and wakes up tasks waiting for select.
  1421. */
  1422. static void
  1423. event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len)
  1424. {
  1425. int s;
  1426. struct lwip_sock *sock;
  1427. struct lwip_select_cb *scb;
  1428. int last_select_cb_ctr;
  1429. SYS_ARCH_DECL_PROTECT(lev);
  1430. LWIP_UNUSED_ARG(len);
  1431. /* Get socket */
  1432. if (conn) {
  1433. s = conn->socket;
  1434. if (s < 0) {
  1435. /* Data comes in right away after an accept, even though
  1436. * the server task might not have created a new socket yet.
  1437. * Just count down (or up) if that's the case and we
  1438. * will use the data later. Note that only receive events
  1439. * can happen before the new socket is set up. */
  1440. SYS_ARCH_PROTECT(lev);
  1441. if (conn->socket < 0) {
  1442. if (evt == NETCONN_EVT_RCVPLUS) {
  1443. conn->socket--;
  1444. }
  1445. SYS_ARCH_UNPROTECT(lev);
  1446. return;
  1447. }
  1448. s = conn->socket;
  1449. SYS_ARCH_UNPROTECT(lev);
  1450. }
  1451. sock = get_socket(s);
  1452. if (!sock) {
  1453. return;
  1454. }
  1455. } else {
  1456. return;
  1457. }
  1458. SYS_ARCH_PROTECT(lev);
  1459. /* Set event as required */
  1460. switch (evt) {
  1461. case NETCONN_EVT_RCVPLUS:
  1462. sock->rcvevent++;
  1463. break;
  1464. case NETCONN_EVT_RCVMINUS:
  1465. sock->rcvevent--;
  1466. break;
  1467. case NETCONN_EVT_SENDPLUS:
  1468. sock->sendevent = 1;
  1469. break;
  1470. case NETCONN_EVT_SENDMINUS:
  1471. sock->sendevent = 0;
  1472. break;
  1473. case NETCONN_EVT_ERROR:
  1474. sock->errevent = 1;
  1475. break;
  1476. default:
  1477. LWIP_ASSERT("unknown event", 0);
  1478. break;
  1479. }
  1480. if (sock->select_waiting == 0) {
  1481. /* noone is waiting for this socket, no need to check select_cb_list */
  1482. SYS_ARCH_UNPROTECT(lev);
  1483. return;
  1484. }
  1485. /* Now decide if anyone is waiting for this socket */
  1486. /* NOTE: This code goes through the select_cb_list list multiple times
  1487. ONLY IF a select was actually waiting. We go through the list the number
  1488. of waiting select calls + 1. This list is expected to be small. */
  1489. /* At this point, SYS_ARCH is still protected! */
  1490. again:
  1491. for (scb = select_cb_list; scb != NULL; scb = scb->next) {
  1492. /* remember the state of select_cb_list to detect changes */
  1493. last_select_cb_ctr = select_cb_ctr;
  1494. if (scb->sem_signalled == 0) {
  1495. /* semaphore not signalled yet */
  1496. int do_signal = 0;
  1497. /* Test this select call for our socket */
  1498. if (sock->rcvevent > 0) {
  1499. if (scb->readset && FD_ISSET(s, scb->readset)) {
  1500. do_signal = 1;
  1501. }
  1502. }
  1503. if (sock->sendevent != 0) {
  1504. if (!do_signal && scb->writeset && FD_ISSET(s, scb->writeset)) {
  1505. do_signal = 1;
  1506. }
  1507. }
  1508. if (sock->errevent != 0) {
  1509. if (!do_signal && scb->exceptset && FD_ISSET(s, scb->exceptset)) {
  1510. do_signal = 1;
  1511. }
  1512. }
  1513. if (do_signal) {
  1514. scb->sem_signalled = 1;
  1515. /* Don't call SYS_ARCH_UNPROTECT() before signaling the semaphore, as this might
  1516. lead to the select thread taking itself off the list, invalidating the semaphore. */
  1517. sys_sem_signal(SELECT_SEM_PTR(scb->sem));
  1518. }
  1519. }
  1520. /* unlock interrupts with each step */
  1521. SYS_ARCH_UNPROTECT(lev);
  1522. /* this makes sure interrupt protection time is short */
  1523. SYS_ARCH_PROTECT(lev);
  1524. if (last_select_cb_ctr != select_cb_ctr) {
  1525. /* someone has changed select_cb_list, restart at the beginning */
  1526. goto again;
  1527. }
  1528. }
  1529. SYS_ARCH_UNPROTECT(lev);
  1530. }
  1531. /**
  1532. * Close one end of a full-duplex connection.
  1533. */
  1534. int
  1535. lwip_shutdown(int s, int how)
  1536. {
  1537. struct lwip_sock *sock;
  1538. err_t err;
  1539. u8_t shut_rx = 0, shut_tx = 0;
  1540. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_shutdown(%d, how=%d)\n", s, how));
  1541. sock = get_socket(s);
  1542. if (!sock) {
  1543. return -1;
  1544. }
  1545. if (sock->conn != NULL) {
  1546. if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_TCP) {
  1547. sock_set_errno(sock, EOPNOTSUPP);
  1548. return -1;
  1549. }
  1550. } else {
  1551. sock_set_errno(sock, ENOTCONN);
  1552. return -1;
  1553. }
  1554. if (how == SHUT_RD) {
  1555. shut_rx = 1;
  1556. } else if (how == SHUT_WR) {
  1557. shut_tx = 1;
  1558. } else if (how == SHUT_RDWR) {
  1559. shut_rx = 1;
  1560. shut_tx = 1;
  1561. } else {
  1562. sock_set_errno(sock, EINVAL);
  1563. return -1;
  1564. }
  1565. err = netconn_shutdown(sock->conn, shut_rx, shut_tx);
  1566. sock_set_errno(sock, err_to_errno(err));
  1567. return (err == ERR_OK ? 0 : -1);
  1568. }
  1569. static int
  1570. lwip_getaddrname(int s, struct sockaddr *name, socklen_t *namelen, u8_t local)
  1571. {
  1572. struct lwip_sock *sock;
  1573. union sockaddr_aligned saddr;
  1574. ip_addr_t naddr;
  1575. u16_t port;
  1576. err_t err;
  1577. sock = get_socket(s);
  1578. if (!sock) {
  1579. return -1;
  1580. }
  1581. /* get the IP address and port */
  1582. err = netconn_getaddr(sock->conn, &naddr, &port, local);
  1583. if (err != ERR_OK) {
  1584. sock_set_errno(sock, err_to_errno(err));
  1585. return -1;
  1586. }
  1587. #if LWIP_IPV4 && LWIP_IPV6
  1588. /* Dual-stack: Map IPv4 addresses to IPv4 mapped IPv6 */
  1589. if (NETCONNTYPE_ISIPV6(netconn_type(sock->conn)) &&
  1590. IP_IS_V4_VAL(naddr)) {
  1591. ip4_2_ipv4_mapped_ipv6(ip_2_ip6(&naddr), ip_2_ip4(&naddr));
  1592. IP_SET_TYPE_VAL(naddr, IPADDR_TYPE_V6);
  1593. }
  1594. #endif /* LWIP_IPV4 && LWIP_IPV6 */
  1595. IPADDR_PORT_TO_SOCKADDR(&saddr, &naddr, port);
  1596. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getaddrname(%d, addr=", s));
  1597. ip_addr_debug_print_val(SOCKETS_DEBUG, naddr);
  1598. LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F")\n", port));
  1599. if (*namelen > saddr.sa.sa_len) {
  1600. *namelen = saddr.sa.sa_len;
  1601. }
  1602. MEMCPY(name, &saddr, *namelen);
  1603. sock_set_errno(sock, 0);
  1604. return 0;
  1605. }
  1606. int
  1607. lwip_getpeername(int s, struct sockaddr *name, socklen_t *namelen)
  1608. {
  1609. return lwip_getaddrname(s, name, namelen, 0);
  1610. }
  1611. int
  1612. lwip_getsockname(int s, struct sockaddr *name, socklen_t *namelen)
  1613. {
  1614. return lwip_getaddrname(s, name, namelen, 1);
  1615. }
  1616. int
  1617. lwip_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen)
  1618. {
  1619. u8_t err;
  1620. struct lwip_sock *sock = get_socket(s);
  1621. #if !LWIP_TCPIP_CORE_LOCKING
  1622. LWIP_SETGETSOCKOPT_DATA_VAR_DECLARE(data);
  1623. #endif /* !LWIP_TCPIP_CORE_LOCKING */
  1624. if (!sock) {
  1625. return -1;
  1626. }
  1627. if ((NULL == optval) || (NULL == optlen)) {
  1628. sock_set_errno(sock, EFAULT);
  1629. return -1;
  1630. }
  1631. #if LWIP_TCPIP_CORE_LOCKING
  1632. /* core-locking can just call the -impl function */
  1633. LOCK_TCPIP_CORE();
  1634. err = lwip_getsockopt_impl(s, level, optname, optval, optlen);
  1635. UNLOCK_TCPIP_CORE();
  1636. #else /* LWIP_TCPIP_CORE_LOCKING */
  1637. #if LWIP_MPU_COMPATIBLE
  1638. /* MPU_COMPATIBLE copies the optval data, so check for max size here */
  1639. if (*optlen > LWIP_SETGETSOCKOPT_MAXOPTLEN) {
  1640. sock_set_errno(sock, ENOBUFS);
  1641. return -1;
  1642. }
  1643. #endif /* LWIP_MPU_COMPATIBLE */
  1644. LWIP_SETGETSOCKOPT_DATA_VAR_ALLOC(data, sock);
  1645. LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).s = s;
  1646. LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).level = level;
  1647. LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optname = optname;
  1648. LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optlen = *optlen;
  1649. #if !LWIP_MPU_COMPATIBLE
  1650. LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optval.p = optval;
  1651. #endif /* !LWIP_MPU_COMPATIBLE */
  1652. LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).err = 0;
  1653. #if LWIP_NETCONN_SEM_PER_THREAD
  1654. LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).completed_sem = LWIP_NETCONN_THREAD_SEM_GET();
  1655. #else
  1656. LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).completed_sem = &sock->conn->op_completed;
  1657. #endif
  1658. err = tcpip_callback(lwip_getsockopt_callback, &LWIP_SETGETSOCKOPT_DATA_VAR_REF(data));
  1659. if (err != ERR_OK) {
  1660. LWIP_SETGETSOCKOPT_DATA_VAR_FREE(data);
  1661. sock_set_errno(sock, err_to_errno(err));
  1662. return -1;
  1663. }
  1664. sys_arch_sem_wait((sys_sem_t*)(LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).completed_sem), 0);
  1665. /* write back optlen and optval */
  1666. *optlen = LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optlen;
  1667. #if LWIP_MPU_COMPATIBLE
  1668. MEMCPY(optval, LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optval,
  1669. LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optlen);
  1670. #endif /* LWIP_MPU_COMPATIBLE */
  1671. /* maybe lwip_getsockopt_internal has changed err */
  1672. err = LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).err;
  1673. LWIP_SETGETSOCKOPT_DATA_VAR_FREE(data);
  1674. #endif /* LWIP_TCPIP_CORE_LOCKING */
  1675. sock_set_errno(sock, err);
  1676. return err ? -1 : 0;
  1677. }
  1678. #if !LWIP_TCPIP_CORE_LOCKING
  1679. /** lwip_getsockopt_callback: only used without CORE_LOCKING
  1680. * to get into the tcpip_thread
  1681. */
  1682. static void
  1683. lwip_getsockopt_callback(void *arg)
  1684. {
  1685. struct lwip_setgetsockopt_data *data;
  1686. LWIP_ASSERT("arg != NULL", arg != NULL);
  1687. data = (struct lwip_setgetsockopt_data*)arg;
  1688. data->err = lwip_getsockopt_impl(data->s, data->level, data->optname,
  1689. #if LWIP_MPU_COMPATIBLE
  1690. data->optval,
  1691. #else /* LWIP_MPU_COMPATIBLE */
  1692. data->optval.p,
  1693. #endif /* LWIP_MPU_COMPATIBLE */
  1694. &data->optlen);
  1695. sys_sem_signal((sys_sem_t*)(data->completed_sem));
  1696. }
  1697. #endif /* LWIP_TCPIP_CORE_LOCKING */
  1698. /** lwip_getsockopt_impl: the actual implementation of getsockopt:
  1699. * same argument as lwip_getsockopt, either called directly or through callback
  1700. */
  1701. static u8_t
  1702. lwip_getsockopt_impl(int s, int level, int optname, void *optval, socklen_t *optlen)
  1703. {
  1704. u8_t err = 0;
  1705. struct lwip_sock *sock = tryget_socket(s);
  1706. if (!sock) {
  1707. return EBADF;
  1708. }
  1709. switch (level) {
  1710. /* Level: SOL_SOCKET */
  1711. case SOL_SOCKET:
  1712. switch (optname) {
  1713. #if LWIP_TCP
  1714. case SO_ACCEPTCONN:
  1715. LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, int);
  1716. if (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_TCP) {
  1717. return ENOPROTOOPT;
  1718. }
  1719. if ((sock->conn->pcb.tcp != NULL) && (sock->conn->pcb.tcp->state == LISTEN)) {
  1720. *(int*)optval = 1;
  1721. } else {
  1722. *(int*)optval = 0;
  1723. }
  1724. break;
  1725. #endif /* LWIP_TCP */
  1726. /* The option flags */
  1727. case SO_BROADCAST:
  1728. case SO_KEEPALIVE:
  1729. #if SO_REUSE
  1730. case SO_REUSEADDR:
  1731. #endif /* SO_REUSE */
  1732. LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, int);
  1733. *(int*)optval = ip_get_option(sock->conn->pcb.ip, optname);
  1734. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, optname=0x%x, ..) = %s\n",
  1735. s, optname, (*(int*)optval?"on":"off")));
  1736. break;
  1737. case SO_TYPE:
  1738. LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, *optlen, int);
  1739. switch (NETCONNTYPE_GROUP(netconn_type(sock->conn))) {
  1740. case NETCONN_RAW:
  1741. *(int*)optval = SOCK_RAW;
  1742. break;
  1743. case NETCONN_TCP:
  1744. *(int*)optval = SOCK_STREAM;
  1745. break;
  1746. case NETCONN_UDP:
  1747. *(int*)optval = SOCK_DGRAM;
  1748. break;
  1749. default: /* unrecognized socket type */
  1750. *(int*)optval = netconn_type(sock->conn);
  1751. LWIP_DEBUGF(SOCKETS_DEBUG,
  1752. ("lwip_getsockopt(%d, SOL_SOCKET, SO_TYPE): unrecognized socket type %d\n",
  1753. s, *(int *)optval));
  1754. } /* switch (netconn_type(sock->conn)) */
  1755. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_TYPE) = %d\n",
  1756. s, *(int *)optval));
  1757. break;
  1758. case SO_ERROR:
  1759. LWIP_SOCKOPT_CHECK_OPTLEN(*optlen, int);
  1760. /* only overwrite ERR_OK or temporary errors */
  1761. if (((sock->err == 0) || (sock->err == EINPROGRESS)) && (sock->conn != NULL)) {
  1762. sock_set_errno(sock, err_to_errno(sock->conn->last_err));
  1763. }
  1764. *(int *)optval = (sock->err == 0xFF ? (int)-1 : (int)sock->err);
  1765. sock->err = 0;
  1766. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_ERROR) = %d\n",
  1767. s, *(int *)optval));
  1768. break;
  1769. #if LWIP_SO_SNDTIMEO
  1770. case SO_SNDTIMEO:
  1771. LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, *optlen, LWIP_SO_SNDRCVTIMEO_OPTTYPE);
  1772. LWIP_SO_SNDRCVTIMEO_SET(optval, netconn_get_sendtimeout(sock->conn));
  1773. break;
  1774. #endif /* LWIP_SO_SNDTIMEO */
  1775. #if LWIP_SO_RCVTIMEO
  1776. case SO_RCVTIMEO:
  1777. LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, *optlen, LWIP_SO_SNDRCVTIMEO_OPTTYPE);
  1778. LWIP_SO_SNDRCVTIMEO_SET(optval, netconn_get_recvtimeout(sock->conn));
  1779. break;
  1780. #endif /* LWIP_SO_RCVTIMEO */
  1781. #if LWIP_SO_RCVBUF
  1782. case SO_RCVBUF:
  1783. LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, *optlen, int);
  1784. *(int *)optval = netconn_get_recvbufsize(sock->conn);
  1785. break;
  1786. #endif /* LWIP_SO_RCVBUF */
  1787. #if LWIP_SO_LINGER
  1788. case SO_LINGER:
  1789. {
  1790. s16_t conn_linger;
  1791. struct linger* linger = (struct linger*)optval;
  1792. LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, *optlen, struct linger);
  1793. conn_linger = sock->conn->linger;
  1794. if (conn_linger >= 0) {
  1795. linger->l_onoff = 1;
  1796. linger->l_linger = (int)conn_linger;
  1797. } else {
  1798. linger->l_onoff = 0;
  1799. linger->l_linger = 0;
  1800. }
  1801. }
  1802. break;
  1803. #endif /* LWIP_SO_LINGER */
  1804. #if LWIP_UDP
  1805. case SO_NO_CHECK:
  1806. LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, *optlen, int, NETCONN_UDP);
  1807. #if LWIP_UDPLITE
  1808. if ((udp_flags(sock->conn->pcb.udp) & UDP_FLAGS_UDPLITE) != 0) {
  1809. /* this flag is only available for UDP, not for UDP lite */
  1810. return EAFNOSUPPORT;
  1811. }
  1812. #endif /* LWIP_UDPLITE */
  1813. *(int*)optval = (udp_flags(sock->conn->pcb.udp) & UDP_FLAGS_NOCHKSUM) ? 1 : 0;
  1814. break;
  1815. #endif /* LWIP_UDP*/
  1816. default:
  1817. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, UNIMPL: optname=0x%x, ..)\n",
  1818. s, optname));
  1819. err = ENOPROTOOPT;
  1820. break;
  1821. } /* switch (optname) */
  1822. break;
  1823. /* Level: IPPROTO_IP */
  1824. case IPPROTO_IP:
  1825. switch (optname) {
  1826. case IP_TTL:
  1827. LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, int);
  1828. *(int*)optval = sock->conn->pcb.ip->ttl;
  1829. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_TTL) = %d\n",
  1830. s, *(int *)optval));
  1831. break;
  1832. case IP_TOS:
  1833. LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, int);
  1834. *(int*)optval = sock->conn->pcb.ip->tos;
  1835. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_TOS) = %d\n",
  1836. s, *(int *)optval));
  1837. break;
  1838. #if LWIP_MULTICAST_TX_OPTIONS
  1839. case IP_MULTICAST_TTL:
  1840. LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, u8_t);
  1841. if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_UDP) {
  1842. return ENOPROTOOPT;
  1843. }
  1844. *(u8_t*)optval = udp_get_multicast_ttl(sock->conn->pcb.udp);
  1845. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_MULTICAST_TTL) = %d\n",
  1846. s, *(int *)optval));
  1847. break;
  1848. case IP_MULTICAST_IF:
  1849. LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, struct in_addr);
  1850. if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_UDP) {
  1851. return ENOPROTOOPT;
  1852. }
  1853. inet_addr_from_ip4addr((struct in_addr*)optval, udp_get_multicast_netif_addr(sock->conn->pcb.udp));
  1854. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_MULTICAST_IF) = 0x%"X32_F"\n",
  1855. s, *(u32_t *)optval));
  1856. break;
  1857. case IP_MULTICAST_LOOP:
  1858. LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, u8_t);
  1859. if ((sock->conn->pcb.udp->flags & UDP_FLAGS_MULTICAST_LOOP) != 0) {
  1860. *(u8_t*)optval = 1;
  1861. } else {
  1862. *(u8_t*)optval = 0;
  1863. }
  1864. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_MULTICAST_LOOP) = %d\n",
  1865. s, *(int *)optval));
  1866. break;
  1867. #endif /* LWIP_MULTICAST_TX_OPTIONS */
  1868. default:
  1869. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, UNIMPL: optname=0x%x, ..)\n",
  1870. s, optname));
  1871. err = ENOPROTOOPT;
  1872. break;
  1873. } /* switch (optname) */
  1874. break;
  1875. #if LWIP_TCP
  1876. /* Level: IPPROTO_TCP */
  1877. case IPPROTO_TCP:
  1878. /* Special case: all IPPROTO_TCP option take an int */
  1879. LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, *optlen, int, NETCONN_TCP);
  1880. if (sock->conn->pcb.tcp->state == LISTEN) {
  1881. return EINVAL;
  1882. }
  1883. switch (optname) {
  1884. case TCP_NODELAY:
  1885. *(int*)optval = tcp_nagle_disabled(sock->conn->pcb.tcp);
  1886. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, TCP_NODELAY) = %s\n",
  1887. s, (*(int*)optval)?"on":"off") );
  1888. break;
  1889. case TCP_KEEPALIVE:
  1890. *(int*)optval = (int)sock->conn->pcb.tcp->keep_idle;
  1891. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, TCP_KEEPALIVE) = %d\n",
  1892. s, *(int *)optval));
  1893. break;
  1894. #if LWIP_TCP_KEEPALIVE
  1895. case TCP_KEEPIDLE:
  1896. *(int*)optval = (int)(sock->conn->pcb.tcp->keep_idle/1000);
  1897. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, TCP_KEEPIDLE) = %d\n",
  1898. s, *(int *)optval));
  1899. break;
  1900. case TCP_KEEPINTVL:
  1901. *(int*)optval = (int)(sock->conn->pcb.tcp->keep_intvl/1000);
  1902. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, TCP_KEEPINTVL) = %d\n",
  1903. s, *(int *)optval));
  1904. break;
  1905. case TCP_KEEPCNT:
  1906. *(int*)optval = (int)sock->conn->pcb.tcp->keep_cnt;
  1907. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, TCP_KEEPCNT) = %d\n",
  1908. s, *(int *)optval));
  1909. break;
  1910. #endif /* LWIP_TCP_KEEPALIVE */
  1911. default:
  1912. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, UNIMPL: optname=0x%x, ..)\n",
  1913. s, optname));
  1914. err = ENOPROTOOPT;
  1915. break;
  1916. } /* switch (optname) */
  1917. break;
  1918. #endif /* LWIP_TCP */
  1919. #if LWIP_IPV6
  1920. /* Level: IPPROTO_IPV6 */
  1921. case IPPROTO_IPV6:
  1922. switch (optname) {
  1923. case IPV6_V6ONLY:
  1924. LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, *optlen, int);
  1925. *(int*)optval = (netconn_get_ipv6only(sock->conn) ? 1 : 0);
  1926. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IPV6, IPV6_V6ONLY) = %d\n",
  1927. s, *(int *)optval));
  1928. break;
  1929. default:
  1930. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IPV6, UNIMPL: optname=0x%x, ..)\n",
  1931. s, optname));
  1932. err = ENOPROTOOPT;
  1933. break;
  1934. } /* switch (optname) */
  1935. break;
  1936. #endif /* LWIP_IPV6 */
  1937. #if LWIP_UDP && LWIP_UDPLITE
  1938. /* Level: IPPROTO_UDPLITE */
  1939. case IPPROTO_UDPLITE:
  1940. /* Special case: all IPPROTO_UDPLITE option take an int */
  1941. LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, int);
  1942. /* If this is no UDP lite socket, ignore any options. */
  1943. if (!NETCONNTYPE_ISUDPLITE(netconn_type(sock->conn))) {
  1944. return ENOPROTOOPT;
  1945. }
  1946. switch (optname) {
  1947. case UDPLITE_SEND_CSCOV:
  1948. *(int*)optval = sock->conn->pcb.udp->chksum_len_tx;
  1949. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UDPLITE_SEND_CSCOV) = %d\n",
  1950. s, (*(int*)optval)) );
  1951. break;
  1952. case UDPLITE_RECV_CSCOV:
  1953. *(int*)optval = sock->conn->pcb.udp->chksum_len_rx;
  1954. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UDPLITE_RECV_CSCOV) = %d\n",
  1955. s, (*(int*)optval)) );
  1956. break;
  1957. default:
  1958. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UNIMPL: optname=0x%x, ..)\n",
  1959. s, optname));
  1960. err = ENOPROTOOPT;
  1961. break;
  1962. } /* switch (optname) */
  1963. break;
  1964. #endif /* LWIP_UDP */
  1965. /* Level: IPPROTO_RAW */
  1966. case IPPROTO_RAW:
  1967. switch (optname) {
  1968. #if LWIP_IPV6 && LWIP_RAW
  1969. case IPV6_CHECKSUM:
  1970. LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, *optlen, int, NETCONN_RAW);
  1971. if (sock->conn->pcb.raw->chksum_reqd == 0) {
  1972. *(int *)optval = -1;
  1973. } else {
  1974. *(int *)optval = sock->conn->pcb.raw->chksum_offset;
  1975. }
  1976. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_RAW, IPV6_CHECKSUM) = %d\n",
  1977. s, (*(int*)optval)) );
  1978. break;
  1979. #endif /* LWIP_IPV6 && LWIP_RAW */
  1980. default:
  1981. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_RAW, UNIMPL: optname=0x%x, ..)\n",
  1982. s, optname));
  1983. err = ENOPROTOOPT;
  1984. break;
  1985. } /* switch (optname) */
  1986. break;
  1987. default:
  1988. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, level=0x%x, UNIMPL: optname=0x%x, ..)\n",
  1989. s, level, optname));
  1990. err = ENOPROTOOPT;
  1991. break;
  1992. } /* switch (level) */
  1993. return err;
  1994. }
  1995. int
  1996. lwip_setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen)
  1997. {
  1998. u8_t err = 0;
  1999. struct lwip_sock *sock = get_socket(s);
  2000. #if !LWIP_TCPIP_CORE_LOCKING
  2001. LWIP_SETGETSOCKOPT_DATA_VAR_DECLARE(data);
  2002. #endif /* !LWIP_TCPIP_CORE_LOCKING */
  2003. if (!sock) {
  2004. return -1;
  2005. }
  2006. if (NULL == optval) {
  2007. sock_set_errno(sock, EFAULT);
  2008. return -1;
  2009. }
  2010. #if LWIP_TCPIP_CORE_LOCKING
  2011. /* core-locking can just call the -impl function */
  2012. LOCK_TCPIP_CORE();
  2013. err = lwip_setsockopt_impl(s, level, optname, optval, optlen);
  2014. UNLOCK_TCPIP_CORE();
  2015. #else /* LWIP_TCPIP_CORE_LOCKING */
  2016. #if LWIP_MPU_COMPATIBLE
  2017. /* MPU_COMPATIBLE copies the optval data, so check for max size here */
  2018. if (optlen > LWIP_SETGETSOCKOPT_MAXOPTLEN) {
  2019. sock_set_errno(sock, ENOBUFS);
  2020. return -1;
  2021. }
  2022. #endif /* LWIP_MPU_COMPATIBLE */
  2023. LWIP_SETGETSOCKOPT_DATA_VAR_ALLOC(data, sock);
  2024. LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).s = s;
  2025. LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).level = level;
  2026. LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optname = optname;
  2027. LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optlen = optlen;
  2028. #if LWIP_MPU_COMPATIBLE
  2029. MEMCPY(LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optval, optval, optlen);
  2030. #else /* LWIP_MPU_COMPATIBLE */
  2031. LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).optval.pc = (const void*)optval;
  2032. #endif /* LWIP_MPU_COMPATIBLE */
  2033. LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).err = 0;
  2034. #if LWIP_NETCONN_SEM_PER_THREAD
  2035. LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).completed_sem = LWIP_NETCONN_THREAD_SEM_GET();
  2036. #else
  2037. LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).completed_sem = &sock->conn->op_completed;
  2038. #endif
  2039. err = tcpip_callback(lwip_setsockopt_callback, &LWIP_SETGETSOCKOPT_DATA_VAR_REF(data));
  2040. if (err != ERR_OK) {
  2041. LWIP_SETGETSOCKOPT_DATA_VAR_FREE(data);
  2042. sock_set_errno(sock, err_to_errno(err));
  2043. return -1;
  2044. }
  2045. sys_arch_sem_wait((sys_sem_t*)(LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).completed_sem), 0);
  2046. /* maybe lwip_getsockopt_internal has changed err */
  2047. err = LWIP_SETGETSOCKOPT_DATA_VAR_REF(data).err;
  2048. LWIP_SETGETSOCKOPT_DATA_VAR_FREE(data);
  2049. #endif /* LWIP_TCPIP_CORE_LOCKING */
  2050. sock_set_errno(sock, err);
  2051. return err ? -1 : 0;
  2052. }
  2053. #if !LWIP_TCPIP_CORE_LOCKING
  2054. /** lwip_setsockopt_callback: only used without CORE_LOCKING
  2055. * to get into the tcpip_thread
  2056. */
  2057. static void
  2058. lwip_setsockopt_callback(void *arg)
  2059. {
  2060. struct lwip_setgetsockopt_data *data;
  2061. LWIP_ASSERT("arg != NULL", arg != NULL);
  2062. data = (struct lwip_setgetsockopt_data*)arg;
  2063. data->err = lwip_setsockopt_impl(data->s, data->level, data->optname,
  2064. #if LWIP_MPU_COMPATIBLE
  2065. data->optval,
  2066. #else /* LWIP_MPU_COMPATIBLE */
  2067. data->optval.pc,
  2068. #endif /* LWIP_MPU_COMPATIBLE */
  2069. data->optlen);
  2070. sys_sem_signal((sys_sem_t*)(data->completed_sem));
  2071. }
  2072. #endif /* LWIP_TCPIP_CORE_LOCKING */
  2073. /** lwip_setsockopt_impl: the actual implementation of setsockopt:
  2074. * same argument as lwip_setsockopt, either called directly or through callback
  2075. */
  2076. static u8_t
  2077. lwip_setsockopt_impl(int s, int level, int optname, const void *optval, socklen_t optlen)
  2078. {
  2079. u8_t err = 0;
  2080. struct lwip_sock *sock = tryget_socket(s);
  2081. if (!sock) {
  2082. return EBADF;
  2083. }
  2084. switch (level) {
  2085. /* Level: SOL_SOCKET */
  2086. case SOL_SOCKET:
  2087. switch (optname) {
  2088. /* SO_ACCEPTCONN is get-only */
  2089. /* The option flags */
  2090. case SO_BROADCAST:
  2091. case SO_KEEPALIVE:
  2092. #if SO_REUSE
  2093. case SO_REUSEADDR:
  2094. #endif /* SO_REUSE */
  2095. LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, int);
  2096. if (*(const int*)optval) {
  2097. ip_set_option(sock->conn->pcb.ip, optname);
  2098. } else {
  2099. ip_reset_option(sock->conn->pcb.ip, optname);
  2100. }
  2101. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, SOL_SOCKET, optname=0x%x, ..) -> %s\n",
  2102. s, optname, (*(const int*)optval?"on":"off")));
  2103. break;
  2104. /* SO_TYPE is get-only */
  2105. /* SO_ERROR is get-only */
  2106. #if LWIP_SO_SNDTIMEO
  2107. case SO_SNDTIMEO:
  2108. LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, optlen, LWIP_SO_SNDRCVTIMEO_OPTTYPE);
  2109. netconn_set_sendtimeout(sock->conn, LWIP_SO_SNDRCVTIMEO_GET_MS(optval));
  2110. break;
  2111. #endif /* LWIP_SO_SNDTIMEO */
  2112. #if LWIP_SO_RCVTIMEO
  2113. case SO_RCVTIMEO:
  2114. LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, optlen, LWIP_SO_SNDRCVTIMEO_OPTTYPE);
  2115. netconn_set_recvtimeout(sock->conn, (int)LWIP_SO_SNDRCVTIMEO_GET_MS(optval));
  2116. break;
  2117. #endif /* LWIP_SO_RCVTIMEO */
  2118. #if LWIP_SO_RCVBUF
  2119. case SO_RCVBUF:
  2120. LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, optlen, int);
  2121. netconn_set_recvbufsize(sock->conn, *(const int*)optval);
  2122. break;
  2123. #endif /* LWIP_SO_RCVBUF */
  2124. #if LWIP_SO_LINGER
  2125. case SO_LINGER:
  2126. {
  2127. const struct linger* linger = (const struct linger*)optval;
  2128. LWIP_SOCKOPT_CHECK_OPTLEN_CONN(sock, optlen, struct linger);
  2129. if (linger->l_onoff) {
  2130. int lingersec = linger->l_linger;
  2131. if (lingersec < 0) {
  2132. return EINVAL;
  2133. }
  2134. if (lingersec > 0xFFFF) {
  2135. lingersec = 0xFFFF;
  2136. }
  2137. sock->conn->linger = (s16_t)lingersec;
  2138. } else {
  2139. sock->conn->linger = -1;
  2140. }
  2141. }
  2142. break;
  2143. #endif /* LWIP_SO_LINGER */
  2144. #if LWIP_UDP
  2145. case SO_NO_CHECK:
  2146. LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, int, NETCONN_UDP);
  2147. #if LWIP_UDPLITE
  2148. if ((udp_flags(sock->conn->pcb.udp) & UDP_FLAGS_UDPLITE) != 0) {
  2149. /* this flag is only available for UDP, not for UDP lite */
  2150. return EAFNOSUPPORT;
  2151. }
  2152. #endif /* LWIP_UDPLITE */
  2153. if (*(const int*)optval) {
  2154. udp_setflags(sock->conn->pcb.udp, udp_flags(sock->conn->pcb.udp) | UDP_FLAGS_NOCHKSUM);
  2155. } else {
  2156. udp_setflags(sock->conn->pcb.udp, udp_flags(sock->conn->pcb.udp) & ~UDP_FLAGS_NOCHKSUM);
  2157. }
  2158. break;
  2159. #endif /* LWIP_UDP */
  2160. default:
  2161. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, SOL_SOCKET, UNIMPL: optname=0x%x, ..)\n",
  2162. s, optname));
  2163. err = ENOPROTOOPT;
  2164. break;
  2165. } /* switch (optname) */
  2166. break;
  2167. /* Level: IPPROTO_IP */
  2168. case IPPROTO_IP:
  2169. switch (optname) {
  2170. case IP_TTL:
  2171. LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, int);
  2172. sock->conn->pcb.ip->ttl = (u8_t)(*(const int*)optval);
  2173. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, IP_TTL, ..) -> %d\n",
  2174. s, sock->conn->pcb.ip->ttl));
  2175. break;
  2176. case IP_TOS:
  2177. LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, int);
  2178. sock->conn->pcb.ip->tos = (u8_t)(*(const int*)optval);
  2179. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, IP_TOS, ..)-> %d\n",
  2180. s, sock->conn->pcb.ip->tos));
  2181. break;
  2182. #if LWIP_MULTICAST_TX_OPTIONS
  2183. case IP_MULTICAST_TTL:
  2184. LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, u8_t, NETCONN_UDP);
  2185. udp_set_multicast_ttl(sock->conn->pcb.udp, (u8_t)(*(const u8_t*)optval));
  2186. break;
  2187. case IP_MULTICAST_IF:
  2188. {
  2189. ip4_addr_t if_addr;
  2190. LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, struct in_addr, NETCONN_UDP);
  2191. inet_addr_to_ip4addr(&if_addr, (const struct in_addr*)optval);
  2192. udp_set_multicast_netif_addr(sock->conn->pcb.udp, &if_addr);
  2193. }
  2194. break;
  2195. case IP_MULTICAST_LOOP:
  2196. LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, u8_t, NETCONN_UDP);
  2197. if (*(const u8_t*)optval) {
  2198. udp_setflags(sock->conn->pcb.udp, udp_flags(sock->conn->pcb.udp) | UDP_FLAGS_MULTICAST_LOOP);
  2199. } else {
  2200. udp_setflags(sock->conn->pcb.udp, udp_flags(sock->conn->pcb.udp) & ~UDP_FLAGS_MULTICAST_LOOP);
  2201. }
  2202. break;
  2203. #endif /* LWIP_MULTICAST_TX_OPTIONS */
  2204. #if LWIP_IGMP
  2205. case IP_ADD_MEMBERSHIP:
  2206. case IP_DROP_MEMBERSHIP:
  2207. {
  2208. /* If this is a TCP or a RAW socket, ignore these options. */
  2209. /* @todo: assign membership to this socket so that it is dropped when closing the socket */
  2210. err_t igmp_err;
  2211. const struct ip_mreq *imr = (const struct ip_mreq *)optval;
  2212. ip4_addr_t if_addr;
  2213. ip4_addr_t multi_addr;
  2214. LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, struct ip_mreq, NETCONN_UDP);
  2215. inet_addr_to_ip4addr(&if_addr, &imr->imr_interface);
  2216. inet_addr_to_ip4addr(&multi_addr, &imr->imr_multiaddr);
  2217. if (optname == IP_ADD_MEMBERSHIP) {
  2218. if (!lwip_socket_register_membership(s, &if_addr, &multi_addr)) {
  2219. /* cannot track membership (out of memory) */
  2220. err = ENOMEM;
  2221. igmp_err = ERR_OK;
  2222. } else {
  2223. igmp_err = igmp_joingroup(&if_addr, &multi_addr);
  2224. }
  2225. } else {
  2226. igmp_err = igmp_leavegroup(&if_addr, &multi_addr);
  2227. lwip_socket_unregister_membership(s, &if_addr, &multi_addr);
  2228. }
  2229. if (igmp_err != ERR_OK) {
  2230. err = EADDRNOTAVAIL;
  2231. }
  2232. }
  2233. break;
  2234. #endif /* LWIP_IGMP */
  2235. default:
  2236. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, UNIMPL: optname=0x%x, ..)\n",
  2237. s, optname));
  2238. err = ENOPROTOOPT;
  2239. break;
  2240. } /* switch (optname) */
  2241. break;
  2242. #if LWIP_TCP
  2243. /* Level: IPPROTO_TCP */
  2244. case IPPROTO_TCP:
  2245. /* Special case: all IPPROTO_TCP option take an int */
  2246. LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, int, NETCONN_TCP);
  2247. if (sock->conn->pcb.tcp->state == LISTEN) {
  2248. return EINVAL;
  2249. }
  2250. switch (optname) {
  2251. case TCP_NODELAY:
  2252. if (*(const int*)optval) {
  2253. tcp_nagle_disable(sock->conn->pcb.tcp);
  2254. } else {
  2255. tcp_nagle_enable(sock->conn->pcb.tcp);
  2256. }
  2257. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_NODELAY) -> %s\n",
  2258. s, (*(const int *)optval)?"on":"off") );
  2259. break;
  2260. case TCP_KEEPALIVE:
  2261. sock->conn->pcb.tcp->keep_idle = (u32_t)(*(const int*)optval);
  2262. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPALIVE) -> %"U32_F"\n",
  2263. s, sock->conn->pcb.tcp->keep_idle));
  2264. break;
  2265. #if LWIP_TCP_KEEPALIVE
  2266. case TCP_KEEPIDLE:
  2267. sock->conn->pcb.tcp->keep_idle = 1000*(u32_t)(*(const int*)optval);
  2268. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPIDLE) -> %"U32_F"\n",
  2269. s, sock->conn->pcb.tcp->keep_idle));
  2270. break;
  2271. case TCP_KEEPINTVL:
  2272. sock->conn->pcb.tcp->keep_intvl = 1000*(u32_t)(*(const int*)optval);
  2273. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPINTVL) -> %"U32_F"\n",
  2274. s, sock->conn->pcb.tcp->keep_intvl));
  2275. break;
  2276. case TCP_KEEPCNT:
  2277. sock->conn->pcb.tcp->keep_cnt = (u32_t)(*(const int*)optval);
  2278. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPCNT) -> %"U32_F"\n",
  2279. s, sock->conn->pcb.tcp->keep_cnt));
  2280. break;
  2281. #endif /* LWIP_TCP_KEEPALIVE */
  2282. default:
  2283. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, UNIMPL: optname=0x%x, ..)\n",
  2284. s, optname));
  2285. err = ENOPROTOOPT;
  2286. break;
  2287. } /* switch (optname) */
  2288. break;
  2289. #endif /* LWIP_TCP*/
  2290. #if LWIP_IPV6
  2291. /* Level: IPPROTO_IPV6 */
  2292. case IPPROTO_IPV6:
  2293. switch (optname) {
  2294. case IPV6_V6ONLY:
  2295. LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, int, NETCONN_TCP);
  2296. if (*(const int*)optval) {
  2297. netconn_set_ipv6only(sock->conn, 1);
  2298. } else {
  2299. netconn_set_ipv6only(sock->conn, 0);
  2300. }
  2301. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IPV6, IPV6_V6ONLY, ..) -> %d\n",
  2302. s, (netconn_get_ipv6only(sock->conn) ? 1 : 0)));
  2303. break;
  2304. default:
  2305. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IPV6, UNIMPL: optname=0x%x, ..)\n",
  2306. s, optname));
  2307. err = ENOPROTOOPT;
  2308. break;
  2309. } /* switch (optname) */
  2310. break;
  2311. #endif /* LWIP_IPV6 */
  2312. #if LWIP_UDP && LWIP_UDPLITE
  2313. /* Level: IPPROTO_UDPLITE */
  2314. case IPPROTO_UDPLITE:
  2315. /* Special case: all IPPROTO_UDPLITE option take an int */
  2316. LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, optlen, int);
  2317. /* If this is no UDP lite socket, ignore any options. */
  2318. if (!NETCONNTYPE_ISUDPLITE(netconn_type(sock->conn))) {
  2319. return ENOPROTOOPT;
  2320. }
  2321. switch (optname) {
  2322. case UDPLITE_SEND_CSCOV:
  2323. if ((*(const int*)optval != 0) && ((*(const int*)optval < 8) || (*(const int*)optval > 0xffff))) {
  2324. /* don't allow illegal values! */
  2325. sock->conn->pcb.udp->chksum_len_tx = 8;
  2326. } else {
  2327. sock->conn->pcb.udp->chksum_len_tx = (u16_t)*(const int*)optval;
  2328. }
  2329. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_UDPLITE, UDPLITE_SEND_CSCOV) -> %d\n",
  2330. s, (*(const int*)optval)) );
  2331. break;
  2332. case UDPLITE_RECV_CSCOV:
  2333. if ((*(const int*)optval != 0) && ((*(const int*)optval < 8) || (*(const int*)optval > 0xffff))) {
  2334. /* don't allow illegal values! */
  2335. sock->conn->pcb.udp->chksum_len_rx = 8;
  2336. } else {
  2337. sock->conn->pcb.udp->chksum_len_rx = (u16_t)*(const int*)optval;
  2338. }
  2339. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_UDPLITE, UDPLITE_RECV_CSCOV) -> %d\n",
  2340. s, (*(const int*)optval)) );
  2341. break;
  2342. default:
  2343. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_UDPLITE, UNIMPL: optname=0x%x, ..)\n",
  2344. s, optname));
  2345. err = ENOPROTOOPT;
  2346. break;
  2347. } /* switch (optname) */
  2348. break;
  2349. #endif /* LWIP_UDP */
  2350. /* Level: IPPROTO_RAW */
  2351. case IPPROTO_RAW:
  2352. switch (optname) {
  2353. #if LWIP_IPV6 && LWIP_RAW
  2354. case IPV6_CHECKSUM:
  2355. /* It should not be possible to disable the checksum generation with ICMPv6
  2356. * as per RFC 3542 chapter 3.1 */
  2357. if(sock->conn->pcb.raw->protocol == IPPROTO_ICMPV6) {
  2358. return EINVAL;
  2359. }
  2360. LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB_TYPE(sock, optlen, int, NETCONN_RAW);
  2361. if (*(const int *)optval < 0) {
  2362. sock->conn->pcb.raw->chksum_reqd = 0;
  2363. } else if (*(const int *)optval & 1) {
  2364. /* Per RFC3542, odd offsets are not allowed */
  2365. return EINVAL;
  2366. } else {
  2367. sock->conn->pcb.raw->chksum_reqd = 1;
  2368. sock->conn->pcb.raw->chksum_offset = (u16_t)*(const int *)optval;
  2369. }
  2370. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_RAW, IPV6_CHECKSUM, ..) -> %d\n",
  2371. s, sock->conn->pcb.raw->chksum_reqd));
  2372. break;
  2373. #endif /* LWIP_IPV6 && LWIP_RAW */
  2374. default:
  2375. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_RAW, UNIMPL: optname=0x%x, ..)\n",
  2376. s, optname));
  2377. err = ENOPROTOOPT;
  2378. break;
  2379. } /* switch (optname) */
  2380. break;
  2381. default:
  2382. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, level=0x%x, UNIMPL: optname=0x%x, ..)\n",
  2383. s, level, optname));
  2384. err = ENOPROTOOPT;
  2385. break;
  2386. } /* switch (level) */
  2387. return err;
  2388. }
  2389. int
  2390. lwip_ioctl(int s, long cmd, void *argp)
  2391. {
  2392. struct lwip_sock *sock = get_socket(s);
  2393. u8_t val;
  2394. #if LWIP_SO_RCVBUF
  2395. u16_t buflen = 0;
  2396. int recv_avail;
  2397. #endif /* LWIP_SO_RCVBUF */
  2398. if (!sock) {
  2399. return -1;
  2400. }
  2401. switch (cmd) {
  2402. #if LWIP_SO_RCVBUF || LWIP_FIONREAD_LINUXMODE
  2403. case FIONREAD:
  2404. if (!argp) {
  2405. sock_set_errno(sock, EINVAL);
  2406. return -1;
  2407. }
  2408. #if LWIP_FIONREAD_LINUXMODE
  2409. if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_TCP) {
  2410. struct pbuf *p;
  2411. if (sock->lastdata) {
  2412. p = ((struct netbuf *)sock->lastdata)->p;
  2413. *((int*)argp) = p->tot_len - sock->lastoffset;
  2414. } else {
  2415. struct netbuf *rxbuf;
  2416. err_t err;
  2417. if (sock->rcvevent <= 0) {
  2418. *((int*)argp) = 0;
  2419. } else {
  2420. err = netconn_recv(sock->conn, &rxbuf);
  2421. if (err != ERR_OK) {
  2422. *((int*)argp) = 0;
  2423. } else {
  2424. sock->lastdata = rxbuf;
  2425. sock->lastoffset = 0;
  2426. *((int*)argp) = rxbuf->p->tot_len;
  2427. }
  2428. }
  2429. }
  2430. return 0;
  2431. }
  2432. #endif /* LWIP_FIONREAD_LINUXMODE */
  2433. #if LWIP_SO_RCVBUF
  2434. /* we come here if either LWIP_FIONREAD_LINUXMODE==0 or this is a TCP socket */
  2435. SYS_ARCH_GET(sock->conn->recv_avail, recv_avail);
  2436. if (recv_avail < 0) {
  2437. recv_avail = 0;
  2438. }
  2439. *((int*)argp) = recv_avail;
  2440. /* Check if there is data left from the last recv operation. /maq 041215 */
  2441. if (sock->lastdata) {
  2442. struct pbuf *p = (struct pbuf *)sock->lastdata;
  2443. if (NETCONNTYPE_GROUP(netconn_type(sock->conn)) != NETCONN_TCP) {
  2444. p = ((struct netbuf *)p)->p;
  2445. }
  2446. buflen = p->tot_len;
  2447. buflen -= sock->lastoffset;
  2448. *((int*)argp) += buflen;
  2449. }
  2450. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, FIONREAD, %p) = %"U16_F"\n", s, argp, *((u16_t*)argp)));
  2451. sock_set_errno(sock, 0);
  2452. return 0;
  2453. #else /* LWIP_SO_RCVBUF */
  2454. break;
  2455. #endif /* LWIP_SO_RCVBUF */
  2456. #endif /* LWIP_SO_RCVBUF || LWIP_FIONREAD_LINUXMODE */
  2457. case (long)FIONBIO:
  2458. val = 0;
  2459. if (argp && *(u32_t*)argp) {
  2460. val = 1;
  2461. }
  2462. netconn_set_nonblocking(sock->conn, val);
  2463. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, FIONBIO, %d)\n", s, val));
  2464. sock_set_errno(sock, 0);
  2465. return 0;
  2466. default:
  2467. break;
  2468. } /* switch (cmd) */
  2469. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, UNIMPL: 0x%lx, %p)\n", s, cmd, argp));
  2470. sock_set_errno(sock, ENOSYS); /* not yet implemented */
  2471. return -1;
  2472. }
  2473. /** A minimal implementation of fcntl.
  2474. * Currently only the commands F_GETFL and F_SETFL are implemented.
  2475. * Only the flag O_NONBLOCK is implemented.
  2476. */
  2477. int
  2478. lwip_fcntl(int s, int cmd, int val)
  2479. {
  2480. struct lwip_sock *sock = get_socket(s);
  2481. int ret = -1;
  2482. if (!sock) {
  2483. return -1;
  2484. }
  2485. switch (cmd) {
  2486. case F_GETFL:
  2487. ret = netconn_is_nonblocking(sock->conn) ? O_NONBLOCK : 0;
  2488. sock_set_errno(sock, 0);
  2489. break;
  2490. case F_SETFL:
  2491. if ((val & ~O_NONBLOCK) == 0) {
  2492. /* only O_NONBLOCK, all other bits are zero */
  2493. netconn_set_nonblocking(sock->conn, val & O_NONBLOCK);
  2494. ret = 0;
  2495. sock_set_errno(sock, 0);
  2496. } else {
  2497. sock_set_errno(sock, ENOSYS); /* not yet implemented */
  2498. }
  2499. break;
  2500. default:
  2501. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_fcntl(%d, UNIMPL: %d, %d)\n", s, cmd, val));
  2502. sock_set_errno(sock, ENOSYS); /* not yet implemented */
  2503. break;
  2504. }
  2505. return ret;
  2506. }
  2507. #if LWIP_IGMP
  2508. /** Register a new IGMP membership. On socket close, the membership is dropped automatically.
  2509. *
  2510. * ATTENTION: this function is called from tcpip_thread (or under CORE_LOCK).
  2511. *
  2512. * @return 1 on success, 0 on failure
  2513. */
  2514. static int
  2515. lwip_socket_register_membership(int s, const ip4_addr_t *if_addr, const ip4_addr_t *multi_addr)
  2516. {
  2517. struct lwip_sock *sock = get_socket(s);
  2518. int i;
  2519. if (!sock) {
  2520. return 0;
  2521. }
  2522. for (i = 0; i < LWIP_SOCKET_MAX_MEMBERSHIPS; i++) {
  2523. if (socket_ipv4_multicast_memberships[i].sock == NULL) {
  2524. socket_ipv4_multicast_memberships[i].sock = sock;
  2525. ip4_addr_copy(socket_ipv4_multicast_memberships[i].if_addr, *if_addr);
  2526. ip4_addr_copy(socket_ipv4_multicast_memberships[i].multi_addr, *multi_addr);
  2527. return 1;
  2528. }
  2529. }
  2530. return 0;
  2531. }
  2532. /** Unregister a previously registered membership. This prevents dropping the membership
  2533. * on socket close.
  2534. *
  2535. * ATTENTION: this function is called from tcpip_thread (or under CORE_LOCK).
  2536. */
  2537. static void
  2538. lwip_socket_unregister_membership(int s, const ip4_addr_t *if_addr, const ip4_addr_t *multi_addr)
  2539. {
  2540. struct lwip_sock *sock = get_socket(s);
  2541. int i;
  2542. if (!sock) {
  2543. return;
  2544. }
  2545. for (i = 0; i < LWIP_SOCKET_MAX_MEMBERSHIPS; i++) {
  2546. if ((socket_ipv4_multicast_memberships[i].sock == sock) &&
  2547. ip4_addr_cmp(&socket_ipv4_multicast_memberships[i].if_addr, if_addr) &&
  2548. ip4_addr_cmp(&socket_ipv4_multicast_memberships[i].multi_addr, multi_addr)) {
  2549. socket_ipv4_multicast_memberships[i].sock = NULL;
  2550. ip4_addr_set_zero(&socket_ipv4_multicast_memberships[i].if_addr);
  2551. ip4_addr_set_zero(&socket_ipv4_multicast_memberships[i].multi_addr);
  2552. return;
  2553. }
  2554. }
  2555. }
  2556. /** Drop all memberships of a socket that were not dropped explicitly via setsockopt.
  2557. *
  2558. * ATTENTION: this function is NOT called from tcpip_thread (or under CORE_LOCK).
  2559. */
  2560. static void
  2561. lwip_socket_drop_registered_memberships(int s)
  2562. {
  2563. struct lwip_sock *sock = get_socket(s);
  2564. int i;
  2565. if (!sock) {
  2566. return;
  2567. }
  2568. for (i = 0; i < LWIP_SOCKET_MAX_MEMBERSHIPS; i++) {
  2569. if (socket_ipv4_multicast_memberships[i].sock == sock) {
  2570. ip_addr_t multi_addr, if_addr;
  2571. ip_addr_copy_from_ip4(multi_addr, socket_ipv4_multicast_memberships[i].multi_addr);
  2572. ip_addr_copy_from_ip4(if_addr, socket_ipv4_multicast_memberships[i].if_addr);
  2573. socket_ipv4_multicast_memberships[i].sock = NULL;
  2574. ip4_addr_set_zero(&socket_ipv4_multicast_memberships[i].if_addr);
  2575. ip4_addr_set_zero(&socket_ipv4_multicast_memberships[i].multi_addr);
  2576. netconn_join_leave_group(sock->conn, &multi_addr, &if_addr, NETCONN_LEAVE);
  2577. }
  2578. }
  2579. }
  2580. #endif /* LWIP_IGMP */
  2581. #endif /* LWIP_SOCKET */