summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTheo de Raadt <deraadt@cvs.openbsd.org>2000-01-26 03:43:22 +0000
committerTheo de Raadt <deraadt@cvs.openbsd.org>2000-01-26 03:43:22 +0000
commit63ad0684f59475bf125674e96ca5ce50525627c4 (patch)
tree0af35de0f040277833ca21a4e49dbace7be71f23
parent868ca49b24dfbc5fabc8ae0b15115e57f0a2d555 (diff)
new bindresvport() semantics that itojun, shin, jean-luc and i have agreed on, which will be happy for the future. bindresvport_sa() for sockaddr *, too. docs later..
-rw-r--r--lib/libc/net/rresvport.c5
-rw-r--r--lib/libc/rpc/bindresvport.c38
-rw-r--r--sys/netinet/in.h4
3 files changed, 26 insertions, 21 deletions
diff --git a/lib/libc/net/rresvport.c b/lib/libc/net/rresvport.c
index 21387aa3b13..c807adfb83e 100644
--- a/lib/libc/net/rresvport.c
+++ b/lib/libc/net/rresvport.c
@@ -34,7 +34,7 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
-static char *rcsid = "$OpenBSD: rresvport.c,v 1.4 1999/12/17 20:48:03 deraadt Exp $";
+static char *rcsid = "$OpenBSD: rresvport.c,v 1.5 2000/01/26 03:43:20 deraadt Exp $";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
@@ -108,7 +108,8 @@ rresvport_af(alport, af)
}
*portp = 0;
- if (bindresvport_af(s, sa, af) == -1) {
+ sa->sa_family = af;
+ if (bindresvport_sa(s, sa) == -1) {
(void)close(s);
return (-1);
}
diff --git a/lib/libc/rpc/bindresvport.c b/lib/libc/rpc/bindresvport.c
index 37789e9d786..02a8857af9c 100644
--- a/lib/libc/rpc/bindresvport.c
+++ b/lib/libc/rpc/bindresvport.c
@@ -28,7 +28,7 @@
*/
#if defined(LIBC_SCCS) && !defined(lint)
-static char *rcsid = "$OpenBSD: bindresvport.c,v 1.12 2000/01/24 02:24:21 deraadt Exp $";
+static char *rcsid = "$OpenBSD: bindresvport.c,v 1.13 2000/01/26 03:43:21 deraadt Exp $";
#endif /* LIBC_SCCS and not lint */
/*
@@ -51,32 +51,36 @@ bindresvport(sd, sin)
int sd;
struct sockaddr_in *sin;
{
- if (sin)
- return bindresvport_af(sd, (struct sockaddr *)sin, sin->sin_family);
- return bindresvport_af(sd, NULL, AF_INET);
+ return bindresvport_sa(sd, (struct sockaddr *)sin);
}
/*
- * Bind a socket to a privileged IP port
+ * Bind a socket to a privileged port for whatever protocol.
*/
int
-bindresvport_af(sd, sa, af)
+bindresvport_sa(sd, sa)
int sd;
struct sockaddr *sa;
- int af;
{
- int old, error;
+ int old, error, af;
struct sockaddr_storage myaddr;
struct sockaddr_in *sin;
struct sockaddr_in6 *sin6;
int proto, portrange, portlow;
- u_int16_t *portp;
+ u_int16_t port;
int salen;
if (sa == NULL) {
- memset(&myaddr, 0, sizeof(myaddr));
+ salen = sizeof(myaddr);
sa = (struct sockaddr *)&myaddr;
- }
+
+ 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;
if (af == AF_INET) {
proto = IPPROTO_IP;
@@ -84,14 +88,14 @@ bindresvport_af(sd, sa, af)
portlow = IP_PORTRANGE_LOW;
sin = (struct sockaddr_in *)sa;
salen = sizeof(struct sockaddr_in);
- portp = &sin->sin_port;
+ port = sin->sin_port;
} else if (af == AF_INET6) {
proto = IPPROTO_IPV6;
portrange = IPV6_PORTRANGE;
portlow = IPV6_PORTRANGE_LOW;
sin6 = (struct sockaddr_in6 *)sa;
salen = sizeof(struct sockaddr_in6);
- portp = &sin6->sin6_port;
+ port = sin6->sin6_port;
} else {
errno = EPFNOSUPPORT;
return (-1);
@@ -99,22 +103,22 @@ bindresvport_af(sd, sa, af)
sa->sa_family = af;
sa->sa_len = salen;
- if (*portp == 0) {
+ if (port == 0) {
int oldlen = sizeof(old);
error = getsockopt(sd, proto, portrange, &old, &oldlen);
if (error < 0)
- return(error);
+ return (error);
error = setsockopt(sd, proto, portrange, &portlow,
sizeof(portlow));
if (error < 0)
- return(error);
+ return (error);
}
error = bind(sd, sa, salen);
- if (*portp == 0) {
+ if (port == 0) {
int saved_errno = errno;
if (error) {
diff --git a/sys/netinet/in.h b/sys/netinet/in.h
index d5562203bed..e57cd11fece 100644
--- a/sys/netinet/in.h
+++ b/sys/netinet/in.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: in.h,v 1.36 2000/01/21 03:15:04 angelos Exp $ */
+/* $OpenBSD: in.h,v 1.37 2000/01/26 03:43:17 deraadt Exp $ */
/* $NetBSD: in.h,v 1.20 1996/02/13 23:41:47 christos Exp $ */
/*
@@ -487,7 +487,7 @@ struct ip_mreq {
__BEGIN_DECLS
int bindresvport __P((int, struct sockaddr_in *));
struct sockaddr;
-int bindresvport_af __P((int, struct sockaddr *, int af));
+int bindresvport_sa __P((int, struct sockaddr *));
__END_DECLS
#else