]> andersk Git - openssh.git/blame - openbsd-compat/bindresvport.c
- (dtucker) [configure.ac includes.h openbsd-compat/glob.{c,h}] Explicitly
[openssh.git] / openbsd-compat / bindresvport.c
CommitLineData
382fe2fa 1/* This file has be substantially modified from the original OpenBSD source */
2
295034ce 3/* $OpenBSD: bindresvport.c,v 1.16 2005/04/01 07:44:03 otto Exp $ */
d7d5f0b2 4
48e671d5 5/*
382fe2fa 6 * Copyright 1996, Jason Downs. All rights reserved.
7 * Copyright 1998, Theo de Raadt. All rights reserved.
8 * Copyright 2000, Damien Miller. All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48e671d5 29 */
30
c9d7b187 31/* OPENBSD ORIGINAL: lib/libc/rpc/bindresvport.c */
32
17962c40 33#include "includes.h"
48e671d5 34
d7d5f0b2 35#ifndef HAVE_BINDRESVPORT_SA
aa751414 36#include <sys/types.h>
37#include <sys/socket.h>
48e671d5 38
aa751414 39#include <netinet/in.h>
48e671d5 40
fec71b2f 41#include <errno.h>
28cb0a43 42#include <string.h>
fec71b2f 43
48e671d5 44#define STARTPORT 600
45#define ENDPORT (IPPORT_RESERVED - 1)
46#define NPORTS (ENDPORT - STARTPORT + 1)
47
48e671d5 48/*
49 * Bind a socket to a privileged IP port
50 */
51int
295034ce 52bindresvport_sa(int sd, struct sockaddr *sa)
48e671d5 53{
d7d5f0b2 54 int error, af;
48e671d5 55 struct sockaddr_storage myaddr;
56 struct sockaddr_in *sin;
57 struct sockaddr_in6 *sin6;
58 u_int16_t *portp;
1e64903d 59 u_int16_t port;
f1c4659d 60 socklen_t salen;
48e671d5 61 int i;
62
63 if (sa == NULL) {
64 memset(&myaddr, 0, sizeof(myaddr));
65 sa = (struct sockaddr *)&myaddr;
d7d5f0b2 66
67 if (getsockname(sd, sa, &salen) == -1)
68 return -1; /* errno is correctly set */
69
70 af = sa->sa_family;
71 memset(&myaddr, 0, salen);
72 } else
73 af = sa->sa_family;
48e671d5 74
75 if (af == AF_INET) {
76 sin = (struct sockaddr_in *)sa;
77 salen = sizeof(struct sockaddr_in);
78 portp = &sin->sin_port;
79 } else if (af == AF_INET6) {
80 sin6 = (struct sockaddr_in6 *)sa;
81 salen = sizeof(struct sockaddr_in6);
82 portp = &sin6->sin6_port;
83 } else {
84 errno = EPFNOSUPPORT;
85 return (-1);
86 }
87 sa->sa_family = af;
88
1e64903d 89 port = ntohs(*portp);
90 if (port == 0)
91 port = (arc4random() % NPORTS) + STARTPORT;
48e671d5 92
717057b6 93 /* Avoid warning */
94 error = -1;
95
48e671d5 96 for(i = 0; i < NPORTS; i++) {
1e64903d 97 *portp = htons(port);
98
48e671d5 99 error = bind(sd, sa, salen);
d7d5f0b2 100
f914c7fb 101 /* Terminate on success */
102 if (error == 0)
103 break;
104
105 /* Terminate on errors, except "address already in use" */
f4a7cf29 106 if ((error < 0) && !((errno == EADDRINUSE) || (errno == EINVAL)))
48e671d5 107 break;
108
1e64903d 109 port++;
110 if (port > ENDPORT)
111 port = STARTPORT;
48e671d5 112 }
113
114 return (error);
115}
116
d7d5f0b2 117#endif /* HAVE_BINDRESVPORT_SA */
This page took 0.295164 seconds and 5 git commands to generate.