sockets.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988
  1. /**
  2. * @file
  3. * Sockets BSD-Like API module
  4. *
  5. */
  6. /*
  7. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without modification,
  11. * are permitted provided that the following conditions are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. The name of the author may not be used to endorse or promote products
  19. * derived from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  24. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  26. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  29. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. * OF SUCH DAMAGE.
  31. *
  32. * This file is part of the lwIP TCP/IP stack.
  33. *
  34. * Author: Adam Dunkels <adam@sics.se>
  35. *
  36. * Improved by Marc Boucher <marc@mbsi.ca> and David Haas <dhaas@alum.rpi.edu>
  37. *
  38. */
  39. #include "lwip/opt.h"
  40. #if LWIP_SOCKET /* don't build if not configured for use in lwipopts.h */
  41. #include "lwip/sockets.h"
  42. #include "lwip/api.h"
  43. #include "lwip/sys.h"
  44. #include "lwip/igmp.h"
  45. #include "lwip/inet.h"
  46. #include "lwip/tcp.h"
  47. #include "lwip/raw.h"
  48. #include "lwip/udp.h"
  49. #include "lwip/tcpip.h"
  50. #include <string.h>
  51. #define NUM_SOCKETS MEMP_NUM_NETCONN
  52. /** Contains all internal pointers and states used for a socket */
  53. struct lwip_socket {
  54. /** sockets currently are built on netconns, each socket has one netconn */
  55. struct netconn *conn;
  56. /** data that was left from the previous read */
  57. struct netbuf *lastdata;
  58. /** offset in the data that was left from the previous read */
  59. u16_t lastoffset;
  60. /** number of times data was received, set by event_callback(),
  61. tested by the receive and select functions */
  62. s16_t rcvevent;
  63. /** number of times data was received, set by event_callback(),
  64. tested by select */
  65. u16_t sendevent;
  66. /** socket flags (currently, only used for O_NONBLOCK) */
  67. u16_t flags;
  68. /** last error that occurred on this socket */
  69. int err;
  70. };
  71. /** Description for a task waiting in select */
  72. struct lwip_select_cb {
  73. /** Pointer to the next waiting task */
  74. struct lwip_select_cb *next;
  75. /** readset passed to select */
  76. fd_set *readset;
  77. /** writeset passed to select */
  78. fd_set *writeset;
  79. /** unimplemented: exceptset passed to select */
  80. fd_set *exceptset;
  81. /** don't signal the same semaphore twice: set to 1 when signalled */
  82. int sem_signalled;
  83. /** semaphore to wake up a task waiting for select */
  84. sys_sem_t sem;
  85. };
  86. /** This struct is used to pass data to the set/getsockopt_internal
  87. * functions running in tcpip_thread context (only a void* is allowed) */
  88. struct lwip_setgetsockopt_data {
  89. /** socket struct for which to change options */
  90. struct lwip_socket *sock;
  91. /** socket index for which to change options */
  92. int s;
  93. /** level of the option to process */
  94. int level;
  95. /** name of the option to process */
  96. int optname;
  97. /** set: value to set the option to
  98. * get: value of the option is stored here */
  99. void *optval;
  100. /** size of *optval */
  101. socklen_t *optlen;
  102. /** if an error occures, it is temporarily stored here */
  103. err_t err;
  104. };
  105. /** The global array of available sockets */
  106. static struct lwip_socket sockets[NUM_SOCKETS];
  107. /** The global list of tasks waiting for select */
  108. static struct lwip_select_cb *select_cb_list;
  109. /** Semaphore protecting the sockets array */
  110. static sys_sem_t socksem;
  111. /** Semaphore protecting select_cb_list */
  112. static sys_sem_t selectsem;
  113. /** Table to quickly map an lwIP error (err_t) to a socket error
  114. * by using -err as an index */
  115. static const int err_to_errno_table[] = {
  116. 0, /* ERR_OK 0 No error, everything OK. */
  117. ENOMEM, /* ERR_MEM -1 Out of memory error. */
  118. ENOBUFS, /* ERR_BUF -2 Buffer error. */
  119. ETIMEDOUT, /* ERR_TIMEOUT -3 Timeout */
  120. EHOSTUNREACH, /* ERR_RTE -4 Routing problem. */
  121. ECONNABORTED, /* ERR_ABRT -5 Connection aborted. */
  122. ECONNRESET, /* ERR_RST -6 Connection reset. */
  123. ESHUTDOWN, /* ERR_CLSD -7 Connection closed. */
  124. ENOTCONN, /* ERR_CONN -8 Not connected. */
  125. EINVAL, /* ERR_VAL -9 Illegal value. */
  126. EIO, /* ERR_ARG -10 Illegal argument. */
  127. EADDRINUSE, /* ERR_USE -11 Address in use. */
  128. -1, /* ERR_IF -12 Low-level netif error */
  129. -1, /* ERR_ISCONN -13 Already connected. */
  130. EINPROGRESS /* ERR_INPROGRESS -14 Operation in progress */
  131. };
  132. #define ERR_TO_ERRNO_TABLE_SIZE \
  133. (sizeof(err_to_errno_table)/sizeof(err_to_errno_table[0]))
  134. #define err_to_errno(err) \
  135. ((unsigned)(-(err)) < ERR_TO_ERRNO_TABLE_SIZE ? \
  136. err_to_errno_table[-(err)] : EIO)
  137. #ifdef ERRNO
  138. #ifndef set_errno
  139. #define set_errno(err) errno = (err)
  140. #endif
  141. #else
  142. #define set_errno(err)
  143. #endif
  144. #define sock_set_errno(sk, e) do { \
  145. sk->err = (e); \
  146. set_errno(sk->err); \
  147. } while (0)
  148. /* Forward delcaration of some functions */
  149. static void event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len);
  150. static void lwip_getsockopt_internal(void *arg);
  151. static void lwip_setsockopt_internal(void *arg);
  152. /**
  153. * Initialize this module. This function has to be called before any other
  154. * functions in this module!
  155. */
  156. void
  157. lwip_socket_init(void)
  158. {
  159. socksem = sys_sem_new(1);
  160. selectsem = sys_sem_new(1);
  161. }
  162. /**
  163. * Map a externally used socket index to the internal socket representation.
  164. *
  165. * @param s externally used socket index
  166. * @return struct lwip_socket for the socket or NULL if not found
  167. */
  168. static struct lwip_socket *
  169. get_socket(int s)
  170. {
  171. struct lwip_socket *sock;
  172. if ((s < 0) || (s >= NUM_SOCKETS)) {
  173. LWIP_DEBUGF(SOCKETS_DEBUG, ("get_socket(%d): invalid\n", s));
  174. set_errno(EBADF);
  175. return NULL;
  176. }
  177. sock = &sockets[s];
  178. if (!sock->conn) {
  179. LWIP_DEBUGF(SOCKETS_DEBUG, ("get_socket(%d): not active\n", s));
  180. set_errno(EBADF);
  181. return NULL;
  182. }
  183. return sock;
  184. }
  185. /**
  186. * Allocate a new socket for a given netconn.
  187. *
  188. * @param newconn the netconn for which to allocate a socket
  189. * @return the index of the new socket; -1 on error
  190. */
  191. static int
  192. alloc_socket(struct netconn *newconn)
  193. {
  194. int i;
  195. /* Protect socket array */
  196. sys_sem_wait(socksem);
  197. /* allocate a new socket identifier */
  198. for (i = 0; i < NUM_SOCKETS; ++i) {
  199. if (!sockets[i].conn) {
  200. sockets[i].conn = newconn;
  201. sockets[i].lastdata = NULL;
  202. sockets[i].lastoffset = 0;
  203. sockets[i].rcvevent = 0;
  204. sockets[i].sendevent = 1; /* TCP send buf is empty */
  205. sockets[i].flags = 0;
  206. sockets[i].err = 0;
  207. sys_sem_signal(socksem);
  208. return i;
  209. }
  210. }
  211. sys_sem_signal(socksem);
  212. return -1;
  213. }
  214. int lwip_get_error(int s)
  215. {
  216. struct lwip_socket *sock;
  217. sock = get_socket(s);
  218. return sock->err;
  219. }
  220. /* Below this, the well-known socket functions are implemented.
  221. * Use google.com or opengroup.org to get a good description :-)
  222. *
  223. * Exceptions are documented!
  224. */
  225. int
  226. lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
  227. {
  228. struct lwip_socket *sock, *nsock;
  229. struct netconn *newconn;
  230. struct ip_addr naddr;
  231. u16_t port;
  232. int newsock;
  233. struct sockaddr_in sin;
  234. err_t err;
  235. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d)...\n", s));
  236. sock = get_socket(s);
  237. if (!sock)
  238. return -1;
  239. if ((sock->flags & O_NONBLOCK) && (sock->rcvevent <= 0)) {
  240. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d): returning EWOULDBLOCK\n", s));
  241. sock_set_errno(sock, EWOULDBLOCK);
  242. return -1;
  243. }
  244. newconn = netconn_accept(sock->conn);
  245. if (!newconn) {
  246. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d) failed, err=%d\n", s, sock->conn->err));
  247. sock_set_errno(sock, err_to_errno(sock->conn->err));
  248. return -1;
  249. }
  250. /* get the IP address and port of the remote host */
  251. err = netconn_peer(newconn, &naddr, &port);
  252. if (err != ERR_OK) {
  253. netconn_delete(newconn);
  254. sock_set_errno(sock, err_to_errno(err));
  255. return -1;
  256. }
  257. /* Note that POSIX only requires us to check addr is non-NULL. addrlen must
  258. * not be NULL if addr is valid.
  259. */
  260. if (NULL != addr) {
  261. LWIP_ASSERT("addr valid but addrlen NULL", addrlen != NULL);
  262. memset(&sin, 0, sizeof(sin));
  263. sin.sin_len = sizeof(sin);
  264. sin.sin_family = AF_INET;
  265. sin.sin_port = htons(port);
  266. sin.sin_addr.s_addr = naddr.addr;
  267. if (*addrlen > sizeof(sin))
  268. *addrlen = sizeof(sin);
  269. MEMCPY(addr, &sin, *addrlen);
  270. }
  271. newsock = alloc_socket(newconn);
  272. if (newsock == -1) {
  273. netconn_delete(newconn);
  274. sock_set_errno(sock, ENFILE);
  275. return -1;
  276. }
  277. LWIP_ASSERT("invalid socket index", (newsock >= 0) && (newsock < NUM_SOCKETS));
  278. newconn->callback = event_callback;
  279. nsock = &sockets[newsock];
  280. LWIP_ASSERT("invalid socket pointer", nsock != NULL);
  281. sys_sem_wait(socksem);
  282. /* See event_callback: If data comes in right away after an accept, even
  283. * though the server task might not have created a new socket yet.
  284. * In that case, newconn->socket is counted down (newconn->socket--),
  285. * so nsock->rcvevent is >= 1 here!
  286. */
  287. nsock->rcvevent += -1 - newconn->socket;
  288. newconn->socket = newsock;
  289. sys_sem_signal(socksem);
  290. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d) returning new sock=%d addr=", s, newsock));
  291. ip_addr_debug_print(SOCKETS_DEBUG, &naddr);
  292. LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F"\n", port));
  293. sock_set_errno(sock, 0);
  294. return newsock;
  295. }
  296. int
  297. lwip_bind(int s, const struct sockaddr *name, socklen_t namelen)
  298. {
  299. struct lwip_socket *sock;
  300. struct ip_addr local_addr;
  301. u16_t local_port;
  302. err_t err;
  303. sock = get_socket(s);
  304. if (!sock)
  305. return -1;
  306. LWIP_ERROR("lwip_bind: invalid address", ((namelen == sizeof(struct sockaddr_in)) &&
  307. ((((const struct sockaddr_in *)name)->sin_family) == AF_INET)),
  308. sock_set_errno(sock, err_to_errno(ERR_ARG)); return -1;);
  309. local_addr.addr = ((const struct sockaddr_in *)name)->sin_addr.s_addr;
  310. local_port = ((const struct sockaddr_in *)name)->sin_port;
  311. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d, addr=", s));
  312. ip_addr_debug_print(SOCKETS_DEBUG, &local_addr);
  313. LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F")\n", ntohs(local_port)));
  314. err = netconn_bind(sock->conn, &local_addr, ntohs(local_port));
  315. if (err != ERR_OK) {
  316. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d) failed, err=%d\n", s, err));
  317. sock_set_errno(sock, err_to_errno(err));
  318. return -1;
  319. }
  320. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d) succeeded\n", s));
  321. sock_set_errno(sock, 0);
  322. return 0;
  323. }
  324. int
  325. lwip_close(int s)
  326. {
  327. struct lwip_socket *sock;
  328. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_close(%d)\n", s));
  329. sock = get_socket(s);
  330. if (!sock) {
  331. return -1;
  332. }
  333. netconn_delete(sock->conn);
  334. sys_sem_wait(socksem);
  335. if (sock->lastdata) {
  336. netbuf_delete(sock->lastdata);
  337. }
  338. sock->lastdata = NULL;
  339. sock->lastoffset = 0;
  340. sock->conn = NULL;
  341. sock_set_errno(sock, 0);
  342. sys_sem_signal(socksem);
  343. return 0;
  344. }
  345. int
  346. lwip_connect(int s, const struct sockaddr *name, socklen_t namelen)
  347. {
  348. struct lwip_socket *sock;
  349. err_t err;
  350. sock = get_socket(s);
  351. if (!sock)
  352. return -1;
  353. LWIP_ERROR("lwip_connect: invalid address", ((namelen == sizeof(struct sockaddr_in)) &&
  354. ((((const struct sockaddr_in *)name)->sin_family) == AF_INET)),
  355. sock_set_errno(sock, err_to_errno(ERR_ARG)); return -1;);
  356. if (((const struct sockaddr_in *)name)->sin_family == AF_UNSPEC) {
  357. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, AF_UNSPEC)\n", s));
  358. err = netconn_disconnect(sock->conn);
  359. } else {
  360. struct ip_addr remote_addr;
  361. u16_t remote_port;
  362. remote_addr.addr = ((const struct sockaddr_in *)name)->sin_addr.s_addr;
  363. remote_port = ((const struct sockaddr_in *)name)->sin_port;
  364. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, addr=", s));
  365. ip_addr_debug_print(SOCKETS_DEBUG, &remote_addr);
  366. LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F")\n", ntohs(remote_port)));
  367. err = netconn_connect(sock->conn, &remote_addr, ntohs(remote_port));
  368. }
  369. if (err != ERR_OK) {
  370. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d) failed, err=%d\n", s, err));
  371. sock_set_errno(sock, err_to_errno(err));
  372. return -1;
  373. }
  374. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d) succeeded\n", s));
  375. sock_set_errno(sock, 0);
  376. return 0;
  377. }
  378. /**
  379. * Set a socket into listen mode.
  380. * The socket may not have been used for another connection previously.
  381. *
  382. * @param s the socket to set to listening mode
  383. * @param backlog (ATTENTION: need TCP_LISTEN_BACKLOG=1)
  384. * @return 0 on success, non-zero on failure
  385. */
  386. int
  387. lwip_listen(int s, int backlog)
  388. {
  389. struct lwip_socket *sock;
  390. err_t err;
  391. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_listen(%d, backlog=%d)\n", s, backlog));
  392. sock = get_socket(s);
  393. if (!sock)
  394. return -1;
  395. /* limit the "backlog" parameter to fit in an u8_t */
  396. if (backlog < 0) {
  397. backlog = 0;
  398. }
  399. if (backlog > 0xff) {
  400. backlog = 0xff;
  401. }
  402. err = netconn_listen_with_backlog(sock->conn, backlog);
  403. if (err != ERR_OK) {
  404. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_listen(%d) failed, err=%d\n", s, err));
  405. sock_set_errno(sock, err_to_errno(err));
  406. return -1;
  407. }
  408. sock_set_errno(sock, 0);
  409. return 0;
  410. }
  411. int
  412. lwip_recvfrom(int s, void *mem, size_t len, int flags,
  413. struct sockaddr *from, socklen_t *fromlen)
  414. {
  415. struct lwip_socket *sock;
  416. struct netbuf *buf;
  417. u16_t buflen, copylen, off = 0;
  418. struct ip_addr *addr;
  419. u16_t port;
  420. u8_t done = 0;
  421. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d, %p, %"SZT_F", 0x%x, ..)\n", s, mem, len, flags));
  422. sock = get_socket(s);
  423. if (!sock)
  424. return -1;
  425. do {
  426. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: top while sock->lastdata=%p\n", (void*)sock->lastdata));
  427. /* Check if there is data left from the last recv operation. */
  428. if (sock->lastdata) {
  429. buf = sock->lastdata;
  430. } else {
  431. /* If this is non-blocking call, then check first */
  432. if (((flags & MSG_DONTWAIT) || (sock->flags & O_NONBLOCK)) &&
  433. (sock->rcvevent <= 0)) {
  434. if (off > 0) {
  435. /* already received data, return that */
  436. sock_set_errno(sock, 0);
  437. return off;
  438. }
  439. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): returning EWOULDBLOCK\n", s));
  440. sock_set_errno(sock, EWOULDBLOCK);
  441. return -1;
  442. }
  443. /* No data was left from the previous operation, so we try to get
  444. some from the network. */
  445. sock->lastdata = buf = netconn_recv(sock->conn);
  446. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: netconn_recv netbuf=%p\n", (void*)buf));
  447. if (!buf) {
  448. if (off > 0) {
  449. /* already received data, return that */
  450. sock_set_errno(sock, 0);
  451. return off;
  452. }
  453. /* We should really do some error checking here. */
  454. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): buf == NULL!\n", s));
  455. sock_set_errno(sock, (((sock->conn->pcb.ip != NULL) && (sock->conn->err == ERR_OK))
  456. ? ETIMEDOUT : err_to_errno(sock->conn->err)));
  457. return 0;
  458. }
  459. }
  460. buflen = netbuf_len(buf);
  461. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: buflen=%"U16_F" len=%"SZT_F" off=%"U16_F" sock->lastoffset=%"U16_F"\n",
  462. buflen, len, off, sock->lastoffset));
  463. buflen -= sock->lastoffset;
  464. if (len > buflen) {
  465. copylen = buflen;
  466. } else {
  467. copylen = (u16_t)len;
  468. }
  469. /* copy the contents of the received buffer into
  470. the supplied memory pointer mem */
  471. netbuf_copy_partial(buf, (u8_t*)mem + off, copylen, sock->lastoffset);
  472. off += copylen;
  473. if (netconn_type(sock->conn) == NETCONN_TCP) {
  474. LWIP_ASSERT("invalid copylen, len would underflow", len >= copylen);
  475. len -= copylen;
  476. if ( (len <= 0) ||
  477. (buf->p->flags & PBUF_FLAG_PUSH) ||
  478. (sock->rcvevent <= 0) ||
  479. ((flags & MSG_PEEK)!=0)) {
  480. done = 1;
  481. }
  482. } else {
  483. done = 1;
  484. }
  485. /* Check to see from where the data was.*/
  486. if (done) {
  487. if (from && fromlen) {
  488. struct sockaddr_in sin;
  489. if (netconn_type(sock->conn) == NETCONN_TCP) {
  490. addr = (struct ip_addr*)&(sin.sin_addr.s_addr);
  491. netconn_getaddr(sock->conn, addr, &port, 0);
  492. } else {
  493. addr = netbuf_fromaddr(buf);
  494. port = netbuf_fromport(buf);
  495. }
  496. memset(&sin, 0, sizeof(sin));
  497. sin.sin_len = sizeof(sin);
  498. sin.sin_family = AF_INET;
  499. sin.sin_port = htons(port);
  500. sin.sin_addr.s_addr = addr->addr;
  501. if (*fromlen > sizeof(sin)) {
  502. *fromlen = sizeof(sin);
  503. }
  504. MEMCPY(from, &sin, *fromlen);
  505. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): addr=", s));
  506. ip_addr_debug_print(SOCKETS_DEBUG, addr);
  507. LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F" len=%"U16_F"\n", port, off));
  508. } else {
  509. #if SOCKETS_DEBUG
  510. struct sockaddr_in sin;
  511. if (netconn_type(sock->conn) == NETCONN_TCP) {
  512. addr = (struct ip_addr*)&(sin.sin_addr.s_addr);
  513. netconn_getaddr(sock->conn, addr, &port, 0);
  514. } else {
  515. addr = netbuf_fromaddr(buf);
  516. port = netbuf_fromport(buf);
  517. }
  518. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): addr=", s));
  519. ip_addr_debug_print(SOCKETS_DEBUG, addr);
  520. LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F" len=%"U16_F"\n", port, off));
  521. #endif /* SOCKETS_DEBUG */
  522. }
  523. }
  524. /* If we don't peek the incoming message... */
  525. if ((flags & MSG_PEEK)==0) {
  526. /* If this is a TCP socket, check if there is data left in the
  527. buffer. If so, it should be saved in the sock structure for next
  528. time around. */
  529. if ((netconn_type(sock->conn) == NETCONN_TCP) && (buflen - copylen > 0)) {
  530. sock->lastdata = buf;
  531. sock->lastoffset += copylen;
  532. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: lastdata now netbuf=%p\n", (void*)buf));
  533. } else {
  534. sock->lastdata = NULL;
  535. sock->lastoffset = 0;
  536. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: deleting netbuf=%p\n", (void*)buf));
  537. netbuf_delete(buf);
  538. }
  539. }
  540. } while (!done);
  541. sock_set_errno(sock, 0);
  542. return off;
  543. }
  544. int
  545. lwip_read(int s, void *mem, size_t len)
  546. {
  547. return lwip_recvfrom(s, mem, len, 0, NULL, NULL);
  548. }
  549. int
  550. lwip_recv(int s, void *mem, size_t len, int flags)
  551. {
  552. return lwip_recvfrom(s, mem, len, flags, NULL, NULL);
  553. }
  554. int
  555. lwip_send(int s, const void *data, size_t size, int flags)
  556. {
  557. struct lwip_socket *sock;
  558. err_t err;
  559. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d, data=%p, size=%"SZT_F", flags=0x%x)\n",
  560. s, data, size, flags));
  561. sock = get_socket(s);
  562. if (!sock)
  563. return -1;
  564. if (sock->conn->type != NETCONN_TCP) {
  565. #if (LWIP_UDP || LWIP_RAW)
  566. return lwip_sendto(s, data, size, flags, NULL, 0);
  567. #else
  568. sock_set_errno(sock, err_to_errno(ERR_ARG));
  569. return -1;
  570. #endif /* (LWIP_UDP || LWIP_RAW) */
  571. }
  572. err = netconn_write(sock->conn, data, size, NETCONN_COPY | ((flags & MSG_MORE)?NETCONN_MORE:0));
  573. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d) err=%d size=%"SZT_F"\n", s, err, size));
  574. sock_set_errno(sock, err_to_errno(err));
  575. return (err == ERR_OK ? (int)size : -1);
  576. }
  577. int
  578. lwip_sendto(int s, const void *data, size_t size, int flags,
  579. const struct sockaddr *to, socklen_t tolen)
  580. {
  581. struct lwip_socket *sock;
  582. struct ip_addr remote_addr;
  583. err_t err;
  584. u16_t short_size;
  585. #if !LWIP_TCPIP_CORE_LOCKING
  586. struct netbuf buf;
  587. u16_t remote_port;
  588. #endif
  589. sock = get_socket(s);
  590. if (!sock)
  591. return -1;
  592. if (sock->conn->type == NETCONN_TCP) {
  593. #if LWIP_TCP
  594. return lwip_send(s, data, size, flags);
  595. #else
  596. sock_set_errno(sock, err_to_errno(ERR_ARG));
  597. return -1;
  598. #endif /* LWIP_TCP */
  599. }
  600. LWIP_ASSERT("lwip_sendto: size must fit in u16_t", size <= 0xffff);
  601. short_size = (u16_t)size;
  602. LWIP_ERROR("lwip_sendto: invalid address", (((to == NULL) && (tolen == 0)) ||
  603. ((tolen == sizeof(struct sockaddr_in)) &&
  604. ((((const struct sockaddr_in *)to)->sin_family) == AF_INET))),
  605. sock_set_errno(sock, err_to_errno(ERR_ARG)); return -1;);
  606. #if LWIP_TCPIP_CORE_LOCKING
  607. /* Should only be consider like a sample or a simple way to experiment this option (no check of "to" field...) */
  608. { struct pbuf* p;
  609. p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF);
  610. if (p == NULL) {
  611. err = ERR_MEM;
  612. } else {
  613. p->payload = (void*)data;
  614. p->len = p->tot_len = short_size;
  615. remote_addr.addr = ((const struct sockaddr_in *)to)->sin_addr.s_addr;
  616. LOCK_TCPIP_CORE();
  617. if (sock->conn->type==NETCONN_RAW) {
  618. err = sock->conn->err = raw_sendto(sock->conn->pcb.raw, p, &remote_addr);
  619. } else {
  620. err = sock->conn->err = udp_sendto(sock->conn->pcb.udp, p, &remote_addr, ntohs(((const struct sockaddr_in *)to)->sin_port));
  621. }
  622. UNLOCK_TCPIP_CORE();
  623. pbuf_free(p);
  624. }
  625. }
  626. #else
  627. /* initialize a buffer */
  628. buf.p = buf.ptr = NULL;
  629. if (to) {
  630. remote_addr.addr = ((const struct sockaddr_in *)to)->sin_addr.s_addr;
  631. remote_port = ntohs(((const struct sockaddr_in *)to)->sin_port);
  632. buf.addr = &remote_addr;
  633. buf.port = remote_port;
  634. } else {
  635. remote_addr.addr = 0;
  636. remote_port = 0;
  637. buf.addr = NULL;
  638. buf.port = 0;
  639. }
  640. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_sendto(%d, data=%p, short_size=%d"U16_F", flags=0x%x to=",
  641. s, data, short_size, flags));
  642. ip_addr_debug_print(SOCKETS_DEBUG, &remote_addr);
  643. LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F"\n", remote_port));
  644. /* make the buffer point to the data that should be sent */
  645. #if LWIP_NETIF_TX_SINGLE_PBUF
  646. /* Allocate a new netbuf and copy the data into it. */
  647. if (netbuf_alloc(&buf, short_size) == NULL) {
  648. err = ERR_MEM;
  649. } else {
  650. err = netbuf_take(&buf, data, short_size);
  651. }
  652. #else /* LWIP_NETIF_TX_SINGLE_PBUF */
  653. err = netbuf_ref(&buf, data, short_size);
  654. #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
  655. if (err == ERR_OK) {
  656. /* send the data */
  657. err = netconn_send(sock->conn, &buf);
  658. }
  659. /* deallocated the buffer */
  660. netbuf_free(&buf);
  661. #endif /* LWIP_TCPIP_CORE_LOCKING */
  662. sock_set_errno(sock, err_to_errno(err));
  663. return (err == ERR_OK ? short_size : -1);
  664. }
  665. int
  666. lwip_socket(int domain, int type, int protocol)
  667. {
  668. struct netconn *conn;
  669. int i;
  670. LWIP_UNUSED_ARG(domain);
  671. /* create a netconn */
  672. switch (type) {
  673. case SOCK_RAW:
  674. conn = netconn_new_with_proto_and_callback(NETCONN_RAW, (u8_t)protocol, event_callback);
  675. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_RAW, %d) = ",
  676. domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
  677. break;
  678. case SOCK_DGRAM:
  679. conn = netconn_new_with_callback( (protocol == IPPROTO_UDPLITE) ?
  680. NETCONN_UDPLITE : NETCONN_UDP, event_callback);
  681. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_DGRAM, %d) = ",
  682. domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
  683. break;
  684. case SOCK_STREAM:
  685. conn = netconn_new_with_callback(NETCONN_TCP, event_callback);
  686. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_STREAM, %d) = ",
  687. domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
  688. break;
  689. default:
  690. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%d, %d/UNKNOWN, %d) = -1\n",
  691. domain, type, protocol));
  692. set_errno(EINVAL);
  693. return -1;
  694. }
  695. if (!conn) {
  696. LWIP_DEBUGF(SOCKETS_DEBUG, ("-1 / ENOBUFS (could not create netconn)\n"));
  697. set_errno(ENOBUFS);
  698. return -1;
  699. }
  700. i = alloc_socket(conn);
  701. if (i == -1) {
  702. netconn_delete(conn);
  703. set_errno(ENFILE);
  704. return -1;
  705. }
  706. conn->socket = i;
  707. LWIP_DEBUGF(SOCKETS_DEBUG, ("%d\n", i));
  708. set_errno(0);
  709. return i;
  710. }
  711. int
  712. lwip_write(int s, const void *data, size_t size)
  713. {
  714. return lwip_send(s, data, size, 0);
  715. }
  716. /**
  717. * Go through the readset and writeset lists and see which socket of the sockets
  718. * set in the sets has events. On return, readset, writeset and exceptset have
  719. * the sockets enabled that had events.
  720. *
  721. * exceptset is not used for now!!!
  722. *
  723. * @param maxfdp1 the highest socket index in the sets
  724. * @param readset in: set of sockets to check for read events;
  725. * out: set of sockets that had read events
  726. * @param writeset in: set of sockets to check for write events;
  727. * out: set of sockets that had write events
  728. * @param exceptset not yet implemented
  729. * @return number of sockets that had events (read+write)
  730. */
  731. static int
  732. lwip_selscan(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset)
  733. {
  734. int i, nready = 0;
  735. fd_set lreadset, lwriteset, lexceptset;
  736. struct lwip_socket *p_sock;
  737. FD_ZERO(&lreadset);
  738. FD_ZERO(&lwriteset);
  739. FD_ZERO(&lexceptset);
  740. /* Go through each socket in each list to count number of sockets which
  741. currently match */
  742. for(i = 0; i < maxfdp1; i++) {
  743. if (FD_ISSET(i, readset)) {
  744. /* See if netconn of this socket is ready for read */
  745. p_sock = get_socket(i);
  746. if (p_sock && (p_sock->lastdata || (p_sock->rcvevent > 0))) {
  747. FD_SET(i, &lreadset);
  748. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_selscan: fd=%d ready for reading\n", i));
  749. nready++;
  750. }
  751. }
  752. if (FD_ISSET(i, writeset)) {
  753. /* See if netconn of this socket is ready for write */
  754. p_sock = get_socket(i);
  755. if (p_sock && p_sock->sendevent) {
  756. FD_SET(i, &lwriteset);
  757. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_selscan: fd=%d ready for writing\n", i));
  758. nready++;
  759. }
  760. }
  761. }
  762. *readset = lreadset;
  763. *writeset = lwriteset;
  764. FD_ZERO(exceptset);
  765. return nready;
  766. }
  767. /**
  768. * Processing exceptset is not yet implemented.
  769. */
  770. int
  771. lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
  772. struct timeval *timeout)
  773. {
  774. int i;
  775. int nready;
  776. fd_set lreadset, lwriteset, lexceptset;
  777. u32_t msectimeout;
  778. struct lwip_select_cb select_cb;
  779. struct lwip_select_cb *p_selcb;
  780. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select(%d, %p, %p, %p, tvsec=%ld tvusec=%ld)\n",
  781. maxfdp1, (void *)readset, (void *) writeset, (void *) exceptset,
  782. timeout ? (long)timeout->tv_sec : (long)-1,
  783. timeout ? (long)timeout->tv_usec : (long)-1));
  784. select_cb.next = 0;
  785. select_cb.readset = readset;
  786. select_cb.writeset = writeset;
  787. select_cb.exceptset = exceptset;
  788. select_cb.sem_signalled = 0;
  789. /* Protect ourselves searching through the list */
  790. sys_sem_wait(selectsem);
  791. if (readset)
  792. lreadset = *readset;
  793. else
  794. FD_ZERO(&lreadset);
  795. if (writeset)
  796. lwriteset = *writeset;
  797. else
  798. FD_ZERO(&lwriteset);
  799. if (exceptset)
  800. lexceptset = *exceptset;
  801. else
  802. FD_ZERO(&lexceptset);
  803. /* Go through each socket in each list to count number of sockets which
  804. currently match */
  805. nready = lwip_selscan(maxfdp1, &lreadset, &lwriteset, &lexceptset);
  806. /* If we don't have any current events, then suspend if we are supposed to */
  807. if (!nready) {
  808. if (timeout && timeout->tv_sec == 0 && timeout->tv_usec == 0) {
  809. sys_sem_signal(selectsem);
  810. if (readset)
  811. FD_ZERO(readset);
  812. if (writeset)
  813. FD_ZERO(writeset);
  814. if (exceptset)
  815. FD_ZERO(exceptset);
  816. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: no timeout, returning 0\n"));
  817. set_errno(0);
  818. return 0;
  819. }
  820. /* add our semaphore to list */
  821. /* We don't actually need any dynamic memory. Our entry on the
  822. * list is only valid while we are in this function, so it's ok
  823. * to use local variables */
  824. select_cb.sem = sys_sem_new(0);
  825. /* Note that we are still protected */
  826. /* Put this select_cb on top of list */
  827. select_cb.next = select_cb_list;
  828. select_cb_list = &select_cb;
  829. /* Now we can safely unprotect */
  830. sys_sem_signal(selectsem);
  831. /* Now just wait to be woken */
  832. if (timeout == 0)
  833. /* Wait forever */
  834. msectimeout = 0;
  835. else {
  836. msectimeout = ((timeout->tv_sec * 1000) + ((timeout->tv_usec + 500)/1000));
  837. if(msectimeout == 0)
  838. msectimeout = 1;
  839. }
  840. if (sys_arch_timeouts() == NULL){
  841. /* it's not a lwip thread, use os semaphore with timeout to handle it */
  842. i = sys_arch_sem_wait(select_cb.sem, msectimeout);
  843. if (i == SYS_ARCH_TIMEOUT) i = 0;
  844. else i = 1;
  845. }
  846. else {
  847. /* it's a lwip thread, use os semaphore with timeout to handle it */
  848. i = sys_sem_wait_timeout(select_cb.sem, msectimeout);
  849. }
  850. /* Take us off the list */
  851. sys_sem_wait(selectsem);
  852. if (select_cb_list == &select_cb)
  853. select_cb_list = select_cb.next;
  854. else
  855. for (p_selcb = select_cb_list; p_selcb; p_selcb = p_selcb->next) {
  856. if (p_selcb->next == &select_cb) {
  857. p_selcb->next = select_cb.next;
  858. break;
  859. }
  860. }
  861. sys_sem_signal(selectsem);
  862. sys_sem_free(select_cb.sem);
  863. if (i == 0) {
  864. /* Timeout */
  865. if (readset)
  866. FD_ZERO(readset);
  867. if (writeset)
  868. FD_ZERO(writeset);
  869. if (exceptset)
  870. FD_ZERO(exceptset);
  871. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: timeout expired\n"));
  872. set_errno(0);
  873. return 0;
  874. }
  875. if (readset)
  876. lreadset = *readset;
  877. else
  878. FD_ZERO(&lreadset);
  879. if (writeset)
  880. lwriteset = *writeset;
  881. else
  882. FD_ZERO(&lwriteset);
  883. if (exceptset)
  884. lexceptset = *exceptset;
  885. else
  886. FD_ZERO(&lexceptset);
  887. /* See what's set */
  888. nready = lwip_selscan(maxfdp1, &lreadset, &lwriteset, &lexceptset);
  889. } else
  890. sys_sem_signal(selectsem);
  891. if (readset)
  892. *readset = lreadset;
  893. if (writeset)
  894. *writeset = lwriteset;
  895. if (exceptset)
  896. *exceptset = lexceptset;
  897. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: nready=%d\n", nready));
  898. set_errno(0);
  899. return nready;
  900. }
  901. /**
  902. * Callback registered in the netconn layer for each socket-netconn.
  903. * Processes recvevent (data available) and wakes up tasks waiting for select.
  904. */
  905. static void
  906. event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len)
  907. {
  908. int s;
  909. struct lwip_socket *sock;
  910. struct lwip_select_cb *scb;
  911. LWIP_UNUSED_ARG(len);
  912. /* Get socket */
  913. if (conn) {
  914. s = conn->socket;
  915. if (s < 0) {
  916. /* Data comes in right away after an accept, even though
  917. * the server task might not have created a new socket yet.
  918. * Just count down (or up) if that's the case and we
  919. * will use the data later. Note that only receive events
  920. * can happen before the new socket is set up. */
  921. sys_sem_wait(socksem);
  922. if (conn->socket < 0) {
  923. if (evt == NETCONN_EVT_RCVPLUS) {
  924. conn->socket--;
  925. }
  926. sys_sem_signal(socksem);
  927. return;
  928. }
  929. s = conn->socket;
  930. sys_sem_signal(socksem);
  931. }
  932. sock = get_socket(s);
  933. if (!sock) {
  934. return;
  935. }
  936. } else {
  937. return;
  938. }
  939. sys_sem_wait(selectsem);
  940. /* Set event as required */
  941. switch (evt) {
  942. case NETCONN_EVT_RCVPLUS:
  943. sock->rcvevent++;
  944. break;
  945. case NETCONN_EVT_RCVMINUS:
  946. sock->rcvevent--;
  947. break;
  948. case NETCONN_EVT_SENDPLUS:
  949. sock->sendevent = 1;
  950. break;
  951. case NETCONN_EVT_SENDMINUS:
  952. sock->sendevent = 0;
  953. break;
  954. default:
  955. LWIP_ASSERT("unknown event", 0);
  956. break;
  957. }
  958. sys_sem_signal(selectsem);
  959. /* Now decide if anyone is waiting for this socket */
  960. /* NOTE: This code is written this way to protect the select link list
  961. but to avoid a deadlock situation by releasing socksem before
  962. signalling for the select. This means we need to go through the list
  963. multiple times ONLY IF a select was actually waiting. We go through
  964. the list the number of waiting select calls + 1. This list is
  965. expected to be small. */
  966. while (1) {
  967. sys_sem_wait(selectsem);
  968. for (scb = select_cb_list; scb; scb = scb->next) {
  969. if (scb->sem_signalled == 0) {
  970. /* Test this select call for our socket */
  971. if (scb->readset && FD_ISSET(s, scb->readset))
  972. if (sock->rcvevent > 0)
  973. break;
  974. if (scb->writeset && FD_ISSET(s, scb->writeset))
  975. if (sock->sendevent)
  976. break;
  977. }
  978. }
  979. if (scb) {
  980. scb->sem_signalled = 1;
  981. sys_sem_signal(scb->sem);
  982. sys_sem_signal(selectsem);
  983. } else {
  984. sys_sem_signal(selectsem);
  985. break;
  986. }
  987. }
  988. }
  989. /**
  990. * Unimplemented: Close one end of a full-duplex connection.
  991. * Currently, the full connection is closed.
  992. */
  993. int
  994. lwip_shutdown(int s, int how)
  995. {
  996. LWIP_UNUSED_ARG(how);
  997. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_shutdown(%d, how=%d)\n", s, how));
  998. return lwip_close(s); /* XXX temporary hack until proper implementation */
  999. }
  1000. static int
  1001. lwip_getaddrname(int s, struct sockaddr *name, socklen_t *namelen, u8_t local)
  1002. {
  1003. struct lwip_socket *sock;
  1004. struct sockaddr_in sin;
  1005. struct ip_addr naddr;
  1006. sock = get_socket(s);
  1007. if (!sock)
  1008. return -1;
  1009. memset(&sin, 0, sizeof(sin));
  1010. sin.sin_len = sizeof(sin);
  1011. sin.sin_family = AF_INET;
  1012. /* get the IP address and port */
  1013. netconn_getaddr(sock->conn, &naddr, &sin.sin_port, local);
  1014. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getaddrname(%d, addr=", s));
  1015. ip_addr_debug_print(SOCKETS_DEBUG, &naddr);
  1016. LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F")\n", sin.sin_port));
  1017. sin.sin_port = htons(sin.sin_port);
  1018. sin.sin_addr.s_addr = naddr.addr;
  1019. if (*namelen > sizeof(sin))
  1020. *namelen = sizeof(sin);
  1021. MEMCPY(name, &sin, *namelen);
  1022. sock_set_errno(sock, 0);
  1023. return 0;
  1024. }
  1025. int
  1026. lwip_getpeername(int s, struct sockaddr *name, socklen_t *namelen)
  1027. {
  1028. return lwip_getaddrname(s, name, namelen, 0);
  1029. }
  1030. int
  1031. lwip_getsockname(int s, struct sockaddr *name, socklen_t *namelen)
  1032. {
  1033. return lwip_getaddrname(s, name, namelen, 1);
  1034. }
  1035. int
  1036. lwip_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen)
  1037. {
  1038. err_t err = ERR_OK;
  1039. struct lwip_socket *sock = get_socket(s);
  1040. struct lwip_setgetsockopt_data data;
  1041. if (!sock)
  1042. return -1;
  1043. if ((NULL == optval) || (NULL == optlen)) {
  1044. sock_set_errno(sock, EFAULT);
  1045. return -1;
  1046. }
  1047. /* Do length and type checks for the various options first, to keep it readable. */
  1048. switch (level) {
  1049. /* Level: SOL_SOCKET */
  1050. case SOL_SOCKET:
  1051. switch (optname) {
  1052. case SO_ACCEPTCONN:
  1053. case SO_BROADCAST:
  1054. /* UNIMPL case SO_DEBUG: */
  1055. /* UNIMPL case SO_DONTROUTE: */
  1056. case SO_ERROR:
  1057. case SO_KEEPALIVE:
  1058. /* UNIMPL case SO_CONTIMEO: */
  1059. /* UNIMPL case SO_SNDTIMEO: */
  1060. #if LWIP_SO_RCVTIMEO
  1061. case SO_RCVTIMEO:
  1062. #endif /* LWIP_SO_RCVTIMEO */
  1063. #if LWIP_SO_RCVBUF
  1064. case SO_RCVBUF:
  1065. #endif /* LWIP_SO_RCVBUF */
  1066. /* UNIMPL case SO_OOBINLINE: */
  1067. /* UNIMPL case SO_SNDBUF: */
  1068. /* UNIMPL case SO_RCVLOWAT: */
  1069. /* UNIMPL case SO_SNDLOWAT: */
  1070. #if SO_REUSE
  1071. case SO_REUSEADDR:
  1072. case SO_REUSEPORT:
  1073. #endif /* SO_REUSE */
  1074. case SO_TYPE:
  1075. /* UNIMPL case SO_USELOOPBACK: */
  1076. if (*optlen < sizeof(int)) {
  1077. err = EINVAL;
  1078. }
  1079. break;
  1080. case SO_NO_CHECK:
  1081. if (*optlen < sizeof(int)) {
  1082. err = EINVAL;
  1083. }
  1084. #if LWIP_UDP
  1085. if ((sock->conn->type != NETCONN_UDP) ||
  1086. ((udp_flags(sock->conn->pcb.udp) & UDP_FLAGS_UDPLITE) != 0)) {
  1087. /* this flag is only available for UDP, not for UDP lite */
  1088. err = EAFNOSUPPORT;
  1089. }
  1090. #endif /* LWIP_UDP */
  1091. break;
  1092. default:
  1093. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, UNIMPL: optname=0x%x, ..)\n",
  1094. s, optname));
  1095. err = ENOPROTOOPT;
  1096. } /* switch (optname) */
  1097. break;
  1098. /* Level: IPPROTO_IP */
  1099. case IPPROTO_IP:
  1100. switch (optname) {
  1101. /* UNIMPL case IP_HDRINCL: */
  1102. /* UNIMPL case IP_RCVDSTADDR: */
  1103. /* UNIMPL case IP_RCVIF: */
  1104. case IP_TTL:
  1105. case IP_TOS:
  1106. if (*optlen < sizeof(int)) {
  1107. err = EINVAL;
  1108. }
  1109. break;
  1110. #if LWIP_IGMP
  1111. case IP_MULTICAST_TTL:
  1112. if (*optlen < sizeof(u8_t)) {
  1113. err = EINVAL;
  1114. }
  1115. break;
  1116. case IP_MULTICAST_IF:
  1117. if (*optlen < sizeof(struct in_addr)) {
  1118. err = EINVAL;
  1119. }
  1120. break;
  1121. #endif /* LWIP_IGMP */
  1122. default:
  1123. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, UNIMPL: optname=0x%x, ..)\n",
  1124. s, optname));
  1125. err = ENOPROTOOPT;
  1126. } /* switch (optname) */
  1127. break;
  1128. #if LWIP_TCP
  1129. /* Level: IPPROTO_TCP */
  1130. case IPPROTO_TCP:
  1131. if (*optlen < sizeof(int)) {
  1132. err = EINVAL;
  1133. break;
  1134. }
  1135. /* If this is no TCP socket, ignore any options. */
  1136. if (sock->conn->type != NETCONN_TCP)
  1137. return 0;
  1138. switch (optname) {
  1139. case TCP_NODELAY:
  1140. case TCP_KEEPALIVE:
  1141. #if LWIP_TCP_KEEPALIVE
  1142. case TCP_KEEPIDLE:
  1143. case TCP_KEEPINTVL:
  1144. case TCP_KEEPCNT:
  1145. #endif /* LWIP_TCP_KEEPALIVE */
  1146. break;
  1147. default:
  1148. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, UNIMPL: optname=0x%x, ..)\n",
  1149. s, optname));
  1150. err = ENOPROTOOPT;
  1151. } /* switch (optname) */
  1152. break;
  1153. #endif /* LWIP_TCP */
  1154. #if LWIP_UDP && LWIP_UDPLITE
  1155. /* Level: IPPROTO_UDPLITE */
  1156. case IPPROTO_UDPLITE:
  1157. if (*optlen < sizeof(int)) {
  1158. err = EINVAL;
  1159. break;
  1160. }
  1161. /* If this is no UDP lite socket, ignore any options. */
  1162. if (sock->conn->type != NETCONN_UDPLITE)
  1163. return 0;
  1164. switch (optname) {
  1165. case UDPLITE_SEND_CSCOV:
  1166. case UDPLITE_RECV_CSCOV:
  1167. break;
  1168. default:
  1169. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UNIMPL: optname=0x%x, ..)\n",
  1170. s, optname));
  1171. err = ENOPROTOOPT;
  1172. } /* switch (optname) */
  1173. break;
  1174. #endif /* LWIP_UDP && LWIP_UDPLITE*/
  1175. /* UNDEFINED LEVEL */
  1176. default:
  1177. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, level=0x%x, UNIMPL: optname=0x%x, ..)\n",
  1178. s, level, optname));
  1179. err = ENOPROTOOPT;
  1180. } /* switch */
  1181. if (err != ERR_OK) {
  1182. sock_set_errno(sock, err);
  1183. return -1;
  1184. }
  1185. /* Now do the actual option processing */
  1186. data.sock = sock;
  1187. data.level = level;
  1188. data.optname = optname;
  1189. data.optval = optval;
  1190. data.optlen = optlen;
  1191. data.err = err;
  1192. tcpip_callback(lwip_getsockopt_internal, &data);
  1193. sys_arch_sem_wait(sock->conn->op_completed, 0);
  1194. /* maybe lwip_getsockopt_internal has changed err */
  1195. err = data.err;
  1196. sock_set_errno(sock, err);
  1197. return err ? -1 : 0;
  1198. }
  1199. static void
  1200. lwip_getsockopt_internal(void *arg)
  1201. {
  1202. struct lwip_socket *sock;
  1203. #ifdef LWIP_DEBUG
  1204. int s;
  1205. #endif /* LWIP_DEBUG */
  1206. int level, optname;
  1207. void *optval;
  1208. struct lwip_setgetsockopt_data *data;
  1209. LWIP_ASSERT("arg != NULL", arg != NULL);
  1210. data = (struct lwip_setgetsockopt_data*)arg;
  1211. sock = data->sock;
  1212. #ifdef LWIP_DEBUG
  1213. s = data->s;
  1214. #endif /* LWIP_DEBUG */
  1215. level = data->level;
  1216. optname = data->optname;
  1217. optval = data->optval;
  1218. switch (level) {
  1219. /* Level: SOL_SOCKET */
  1220. case SOL_SOCKET:
  1221. switch (optname) {
  1222. /* The option flags */
  1223. case SO_ACCEPTCONN:
  1224. case SO_BROADCAST:
  1225. /* UNIMPL case SO_DEBUG: */
  1226. /* UNIMPL case SO_DONTROUTE: */
  1227. case SO_KEEPALIVE:
  1228. /* UNIMPL case SO_OOBINCLUDE: */
  1229. #if SO_REUSE
  1230. case SO_REUSEADDR:
  1231. case SO_REUSEPORT:
  1232. #endif /* SO_REUSE */
  1233. /*case SO_USELOOPBACK: UNIMPL */
  1234. *(int*)optval = sock->conn->pcb.ip->so_options & optname;
  1235. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, optname=0x%x, ..) = %s\n",
  1236. s, optname, (*(int*)optval?"on":"off")));
  1237. break;
  1238. case SO_TYPE:
  1239. switch (NETCONNTYPE_GROUP(sock->conn->type)) {
  1240. case NETCONN_RAW:
  1241. *(int*)optval = SOCK_RAW;
  1242. break;
  1243. case NETCONN_TCP:
  1244. *(int*)optval = SOCK_STREAM;
  1245. break;
  1246. case NETCONN_UDP:
  1247. *(int*)optval = SOCK_DGRAM;
  1248. break;
  1249. default: /* unrecognized socket type */
  1250. *(int*)optval = sock->conn->type;
  1251. LWIP_DEBUGF(SOCKETS_DEBUG,
  1252. ("lwip_getsockopt(%d, SOL_SOCKET, SO_TYPE): unrecognized socket type %d\n",
  1253. s, *(int *)optval));
  1254. } /* switch (sock->conn->type) */
  1255. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_TYPE) = %d\n",
  1256. s, *(int *)optval));
  1257. break;
  1258. case SO_ERROR:
  1259. if (sock->err == 0) {
  1260. sock_set_errno(sock, err_to_errno(sock->conn->err));
  1261. }
  1262. *(int *)optval = sock->err;
  1263. sock->err = 0;
  1264. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_ERROR) = %d\n",
  1265. s, *(int *)optval));
  1266. break;
  1267. #if LWIP_SO_RCVTIMEO
  1268. case SO_RCVTIMEO:
  1269. *(int *)optval = sock->conn->recv_timeout;
  1270. break;
  1271. #endif /* LWIP_SO_RCVTIMEO */
  1272. #if LWIP_SO_RCVBUF
  1273. case SO_RCVBUF:
  1274. *(int *)optval = sock->conn->recv_bufsize;
  1275. break;
  1276. #endif /* LWIP_SO_RCVBUF */
  1277. #if LWIP_UDP
  1278. case SO_NO_CHECK:
  1279. *(int*)optval = (udp_flags(sock->conn->pcb.udp) & UDP_FLAGS_NOCHKSUM) ? 1 : 0;
  1280. break;
  1281. #endif /* LWIP_UDP*/
  1282. } /* switch (optname) */
  1283. break;
  1284. /* Level: IPPROTO_IP */
  1285. case IPPROTO_IP:
  1286. switch (optname) {
  1287. case IP_TTL:
  1288. *(int*)optval = sock->conn->pcb.ip->ttl;
  1289. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_TTL) = %d\n",
  1290. s, *(int *)optval));
  1291. break;
  1292. case IP_TOS:
  1293. *(int*)optval = sock->conn->pcb.ip->tos;
  1294. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_TOS) = %d\n",
  1295. s, *(int *)optval));
  1296. break;
  1297. #if LWIP_IGMP
  1298. case IP_MULTICAST_TTL:
  1299. *(u8_t*)optval = sock->conn->pcb.ip->ttl;
  1300. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_MULTICAST_TTL) = %d\n",
  1301. s, *(int *)optval));
  1302. break;
  1303. case IP_MULTICAST_IF:
  1304. ((struct in_addr*) optval)->s_addr = sock->conn->pcb.udp->multicast_ip.addr;
  1305. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_MULTICAST_IF) = 0x%"X32_F"\n",
  1306. s, *(u32_t *)optval));
  1307. break;
  1308. #endif /* LWIP_IGMP */
  1309. } /* switch (optname) */
  1310. break;
  1311. #if LWIP_TCP
  1312. /* Level: IPPROTO_TCP */
  1313. case IPPROTO_TCP:
  1314. switch (optname) {
  1315. case TCP_NODELAY:
  1316. *(int*)optval = tcp_nagle_disabled(sock->conn->pcb.tcp);
  1317. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, TCP_NODELAY) = %s\n",
  1318. s, (*(int*)optval)?"on":"off") );
  1319. break;
  1320. case TCP_KEEPALIVE:
  1321. *(int*)optval = (int)sock->conn->pcb.tcp->keep_idle;
  1322. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, TCP_KEEPALIVE) = %d\n",
  1323. s, *(int *)optval));
  1324. break;
  1325. #if LWIP_TCP_KEEPALIVE
  1326. case TCP_KEEPIDLE:
  1327. *(int*)optval = (int)(sock->conn->pcb.tcp->keep_idle/1000);
  1328. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, TCP_KEEPIDLE) = %d\n",
  1329. s, *(int *)optval));
  1330. break;
  1331. case TCP_KEEPINTVL:
  1332. *(int*)optval = (int)(sock->conn->pcb.tcp->keep_intvl/1000);
  1333. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, TCP_KEEPINTVL) = %d\n",
  1334. s, *(int *)optval));
  1335. break;
  1336. case TCP_KEEPCNT:
  1337. *(int*)optval = (int)sock->conn->pcb.tcp->keep_cnt;
  1338. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, TCP_KEEPCNT) = %d\n",
  1339. s, *(int *)optval));
  1340. break;
  1341. #endif /* LWIP_TCP_KEEPALIVE */
  1342. } /* switch (optname) */
  1343. break;
  1344. #endif /* LWIP_TCP */
  1345. #if LWIP_UDP && LWIP_UDPLITE
  1346. /* Level: IPPROTO_UDPLITE */
  1347. case IPPROTO_UDPLITE:
  1348. switch (optname) {
  1349. case UDPLITE_SEND_CSCOV:
  1350. *(int*)optval = sock->conn->pcb.udp->chksum_len_tx;
  1351. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UDPLITE_SEND_CSCOV) = %d\n",
  1352. s, (*(int*)optval)) );
  1353. break;
  1354. case UDPLITE_RECV_CSCOV:
  1355. *(int*)optval = sock->conn->pcb.udp->chksum_len_rx;
  1356. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UDPLITE_RECV_CSCOV) = %d\n",
  1357. s, (*(int*)optval)) );
  1358. break;
  1359. } /* switch (optname) */
  1360. break;
  1361. #endif /* LWIP_UDP */
  1362. } /* switch (level) */
  1363. sys_sem_signal(sock->conn->op_completed);
  1364. }
  1365. int
  1366. lwip_setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen)
  1367. {
  1368. struct lwip_socket *sock = get_socket(s);
  1369. int err = ERR_OK;
  1370. struct lwip_setgetsockopt_data data;
  1371. if (!sock)
  1372. return -1;
  1373. if (NULL == optval) {
  1374. sock_set_errno(sock, EFAULT);
  1375. return -1;
  1376. }
  1377. /* Do length and type checks for the various options first, to keep it readable. */
  1378. switch (level) {
  1379. /* Level: SOL_SOCKET */
  1380. case SOL_SOCKET:
  1381. switch (optname) {
  1382. case SO_BROADCAST:
  1383. /* UNIMPL case SO_DEBUG: */
  1384. /* UNIMPL case SO_DONTROUTE: */
  1385. case SO_KEEPALIVE:
  1386. /* UNIMPL case case SO_CONTIMEO: */
  1387. /* UNIMPL case case SO_SNDTIMEO: */
  1388. #if LWIP_SO_RCVTIMEO
  1389. case SO_RCVTIMEO:
  1390. #endif /* LWIP_SO_RCVTIMEO */
  1391. #if LWIP_SO_RCVBUF
  1392. case SO_RCVBUF:
  1393. #endif /* LWIP_SO_RCVBUF */
  1394. /* UNIMPL case SO_OOBINLINE: */
  1395. /* UNIMPL case SO_SNDBUF: */
  1396. /* UNIMPL case SO_RCVLOWAT: */
  1397. /* UNIMPL case SO_SNDLOWAT: */
  1398. #if SO_REUSE
  1399. case SO_REUSEADDR:
  1400. case SO_REUSEPORT:
  1401. #endif /* SO_REUSE */
  1402. /* UNIMPL case SO_USELOOPBACK: */
  1403. if (optlen < sizeof(int)) {
  1404. err = EINVAL;
  1405. }
  1406. break;
  1407. case SO_NO_CHECK:
  1408. if (optlen < sizeof(int)) {
  1409. err = EINVAL;
  1410. }
  1411. #if LWIP_UDP
  1412. if ((sock->conn->type != NETCONN_UDP) ||
  1413. ((udp_flags(sock->conn->pcb.udp) & UDP_FLAGS_UDPLITE) != 0)) {
  1414. /* this flag is only available for UDP, not for UDP lite */
  1415. err = EAFNOSUPPORT;
  1416. }
  1417. #endif /* LWIP_UDP */
  1418. break;
  1419. default:
  1420. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, SOL_SOCKET, UNIMPL: optname=0x%x, ..)\n",
  1421. s, optname));
  1422. err = ENOPROTOOPT;
  1423. } /* switch (optname) */
  1424. break;
  1425. /* Level: IPPROTO_IP */
  1426. case IPPROTO_IP:
  1427. switch (optname) {
  1428. /* UNIMPL case IP_HDRINCL: */
  1429. /* UNIMPL case IP_RCVDSTADDR: */
  1430. /* UNIMPL case IP_RCVIF: */
  1431. case IP_TTL:
  1432. case IP_TOS:
  1433. if (optlen < sizeof(int)) {
  1434. err = EINVAL;
  1435. }
  1436. break;
  1437. #if LWIP_IGMP
  1438. case IP_MULTICAST_TTL:
  1439. if (optlen < sizeof(u8_t)) {
  1440. err = EINVAL;
  1441. }
  1442. if (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_UDP) {
  1443. err = EAFNOSUPPORT;
  1444. }
  1445. break;
  1446. case IP_MULTICAST_IF:
  1447. if (optlen < sizeof(struct in_addr)) {
  1448. err = EINVAL;
  1449. }
  1450. if (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_UDP) {
  1451. err = EAFNOSUPPORT;
  1452. }
  1453. break;
  1454. case IP_ADD_MEMBERSHIP:
  1455. case IP_DROP_MEMBERSHIP:
  1456. if (optlen < sizeof(struct ip_mreq)) {
  1457. err = EINVAL;
  1458. }
  1459. if (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_UDP) {
  1460. err = EAFNOSUPPORT;
  1461. }
  1462. break;
  1463. #endif /* LWIP_IGMP */
  1464. default:
  1465. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, UNIMPL: optname=0x%x, ..)\n",
  1466. s, optname));
  1467. err = ENOPROTOOPT;
  1468. } /* switch (optname) */
  1469. break;
  1470. #if LWIP_TCP
  1471. /* Level: IPPROTO_TCP */
  1472. case IPPROTO_TCP:
  1473. if (optlen < sizeof(int)) {
  1474. err = EINVAL;
  1475. break;
  1476. }
  1477. /* If this is no TCP socket, ignore any options. */
  1478. if (sock->conn->type != NETCONN_TCP)
  1479. return 0;
  1480. switch (optname) {
  1481. case TCP_NODELAY:
  1482. case TCP_KEEPALIVE:
  1483. #if LWIP_TCP_KEEPALIVE
  1484. case TCP_KEEPIDLE:
  1485. case TCP_KEEPINTVL:
  1486. case TCP_KEEPCNT:
  1487. #endif /* LWIP_TCP_KEEPALIVE */
  1488. break;
  1489. default:
  1490. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, UNIMPL: optname=0x%x, ..)\n",
  1491. s, optname));
  1492. err = ENOPROTOOPT;
  1493. } /* switch (optname) */
  1494. break;
  1495. #endif /* LWIP_TCP */
  1496. #if LWIP_UDP && LWIP_UDPLITE
  1497. /* Level: IPPROTO_UDPLITE */
  1498. case IPPROTO_UDPLITE:
  1499. if (optlen < sizeof(int)) {
  1500. err = EINVAL;
  1501. break;
  1502. }
  1503. /* If this is no UDP lite socket, ignore any options. */
  1504. if (sock->conn->type != NETCONN_UDPLITE)
  1505. return 0;
  1506. switch (optname) {
  1507. case UDPLITE_SEND_CSCOV:
  1508. case UDPLITE_RECV_CSCOV:
  1509. break;
  1510. default:
  1511. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_UDPLITE, UNIMPL: optname=0x%x, ..)\n",
  1512. s, optname));
  1513. err = ENOPROTOOPT;
  1514. } /* switch (optname) */
  1515. break;
  1516. #endif /* LWIP_UDP && LWIP_UDPLITE */
  1517. /* UNDEFINED LEVEL */
  1518. default:
  1519. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, level=0x%x, UNIMPL: optname=0x%x, ..)\n",
  1520. s, level, optname));
  1521. err = ENOPROTOOPT;
  1522. } /* switch (level) */
  1523. if (err != ERR_OK) {
  1524. sock_set_errno(sock, err);
  1525. return -1;
  1526. }
  1527. /* Now do the actual option processing */
  1528. data.sock = sock;
  1529. data.level = level;
  1530. data.optname = optname;
  1531. data.optval = (void*)optval;
  1532. data.optlen = &optlen;
  1533. data.err = err;
  1534. tcpip_callback(lwip_setsockopt_internal, &data);
  1535. sys_arch_sem_wait(sock->conn->op_completed, 0);
  1536. /* maybe lwip_setsockopt_internal has changed err */
  1537. err = data.err;
  1538. sock_set_errno(sock, err);
  1539. return err ? -1 : 0;
  1540. }
  1541. static void
  1542. lwip_setsockopt_internal(void *arg)
  1543. {
  1544. struct lwip_socket *sock;
  1545. #ifdef LWIP_DEBUG
  1546. int s;
  1547. #endif /* LWIP_DEBUG */
  1548. int level, optname;
  1549. const void *optval;
  1550. struct lwip_setgetsockopt_data *data;
  1551. LWIP_ASSERT("arg != NULL", arg != NULL);
  1552. data = (struct lwip_setgetsockopt_data*)arg;
  1553. sock = data->sock;
  1554. #ifdef LWIP_DEBUG
  1555. s = data->s;
  1556. #endif /* LWIP_DEBUG */
  1557. level = data->level;
  1558. optname = data->optname;
  1559. optval = data->optval;
  1560. switch (level) {
  1561. /* Level: SOL_SOCKET */
  1562. case SOL_SOCKET:
  1563. switch (optname) {
  1564. /* The option flags */
  1565. case SO_BROADCAST:
  1566. /* UNIMPL case SO_DEBUG: */
  1567. /* UNIMPL case SO_DONTROUTE: */
  1568. case SO_KEEPALIVE:
  1569. /* UNIMPL case SO_OOBINCLUDE: */
  1570. #if SO_REUSE
  1571. case SO_REUSEADDR:
  1572. case SO_REUSEPORT:
  1573. #endif /* SO_REUSE */
  1574. /* UNIMPL case SO_USELOOPBACK: */
  1575. if (*(int*)optval) {
  1576. sock->conn->pcb.ip->so_options |= optname;
  1577. } else {
  1578. sock->conn->pcb.ip->so_options &= ~optname;
  1579. }
  1580. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, SOL_SOCKET, optname=0x%x, ..) -> %s\n",
  1581. s, optname, (*(int*)optval?"on":"off")));
  1582. break;
  1583. #if LWIP_SO_RCVTIMEO
  1584. case SO_RCVTIMEO:
  1585. sock->conn->recv_timeout = ( *(int*)optval );
  1586. break;
  1587. #endif /* LWIP_SO_RCVTIMEO */
  1588. #if LWIP_SO_RCVBUF
  1589. case SO_RCVBUF:
  1590. sock->conn->recv_bufsize = ( *(int*)optval );
  1591. break;
  1592. #endif /* LWIP_SO_RCVBUF */
  1593. #if LWIP_UDP
  1594. case SO_NO_CHECK:
  1595. if (*(int*)optval) {
  1596. udp_setflags(sock->conn->pcb.udp, udp_flags(sock->conn->pcb.udp) | UDP_FLAGS_NOCHKSUM);
  1597. } else {
  1598. udp_setflags(sock->conn->pcb.udp, udp_flags(sock->conn->pcb.udp) & ~UDP_FLAGS_NOCHKSUM);
  1599. }
  1600. break;
  1601. #endif /* LWIP_UDP */
  1602. } /* switch (optname) */
  1603. break;
  1604. /* Level: IPPROTO_IP */
  1605. case IPPROTO_IP:
  1606. switch (optname) {
  1607. case IP_TTL:
  1608. sock->conn->pcb.ip->ttl = (u8_t)(*(int*)optval);
  1609. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, IP_TTL, ..) -> %d\n",
  1610. s, sock->conn->pcb.ip->ttl));
  1611. break;
  1612. case IP_TOS:
  1613. sock->conn->pcb.ip->tos = (u8_t)(*(int*)optval);
  1614. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, IP_TOS, ..)-> %d\n",
  1615. s, sock->conn->pcb.ip->tos));
  1616. break;
  1617. #if LWIP_IGMP
  1618. case IP_MULTICAST_TTL:
  1619. sock->conn->pcb.udp->ttl = (u8_t)(*(u8_t*)optval);
  1620. break;
  1621. case IP_MULTICAST_IF:
  1622. sock->conn->pcb.udp->multicast_ip.addr = ((struct in_addr*) optval)->s_addr;
  1623. break;
  1624. case IP_ADD_MEMBERSHIP:
  1625. case IP_DROP_MEMBERSHIP:
  1626. {
  1627. /* If this is a TCP or a RAW socket, ignore these options. */
  1628. struct ip_mreq *imr = (struct ip_mreq *)optval;
  1629. if(optname == IP_ADD_MEMBERSHIP){
  1630. data->err = igmp_joingroup((struct ip_addr*)&(imr->imr_interface.s_addr), (struct ip_addr*)&(imr->imr_multiaddr.s_addr));
  1631. } else {
  1632. data->err = igmp_leavegroup((struct ip_addr*)&(imr->imr_interface.s_addr), (struct ip_addr*)&(imr->imr_multiaddr.s_addr));
  1633. }
  1634. if(data->err != ERR_OK) {
  1635. data->err = EADDRNOTAVAIL;
  1636. }
  1637. }
  1638. break;
  1639. #endif /* LWIP_IGMP */
  1640. } /* switch (optname) */
  1641. break;
  1642. #if LWIP_TCP
  1643. /* Level: IPPROTO_TCP */
  1644. case IPPROTO_TCP:
  1645. switch (optname) {
  1646. case TCP_NODELAY:
  1647. if (*(int*)optval) {
  1648. tcp_nagle_disable(sock->conn->pcb.tcp);
  1649. } else {
  1650. tcp_nagle_enable(sock->conn->pcb.tcp);
  1651. }
  1652. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_NODELAY) -> %s\n",
  1653. s, (*(int *)optval)?"on":"off") );
  1654. break;
  1655. case TCP_KEEPALIVE:
  1656. sock->conn->pcb.tcp->keep_idle = (u32_t)(*(int*)optval);
  1657. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPALIVE) -> %"U32_F"\n",
  1658. s, sock->conn->pcb.tcp->keep_idle));
  1659. break;
  1660. #if LWIP_TCP_KEEPALIVE
  1661. case TCP_KEEPIDLE:
  1662. sock->conn->pcb.tcp->keep_idle = 1000*(u32_t)(*(int*)optval);
  1663. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPIDLE) -> %"U32_F"\n",
  1664. s, sock->conn->pcb.tcp->keep_idle));
  1665. break;
  1666. case TCP_KEEPINTVL:
  1667. sock->conn->pcb.tcp->keep_intvl = 1000*(u32_t)(*(int*)optval);
  1668. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPINTVL) -> %"U32_F"\n",
  1669. s, sock->conn->pcb.tcp->keep_intvl));
  1670. break;
  1671. case TCP_KEEPCNT:
  1672. sock->conn->pcb.tcp->keep_cnt = (u32_t)(*(int*)optval);
  1673. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPCNT) -> %"U32_F"\n",
  1674. s, sock->conn->pcb.tcp->keep_cnt));
  1675. break;
  1676. #endif /* LWIP_TCP_KEEPALIVE */
  1677. } /* switch (optname) */
  1678. break;
  1679. #endif /* LWIP_TCP*/
  1680. #if LWIP_UDP && LWIP_UDPLITE
  1681. /* Level: IPPROTO_UDPLITE */
  1682. case IPPROTO_UDPLITE:
  1683. switch (optname) {
  1684. case UDPLITE_SEND_CSCOV:
  1685. if ((*(int*)optval != 0) && (*(int*)optval < 8)) {
  1686. /* don't allow illegal values! */
  1687. sock->conn->pcb.udp->chksum_len_tx = 8;
  1688. } else {
  1689. sock->conn->pcb.udp->chksum_len_tx = *(int*)optval;
  1690. }
  1691. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_UDPLITE, UDPLITE_SEND_CSCOV) -> %d\n",
  1692. s, (*(int*)optval)) );
  1693. break;
  1694. case UDPLITE_RECV_CSCOV:
  1695. if ((*(int*)optval != 0) && (*(int*)optval < 8)) {
  1696. /* don't allow illegal values! */
  1697. sock->conn->pcb.udp->chksum_len_rx = 8;
  1698. } else {
  1699. sock->conn->pcb.udp->chksum_len_rx = *(int*)optval;
  1700. }
  1701. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_UDPLITE, UDPLITE_RECV_CSCOV) -> %d\n",
  1702. s, (*(int*)optval)) );
  1703. break;
  1704. } /* switch (optname) */
  1705. break;
  1706. #endif /* LWIP_UDP */
  1707. } /* switch (level) */
  1708. sys_sem_signal(sock->conn->op_completed);
  1709. }
  1710. int
  1711. lwip_ioctl(int s, long cmd, void *argp)
  1712. {
  1713. struct lwip_socket *sock = get_socket(s);
  1714. u16_t buflen = 0;
  1715. s16_t recv_avail;
  1716. if (!sock)
  1717. return -1;
  1718. switch (cmd) {
  1719. case FIONREAD:
  1720. if (!argp) {
  1721. sock_set_errno(sock, EINVAL);
  1722. return -1;
  1723. }
  1724. SYS_ARCH_GET(sock->conn->recv_avail, recv_avail);
  1725. if (recv_avail < 0)
  1726. recv_avail = 0;
  1727. *((u16_t*)argp) = (u16_t)recv_avail;
  1728. /* Check if there is data left from the last recv operation. /maq 041215 */
  1729. if (sock->lastdata) {
  1730. buflen = netbuf_len(sock->lastdata);
  1731. buflen -= sock->lastoffset;
  1732. *((u16_t*)argp) += buflen;
  1733. }
  1734. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, FIONREAD, %p) = %"U16_F"\n", s, argp, *((u16_t*)argp)));
  1735. sock_set_errno(sock, 0);
  1736. return 0;
  1737. case FIONBIO:
  1738. if (argp && *(u32_t*)argp)
  1739. sock->flags |= O_NONBLOCK;
  1740. else
  1741. sock->flags &= ~O_NONBLOCK;
  1742. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, FIONBIO, %d)\n", s, !!(sock->flags & O_NONBLOCK)));
  1743. sock_set_errno(sock, 0);
  1744. return 0;
  1745. default:
  1746. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, UNIMPL: 0x%lx, %p)\n", s, cmd, argp));
  1747. sock_set_errno(sock, ENOSYS); /* not yet implemented */
  1748. return -1;
  1749. } /* switch (cmd) */
  1750. }
  1751. #endif /* LWIP_SOCKET */