1 /* $OpenBSD: bindresvport.c,v 1.15 2003/05/20 22:42:35 deraadt Exp $ */ 2 3 /* 4 * Copyright 1996, Jason Downs. All rights reserved. 5 * Copyright 1998, Theo de Raadt. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <string.h> 29 #include <sys/types.h> 30 #include <sys/errno.h> 31 #include <sys/socket.h> 32 #include <netinet/in.h> 33 34 /* 35 * Bind a socket to a privileged IP port 36 */ 37 int 38 bindresvport(sd, sin) 39 int sd; 40 struct sockaddr_in *sin; 41 { 42 return bindresvport_sa(sd, (struct sockaddr *)sin); 43 } 44 45 /* 46 * Bind a socket to a privileged port for whatever protocol. 47 */ 48 int 49 bindresvport_sa(sd, sa) 50 int sd; 51 struct sockaddr *sa; 52 { 53 int old, error, af; 54 struct sockaddr_storage myaddr; 55 struct sockaddr_in *sin; 56 struct sockaddr_in6 *sin6; 57 int proto, portrange, portlow; 58 u_int16_t port; 59 socklen_t salen; 60 61 if (sa == NULL) { 62 salen = sizeof(myaddr); 63 sa = (struct sockaddr *)&myaddr; 64 65 if (getsockname(sd, sa, &salen) == -1) 66 return -1; /* errno is correctly set */ 67 68 af = sa->sa_family; 69 memset(&myaddr, 0, salen); 70 } else 71 af = sa->sa_family; 72 73 if (af == AF_INET) { 74 proto = IPPROTO_IP; 75 portrange = IP_PORTRANGE; 76 portlow = IP_PORTRANGE_LOW; 77 sin = (struct sockaddr_in *)sa; 78 salen = sizeof(struct sockaddr_in); 79 port = sin->sin_port; 80 } else if (af == AF_INET6) { 81 proto = IPPROTO_IPV6; 82 portrange = IPV6_PORTRANGE; 83 portlow = IPV6_PORTRANGE_LOW; 84 sin6 = (struct sockaddr_in6 *)sa; 85 salen = sizeof(struct sockaddr_in6); 86 port = sin6->sin6_port; 87 } else { 88 errno = EPFNOSUPPORT; 89 return (-1); 90 } 91 sa->sa_family = af; 92 sa->sa_len = salen; 93 94 if (port == 0) { 95 socklen_t oldlen = sizeof(old); 96 97 error = getsockopt(sd, proto, portrange, &old, &oldlen); 98 if (error < 0) 99 return (error); 100 101 error = setsockopt(sd, proto, portrange, &portlow, 102 sizeof(portlow)); 103 if (error < 0) 104 return (error); 105 } 106 107 error = bind(sd, sa, salen); 108 109 if (port == 0) { 110 int saved_errno = errno; 111 112 if (error) { 113 if (setsockopt(sd, proto, portrange, &old, 114 sizeof(old)) < 0) 115 errno = saved_errno; 116 return (error); 117 } 118 119 if (sa != (struct sockaddr *)&myaddr) { 120 /* Hmm, what did the kernel assign... */ 121 if (getsockname(sd, sa, &salen) < 0) 122 errno = saved_errno; 123 return (error); 124 } 125 } 126 return (error); 127 } 128