sal_socket.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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) initialize.
  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. rt_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 protocol family to the global array of protocol families.
  70. *
  71. * @param pf protocol family object
  72. *
  73. * @return >=0 : protocol family object 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. /* check protocol family is already registered */
  83. for(idx = 0; idx < SAL_PROTO_FAMILIES_NUM; idx++)
  84. {
  85. if(rt_strcmp(proto_families[idx].name, pf->name) == 0)
  86. {
  87. /* enable interrupt */
  88. rt_hw_interrupt_enable(level);
  89. LOG_E("%s protocol family is already registered!", pf->name);
  90. return -1;
  91. }
  92. }
  93. /* find an empty protocol family entry */
  94. for(idx = 0; idx < SAL_PROTO_FAMILIES_NUM && proto_families[idx].create; idx++);
  95. /* can't find an empty protocol family entry */
  96. if(idx == SAL_PROTO_FAMILIES_NUM)
  97. {
  98. /* enable interrupt */
  99. rt_hw_interrupt_enable(level);
  100. return -1;
  101. }
  102. rt_strncpy(proto_families[idx].name, pf->name, rt_strlen(pf->name));
  103. proto_families[idx].family = pf->family;
  104. proto_families[idx].sec_family = pf->sec_family;
  105. proto_families[idx].create = pf->create;
  106. proto_families[idx].gethostbyname = pf->gethostbyname;
  107. proto_families[idx].gethostbyname_r = pf->gethostbyname_r;
  108. proto_families[idx].freeaddrinfo = pf->freeaddrinfo;
  109. proto_families[idx].getaddrinfo = pf->getaddrinfo;
  110. /* enable interrupt */
  111. rt_hw_interrupt_enable(level);
  112. return 0;
  113. }
  114. /**
  115. * This function removes a previously registered protocol family object.
  116. *
  117. * @param pf protocol family object
  118. *
  119. * @return >=0 : unregister protocol family index
  120. * -1 : unregister failed
  121. */
  122. int sal_proto_family_unregister(const struct proto_family *pf)
  123. {
  124. int idx = 0;
  125. RT_ASSERT(pf != RT_NULL);
  126. for(idx = 0; idx < SAL_PROTO_FAMILIES_NUM; idx++)
  127. {
  128. if(rt_strcmp(proto_families[idx].name, pf->name) == 0)
  129. {
  130. rt_memset(&proto_families[idx], 0x00, sizeof(struct proto_family));
  131. return idx;
  132. }
  133. }
  134. return -1;
  135. }
  136. /**
  137. * This function will get protocol family by name.
  138. *
  139. * @param name protocol family name
  140. *
  141. * @return protocol family object
  142. */
  143. struct proto_family *sal_proto_family_find(const char *name)
  144. {
  145. int idx = 0;
  146. RT_ASSERT(name != RT_NULL);
  147. for (idx = 0; idx < SAL_PROTO_FAMILIES_NUM; idx++)
  148. {
  149. if (rt_strcmp(proto_families[idx].name, name) == 0)
  150. {
  151. return &proto_families[idx];
  152. }
  153. }
  154. return RT_NULL;
  155. }
  156. /**
  157. * This function will get sal socket object by sal socket descriptor.
  158. *
  159. * @param socket sal socket index
  160. *
  161. * @return sal socket object of the current sal socket index
  162. */
  163. struct sal_socket *sal_get_socket(int socket)
  164. {
  165. struct sal_socket_table *st = &socket_table;
  166. if (socket < 0 || socket >= (int) st->max_socket)
  167. {
  168. return RT_NULL;
  169. }
  170. socket = socket - SAL_SOCKET_OFFSET;
  171. /* check socket structure valid or not */
  172. if (st->sockets[socket]->magic != SAL_SOCKET_MAGIC)
  173. {
  174. return RT_NULL;
  175. }
  176. return st->sockets[socket];
  177. }
  178. /**
  179. * This function will lock sal socket.
  180. *
  181. * @note please don't invoke it on ISR.
  182. */
  183. static void sal_lock(void)
  184. {
  185. rt_err_t result;
  186. result = rt_mutex_take(&sal_core_lock, RT_WAITING_FOREVER);
  187. if (result != RT_EOK)
  188. {
  189. RT_ASSERT(0);
  190. }
  191. }
  192. /**
  193. * This function will lock sal socket.
  194. *
  195. * @note please don't invoke it on ISR.
  196. */
  197. static void sal_unlock(void)
  198. {
  199. rt_mutex_release(&sal_core_lock);
  200. }
  201. /**
  202. * This function will get protocol family structure by family type
  203. *
  204. * @param family protocol family
  205. *
  206. * @return protocol family structure address
  207. */
  208. static struct proto_family *get_proto_family(int family)
  209. {
  210. int idx;
  211. for (idx = 0; idx < SAL_PROTO_FAMILIES_NUM; idx++)
  212. {
  213. if (proto_families[idx].family == family && proto_families[idx].create)
  214. {
  215. return &proto_families[idx];
  216. }
  217. }
  218. /* compare the secondary protocol families when primary protocol families find failed */
  219. for (idx = 0; idx < SAL_PROTO_FAMILIES_NUM; idx++)
  220. {
  221. if (proto_families[idx].sec_family == family && proto_families[idx].create)
  222. {
  223. return &proto_families[idx];
  224. }
  225. }
  226. return RT_NULL;
  227. }
  228. /**
  229. * This function will initialize sal socket object and set socket options
  230. *
  231. * @param family protocol family
  232. * @param type socket type
  233. * @param protocol transfer Protocol
  234. * @param res sal socket object address
  235. *
  236. * @return 0 : socket initialize success
  237. * -1 : input the wrong family
  238. * -2 : input the wrong socket type
  239. * -3 : get protocol family object failed
  240. * -4 : set socket options failed
  241. */
  242. static int socket_init(int family, int type, int protocol, struct sal_socket **res)
  243. {
  244. struct sal_socket *sock;
  245. struct proto_family *pf;
  246. if (family < 0 || family > AF_MAX)
  247. {
  248. return -1;
  249. }
  250. if (type < 0 || type > SOCK_MAX)
  251. {
  252. return -2;
  253. }
  254. sock = *res;
  255. sock->domain = family;
  256. sock->type = type;
  257. sock->protocol = protocol;
  258. /* get socket protocol family object */
  259. if ((pf = get_proto_family(family)) == RT_NULL)
  260. {
  261. return -3;
  262. }
  263. /* registered the current socket options */
  264. if (pf->create(sock, type, protocol) != 0)
  265. {
  266. return -4;
  267. }
  268. return 0;
  269. }
  270. static int socket_alloc(struct sal_socket_table *st, int f_socket)
  271. {
  272. int idx;
  273. /* find an empty socket entry */
  274. for (idx = f_socket; idx < (int) st->max_socket; idx++)
  275. {
  276. if (st->sockets[idx] == RT_NULL)
  277. break;
  278. if (st->sockets[idx]->ops == RT_NULL)
  279. break;
  280. }
  281. /* allocate a larger sockte container */
  282. if (idx == (int) st->max_socket && st->max_socket < SAL_SOCKETS_NUM)
  283. {
  284. int cnt, index;
  285. struct sal_socket **sockets;
  286. /* increase the number of FD with 4 step length */
  287. cnt = st->max_socket + 4;
  288. cnt = cnt > SAL_SOCKETS_NUM ? SAL_SOCKETS_NUM : cnt;
  289. sockets = rt_realloc(st->sockets, cnt * sizeof(struct sal_socket *));
  290. if (sockets == RT_NULL)
  291. goto __result; /* return st->max_socket */
  292. /* clean the new allocated fds */
  293. for (index = st->max_socket; index < cnt; index++)
  294. {
  295. sockets[index] = RT_NULL;
  296. }
  297. st->sockets = sockets;
  298. st->max_socket = cnt;
  299. }
  300. /* allocate 'struct sal_socket' */
  301. if (idx < (int) st->max_socket && st->sockets[idx] == RT_NULL)
  302. {
  303. st->sockets[idx] = rt_malloc(sizeof(struct sal_socket));
  304. if (st->sockets[idx] == RT_NULL)
  305. {
  306. idx = st->max_socket;
  307. }
  308. }
  309. __result:
  310. return idx;
  311. }
  312. static int socket_new(void)
  313. {
  314. struct sal_socket *sock;
  315. struct sal_socket_table *st = &socket_table;
  316. int idx;
  317. sal_lock();
  318. /* find an empty sal socket entry */
  319. idx = socket_alloc(st, 0);
  320. /* can't find an empty sal socket entry */
  321. if (idx == (int) st->max_socket)
  322. {
  323. idx = -(1 + SAL_SOCKET_OFFSET);
  324. goto __result;
  325. }
  326. sock = st->sockets[idx];
  327. sock->socket = idx + SAL_SOCKET_OFFSET;
  328. sock->magic = SAL_SOCKET_MAGIC;
  329. __result:
  330. sal_unlock();
  331. return idx + SAL_SOCKET_OFFSET;
  332. }
  333. int sal_accept(int socket, struct sockaddr *addr, socklen_t *addrlen)
  334. {
  335. int new_socket;
  336. struct sal_socket *sock;
  337. sock = sal_get_socket(socket);
  338. if (!sock)
  339. {
  340. return -1;
  341. }
  342. if (sock->ops->accept == RT_NULL)
  343. {
  344. return -RT_ENOSYS;
  345. }
  346. new_socket = sock->ops->accept((int) sock->user_data, addr, addrlen);
  347. if (new_socket != -1)
  348. {
  349. int retval;
  350. int new_socket;
  351. struct sal_socket *new_sock;
  352. /* allocate a new socket structure and registered socket options */
  353. new_socket = socket_new();
  354. if (new_socket < 0)
  355. {
  356. return -1;
  357. }
  358. new_sock = sal_get_socket(new_socket);
  359. retval = socket_init(sock->domain, sock->type, sock->protocol, &new_sock);
  360. if (retval < 0)
  361. {
  362. LOG_E("New socket registered failed, return error %d.", retval);
  363. return -1;
  364. }
  365. /* socket struct user_data used to store the acquired new socket */
  366. new_sock->user_data = (void *) new_socket;
  367. return new_sock->socket;
  368. }
  369. return -1;
  370. }
  371. int sal_bind(int socket, const struct sockaddr *name, socklen_t namelen)
  372. {
  373. struct sal_socket *sock;
  374. sock = sal_get_socket(socket);
  375. if (!sock)
  376. {
  377. return -1;
  378. }
  379. if (sock->ops->bind == RT_NULL)
  380. {
  381. return -RT_ENOSYS;
  382. }
  383. return sock->ops->bind((int) sock->user_data, name, namelen);
  384. }
  385. int sal_shutdown(int socket, int how)
  386. {
  387. struct sal_socket *sock;
  388. sock = sal_get_socket(socket);
  389. if (!sock)
  390. {
  391. return -1;
  392. }
  393. if (sock->ops->shutdown == RT_NULL)
  394. {
  395. return -RT_ENOSYS;
  396. }
  397. if (sock->ops->shutdown((int) sock->user_data, how) == 0)
  398. {
  399. rt_memset(sock, 0x00, sizeof(struct sal_socket));
  400. return 0;
  401. }
  402. return -1;
  403. }
  404. int sal_getpeername(int socket, struct sockaddr *name, socklen_t *namelen)
  405. {
  406. struct sal_socket *sock;
  407. sock = sal_get_socket(socket);
  408. if (!sock)
  409. {
  410. return -1;
  411. }
  412. if (sock->ops->getpeername == RT_NULL)
  413. {
  414. return -RT_ENOSYS;
  415. }
  416. return sock->ops->getpeername((int) sock->user_data, name, namelen);
  417. }
  418. int sal_getsockname(int socket, struct sockaddr *name, socklen_t *namelen)
  419. {
  420. struct sal_socket *sock;
  421. sock = sal_get_socket(socket);
  422. if (!sock)
  423. {
  424. return -1;
  425. }
  426. if (sock->ops->getsockname == RT_NULL)
  427. {
  428. return -RT_ENOSYS;
  429. }
  430. return sock->ops->getsockname((int) sock->user_data, name, namelen);
  431. }
  432. int sal_getsockopt(int socket, int level, int optname, void *optval, socklen_t *optlen)
  433. {
  434. struct sal_socket *sock;
  435. sock = sal_get_socket(socket);
  436. if (!sock)
  437. {
  438. return -1;
  439. }
  440. if (sock->ops->getsockopt == RT_NULL)
  441. {
  442. return -RT_ENOSYS;
  443. }
  444. return sock->ops->getsockopt((int) sock->user_data, level, optname, optval, optlen);
  445. }
  446. int sal_setsockopt(int socket, int level, int optname, const void *optval, socklen_t optlen)
  447. {
  448. struct sal_socket *sock;
  449. sock = sal_get_socket(socket);
  450. if (!sock)
  451. {
  452. return -1;
  453. }
  454. if (sock->ops->setsockopt == RT_NULL)
  455. {
  456. return -RT_ENOSYS;
  457. }
  458. return sock->ops->setsockopt((int) sock->user_data, level, optname, optval, optlen);
  459. }
  460. int sal_connect(int socket, const struct sockaddr *name, socklen_t namelen)
  461. {
  462. struct sal_socket *sock;
  463. sock = sal_get_socket(socket);
  464. if (!sock)
  465. {
  466. return -1;
  467. }
  468. if (sock->ops->connect == RT_NULL)
  469. {
  470. return -RT_ENOSYS;
  471. }
  472. return sock->ops->connect((int) sock->user_data, name, namelen);
  473. }
  474. int sal_listen(int socket, int backlog)
  475. {
  476. struct sal_socket *sock;
  477. sock = sal_get_socket(socket);
  478. if (!sock)
  479. {
  480. return -1;
  481. }
  482. if (sock->ops->listen == RT_NULL)
  483. {
  484. return -RT_ENOSYS;
  485. }
  486. return sock->ops->listen((int) sock->user_data, backlog);
  487. }
  488. int sal_recvfrom(int socket, void *mem, size_t len, int flags,
  489. struct sockaddr *from, socklen_t *fromlen)
  490. {
  491. struct sal_socket *sock;
  492. sock = sal_get_socket(socket);
  493. if (!sock)
  494. {
  495. return -1;
  496. }
  497. if (sock->ops->recvfrom == RT_NULL)
  498. {
  499. return -RT_ENOSYS;
  500. }
  501. return sock->ops->recvfrom((int) sock->user_data, mem, len, flags, from, fromlen);
  502. }
  503. int sal_sendto(int socket, const void *dataptr, size_t size, int flags,
  504. const struct sockaddr *to, socklen_t tolen)
  505. {
  506. struct sal_socket *sock;
  507. sock = sal_get_socket(socket);
  508. if (!sock)
  509. {
  510. return -1;
  511. }
  512. if (sock->ops->sendto == RT_NULL)
  513. {
  514. return -RT_ENOSYS;
  515. }
  516. return sock->ops->sendto((int) sock->user_data, dataptr, size, flags, to, tolen);
  517. }
  518. int sal_socket(int domain, int type, int protocol)
  519. {
  520. int retval;
  521. int socket, proto_socket;
  522. struct sal_socket *sock;
  523. /* allocate a new socket and registered socket options */
  524. socket = socket_new();
  525. if (socket < 0)
  526. {
  527. return -1;
  528. }
  529. sock = sal_get_socket(socket);
  530. retval = socket_init(domain, type, protocol, &sock);
  531. if (retval < 0)
  532. {
  533. LOG_E("SAL socket protocol family input failed, return error %d.", retval);
  534. return -1;
  535. }
  536. if (sock->ops->socket == RT_NULL)
  537. {
  538. return -RT_ENOSYS;
  539. }
  540. proto_socket = sock->ops->socket(domain, type, protocol);
  541. if (proto_socket >= 0)
  542. {
  543. sock->user_data = (void *) proto_socket;
  544. return sock->socket;
  545. }
  546. return -1;
  547. }
  548. int sal_closesocket(int socket)
  549. {
  550. struct sal_socket *sock;
  551. sock = sal_get_socket(socket);
  552. if (!sock)
  553. {
  554. return -1;
  555. }
  556. if (sock->ops->closesocket == RT_NULL)
  557. {
  558. return -RT_ENOSYS;
  559. }
  560. if (sock->ops->closesocket((int) sock->user_data) == 0)
  561. {
  562. rt_memset(sock, 0x00, sizeof(struct sal_socket));
  563. return 0;
  564. }
  565. return -1;
  566. }
  567. int sal_ioctlsocket(int socket, long cmd, void *arg)
  568. {
  569. struct sal_socket *sock;
  570. sock = sal_get_socket(socket);
  571. if (!sock)
  572. {
  573. return -1;
  574. }
  575. if (sock->ops->ioctlsocket == RT_NULL)
  576. {
  577. return -RT_ENOSYS;
  578. }
  579. return sock->ops->ioctlsocket((int) sock->user_data, cmd, arg);
  580. }
  581. #ifdef SAL_USING_POSIX
  582. int sal_poll(struct dfs_fd *file, struct rt_pollreq *req)
  583. {
  584. struct sal_socket *sock;
  585. int socket = (int) file->data;
  586. sock = sal_get_socket(socket);
  587. if (!sock)
  588. {
  589. return -1;
  590. }
  591. if (sock->ops->poll == RT_NULL)
  592. {
  593. return -RT_ENOSYS;
  594. }
  595. return sock->ops->poll(file, req);
  596. }
  597. #endif
  598. struct hostent *sal_gethostbyname(const char *name)
  599. {
  600. int i;
  601. struct hostent *hst;
  602. for (i = 0; i < SAL_PROTO_FAMILIES_NUM; ++i)
  603. {
  604. if (proto_families[i].gethostbyname)
  605. {
  606. hst = proto_families[i].gethostbyname(name);
  607. if (hst != RT_NULL)
  608. {
  609. return hst;
  610. }
  611. }
  612. }
  613. return RT_NULL;
  614. }
  615. int sal_gethostbyname_r(const char *name, struct hostent *ret, char *buf,
  616. size_t buflen, struct hostent **result, int *h_errnop)
  617. {
  618. int i, res;
  619. for (i = 0; i < SAL_PROTO_FAMILIES_NUM; ++i)
  620. {
  621. if (proto_families[i].gethostbyname_r)
  622. {
  623. res = proto_families[i].gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
  624. if (res == 0)
  625. {
  626. return res;
  627. }
  628. }
  629. }
  630. return -1;
  631. }
  632. void sal_freeaddrinfo(struct addrinfo *ai)
  633. {
  634. int i;
  635. for (i = 0; i < SAL_PROTO_FAMILIES_NUM; ++i)
  636. {
  637. if (proto_families[i].freeaddrinfo)
  638. {
  639. proto_families[i].freeaddrinfo(ai);
  640. return;
  641. }
  642. }
  643. }
  644. int sal_getaddrinfo(const char *nodename,
  645. const char *servname,
  646. const struct addrinfo *hints,
  647. struct addrinfo **res)
  648. {
  649. int i, ret;
  650. for (i = 0; i < SAL_PROTO_FAMILIES_NUM; ++i)
  651. {
  652. if (proto_families[i].getaddrinfo)
  653. {
  654. ret = proto_families[i].getaddrinfo(nodename, servname, hints, res);
  655. if (ret == 0)
  656. {
  657. return ret;
  658. }
  659. }
  660. }
  661. return -1;
  662. }