sal_socket.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. /*
  2. * File : sal_socket.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-05-23 ChenYong First version
  23. */
  24. #include <rtthread.h>
  25. #include <rthw.h>
  26. #include <sal_socket.h>
  27. #include <sal_netdb.h>
  28. #include <sal.h>
  29. #define DBG_ENABLE
  30. #define DBG_SECTION_NAME "SAL_SOC"
  31. #define DBG_LEVEL DBG_INFO
  32. #define DBG_COLOR
  33. #include <rtdbg.h>
  34. #define SOCKET_TABLE_STEP_LEN 4
  35. /* the socket table used to dynamic allocate sockets */
  36. struct sal_socket_table
  37. {
  38. uint32_t max_socket;
  39. struct sal_socket **sockets;
  40. };
  41. /* The global array of available protocol families */
  42. static struct proto_family proto_families[SAL_PROTO_FAMILIES_NUM];
  43. /* The global socket table */
  44. static struct sal_socket_table socket_table;
  45. static struct rt_mutex sal_core_lock;
  46. static rt_bool_t init_ok = RT_FALSE;
  47. /**
  48. * SAL (Socket Abstraction Layer) initialize.
  49. *
  50. * @return result 0: initialize success
  51. * -1: initialize failed
  52. */
  53. int sal_init(void)
  54. {
  55. int cn;
  56. if(init_ok)
  57. {
  58. LOG_D("Socket Abstraction Layer is already initialized.");
  59. return 0;
  60. }
  61. /* init sal socket table */
  62. cn = SOCKET_TABLE_STEP_LEN < SAL_SOCKETS_NUM ? SOCKET_TABLE_STEP_LEN : SAL_SOCKETS_NUM;
  63. socket_table.max_socket = cn;
  64. socket_table.sockets = rt_calloc(1, cn * sizeof(struct sal_socket *));
  65. if (socket_table.sockets == RT_NULL)
  66. {
  67. LOG_E("No memory for socket table.\n");
  68. return -1;
  69. }
  70. /* create sal socket lock */
  71. rt_mutex_init(&sal_core_lock, "sal_lock", RT_IPC_FLAG_FIFO);
  72. LOG_I("Socket Abstraction Layer initialize success.");
  73. init_ok = RT_TRUE;
  74. return 0;
  75. }
  76. INIT_COMPONENT_EXPORT(sal_init);
  77. /**
  78. * This function will register protocol family to the global array of protocol families.
  79. *
  80. * @param pf protocol family object
  81. *
  82. * @return 0 : protocol family object register success
  83. * -1 : the global array of available protocol families is full
  84. */
  85. int sal_proto_family_register(const struct proto_family *pf)
  86. {
  87. rt_base_t level;
  88. int idx;
  89. /* disable interrupt */
  90. level = rt_hw_interrupt_disable();
  91. /* check protocol family is already registered */
  92. for(idx = 0; idx < SAL_PROTO_FAMILIES_NUM; idx++)
  93. {
  94. if(rt_strcmp(proto_families[idx].name, pf->name) == 0)
  95. {
  96. /* enable interrupt */
  97. rt_hw_interrupt_enable(level);
  98. LOG_E("%s protocol family is already registered!", pf->name);
  99. return -1;
  100. }
  101. }
  102. /* find an empty protocol family entry */
  103. for(idx = 0; idx < SAL_PROTO_FAMILIES_NUM && proto_families[idx].create; idx++);
  104. /* can't find an empty protocol family entry */
  105. if(idx == SAL_PROTO_FAMILIES_NUM)
  106. {
  107. /* enable interrupt */
  108. rt_hw_interrupt_enable(level);
  109. return -1;
  110. }
  111. rt_strncpy(proto_families[idx].name, pf->name, rt_strlen(pf->name));
  112. proto_families[idx].family = pf->family;
  113. proto_families[idx].sec_family = pf->sec_family;
  114. proto_families[idx].create = pf->create;
  115. proto_families[idx].gethostbyname = pf->gethostbyname;
  116. proto_families[idx].gethostbyname_r = pf->gethostbyname_r;
  117. proto_families[idx].freeaddrinfo = pf->freeaddrinfo;
  118. proto_families[idx].getaddrinfo = pf->getaddrinfo;
  119. /* enable interrupt */
  120. rt_hw_interrupt_enable(level);
  121. return 0;
  122. }
  123. /**
  124. * This function removes a previously registered protocol family object.
  125. *
  126. * @param pf protocol family object
  127. *
  128. * @return >=0 : unregister protocol family index
  129. * -1 : unregister failed
  130. */
  131. int sal_proto_family_unregister(const struct proto_family *pf)
  132. {
  133. int idx = 0;
  134. RT_ASSERT(pf != RT_NULL);
  135. for(idx = 0; idx < SAL_PROTO_FAMILIES_NUM; idx++)
  136. {
  137. if(rt_strcmp(proto_families[idx].name, pf->name) == 0)
  138. {
  139. rt_memset(&proto_families[idx], 0x00, sizeof(struct proto_family));
  140. return idx;
  141. }
  142. }
  143. return -1;
  144. }
  145. /**
  146. * This function will get protocol family by name.
  147. *
  148. * @param name protocol family name
  149. *
  150. * @return protocol family object
  151. */
  152. struct proto_family *sal_proto_family_find(const char *name)
  153. {
  154. int idx = 0;
  155. RT_ASSERT(name != RT_NULL);
  156. for (idx = 0; idx < SAL_PROTO_FAMILIES_NUM; idx++)
  157. {
  158. if (rt_strcmp(proto_families[idx].name, name) == 0)
  159. {
  160. return &proto_families[idx];
  161. }
  162. }
  163. return RT_NULL;
  164. }
  165. /**
  166. * This function will get sal socket object by sal socket descriptor.
  167. *
  168. * @param socket sal socket index
  169. *
  170. * @return sal socket object of the current sal socket index
  171. */
  172. struct sal_socket *sal_get_socket(int socket)
  173. {
  174. struct sal_socket_table *st = &socket_table;
  175. if (socket < 0 || socket >= (int) st->max_socket)
  176. {
  177. return RT_NULL;
  178. }
  179. socket = socket - SAL_SOCKET_OFFSET;
  180. /* check socket structure valid or not */
  181. if (st->sockets[socket]->magic != SAL_SOCKET_MAGIC)
  182. {
  183. return RT_NULL;
  184. }
  185. return st->sockets[socket];
  186. }
  187. /**
  188. * This function will lock sal socket.
  189. *
  190. * @note please don't invoke it on ISR.
  191. */
  192. static void sal_lock(void)
  193. {
  194. rt_err_t result;
  195. result = rt_mutex_take(&sal_core_lock, RT_WAITING_FOREVER);
  196. if (result != RT_EOK)
  197. {
  198. RT_ASSERT(0);
  199. }
  200. }
  201. /**
  202. * This function will lock sal socket.
  203. *
  204. * @note please don't invoke it on ISR.
  205. */
  206. static void sal_unlock(void)
  207. {
  208. rt_mutex_release(&sal_core_lock);
  209. }
  210. /**
  211. * This function will get protocol family structure by family type
  212. *
  213. * @param family protocol family
  214. *
  215. * @return protocol family structure address
  216. */
  217. static struct proto_family *get_proto_family(int family)
  218. {
  219. int idx;
  220. for (idx = 0; idx < SAL_PROTO_FAMILIES_NUM; idx++)
  221. {
  222. if (proto_families[idx].family == family && proto_families[idx].create)
  223. {
  224. return &proto_families[idx];
  225. }
  226. }
  227. /* compare the secondary protocol families when primary protocol families find failed */
  228. for (idx = 0; idx < SAL_PROTO_FAMILIES_NUM; idx++)
  229. {
  230. if (proto_families[idx].sec_family == family && proto_families[idx].create)
  231. {
  232. return &proto_families[idx];
  233. }
  234. }
  235. return RT_NULL;
  236. }
  237. /**
  238. * This function will initialize sal socket object and set socket options
  239. *
  240. * @param family protocol family
  241. * @param type socket type
  242. * @param protocol transfer Protocol
  243. * @param res sal socket object address
  244. *
  245. * @return 0 : socket initialize success
  246. * -1 : input the wrong family
  247. * -2 : input the wrong socket type
  248. * -3 : get protocol family object failed
  249. * -4 : set socket options failed
  250. */
  251. static int socket_init(int family, int type, int protocol, struct sal_socket **res)
  252. {
  253. struct sal_socket *sock;
  254. struct proto_family *pf;
  255. if (family < 0 || family > AF_MAX)
  256. {
  257. return -1;
  258. }
  259. if (type < 0 || type > SOCK_MAX)
  260. {
  261. return -2;
  262. }
  263. sock = *res;
  264. sock->domain = family;
  265. sock->type = type;
  266. sock->protocol = protocol;
  267. /* get socket protocol family object */
  268. if ((pf = get_proto_family(family)) == RT_NULL)
  269. {
  270. return -3;
  271. }
  272. /* registered the current socket options */
  273. if (pf->create(sock, type, protocol) != 0)
  274. {
  275. return -4;
  276. }
  277. return 0;
  278. }
  279. static int socket_alloc(struct sal_socket_table *st, int f_socket)
  280. {
  281. int idx;
  282. /* find an empty socket entry */
  283. for (idx = f_socket; idx < (int) st->max_socket; idx++)
  284. {
  285. if (st->sockets[idx] == RT_NULL)
  286. break;
  287. if (st->sockets[idx]->ops == RT_NULL)
  288. break;
  289. }
  290. /* allocate a larger sockte container */
  291. if (idx == (int) st->max_socket && st->max_socket < SAL_SOCKETS_NUM)
  292. {
  293. int cnt, index;
  294. struct sal_socket **sockets;
  295. /* increase the number of socket with 4 step length */
  296. cnt = st->max_socket + SOCKET_TABLE_STEP_LEN;
  297. cnt = cnt > SAL_SOCKETS_NUM ? SAL_SOCKETS_NUM : cnt;
  298. sockets = rt_realloc(st->sockets, cnt * sizeof(struct sal_socket *));
  299. if (sockets == RT_NULL)
  300. goto __result; /* return st->max_socket */
  301. /* clean the new allocated fds */
  302. for (index = st->max_socket; index < cnt; index++)
  303. {
  304. sockets[index] = RT_NULL;
  305. }
  306. st->sockets = sockets;
  307. st->max_socket = cnt;
  308. }
  309. /* allocate 'struct sal_socket' */
  310. if (idx < (int) st->max_socket && st->sockets[idx] == RT_NULL)
  311. {
  312. st->sockets[idx] = rt_calloc(1, sizeof(struct sal_socket));
  313. if (st->sockets[idx] == RT_NULL)
  314. {
  315. idx = st->max_socket;
  316. }
  317. }
  318. __result:
  319. return idx;
  320. }
  321. static int socket_new(void)
  322. {
  323. struct sal_socket *sock;
  324. struct sal_socket_table *st = &socket_table;
  325. int idx;
  326. sal_lock();
  327. /* find an empty sal socket entry */
  328. idx = socket_alloc(st, 0);
  329. /* can't find an empty sal socket entry */
  330. if (idx == (int) st->max_socket)
  331. {
  332. idx = -(1 + SAL_SOCKET_OFFSET);
  333. goto __result;
  334. }
  335. sock = st->sockets[idx];
  336. sock->socket = idx + SAL_SOCKET_OFFSET;
  337. sock->magic = SAL_SOCKET_MAGIC;
  338. __result:
  339. sal_unlock();
  340. return idx + SAL_SOCKET_OFFSET;
  341. }
  342. int sal_accept(int socket, struct sockaddr *addr, socklen_t *addrlen)
  343. {
  344. int new_socket;
  345. struct sal_socket *sock;
  346. sock = sal_get_socket(socket);
  347. if (!sock)
  348. {
  349. return -1;
  350. }
  351. if (sock->ops->accept == RT_NULL)
  352. {
  353. return -RT_ENOSYS;
  354. }
  355. new_socket = sock->ops->accept((int) sock->user_data, addr, addrlen);
  356. if (new_socket != -1)
  357. {
  358. int retval;
  359. int new_sal_socket;
  360. struct sal_socket *new_sock;
  361. /* allocate a new socket structure and registered socket options */
  362. new_sal_socket = socket_new();
  363. if (new_sal_socket < 0)
  364. {
  365. sock->ops->closesocket(new_socket);
  366. return -1;
  367. }
  368. new_sock = sal_get_socket(new_sal_socket);
  369. retval = socket_init(sock->domain, sock->type, sock->protocol, &new_sock);
  370. if (retval < 0)
  371. {
  372. sock->ops->closesocket(new_socket);
  373. rt_memset(new_sock, 0x00, sizeof(struct sal_socket));
  374. LOG_E("New socket registered failed, return error %d.", retval);
  375. return -1;
  376. }
  377. /* socket struct user_data used to store the acquired new socket */
  378. new_sock->user_data = (void *) new_socket;
  379. return new_sal_socket;
  380. }
  381. return -1;
  382. }
  383. int sal_bind(int socket, const struct sockaddr *name, socklen_t namelen)
  384. {
  385. struct sal_socket *sock;
  386. sock = sal_get_socket(socket);
  387. if (!sock)
  388. {
  389. return -1;
  390. }
  391. if (sock->ops->bind == RT_NULL)
  392. {
  393. return -RT_ENOSYS;
  394. }
  395. return sock->ops->bind((int) sock->user_data, name, namelen);
  396. }
  397. int sal_shutdown(int socket, int how)
  398. {
  399. struct sal_socket *sock;
  400. sock = sal_get_socket(socket);
  401. if (!sock)
  402. {
  403. return -1;
  404. }
  405. if (sock->ops->shutdown == RT_NULL)
  406. {
  407. return -RT_ENOSYS;
  408. }
  409. if (sock->ops->shutdown((int) sock->user_data, how) == 0)
  410. {
  411. rt_free(sock);
  412. socket_table.sockets[socket] = RT_NULL;
  413. return 0;
  414. }
  415. return -1;
  416. }
  417. int sal_getpeername(int socket, struct sockaddr *name, socklen_t *namelen)
  418. {
  419. struct sal_socket *sock;
  420. sock = sal_get_socket(socket);
  421. if (!sock)
  422. {
  423. return -1;
  424. }
  425. if (sock->ops->getpeername == RT_NULL)
  426. {
  427. return -RT_ENOSYS;
  428. }
  429. return sock->ops->getpeername((int) sock->user_data, name, namelen);
  430. }
  431. int sal_getsockname(int socket, struct sockaddr *name, socklen_t *namelen)
  432. {
  433. struct sal_socket *sock;
  434. sock = sal_get_socket(socket);
  435. if (!sock)
  436. {
  437. return -1;
  438. }
  439. if (sock->ops->getsockname == RT_NULL)
  440. {
  441. return -RT_ENOSYS;
  442. }
  443. return sock->ops->getsockname((int) sock->user_data, name, namelen);
  444. }
  445. int sal_getsockopt(int socket, int level, int optname, void *optval, socklen_t *optlen)
  446. {
  447. struct sal_socket *sock;
  448. sock = sal_get_socket(socket);
  449. if (!sock)
  450. {
  451. return -1;
  452. }
  453. if (sock->ops->getsockopt == RT_NULL)
  454. {
  455. return -RT_ENOSYS;
  456. }
  457. return sock->ops->getsockopt((int) sock->user_data, level, optname, optval, optlen);
  458. }
  459. int sal_setsockopt(int socket, int level, int optname, const void *optval, socklen_t optlen)
  460. {
  461. struct sal_socket *sock;
  462. sock = sal_get_socket(socket);
  463. if (!sock)
  464. {
  465. return -1;
  466. }
  467. if (sock->ops->setsockopt == RT_NULL)
  468. {
  469. return -RT_ENOSYS;
  470. }
  471. return sock->ops->setsockopt((int) sock->user_data, level, optname, optval, optlen);
  472. }
  473. int sal_connect(int socket, const struct sockaddr *name, socklen_t namelen)
  474. {
  475. struct sal_socket *sock;
  476. sock = sal_get_socket(socket);
  477. if (!sock)
  478. {
  479. return -1;
  480. }
  481. if (sock->ops->connect == RT_NULL)
  482. {
  483. return -RT_ENOSYS;
  484. }
  485. return sock->ops->connect((int) sock->user_data, name, namelen);
  486. }
  487. int sal_listen(int socket, int backlog)
  488. {
  489. struct sal_socket *sock;
  490. sock = sal_get_socket(socket);
  491. if (!sock)
  492. {
  493. return -1;
  494. }
  495. if (sock->ops->listen == RT_NULL)
  496. {
  497. return -RT_ENOSYS;
  498. }
  499. return sock->ops->listen((int) sock->user_data, backlog);
  500. }
  501. int sal_recvfrom(int socket, void *mem, size_t len, int flags,
  502. struct sockaddr *from, socklen_t *fromlen)
  503. {
  504. struct sal_socket *sock;
  505. sock = sal_get_socket(socket);
  506. if (!sock)
  507. {
  508. return -1;
  509. }
  510. if (sock->ops->recvfrom == RT_NULL)
  511. {
  512. return -RT_ENOSYS;
  513. }
  514. return sock->ops->recvfrom((int) sock->user_data, mem, len, flags, from, fromlen);
  515. }
  516. int sal_sendto(int socket, const void *dataptr, size_t size, int flags,
  517. const struct sockaddr *to, socklen_t tolen)
  518. {
  519. struct sal_socket *sock;
  520. sock = sal_get_socket(socket);
  521. if (!sock)
  522. {
  523. return -1;
  524. }
  525. if (sock->ops->sendto == RT_NULL)
  526. {
  527. return -RT_ENOSYS;
  528. }
  529. return sock->ops->sendto((int) sock->user_data, dataptr, size, flags, to, tolen);
  530. }
  531. int sal_socket(int domain, int type, int protocol)
  532. {
  533. int retval;
  534. int socket, proto_socket;
  535. struct sal_socket *sock;
  536. /* allocate a new socket and registered socket options */
  537. socket = socket_new();
  538. if (socket < 0)
  539. {
  540. return -1;
  541. }
  542. sock = sal_get_socket(socket);
  543. retval = socket_init(domain, type, protocol, &sock);
  544. if (retval < 0)
  545. {
  546. LOG_E("SAL socket protocol family input failed, return error %d.", retval);
  547. return -1;
  548. }
  549. if (sock->ops->socket == RT_NULL)
  550. {
  551. return -RT_ENOSYS;
  552. }
  553. proto_socket = sock->ops->socket(domain, type, protocol);
  554. if (proto_socket >= 0)
  555. {
  556. sock->user_data = (void *) proto_socket;
  557. return sock->socket;
  558. }
  559. return -1;
  560. }
  561. int sal_closesocket(int socket)
  562. {
  563. struct sal_socket *sock;
  564. sock = sal_get_socket(socket);
  565. if (!sock)
  566. {
  567. return -1;
  568. }
  569. if (sock->ops->closesocket == RT_NULL)
  570. {
  571. return -RT_ENOSYS;
  572. }
  573. if (sock->ops->closesocket((int) sock->user_data) == 0)
  574. {
  575. rt_free(sock);
  576. socket_table.sockets[socket] = RT_NULL;
  577. return 0;
  578. }
  579. return -1;
  580. }
  581. int sal_ioctlsocket(int socket, long cmd, void *arg)
  582. {
  583. struct sal_socket *sock;
  584. sock = sal_get_socket(socket);
  585. if (!sock)
  586. {
  587. return -1;
  588. }
  589. if (sock->ops->ioctlsocket == RT_NULL)
  590. {
  591. return -RT_ENOSYS;
  592. }
  593. return sock->ops->ioctlsocket((int) sock->user_data, cmd, arg);
  594. }
  595. #ifdef SAL_USING_POSIX
  596. int sal_poll(struct dfs_fd *file, struct rt_pollreq *req)
  597. {
  598. struct sal_socket *sock;
  599. int socket = (int) file->data;
  600. sock = sal_get_socket(socket);
  601. if (!sock)
  602. {
  603. return -1;
  604. }
  605. if (sock->ops->poll == RT_NULL)
  606. {
  607. return -RT_ENOSYS;
  608. }
  609. return sock->ops->poll(file, req);
  610. }
  611. #endif
  612. struct hostent *sal_gethostbyname(const char *name)
  613. {
  614. int i;
  615. struct hostent *hst;
  616. for (i = 0; i < SAL_PROTO_FAMILIES_NUM; ++i)
  617. {
  618. if (proto_families[i].gethostbyname)
  619. {
  620. hst = proto_families[i].gethostbyname(name);
  621. if (hst != RT_NULL)
  622. {
  623. return hst;
  624. }
  625. }
  626. }
  627. return RT_NULL;
  628. }
  629. int sal_gethostbyname_r(const char *name, struct hostent *ret, char *buf,
  630. size_t buflen, struct hostent **result, int *h_errnop)
  631. {
  632. int i, res;
  633. for (i = 0; i < SAL_PROTO_FAMILIES_NUM; ++i)
  634. {
  635. if (proto_families[i].gethostbyname_r)
  636. {
  637. res = proto_families[i].gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
  638. if (res == 0)
  639. {
  640. return res;
  641. }
  642. }
  643. }
  644. return -1;
  645. }
  646. void sal_freeaddrinfo(struct addrinfo *ai)
  647. {
  648. int i;
  649. for (i = 0; i < SAL_PROTO_FAMILIES_NUM; ++i)
  650. {
  651. if (proto_families[i].freeaddrinfo)
  652. {
  653. proto_families[i].freeaddrinfo(ai);
  654. return;
  655. }
  656. }
  657. }
  658. int sal_getaddrinfo(const char *nodename,
  659. const char *servname,
  660. const struct addrinfo *hints,
  661. struct addrinfo **res)
  662. {
  663. int i, ret;
  664. for (i = 0; i < SAL_PROTO_FAMILIES_NUM; ++i)
  665. {
  666. if (proto_families[i].getaddrinfo)
  667. {
  668. ret = proto_families[i].getaddrinfo(nodename, servname, hints, res);
  669. if (ret == 0)
  670. {
  671. return ret;
  672. }
  673. }
  674. }
  675. return -1;
  676. }