lwip_sockets.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 <lwip/sockets.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 sock = dfs_lwip_getsocket(s);
  32. return lwip_accept(sock, addr, addrlen);
  33. }
  34. int bind(int s, const struct sockaddr *name, socklen_t namelen)
  35. {
  36. int sock = dfs_lwip_getsocket(s);
  37. return lwip_bind(sock, name, namelen);
  38. }
  39. int shutdown(int s, int how)
  40. {
  41. int sock = dfs_lwip_getsocket(s);
  42. return lwip_shutdown(s, how);
  43. }
  44. int getpeername (int s, struct sockaddr *name, socklen_t *namelen)
  45. {
  46. int sock = dfs_lwip_getsocket(s);
  47. return lwip_getpeername(sock, name, namelen);
  48. }
  49. int getsockname (int s, struct sockaddr *name, socklen_t *namelen)
  50. {
  51. int sock = dfs_lwip_getsocket(s);
  52. return lwip_getsockname(sock, name, namelen);
  53. }
  54. int getsockopt (int s, int level, int optname, void *optval, socklen_t *optlen)
  55. {
  56. int sock = dfs_lwip_getsocket(s);
  57. return lwip_getsockopt(sock, level, optname, optval, optlen);
  58. }
  59. int setsockopt (int s, int level, int optname, const void *optval, socklen_t optlen)
  60. {
  61. int sock = dfs_lwip_getsocket(s);
  62. return lwip_setsockopt(sock, level, optname, optval, optlen);
  63. }
  64. int connect(int s, const struct sockaddr *name, socklen_t namelen)
  65. {
  66. int sock = dfs_lwip_getsocket(s);
  67. return lwip_connect(sock, name, namelen);
  68. }
  69. int listen(int s, int backlog)
  70. {
  71. int sock = dfs_lwip_getsocket(s);
  72. return lwip_listen(sock, backlog);
  73. }
  74. int recv(int s, void *mem, size_t len, int flags)
  75. {
  76. int sock = dfs_lwip_getsocket(s);
  77. return lwip_recv(sock, mem, len, flags);
  78. }
  79. int recvfrom(int s, void *mem, size_t len, int flags,
  80. struct sockaddr *from, socklen_t *fromlen)
  81. {
  82. int sock = dfs_lwip_getsocket(s);
  83. return lwip_recvfrom(sock, mem, len, flags, from, fromlen);
  84. }
  85. int send(int s, const void *dataptr, size_t size, int flags)
  86. {
  87. int sock = dfs_lwip_getsocket(s);
  88. return lwip_send(sock, dataptr, size, flags);
  89. }
  90. int sendto(int s, const void *dataptr, size_t size, int flags,
  91. const struct sockaddr *to, socklen_t tolen)
  92. {
  93. int sock = dfs_lwip_getsocket(s);
  94. return lwip_sendto(sock, dataptr, size, flags, to, tolen);
  95. }
  96. int socket(int domain, int type, int protocol)
  97. {
  98. /* create a BSD socket */
  99. int fd;
  100. int sock;
  101. struct dfs_fd *d;
  102. /* allocate a fd */
  103. fd = fd_new();
  104. if (fd < 0)
  105. {
  106. rt_set_errno(-DFS_STATUS_ENOMEM);
  107. return -1;
  108. }
  109. d = fd_get(fd);
  110. /* create socket in lwip and then put it to the dfs_fd */
  111. sock = lwip_socket(domain, type, protocol);
  112. if (sock > 0)
  113. {
  114. /* this is a socket fd */
  115. d->type = FT_SOCKET;
  116. d->path = RT_NULL;
  117. d->fs = dfs_lwip_get_fs();
  118. d->flags = DFS_O_RDWR; /* set flags as read and write */
  119. d->size = 0;
  120. d->pos = 0;
  121. /* set socket to the data of dfs_fd */
  122. d->data = (void*) sock;
  123. }
  124. /* release the ref-count of fd */
  125. fd_put(d);
  126. return fd;
  127. }