sal_socket.c 30 KB

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