sal_socket.c 22 KB

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