sockets.c 56 KB

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