sal_socket.c 30 KB

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