sal_socket.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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. * 2018-11-12 ChenYong Add TLS support
  10. */
  11. #include <rtthread.h>
  12. #include <rthw.h>
  13. #include <sal_socket.h>
  14. #include <sal_netdb.h>
  15. #ifdef SAL_USING_TLS
  16. #include <sal_tls.h>
  17. #endif
  18. #include <sal.h>
  19. #define DBG_ENABLE
  20. #define DBG_SECTION_NAME "SAL_SOC"
  21. #define DBG_LEVEL DBG_INFO
  22. #define DBG_COLOR
  23. #include <rtdbg.h>
  24. #define SOCKET_TABLE_STEP_LEN 4
  25. /* the socket table used to dynamic allocate sockets */
  26. struct sal_socket_table
  27. {
  28. uint32_t max_socket;
  29. struct sal_socket **sockets;
  30. };
  31. #ifdef SAL_USING_TLS
  32. /* The global TLS protocol options */
  33. static struct sal_proto_tls *proto_tls;
  34. #endif
  35. /* The global array of available protocol families */
  36. static struct sal_proto_family proto_families[SAL_PROTO_FAMILIES_NUM];
  37. /* The global socket table */
  38. static struct sal_socket_table socket_table;
  39. static struct rt_mutex sal_core_lock;
  40. static rt_bool_t init_ok = RT_FALSE;
  41. #define IS_SOCKET_PROTO_TLS(sock) (((sock)->protocol == PROTOCOL_TLS) || \
  42. ((sock)->protocol == PROTOCOL_DTLS))
  43. #define SAL_SOCKOPS_PROTO_TLS_VALID(sock, name) (proto_tls && (proto_tls->ops->name) && IS_SOCKET_PROTO_TLS(sock))
  44. #define SAL_SOCKOPT_PROTO_TLS_EXEC(sock, name, optval, optlen) \
  45. do \
  46. { \
  47. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, name)) \
  48. { \
  49. return proto_tls->ops->name((sock)->user_data_tls, (optval), (optlen)); \
  50. } \
  51. }while(0) \
  52. /**
  53. * SAL (Socket Abstraction Layer) initialize.
  54. *
  55. * @return result 0: initialize success
  56. * -1: initialize failed
  57. */
  58. int sal_init(void)
  59. {
  60. int cn;
  61. if (init_ok)
  62. {
  63. LOG_D("Socket Abstraction Layer is already initialized.");
  64. return 0;
  65. }
  66. /* init sal socket table */
  67. cn = SOCKET_TABLE_STEP_LEN < SAL_SOCKETS_NUM ? SOCKET_TABLE_STEP_LEN : SAL_SOCKETS_NUM;
  68. socket_table.max_socket = cn;
  69. socket_table.sockets = rt_calloc(1, cn * sizeof(struct sal_socket *));
  70. if (socket_table.sockets == RT_NULL)
  71. {
  72. LOG_E("No memory for socket table.\n");
  73. return -1;
  74. }
  75. /* create sal socket lock */
  76. rt_mutex_init(&sal_core_lock, "sal_lock", RT_IPC_FLAG_FIFO);
  77. LOG_I("Socket Abstraction Layer initialize success.");
  78. init_ok = RT_TRUE;
  79. return 0;
  80. }
  81. INIT_COMPONENT_EXPORT(sal_init);
  82. /**
  83. * This function will register TLS protocol to the global TLS protocol.
  84. *
  85. * @param pt TLS protocol object
  86. *
  87. * @return 0: TLS protocol object register success
  88. */
  89. #ifdef SAL_USING_TLS
  90. int sal_proto_tls_register(const struct sal_proto_tls *pt)
  91. {
  92. RT_ASSERT(pt);
  93. proto_tls = (struct sal_proto_tls *) pt;
  94. return 0;
  95. }
  96. #endif
  97. /**
  98. * This function will register protocol family to the global array of protocol families.
  99. *
  100. * @param pf protocol family object
  101. *
  102. * @return 0: protocol family object register success
  103. * -1: the global array of available protocol families is full
  104. */
  105. int sal_proto_family_register(const struct sal_proto_family *pf)
  106. {
  107. rt_base_t level;
  108. int idx;
  109. /* disable interrupt */
  110. level = rt_hw_interrupt_disable();
  111. /* check protocol family is already registered */
  112. for(idx = 0; idx < SAL_PROTO_FAMILIES_NUM; idx++)
  113. {
  114. if (proto_families[idx].family == pf->family && proto_families[idx].create)
  115. {
  116. /* enable interrupt */
  117. rt_hw_interrupt_enable(level);
  118. LOG_E("%s protocol family is already registered!", pf->family);
  119. return -1;
  120. }
  121. }
  122. /* find an empty protocol family entry */
  123. for(idx = 0; idx < SAL_PROTO_FAMILIES_NUM && proto_families[idx].create; idx++);
  124. /* can't find an empty protocol family entry */
  125. if (idx == SAL_PROTO_FAMILIES_NUM)
  126. {
  127. /* enable interrupt */
  128. rt_hw_interrupt_enable(level);
  129. return -1;
  130. }
  131. proto_families[idx].family = pf->family;
  132. proto_families[idx].sec_family = pf->sec_family;
  133. proto_families[idx].create = pf->create;
  134. proto_families[idx].ops = pf->ops;
  135. proto_families[idx].ops->gethostbyname = pf->ops->gethostbyname;
  136. proto_families[idx].ops->gethostbyname_r = pf->ops->gethostbyname_r;
  137. proto_families[idx].ops->freeaddrinfo = pf->ops->freeaddrinfo;
  138. proto_families[idx].ops->getaddrinfo = pf->ops->getaddrinfo;
  139. /* enable interrupt */
  140. rt_hw_interrupt_enable(level);
  141. return 0;
  142. }
  143. /**
  144. * This function removes a previously registered protocol family object.
  145. *
  146. * @param pf protocol family object
  147. *
  148. * @return >=0 : unregister protocol family index
  149. * -1 : unregister failed
  150. */
  151. int sal_proto_family_unregister(int family)
  152. {
  153. int idx = 0;
  154. RT_ASSERT(family > 0 && family < AF_MAX);
  155. for(idx = 0; idx < SAL_PROTO_FAMILIES_NUM; idx++)
  156. {
  157. if (proto_families[idx].family == family && proto_families[idx].create)
  158. {
  159. rt_memset(&proto_families[idx], 0x00, sizeof(struct sal_proto_family));
  160. return idx;
  161. }
  162. }
  163. return -1;
  164. }
  165. /**
  166. * This function will judge whether protocol family is registered
  167. *
  168. * @param family protocol family number
  169. *
  170. * @return 1: protocol family is registered
  171. * 0: protocol family is not registered
  172. */
  173. rt_bool_t sal_proto_family_is_registered(int family)
  174. {
  175. int idx = 0;
  176. RT_ASSERT(family > 0 && family < AF_MAX);
  177. for (idx = 0; idx < SAL_PROTO_FAMILIES_NUM; idx++)
  178. {
  179. if (proto_families[idx].family == family && proto_families[idx].create)
  180. {
  181. return RT_TRUE;
  182. }
  183. }
  184. return RT_FALSE;
  185. }
  186. /**
  187. * This function will get protocol family object by family number.
  188. *
  189. * @param family protocol family number
  190. *
  191. * @return protocol family object
  192. */
  193. struct sal_proto_family *sal_proto_family_find(int family)
  194. {
  195. int idx = 0;
  196. RT_ASSERT(family > 0 && family < AF_MAX);
  197. for (idx = 0; idx < SAL_PROTO_FAMILIES_NUM; idx++)
  198. {
  199. if (proto_families[idx].family == family && proto_families[idx].create)
  200. {
  201. return &proto_families[idx];
  202. }
  203. }
  204. return RT_NULL;
  205. }
  206. /**
  207. * This function will get sal socket object by sal socket descriptor.
  208. *
  209. * @param socket sal socket index
  210. *
  211. * @return sal socket object of the current sal socket index
  212. */
  213. struct sal_socket *sal_get_socket(int socket)
  214. {
  215. struct sal_socket_table *st = &socket_table;
  216. if (socket < 0 || socket >= (int) st->max_socket)
  217. {
  218. return RT_NULL;
  219. }
  220. socket = socket - SAL_SOCKET_OFFSET;
  221. /* check socket structure valid or not */
  222. if (st->sockets[socket]->magic != SAL_SOCKET_MAGIC)
  223. {
  224. return RT_NULL;
  225. }
  226. return st->sockets[socket];
  227. }
  228. /**
  229. * This function will lock sal socket.
  230. *
  231. * @note please don't invoke it on ISR.
  232. */
  233. static void sal_lock(void)
  234. {
  235. rt_err_t result;
  236. result = rt_mutex_take(&sal_core_lock, RT_WAITING_FOREVER);
  237. if (result != RT_EOK)
  238. {
  239. RT_ASSERT(0);
  240. }
  241. }
  242. /**
  243. * This function will lock sal socket.
  244. *
  245. * @note please don't invoke it on ISR.
  246. */
  247. static void sal_unlock(void)
  248. {
  249. rt_mutex_release(&sal_core_lock);
  250. }
  251. /**
  252. * This function will get protocol family structure by family type
  253. *
  254. * @param family protocol family
  255. *
  256. * @return protocol family structure address
  257. */
  258. static struct sal_proto_family *get_proto_family(int family)
  259. {
  260. int idx;
  261. for (idx = 0; idx < SAL_PROTO_FAMILIES_NUM; idx++)
  262. {
  263. if (proto_families[idx].family == family && proto_families[idx].create)
  264. {
  265. return &proto_families[idx];
  266. }
  267. }
  268. /* compare the secondary protocol families when primary protocol families find failed */
  269. for (idx = 0; idx < SAL_PROTO_FAMILIES_NUM; idx++)
  270. {
  271. if (proto_families[idx].sec_family == family && proto_families[idx].create)
  272. {
  273. return &proto_families[idx];
  274. }
  275. }
  276. return RT_NULL;
  277. }
  278. /**
  279. * This function will initialize sal socket object and set socket options
  280. *
  281. * @param family protocol family
  282. * @param type socket type
  283. * @param protocol transfer Protocol
  284. * @param res sal socket object address
  285. *
  286. * @return 0 : socket initialize success
  287. * -1 : input the wrong family
  288. * -2 : input the wrong socket type
  289. * -3 : get protocol family object failed
  290. * -4 : set socket options failed
  291. */
  292. static int socket_init(int family, int type, int protocol, struct sal_socket **res)
  293. {
  294. struct sal_socket *sock;
  295. struct sal_proto_family *pf;
  296. if (family < 0 || family > AF_MAX)
  297. {
  298. return -1;
  299. }
  300. if (type < 0 || type > SOCK_MAX)
  301. {
  302. return -2;
  303. }
  304. sock = *res;
  305. sock->domain = family;
  306. sock->type = type;
  307. sock->protocol = protocol;
  308. /* get socket protocol family object */
  309. if ((pf = get_proto_family(family)) == RT_NULL)
  310. {
  311. return -3;
  312. }
  313. /* registered the current socket options */
  314. if (pf->create(sock, type, protocol) != 0)
  315. {
  316. return -4;
  317. }
  318. return 0;
  319. }
  320. static int socket_alloc(struct sal_socket_table *st, int f_socket)
  321. {
  322. int idx;
  323. /* find an empty socket entry */
  324. for (idx = f_socket; idx < (int) st->max_socket; idx++)
  325. {
  326. if (st->sockets[idx] == RT_NULL)
  327. break;
  328. if (st->sockets[idx]->ops == RT_NULL)
  329. break;
  330. }
  331. /* allocate a larger sockte container */
  332. if (idx == (int) st->max_socket && st->max_socket < SAL_SOCKETS_NUM)
  333. {
  334. int cnt, index;
  335. struct sal_socket **sockets;
  336. /* increase the number of socket with 4 step length */
  337. cnt = st->max_socket + SOCKET_TABLE_STEP_LEN;
  338. cnt = cnt > SAL_SOCKETS_NUM ? SAL_SOCKETS_NUM : cnt;
  339. sockets = rt_realloc(st->sockets, cnt * sizeof(struct sal_socket *));
  340. if (sockets == RT_NULL)
  341. goto __result; /* return st->max_socket */
  342. /* clean the new allocated fds */
  343. for (index = st->max_socket; index < cnt; index++)
  344. {
  345. sockets[index] = RT_NULL;
  346. }
  347. st->sockets = sockets;
  348. st->max_socket = cnt;
  349. }
  350. /* allocate 'struct sal_socket' */
  351. if (idx < (int) st->max_socket && st->sockets[idx] == RT_NULL)
  352. {
  353. st->sockets[idx] = rt_calloc(1, sizeof(struct sal_socket));
  354. if (st->sockets[idx] == RT_NULL)
  355. {
  356. idx = st->max_socket;
  357. }
  358. }
  359. __result:
  360. return idx;
  361. }
  362. static int socket_new(void)
  363. {
  364. struct sal_socket *sock;
  365. struct sal_socket_table *st = &socket_table;
  366. int idx;
  367. sal_lock();
  368. /* find an empty sal socket entry */
  369. idx = socket_alloc(st, 0);
  370. /* can't find an empty sal socket entry */
  371. if (idx == (int) st->max_socket)
  372. {
  373. idx = -(1 + SAL_SOCKET_OFFSET);
  374. goto __result;
  375. }
  376. sock = st->sockets[idx];
  377. sock->socket = idx + SAL_SOCKET_OFFSET;
  378. sock->magic = SAL_SOCKET_MAGIC;
  379. sock->ops = RT_NULL;
  380. sock->user_data = RT_NULL;
  381. #ifdef SAL_USING_TLS
  382. sock->user_data_tls = RT_NULL;
  383. #endif
  384. __result:
  385. sal_unlock();
  386. return idx + SAL_SOCKET_OFFSET;
  387. }
  388. int sal_accept(int socket, struct sockaddr *addr, socklen_t *addrlen)
  389. {
  390. int new_socket;
  391. struct sal_socket *sock;
  392. sock = sal_get_socket(socket);
  393. if (!sock)
  394. {
  395. return -1;
  396. }
  397. if (sock->ops->accept == RT_NULL)
  398. {
  399. return -RT_ENOSYS;
  400. }
  401. new_socket = sock->ops->accept((int) sock->user_data, addr, addrlen);
  402. if (new_socket != -1)
  403. {
  404. int retval;
  405. int new_sal_socket;
  406. struct sal_socket *new_sock;
  407. /* allocate a new socket structure and registered socket options */
  408. new_sal_socket = socket_new();
  409. if (new_sal_socket < 0)
  410. {
  411. sock->ops->closesocket(new_socket);
  412. return -1;
  413. }
  414. new_sock = sal_get_socket(new_sal_socket);
  415. retval = socket_init(sock->domain, sock->type, sock->protocol, &new_sock);
  416. if (retval < 0)
  417. {
  418. sock->ops->closesocket(new_socket);
  419. rt_memset(new_sock, 0x00, sizeof(struct sal_socket));
  420. LOG_E("New socket registered failed, return error %d.", retval);
  421. return -1;
  422. }
  423. /* socket struct user_data used to store the acquired new socket */
  424. new_sock->user_data = (void *) new_socket;
  425. return new_sal_socket;
  426. }
  427. return -1;
  428. }
  429. int sal_bind(int socket, const struct sockaddr *name, socklen_t namelen)
  430. {
  431. struct sal_socket *sock;
  432. sock = sal_get_socket(socket);
  433. if (!sock)
  434. {
  435. return -1;
  436. }
  437. if (sock->ops->bind == RT_NULL)
  438. {
  439. return -RT_ENOSYS;
  440. }
  441. return sock->ops->bind((int) sock->user_data, name, namelen);
  442. }
  443. int sal_shutdown(int socket, int how)
  444. {
  445. struct sal_socket *sock;
  446. sock = sal_get_socket(socket);
  447. if (!sock)
  448. {
  449. return -1;
  450. }
  451. if (sock->ops->shutdown == RT_NULL)
  452. {
  453. return -RT_ENOSYS;
  454. }
  455. if (sock->ops->shutdown((int) sock->user_data, how) == 0)
  456. {
  457. #ifdef SAL_USING_TLS
  458. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, closesocket))
  459. {
  460. if (proto_tls->ops->closesocket(sock->user_data_tls) < 0)
  461. {
  462. return -1;
  463. }
  464. }
  465. #endif
  466. rt_free(sock);
  467. socket_table.sockets[socket] = RT_NULL;
  468. return 0;
  469. }
  470. return -1;
  471. }
  472. int sal_getpeername(int socket, struct sockaddr *name, socklen_t *namelen)
  473. {
  474. struct sal_socket *sock;
  475. sock = sal_get_socket(socket);
  476. if (!sock)
  477. {
  478. return -1;
  479. }
  480. if (sock->ops->getpeername == RT_NULL)
  481. {
  482. return -RT_ENOSYS;
  483. }
  484. return sock->ops->getpeername((int) sock->user_data, name, namelen);
  485. }
  486. int sal_getsockname(int socket, struct sockaddr *name, socklen_t *namelen)
  487. {
  488. struct sal_socket *sock;
  489. sock = sal_get_socket(socket);
  490. if (!sock)
  491. {
  492. return -1;
  493. }
  494. if (sock->ops->getsockname == RT_NULL)
  495. {
  496. return -RT_ENOSYS;
  497. }
  498. return sock->ops->getsockname((int) sock->user_data, name, namelen);
  499. }
  500. int sal_getsockopt(int socket, int level, int optname, void *optval, socklen_t *optlen)
  501. {
  502. struct sal_socket *sock;
  503. sock = sal_get_socket(socket);
  504. if (!sock)
  505. {
  506. return -1;
  507. }
  508. if (sock->ops->getsockopt == RT_NULL)
  509. {
  510. return -RT_ENOSYS;
  511. }
  512. return sock->ops->getsockopt((int) sock->user_data, level, optname, optval, optlen);
  513. }
  514. int sal_setsockopt(int socket, int level, int optname, const void *optval, socklen_t optlen)
  515. {
  516. struct sal_socket *sock;
  517. sock = sal_get_socket(socket);
  518. if (!sock)
  519. {
  520. return -1;
  521. }
  522. if (sock->ops->setsockopt == RT_NULL)
  523. {
  524. return -RT_ENOSYS;
  525. }
  526. #ifdef SAL_USING_TLS
  527. if (level == SOL_TLS)
  528. {
  529. switch (optname)
  530. {
  531. case TLS_CRET_LIST:
  532. SAL_SOCKOPT_PROTO_TLS_EXEC(sock, set_cret_list, optval, optlen);
  533. break;
  534. case TLS_CIPHERSUITE_LIST:
  535. SAL_SOCKOPT_PROTO_TLS_EXEC(sock, set_ciphersurite, optval, optlen);
  536. break;
  537. case TLS_PEER_VERIFY:
  538. SAL_SOCKOPT_PROTO_TLS_EXEC(sock, set_peer_verify, optval, optlen);
  539. break;
  540. case TLS_DTLS_ROLE:
  541. SAL_SOCKOPT_PROTO_TLS_EXEC(sock, set_dtls_role, optval, optlen);
  542. break;
  543. default:
  544. return -1;
  545. }
  546. return 0;
  547. }
  548. else
  549. {
  550. return sock->ops->setsockopt((int) sock->user_data, level, optname, optval, optlen);
  551. }
  552. #else
  553. return sock->ops->setsockopt((int) sock->user_data, level, optname, optval, optlen);
  554. #endif /* SAL_USING_TLS */
  555. }
  556. int sal_connect(int socket, const struct sockaddr *name, socklen_t namelen)
  557. {
  558. struct sal_socket *sock;
  559. int ret;
  560. sock = sal_get_socket(socket);
  561. if (!sock)
  562. {
  563. return -1;
  564. }
  565. if (sock->ops->connect == RT_NULL)
  566. {
  567. return -RT_ENOSYS;
  568. }
  569. ret = sock->ops->connect((int) sock->user_data, name, namelen);
  570. #ifdef SAL_USING_TLS
  571. if (ret >= 0 && SAL_SOCKOPS_PROTO_TLS_VALID(sock, connect))
  572. {
  573. if (proto_tls->ops->connect(sock->user_data_tls) < 0)
  574. {
  575. return -1;
  576. }
  577. return ret;
  578. }
  579. #endif
  580. return ret;
  581. }
  582. int sal_listen(int socket, int backlog)
  583. {
  584. struct sal_socket *sock;
  585. sock = sal_get_socket(socket);
  586. if (!sock)
  587. {
  588. return -1;
  589. }
  590. if (sock->ops->listen == RT_NULL)
  591. {
  592. return -RT_ENOSYS;
  593. }
  594. return sock->ops->listen((int) sock->user_data, backlog);
  595. }
  596. int sal_recvfrom(int socket, void *mem, size_t len, int flags,
  597. struct sockaddr *from, socklen_t *fromlen)
  598. {
  599. struct sal_socket *sock;
  600. sock = sal_get_socket(socket);
  601. if (!sock)
  602. {
  603. return -1;
  604. }
  605. if (sock->ops->recvfrom == RT_NULL)
  606. {
  607. return -RT_ENOSYS;
  608. }
  609. #ifdef SAL_USING_TLS
  610. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, recv))
  611. {
  612. int ret;
  613. if ((ret = proto_tls->ops->recv(sock->user_data_tls, mem, len)) < 0)
  614. {
  615. return -1;
  616. }
  617. return ret;
  618. }
  619. else
  620. {
  621. return sock->ops->recvfrom((int) sock->user_data, mem, len, flags, from, fromlen);
  622. }
  623. #else
  624. return sock->ops->recvfrom((int) sock->user_data, mem, len, flags, from, fromlen);
  625. #endif
  626. }
  627. int sal_sendto(int socket, const void *dataptr, size_t size, int flags,
  628. const struct sockaddr *to, socklen_t tolen)
  629. {
  630. struct sal_socket *sock;
  631. sock = sal_get_socket(socket);
  632. if (!sock)
  633. {
  634. return -1;
  635. }
  636. if (sock->ops->sendto == RT_NULL)
  637. {
  638. return -RT_ENOSYS;
  639. }
  640. #ifdef SAL_USING_TLS
  641. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, send))
  642. {
  643. int ret;
  644. if ((ret = proto_tls->ops->send(sock->user_data_tls, dataptr, size)) < 0)
  645. {
  646. return -1;
  647. }
  648. return ret;
  649. }
  650. else
  651. {
  652. return sock->ops->sendto((int) sock->user_data, dataptr, size, flags, to, tolen);
  653. }
  654. #else
  655. return sock->ops->sendto((int) sock->user_data, dataptr, size, flags, to, tolen);
  656. #endif
  657. }
  658. int sal_socket(int domain, int type, int protocol)
  659. {
  660. int retval;
  661. int socket, proto_socket;
  662. struct sal_socket *sock;
  663. /* allocate a new socket and registered socket options */
  664. socket = socket_new();
  665. if (socket < 0)
  666. {
  667. return -1;
  668. }
  669. sock = sal_get_socket(socket);
  670. retval = socket_init(domain, type, protocol, &sock);
  671. if (retval < 0)
  672. {
  673. LOG_E("SAL socket protocol family input failed, return error %d.", retval);
  674. return -1;
  675. }
  676. if (sock->ops->socket == RT_NULL)
  677. {
  678. return -RT_ENOSYS;
  679. }
  680. proto_socket = sock->ops->socket(domain, type, protocol);
  681. if (proto_socket >= 0)
  682. {
  683. #ifdef SAL_USING_TLS
  684. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, socket))
  685. {
  686. sock->user_data_tls = proto_tls->ops->socket(proto_socket);
  687. if (sock->user_data_tls == RT_NULL)
  688. {
  689. return -1;
  690. }
  691. }
  692. #endif
  693. sock->user_data = (void *) proto_socket;
  694. return sock->socket;
  695. }
  696. return -1;
  697. }
  698. int sal_closesocket(int socket)
  699. {
  700. struct sal_socket *sock;
  701. sock = sal_get_socket(socket);
  702. if (!sock)
  703. {
  704. return -1;
  705. }
  706. if (sock->ops->closesocket == RT_NULL)
  707. {
  708. return -RT_ENOSYS;
  709. }
  710. if (sock->ops->closesocket((int) sock->user_data) == 0)
  711. {
  712. #ifdef SAL_USING_TLS
  713. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, closesocket))
  714. {
  715. if (proto_tls->ops->closesocket(sock->user_data_tls) < 0)
  716. {
  717. return -1;
  718. }
  719. }
  720. #endif
  721. rt_free(sock);
  722. socket_table.sockets[socket] = RT_NULL;
  723. return 0;
  724. }
  725. return -1;
  726. }
  727. int sal_ioctlsocket(int socket, long cmd, void *arg)
  728. {
  729. struct sal_socket *sock;
  730. sock = sal_get_socket(socket);
  731. if (!sock)
  732. {
  733. return -1;
  734. }
  735. if (sock->ops->ioctlsocket == RT_NULL)
  736. {
  737. return -RT_ENOSYS;
  738. }
  739. return sock->ops->ioctlsocket((int) sock->user_data, cmd, arg);
  740. }
  741. #ifdef SAL_USING_POSIX
  742. int sal_poll(struct dfs_fd *file, struct rt_pollreq *req)
  743. {
  744. struct sal_socket *sock;
  745. int socket = (int) file->data;
  746. sock = sal_get_socket(socket);
  747. if (!sock)
  748. {
  749. return -1;
  750. }
  751. if (sock->ops->poll == RT_NULL)
  752. {
  753. return -RT_ENOSYS;
  754. }
  755. return sock->ops->poll(file, req);
  756. }
  757. #endif
  758. struct hostent *sal_gethostbyname(const char *name)
  759. {
  760. int i;
  761. struct hostent *hst;
  762. for (i = 0; i < SAL_PROTO_FAMILIES_NUM; ++i)
  763. {
  764. if (proto_families[i].ops && proto_families[i].ops->gethostbyname)
  765. {
  766. hst = proto_families[i].ops->gethostbyname(name);
  767. if (hst != RT_NULL)
  768. {
  769. return hst;
  770. }
  771. }
  772. }
  773. return RT_NULL;
  774. }
  775. int sal_gethostbyname_r(const char *name, struct hostent *ret, char *buf,
  776. size_t buflen, struct hostent **result, int *h_errnop)
  777. {
  778. int i, res;
  779. for (i = 0; i < SAL_PROTO_FAMILIES_NUM; ++i)
  780. {
  781. if (proto_families[i].ops && proto_families[i].ops->gethostbyname_r)
  782. {
  783. res = proto_families[i].ops->gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
  784. if (res == 0)
  785. {
  786. return res;
  787. }
  788. }
  789. }
  790. return -1;
  791. }
  792. int sal_getaddrinfo(const char *nodename,
  793. const char *servname,
  794. const struct addrinfo *hints,
  795. struct addrinfo **res)
  796. {
  797. int i, ret;
  798. for (i = 0; i < SAL_PROTO_FAMILIES_NUM; ++i)
  799. {
  800. if (proto_families[i].ops && proto_families[i].ops->getaddrinfo)
  801. {
  802. ret = proto_families[i].ops->getaddrinfo(nodename, servname, hints, res);
  803. if (ret == 0)
  804. {
  805. return ret;
  806. }
  807. }
  808. }
  809. return -1;
  810. }
  811. void sal_freeaddrinfo(struct addrinfo *ai)
  812. {
  813. int i;
  814. for (i = 0; i < SAL_PROTO_FAMILIES_NUM; ++i)
  815. {
  816. if (proto_families[i].ops && proto_families[i].ops->freeaddrinfo)
  817. {
  818. proto_families[i].ops->freeaddrinfo(ai);
  819. return;
  820. }
  821. }
  822. }