sal_socket.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /*
  2. * File : sal_socket.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-05-23 ChenYong First version
  23. */
  24. #include <rtthread.h>
  25. #include <rthw.h>
  26. #include <sal_socket.h>
  27. #include <sal_netdb.h>
  28. #include <sal.h>
  29. #define DBG_ENABLE
  30. #define DBG_SECTION_NAME "SAL_SOC"
  31. #define DBG_LEVEL DBG_INFO
  32. #define DBG_COLOR
  33. #include <rtdbg.h>
  34. /* the socket table used to dynamic allocate sockets */
  35. struct sal_socket_table
  36. {
  37. uint32_t max_socket;
  38. struct sal_socket **sockets;
  39. };
  40. /* The global array of available protocol families */
  41. static struct proto_family proto_families[SAL_PROTO_FAMILIES_NUM];
  42. /* The global socket table */
  43. static struct sal_socket_table socket_table;
  44. static struct rt_mutex sal_core_lock;
  45. static rt_bool_t init_ok = RT_FALSE;
  46. /**
  47. * SAL (Socket Abstraction Layer) initialization.
  48. *
  49. * @return result
  50. * >= 0: initialize success
  51. */
  52. int sal_init(void)
  53. {
  54. if(init_ok)
  55. {
  56. LOG_D("Socket Abstraction Layer is already initialized.");
  57. return 0;
  58. }
  59. /* clean sal socket table */
  60. memset(&socket_table, 0, sizeof(socket_table));
  61. /* create sal socket lock */
  62. rt_mutex_init(&sal_core_lock, "sal_lock", RT_IPC_FLAG_FIFO);
  63. LOG_I("Socket Abstraction Layer initialize success.");
  64. init_ok = RT_TRUE;
  65. return 0;
  66. }
  67. INIT_COMPONENT_EXPORT(sal_init);
  68. /**
  69. * this function will register the current protocol family to the global array of protocol families.
  70. *
  71. * @param pf protocol families structure
  72. *
  73. * @return 0 : protocol families structure index
  74. * -1 : the global array of available protocol families is full
  75. */
  76. int sal_proto_family_register(const struct proto_family *pf)
  77. {
  78. rt_base_t level;
  79. int idx;
  80. /* disable interrupt */
  81. level = rt_hw_interrupt_disable();
  82. /* find an empty protocol family entry */
  83. for(idx = 0; idx < SAL_PROTO_FAMILIES_NUM && proto_families[idx].create; idx++);
  84. /* can't find an empty protocol family entry */
  85. if(idx == SAL_PROTO_FAMILIES_NUM)
  86. {
  87. /* enable interrupt */
  88. rt_hw_interrupt_enable(level);
  89. return -1;
  90. }
  91. proto_families[idx].family = pf->family;
  92. proto_families[idx].sec_family = pf->sec_family;
  93. proto_families[idx].create = pf->create;
  94. proto_families[idx].gethostbyname = pf->gethostbyname;
  95. proto_families[idx].gethostbyname_r = pf->gethostbyname_r;
  96. proto_families[idx].freeaddrinfo = pf->freeaddrinfo;
  97. proto_families[idx].getaddrinfo = pf->getaddrinfo;
  98. /* enable interrupt */
  99. rt_hw_interrupt_enable(level);
  100. return 0;
  101. }
  102. /**
  103. * this function will get socket structure by sal socket descriptor
  104. *
  105. * @param socket sal socket index
  106. *
  107. * @return socket structure of the current sal socket index
  108. */
  109. struct sal_socket *sal_get_socket(int socket)
  110. {
  111. struct sal_socket_table *st = &socket_table;
  112. if (socket < 0 || socket >= (int) st->max_socket)
  113. {
  114. return RT_NULL;
  115. }
  116. socket = socket - SAL_SOCKET_OFFSET;
  117. /* check socket structure valid or not */
  118. if (st->sockets[socket]->magic != SAL_SOCKET_MAGIC)
  119. {
  120. return RT_NULL;
  121. }
  122. return st->sockets[socket];
  123. }
  124. /**
  125. * this function will lock sal socket.
  126. *
  127. * @note please don't invoke it on ISR.
  128. */
  129. static void sal_lock(void)
  130. {
  131. rt_err_t result;
  132. result = rt_mutex_take(&sal_core_lock, RT_WAITING_FOREVER);
  133. if (result != RT_EOK)
  134. {
  135. RT_ASSERT(0);
  136. }
  137. }
  138. /**
  139. * this function will lock sal socket.
  140. *
  141. * @note please don't invoke it on ISR.
  142. */
  143. static void sal_unlock(void)
  144. {
  145. rt_mutex_release(&sal_core_lock);
  146. }
  147. /**
  148. * this function will get protocol family structure by family type
  149. *
  150. * @param family protocol family
  151. *
  152. * @return protocol family structure address
  153. */
  154. static struct proto_family *get_proto_family(int family)
  155. {
  156. int idx;
  157. for (idx = 0; idx < SAL_PROTO_FAMILIES_NUM; idx++)
  158. {
  159. if (proto_families[idx].family == family && proto_families[idx].create)
  160. {
  161. return &proto_families[idx];
  162. }
  163. }
  164. /* compare the secondary protocol families when primary protocol families find failed */
  165. for (idx = 0; idx < SAL_PROTO_FAMILIES_NUM; idx++)
  166. {
  167. if (proto_families[idx].sec_family == family && proto_families[idx].create)
  168. {
  169. return &proto_families[idx];
  170. }
  171. }
  172. return RT_NULL;
  173. }
  174. /**
  175. * this function will initialize socket structure and set socket options
  176. *
  177. * @param family protocol family
  178. * @param type socket type
  179. * @param protocol transfer Protocol
  180. * @param res socket structure address
  181. *
  182. * @return 0 : socket initialize success
  183. * -1 : input the wrong family
  184. * -2 : input the wrong socket type
  185. * -3 : get protocol family structure failed
  186. * -4 : set socket options failed
  187. */
  188. static int socket_init(int family, int type, int protocol, struct sal_socket **res)
  189. {
  190. struct sal_socket *sock;
  191. struct proto_family *pf;
  192. if (family < 0 || family > AF_MAX)
  193. {
  194. return -1;
  195. }
  196. if (type < 0 || type > SOCK_MAX)
  197. {
  198. return -2;
  199. }
  200. sock = *res;
  201. sock->domain = family;
  202. sock->type = type;
  203. sock->protocol = protocol;
  204. /* get socket protocol family structure */
  205. if ((pf = get_proto_family(family)) == RT_NULL)
  206. {
  207. return -3;
  208. }
  209. /* registered the current socket options */
  210. if (pf->create(sock, type, protocol) != 0)
  211. {
  212. return -4;
  213. }
  214. return 0;
  215. }
  216. static int socket_alloc(struct sal_socket_table *st, int f_socket)
  217. {
  218. int idx;
  219. /* find an empty socket entry */
  220. for (idx = f_socket; idx < (int) st->max_socket; idx++)
  221. {
  222. if (st->sockets[idx] == RT_NULL)
  223. break;
  224. if (st->sockets[idx]->ops == RT_NULL)
  225. break;
  226. }
  227. /* allocate a larger sockte container */
  228. if (idx == (int) st->max_socket && st->max_socket < SAL_SOCKETS_NUM)
  229. {
  230. int cnt, index;
  231. struct sal_socket **sockets;
  232. /* increase the number of FD with 4 step length */
  233. cnt = st->max_socket + 4;
  234. cnt = cnt > SAL_SOCKETS_NUM ? SAL_SOCKETS_NUM : cnt;
  235. sockets = rt_realloc(st->sockets, cnt * sizeof(struct sal_socket *));
  236. if (sockets == RT_NULL)
  237. goto __result; /* return st->max_socket */
  238. /* clean the new allocated fds */
  239. for (index = st->max_socket; index < cnt; index++)
  240. {
  241. sockets[index] = RT_NULL;
  242. }
  243. st->sockets = sockets;
  244. st->max_socket = cnt;
  245. }
  246. /* allocate 'struct sal_socket' */
  247. if (idx < (int) st->max_socket && st->sockets[idx] == RT_NULL)
  248. {
  249. st->sockets[idx] = rt_malloc(sizeof(struct sal_socket));
  250. if (st->sockets[idx] == RT_NULL)
  251. {
  252. idx = st->max_socket;
  253. }
  254. }
  255. __result:
  256. return idx;
  257. }
  258. /**
  259. * this function will return a empty sal socket structure address
  260. *
  261. * @return sal socket structure address
  262. */
  263. static int socket_new(void)
  264. {
  265. struct sal_socket *sock;
  266. struct sal_socket_table *st = &socket_table;
  267. int idx;
  268. sal_lock();
  269. /* find an empty sal socket entry */
  270. idx = socket_alloc(st, 0);
  271. /* can't find an empty sal socket entry */
  272. if (idx == (int) st->max_socket)
  273. {
  274. idx = -(1 + SAL_SOCKET_OFFSET);
  275. goto __result;
  276. }
  277. sock = st->sockets[idx];
  278. sock->socket = idx + SAL_SOCKET_OFFSET;
  279. sock->magic = SAL_SOCKET_MAGIC;
  280. __result:
  281. sal_unlock();
  282. return idx + SAL_SOCKET_OFFSET;
  283. }
  284. int sal_accept(int socket, struct sockaddr *addr, socklen_t *addrlen)
  285. {
  286. int new_socket;
  287. struct sal_socket *sock;
  288. sock = sal_get_socket(socket);
  289. if (!sock)
  290. {
  291. return -1;
  292. }
  293. if (sock->ops->accept == RT_NULL)
  294. {
  295. return -RT_ENOSYS;
  296. }
  297. new_socket = sock->ops->accept((int) sock->user_data, addr, addrlen);
  298. if (new_socket != -1)
  299. {
  300. int retval;
  301. int new_socket;
  302. struct sal_socket *new_sock;
  303. /* allocate a new socket structure and registered socket options */
  304. new_socket = socket_new();
  305. if (new_socket < 0)
  306. {
  307. return -1;
  308. }
  309. new_sock = sal_get_socket(new_socket);
  310. retval = socket_init(sock->domain, sock->type, sock->protocol, &new_sock);
  311. if (retval < 0)
  312. {
  313. LOG_E("New socket registered failed, return error %d.", retval);
  314. return -1;
  315. }
  316. /* socket struct user_data used to store the acquired new socket */
  317. new_sock->user_data = (void *) new_socket;
  318. return new_sock->socket;
  319. }
  320. return -1;
  321. }
  322. int sal_bind(int socket, const struct sockaddr *name, socklen_t namelen)
  323. {
  324. struct sal_socket *sock;
  325. sock = sal_get_socket(socket);
  326. if (!sock)
  327. {
  328. return -1;
  329. }
  330. if (sock->ops->bind == RT_NULL)
  331. {
  332. return -RT_ENOSYS;
  333. }
  334. return sock->ops->bind((int) sock->user_data, name, namelen);
  335. }
  336. int sal_shutdown(int socket, int how)
  337. {
  338. struct sal_socket *sock;
  339. sock = sal_get_socket(socket);
  340. if (!sock)
  341. {
  342. return -1;
  343. }
  344. if (sock->ops->shutdown == RT_NULL)
  345. {
  346. return -RT_ENOSYS;
  347. }
  348. if (sock->ops->shutdown((int) sock->user_data, how) == 0)
  349. {
  350. memset(sock, 0x00, sizeof(struct sal_socket));
  351. return 0;
  352. }
  353. return -1;
  354. }
  355. int sal_getpeername(int socket, struct sockaddr *name, socklen_t *namelen)
  356. {
  357. struct sal_socket *sock;
  358. sock = sal_get_socket(socket);
  359. if (!sock)
  360. {
  361. return -1;
  362. }
  363. if (sock->ops->getpeername == RT_NULL)
  364. {
  365. return -RT_ENOSYS;
  366. }
  367. return sock->ops->getpeername((int) sock->user_data, name, namelen);
  368. }
  369. int sal_getsockname(int socket, struct sockaddr *name, socklen_t *namelen)
  370. {
  371. struct sal_socket *sock;
  372. sock = sal_get_socket(socket);
  373. if (!sock)
  374. {
  375. return -1;
  376. }
  377. if (sock->ops->getsockname == RT_NULL)
  378. {
  379. return -RT_ENOSYS;
  380. }
  381. return sock->ops->getsockname((int) sock->user_data, name, namelen);
  382. }
  383. int sal_getsockopt(int socket, int level, int optname, void *optval, socklen_t *optlen)
  384. {
  385. struct sal_socket *sock;
  386. sock = sal_get_socket(socket);
  387. if (!sock)
  388. {
  389. return -1;
  390. }
  391. if (sock->ops->getsockopt == RT_NULL)
  392. {
  393. return -RT_ENOSYS;
  394. }
  395. return sock->ops->getsockopt((int) sock->user_data, level, optname, optval, optlen);
  396. }
  397. int sal_setsockopt(int socket, int level, int optname, const void *optval, socklen_t optlen)
  398. {
  399. struct sal_socket *sock;
  400. sock = sal_get_socket(socket);
  401. if (!sock)
  402. {
  403. return -1;
  404. }
  405. if (sock->ops->setsockopt == RT_NULL)
  406. {
  407. return -RT_ENOSYS;
  408. }
  409. return sock->ops->setsockopt((int) sock->user_data, level, optname, optval, optlen);
  410. }
  411. int sal_connect(int socket, const struct sockaddr *name, socklen_t namelen)
  412. {
  413. struct sal_socket *sock;
  414. sock = sal_get_socket(socket);
  415. if (!sock)
  416. {
  417. return -1;
  418. }
  419. if (sock->ops->connect == RT_NULL)
  420. {
  421. return -RT_ENOSYS;
  422. }
  423. return sock->ops->connect((int) sock->user_data, name, namelen);
  424. }
  425. int sal_listen(int socket, int backlog)
  426. {
  427. struct sal_socket *sock;
  428. sock = sal_get_socket(socket);
  429. if (!sock)
  430. {
  431. return -1;
  432. }
  433. if (sock->ops->listen == RT_NULL)
  434. {
  435. return -RT_ENOSYS;
  436. }
  437. return sock->ops->listen((int) sock->user_data, backlog);
  438. }
  439. int sal_recvfrom(int socket, void *mem, size_t len, int flags,
  440. struct sockaddr *from, socklen_t *fromlen)
  441. {
  442. struct sal_socket *sock;
  443. sock = sal_get_socket(socket);
  444. if (!sock)
  445. {
  446. return -1;
  447. }
  448. if (sock->ops->recvfrom == RT_NULL)
  449. {
  450. return -RT_ENOSYS;
  451. }
  452. return sock->ops->recvfrom((int) sock->user_data, mem, len, flags, from, fromlen);
  453. }
  454. int sal_sendto(int socket, const void *dataptr, size_t size, int flags,
  455. const struct sockaddr *to, socklen_t tolen)
  456. {
  457. struct sal_socket *sock;
  458. sock = sal_get_socket(socket);
  459. if (!sock)
  460. {
  461. return -1;
  462. }
  463. if (sock->ops->sendto == RT_NULL)
  464. {
  465. return -RT_ENOSYS;
  466. }
  467. return sock->ops->sendto((int) sock->user_data, dataptr, size, flags, to, tolen);
  468. }
  469. int sal_socket(int domain, int type, int protocol)
  470. {
  471. int retval;
  472. int socket, proto_socket;
  473. struct sal_socket *sock;
  474. /* allocate a new socket and registered socket options */
  475. socket = socket_new();
  476. if (socket < 0)
  477. {
  478. return -1;
  479. }
  480. sock = sal_get_socket(socket);
  481. retval = socket_init(domain, type, protocol, &sock);
  482. if (retval < 0)
  483. {
  484. LOG_E("SAL socket protocol family input failed, return error %d.", retval);
  485. return -1;
  486. }
  487. if (sock->ops->socket == RT_NULL)
  488. {
  489. return -RT_ENOSYS;
  490. }
  491. proto_socket = sock->ops->socket(domain, type, protocol);
  492. if (proto_socket >= 0)
  493. {
  494. sock->user_data = (void *) proto_socket;
  495. return sock->socket;
  496. }
  497. return -1;
  498. }
  499. int sal_closesocket(int socket)
  500. {
  501. struct sal_socket *sock;
  502. sock = sal_get_socket(socket);
  503. if (!sock)
  504. {
  505. return -1;
  506. }
  507. if (sock->ops->closesocket == RT_NULL)
  508. {
  509. return -RT_ENOSYS;
  510. }
  511. if (sock->ops->closesocket((int) sock->user_data) == 0)
  512. {
  513. memset(sock, 0x00, sizeof(struct sal_socket));
  514. return 0;
  515. }
  516. return -1;
  517. }
  518. int sal_ioctlsocket(int socket, long cmd, void *arg)
  519. {
  520. struct sal_socket *sock;
  521. sock = sal_get_socket(socket);
  522. if (!sock)
  523. {
  524. return -1;
  525. }
  526. if (sock->ops->ioctlsocket == RT_NULL)
  527. {
  528. return -RT_ENOSYS;
  529. }
  530. return sock->ops->ioctlsocket((int) sock->user_data, cmd, arg);
  531. }
  532. int sal_poll(struct dfs_fd *file, struct rt_pollreq *req)
  533. {
  534. struct sal_socket *sock;
  535. int socket = (int) file->data;
  536. sock = sal_get_socket(socket);
  537. if (!sock)
  538. {
  539. return -1;
  540. }
  541. if (sock->ops->poll == RT_NULL)
  542. {
  543. return -RT_ENOSYS;
  544. }
  545. return sock->ops->poll(file, req);
  546. }
  547. struct hostent *sal_gethostbyname(const char *name)
  548. {
  549. int i;
  550. for (i = 0; i < SAL_PROTO_FAMILIES_NUM; ++i)
  551. {
  552. if (proto_families[i].gethostbyname)
  553. {
  554. return proto_families[i].gethostbyname(name);
  555. }
  556. }
  557. return RT_NULL;
  558. }
  559. int sal_gethostbyname_r(const char *name, struct hostent *ret, char *buf,
  560. size_t buflen, struct hostent **result, int *h_errnop)
  561. {
  562. int i;
  563. for (i = 0; i < SAL_PROTO_FAMILIES_NUM; ++i)
  564. {
  565. if (proto_families[i].gethostbyname_r)
  566. {
  567. return proto_families[i].gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
  568. }
  569. }
  570. return -1;
  571. }
  572. void sal_freeaddrinfo(struct addrinfo *ai)
  573. {
  574. int i;
  575. for (i = 0; i < SAL_PROTO_FAMILIES_NUM; ++i)
  576. {
  577. if (proto_families[i].freeaddrinfo)
  578. {
  579. proto_families[i].freeaddrinfo(ai);
  580. return;
  581. }
  582. }
  583. }
  584. int sal_getaddrinfo(const char *nodename,
  585. const char *servname,
  586. const struct addrinfo *hints,
  587. struct addrinfo **res)
  588. {
  589. int i;
  590. for (i = 0; i < SAL_PROTO_FAMILIES_NUM; ++i)
  591. {
  592. if (proto_families[i].getaddrinfo)
  593. {
  594. return proto_families[i].getaddrinfo(nodename, servname, hints, res);
  595. }
  596. }
  597. return -1;
  598. }