lwip_sockets.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * File : lwip_sockets.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2015, 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. * 2015-02-17 Bernard First version
  23. */
  24. #include <dfs.h>
  25. #include <dfs_def.h>
  26. #include <dfs_posix.h>
  27. #include <sys/socket.h>
  28. #include "dfs_lwip.h"
  29. int accept(int s, struct sockaddr *addr, socklen_t *addrlen)
  30. {
  31. int new_client = -1;
  32. int sock = dfs_lwip_getsocket(s);
  33. new_client = lwip_accept(sock, addr, addrlen);
  34. if (new_client != -1)
  35. {
  36. /* this is a new socket, create it in file system fd */
  37. int fd;
  38. struct dfs_fd *d;
  39. /* allocate a fd */
  40. fd = fd_new();
  41. if (fd < 0)
  42. {
  43. rt_set_errno(-DFS_STATUS_ENOMEM);
  44. lwip_close(sock);
  45. printf("no fd yet!\n");
  46. return -1;
  47. }
  48. d = fd_get(fd);
  49. /* this is a socket fd */
  50. d->type = FT_SOCKET;
  51. d->path = RT_NULL;
  52. d->fs = dfs_lwip_get_fs();
  53. d->flags = DFS_O_RDWR; /* set flags as read and write */
  54. d->size = 0;
  55. d->pos = 0;
  56. /* set socket to the data of dfs_fd */
  57. d->data = (void*) new_client;
  58. /* release the ref-count of fd */
  59. fd_put(d);
  60. return fd;
  61. }
  62. return new_client;
  63. }
  64. int bind(int s, const struct sockaddr *name, socklen_t namelen)
  65. {
  66. int sock = dfs_lwip_getsocket(s);
  67. return lwip_bind(sock, name, namelen);
  68. }
  69. int shutdown(int s, int how)
  70. {
  71. int sock;
  72. struct dfs_fd *d;
  73. d = fd_get(s);
  74. if (d == RT_NULL)
  75. {
  76. rt_set_errno(-DFS_STATUS_EBADF);
  77. return -1;
  78. }
  79. sock = dfs_lwip_getsocket(s);
  80. if (lwip_shutdown(sock, how) == 0)
  81. {
  82. /* socket has been closed, delete it from file system fd */
  83. fd_put(d);
  84. fd_put(d);
  85. return 0;
  86. }
  87. return -1;
  88. }
  89. int getpeername (int s, struct sockaddr *name, socklen_t *namelen)
  90. {
  91. int sock = dfs_lwip_getsocket(s);
  92. return lwip_getpeername(sock, name, namelen);
  93. }
  94. int getsockname (int s, struct sockaddr *name, socklen_t *namelen)
  95. {
  96. int sock = dfs_lwip_getsocket(s);
  97. return lwip_getsockname(sock, name, namelen);
  98. }
  99. int getsockopt (int s, int level, int optname, void *optval, socklen_t *optlen)
  100. {
  101. int sock = dfs_lwip_getsocket(s);
  102. return lwip_getsockopt(sock, level, optname, optval, optlen);
  103. }
  104. int setsockopt (int s, int level, int optname, const void *optval, socklen_t optlen)
  105. {
  106. int sock = dfs_lwip_getsocket(s);
  107. return lwip_setsockopt(sock, level, optname, optval, optlen);
  108. }
  109. int connect(int s, const struct sockaddr *name, socklen_t namelen)
  110. {
  111. int sock = dfs_lwip_getsocket(s);
  112. return lwip_connect(sock, name, namelen);
  113. }
  114. int listen(int s, int backlog)
  115. {
  116. int sock = dfs_lwip_getsocket(s);
  117. return lwip_listen(sock, backlog);
  118. }
  119. int recv(int s, void *mem, size_t len, int flags)
  120. {
  121. int sock = dfs_lwip_getsocket(s);
  122. return lwip_recv(sock, mem, len, flags);
  123. }
  124. int recvfrom(int s, void *mem, size_t len, int flags,
  125. struct sockaddr *from, socklen_t *fromlen)
  126. {
  127. int sock = dfs_lwip_getsocket(s);
  128. return lwip_recvfrom(sock, mem, len, flags, from, fromlen);
  129. }
  130. int send(int s, const void *dataptr, size_t size, int flags)
  131. {
  132. int sock = dfs_lwip_getsocket(s);
  133. return lwip_send(sock, dataptr, size, flags);
  134. }
  135. int sendto(int s, const void *dataptr, size_t size, int flags,
  136. const struct sockaddr *to, socklen_t tolen)
  137. {
  138. int sock = dfs_lwip_getsocket(s);
  139. return lwip_sendto(sock, dataptr, size, flags, to, tolen);
  140. }
  141. int socket(int domain, int type, int protocol)
  142. {
  143. /* create a BSD socket */
  144. int fd;
  145. int sock;
  146. struct dfs_fd *d;
  147. /* allocate a fd */
  148. fd = fd_new();
  149. if (fd < 0)
  150. {
  151. rt_set_errno(-DFS_STATUS_ENOMEM);
  152. return -1;
  153. }
  154. d = fd_get(fd);
  155. /* create socket in lwip and then put it to the dfs_fd */
  156. sock = lwip_socket(domain, type, protocol);
  157. if (sock > 0)
  158. {
  159. /* this is a socket fd */
  160. d->type = FT_SOCKET;
  161. d->path = RT_NULL;
  162. d->fs = dfs_lwip_get_fs();
  163. d->flags = DFS_O_RDWR; /* set flags as read and write */
  164. d->size = 0;
  165. d->pos = 0;
  166. /* set socket to the data of dfs_fd */
  167. d->data = (void*) sock;
  168. }
  169. /* release the ref-count of fd */
  170. fd_put(d);
  171. return fd;
  172. }