sockets.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347
  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 "lwip/pbuf.h"
  51. #if LWIP_CHECKSUM_ON_COPY
  52. #include "lwip/inet_chksum.h"
  53. #endif
  54. #include <string.h>
  55. #define NUM_SOCKETS MEMP_NUM_NETCONN
  56. /** Contains all internal pointers and states used for a socket */
  57. struct lwip_sock {
  58. /** sockets currently are built on netconns, each socket has one netconn */
  59. struct netconn *conn;
  60. /** data that was left from the previous read */
  61. void *lastdata;
  62. /** offset in the data that was left from the previous read */
  63. u16_t lastoffset;
  64. /** number of times data was received, set by event_callback(),
  65. tested by the receive and select functions */
  66. s16_t rcvevent;
  67. /** number of times data was ACKed (free send buffer), set by event_callback(),
  68. tested by select */
  69. u16_t sendevent;
  70. /** error happened for this socket, set by event_callback(), tested by select */
  71. u16_t errevent;
  72. /** last error that occurred on this socket */
  73. int err;
  74. /** counter of how many threads are waiting for this socket using select */
  75. int select_waiting;
  76. };
  77. /** Description for a task waiting in select */
  78. struct lwip_select_cb {
  79. /** Pointer to the next waiting task */
  80. struct lwip_select_cb *next;
  81. /** Pointer to the previous waiting task */
  82. struct lwip_select_cb *prev;
  83. /** readset passed to select */
  84. fd_set *readset;
  85. /** writeset passed to select */
  86. fd_set *writeset;
  87. /** unimplemented: exceptset passed to select */
  88. fd_set *exceptset;
  89. /** don't signal the same semaphore twice: set to 1 when signalled */
  90. int sem_signalled;
  91. /** semaphore to wake up a task waiting for select */
  92. sys_sem_t sem;
  93. };
  94. /** This struct is used to pass data to the set/getsockopt_internal
  95. * functions running in tcpip_thread context (only a void* is allowed) */
  96. struct lwip_setgetsockopt_data {
  97. /** socket struct for which to change options */
  98. struct lwip_sock *sock;
  99. #ifdef LWIP_DEBUG
  100. /** socket index for which to change options */
  101. int s;
  102. #endif /* LWIP_DEBUG */
  103. /** level of the option to process */
  104. int level;
  105. /** name of the option to process */
  106. int optname;
  107. /** set: value to set the option to
  108. * get: value of the option is stored here */
  109. void *optval;
  110. /** size of *optval */
  111. socklen_t *optlen;
  112. /** if an error occures, it is temporarily stored here */
  113. err_t err;
  114. };
  115. /** The global array of available sockets */
  116. static struct lwip_sock sockets[NUM_SOCKETS];
  117. /** The global list of tasks waiting for select */
  118. static struct lwip_select_cb *select_cb_list;
  119. /** This counter is increased from lwip_select when the list is chagned
  120. and checked in event_callback to see if it has changed. */
  121. static volatile int select_cb_ctr;
  122. /** Table to quickly map an lwIP error (err_t) to a socket error
  123. * by using -err as an index */
  124. static const int err_to_errno_table[] = {
  125. 0, /* ERR_OK 0 No error, everything OK. */
  126. ENOMEM, /* ERR_MEM -1 Out of memory error. */
  127. ENOBUFS, /* ERR_BUF -2 Buffer error. */
  128. EWOULDBLOCK, /* ERR_TIMEOUT -3 Timeout */
  129. EHOSTUNREACH, /* ERR_RTE -4 Routing problem. */
  130. EINPROGRESS, /* ERR_INPROGRESS -5 Operation in progress */
  131. EINVAL, /* ERR_VAL -6 Illegal value. */
  132. EWOULDBLOCK, /* ERR_WOULDBLOCK -7 Operation would block. */
  133. EADDRINUSE, /* ERR_USE -8 Address in use. */
  134. EALREADY, /* ERR_ISCONN -9 Already connected. */
  135. ECONNABORTED, /* ERR_ABRT -10 Connection aborted. */
  136. ECONNRESET, /* ERR_RST -11 Connection reset. */
  137. ENOTCONN, /* ERR_CLSD -12 Connection closed. */
  138. ENOTCONN, /* ERR_CONN -13 Not connected. */
  139. EIO, /* ERR_ARG -14 Illegal argument. */
  140. -1, /* ERR_IF -15 Low-level netif error */
  141. };
  142. #define ERR_TO_ERRNO_TABLE_SIZE \
  143. (sizeof(err_to_errno_table)/sizeof(err_to_errno_table[0]))
  144. #define err_to_errno(err) \
  145. ((unsigned)(-(err)) < ERR_TO_ERRNO_TABLE_SIZE ? \
  146. err_to_errno_table[-(err)] : EIO)
  147. #ifdef ERRNO
  148. #ifndef set_errno
  149. #define set_errno(err) errno = (err)
  150. #endif
  151. #else /* ERRNO */
  152. #define set_errno(err)
  153. #endif /* ERRNO */
  154. #define sock_set_errno(sk, e) do { \
  155. sk->err = (e); \
  156. set_errno(sk->err); \
  157. } while (0)
  158. /* Forward delcaration of some functions */
  159. static void event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len);
  160. static void lwip_getsockopt_internal(void *arg);
  161. static void lwip_setsockopt_internal(void *arg);
  162. /**
  163. * Initialize this module. This function has to be called before any other
  164. * functions in this module!
  165. */
  166. void
  167. lwip_socket_init(void)
  168. {
  169. }
  170. /**
  171. * Map a externally used socket index to the internal socket representation.
  172. *
  173. * @param s externally used socket index
  174. * @return struct lwip_sock for the socket or NULL if not found
  175. */
  176. static struct lwip_sock *
  177. get_socket(int s)
  178. {
  179. struct lwip_sock *sock;
  180. if ((s < 0) || (s >= NUM_SOCKETS)) {
  181. LWIP_DEBUGF(SOCKETS_DEBUG, ("get_socket(%d): invalid\n", s));
  182. set_errno(EBADF);
  183. return NULL;
  184. }
  185. sock = &sockets[s];
  186. if (!sock->conn) {
  187. LWIP_DEBUGF(SOCKETS_DEBUG, ("get_socket(%d): not active\n", s));
  188. set_errno(EBADF);
  189. return NULL;
  190. }
  191. return sock;
  192. }
  193. /**
  194. * Same as get_socket but doesn't set errno
  195. *
  196. * @param s externally used socket index
  197. * @return struct lwip_sock for the socket or NULL if not found
  198. */
  199. static struct lwip_sock *
  200. tryget_socket(int s)
  201. {
  202. if ((s < 0) || (s >= NUM_SOCKETS)) {
  203. return NULL;
  204. }
  205. if (!sockets[s].conn) {
  206. return NULL;
  207. }
  208. return &sockets[s];
  209. }
  210. /**
  211. * Allocate a new socket for a given netconn.
  212. *
  213. * @param newconn the netconn for which to allocate a socket
  214. * @param accepted 1 if socket has been created by accept(),
  215. * 0 if socket has been created by socket()
  216. * @return the index of the new socket; -1 on error
  217. */
  218. static int
  219. alloc_socket(struct netconn *newconn, int accepted)
  220. {
  221. int i;
  222. SYS_ARCH_DECL_PROTECT(lev);
  223. /* allocate a new socket identifier */
  224. for (i = 0; i < NUM_SOCKETS; ++i) {
  225. /* Protect socket array */
  226. SYS_ARCH_PROTECT(lev);
  227. if (!sockets[i].conn) {
  228. sockets[i].conn = newconn;
  229. /* The socket is not yet known to anyone, so no need to protect
  230. after having marked it as used. */
  231. SYS_ARCH_UNPROTECT(lev);
  232. sockets[i].lastdata = NULL;
  233. sockets[i].lastoffset = 0;
  234. sockets[i].rcvevent = 0;
  235. /* TCP sendbuf is empty, but the socket is not yet writable until connected
  236. * (unless it has been created by accept()). */
  237. sockets[i].sendevent = (newconn->type == NETCONN_TCP ? (accepted != 0) : 1);
  238. sockets[i].errevent = 0;
  239. sockets[i].err = 0;
  240. sockets[i].select_waiting = 0;
  241. return i;
  242. }
  243. SYS_ARCH_UNPROTECT(lev);
  244. }
  245. return -1;
  246. }
  247. /** Free a socket. The socket's netconn must have been
  248. * delete before!
  249. *
  250. * @param sock the socket to free
  251. * @param is_tcp != 0 for TCP sockets, used to free lastdata
  252. */
  253. static void
  254. free_socket(struct lwip_sock *sock, int is_tcp)
  255. {
  256. void *lastdata;
  257. SYS_ARCH_DECL_PROTECT(lev);
  258. lastdata = sock->lastdata;
  259. sock->lastdata = NULL;
  260. sock->lastoffset = 0;
  261. sock->err = 0;
  262. /* Protect socket array */
  263. SYS_ARCH_PROTECT(lev);
  264. sock->conn = NULL;
  265. SYS_ARCH_UNPROTECT(lev);
  266. /* don't use 'sock' after this line, as another task might have allocated it */
  267. if (lastdata != NULL) {
  268. if (is_tcp) {
  269. pbuf_free((struct pbuf *)lastdata);
  270. } else {
  271. netbuf_delete((struct netbuf *)lastdata);
  272. }
  273. }
  274. }
  275. /* Below this, the well-known socket functions are implemented.
  276. * Use google.com or opengroup.org to get a good description :-)
  277. *
  278. * Exceptions are documented!
  279. */
  280. int
  281. lwip_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
  282. {
  283. struct lwip_sock *sock, *nsock;
  284. struct netconn *newconn;
  285. ip_addr_t naddr;
  286. u16_t port;
  287. int newsock;
  288. struct sockaddr_in sin;
  289. err_t err;
  290. SYS_ARCH_DECL_PROTECT(lev);
  291. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d)...\n", s));
  292. sock = get_socket(s);
  293. if (!sock) {
  294. return -1;
  295. }
  296. if (netconn_is_nonblocking(sock->conn) && (sock->rcvevent <= 0)) {
  297. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d): returning EWOULDBLOCK\n", s));
  298. sock_set_errno(sock, EWOULDBLOCK);
  299. return -1;
  300. }
  301. /* wait for a new connection */
  302. err = netconn_accept(sock->conn, &newconn);
  303. if (err != ERR_OK) {
  304. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d): netconn_acept failed, err=%d\n", s, err));
  305. sock_set_errno(sock, err_to_errno(err));
  306. return -1;
  307. }
  308. LWIP_ASSERT("newconn != NULL", newconn != NULL);
  309. /* Prevent automatic window updates, we do this on our own! */
  310. netconn_set_noautorecved(newconn, 1);
  311. /* get the IP address and port of the remote host */
  312. err = netconn_peer(newconn, &naddr, &port);
  313. if (err != ERR_OK) {
  314. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d): netconn_peer failed, err=%d\n", s, err));
  315. netconn_delete(newconn);
  316. sock_set_errno(sock, err_to_errno(err));
  317. return -1;
  318. }
  319. /* Note that POSIX only requires us to check addr is non-NULL. addrlen must
  320. * not be NULL if addr is valid.
  321. */
  322. if (NULL != addr) {
  323. LWIP_ASSERT("addr valid but addrlen NULL", addrlen != NULL);
  324. memset(&sin, 0, sizeof(sin));
  325. sin.sin_len = sizeof(sin);
  326. sin.sin_family = AF_INET;
  327. sin.sin_port = htons(port);
  328. inet_addr_from_ipaddr(&sin.sin_addr, &naddr);
  329. if (*addrlen > sizeof(sin))
  330. *addrlen = sizeof(sin);
  331. MEMCPY(addr, &sin, *addrlen);
  332. }
  333. newsock = alloc_socket(newconn, 1);
  334. if (newsock == -1) {
  335. netconn_delete(newconn);
  336. sock_set_errno(sock, ENFILE);
  337. return -1;
  338. }
  339. LWIP_ASSERT("invalid socket index", (newsock >= 0) && (newsock < NUM_SOCKETS));
  340. LWIP_ASSERT("newconn->callback == event_callback", newconn->callback == event_callback);
  341. nsock = &sockets[newsock];
  342. /* See event_callback: If data comes in right away after an accept, even
  343. * though the server task might not have created a new socket yet.
  344. * In that case, newconn->socket is counted down (newconn->socket--),
  345. * so nsock->rcvevent is >= 1 here!
  346. */
  347. SYS_ARCH_PROTECT(lev);
  348. nsock->rcvevent += (s16_t)(-1 - newconn->socket);
  349. newconn->socket = newsock;
  350. SYS_ARCH_UNPROTECT(lev);
  351. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_accept(%d) returning new sock=%d addr=", s, newsock));
  352. ip_addr_debug_print(SOCKETS_DEBUG, &naddr);
  353. LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F"\n", port));
  354. sock_set_errno(sock, 0);
  355. return newsock;
  356. }
  357. int
  358. lwip_bind(int s, const struct sockaddr *name, socklen_t namelen)
  359. {
  360. struct lwip_sock *sock;
  361. ip_addr_t local_addr;
  362. u16_t local_port;
  363. err_t err;
  364. const struct sockaddr_in *name_in;
  365. sock = get_socket(s);
  366. if (!sock) {
  367. return -1;
  368. }
  369. /* check size, familiy and alignment of 'name' */
  370. LWIP_ERROR("lwip_bind: invalid address", ((namelen == sizeof(struct sockaddr_in)) &&
  371. ((name->sa_family) == AF_INET) && ((((mem_ptr_t)name) % 4) == 0)),
  372. sock_set_errno(sock, err_to_errno(ERR_ARG)); return -1;);
  373. name_in = (const struct sockaddr_in *)(void*)name;
  374. inet_addr_to_ipaddr(&local_addr, &name_in->sin_addr);
  375. local_port = name_in->sin_port;
  376. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d, addr=", s));
  377. ip_addr_debug_print(SOCKETS_DEBUG, &local_addr);
  378. LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F")\n", ntohs(local_port)));
  379. err = netconn_bind(sock->conn, &local_addr, ntohs(local_port));
  380. if (err != ERR_OK) {
  381. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d) failed, err=%d\n", s, err));
  382. sock_set_errno(sock, err_to_errno(err));
  383. return -1;
  384. }
  385. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_bind(%d) succeeded\n", s));
  386. sock_set_errno(sock, 0);
  387. return 0;
  388. }
  389. int
  390. lwip_close(int s)
  391. {
  392. struct lwip_sock *sock;
  393. int is_tcp = 0;
  394. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_close(%d)\n", s));
  395. sock = get_socket(s);
  396. if (!sock) {
  397. return -1;
  398. }
  399. if(sock->conn != NULL) {
  400. is_tcp = netconn_type(sock->conn) == NETCONN_TCP;
  401. } else {
  402. LWIP_ASSERT("sock->lastdata == NULL", sock->lastdata == NULL);
  403. }
  404. netconn_delete(sock->conn);
  405. free_socket(sock, is_tcp);
  406. set_errno(0);
  407. return 0;
  408. }
  409. int
  410. lwip_connect(int s, const struct sockaddr *name, socklen_t namelen)
  411. {
  412. struct lwip_sock *sock;
  413. err_t err;
  414. const struct sockaddr_in *name_in;
  415. sock = get_socket(s);
  416. if (!sock) {
  417. return -1;
  418. }
  419. /* check size, familiy and alignment of 'name' */
  420. LWIP_ERROR("lwip_connect: invalid address", ((namelen == sizeof(struct sockaddr_in)) &&
  421. ((name->sa_family) == AF_INET) && ((((mem_ptr_t)name) % 4) == 0)),
  422. sock_set_errno(sock, err_to_errno(ERR_ARG)); return -1;);
  423. name_in = (const struct sockaddr_in *)(void*)name;
  424. if (name_in->sin_family == AF_UNSPEC) {
  425. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, AF_UNSPEC)\n", s));
  426. err = netconn_disconnect(sock->conn);
  427. } else {
  428. ip_addr_t remote_addr;
  429. u16_t remote_port;
  430. inet_addr_to_ipaddr(&remote_addr, &name_in->sin_addr);
  431. remote_port = name_in->sin_port;
  432. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d, addr=", s));
  433. ip_addr_debug_print(SOCKETS_DEBUG, &remote_addr);
  434. LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F")\n", ntohs(remote_port)));
  435. err = netconn_connect(sock->conn, &remote_addr, ntohs(remote_port));
  436. }
  437. if (err != ERR_OK) {
  438. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d) failed, err=%d\n", s, err));
  439. sock_set_errno(sock, err_to_errno(err));
  440. return -1;
  441. }
  442. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_connect(%d) succeeded\n", s));
  443. sock_set_errno(sock, 0);
  444. return 0;
  445. }
  446. /**
  447. * Set a socket into listen mode.
  448. * The socket may not have been used for another connection previously.
  449. *
  450. * @param s the socket to set to listening mode
  451. * @param backlog (ATTENTION: needs TCP_LISTEN_BACKLOG=1)
  452. * @return 0 on success, non-zero on failure
  453. */
  454. int
  455. lwip_listen(int s, int backlog)
  456. {
  457. struct lwip_sock *sock;
  458. err_t err;
  459. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_listen(%d, backlog=%d)\n", s, backlog));
  460. sock = get_socket(s);
  461. if (!sock) {
  462. return -1;
  463. }
  464. /* limit the "backlog" parameter to fit in an u8_t */
  465. backlog = LWIP_MIN(LWIP_MAX(backlog, 0), 0xff);
  466. err = netconn_listen_with_backlog(sock->conn, (u8_t)backlog);
  467. if (err != ERR_OK) {
  468. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_listen(%d) failed, err=%d\n", s, err));
  469. sock_set_errno(sock, err_to_errno(err));
  470. return -1;
  471. }
  472. sock_set_errno(sock, 0);
  473. return 0;
  474. }
  475. int
  476. lwip_recvfrom(int s, void *mem, size_t len, int flags,
  477. struct sockaddr *from, socklen_t *fromlen)
  478. {
  479. struct lwip_sock *sock;
  480. void *buf = NULL;
  481. struct pbuf *p;
  482. u16_t buflen, copylen;
  483. int off = 0;
  484. ip_addr_t *addr;
  485. u16_t port;
  486. u8_t done = 0;
  487. err_t err;
  488. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d, %p, %"SZT_F", 0x%x, ..)\n", s, mem, len, flags));
  489. sock = get_socket(s);
  490. if (!sock) {
  491. return -1;
  492. }
  493. do {
  494. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: top while sock->lastdata=%p\n", sock->lastdata));
  495. /* Check if there is data left from the last recv operation. */
  496. if (sock->lastdata) {
  497. buf = sock->lastdata;
  498. } else {
  499. /* If this is non-blocking call, then check first */
  500. if (((flags & MSG_DONTWAIT) || netconn_is_nonblocking(sock->conn)) &&
  501. (sock->rcvevent <= 0)) {
  502. if (off > 0) {
  503. /* update receive window */
  504. netconn_recved(sock->conn, (u32_t)off);
  505. /* already received data, return that */
  506. sock_set_errno(sock, 0);
  507. return off;
  508. }
  509. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): returning EWOULDBLOCK\n", s));
  510. sock_set_errno(sock, EWOULDBLOCK);
  511. return -1;
  512. }
  513. /* No data was left from the previous operation, so we try to get
  514. some from the network. */
  515. if (netconn_type(sock->conn) == NETCONN_TCP) {
  516. err = netconn_recv_tcp_pbuf(sock->conn, (struct pbuf **)&buf);
  517. } else {
  518. err = netconn_recv(sock->conn, (struct netbuf **)&buf);
  519. }
  520. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: netconn_recv err=%d, netbuf=%p\n",
  521. err, buf));
  522. if (err != ERR_OK) {
  523. if (off > 0) {
  524. /* update receive window */
  525. netconn_recved(sock->conn, (u32_t)off);
  526. /* already received data, return that */
  527. sock_set_errno(sock, 0);
  528. return off;
  529. }
  530. /* We should really do some error checking here. */
  531. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): buf == NULL, error is \"%s\"!\n",
  532. s, lwip_strerr(err)));
  533. sock_set_errno(sock, err_to_errno(err));
  534. if (err == ERR_CLSD) {
  535. return 0;
  536. } else {
  537. return -1;
  538. }
  539. }
  540. LWIP_ASSERT("buf != NULL", buf != NULL);
  541. sock->lastdata = buf;
  542. }
  543. if (netconn_type(sock->conn) == NETCONN_TCP) {
  544. p = (struct pbuf *)buf;
  545. } else {
  546. p = ((struct netbuf *)buf)->p;
  547. }
  548. buflen = p->tot_len;
  549. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: buflen=%"U16_F" len=%"SZT_F" off=%d sock->lastoffset=%"U16_F"\n",
  550. buflen, len, off, sock->lastoffset));
  551. buflen -= sock->lastoffset;
  552. if (len > buflen) {
  553. copylen = buflen;
  554. } else {
  555. copylen = (u16_t)len;
  556. }
  557. /* copy the contents of the received buffer into
  558. the supplied memory pointer mem */
  559. pbuf_copy_partial(p, (u8_t*)mem + off, copylen, sock->lastoffset);
  560. off += copylen;
  561. if (netconn_type(sock->conn) == NETCONN_TCP) {
  562. LWIP_ASSERT("invalid copylen, len would underflow", len >= copylen);
  563. len -= copylen;
  564. if ( (len <= 0) ||
  565. (p->flags & PBUF_FLAG_PUSH) ||
  566. (sock->rcvevent <= 0) ||
  567. ((flags & MSG_PEEK)!=0)) {
  568. done = 1;
  569. }
  570. } else {
  571. done = 1;
  572. }
  573. /* Check to see from where the data was.*/
  574. if (done) {
  575. ip_addr_t fromaddr;
  576. if (from && fromlen) {
  577. struct sockaddr_in sin;
  578. if (netconn_type(sock->conn) == NETCONN_TCP) {
  579. addr = &fromaddr;
  580. netconn_getaddr(sock->conn, addr, &port, 0);
  581. } else {
  582. addr = netbuf_fromaddr((struct netbuf *)buf);
  583. port = netbuf_fromport((struct netbuf *)buf);
  584. }
  585. memset(&sin, 0, sizeof(sin));
  586. sin.sin_len = sizeof(sin);
  587. sin.sin_family = AF_INET;
  588. sin.sin_port = htons(port);
  589. inet_addr_from_ipaddr(&sin.sin_addr, addr);
  590. if (*fromlen > sizeof(sin)) {
  591. *fromlen = sizeof(sin);
  592. }
  593. MEMCPY(from, &sin, *fromlen);
  594. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): addr=", s));
  595. ip_addr_debug_print(SOCKETS_DEBUG, addr);
  596. LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F" len=%d\n", port, off));
  597. } else {
  598. #if SOCKETS_DEBUG
  599. if (netconn_type(sock->conn) == NETCONN_TCP) {
  600. addr = &fromaddr;
  601. netconn_getaddr(sock->conn, addr, &port, 0);
  602. } else {
  603. addr = netbuf_fromaddr((struct netbuf *)buf);
  604. port = netbuf_fromport((struct netbuf *)buf);
  605. }
  606. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom(%d): addr=", s));
  607. ip_addr_debug_print(SOCKETS_DEBUG, addr);
  608. LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F" len=%d\n", port, off));
  609. #endif /* SOCKETS_DEBUG */
  610. }
  611. }
  612. /* If we don't peek the incoming message... */
  613. if ((flags & MSG_PEEK) == 0) {
  614. /* If this is a TCP socket, check if there is data left in the
  615. buffer. If so, it should be saved in the sock structure for next
  616. time around. */
  617. if ((netconn_type(sock->conn) == NETCONN_TCP) && (buflen - copylen > 0)) {
  618. sock->lastdata = buf;
  619. sock->lastoffset += copylen;
  620. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: lastdata now netbuf=%p\n", buf));
  621. } else {
  622. sock->lastdata = NULL;
  623. sock->lastoffset = 0;
  624. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_recvfrom: deleting netbuf=%p\n", buf));
  625. if (netconn_type(sock->conn) == NETCONN_TCP) {
  626. pbuf_free((struct pbuf *)buf);
  627. } else {
  628. netbuf_delete((struct netbuf *)buf);
  629. }
  630. }
  631. }
  632. } while (!done);
  633. if (off > 0) {
  634. /* update receive window */
  635. netconn_recved(sock->conn, (u32_t)off);
  636. }
  637. sock_set_errno(sock, 0);
  638. return off;
  639. }
  640. int
  641. lwip_read(int s, void *mem, size_t len)
  642. {
  643. return lwip_recvfrom(s, mem, len, 0, NULL, NULL);
  644. }
  645. int
  646. lwip_recv(int s, void *mem, size_t len, int flags)
  647. {
  648. return lwip_recvfrom(s, mem, len, flags, NULL, NULL);
  649. }
  650. int
  651. lwip_send(int s, const void *data, size_t size, int flags)
  652. {
  653. struct lwip_sock *sock;
  654. err_t err;
  655. u8_t write_flags;
  656. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d, data=%p, size=%"SZT_F", flags=0x%x)\n",
  657. s, data, size, flags));
  658. sock = get_socket(s);
  659. if (!sock) {
  660. return -1;
  661. }
  662. if (sock->conn->type != NETCONN_TCP) {
  663. #if (LWIP_UDP || LWIP_RAW)
  664. return lwip_sendto(s, data, size, flags, NULL, 0);
  665. #else /* (LWIP_UDP || LWIP_RAW) */
  666. sock_set_errno(sock, err_to_errno(ERR_ARG));
  667. return -1;
  668. #endif /* (LWIP_UDP || LWIP_RAW) */
  669. }
  670. if ((flags & MSG_DONTWAIT) || netconn_is_nonblocking(sock->conn)) {
  671. if ((size > TCP_SND_BUF) || ((size / TCP_MSS) > TCP_SND_QUEUELEN)) {
  672. /* too much data to ever send nonblocking! */
  673. sock_set_errno(sock, EMSGSIZE);
  674. return -1;
  675. }
  676. }
  677. write_flags = NETCONN_COPY |
  678. ((flags & MSG_MORE) ? NETCONN_MORE : 0) |
  679. ((flags & MSG_DONTWAIT) ? NETCONN_DONTBLOCK : 0);
  680. err = netconn_write(sock->conn, data, size, write_flags);
  681. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_send(%d) err=%d size=%"SZT_F"\n", s, err, size));
  682. sock_set_errno(sock, err_to_errno(err));
  683. return (err == ERR_OK ? (int)size : -1);
  684. }
  685. int
  686. lwip_sendto(int s, const void *data, size_t size, int flags,
  687. const struct sockaddr *to, socklen_t tolen)
  688. {
  689. struct lwip_sock *sock;
  690. err_t err;
  691. u16_t short_size;
  692. const struct sockaddr_in *to_in;
  693. u16_t remote_port;
  694. #if !LWIP_TCPIP_CORE_LOCKING
  695. struct netbuf buf;
  696. #endif
  697. sock = get_socket(s);
  698. if (!sock) {
  699. return -1;
  700. }
  701. if (sock->conn->type == NETCONN_TCP) {
  702. #if LWIP_TCP
  703. return lwip_send(s, data, size, flags);
  704. #else /* LWIP_TCP */
  705. LWIP_UNUSED_ARG(flags);
  706. sock_set_errno(sock, err_to_errno(ERR_ARG));
  707. return -1;
  708. #endif /* LWIP_TCP */
  709. }
  710. /* @todo: split into multiple sendto's? */
  711. LWIP_ASSERT("lwip_sendto: size must fit in u16_t", size <= 0xffff);
  712. short_size = (u16_t)size;
  713. LWIP_ERROR("lwip_sendto: invalid address", (((to == NULL) && (tolen == 0)) ||
  714. ((tolen == sizeof(struct sockaddr_in)) &&
  715. ((to->sa_family) == AF_INET) && ((((mem_ptr_t)to) % 4) == 0))),
  716. sock_set_errno(sock, err_to_errno(ERR_ARG)); return -1;);
  717. to_in = (const struct sockaddr_in *)(void*)to;
  718. #if LWIP_TCPIP_CORE_LOCKING
  719. /* Should only be consider like a sample or a simple way to experiment this option (no check of "to" field...) */
  720. {
  721. struct pbuf* p;
  722. ip_addr_t *remote_addr;
  723. #if LWIP_NETIF_TX_SINGLE_PBUF
  724. p = pbuf_alloc(PBUF_TRANSPORT, short_size, PBUF_RAM);
  725. if (p != NULL) {
  726. #if LWIP_CHECKSUM_ON_COPY
  727. u16_t chksum = 0;
  728. if (sock->conn->type != NETCONN_RAW) {
  729. chksum = LWIP_CHKSUM_COPY(p->payload, data, short_size);
  730. } else
  731. #endif /* LWIP_CHECKSUM_ON_COPY */
  732. MEMCPY(p->payload, data, size);
  733. #else /* LWIP_NETIF_TX_SINGLE_PBUF */
  734. p = pbuf_alloc(PBUF_TRANSPORT, short_size, PBUF_REF);
  735. if (p != NULL) {
  736. p->payload = (void*)data;
  737. #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
  738. if (to_in != NULL) {
  739. inet_addr_to_ipaddr_p(remote_addr, &to_in->sin_addr);
  740. remote_port = ntohs(to_in->sin_port);
  741. } else {
  742. remote_addr = &sock->conn->pcb.raw->remote_ip;
  743. if (sock->conn->type == NETCONN_RAW) {
  744. remote_port = 0;
  745. } else {
  746. remote_port = sock->conn->pcb.udp->remote_port;
  747. }
  748. }
  749. LOCK_TCPIP_CORE();
  750. if (sock->conn->type == NETCONN_RAW) {
  751. err = sock->conn->last_err = raw_sendto(sock->conn->pcb.raw, p, remote_addr);
  752. } else {
  753. #if LWIP_UDP
  754. #if LWIP_CHECKSUM_ON_COPY && LWIP_NETIF_TX_SINGLE_PBUF
  755. err = sock->conn->last_err = udp_sendto_chksum(sock->conn->pcb.udp, p,
  756. remote_addr, remote_port, 1, chksum);
  757. #else /* LWIP_CHECKSUM_ON_COPY && LWIP_NETIF_TX_SINGLE_PBUF */
  758. err = sock->conn->last_err = udp_sendto(sock->conn->pcb.udp, p,
  759. remote_addr, remote_port);
  760. #endif /* LWIP_CHECKSUM_ON_COPY && LWIP_NETIF_TX_SINGLE_PBUF */
  761. #else /* LWIP_UDP */
  762. err = ERR_ARG;
  763. #endif /* LWIP_UDP */
  764. }
  765. UNLOCK_TCPIP_CORE();
  766. pbuf_free(p);
  767. } else {
  768. err = ERR_MEM;
  769. }
  770. }
  771. #else /* LWIP_TCPIP_CORE_LOCKING */
  772. /* initialize a buffer */
  773. buf.p = buf.ptr = NULL;
  774. #if LWIP_CHECKSUM_ON_COPY
  775. buf.flags = 0;
  776. #endif /* LWIP_CHECKSUM_ON_COPY */
  777. if (to) {
  778. inet_addr_to_ipaddr(&buf.addr, &to_in->sin_addr);
  779. remote_port = ntohs(to_in->sin_port);
  780. netbuf_fromport(&buf) = remote_port;
  781. } else {
  782. remote_port = 0;
  783. ip_addr_set_any(&buf.addr);
  784. netbuf_fromport(&buf) = 0;
  785. }
  786. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_sendto(%d, data=%p, short_size=%"U16_F", flags=0x%x to=",
  787. s, data, short_size, flags));
  788. ip_addr_debug_print(SOCKETS_DEBUG, &buf.addr);
  789. LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F"\n", remote_port));
  790. /* make the buffer point to the data that should be sent */
  791. #if LWIP_NETIF_TX_SINGLE_PBUF
  792. /* Allocate a new netbuf and copy the data into it. */
  793. if (netbuf_alloc(&buf, short_size) == NULL) {
  794. err = ERR_MEM;
  795. } else {
  796. #if LWIP_CHECKSUM_ON_COPY
  797. if (sock->conn->type != NETCONN_RAW) {
  798. u16_t chksum = LWIP_CHKSUM_COPY(buf.p->payload, data, short_size);
  799. netbuf_set_chksum(&buf, chksum);
  800. err = ERR_OK;
  801. } else
  802. #endif /* LWIP_CHECKSUM_ON_COPY */
  803. {
  804. err = netbuf_take(&buf, data, short_size);
  805. }
  806. }
  807. #else /* LWIP_NETIF_TX_SINGLE_PBUF */
  808. err = netbuf_ref(&buf, data, short_size);
  809. #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
  810. if (err == ERR_OK) {
  811. /* send the data */
  812. err = netconn_send(sock->conn, &buf);
  813. }
  814. /* deallocated the buffer */
  815. netbuf_free(&buf);
  816. #endif /* LWIP_TCPIP_CORE_LOCKING */
  817. sock_set_errno(sock, err_to_errno(err));
  818. return (err == ERR_OK ? short_size : -1);
  819. }
  820. int
  821. lwip_socket(int domain, int type, int protocol)
  822. {
  823. struct netconn *conn;
  824. int i;
  825. LWIP_UNUSED_ARG(domain);
  826. /* create a netconn */
  827. switch (type) {
  828. case SOCK_RAW:
  829. conn = netconn_new_with_proto_and_callback(NETCONN_RAW, (u8_t)protocol, event_callback);
  830. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_RAW, %d) = ",
  831. domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
  832. break;
  833. case SOCK_DGRAM:
  834. conn = netconn_new_with_callback( (protocol == IPPROTO_UDPLITE) ?
  835. NETCONN_UDPLITE : NETCONN_UDP, event_callback);
  836. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_DGRAM, %d) = ",
  837. domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
  838. break;
  839. case SOCK_STREAM:
  840. conn = netconn_new_with_callback(NETCONN_TCP, event_callback);
  841. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%s, SOCK_STREAM, %d) = ",
  842. domain == PF_INET ? "PF_INET" : "UNKNOWN", protocol));
  843. if (conn != NULL) {
  844. /* Prevent automatic window updates, we do this on our own! */
  845. netconn_set_noautorecved(conn, 1);
  846. }
  847. break;
  848. default:
  849. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_socket(%d, %d/UNKNOWN, %d) = -1\n",
  850. domain, type, protocol));
  851. set_errno(EINVAL);
  852. return -1;
  853. }
  854. if (!conn) {
  855. LWIP_DEBUGF(SOCKETS_DEBUG, ("-1 / ENOBUFS (could not create netconn)\n"));
  856. set_errno(ENOBUFS);
  857. return -1;
  858. }
  859. i = alloc_socket(conn, 0);
  860. if (i == -1) {
  861. netconn_delete(conn);
  862. set_errno(ENFILE);
  863. return -1;
  864. }
  865. conn->socket = i;
  866. LWIP_DEBUGF(SOCKETS_DEBUG, ("%d\n", i));
  867. set_errno(0);
  868. return i;
  869. }
  870. int
  871. lwip_write(int s, const void *data, size_t size)
  872. {
  873. return lwip_send(s, data, size, 0);
  874. }
  875. /**
  876. * Go through the readset and writeset lists and see which socket of the sockets
  877. * set in the sets has events. On return, readset, writeset and exceptset have
  878. * the sockets enabled that had events.
  879. *
  880. * exceptset is not used for now!!!
  881. *
  882. * @param maxfdp1 the highest socket index in the sets
  883. * @param readset_in: set of sockets to check for read events
  884. * @param writeset_in: set of sockets to check for write events
  885. * @param exceptset_in: set of sockets to check for error events
  886. * @param readset_out: set of sockets that had read events
  887. * @param writeset_out: set of sockets that had write events
  888. * @param exceptset_out: set os sockets that had error events
  889. * @return number of sockets that had events (read/write/exception) (>= 0)
  890. */
  891. static int
  892. lwip_selscan(int maxfdp1, fd_set *readset_in, fd_set *writeset_in, fd_set *exceptset_in,
  893. fd_set *readset_out, fd_set *writeset_out, fd_set *exceptset_out)
  894. {
  895. int i, nready = 0;
  896. fd_set lreadset, lwriteset, lexceptset;
  897. struct lwip_sock *sock;
  898. SYS_ARCH_DECL_PROTECT(lev);
  899. FD_ZERO(&lreadset);
  900. FD_ZERO(&lwriteset);
  901. FD_ZERO(&lexceptset);
  902. /* Go through each socket in each list to count number of sockets which
  903. currently match */
  904. for(i = 0; i < maxfdp1; i++) {
  905. void* lastdata = NULL;
  906. s16_t rcvevent = 0;
  907. u16_t sendevent = 0;
  908. u16_t errevent = 0;
  909. /* First get the socket's status (protected)... */
  910. SYS_ARCH_PROTECT(lev);
  911. sock = tryget_socket(i);
  912. if (sock != NULL) {
  913. lastdata = sock->lastdata;
  914. rcvevent = sock->rcvevent;
  915. sendevent = sock->sendevent;
  916. errevent = sock->errevent;
  917. }
  918. SYS_ARCH_UNPROTECT(lev);
  919. /* ... then examine it: */
  920. /* See if netconn of this socket is ready for read */
  921. if (readset_in && FD_ISSET(i, readset_in) && ((lastdata != NULL) || (rcvevent > 0))) {
  922. FD_SET(i, &lreadset);
  923. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_selscan: fd=%d ready for reading\n", i));
  924. nready++;
  925. }
  926. /* See if netconn of this socket is ready for write */
  927. if (writeset_in && FD_ISSET(i, writeset_in) && (sendevent != 0)) {
  928. FD_SET(i, &lwriteset);
  929. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_selscan: fd=%d ready for writing\n", i));
  930. nready++;
  931. }
  932. /* See if netconn of this socket had an error */
  933. if (exceptset_in && FD_ISSET(i, exceptset_in) && (errevent != 0)) {
  934. FD_SET(i, &lexceptset);
  935. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_selscan: fd=%d ready for exception\n", i));
  936. nready++;
  937. }
  938. }
  939. /* copy local sets to the ones provided as arguments */
  940. *readset_out = lreadset;
  941. *writeset_out = lwriteset;
  942. *exceptset_out = lexceptset;
  943. LWIP_ASSERT("nready >= 0", nready >= 0);
  944. return nready;
  945. }
  946. /**
  947. * Processing exceptset is not yet implemented.
  948. */
  949. int
  950. lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset,
  951. struct timeval *timeout)
  952. {
  953. u32_t waitres = 0;
  954. int nready;
  955. fd_set lreadset, lwriteset, lexceptset;
  956. u32_t msectimeout;
  957. struct lwip_select_cb select_cb;
  958. err_t err;
  959. int i;
  960. SYS_ARCH_DECL_PROTECT(lev);
  961. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select(%d, %p, %p, %p, tvsec=%"S32_F" tvusec=%"S32_F")\n",
  962. maxfdp1, (void *)readset, (void *) writeset, (void *) exceptset,
  963. timeout ? (s32_t)timeout->tv_sec : (s32_t)-1,
  964. timeout ? (s32_t)timeout->tv_usec : (s32_t)-1));
  965. /* Go through each socket in each list to count number of sockets which
  966. currently match */
  967. nready = lwip_selscan(maxfdp1, readset, writeset, exceptset, &lreadset, &lwriteset, &lexceptset);
  968. /* If we don't have any current events, then suspend if we are supposed to */
  969. if (!nready) {
  970. if (timeout && timeout->tv_sec == 0 && timeout->tv_usec == 0) {
  971. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: no timeout, returning 0\n"));
  972. /* This is OK as the local fdsets are empty and nready is zero,
  973. or we would have returned earlier. */
  974. goto return_copy_fdsets;
  975. }
  976. /* None ready: add our semaphore to list:
  977. We don't actually need any dynamic memory. Our entry on the
  978. list is only valid while we are in this function, so it's ok
  979. to use local variables. */
  980. select_cb.next = NULL;
  981. select_cb.prev = NULL;
  982. select_cb.readset = readset;
  983. select_cb.writeset = writeset;
  984. select_cb.exceptset = exceptset;
  985. select_cb.sem_signalled = 0;
  986. err = sys_sem_new(&select_cb.sem, 0);
  987. if (err != ERR_OK) {
  988. /* failed to create semaphore */
  989. set_errno(ENOMEM);
  990. return -1;
  991. }
  992. /* Protect the select_cb_list */
  993. SYS_ARCH_PROTECT(lev);
  994. /* Put this select_cb on top of list */
  995. select_cb.next = select_cb_list;
  996. if (select_cb_list != NULL) {
  997. select_cb_list->prev = &select_cb;
  998. }
  999. select_cb_list = &select_cb;
  1000. /* Increasing this counter tells even_callback that the list has changed. */
  1001. select_cb_ctr++;
  1002. /* Now we can safely unprotect */
  1003. SYS_ARCH_UNPROTECT(lev);
  1004. /* Increase select_waiting for each socket we are interested in */
  1005. for(i = 0; i < maxfdp1; i++) {
  1006. if ((readset && FD_ISSET(i, readset)) ||
  1007. (writeset && FD_ISSET(i, writeset)) ||
  1008. (exceptset && FD_ISSET(i, exceptset))) {
  1009. struct lwip_sock *sock = tryget_socket(i);
  1010. LWIP_ASSERT("sock != NULL", sock != NULL);
  1011. SYS_ARCH_PROTECT(lev);
  1012. sock->select_waiting++;
  1013. LWIP_ASSERT("sock->select_waiting > 0", sock->select_waiting > 0);
  1014. SYS_ARCH_UNPROTECT(lev);
  1015. }
  1016. }
  1017. /* Call lwip_selscan again: there could have been events between
  1018. the last scan (whithout us on the list) and putting us on the list! */
  1019. nready = lwip_selscan(maxfdp1, readset, writeset, exceptset, &lreadset, &lwriteset, &lexceptset);
  1020. if (!nready) {
  1021. /* Still none ready, just wait to be woken */
  1022. if (timeout == 0) {
  1023. /* Wait forever */
  1024. msectimeout = 0;
  1025. } else {
  1026. msectimeout = ((timeout->tv_sec * 1000) + ((timeout->tv_usec + 500)/1000));
  1027. if (msectimeout == 0) {
  1028. /* Wait 1ms at least (0 means wait forever) */
  1029. msectimeout = 1;
  1030. }
  1031. }
  1032. waitres = sys_arch_sem_wait(&select_cb.sem, msectimeout);
  1033. }
  1034. /* Increase select_waiting for each socket we are interested in */
  1035. for(i = 0; i < maxfdp1; i++) {
  1036. if ((readset && FD_ISSET(i, readset)) ||
  1037. (writeset && FD_ISSET(i, writeset)) ||
  1038. (exceptset && FD_ISSET(i, exceptset))) {
  1039. struct lwip_sock *sock = tryget_socket(i);
  1040. LWIP_ASSERT("sock != NULL", sock != NULL);
  1041. SYS_ARCH_PROTECT(lev);
  1042. sock->select_waiting--;
  1043. LWIP_ASSERT("sock->select_waiting >= 0", sock->select_waiting >= 0);
  1044. SYS_ARCH_UNPROTECT(lev);
  1045. }
  1046. }
  1047. /* Take us off the list */
  1048. SYS_ARCH_PROTECT(lev);
  1049. if (select_cb.next != NULL) {
  1050. select_cb.next->prev = select_cb.prev;
  1051. }
  1052. if (select_cb_list == &select_cb) {
  1053. LWIP_ASSERT("select_cb.prev == NULL", select_cb.prev == NULL);
  1054. select_cb_list = select_cb.next;
  1055. } else {
  1056. LWIP_ASSERT("select_cb.prev != NULL", select_cb.prev != NULL);
  1057. select_cb.prev->next = select_cb.next;
  1058. }
  1059. /* Increasing this counter tells even_callback that the list has changed. */
  1060. select_cb_ctr++;
  1061. SYS_ARCH_UNPROTECT(lev);
  1062. sys_sem_free(&select_cb.sem);
  1063. if (waitres == SYS_ARCH_TIMEOUT) {
  1064. /* Timeout */
  1065. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: timeout expired\n"));
  1066. /* This is OK as the local fdsets are empty and nready is zero,
  1067. or we would have returned earlier. */
  1068. goto return_copy_fdsets;
  1069. }
  1070. /* See what's set */
  1071. nready = lwip_selscan(maxfdp1, readset, writeset, exceptset, &lreadset, &lwriteset, &lexceptset);
  1072. }
  1073. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: nready=%d\n", nready));
  1074. return_copy_fdsets:
  1075. set_errno(0);
  1076. if (readset) {
  1077. *readset = lreadset;
  1078. }
  1079. if (writeset) {
  1080. *writeset = lwriteset;
  1081. }
  1082. if (exceptset) {
  1083. *exceptset = lexceptset;
  1084. }
  1085. return nready;
  1086. }
  1087. /**
  1088. * Callback registered in the netconn layer for each socket-netconn.
  1089. * Processes recvevent (data available) and wakes up tasks waiting for select.
  1090. */
  1091. static void
  1092. event_callback(struct netconn *conn, enum netconn_evt evt, u16_t len)
  1093. {
  1094. int s;
  1095. struct lwip_sock *sock;
  1096. struct lwip_select_cb *scb;
  1097. int last_select_cb_ctr;
  1098. SYS_ARCH_DECL_PROTECT(lev);
  1099. LWIP_UNUSED_ARG(len);
  1100. /* Get socket */
  1101. if (conn) {
  1102. s = conn->socket;
  1103. if (s < 0) {
  1104. /* Data comes in right away after an accept, even though
  1105. * the server task might not have created a new socket yet.
  1106. * Just count down (or up) if that's the case and we
  1107. * will use the data later. Note that only receive events
  1108. * can happen before the new socket is set up. */
  1109. SYS_ARCH_PROTECT(lev);
  1110. if (conn->socket < 0) {
  1111. if (evt == NETCONN_EVT_RCVPLUS) {
  1112. conn->socket--;
  1113. }
  1114. SYS_ARCH_UNPROTECT(lev);
  1115. return;
  1116. }
  1117. s = conn->socket;
  1118. SYS_ARCH_UNPROTECT(lev);
  1119. }
  1120. sock = get_socket(s);
  1121. if (!sock) {
  1122. return;
  1123. }
  1124. } else {
  1125. return;
  1126. }
  1127. SYS_ARCH_PROTECT(lev);
  1128. /* Set event as required */
  1129. switch (evt) {
  1130. case NETCONN_EVT_RCVPLUS:
  1131. sock->rcvevent++;
  1132. break;
  1133. case NETCONN_EVT_RCVMINUS:
  1134. sock->rcvevent--;
  1135. break;
  1136. case NETCONN_EVT_SENDPLUS:
  1137. sock->sendevent = 1;
  1138. break;
  1139. case NETCONN_EVT_SENDMINUS:
  1140. sock->sendevent = 0;
  1141. break;
  1142. case NETCONN_EVT_ERROR:
  1143. sock->errevent = 1;
  1144. break;
  1145. default:
  1146. LWIP_ASSERT("unknown event", 0);
  1147. break;
  1148. }
  1149. if (sock->select_waiting == 0) {
  1150. /* noone is waiting for this socket, no need to check select_cb_list */
  1151. SYS_ARCH_UNPROTECT(lev);
  1152. return;
  1153. }
  1154. /* Now decide if anyone is waiting for this socket */
  1155. /* NOTE: This code goes through the select_cb_list list multiple times
  1156. ONLY IF a select was actually waiting. We go through the list the number
  1157. of waiting select calls + 1. This list is expected to be small. */
  1158. /* At this point, SYS_ARCH is still protected! */
  1159. again:
  1160. for (scb = select_cb_list; scb != NULL; scb = scb->next) {
  1161. if (scb->sem_signalled == 0) {
  1162. /* semaphore not signalled yet */
  1163. int do_signal = 0;
  1164. /* Test this select call for our socket */
  1165. if (sock->rcvevent > 0) {
  1166. if (scb->readset && FD_ISSET(s, scb->readset)) {
  1167. do_signal = 1;
  1168. }
  1169. }
  1170. if (sock->sendevent != 0) {
  1171. if (!do_signal && scb->writeset && FD_ISSET(s, scb->writeset)) {
  1172. do_signal = 1;
  1173. }
  1174. }
  1175. if (sock->errevent != 0) {
  1176. if (!do_signal && scb->exceptset && FD_ISSET(s, scb->exceptset)) {
  1177. do_signal = 1;
  1178. }
  1179. }
  1180. if (do_signal) {
  1181. scb->sem_signalled = 1;
  1182. /* Don't call SYS_ARCH_UNPROTECT() before signaling the semaphore, as this might
  1183. lead to the select thread taking itself off the list, invalidagin the semaphore. */
  1184. sys_sem_signal(&scb->sem);
  1185. }
  1186. }
  1187. /* unlock interrupts with each step */
  1188. last_select_cb_ctr = select_cb_ctr;
  1189. SYS_ARCH_UNPROTECT(lev);
  1190. /* this makes sure interrupt protection time is short */
  1191. SYS_ARCH_PROTECT(lev);
  1192. if (last_select_cb_ctr != select_cb_ctr) {
  1193. /* someone has changed select_cb_list, restart at the beginning */
  1194. goto again;
  1195. }
  1196. }
  1197. SYS_ARCH_UNPROTECT(lev);
  1198. }
  1199. /**
  1200. * Unimplemented: Close one end of a full-duplex connection.
  1201. * Currently, the full connection is closed.
  1202. */
  1203. int
  1204. lwip_shutdown(int s, int how)
  1205. {
  1206. struct lwip_sock *sock;
  1207. err_t err;
  1208. u8_t shut_rx = 0, shut_tx = 0;
  1209. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_shutdown(%d, how=%d)\n", s, how));
  1210. sock = get_socket(s);
  1211. if (!sock) {
  1212. return -1;
  1213. }
  1214. if (sock->conn != NULL) {
  1215. if (netconn_type(sock->conn) != NETCONN_TCP) {
  1216. sock_set_errno(sock, EOPNOTSUPP);
  1217. return EOPNOTSUPP;
  1218. }
  1219. } else {
  1220. sock_set_errno(sock, ENOTCONN);
  1221. return ENOTCONN;
  1222. }
  1223. if (how == SHUT_RD) {
  1224. shut_rx = 1;
  1225. } else if (how == SHUT_WR) {
  1226. shut_tx = 1;
  1227. } else if(how == SHUT_RDWR) {
  1228. shut_rx = 1;
  1229. shut_tx = 1;
  1230. } else {
  1231. sock_set_errno(sock, EINVAL);
  1232. return EINVAL;
  1233. }
  1234. err = netconn_shutdown(sock->conn, shut_rx, shut_tx);
  1235. sock_set_errno(sock, err_to_errno(err));
  1236. return (err == ERR_OK ? 0 : -1);
  1237. }
  1238. static int
  1239. lwip_getaddrname(int s, struct sockaddr *name, socklen_t *namelen, u8_t local)
  1240. {
  1241. struct lwip_sock *sock;
  1242. struct sockaddr_in sin;
  1243. ip_addr_t naddr;
  1244. sock = get_socket(s);
  1245. if (!sock) {
  1246. return -1;
  1247. }
  1248. memset(&sin, 0, sizeof(sin));
  1249. sin.sin_len = sizeof(sin);
  1250. sin.sin_family = AF_INET;
  1251. /* get the IP address and port */
  1252. netconn_getaddr(sock->conn, &naddr, &sin.sin_port, local);
  1253. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getaddrname(%d, addr=", s));
  1254. ip_addr_debug_print(SOCKETS_DEBUG, &naddr);
  1255. LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F")\n", sin.sin_port));
  1256. sin.sin_port = htons(sin.sin_port);
  1257. inet_addr_from_ipaddr(&sin.sin_addr, &naddr);
  1258. if (*namelen > sizeof(sin)) {
  1259. *namelen = sizeof(sin);
  1260. }
  1261. MEMCPY(name, &sin, *namelen);
  1262. sock_set_errno(sock, 0);
  1263. return 0;
  1264. }
  1265. int
  1266. lwip_getpeername(int s, struct sockaddr *name, socklen_t *namelen)
  1267. {
  1268. return lwip_getaddrname(s, name, namelen, 0);
  1269. }
  1270. int
  1271. lwip_getsockname(int s, struct sockaddr *name, socklen_t *namelen)
  1272. {
  1273. return lwip_getaddrname(s, name, namelen, 1);
  1274. }
  1275. int
  1276. lwip_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen)
  1277. {
  1278. err_t err = ERR_OK;
  1279. struct lwip_sock *sock = get_socket(s);
  1280. struct lwip_setgetsockopt_data data;
  1281. if (!sock) {
  1282. return -1;
  1283. }
  1284. if ((NULL == optval) || (NULL == optlen)) {
  1285. sock_set_errno(sock, EFAULT);
  1286. return -1;
  1287. }
  1288. /* Do length and type checks for the various options first, to keep it readable. */
  1289. switch (level) {
  1290. /* Level: SOL_SOCKET */
  1291. case SOL_SOCKET:
  1292. switch (optname) {
  1293. case SO_ACCEPTCONN:
  1294. case SO_BROADCAST:
  1295. /* UNIMPL case SO_DEBUG: */
  1296. /* UNIMPL case SO_DONTROUTE: */
  1297. case SO_ERROR:
  1298. case SO_KEEPALIVE:
  1299. /* UNIMPL case SO_CONTIMEO: */
  1300. /* UNIMPL case SO_SNDTIMEO: */
  1301. #if LWIP_SO_RCVTIMEO
  1302. case SO_RCVTIMEO:
  1303. #endif /* LWIP_SO_RCVTIMEO */
  1304. #if LWIP_SO_RCVBUF
  1305. case SO_RCVBUF:
  1306. #endif /* LWIP_SO_RCVBUF */
  1307. /* UNIMPL case SO_OOBINLINE: */
  1308. /* UNIMPL case SO_SNDBUF: */
  1309. /* UNIMPL case SO_RCVLOWAT: */
  1310. /* UNIMPL case SO_SNDLOWAT: */
  1311. #if SO_REUSE
  1312. case SO_REUSEADDR:
  1313. case SO_REUSEPORT:
  1314. #endif /* SO_REUSE */
  1315. case SO_TYPE:
  1316. /* UNIMPL case SO_USELOOPBACK: */
  1317. if (*optlen < sizeof(int)) {
  1318. err = EINVAL;
  1319. }
  1320. break;
  1321. case SO_NO_CHECK:
  1322. if (*optlen < sizeof(int)) {
  1323. err = EINVAL;
  1324. }
  1325. #if LWIP_UDP
  1326. if ((sock->conn->type != NETCONN_UDP) ||
  1327. ((udp_flags(sock->conn->pcb.udp) & UDP_FLAGS_UDPLITE) != 0)) {
  1328. /* this flag is only available for UDP, not for UDP lite */
  1329. err = EAFNOSUPPORT;
  1330. }
  1331. #endif /* LWIP_UDP */
  1332. break;
  1333. default:
  1334. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, UNIMPL: optname=0x%x, ..)\n",
  1335. s, optname));
  1336. err = ENOPROTOOPT;
  1337. } /* switch (optname) */
  1338. break;
  1339. /* Level: IPPROTO_IP */
  1340. case IPPROTO_IP:
  1341. switch (optname) {
  1342. /* UNIMPL case IP_HDRINCL: */
  1343. /* UNIMPL case IP_RCVDSTADDR: */
  1344. /* UNIMPL case IP_RCVIF: */
  1345. case IP_TTL:
  1346. case IP_TOS:
  1347. if (*optlen < sizeof(int)) {
  1348. err = EINVAL;
  1349. }
  1350. break;
  1351. #if LWIP_IGMP
  1352. case IP_MULTICAST_TTL:
  1353. if (*optlen < sizeof(u8_t)) {
  1354. err = EINVAL;
  1355. }
  1356. break;
  1357. case IP_MULTICAST_IF:
  1358. if (*optlen < sizeof(struct in_addr)) {
  1359. err = EINVAL;
  1360. }
  1361. break;
  1362. case IP_MULTICAST_LOOP:
  1363. if (*optlen < sizeof(u8_t)) {
  1364. err = EINVAL;
  1365. }
  1366. if (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_UDP) {
  1367. err = EAFNOSUPPORT;
  1368. }
  1369. break;
  1370. #endif /* LWIP_IGMP */
  1371. default:
  1372. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, UNIMPL: optname=0x%x, ..)\n",
  1373. s, optname));
  1374. err = ENOPROTOOPT;
  1375. } /* switch (optname) */
  1376. break;
  1377. #if LWIP_TCP
  1378. /* Level: IPPROTO_TCP */
  1379. case IPPROTO_TCP:
  1380. if (*optlen < sizeof(int)) {
  1381. err = EINVAL;
  1382. break;
  1383. }
  1384. /* If this is no TCP socket, ignore any options. */
  1385. if (sock->conn->type != NETCONN_TCP)
  1386. return 0;
  1387. switch (optname) {
  1388. case TCP_NODELAY:
  1389. case TCP_KEEPALIVE:
  1390. #if LWIP_TCP_KEEPALIVE
  1391. case TCP_KEEPIDLE:
  1392. case TCP_KEEPINTVL:
  1393. case TCP_KEEPCNT:
  1394. #endif /* LWIP_TCP_KEEPALIVE */
  1395. break;
  1396. default:
  1397. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, UNIMPL: optname=0x%x, ..)\n",
  1398. s, optname));
  1399. err = ENOPROTOOPT;
  1400. } /* switch (optname) */
  1401. break;
  1402. #endif /* LWIP_TCP */
  1403. #if LWIP_UDP && LWIP_UDPLITE
  1404. /* Level: IPPROTO_UDPLITE */
  1405. case IPPROTO_UDPLITE:
  1406. if (*optlen < sizeof(int)) {
  1407. err = EINVAL;
  1408. break;
  1409. }
  1410. /* If this is no UDP lite socket, ignore any options. */
  1411. if (sock->conn->type != NETCONN_UDPLITE) {
  1412. return 0;
  1413. }
  1414. switch (optname) {
  1415. case UDPLITE_SEND_CSCOV:
  1416. case UDPLITE_RECV_CSCOV:
  1417. break;
  1418. default:
  1419. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UNIMPL: optname=0x%x, ..)\n",
  1420. s, optname));
  1421. err = ENOPROTOOPT;
  1422. } /* switch (optname) */
  1423. break;
  1424. #endif /* LWIP_UDP && LWIP_UDPLITE*/
  1425. /* UNDEFINED LEVEL */
  1426. default:
  1427. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, level=0x%x, UNIMPL: optname=0x%x, ..)\n",
  1428. s, level, optname));
  1429. err = ENOPROTOOPT;
  1430. } /* switch */
  1431. if (err != ERR_OK) {
  1432. sock_set_errno(sock, err);
  1433. return -1;
  1434. }
  1435. /* Now do the actual option processing */
  1436. data.sock = sock;
  1437. #ifdef LWIP_DEBUG
  1438. data.s = s;
  1439. #endif /* LWIP_DEBUG */
  1440. data.level = level;
  1441. data.optname = optname;
  1442. data.optval = optval;
  1443. data.optlen = optlen;
  1444. data.err = err;
  1445. tcpip_callback(lwip_getsockopt_internal, &data);
  1446. sys_arch_sem_wait(&sock->conn->op_completed, 0);
  1447. /* maybe lwip_getsockopt_internal has changed err */
  1448. err = data.err;
  1449. sock_set_errno(sock, err);
  1450. return err ? -1 : 0;
  1451. }
  1452. static void
  1453. lwip_getsockopt_internal(void *arg)
  1454. {
  1455. struct lwip_sock *sock;
  1456. #ifdef LWIP_DEBUG
  1457. int s;
  1458. #endif /* LWIP_DEBUG */
  1459. int level, optname;
  1460. void *optval;
  1461. struct lwip_setgetsockopt_data *data;
  1462. LWIP_ASSERT("arg != NULL", arg != NULL);
  1463. data = (struct lwip_setgetsockopt_data*)arg;
  1464. sock = data->sock;
  1465. #ifdef LWIP_DEBUG
  1466. s = data->s;
  1467. #endif /* LWIP_DEBUG */
  1468. level = data->level;
  1469. optname = data->optname;
  1470. optval = data->optval;
  1471. switch (level) {
  1472. /* Level: SOL_SOCKET */
  1473. case SOL_SOCKET:
  1474. switch (optname) {
  1475. /* The option flags */
  1476. case SO_ACCEPTCONN:
  1477. case SO_BROADCAST:
  1478. /* UNIMPL case SO_DEBUG: */
  1479. /* UNIMPL case SO_DONTROUTE: */
  1480. case SO_KEEPALIVE:
  1481. /* UNIMPL case SO_OOBINCLUDE: */
  1482. #if SO_REUSE
  1483. case SO_REUSEADDR:
  1484. case SO_REUSEPORT:
  1485. #endif /* SO_REUSE */
  1486. /*case SO_USELOOPBACK: UNIMPL */
  1487. *(int*)optval = sock->conn->pcb.ip->so_options & optname;
  1488. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, optname=0x%x, ..) = %s\n",
  1489. s, optname, (*(int*)optval?"on":"off")));
  1490. break;
  1491. case SO_TYPE:
  1492. switch (NETCONNTYPE_GROUP(sock->conn->type)) {
  1493. case NETCONN_RAW:
  1494. *(int*)optval = SOCK_RAW;
  1495. break;
  1496. case NETCONN_TCP:
  1497. *(int*)optval = SOCK_STREAM;
  1498. break;
  1499. case NETCONN_UDP:
  1500. *(int*)optval = SOCK_DGRAM;
  1501. break;
  1502. default: /* unrecognized socket type */
  1503. *(int*)optval = sock->conn->type;
  1504. LWIP_DEBUGF(SOCKETS_DEBUG,
  1505. ("lwip_getsockopt(%d, SOL_SOCKET, SO_TYPE): unrecognized socket type %d\n",
  1506. s, *(int *)optval));
  1507. } /* switch (sock->conn->type) */
  1508. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_TYPE) = %d\n",
  1509. s, *(int *)optval));
  1510. break;
  1511. case SO_ERROR:
  1512. /* only overwrite ERR_OK or tempoary errors */
  1513. if ((sock->err == 0) || (sock->err == EINPROGRESS)) {
  1514. sock_set_errno(sock, err_to_errno(sock->conn->last_err));
  1515. }
  1516. *(int *)optval = sock->err;
  1517. sock->err = 0;
  1518. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_ERROR) = %d\n",
  1519. s, *(int *)optval));
  1520. break;
  1521. #if LWIP_SO_RCVTIMEO
  1522. case SO_RCVTIMEO:
  1523. *(int *)optval = netconn_get_recvtimeout(sock->conn);
  1524. break;
  1525. #endif /* LWIP_SO_RCVTIMEO */
  1526. #if LWIP_SO_RCVBUF
  1527. case SO_RCVBUF:
  1528. *(int *)optval = netconn_get_recvbufsize(sock->conn);
  1529. break;
  1530. #endif /* LWIP_SO_RCVBUF */
  1531. #if LWIP_UDP
  1532. case SO_NO_CHECK:
  1533. *(int*)optval = (udp_flags(sock->conn->pcb.udp) & UDP_FLAGS_NOCHKSUM) ? 1 : 0;
  1534. break;
  1535. #endif /* LWIP_UDP*/
  1536. default:
  1537. LWIP_ASSERT("unhandled optname", 0);
  1538. break;
  1539. } /* switch (optname) */
  1540. break;
  1541. /* Level: IPPROTO_IP */
  1542. case IPPROTO_IP:
  1543. switch (optname) {
  1544. case IP_TTL:
  1545. *(int*)optval = sock->conn->pcb.ip->ttl;
  1546. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_TTL) = %d\n",
  1547. s, *(int *)optval));
  1548. break;
  1549. case IP_TOS:
  1550. *(int*)optval = sock->conn->pcb.ip->tos;
  1551. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_TOS) = %d\n",
  1552. s, *(int *)optval));
  1553. break;
  1554. #if LWIP_IGMP
  1555. case IP_MULTICAST_TTL:
  1556. *(u8_t*)optval = sock->conn->pcb.ip->ttl;
  1557. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_MULTICAST_TTL) = %d\n",
  1558. s, *(int *)optval));
  1559. break;
  1560. case IP_MULTICAST_IF:
  1561. inet_addr_from_ipaddr((struct in_addr*)optval, &sock->conn->pcb.udp->multicast_ip);
  1562. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_MULTICAST_IF) = 0x%"X32_F"\n",
  1563. s, *(u32_t *)optval));
  1564. break;
  1565. case IP_MULTICAST_LOOP:
  1566. if ((sock->conn->pcb.udp->flags & UDP_FLAGS_MULTICAST_LOOP) != 0) {
  1567. *(u8_t*)optval = 1;
  1568. } else {
  1569. *(u8_t*)optval = 0;
  1570. }
  1571. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, IP_MULTICAST_LOOP) = %d\n",
  1572. s, *(int *)optval));
  1573. break;
  1574. #endif /* LWIP_IGMP */
  1575. default:
  1576. LWIP_ASSERT("unhandled optname", 0);
  1577. break;
  1578. } /* switch (optname) */
  1579. break;
  1580. #if LWIP_TCP
  1581. /* Level: IPPROTO_TCP */
  1582. case IPPROTO_TCP:
  1583. switch (optname) {
  1584. case TCP_NODELAY:
  1585. *(int*)optval = tcp_nagle_disabled(sock->conn->pcb.tcp);
  1586. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, TCP_NODELAY) = %s\n",
  1587. s, (*(int*)optval)?"on":"off") );
  1588. break;
  1589. case TCP_KEEPALIVE:
  1590. *(int*)optval = (int)sock->conn->pcb.tcp->keep_idle;
  1591. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, TCP_KEEPALIVE) = %d\n",
  1592. s, *(int *)optval));
  1593. break;
  1594. #if LWIP_TCP_KEEPALIVE
  1595. case TCP_KEEPIDLE:
  1596. *(int*)optval = (int)(sock->conn->pcb.tcp->keep_idle/1000);
  1597. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, TCP_KEEPIDLE) = %d\n",
  1598. s, *(int *)optval));
  1599. break;
  1600. case TCP_KEEPINTVL:
  1601. *(int*)optval = (int)(sock->conn->pcb.tcp->keep_intvl/1000);
  1602. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, TCP_KEEPINTVL) = %d\n",
  1603. s, *(int *)optval));
  1604. break;
  1605. case TCP_KEEPCNT:
  1606. *(int*)optval = (int)sock->conn->pcb.tcp->keep_cnt;
  1607. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_IP, TCP_KEEPCNT) = %d\n",
  1608. s, *(int *)optval));
  1609. break;
  1610. #endif /* LWIP_TCP_KEEPALIVE */
  1611. default:
  1612. LWIP_ASSERT("unhandled optname", 0);
  1613. break;
  1614. } /* switch (optname) */
  1615. break;
  1616. #endif /* LWIP_TCP */
  1617. #if LWIP_UDP && LWIP_UDPLITE
  1618. /* Level: IPPROTO_UDPLITE */
  1619. case IPPROTO_UDPLITE:
  1620. switch (optname) {
  1621. case UDPLITE_SEND_CSCOV:
  1622. *(int*)optval = sock->conn->pcb.udp->chksum_len_tx;
  1623. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UDPLITE_SEND_CSCOV) = %d\n",
  1624. s, (*(int*)optval)) );
  1625. break;
  1626. case UDPLITE_RECV_CSCOV:
  1627. *(int*)optval = sock->conn->pcb.udp->chksum_len_rx;
  1628. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UDPLITE_RECV_CSCOV) = %d\n",
  1629. s, (*(int*)optval)) );
  1630. break;
  1631. default:
  1632. LWIP_ASSERT("unhandled optname", 0);
  1633. break;
  1634. } /* switch (optname) */
  1635. break;
  1636. #endif /* LWIP_UDP */
  1637. default:
  1638. LWIP_ASSERT("unhandled level", 0);
  1639. break;
  1640. } /* switch (level) */
  1641. sys_sem_signal(&sock->conn->op_completed);
  1642. }
  1643. int
  1644. lwip_setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen)
  1645. {
  1646. struct lwip_sock *sock = get_socket(s);
  1647. err_t err = ERR_OK;
  1648. struct lwip_setgetsockopt_data data;
  1649. if (!sock) {
  1650. return -1;
  1651. }
  1652. if (NULL == optval) {
  1653. sock_set_errno(sock, EFAULT);
  1654. return -1;
  1655. }
  1656. /* Do length and type checks for the various options first, to keep it readable. */
  1657. switch (level) {
  1658. /* Level: SOL_SOCKET */
  1659. case SOL_SOCKET:
  1660. switch (optname) {
  1661. case SO_BROADCAST:
  1662. /* UNIMPL case SO_DEBUG: */
  1663. /* UNIMPL case SO_DONTROUTE: */
  1664. case SO_KEEPALIVE:
  1665. /* UNIMPL case case SO_CONTIMEO: */
  1666. /* UNIMPL case case SO_SNDTIMEO: */
  1667. #if LWIP_SO_RCVTIMEO
  1668. case SO_RCVTIMEO:
  1669. #endif /* LWIP_SO_RCVTIMEO */
  1670. #if LWIP_SO_RCVBUF
  1671. case SO_RCVBUF:
  1672. #endif /* LWIP_SO_RCVBUF */
  1673. /* UNIMPL case SO_OOBINLINE: */
  1674. /* UNIMPL case SO_SNDBUF: */
  1675. /* UNIMPL case SO_RCVLOWAT: */
  1676. /* UNIMPL case SO_SNDLOWAT: */
  1677. #if SO_REUSE
  1678. case SO_REUSEADDR:
  1679. case SO_REUSEPORT:
  1680. #endif /* SO_REUSE */
  1681. /* UNIMPL case SO_USELOOPBACK: */
  1682. if (optlen < sizeof(int)) {
  1683. err = EINVAL;
  1684. }
  1685. break;
  1686. case SO_NO_CHECK:
  1687. if (optlen < sizeof(int)) {
  1688. err = EINVAL;
  1689. }
  1690. #if LWIP_UDP
  1691. if ((sock->conn->type != NETCONN_UDP) ||
  1692. ((udp_flags(sock->conn->pcb.udp) & UDP_FLAGS_UDPLITE) != 0)) {
  1693. /* this flag is only available for UDP, not for UDP lite */
  1694. err = EAFNOSUPPORT;
  1695. }
  1696. #endif /* LWIP_UDP */
  1697. break;
  1698. default:
  1699. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, SOL_SOCKET, UNIMPL: optname=0x%x, ..)\n",
  1700. s, optname));
  1701. err = ENOPROTOOPT;
  1702. } /* switch (optname) */
  1703. break;
  1704. /* Level: IPPROTO_IP */
  1705. case IPPROTO_IP:
  1706. switch (optname) {
  1707. /* UNIMPL case IP_HDRINCL: */
  1708. /* UNIMPL case IP_RCVDSTADDR: */
  1709. /* UNIMPL case IP_RCVIF: */
  1710. case IP_TTL:
  1711. case IP_TOS:
  1712. if (optlen < sizeof(int)) {
  1713. err = EINVAL;
  1714. }
  1715. break;
  1716. #if LWIP_IGMP
  1717. case IP_MULTICAST_TTL:
  1718. if (optlen < sizeof(u8_t)) {
  1719. err = EINVAL;
  1720. }
  1721. if (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_UDP) {
  1722. err = EAFNOSUPPORT;
  1723. }
  1724. break;
  1725. case IP_MULTICAST_IF:
  1726. if (optlen < sizeof(struct in_addr)) {
  1727. err = EINVAL;
  1728. }
  1729. if (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_UDP) {
  1730. err = EAFNOSUPPORT;
  1731. }
  1732. break;
  1733. case IP_MULTICAST_LOOP:
  1734. if (optlen < sizeof(u8_t)) {
  1735. err = EINVAL;
  1736. }
  1737. if (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_UDP) {
  1738. err = EAFNOSUPPORT;
  1739. }
  1740. break;
  1741. case IP_ADD_MEMBERSHIP:
  1742. case IP_DROP_MEMBERSHIP:
  1743. if (optlen < sizeof(struct ip_mreq)) {
  1744. err = EINVAL;
  1745. }
  1746. if (NETCONNTYPE_GROUP(sock->conn->type) != NETCONN_UDP) {
  1747. err = EAFNOSUPPORT;
  1748. }
  1749. break;
  1750. #endif /* LWIP_IGMP */
  1751. default:
  1752. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, UNIMPL: optname=0x%x, ..)\n",
  1753. s, optname));
  1754. err = ENOPROTOOPT;
  1755. } /* switch (optname) */
  1756. break;
  1757. #if LWIP_TCP
  1758. /* Level: IPPROTO_TCP */
  1759. case IPPROTO_TCP:
  1760. if (optlen < sizeof(int)) {
  1761. err = EINVAL;
  1762. break;
  1763. }
  1764. /* If this is no TCP socket, ignore any options. */
  1765. if (sock->conn->type != NETCONN_TCP)
  1766. return 0;
  1767. switch (optname) {
  1768. case TCP_NODELAY:
  1769. case TCP_KEEPALIVE:
  1770. #if LWIP_TCP_KEEPALIVE
  1771. case TCP_KEEPIDLE:
  1772. case TCP_KEEPINTVL:
  1773. case TCP_KEEPCNT:
  1774. #endif /* LWIP_TCP_KEEPALIVE */
  1775. break;
  1776. default:
  1777. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, UNIMPL: optname=0x%x, ..)\n",
  1778. s, optname));
  1779. err = ENOPROTOOPT;
  1780. } /* switch (optname) */
  1781. break;
  1782. #endif /* LWIP_TCP */
  1783. #if LWIP_UDP && LWIP_UDPLITE
  1784. /* Level: IPPROTO_UDPLITE */
  1785. case IPPROTO_UDPLITE:
  1786. if (optlen < sizeof(int)) {
  1787. err = EINVAL;
  1788. break;
  1789. }
  1790. /* If this is no UDP lite socket, ignore any options. */
  1791. if (sock->conn->type != NETCONN_UDPLITE)
  1792. return 0;
  1793. switch (optname) {
  1794. case UDPLITE_SEND_CSCOV:
  1795. case UDPLITE_RECV_CSCOV:
  1796. break;
  1797. default:
  1798. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_UDPLITE, UNIMPL: optname=0x%x, ..)\n",
  1799. s, optname));
  1800. err = ENOPROTOOPT;
  1801. } /* switch (optname) */
  1802. break;
  1803. #endif /* LWIP_UDP && LWIP_UDPLITE */
  1804. /* UNDEFINED LEVEL */
  1805. default:
  1806. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, level=0x%x, UNIMPL: optname=0x%x, ..)\n",
  1807. s, level, optname));
  1808. err = ENOPROTOOPT;
  1809. } /* switch (level) */
  1810. if (err != ERR_OK) {
  1811. sock_set_errno(sock, err);
  1812. return -1;
  1813. }
  1814. /* Now do the actual option processing */
  1815. data.sock = sock;
  1816. #ifdef LWIP_DEBUG
  1817. data.s = s;
  1818. #endif /* LWIP_DEBUG */
  1819. data.level = level;
  1820. data.optname = optname;
  1821. data.optval = (void*)optval;
  1822. data.optlen = &optlen;
  1823. data.err = err;
  1824. tcpip_callback(lwip_setsockopt_internal, &data);
  1825. sys_arch_sem_wait(&sock->conn->op_completed, 0);
  1826. /* maybe lwip_setsockopt_internal has changed err */
  1827. err = data.err;
  1828. sock_set_errno(sock, err);
  1829. return err ? -1 : 0;
  1830. }
  1831. static void
  1832. lwip_setsockopt_internal(void *arg)
  1833. {
  1834. struct lwip_sock *sock;
  1835. #ifdef LWIP_DEBUG
  1836. int s;
  1837. #endif /* LWIP_DEBUG */
  1838. int level, optname;
  1839. const void *optval;
  1840. struct lwip_setgetsockopt_data *data;
  1841. LWIP_ASSERT("arg != NULL", arg != NULL);
  1842. data = (struct lwip_setgetsockopt_data*)arg;
  1843. sock = data->sock;
  1844. #ifdef LWIP_DEBUG
  1845. s = data->s;
  1846. #endif /* LWIP_DEBUG */
  1847. level = data->level;
  1848. optname = data->optname;
  1849. optval = data->optval;
  1850. switch (level) {
  1851. /* Level: SOL_SOCKET */
  1852. case SOL_SOCKET:
  1853. switch (optname) {
  1854. /* The option flags */
  1855. case SO_BROADCAST:
  1856. /* UNIMPL case SO_DEBUG: */
  1857. /* UNIMPL case SO_DONTROUTE: */
  1858. case SO_KEEPALIVE:
  1859. /* UNIMPL case SO_OOBINCLUDE: */
  1860. #if SO_REUSE
  1861. case SO_REUSEADDR:
  1862. case SO_REUSEPORT:
  1863. #endif /* SO_REUSE */
  1864. /* UNIMPL case SO_USELOOPBACK: */
  1865. if (*(int*)optval) {
  1866. sock->conn->pcb.ip->so_options |= optname;
  1867. } else {
  1868. sock->conn->pcb.ip->so_options &= ~optname;
  1869. }
  1870. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, SOL_SOCKET, optname=0x%x, ..) -> %s\n",
  1871. s, optname, (*(int*)optval?"on":"off")));
  1872. break;
  1873. #if LWIP_SO_RCVTIMEO
  1874. case SO_RCVTIMEO:
  1875. netconn_set_recvtimeout(sock->conn, *(int*)optval);
  1876. break;
  1877. #endif /* LWIP_SO_RCVTIMEO */
  1878. #if LWIP_SO_RCVBUF
  1879. case SO_RCVBUF:
  1880. netconn_set_recvbufsize(sock->conn, *(int*)optval);
  1881. break;
  1882. #endif /* LWIP_SO_RCVBUF */
  1883. #if LWIP_UDP
  1884. case SO_NO_CHECK:
  1885. if (*(int*)optval) {
  1886. udp_setflags(sock->conn->pcb.udp, udp_flags(sock->conn->pcb.udp) | UDP_FLAGS_NOCHKSUM);
  1887. } else {
  1888. udp_setflags(sock->conn->pcb.udp, udp_flags(sock->conn->pcb.udp) & ~UDP_FLAGS_NOCHKSUM);
  1889. }
  1890. break;
  1891. #endif /* LWIP_UDP */
  1892. default:
  1893. LWIP_ASSERT("unhandled optname", 0);
  1894. break;
  1895. } /* switch (optname) */
  1896. break;
  1897. /* Level: IPPROTO_IP */
  1898. case IPPROTO_IP:
  1899. switch (optname) {
  1900. case IP_TTL:
  1901. sock->conn->pcb.ip->ttl = (u8_t)(*(int*)optval);
  1902. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, IP_TTL, ..) -> %d\n",
  1903. s, sock->conn->pcb.ip->ttl));
  1904. break;
  1905. case IP_TOS:
  1906. sock->conn->pcb.ip->tos = (u8_t)(*(int*)optval);
  1907. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_IP, IP_TOS, ..)-> %d\n",
  1908. s, sock->conn->pcb.ip->tos));
  1909. break;
  1910. #if LWIP_IGMP
  1911. case IP_MULTICAST_TTL:
  1912. sock->conn->pcb.udp->ttl = (u8_t)(*(u8_t*)optval);
  1913. break;
  1914. case IP_MULTICAST_IF:
  1915. inet_addr_to_ipaddr(&sock->conn->pcb.udp->multicast_ip, (struct in_addr*)optval);
  1916. break;
  1917. case IP_MULTICAST_LOOP:
  1918. if (*(u8_t*)optval) {
  1919. udp_setflags(sock->conn->pcb.udp, udp_flags(sock->conn->pcb.udp) | UDP_FLAGS_MULTICAST_LOOP);
  1920. } else {
  1921. udp_setflags(sock->conn->pcb.udp, udp_flags(sock->conn->pcb.udp) & ~UDP_FLAGS_MULTICAST_LOOP);
  1922. }
  1923. break;
  1924. case IP_ADD_MEMBERSHIP:
  1925. case IP_DROP_MEMBERSHIP:
  1926. {
  1927. /* If this is a TCP or a RAW socket, ignore these options. */
  1928. struct ip_mreq *imr = (struct ip_mreq *)optval;
  1929. ip_addr_t if_addr;
  1930. ip_addr_t multi_addr;
  1931. inet_addr_to_ipaddr(&if_addr, &imr->imr_interface);
  1932. inet_addr_to_ipaddr(&multi_addr, &imr->imr_multiaddr);
  1933. if(optname == IP_ADD_MEMBERSHIP){
  1934. data->err = igmp_joingroup(&if_addr, &multi_addr);
  1935. } else {
  1936. data->err = igmp_leavegroup(&if_addr, &multi_addr);
  1937. }
  1938. if(data->err != ERR_OK) {
  1939. data->err = EADDRNOTAVAIL;
  1940. }
  1941. }
  1942. break;
  1943. #endif /* LWIP_IGMP */
  1944. default:
  1945. LWIP_ASSERT("unhandled optname", 0);
  1946. break;
  1947. } /* switch (optname) */
  1948. break;
  1949. #if LWIP_TCP
  1950. /* Level: IPPROTO_TCP */
  1951. case IPPROTO_TCP:
  1952. switch (optname) {
  1953. case TCP_NODELAY:
  1954. if (*(int*)optval) {
  1955. tcp_nagle_disable(sock->conn->pcb.tcp);
  1956. } else {
  1957. tcp_nagle_enable(sock->conn->pcb.tcp);
  1958. }
  1959. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_NODELAY) -> %s\n",
  1960. s, (*(int *)optval)?"on":"off") );
  1961. break;
  1962. case TCP_KEEPALIVE:
  1963. sock->conn->pcb.tcp->keep_idle = (u32_t)(*(int*)optval);
  1964. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPALIVE) -> %"U32_F"\n",
  1965. s, sock->conn->pcb.tcp->keep_idle));
  1966. break;
  1967. #if LWIP_TCP_KEEPALIVE
  1968. case TCP_KEEPIDLE:
  1969. sock->conn->pcb.tcp->keep_idle = 1000*(u32_t)(*(int*)optval);
  1970. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPIDLE) -> %"U32_F"\n",
  1971. s, sock->conn->pcb.tcp->keep_idle));
  1972. break;
  1973. case TCP_KEEPINTVL:
  1974. sock->conn->pcb.tcp->keep_intvl = 1000*(u32_t)(*(int*)optval);
  1975. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPINTVL) -> %"U32_F"\n",
  1976. s, sock->conn->pcb.tcp->keep_intvl));
  1977. break;
  1978. case TCP_KEEPCNT:
  1979. sock->conn->pcb.tcp->keep_cnt = (u32_t)(*(int*)optval);
  1980. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_TCP, TCP_KEEPCNT) -> %"U32_F"\n",
  1981. s, sock->conn->pcb.tcp->keep_cnt));
  1982. break;
  1983. #endif /* LWIP_TCP_KEEPALIVE */
  1984. default:
  1985. LWIP_ASSERT("unhandled optname", 0);
  1986. break;
  1987. } /* switch (optname) */
  1988. break;
  1989. #endif /* LWIP_TCP*/
  1990. #if LWIP_UDP && LWIP_UDPLITE
  1991. /* Level: IPPROTO_UDPLITE */
  1992. case IPPROTO_UDPLITE:
  1993. switch (optname) {
  1994. case UDPLITE_SEND_CSCOV:
  1995. if ((*(int*)optval != 0) && ((*(int*)optval < 8)) || (*(int*)optval > 0xffff)) {
  1996. /* don't allow illegal values! */
  1997. sock->conn->pcb.udp->chksum_len_tx = 8;
  1998. } else {
  1999. sock->conn->pcb.udp->chksum_len_tx = (u16_t)*(int*)optval;
  2000. }
  2001. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_UDPLITE, UDPLITE_SEND_CSCOV) -> %d\n",
  2002. s, (*(int*)optval)) );
  2003. break;
  2004. case UDPLITE_RECV_CSCOV:
  2005. if ((*(int*)optval != 0) && ((*(int*)optval < 8)) || (*(int*)optval > 0xffff)) {
  2006. /* don't allow illegal values! */
  2007. sock->conn->pcb.udp->chksum_len_rx = 8;
  2008. } else {
  2009. sock->conn->pcb.udp->chksum_len_rx = (u16_t)*(int*)optval;
  2010. }
  2011. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_setsockopt(%d, IPPROTO_UDPLITE, UDPLITE_RECV_CSCOV) -> %d\n",
  2012. s, (*(int*)optval)) );
  2013. break;
  2014. default:
  2015. LWIP_ASSERT("unhandled optname", 0);
  2016. break;
  2017. } /* switch (optname) */
  2018. break;
  2019. #endif /* LWIP_UDP */
  2020. default:
  2021. LWIP_ASSERT("unhandled level", 0);
  2022. break;
  2023. } /* switch (level) */
  2024. sys_sem_signal(&sock->conn->op_completed);
  2025. }
  2026. int
  2027. lwip_ioctl(int s, long cmd, void *argp)
  2028. {
  2029. struct lwip_sock *sock = get_socket(s);
  2030. u8_t val;
  2031. #if LWIP_SO_RCVBUF
  2032. u16_t buflen = 0;
  2033. s16_t recv_avail;
  2034. #endif /* LWIP_SO_RCVBUF */
  2035. if (!sock) {
  2036. return -1;
  2037. }
  2038. switch (cmd) {
  2039. #if LWIP_SO_RCVBUF
  2040. case FIONREAD:
  2041. if (!argp) {
  2042. sock_set_errno(sock, EINVAL);
  2043. return -1;
  2044. }
  2045. SYS_ARCH_GET(sock->conn->recv_avail, recv_avail);
  2046. if (recv_avail < 0) {
  2047. recv_avail = 0;
  2048. }
  2049. *((u16_t*)argp) = (u16_t)recv_avail;
  2050. /* Check if there is data left from the last recv operation. /maq 041215 */
  2051. if (sock->lastdata) {
  2052. struct pbuf *p = (struct pbuf *)sock->lastdata;
  2053. if (netconn_type(sock->conn) != NETCONN_TCP) {
  2054. p = ((struct netbuf *)p)->p;
  2055. }
  2056. buflen = p->tot_len;
  2057. buflen -= sock->lastoffset;
  2058. *((u16_t*)argp) += buflen;
  2059. }
  2060. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, FIONREAD, %p) = %"U16_F"\n", s, argp, *((u16_t*)argp)));
  2061. sock_set_errno(sock, 0);
  2062. return 0;
  2063. #endif /* LWIP_SO_RCVBUF */
  2064. case FIONBIO:
  2065. val = 0;
  2066. if (argp && *(u32_t*)argp) {
  2067. val = 1;
  2068. }
  2069. netconn_set_nonblocking(sock->conn, val);
  2070. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, FIONBIO, %d)\n", s, val));
  2071. sock_set_errno(sock, 0);
  2072. return 0;
  2073. default:
  2074. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_ioctl(%d, UNIMPL: 0x%lx, %p)\n", s, cmd, argp));
  2075. sock_set_errno(sock, ENOSYS); /* not yet implemented */
  2076. return -1;
  2077. } /* switch (cmd) */
  2078. }
  2079. /** A minimal implementation of fcntl.
  2080. * Currently only the commands F_GETFL and F_SETFL are implemented.
  2081. * Only the flag O_NONBLOCK is implemented.
  2082. */
  2083. int
  2084. lwip_fcntl(int s, int cmd, int val)
  2085. {
  2086. struct lwip_sock *sock = get_socket(s);
  2087. int ret = -1;
  2088. if (!sock || !sock->conn) {
  2089. return -1;
  2090. }
  2091. switch (cmd) {
  2092. case F_GETFL:
  2093. ret = netconn_is_nonblocking(sock->conn) ? O_NONBLOCK : 0;
  2094. break;
  2095. case F_SETFL:
  2096. if ((val & ~O_NONBLOCK) == 0) {
  2097. /* only O_NONBLOCK, all other bits are zero */
  2098. netconn_set_nonblocking(sock->conn, val & O_NONBLOCK);
  2099. ret = 0;
  2100. }
  2101. break;
  2102. default:
  2103. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_fcntl(%d, UNIMPL: %d, %d)\n", s, cmd, val));
  2104. break;
  2105. }
  2106. return ret;
  2107. }
  2108. #endif /* LWIP_SOCKET */