bindresvport.c revision 2
2N/A * The contents of this file are subject to the terms of the 2N/A * Common Development and Distribution License (the "License"). 2N/A * You may not use this file except in compliance with the License. 2N/A * See the License for the specific language governing permissions 2N/A * and limitations under the License. 2N/A * When distributing Covered Code, include this CDDL HEADER in each 2N/A * If applicable, add the following below this CDDL HEADER, with the 2N/A * fields enclosed by brackets "[]" replaced with your own identifying 2N/A * information: Portions Copyright [yyyy] [name of copyright owner] 2N/A * Copyright (c) 1994, 2011, Oracle and/or its affiliates. All rights reserved. 2N/A/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 2N/A/* All Rights Reserved */ 2N/A * Portions of this source code were derived from Berkeley 4.3 BSD 2N/A * under license from the Regents of the University of California. 2N/A * XXX This routine should be changed to use 2N/A * ND_CHECK_RESERVED_PORT and ND_SET_RESERVED_PORT 2N/A * which can be invoked via netdir_options. 2N/A * The argument is a client handle for a UDP connection. 2N/A * Unbind its transport endpoint from the existing port 2N/A * and rebind it to a reserved port. 2N/A * On failure, the client handle can be unbound even if it 2N/A * was previously bound. Callers should destroy the client 2N/A * handle after a failure. 2N/A /* extern int t_errno; */ 2N/A /* make sure it's a UDP connection */ 2N/A return (0);
/* not udp - don't need resv port */ 2N/A /* If fd is already bound - unbind it */ 2N/A * If there is a message queued to this descriptor, 2N/A /* warning: this sockaddr_in is truncated to 8 bytes */ 2N/A * Need to find a reserved port in the interval 2N/A * STARTPORT - ENDPORT. Choose a random starting 2N/A * place in the interval based on the process pid 2N/A * and sequentially search the ports for one 2N/A * that is available. 2N/A * Try to bind to the requested address. If 2N/A * the call to t_bind succeeds, then we need 2N/A * to make sure that the address that we bound 2N/A * to was the address that we requested. If it 2N/A * was, then we are done. If not, we fake an 2N/A * EADDRINUSE error by setting res, t_errno, 2N/A * and errno to indicate that a bind failure 2N/A * occurred. Otherwise, if the t_bind call 2N/A * failed, we check to see whether it makes 2N/A * sense to continue trying to t_bind requests.