sal_socket.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  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_UP(netdev) \
  61. do { \
  62. if (!netdev_is_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) && \
  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 = -1, 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 clean the netdev.
  297. *
  298. * @note please don't invoke it on ISR.
  299. */
  300. int sal_netdev_cleanup(struct netdev *netdev)
  301. {
  302. int idx = 0, find_dev;
  303. do
  304. {
  305. find_dev = 0;
  306. sal_lock();
  307. for (idx = 0; idx < socket_table.max_socket; idx++)
  308. {
  309. if (socket_table.sockets[idx] && socket_table.sockets[idx]->netdev == netdev)
  310. {
  311. find_dev = 1;
  312. break;
  313. }
  314. }
  315. sal_unlock();
  316. if (find_dev)
  317. {
  318. rt_thread_mdelay(rt_tick_from_millisecond(100));
  319. }
  320. } while (find_dev);
  321. return 0;
  322. }
  323. /**
  324. * This function will initialize sal socket object and set socket options
  325. *
  326. * @param family protocol family
  327. * @param type socket type
  328. * @param protocol transfer Protocol
  329. * @param res sal socket object address
  330. *
  331. * @return 0 : socket initialize success
  332. * -1 : input the wrong family
  333. * -2 : input the wrong socket type
  334. * -3 : get network interface failed
  335. */
  336. static int socket_init(int family, int type, int protocol, struct sal_socket **res)
  337. {
  338. struct sal_socket *sock;
  339. struct sal_proto_family *pf;
  340. struct netdev *netdv_def = netdev_default;
  341. struct netdev *netdev = RT_NULL;
  342. rt_bool_t flag = RT_FALSE;
  343. if (family < 0 || family > AF_MAX)
  344. {
  345. return -1;
  346. }
  347. if (type < 0 || type > SOCK_MAX)
  348. {
  349. return -2;
  350. }
  351. sock = *res;
  352. sock->domain = family;
  353. sock->type = type;
  354. sock->protocol = protocol;
  355. if (netdv_def && netdev_is_up(netdv_def))
  356. {
  357. /* check default network interface device protocol family */
  358. pf = (struct sal_proto_family *) netdv_def->sal_user_data;
  359. if (pf != RT_NULL && pf->skt_ops && (pf->family == family || pf->sec_family == family))
  360. {
  361. sock->netdev = netdv_def;
  362. flag = RT_TRUE;
  363. }
  364. }
  365. if (flag == RT_FALSE)
  366. {
  367. /* get network interface device by protocol family */
  368. netdev = netdev_get_by_family(family);
  369. if (netdev == RT_NULL)
  370. {
  371. LOG_E("not find network interface device by protocol family(%d).", family);
  372. return -3;
  373. }
  374. sock->netdev = netdev;
  375. }
  376. return 0;
  377. }
  378. static int socket_alloc(struct sal_socket_table *st, int f_socket)
  379. {
  380. int idx;
  381. /* find an empty socket entry */
  382. for (idx = f_socket; idx < (int) st->max_socket; idx++)
  383. {
  384. if (st->sockets[idx] == RT_NULL ||
  385. st->sockets[idx]->netdev == RT_NULL)
  386. {
  387. break;
  388. }
  389. }
  390. /* allocate a larger sockte container */
  391. if (idx == (int) st->max_socket && st->max_socket < SAL_SOCKETS_NUM)
  392. {
  393. int cnt, index;
  394. struct sal_socket **sockets;
  395. /* increase the number of socket with 4 step length */
  396. cnt = st->max_socket + SOCKET_TABLE_STEP_LEN;
  397. cnt = cnt > SAL_SOCKETS_NUM ? SAL_SOCKETS_NUM : cnt;
  398. sockets = rt_realloc(st->sockets, cnt * sizeof(struct sal_socket *));
  399. if (sockets == RT_NULL)
  400. goto __result; /* return st->max_socket */
  401. /* clean the new allocated fds */
  402. for (index = st->max_socket; index < cnt; index++)
  403. {
  404. sockets[index] = RT_NULL;
  405. }
  406. st->sockets = sockets;
  407. st->max_socket = cnt;
  408. }
  409. /* allocate 'struct sal_socket' */
  410. if (idx < (int) st->max_socket && st->sockets[idx] == RT_NULL)
  411. {
  412. st->sockets[idx] = rt_calloc(1, sizeof(struct sal_socket));
  413. if (st->sockets[idx] == RT_NULL)
  414. {
  415. idx = st->max_socket;
  416. }
  417. }
  418. __result:
  419. return idx;
  420. }
  421. static int socket_new(void)
  422. {
  423. struct sal_socket *sock;
  424. struct sal_socket_table *st = &socket_table;
  425. int idx;
  426. sal_lock();
  427. /* find an empty sal socket entry */
  428. idx = socket_alloc(st, 0);
  429. /* can't find an empty sal socket entry */
  430. if (idx == (int) st->max_socket)
  431. {
  432. idx = -(1 + SAL_SOCKET_OFFSET);
  433. goto __result;
  434. }
  435. sock = st->sockets[idx];
  436. sock->socket = idx + SAL_SOCKET_OFFSET;
  437. sock->magic = SAL_SOCKET_MAGIC;
  438. sock->netdev = RT_NULL;
  439. sock->user_data = RT_NULL;
  440. #ifdef SAL_USING_TLS
  441. sock->user_data_tls = RT_NULL;
  442. #endif
  443. __result:
  444. sal_unlock();
  445. return idx + SAL_SOCKET_OFFSET;
  446. }
  447. int sal_accept(int socket, struct sockaddr *addr, socklen_t *addrlen)
  448. {
  449. int new_socket;
  450. struct sal_socket *sock;
  451. struct sal_proto_family *pf;
  452. /* get the socket object by socket descriptor */
  453. SAL_SOCKET_OBJ_GET(sock, socket);
  454. /* check the network interface socket operations */
  455. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, accept);
  456. new_socket = pf->skt_ops->accept((int) sock->user_data, addr, addrlen);
  457. if (new_socket != -1)
  458. {
  459. int retval;
  460. int new_sal_socket;
  461. struct sal_socket *new_sock;
  462. /* allocate a new socket structure and registered socket options */
  463. new_sal_socket = socket_new();
  464. if (new_sal_socket < 0)
  465. {
  466. pf->skt_ops->closesocket(new_socket);
  467. return -1;
  468. }
  469. new_sock = sal_get_socket(new_sal_socket);
  470. retval = socket_init(sock->domain, sock->type, sock->protocol, &new_sock);
  471. if (retval < 0)
  472. {
  473. pf->skt_ops->closesocket(new_socket);
  474. rt_memset(new_sock, 0x00, sizeof(struct sal_socket));
  475. LOG_E("New socket registered failed, return error %d.", retval);
  476. return -1;
  477. }
  478. /* socket structure user_data used to store the acquired new socket */
  479. new_sock->user_data = (void *) new_socket;
  480. return new_sal_socket;
  481. }
  482. return -1;
  483. }
  484. static void sal_sockaddr_to_ipaddr(const struct sockaddr *name, ip_addr_t *local_ipaddr)
  485. {
  486. const struct sockaddr_in *svr_addr = (const struct sockaddr_in *) name;
  487. #if NETDEV_IPV4 && NETDEV_IPV6
  488. local_ipaddr->u_addr.ip4.addr = svr_addr->sin_addr.s_addr;
  489. local_ipaddr->type = IPADDR_TYPE_V4;
  490. #elif NETDEV_IPV4
  491. local_ipaddr->addr = svr_addr->sin_addr.s_addr;
  492. #elif NETDEV_IPV6
  493. #error "not only support IPV6"
  494. #endif /* NETDEV_IPV4 && NETDEV_IPV6*/
  495. }
  496. int sal_bind(int socket, const struct sockaddr *name, socklen_t namelen)
  497. {
  498. struct sal_socket *sock;
  499. struct sal_proto_family *pf;
  500. ip_addr_t input_ipaddr;
  501. RT_ASSERT(name);
  502. /* get the socket object by socket descriptor */
  503. SAL_SOCKET_OBJ_GET(sock, socket);
  504. /* bind network interface by ip address */
  505. sal_sockaddr_to_ipaddr(name, &input_ipaddr);
  506. /* check input ipaddr is default netdev ipaddr */
  507. if (!ip_addr_isany_val(input_ipaddr))
  508. {
  509. struct sal_proto_family *input_pf = RT_NULL, *local_pf = RT_NULL;
  510. struct netdev *new_netdev = RT_NULL;
  511. new_netdev = netdev_get_by_ipaddr(&input_ipaddr);
  512. if (new_netdev == RT_NULL)
  513. {
  514. return -1;
  515. }
  516. /* get input and local ip address proto_family */
  517. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, local_pf, bind);
  518. SAL_NETDEV_SOCKETOPS_VALID(new_netdev, input_pf, bind);
  519. /* check the network interface protocol family type */
  520. if (input_pf->family != local_pf->family)
  521. {
  522. int new_socket = -1;
  523. /* protocol family is different, close old socket and create new socket by input ip address */
  524. local_pf->skt_ops->closesocket(socket);
  525. new_socket = input_pf->skt_ops->socket(input_pf->family, sock->type, sock->protocol);
  526. if (new_socket < 0)
  527. {
  528. return -1;
  529. }
  530. sock->netdev = new_netdev;
  531. sock->user_data = (void *) new_socket;
  532. }
  533. }
  534. /* check and get protocol families by the network interface device */
  535. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, bind);
  536. return pf->skt_ops->bind((int) sock->user_data, name, namelen);
  537. }
  538. int sal_shutdown(int socket, int how)
  539. {
  540. struct sal_socket *sock;
  541. struct sal_proto_family *pf;
  542. int error = 0;
  543. /* get the socket object by socket descriptor */
  544. SAL_SOCKET_OBJ_GET(sock, socket);
  545. /* shutdown operation not nead to check network interface status */
  546. /* check the network interface socket opreation */
  547. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, shutdown);
  548. if (pf->skt_ops->shutdown((int) sock->user_data, how) == 0)
  549. {
  550. #ifdef SAL_USING_TLS
  551. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, closesocket))
  552. {
  553. if (proto_tls->ops->closesocket(sock->user_data_tls) < 0)
  554. {
  555. return -1;
  556. }
  557. }
  558. #endif
  559. error = 0;
  560. }
  561. else
  562. {
  563. error = -1;
  564. }
  565. /* free socket */
  566. rt_free(sock);
  567. socket_table.sockets[socket] = RT_NULL;
  568. return error;
  569. }
  570. int sal_getpeername(int socket, struct sockaddr *name, socklen_t *namelen)
  571. {
  572. struct sal_socket *sock;
  573. struct sal_proto_family *pf;
  574. /* get the socket object by socket descriptor */
  575. SAL_SOCKET_OBJ_GET(sock, socket);
  576. /* check the network interface socket opreation */
  577. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, getpeername);
  578. return pf->skt_ops->getpeername((int) sock->user_data, name, namelen);
  579. }
  580. int sal_getsockname(int socket, struct sockaddr *name, socklen_t *namelen)
  581. {
  582. struct sal_socket *sock;
  583. struct sal_proto_family *pf;
  584. /* get socket object by socket descriptor */
  585. SAL_SOCKET_OBJ_GET(sock, socket);
  586. /* check the network interface socket opreation */
  587. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, getsockname);
  588. return pf->skt_ops->getsockname((int) sock->user_data, name, namelen);
  589. }
  590. int sal_getsockopt(int socket, int level, int optname, void *optval, socklen_t *optlen)
  591. {
  592. struct sal_socket *sock;
  593. struct sal_proto_family *pf;
  594. /* get the socket object by socket descriptor */
  595. SAL_SOCKET_OBJ_GET(sock, socket);
  596. /* check the network interface socket opreation */
  597. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, getsockopt);
  598. return pf->skt_ops->getsockopt((int) sock->user_data, level, optname, optval, optlen);
  599. }
  600. int sal_setsockopt(int socket, int level, int optname, const void *optval, socklen_t optlen)
  601. {
  602. struct sal_socket *sock;
  603. struct sal_proto_family *pf;
  604. /* get the socket object by socket descriptor */
  605. SAL_SOCKET_OBJ_GET(sock, socket);
  606. /* check the network interface socket opreation */
  607. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, setsockopt);
  608. #ifdef SAL_USING_TLS
  609. if (level == SOL_TLS)
  610. {
  611. switch (optname)
  612. {
  613. case TLS_CRET_LIST:
  614. SAL_SOCKOPT_PROTO_TLS_EXEC(sock, set_cret_list, optval, optlen);
  615. break;
  616. case TLS_CIPHERSUITE_LIST:
  617. SAL_SOCKOPT_PROTO_TLS_EXEC(sock, set_ciphersurite, optval, optlen);
  618. break;
  619. case TLS_PEER_VERIFY:
  620. SAL_SOCKOPT_PROTO_TLS_EXEC(sock, set_peer_verify, optval, optlen);
  621. break;
  622. case TLS_DTLS_ROLE:
  623. SAL_SOCKOPT_PROTO_TLS_EXEC(sock, set_dtls_role, optval, optlen);
  624. break;
  625. default:
  626. return -1;
  627. }
  628. return 0;
  629. }
  630. else
  631. {
  632. return pf->skt_ops->setsockopt((int) sock->user_data, level, optname, optval, optlen);
  633. }
  634. #else
  635. return pf->skt_ops->setsockopt((int) sock->user_data, level, optname, optval, optlen);
  636. #endif /* SAL_USING_TLS */
  637. }
  638. int sal_connect(int socket, const struct sockaddr *name, socklen_t namelen)
  639. {
  640. struct sal_socket *sock;
  641. struct sal_proto_family *pf;
  642. int ret;
  643. /* get the socket object by socket descriptor */
  644. SAL_SOCKET_OBJ_GET(sock, socket);
  645. /* check the network interface is up status */
  646. SAL_NETDEV_IS_UP(sock->netdev);
  647. /* check the network interface socket opreation */
  648. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, connect);
  649. ret = pf->skt_ops->connect((int) sock->user_data, name, namelen);
  650. #ifdef SAL_USING_TLS
  651. if (ret >= 0 && SAL_SOCKOPS_PROTO_TLS_VALID(sock, connect))
  652. {
  653. if (proto_tls->ops->connect(sock->user_data_tls) < 0)
  654. {
  655. return -1;
  656. }
  657. return ret;
  658. }
  659. #endif
  660. return ret;
  661. }
  662. int sal_listen(int socket, int backlog)
  663. {
  664. struct sal_socket *sock;
  665. struct sal_proto_family *pf;
  666. /* get the socket object by socket descriptor */
  667. SAL_SOCKET_OBJ_GET(sock, socket);
  668. /* check the network interface socket opreation */
  669. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, listen);
  670. return pf->skt_ops->listen((int) sock->user_data, backlog);
  671. }
  672. int sal_recvfrom(int socket, void *mem, size_t len, int flags,
  673. struct sockaddr *from, socklen_t *fromlen)
  674. {
  675. struct sal_socket *sock;
  676. struct sal_proto_family *pf;
  677. /* get the socket object by socket descriptor */
  678. SAL_SOCKET_OBJ_GET(sock, socket);
  679. /* check the network interface is up status */
  680. SAL_NETDEV_IS_UP(sock->netdev);
  681. /* check the network interface socket opreation */
  682. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, recvfrom);
  683. #ifdef SAL_USING_TLS
  684. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, recv))
  685. {
  686. int ret;
  687. if ((ret = proto_tls->ops->recv(sock->user_data_tls, mem, len)) < 0)
  688. {
  689. return -1;
  690. }
  691. return ret;
  692. }
  693. else
  694. {
  695. return pf->skt_ops->recvfrom((int) sock->user_data, mem, len, flags, from, fromlen);
  696. }
  697. #else
  698. return pf->skt_ops->recvfrom((int) sock->user_data, mem, len, flags, from, fromlen);
  699. #endif
  700. }
  701. int sal_sendto(int socket, const void *dataptr, size_t size, int flags,
  702. const struct sockaddr *to, socklen_t tolen)
  703. {
  704. struct sal_socket *sock;
  705. struct sal_proto_family *pf;
  706. /* get the socket object by socket descriptor */
  707. SAL_SOCKET_OBJ_GET(sock, socket);
  708. /* check the network interface is up status */
  709. SAL_NETDEV_IS_UP(sock->netdev);
  710. /* check the network interface socket opreation */
  711. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, sendto);
  712. #ifdef SAL_USING_TLS
  713. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, send))
  714. {
  715. int ret;
  716. if ((ret = proto_tls->ops->send(sock->user_data_tls, dataptr, size)) < 0)
  717. {
  718. return -1;
  719. }
  720. return ret;
  721. }
  722. else
  723. {
  724. return pf->skt_ops->sendto((int) sock->user_data, dataptr, size, flags, to, tolen);
  725. }
  726. #else
  727. return pf->skt_ops->sendto((int) sock->user_data, dataptr, size, flags, to, tolen);
  728. #endif
  729. }
  730. int sal_socket(int domain, int type, int protocol)
  731. {
  732. int retval;
  733. int socket, proto_socket;
  734. struct sal_socket *sock;
  735. struct sal_proto_family *pf;
  736. /* allocate a new socket and registered socket options */
  737. socket = socket_new();
  738. if (socket < 0)
  739. {
  740. return -1;
  741. }
  742. /* get sal socket object by socket descriptor */
  743. sock = sal_get_socket(socket);
  744. if (sock == RT_NULL)
  745. {
  746. return -1;
  747. }
  748. /* Initialize sal socket object */
  749. retval = socket_init(domain, type, protocol, &sock);
  750. if (retval < 0)
  751. {
  752. LOG_E("SAL socket protocol family input failed, return error %d.", retval);
  753. return -1;
  754. }
  755. /* valid the network interface socket opreation */
  756. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, socket);
  757. proto_socket = pf->skt_ops->socket(domain, type, protocol);
  758. if (proto_socket >= 0)
  759. {
  760. #ifdef SAL_USING_TLS
  761. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, socket))
  762. {
  763. sock->user_data_tls = proto_tls->ops->socket(socket);
  764. if (sock->user_data_tls == RT_NULL)
  765. {
  766. return -1;
  767. }
  768. }
  769. #endif
  770. sock->user_data = (void *) proto_socket;
  771. return sock->socket;
  772. }
  773. return -1;
  774. }
  775. int sal_closesocket(int socket)
  776. {
  777. struct sal_socket *sock;
  778. struct sal_proto_family *pf;
  779. int error = 0;
  780. /* get the socket object by socket descriptor */
  781. SAL_SOCKET_OBJ_GET(sock, socket);
  782. /* clsoesocket operation not nead to vaild network interface status */
  783. /* valid the network interface socket opreation */
  784. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, socket);
  785. if (pf->skt_ops->closesocket((int) sock->user_data) == 0)
  786. {
  787. #ifdef SAL_USING_TLS
  788. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, closesocket))
  789. {
  790. if (proto_tls->ops->closesocket(sock->user_data_tls) < 0)
  791. {
  792. return -1;
  793. }
  794. }
  795. #endif
  796. error = 0;
  797. }
  798. else
  799. {
  800. error = -1;
  801. }
  802. /* free socket */
  803. rt_free(sock);
  804. socket_table.sockets[socket] = RT_NULL;
  805. return error;
  806. }
  807. int sal_ioctlsocket(int socket, long cmd, void *arg)
  808. {
  809. struct sal_socket *sock;
  810. struct sal_proto_family *pf;
  811. /* get the socket object by socket descriptor */
  812. SAL_SOCKET_OBJ_GET(sock, socket);
  813. /* check the network interface socket opreation */
  814. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, ioctlsocket);
  815. return pf->skt_ops->ioctlsocket((int) sock->user_data, cmd, arg);
  816. }
  817. #ifdef SAL_USING_POSIX
  818. int sal_poll(struct dfs_fd *file, struct rt_pollreq *req)
  819. {
  820. struct sal_socket *sock;
  821. struct sal_proto_family *pf;
  822. int socket = (int) file->data;
  823. /* get the socket object by socket descriptor */
  824. SAL_SOCKET_OBJ_GET(sock, socket);
  825. /* check the network interface is up status */
  826. SAL_NETDEV_IS_UP(sock->netdev);
  827. /* check the network interface socket opreation */
  828. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, poll);
  829. return pf->skt_ops->poll(file, req);
  830. }
  831. #endif
  832. struct hostent *sal_gethostbyname(const char *name)
  833. {
  834. struct netdev *netdev = netdev_default;
  835. struct sal_proto_family *pf;
  836. if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, gethostbyname))
  837. {
  838. return pf->netdb_ops->gethostbyname(name);
  839. }
  840. else
  841. {
  842. /* get the first network interface device with up status */
  843. netdev = netdev_get_first_by_flags(NETDEV_FLAG_UP);
  844. if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, gethostbyname))
  845. {
  846. return pf->netdb_ops->gethostbyname(name);
  847. }
  848. }
  849. return RT_NULL;
  850. }
  851. int sal_gethostbyname_r(const char *name, struct hostent *ret, char *buf,
  852. size_t buflen, struct hostent **result, int *h_errnop)
  853. {
  854. struct netdev *netdev = netdev_default;
  855. struct sal_proto_family *pf;
  856. if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, gethostbyname_r))
  857. {
  858. return pf->netdb_ops->gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
  859. }
  860. else
  861. {
  862. /* get the first network interface device with up status */
  863. netdev = netdev_get_first_by_flags(NETDEV_FLAG_UP);
  864. if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, gethostbyname_r))
  865. {
  866. return pf->netdb_ops->gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
  867. }
  868. }
  869. return -1;
  870. }
  871. int sal_getaddrinfo(const char *nodename,
  872. const char *servname,
  873. const struct addrinfo *hints,
  874. struct addrinfo **res)
  875. {
  876. struct netdev *netdev = netdev_default;
  877. struct sal_proto_family *pf;
  878. if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, getaddrinfo))
  879. {
  880. return pf->netdb_ops->getaddrinfo(nodename, servname, hints, res);
  881. }
  882. else
  883. {
  884. /* get the first network interface device with up status */
  885. netdev = netdev_get_first_by_flags(NETDEV_FLAG_UP);
  886. if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, getaddrinfo))
  887. {
  888. return pf->netdb_ops->getaddrinfo(nodename, servname, hints, res);
  889. }
  890. }
  891. return -1;
  892. }
  893. void sal_freeaddrinfo(struct addrinfo *ai)
  894. {
  895. struct netdev *netdev = netdev_default;
  896. struct sal_proto_family *pf;
  897. if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, freeaddrinfo))
  898. {
  899. pf->netdb_ops->freeaddrinfo(ai);
  900. }
  901. else
  902. {
  903. /* get the first network interface device with up status */
  904. netdev = netdev_get_first_by_flags(NETDEV_FLAG_UP);
  905. if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, freeaddrinfo))
  906. {
  907. pf->netdb_ops->freeaddrinfo(ai);
  908. }
  909. }
  910. }