blob: 8a273f9b5e81dd2ee8ce7962e07c1725fa046082 [file] [log] [blame]
Damien Millerf4684422003-06-02 18:59:08 +10001/* This file has be substantially modified from the original OpenBSD source */
2
3/* $OpenBSD: bindresvport.c,v 1.15 2003/05/20 22:42:35 deraadt Exp $ */
Damien Miller2a5c1ce2001-01-25 10:32:00 +11004
Damien Miller34132e52000-01-14 15:45:46 +11005/*
Damien Millerf4684422003-06-02 18:59:08 +10006 * 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.
Damien Miller34132e52000-01-14 15:45:46 +110029 */
30
Ben Lindstromdd21fe92002-06-27 18:23:20 +000031#include "includes.h"
Damien Miller34132e52000-01-14 15:45:46 +110032
Damien Miller2a5c1ce2001-01-25 10:32:00 +110033#ifndef HAVE_BINDRESVPORT_SA
Damien Miller34132e52000-01-14 15:45:46 +110034
Damien Miller34132e52000-01-14 15:45:46 +110035#include "includes.h"
36
37#define STARTPORT 600
38#define ENDPORT (IPPORT_RESERVED - 1)
39#define NPORTS (ENDPORT - STARTPORT + 1)
40
Damien Miller34132e52000-01-14 15:45:46 +110041/*
42 * Bind a socket to a privileged IP port
43 */
44int
Damien Miller2a5c1ce2001-01-25 10:32:00 +110045bindresvport_sa(sd, sa)
Damien Miller34132e52000-01-14 15:45:46 +110046 int sd;
47 struct sockaddr *sa;
Damien Miller34132e52000-01-14 15:45:46 +110048{
Damien Miller2a5c1ce2001-01-25 10:32:00 +110049 int error, af;
Damien Miller34132e52000-01-14 15:45:46 +110050 struct sockaddr_storage myaddr;
51 struct sockaddr_in *sin;
52 struct sockaddr_in6 *sin6;
53 u_int16_t *portp;
Damien Miller8dbbe6e2000-01-22 18:17:42 +110054 u_int16_t port;
Ben Lindstrom0d5af602001-01-09 00:50:29 +000055 socklen_t salen;
Damien Miller34132e52000-01-14 15:45:46 +110056 int i;
57
58 if (sa == NULL) {
59 memset(&myaddr, 0, sizeof(myaddr));
60 sa = (struct sockaddr *)&myaddr;
Damien Miller2a5c1ce2001-01-25 10:32:00 +110061
62 if (getsockname(sd, sa, &salen) == -1)
63 return -1; /* errno is correctly set */
64
65 af = sa->sa_family;
66 memset(&myaddr, 0, salen);
67 } else
68 af = sa->sa_family;
Damien Miller34132e52000-01-14 15:45:46 +110069
70 if (af == AF_INET) {
71 sin = (struct sockaddr_in *)sa;
72 salen = sizeof(struct sockaddr_in);
73 portp = &sin->sin_port;
74 } else if (af == AF_INET6) {
75 sin6 = (struct sockaddr_in6 *)sa;
76 salen = sizeof(struct sockaddr_in6);
77 portp = &sin6->sin6_port;
78 } else {
79 errno = EPFNOSUPPORT;
80 return (-1);
81 }
82 sa->sa_family = af;
83
Damien Miller8dbbe6e2000-01-22 18:17:42 +110084 port = ntohs(*portp);
85 if (port == 0)
86 port = (arc4random() % NPORTS) + STARTPORT;
Damien Miller34132e52000-01-14 15:45:46 +110087
Damien Millera64b57a2001-01-17 10:44:13 +110088 /* Avoid warning */
89 error = -1;
90
Damien Miller34132e52000-01-14 15:45:46 +110091 for(i = 0; i < NPORTS; i++) {
Damien Miller8dbbe6e2000-01-22 18:17:42 +110092 *portp = htons(port);
93
Damien Miller34132e52000-01-14 15:45:46 +110094 error = bind(sd, sa, salen);
Damien Miller2a5c1ce2001-01-25 10:32:00 +110095
Damien Millerb9b94a72000-01-17 09:52:46 +110096 /* Terminate on success */
97 if (error == 0)
Damien Miller34132e52000-01-14 15:45:46 +110098 break;
99
Damien Millerb9b94a72000-01-17 09:52:46 +1100100 /* Terminate on errors, except "address already in use" */
Damien Miller19fe9c72000-01-17 15:23:01 +1100101 if ((error < 0) && !((errno == EADDRINUSE) || (errno == EINVAL)))
Damien Millerb9b94a72000-01-17 09:52:46 +1100102 break;
103
Damien Miller8dbbe6e2000-01-22 18:17:42 +1100104 port++;
105 if (port > ENDPORT)
106 port = STARTPORT;
Damien Miller34132e52000-01-14 15:45:46 +1100107 }
108
109 return (error);
110}
111
Damien Miller2a5c1ce2001-01-25 10:32:00 +1100112#endif /* HAVE_BINDRESVPORT_SA */