1666aa9cdes/* $OpenBSD: rresvport.c,v 1.9 2005/11/10 10:00:17 espie Exp $ */
2666aa9cdes/*
3666aa9cdes * Copyright (c) 1995, 1996, 1998 Theo de Raadt.  All rights reserved.
4666aa9cdes * Copyright (c) 1983, 1993, 1994
5666aa9cdes *	The Regents of the University of California.  All rights reserved.
6666aa9cdes *
7666aa9cdes * Redistribution and use in source and binary forms, with or without
8666aa9cdes * modification, are permitted provided that the following conditions
9666aa9cdes * are met:
10666aa9cdes * 1. Redistributions of source code must retain the above copyright
11666aa9cdes *    notice, this list of conditions and the following disclaimer.
12666aa9cdes * 2. Redistributions in binary form must reproduce the above copyright
13666aa9cdes *    notice, this list of conditions and the following disclaimer in the
14666aa9cdes *    documentation and/or other materials provided with the distribution.
15666aa9cdes * 3. Neither the name of the University nor the names of its contributors
16666aa9cdes *    may be used to endorse or promote products derived from this software
17666aa9cdes *    without specific prior written permission.
18666aa9cdes *
19666aa9cdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20666aa9cdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21666aa9cdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22666aa9cdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23666aa9cdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24666aa9cdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25666aa9cdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26666aa9cdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27666aa9cdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28666aa9cdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29666aa9cdes * SUCH DAMAGE.
30666aa9cdes */
31666aa9cdes
32666aa9cdes/* OPENBSD ORIGINAL: lib/libc/net/rresvport.c */
33666aa9cdes
34666aa9cdes#include "includes.h"
35666aa9cdes
36666aa9cdes#ifndef HAVE_RRESVPORT_AF
37666aa9cdes
38666aa9cdes#include <sys/types.h>
39666aa9cdes#include <sys/socket.h>
40666aa9cdes
41666aa9cdes#include <netinet/in.h>
42666aa9cdes#include <arpa/inet.h>
43666aa9cdes
44666aa9cdes#include <errno.h>
45666aa9cdes#include <stdlib.h>
46666aa9cdes#include <string.h>
47f3e1deddes#include <unistd.h>
48666aa9cdes
49666aa9cdes#if 0
50666aa9cdesint
51666aa9cdesrresvport(int *alport)
52666aa9cdes{
53666aa9cdes	return rresvport_af(alport, AF_INET);
54666aa9cdes}
55666aa9cdes#endif
56666aa9cdes
57666aa9cdesint
58666aa9cdesrresvport_af(int *alport, sa_family_t af)
59666aa9cdes{
60666aa9cdes	struct sockaddr_storage ss;
61666aa9cdes	struct sockaddr *sa;
62666aa9cdes	u_int16_t *portp;
63666aa9cdes	int s;
64666aa9cdes	socklen_t salen;
65666aa9cdes
66666aa9cdes	memset(&ss, '\0', sizeof ss);
67666aa9cdes	sa = (struct sockaddr *)&ss;
68666aa9cdes
69666aa9cdes	switch (af) {
70666aa9cdes	case AF_INET:
71666aa9cdes		salen = sizeof(struct sockaddr_in);
72666aa9cdes		portp = &((struct sockaddr_in *)sa)->sin_port;
73666aa9cdes		break;
74666aa9cdes	case AF_INET6:
75666aa9cdes		salen = sizeof(struct sockaddr_in6);
76666aa9cdes		portp = &((struct sockaddr_in6 *)sa)->sin6_port;
77666aa9cdes		break;
78666aa9cdes	default:
79666aa9cdes		errno = EPFNOSUPPORT;
80666aa9cdes		return (-1);
81666aa9cdes	}
82666aa9cdes	sa->sa_family = af;
83666aa9cdes
84666aa9cdes	s = socket(af, SOCK_STREAM, 0);
85666aa9cdes	if (s < 0)
86666aa9cdes		return (-1);
87666aa9cdes
88666aa9cdes	*portp = htons(*alport);
89666aa9cdes	if (*alport < IPPORT_RESERVED - 1) {
90666aa9cdes		if (bind(s, sa, salen) >= 0)
91666aa9cdes			return (s);
92666aa9cdes		if (errno != EADDRINUSE) {
93666aa9cdes			(void)close(s);
94666aa9cdes			return (-1);
95666aa9cdes		}
96666aa9cdes	}
97666aa9cdes
98666aa9cdes	*portp = 0;
99666aa9cdes	sa->sa_family = af;
100666aa9cdes	if (bindresvport_sa(s, sa) == -1) {
101666aa9cdes		(void)close(s);
102666aa9cdes		return (-1);
103666aa9cdes	}
104666aa9cdes	*alport = ntohs(*portp);
105666aa9cdes	return (s);
106666aa9cdes}
107666aa9cdes
108666aa9cdes#endif /* HAVE_RRESVPORT_AF */
109