Watt-32 tcp/ip  2.2 dev-rel.10
getname.c
Go to the documentation of this file.
1 
5 /* BSD sockets functionality for Watt-32 TCP/IP
6  *
7  * Copyright (c) 1997-2002 Gisle Vanem <gvanem@yahoo.no>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  * must display the following acknowledgement:
19  * This product includes software developed by Gisle Vanem
20  * Bergen, Norway.
21  *
22  * THIS SOFTWARE IS PROVIDED BY ME (Gisle Vanem) AND CONTRIBUTORS ``AS IS''
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL I OR CONTRIBUTORS BE LIABLE FOR ANY
26  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Version
34  *
35  * 0.5 : Dec 18, 1997 : G. Vanem - created
36  * 0.6 : Aug 07, 2002 : G. Vanem - added AF_INET6 support
37  */
38 
39 #include "socket.h"
40 
41 #if defined(USE_BSD_API)
42 
43 int W32_CALL getsockname (int s, struct sockaddr *name, int *namelen)
44 {
46  int sa_len;
47 
48  SOCK_PROLOGUE (socket, "\ngetsockname:%d", s);
49 
50  if (!socket)
51  goto einval;
52 
53  sa_len = (socket->so_family == AF_INET6) ? sizeof(struct sockaddr_in6) :
54  sizeof(struct sockaddr_in);
55 
56  if (!name || !namelen || (*namelen < sa_len))
57  {
58 einval:
59  SOCK_DEBUGF ((", EINVAL"));
60  SOCK_ERRNO (EINVAL);
61  if (namelen)
62  *namelen = 0;
63  return (-1);
64  }
65 
66  if (!socket->local_addr)
67  {
68  SOCK_DEBUGF ((", EINVAL"));
69  SOCK_ERRNO (EINVAL); /* according to HP/UX manpage */
70  return (-1);
71  }
72 
73  VERIFY_RW (name, *namelen);
74 
75  *namelen = sa_len;
76  memcpy (name, socket->local_addr, sa_len);
77 
78 #if defined(USE_IPV6)
79  if (socket->so_family == AF_INET6)
80  {
81  const struct sockaddr_in6 *la = (const struct sockaddr_in6*)socket->local_addr;
82 
83  SOCK_DEBUGF ((", %s (%d)", _inet6_ntoa(&la->sin6_addr), ntohs(la->sin6_port)));
84  ARGSUSED (la);
85  }
86  else
87 #endif
88  {
89  const struct sockaddr_in *la = socket->local_addr;
90 
91  SOCK_DEBUGF ((", %s (%d)", inet_ntoa(la->sin_addr), ntohs(la->sin_port)));
92  ARGSUSED (la);
93  }
94  return (0);
95 }
96 
97 int W32_CALL getpeername (int s, struct sockaddr *name, int *namelen)
98 {
99  Socket *socket = _socklist_find (s);
100  int sa_len;
101 
102  SOCK_PROLOGUE (socket, "\ngetpeername:%d", s);
103 
104  sa_len = (socket->so_family == AF_INET6) ? sizeof(struct sockaddr_in6) :
105  sizeof(struct sockaddr_in);
106 
107  if (!name || !namelen || (*namelen < sa_len))
108  {
109  SOCK_DEBUGF ((", EINVAL"));
110  SOCK_ERRNO (EINVAL);
111  if (namelen)
112  *namelen = 0;
113  return (-1);
114  }
115 
116  if (!(socket->so_state & SS_ISCONNECTED))
117  {
118  SOCK_DEBUGF ((", ENOTCONN"));
119  SOCK_ERRNO (ENOTCONN);
120  return (-1);
121  }
122 
123  VERIFY_RW (name, *namelen);
124 
125  *namelen = sa_len;
126  memcpy (name, socket->remote_addr, *namelen);
127 
128 #if defined(USE_IPV6)
129  if (socket->so_family == AF_INET6)
130  {
131  const struct sockaddr_in6 *ra = (const struct sockaddr_in6*)socket->remote_addr;
132 
133  SOCK_DEBUGF ((", %s (%d)", _inet6_ntoa(&ra->sin6_addr), ntohs(ra->sin6_port)));
134  ARGSUSED (ra);
135  }
136  else
137 #endif
138  {
139  const struct sockaddr_in *ra = socket->remote_addr;
140 
141  SOCK_DEBUGF ((", %s (%d)", inet_ntoa(ra->sin_addr), ntohs(ra->sin_port)));
142  ARGSUSED (ra);
143  }
144  return (0);
145 }
146 #endif
Definition: socket.h:137
Definition: if.h:84
Socket * _socklist_find(int s)
Returns a pointer to the Socket structure associated with socket 's'.
Definition: socket.c:1534
const char * _inet6_ntoa(const void *ip)
Convert an IPv6-address 'ip' into a string.
Definition: netaddr.c:401