sal_socket.c 17 KB

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