sal_socket.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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_low_level_set_internet_status(netdev, RT_TRUE);
  220. }
  221. else
  222. {
  223. LOG_D("Set network interface device(%s) internet status down.", netdev->name);
  224. netdev_low_level_set_internet_status(netdev, RT_FALSE);
  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. /* new socket create by accept should have the same netdev with server*/
  524. new_sock->netdev = sock->netdev;
  525. /* socket structure user_data used to store the acquired new socket */
  526. new_sock->user_data = (void *) new_socket;
  527. return new_sal_socket;
  528. }
  529. return -1;
  530. }
  531. static void sal_sockaddr_to_ipaddr(const struct sockaddr *name, ip_addr_t *local_ipaddr)
  532. {
  533. const struct sockaddr_in *svr_addr = (const struct sockaddr_in *) name;
  534. #if NETDEV_IPV4 && NETDEV_IPV6
  535. local_ipaddr->u_addr.ip4.addr = svr_addr->sin_addr.s_addr;
  536. local_ipaddr->type = IPADDR_TYPE_V4;
  537. #elif NETDEV_IPV4
  538. local_ipaddr->addr = svr_addr->sin_addr.s_addr;
  539. #elif NETDEV_IPV6
  540. #error "not only support IPV6"
  541. #endif /* NETDEV_IPV4 && NETDEV_IPV6*/
  542. }
  543. int sal_bind(int socket, const struct sockaddr *name, socklen_t namelen)
  544. {
  545. struct sal_socket *sock;
  546. struct sal_proto_family *pf;
  547. ip_addr_t input_ipaddr;
  548. RT_ASSERT(name);
  549. /* get the socket object by socket descriptor */
  550. SAL_SOCKET_OBJ_GET(sock, socket);
  551. /* bind network interface by ip address */
  552. sal_sockaddr_to_ipaddr(name, &input_ipaddr);
  553. /* check input ipaddr is default netdev ipaddr */
  554. if (!ip_addr_isany_val(input_ipaddr))
  555. {
  556. struct sal_proto_family *input_pf = RT_NULL, *local_pf = RT_NULL;
  557. struct netdev *new_netdev = RT_NULL;
  558. new_netdev = netdev_get_by_ipaddr(&input_ipaddr);
  559. if (new_netdev == RT_NULL)
  560. {
  561. return -1;
  562. }
  563. /* get input and local ip address proto_family */
  564. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, local_pf, bind);
  565. SAL_NETDEV_SOCKETOPS_VALID(new_netdev, input_pf, bind);
  566. /* check the network interface protocol family type */
  567. if (input_pf->family != local_pf->family)
  568. {
  569. int new_socket = -1;
  570. /* protocol family is different, close old socket and create new socket by input ip address */
  571. local_pf->skt_ops->closesocket(socket);
  572. new_socket = input_pf->skt_ops->socket(input_pf->family, sock->type, sock->protocol);
  573. if (new_socket < 0)
  574. {
  575. return -1;
  576. }
  577. sock->netdev = new_netdev;
  578. sock->user_data = (void *) new_socket;
  579. }
  580. }
  581. /* check and get protocol families by the network interface device */
  582. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, bind);
  583. return pf->skt_ops->bind((int) sock->user_data, name, namelen);
  584. }
  585. int sal_shutdown(int socket, int how)
  586. {
  587. struct sal_socket *sock;
  588. struct sal_proto_family *pf;
  589. int error = 0;
  590. /* get the socket object by socket descriptor */
  591. SAL_SOCKET_OBJ_GET(sock, socket);
  592. /* shutdown operation not need to check network interface status */
  593. /* check the network interface socket opreation */
  594. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, shutdown);
  595. if (pf->skt_ops->shutdown((int) sock->user_data, how) == 0)
  596. {
  597. #ifdef SAL_USING_TLS
  598. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, closesocket))
  599. {
  600. if (proto_tls->ops->closesocket(sock->user_data_tls) < 0)
  601. {
  602. return -1;
  603. }
  604. }
  605. #endif
  606. error = 0;
  607. }
  608. else
  609. {
  610. error = -1;
  611. }
  612. return error;
  613. }
  614. int sal_getpeername(int socket, struct sockaddr *name, socklen_t *namelen)
  615. {
  616. struct sal_socket *sock;
  617. struct sal_proto_family *pf;
  618. /* get the socket object by socket descriptor */
  619. SAL_SOCKET_OBJ_GET(sock, socket);
  620. /* check the network interface socket opreation */
  621. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, getpeername);
  622. return pf->skt_ops->getpeername((int) sock->user_data, name, namelen);
  623. }
  624. int sal_getsockname(int socket, struct sockaddr *name, socklen_t *namelen)
  625. {
  626. struct sal_socket *sock;
  627. struct sal_proto_family *pf;
  628. /* get socket object by socket descriptor */
  629. SAL_SOCKET_OBJ_GET(sock, socket);
  630. /* check the network interface socket opreation */
  631. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, getsockname);
  632. return pf->skt_ops->getsockname((int) sock->user_data, name, namelen);
  633. }
  634. int sal_getsockopt(int socket, int level, int optname, void *optval, socklen_t *optlen)
  635. {
  636. struct sal_socket *sock;
  637. struct sal_proto_family *pf;
  638. /* get the socket object by socket descriptor */
  639. SAL_SOCKET_OBJ_GET(sock, socket);
  640. /* check the network interface socket opreation */
  641. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, getsockopt);
  642. return pf->skt_ops->getsockopt((int) sock->user_data, level, optname, optval, optlen);
  643. }
  644. int sal_setsockopt(int socket, int level, int optname, const void *optval, socklen_t optlen)
  645. {
  646. struct sal_socket *sock;
  647. struct sal_proto_family *pf;
  648. /* get the socket object by socket descriptor */
  649. SAL_SOCKET_OBJ_GET(sock, socket);
  650. /* check the network interface socket opreation */
  651. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, setsockopt);
  652. #ifdef SAL_USING_TLS
  653. if (level == SOL_TLS)
  654. {
  655. switch (optname)
  656. {
  657. case TLS_CRET_LIST:
  658. SAL_SOCKOPT_PROTO_TLS_EXEC(sock, set_cret_list, optval, optlen);
  659. break;
  660. case TLS_CIPHERSUITE_LIST:
  661. SAL_SOCKOPT_PROTO_TLS_EXEC(sock, set_ciphersurite, optval, optlen);
  662. break;
  663. case TLS_PEER_VERIFY:
  664. SAL_SOCKOPT_PROTO_TLS_EXEC(sock, set_peer_verify, optval, optlen);
  665. break;
  666. case TLS_DTLS_ROLE:
  667. SAL_SOCKOPT_PROTO_TLS_EXEC(sock, set_dtls_role, optval, optlen);
  668. break;
  669. default:
  670. return -1;
  671. }
  672. return 0;
  673. }
  674. else
  675. {
  676. return pf->skt_ops->setsockopt((int) sock->user_data, level, optname, optval, optlen);
  677. }
  678. #else
  679. return pf->skt_ops->setsockopt((int) sock->user_data, level, optname, optval, optlen);
  680. #endif /* SAL_USING_TLS */
  681. }
  682. int sal_connect(int socket, const struct sockaddr *name, socklen_t namelen)
  683. {
  684. struct sal_socket *sock;
  685. struct sal_proto_family *pf;
  686. int ret;
  687. /* get the socket object by socket descriptor */
  688. SAL_SOCKET_OBJ_GET(sock, socket);
  689. /* check the network interface is up status */
  690. SAL_NETDEV_IS_UP(sock->netdev);
  691. /* check the network interface socket opreation */
  692. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, connect);
  693. ret = pf->skt_ops->connect((int) sock->user_data, name, namelen);
  694. #ifdef SAL_USING_TLS
  695. if (ret >= 0 && SAL_SOCKOPS_PROTO_TLS_VALID(sock, connect))
  696. {
  697. if (proto_tls->ops->connect(sock->user_data_tls) < 0)
  698. {
  699. return -1;
  700. }
  701. return ret;
  702. }
  703. #endif
  704. return ret;
  705. }
  706. int sal_listen(int socket, int backlog)
  707. {
  708. struct sal_socket *sock;
  709. struct sal_proto_family *pf;
  710. /* get the socket object by socket descriptor */
  711. SAL_SOCKET_OBJ_GET(sock, socket);
  712. /* check the network interface socket opreation */
  713. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, listen);
  714. return pf->skt_ops->listen((int) sock->user_data, backlog);
  715. }
  716. int sal_recvfrom(int socket, void *mem, size_t len, int flags,
  717. struct sockaddr *from, socklen_t *fromlen)
  718. {
  719. struct sal_socket *sock;
  720. struct sal_proto_family *pf;
  721. /* get the socket object by socket descriptor */
  722. SAL_SOCKET_OBJ_GET(sock, socket);
  723. /* check the network interface is up status */
  724. SAL_NETDEV_IS_UP(sock->netdev);
  725. /* check the network interface socket opreation */
  726. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, recvfrom);
  727. #ifdef SAL_USING_TLS
  728. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, recv))
  729. {
  730. int ret;
  731. if ((ret = proto_tls->ops->recv(sock->user_data_tls, mem, len)) < 0)
  732. {
  733. return -1;
  734. }
  735. return ret;
  736. }
  737. else
  738. {
  739. return pf->skt_ops->recvfrom((int) sock->user_data, mem, len, flags, from, fromlen);
  740. }
  741. #else
  742. return pf->skt_ops->recvfrom((int) sock->user_data, mem, len, flags, from, fromlen);
  743. #endif
  744. }
  745. int sal_sendto(int socket, const void *dataptr, size_t size, int flags,
  746. const struct sockaddr *to, socklen_t tolen)
  747. {
  748. struct sal_socket *sock;
  749. struct sal_proto_family *pf;
  750. /* get the socket object by socket descriptor */
  751. SAL_SOCKET_OBJ_GET(sock, socket);
  752. /* check the network interface is up status */
  753. SAL_NETDEV_IS_UP(sock->netdev);
  754. /* check the network interface socket opreation */
  755. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, sendto);
  756. #ifdef SAL_USING_TLS
  757. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, send))
  758. {
  759. int ret;
  760. if ((ret = proto_tls->ops->send(sock->user_data_tls, dataptr, size)) < 0)
  761. {
  762. return -1;
  763. }
  764. return ret;
  765. }
  766. else
  767. {
  768. return pf->skt_ops->sendto((int) sock->user_data, dataptr, size, flags, to, tolen);
  769. }
  770. #else
  771. return pf->skt_ops->sendto((int) sock->user_data, dataptr, size, flags, to, tolen);
  772. #endif
  773. }
  774. int sal_socket(int domain, int type, int protocol)
  775. {
  776. int retval;
  777. int socket, proto_socket;
  778. struct sal_socket *sock;
  779. struct sal_proto_family *pf;
  780. /* allocate a new socket and registered socket options */
  781. socket = socket_new();
  782. if (socket < 0)
  783. {
  784. return -1;
  785. }
  786. /* get sal socket object by socket descriptor */
  787. sock = sal_get_socket(socket);
  788. if (sock == RT_NULL)
  789. {
  790. socket_delete(socket);
  791. return -1;
  792. }
  793. /* Initialize sal socket object */
  794. retval = socket_init(domain, type, protocol, &sock);
  795. if (retval < 0)
  796. {
  797. LOG_E("SAL socket protocol family input failed, return error %d.", retval);
  798. socket_delete(socket);
  799. return -1;
  800. }
  801. /* valid the network interface socket opreation */
  802. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, socket);
  803. proto_socket = pf->skt_ops->socket(domain, type, protocol);
  804. if (proto_socket >= 0)
  805. {
  806. #ifdef SAL_USING_TLS
  807. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, socket))
  808. {
  809. sock->user_data_tls = proto_tls->ops->socket(socket);
  810. if (sock->user_data_tls == RT_NULL)
  811. {
  812. socket_delete(socket);
  813. return -1;
  814. }
  815. }
  816. #endif
  817. sock->user_data = (void *) proto_socket;
  818. return sock->socket;
  819. }
  820. socket_delete(socket);
  821. return -1;
  822. }
  823. int sal_closesocket(int socket)
  824. {
  825. struct sal_socket *sock;
  826. struct sal_proto_family *pf;
  827. int error = 0;
  828. /* get the socket object by socket descriptor */
  829. SAL_SOCKET_OBJ_GET(sock, socket);
  830. /* clsoesocket operation not need to vaild network interface status */
  831. /* valid the network interface socket opreation */
  832. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, closesocket);
  833. if (pf->skt_ops->closesocket((int) sock->user_data) == 0)
  834. {
  835. #ifdef SAL_USING_TLS
  836. if (SAL_SOCKOPS_PROTO_TLS_VALID(sock, closesocket))
  837. {
  838. if (proto_tls->ops->closesocket(sock->user_data_tls) < 0)
  839. {
  840. return -1;
  841. }
  842. }
  843. #endif
  844. error = 0;
  845. }
  846. else
  847. {
  848. error = -1;
  849. }
  850. /* delete socket */
  851. socket_delete(socket);
  852. return error;
  853. }
  854. int sal_ioctlsocket(int socket, long cmd, void *arg)
  855. {
  856. struct sal_socket *sock;
  857. struct sal_proto_family *pf;
  858. /* get the socket object by socket descriptor */
  859. SAL_SOCKET_OBJ_GET(sock, socket);
  860. /* check the network interface socket opreation */
  861. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, ioctlsocket);
  862. return pf->skt_ops->ioctlsocket((int) sock->user_data, cmd, arg);
  863. }
  864. #ifdef SAL_USING_POSIX
  865. int sal_poll(struct dfs_fd *file, struct rt_pollreq *req)
  866. {
  867. struct sal_socket *sock;
  868. struct sal_proto_family *pf;
  869. int socket = (int) file->data;
  870. /* get the socket object by socket descriptor */
  871. SAL_SOCKET_OBJ_GET(sock, socket);
  872. /* check the network interface is up status */
  873. SAL_NETDEV_IS_UP(sock->netdev);
  874. /* check the network interface socket opreation */
  875. SAL_NETDEV_SOCKETOPS_VALID(sock->netdev, pf, poll);
  876. return pf->skt_ops->poll(file, req);
  877. }
  878. #endif
  879. struct hostent *sal_gethostbyname(const char *name)
  880. {
  881. struct netdev *netdev = netdev_default;
  882. struct sal_proto_family *pf;
  883. if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, gethostbyname))
  884. {
  885. return pf->netdb_ops->gethostbyname(name);
  886. }
  887. else
  888. {
  889. /* get the first network interface device with up status */
  890. netdev = netdev_get_first_by_flags(NETDEV_FLAG_UP);
  891. if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, gethostbyname))
  892. {
  893. return pf->netdb_ops->gethostbyname(name);
  894. }
  895. }
  896. return RT_NULL;
  897. }
  898. int sal_gethostbyname_r(const char *name, struct hostent *ret, char *buf,
  899. size_t buflen, struct hostent **result, int *h_errnop)
  900. {
  901. struct netdev *netdev = netdev_default;
  902. struct sal_proto_family *pf;
  903. if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, gethostbyname_r))
  904. {
  905. return pf->netdb_ops->gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
  906. }
  907. else
  908. {
  909. /* get the first network interface device with up status */
  910. netdev = netdev_get_first_by_flags(NETDEV_FLAG_UP);
  911. if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, gethostbyname_r))
  912. {
  913. return pf->netdb_ops->gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
  914. }
  915. }
  916. return -1;
  917. }
  918. int sal_getaddrinfo(const char *nodename,
  919. const char *servname,
  920. const struct addrinfo *hints,
  921. struct addrinfo **res)
  922. {
  923. struct netdev *netdev = netdev_default;
  924. struct sal_proto_family *pf;
  925. int ret = 0;
  926. rt_uint32_t i = 0;
  927. if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, getaddrinfo))
  928. {
  929. ret = pf->netdb_ops->getaddrinfo(nodename, servname, hints, res);
  930. }
  931. else
  932. {
  933. /* get the first network interface device with up status */
  934. netdev = netdev_get_first_by_flags(NETDEV_FLAG_UP);
  935. if (SAL_NETDEV_NETDBOPS_VALID(netdev, pf, getaddrinfo))
  936. {
  937. ret = pf->netdb_ops->getaddrinfo(nodename, servname, hints, res);
  938. }
  939. else
  940. {
  941. ret = -1;
  942. }
  943. }
  944. if(ret == RT_EOK)
  945. {
  946. /*record the netdev and res*/
  947. for(i = 0; i < SAL_SOCKETS_NUM; i++)
  948. {
  949. if(sal_dev_res_tbl[i].res == RT_NULL)
  950. {
  951. sal_dev_res_tbl[i].res = *res;
  952. sal_dev_res_tbl[i].netdev = netdev;
  953. break;
  954. }
  955. }
  956. RT_ASSERT((i < SAL_SOCKETS_NUM));
  957. }
  958. return ret;
  959. }
  960. void sal_freeaddrinfo(struct addrinfo *ai)
  961. {
  962. struct netdev *netdev = RT_NULL;
  963. struct sal_proto_family *pf = RT_NULL;
  964. rt_uint32_t i = 0;
  965. /*when use the multi netdev, it must free the ai use the getaddrinfo netdev */
  966. for(i = 0; i < SAL_SOCKETS_NUM; i++)
  967. {
  968. if(sal_dev_res_tbl[i].res == ai)
  969. {
  970. netdev = sal_dev_res_tbl[i].netdev;
  971. sal_dev_res_tbl[i].res = RT_NULL;
  972. sal_dev_res_tbl[i].netdev = RT_NULL;
  973. break;
  974. }
  975. }
  976. RT_ASSERT((i < SAL_SOCKETS_NUM));
  977. if (SAL_NETDBOPS_VALID(netdev, pf, freeaddrinfo))
  978. {
  979. pf->netdb_ops->freeaddrinfo(ai);
  980. }
  981. }