sal_socket.c 31 KB

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