getpeername.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /****************************************************************************
  2. * nuttx/net/socket/getpeername.c
  3. *
  4. * Copyright (C) 2018 Pinecone Inc. All rights reserved.
  5. * Author: Guiding Li<liguiding@pinecone.net>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/types.h>
  40. #include <sys/socket.h>
  41. #include <string.h>
  42. #include <errno.h>
  43. #include <assert.h>
  44. #include <nuttx/net/net.h>
  45. #include "socket/socket.h"
  46. #ifdef CONFIG_NET
  47. /****************************************************************************
  48. * Public Functions
  49. ****************************************************************************/
  50. /****************************************************************************
  51. * Name: psock_getpeername
  52. *
  53. * Description:
  54. * The psock_getpeername() function retrieves the remote-connected name of the
  55. * specified socket, stores this address in the sockaddr structure pointed
  56. * to by the 'addr' argument, and stores the length of this address in the
  57. * object pointed to by the 'addrlen' argument.
  58. *
  59. * If the actual length of the address is greater than the length of the
  60. * supplied sockaddr structure, the stored address will be truncated.
  61. *
  62. * If the socket has not been bound to a local name, the value stored in
  63. * the object pointed to by address is unspecified.
  64. *
  65. * Parameters:
  66. * psock Socket structure of socket to operate on
  67. * addr sockaddr structure to receive data [out]
  68. * addrlen Length of sockaddr structure [in/out]
  69. *
  70. * Returned Value:
  71. * On success, 0 is returned, the 'addr' argument points to the address
  72. * of the socket, and the 'addrlen' argument points to the length of the
  73. * address. Otherwise, -1 is returned and errno is set to indicate the error.
  74. * Possible errno values that may be returned include:
  75. *
  76. * EBADF - The socket argument is not a valid file descriptor.
  77. * ENOTSOCK - The socket argument does not refer to a socket.
  78. * EOPNOTSUPP - The operation is not supported for this socket's protocol.
  79. * EINVAL - The socket has been shut down.
  80. * ENOBUFS - Insufficient resources were available in the system to
  81. * complete the function.
  82. *
  83. ****************************************************************************/
  84. int psock_getpeername(FAR struct socket *psock, FAR struct sockaddr *addr, FAR socklen_t *addrlen)
  85. {
  86. /* Verify that the psock corresponds to valid, allocated socket */
  87. if (psock == NULL || psock->s_crefs <= 0)
  88. {
  89. return -EBADF;
  90. }
  91. /* Some sanity checking... Shouldn't need this on a buckled up embedded
  92. * system (?)
  93. */
  94. #ifdef CONFIG_DEBUG_FEATURES
  95. if (addr == NULL || addrlen <= 0)
  96. {
  97. return -EINVAL;
  98. }
  99. #endif
  100. /* Let the address family's send() method handle the operation */
  101. DEBUGASSERT(psock->s_sockif != NULL);
  102. if (psock->s_sockif->si_getpeername == NULL)
  103. {
  104. return -EOPNOTSUPP;
  105. }
  106. return psock->s_sockif->si_getpeername(psock, addr, addrlen);
  107. }
  108. /****************************************************************************
  109. * Name: getpeername
  110. *
  111. * Description:
  112. * The getpeername() function retrieves the remote-connected name of the
  113. * specified socket, stores this address in the sockaddr structure pointed
  114. * to by the 'addr' argument, and stores the length of this address in the
  115. * object pointed to by the 'addrlen' argument.
  116. *
  117. * If the actual length of the address is greater than the length of the
  118. * supplied sockaddr structure, the stored address will be truncated.
  119. *
  120. * If the socket has not been bound to a local name, the value stored in
  121. * the object pointed to by address is unspecified.
  122. *
  123. * Parameters:
  124. * sockfd Socket descriptor of socket [in]
  125. * addr sockaddr structure to receive data [out]
  126. * addrlen Length of sockaddr structure [in/out]
  127. *
  128. * Returned Value:
  129. * On success, 0 is returned, the 'addr' argument points to the address
  130. * of the socket, and the 'addrlen' argument points to the length of the
  131. * address. Otherwise, -1 is returned and errno is set to indicate the error.
  132. * Possible errno values that may be returned include:
  133. *
  134. * EBADF - The socket argument is not a valid file descriptor.
  135. * ENOTSOCK - The socket argument does not refer to a socket.
  136. * EOPNOTSUPP - The operation is not supported for this socket's protocol.
  137. * EINVAL - The socket has been shut down.
  138. * ENOBUFS - Insufficient resources were available in the system to
  139. * complete the function.
  140. *
  141. ****************************************************************************/
  142. int getpeername(int sockfd, FAR struct sockaddr *addr, FAR socklen_t *addrlen)
  143. {
  144. FAR struct socket *psock = sockfd_socket(sockfd);
  145. int ret;
  146. /* Let psock_getpeername() do all of the work */
  147. ret = psock_getpeername(psock, addr, addrlen);
  148. if (ret < 0)
  149. {
  150. set_errno(-ret);
  151. return ERROR;
  152. }
  153. return OK;
  154. }
  155. #endif /* CONFIG_NET */