ipv4_getsockname.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /****************************************************************************
  2. * net/inet/ipv4_getsockname.c
  3. *
  4. * Copyright (C) 2011-2012, 2017 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  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 <nuttx/net/netdev.h>
  46. #include "netdev/netdev.h"
  47. #include "udp/udp.h"
  48. #include "tcp/tcp.h"
  49. #include "inet/inet.h"
  50. #ifdef CONFIG_NET_IPv4
  51. /****************************************************************************
  52. * Public Functions
  53. ****************************************************************************/
  54. /****************************************************************************
  55. * Name: ipv4_getsockname
  56. *
  57. * Description:
  58. * The ipv4_getsockname() function retrieves the locally-bound name of the
  59. * specified PF_NET socket.
  60. *
  61. * Input Parameters:
  62. * psock Point to the socket structure instance [in]
  63. * addr sockaddr structure to receive data [out]
  64. * addrlen Length of sockaddr structure [in/out]
  65. *
  66. * Returned Value:
  67. * On success, 0 is returned, the 'addr' argument points to the address
  68. * of the socket, and the 'addrlen' argument points to the length of the
  69. * address. Otherwise, a negated errno value is returned. See
  70. * getsockname() for the list of returned error values.
  71. *
  72. ****************************************************************************/
  73. int ipv4_getsockname(FAR struct socket *psock, FAR struct sockaddr *addr,
  74. FAR socklen_t *addrlen)
  75. {
  76. #if defined(NET_TCP_HAVE_STACK) || defined(NET_UDP_HAVE_STACK)
  77. FAR struct sockaddr_in *outaddr = (FAR struct sockaddr_in *)addr;
  78. FAR struct net_driver_s *dev;
  79. in_addr_t lipaddr;
  80. in_addr_t ripaddr;
  81. /* Check if enough space has been provided for the full address */
  82. if (*addrlen < sizeof(struct sockaddr_in))
  83. {
  84. /* This function is supposed to return the partial address if
  85. * a smaller buffer has been provided. This support has not
  86. * been implemented.
  87. */
  88. return -ENOSYS;
  89. }
  90. /* Set the port number */
  91. switch (psock->s_type)
  92. {
  93. #ifdef NET_TCP_HAVE_STACK
  94. case SOCK_STREAM:
  95. {
  96. FAR struct tcp_conn_s *tcp_conn = (FAR struct tcp_conn_s *)psock->s_conn;
  97. outaddr->sin_port = tcp_conn->lport; /* Already in network byte order */
  98. lipaddr = tcp_conn->u.ipv4.laddr;
  99. ripaddr = tcp_conn->u.ipv4.raddr;
  100. }
  101. break;
  102. #endif
  103. #ifdef NET_UDP_HAVE_STACK
  104. case SOCK_DGRAM:
  105. {
  106. FAR struct udp_conn_s *udp_conn = (FAR struct udp_conn_s *)psock->s_conn;
  107. outaddr->sin_port = udp_conn->lport; /* Already in network byte order */
  108. lipaddr = udp_conn->u.ipv4.laddr;
  109. ripaddr = udp_conn->u.ipv4.raddr;
  110. }
  111. break;
  112. #endif
  113. default:
  114. return -EOPNOTSUPP;
  115. }
  116. if (lipaddr == 0)
  117. {
  118. outaddr->sin_family = psock->s_domain;
  119. outaddr->sin_addr.s_addr = 0;
  120. *addrlen = sizeof(struct sockaddr_in);
  121. return OK;
  122. }
  123. net_lock();
  124. /* Find the device matching the IPv4 address in the connection structure.
  125. * NOTE: listening sockets have no ripaddr. Work around is to use the
  126. * lipaddr when ripaddr is not available.
  127. */
  128. if (ripaddr == 0)
  129. {
  130. ripaddr = lipaddr;
  131. }
  132. dev = netdev_findby_ripv4addr(lipaddr, ripaddr);
  133. if (dev == NULL)
  134. {
  135. net_unlock();
  136. return -EINVAL;
  137. }
  138. /* Set the address family and the IP address */
  139. outaddr->sin_family = psock->s_domain;
  140. outaddr->sin_addr.s_addr = dev->d_ipaddr;
  141. *addrlen = sizeof(struct sockaddr_in);
  142. net_unlock();
  143. /* Return success */
  144. return OK;
  145. #else
  146. return -EOPNOTSUPP;
  147. #endif
  148. }
  149. #endif /* CONFIG_NET_IPv4 */