ReactOS 0.4.15-dev-7842-g558ab78
bindresvport.c File Reference
#include <wintirpc.h>
#include <sys/types.h>
#include <errno.h>
#include <string.h>
#include <rpc/rpc.h>
Include dependency graph for bindresvport.c:

Go to the source code of this file.

Macros

#define IP_PORTRANGE   19
 
#define IP_PORTRANGE_LOW   2
 

Functions

int bindresvport (SOCKET sd, struct sockaddr_in *sin)
 
int bindresvport_sa (SOCKET sd, struct sockaddr *sa)
 

Macro Definition Documentation

◆ IP_PORTRANGE

#define IP_PORTRANGE   19

Definition at line 154 of file bindresvport.c.

◆ IP_PORTRANGE_LOW

#define IP_PORTRANGE_LOW   2

Definition at line 155 of file bindresvport.c.

Function Documentation

◆ bindresvport()

int bindresvport ( SOCKET  sd,
struct sockaddr_in sin 
)

Definition at line 53 of file bindresvport.c.

56{
57 return bindresvport_sa(sd, (struct sockaddr *)sin);
58}
_STLP_DECLSPEC complex< float > _STLP_CALL sin(const complex< float > &)
int bindresvport_sa(SOCKET sd, struct sockaddr *sa)
Definition: bindresvport.c:161
static const WCHAR sd[]
Definition: suminfo.c:286

Referenced by clnt_tli_create(), and svc_tli_create().

◆ bindresvport_sa()

int bindresvport_sa ( SOCKET  sd,
struct sockaddr sa 
)

Definition at line 161 of file bindresvport.c.

164{
165#ifdef IPV6_PORTRANGE
166 int old;
167#endif
168 int error, af;
169 struct sockaddr_storage myaddr;
170 struct sockaddr_in *sin;
171#ifdef INET6
172 struct sockaddr_in6 *sin6;
173#endif
174 int proto, portrange, portlow;
175 u_int16_t *portp;
176 socklen_t salen;
177#ifdef _WIN32
178 WSAPROTOCOL_INFO proto_info;
179 int proto_info_size = sizeof(proto_info);
180#endif
181
182 if (sa == NULL) {
183 salen = sizeof(myaddr);
184 sa = (struct sockaddr *)&myaddr;
185
186#ifdef _WIN32
187 memset(sa, 0, salen);
188 if (error = getsockopt(sd, SOL_SOCKET, SO_PROTOCOL_INFO, (char *)&proto_info, &proto_info_size) == SOCKET_ERROR) {
189#ifndef __REACTOS__
190 int sockerr = WSAGetLastError();
191#endif
192 return -1;
193 }
194 af = proto_info.iAddressFamily;
195#else
196 if (getsockname(sd, sa, &salen) == -1)
197 return -1; /* errno is correctly set */
198
199 af = sa->sa_family;
200 memset(sa, 0, salen);
201#endif
202 } else
203 af = sa->sa_family;
204
205 switch (af) {
206 case AF_INET:
208 portrange = IP_PORTRANGE;
209 portlow = IP_PORTRANGE_LOW;
210 sin = (struct sockaddr_in *)sa;
211 salen = sizeof(struct sockaddr_in);
212 portp = &sin->sin_port;
213 break;
214#ifdef INET6
215 case AF_INET6:
216 proto = IPPROTO_IPV6;
217#ifdef IPV6_PORTRANGE
218 portrange = IPV6_PORTRANGE;
219 portlow = IPV6_PORTRANGE_LOW;
220#endif
221 sin6 = (struct sockaddr_in6 *)sa;
222 salen = sizeof(struct sockaddr_in6);
223 portp = &sin6->sin6_port;
224 break;
225#endif /* INET6 */
226 default:
228 return (-1);
229 }
230 sa->sa_family = (ADDRESS_FAMILY) af;
231
232#ifdef IPV6_PORTRANGE
233 if (*portp == 0) {
234 socklen_t oldlen = sizeof(old);
235
236 error = getsockopt(sd, proto, portrange, &old, &oldlen);
237 if (error < 0)
238 return (error);
239
240 error = setsockopt(sd, proto, portrange, &portlow,
241 sizeof(portlow));
242 if (error < 0)
243 return (error);
244 }
245#endif
246
247 error = bind(sd, sa, salen);
248 if (error) {
249#ifndef __REACTOS__
250 int err = WSAGetLastError();
251#endif
252 }
253
254#ifdef IPV6_PORTRANGE
255 if (*portp == 0) {
256 int saved_errno = errno;
257
258 if (error < 0) {
259 if (setsockopt(sd, proto, portrange, &old,
260 sizeof(old)) < 0)
261 errno = saved_errno;
262 return (error);
263 }
264
265 if (sa != (struct sockaddr *)&myaddr) {
266 /* Hmm, what did the kernel assign? */
267 if (getsockname(sd, sa, &salen) < 0)
268 errno = saved_errno;
269 return (error);
270 }
271 }
272#endif
273 return (error);
274}
static struct sockaddr_in sa
Definition: adnsresfilter.c:69
#define IP_PORTRANGE
Definition: bindresvport.c:154
#define IP_PORTRANGE_LOW
Definition: bindresvport.c:155
#define NULL
Definition: types.h:112
#define AF_INET
Definition: tcpip.h:117
#define error(str)
Definition: mkdosfs.c:1605
int socklen_t
Definition: tcp.c:35
#define err(...)
unsigned short u_int16_t
Definition: rosdhcp.h:34
#define errno
Definition: errno.h:18
#define memset(x, y, z)
Definition: compat.h:39
INT WSAAPI getsockname(IN SOCKET s, OUT LPSOCKADDR name, IN OUT INT FAR *namelen)
Definition: sockctrl.c:213
INT WSAAPI setsockopt(IN SOCKET s, IN INT level, IN INT optname, IN CONST CHAR FAR *optval, IN INT optlen)
Definition: sockctrl.c:421
INT WSAAPI getsockopt(IN SOCKET s, IN INT level, IN INT optname, OUT CHAR FAR *optval, IN OUT INT FAR *optlen)
Definition: sockctrl.c:271
INT WSAAPI bind(IN SOCKET s, IN CONST struct sockaddr *name, IN INT namelen)
Definition: socklife.c:36
USHORT sin6_port
Definition: ws2ipdef.h:180
#define WSAEPFNOSUPPORT
Definition: winerror.h:1959
#define SO_PROTOCOL_INFO
Definition: winsock2.h:253
int PASCAL FAR WSAGetLastError(void)
Definition: dllmain.c:112
#define SOCKET_ERROR
Definition: winsock.h:333
#define SOL_SOCKET
Definition: winsock.h:398
#define AF_INET6
Definition: winsock.h:369
#define IPPROTO_IP
Definition: winsock.h:255
USHORT ADDRESS_FAMILY
Definition: ws2def.h:25

Referenced by bindresvport(), and clnt_dg_create().