2003-06-02 08:59:08 +00:00
|
|
|
/* This file has be substantially modified from the original OpenBSD source */
|
|
|
|
|
2007-10-26 06:16:09 +00:00
|
|
|
/* $OpenBSD: bindresvport.c,v 1.17 2005/12/21 01:40:22 millert Exp $ */
|
2001-01-24 23:32:00 +00:00
|
|
|
|
2000-01-14 04:45:46 +00:00
|
|
|
/*
|
2003-06-02 08:59:08 +00:00
|
|
|
* Copyright 1996, Jason Downs. All rights reserved.
|
|
|
|
* Copyright 1998, Theo de Raadt. All rights reserved.
|
|
|
|
* Copyright 2000, Damien Miller. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2000-01-14 04:45:46 +00:00
|
|
|
*/
|
|
|
|
|
2005-11-10 06:52:08 +00:00
|
|
|
/* OPENBSD ORIGINAL: lib/libc/rpc/bindresvport.c */
|
|
|
|
|
2002-06-27 18:23:20 +00:00
|
|
|
#include "includes.h"
|
2000-01-14 04:45:46 +00:00
|
|
|
|
2001-01-24 23:32:00 +00:00
|
|
|
#ifndef HAVE_BINDRESVPORT_SA
|
2006-09-01 05:48:19 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2000-01-14 04:45:46 +00:00
|
|
|
|
2006-09-01 05:48:19 +00:00
|
|
|
#include <netinet/in.h>
|
2006-09-02 05:32:40 +00:00
|
|
|
#include <arpa/inet.h>
|
2000-01-14 04:45:46 +00:00
|
|
|
|
2006-07-12 12:40:50 +00:00
|
|
|
#include <errno.h>
|
2021-09-29 10:03:58 +00:00
|
|
|
#include <stdlib.h>
|
2006-07-24 04:51:00 +00:00
|
|
|
#include <string.h>
|
2006-07-12 12:40:50 +00:00
|
|
|
|
2000-01-14 04:45:46 +00:00
|
|
|
#define STARTPORT 600
|
|
|
|
#define ENDPORT (IPPORT_RESERVED - 1)
|
|
|
|
#define NPORTS (ENDPORT - STARTPORT + 1)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bind a socket to a privileged IP port
|
|
|
|
*/
|
|
|
|
int
|
2005-11-10 06:54:46 +00:00
|
|
|
bindresvport_sa(int sd, struct sockaddr *sa)
|
2000-01-14 04:45:46 +00:00
|
|
|
{
|
2001-01-24 23:32:00 +00:00
|
|
|
int error, af;
|
2000-01-14 04:45:46 +00:00
|
|
|
struct sockaddr_storage myaddr;
|
2008-07-14 02:02:24 +00:00
|
|
|
struct sockaddr_in *in;
|
|
|
|
struct sockaddr_in6 *in6;
|
2000-01-14 04:45:46 +00:00
|
|
|
u_int16_t *portp;
|
2000-01-22 07:17:42 +00:00
|
|
|
u_int16_t port;
|
2001-01-09 00:50:29 +00:00
|
|
|
socklen_t salen;
|
2000-01-14 04:45:46 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (sa == NULL) {
|
|
|
|
memset(&myaddr, 0, sizeof(myaddr));
|
|
|
|
sa = (struct sockaddr *)&myaddr;
|
2016-05-09 23:51:06 +00:00
|
|
|
salen = sizeof(myaddr);
|
2001-01-24 23:32:00 +00:00
|
|
|
|
|
|
|
if (getsockname(sd, sa, &salen) == -1)
|
|
|
|
return -1; /* errno is correctly set */
|
|
|
|
|
|
|
|
af = sa->sa_family;
|
|
|
|
memset(&myaddr, 0, salen);
|
|
|
|
} else
|
|
|
|
af = sa->sa_family;
|
2000-01-14 04:45:46 +00:00
|
|
|
|
|
|
|
if (af == AF_INET) {
|
2008-07-14 02:02:24 +00:00
|
|
|
in = (struct sockaddr_in *)sa;
|
2000-01-14 04:45:46 +00:00
|
|
|
salen = sizeof(struct sockaddr_in);
|
2008-07-14 02:02:24 +00:00
|
|
|
portp = &in->sin_port;
|
2000-01-14 04:45:46 +00:00
|
|
|
} else if (af == AF_INET6) {
|
2008-07-14 02:02:24 +00:00
|
|
|
in6 = (struct sockaddr_in6 *)sa;
|
2000-01-14 04:45:46 +00:00
|
|
|
salen = sizeof(struct sockaddr_in6);
|
2008-07-14 02:02:24 +00:00
|
|
|
portp = &in6->sin6_port;
|
2000-01-14 04:45:46 +00:00
|
|
|
} else {
|
|
|
|
errno = EPFNOSUPPORT;
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
sa->sa_family = af;
|
|
|
|
|
2000-01-22 07:17:42 +00:00
|
|
|
port = ntohs(*portp);
|
|
|
|
if (port == 0)
|
2010-12-02 23:50:26 +00:00
|
|
|
port = arc4random_uniform(NPORTS) + STARTPORT;
|
2000-01-14 04:45:46 +00:00
|
|
|
|
2001-01-16 23:44:13 +00:00
|
|
|
/* Avoid warning */
|
|
|
|
error = -1;
|
|
|
|
|
2000-01-14 04:45:46 +00:00
|
|
|
for(i = 0; i < NPORTS; i++) {
|
2000-01-22 07:17:42 +00:00
|
|
|
*portp = htons(port);
|
|
|
|
|
2000-01-14 04:45:46 +00:00
|
|
|
error = bind(sd, sa, salen);
|
2001-01-24 23:32:00 +00:00
|
|
|
|
2000-01-16 22:52:46 +00:00
|
|
|
/* Terminate on success */
|
|
|
|
if (error == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Terminate on errors, except "address already in use" */
|
2000-01-17 04:23:01 +00:00
|
|
|
if ((error < 0) && !((errno == EADDRINUSE) || (errno == EINVAL)))
|
2000-01-14 04:45:46 +00:00
|
|
|
break;
|
|
|
|
|
2000-01-22 07:17:42 +00:00
|
|
|
port++;
|
|
|
|
if (port > ENDPORT)
|
|
|
|
port = STARTPORT;
|
2000-01-14 04:45:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2001-01-24 23:32:00 +00:00
|
|
|
#endif /* HAVE_BINDRESVPORT_SA */
|