sal_socket.c 32 KB

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