sal_socket.c 32 KB

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