xref: /freebsd/lib/libc/inet/inet_makeaddr.c (revision ab96eeabe8f319206346df2f486e01caabbfd8f4)
165e96449SHajimu UMEMOTO /*
265e96449SHajimu UMEMOTO  * Copyright (c) 1983, 1993
365e96449SHajimu UMEMOTO  *	The Regents of the University of California.  All rights reserved.
465e96449SHajimu UMEMOTO  *
565e96449SHajimu UMEMOTO  * Redistribution and use in source and binary forms, with or without
665e96449SHajimu UMEMOTO  * modification, are permitted provided that the following conditions
765e96449SHajimu UMEMOTO  * are met:
865e96449SHajimu UMEMOTO  * 1. Redistributions of source code must retain the above copyright
965e96449SHajimu UMEMOTO  *    notice, this list of conditions and the following disclaimer.
1065e96449SHajimu UMEMOTO  * 2. Redistributions in binary form must reproduce the above copyright
1165e96449SHajimu UMEMOTO  *    notice, this list of conditions and the following disclaimer in the
1265e96449SHajimu UMEMOTO  *    documentation and/or other materials provided with the distribution.
1365e96449SHajimu UMEMOTO  * 3. All advertising materials mentioning features or use of this software
1465e96449SHajimu UMEMOTO  *    must display the following acknowledgement:
1565e96449SHajimu UMEMOTO  *	This product includes software developed by the University of
1665e96449SHajimu UMEMOTO  *	California, Berkeley and its contributors.
1765e96449SHajimu UMEMOTO  * 4. Neither the name of the University nor the names of its contributors
1865e96449SHajimu UMEMOTO  *    may be used to endorse or promote products derived from this software
1965e96449SHajimu UMEMOTO  *    without specific prior written permission.
2065e96449SHajimu UMEMOTO  *
2165e96449SHajimu UMEMOTO  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2265e96449SHajimu UMEMOTO  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2365e96449SHajimu UMEMOTO  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2465e96449SHajimu UMEMOTO  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2565e96449SHajimu UMEMOTO  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2665e96449SHajimu UMEMOTO  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2765e96449SHajimu UMEMOTO  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2865e96449SHajimu UMEMOTO  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2965e96449SHajimu UMEMOTO  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3065e96449SHajimu UMEMOTO  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3165e96449SHajimu UMEMOTO  * SUCH DAMAGE.
3265e96449SHajimu UMEMOTO  */
3365e96449SHajimu UMEMOTO 
3465e96449SHajimu UMEMOTO #if defined(LIBC_SCCS) && !defined(lint)
3565e96449SHajimu UMEMOTO static const char sccsid[] = "@(#)inet_makeaddr.c	8.1 (Berkeley) 6/4/93";
3665e96449SHajimu UMEMOTO #endif /* LIBC_SCCS and not lint */
37ab96eeabSHajimu UMEMOTO #include <sys/cdefs.h>
38ab96eeabSHajimu UMEMOTO __FBSDID("$FreeBSD$");
3965e96449SHajimu UMEMOTO 
4065e96449SHajimu UMEMOTO #include "port_before.h"
4165e96449SHajimu UMEMOTO 
4265e96449SHajimu UMEMOTO #include <sys/param.h>
4365e96449SHajimu UMEMOTO #include <netinet/in.h>
4465e96449SHajimu UMEMOTO #include <arpa/inet.h>
4565e96449SHajimu UMEMOTO 
4665e96449SHajimu UMEMOTO #include "port_after.h"
4765e96449SHajimu UMEMOTO 
4865e96449SHajimu UMEMOTO /*
4965e96449SHajimu UMEMOTO  * Formulate an Internet address from network + host.  Used in
5065e96449SHajimu UMEMOTO  * building addresses stored in the ifnet structure.
5165e96449SHajimu UMEMOTO  */
5265e96449SHajimu UMEMOTO struct in_addr
5365e96449SHajimu UMEMOTO inet_makeaddr(net, host)
54ab96eeabSHajimu UMEMOTO 	in_addr_t net, host;
5565e96449SHajimu UMEMOTO {
5665e96449SHajimu UMEMOTO 	struct in_addr a;
5765e96449SHajimu UMEMOTO 
5865e96449SHajimu UMEMOTO 	if (net < 128U)
5965e96449SHajimu UMEMOTO 		a.s_addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST);
6065e96449SHajimu UMEMOTO 	else if (net < 65536U)
6165e96449SHajimu UMEMOTO 		a.s_addr = (net << IN_CLASSB_NSHIFT) | (host & IN_CLASSB_HOST);
6265e96449SHajimu UMEMOTO 	else if (net < 16777216L)
6365e96449SHajimu UMEMOTO 		a.s_addr = (net << IN_CLASSC_NSHIFT) | (host & IN_CLASSC_HOST);
6465e96449SHajimu UMEMOTO 	else
6565e96449SHajimu UMEMOTO 		a.s_addr = net | host;
6665e96449SHajimu UMEMOTO 	a.s_addr = htonl(a.s_addr);
6765e96449SHajimu UMEMOTO 	return (a);
6865e96449SHajimu UMEMOTO }
69ab96eeabSHajimu UMEMOTO 
70ab96eeabSHajimu UMEMOTO /*
71ab96eeabSHajimu UMEMOTO  * Weak aliases for applications that use certain private entry points,
72ab96eeabSHajimu UMEMOTO  * and fail to include <arpa/inet.h>.
73ab96eeabSHajimu UMEMOTO  */
74ab96eeabSHajimu UMEMOTO #undef inet_makeaddr
75ab96eeabSHajimu UMEMOTO __weak_reference(__inet_makeaddr, inet_makeaddr);
76