16a41b0fb0e589c4afc4ecbcf7c425f0aa40eaea4Elliott Hughes/*	$OpenBSD: inet_makeaddr.c,v 1.6 2005/08/06 20:30:03 espie Exp $ */
21dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
31dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Copyright (c) 1983, 1993
41dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *	The Regents of the University of California.  All rights reserved.
51dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
61dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Redistribution and use in source and binary forms, with or without
71dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * modification, are permitted provided that the following conditions
81dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * are met:
91dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 1. Redistributions of source code must retain the above copyright
101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice, this list of conditions and the following disclaimer.
111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright
121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice, this list of conditions and the following disclaimer in the
131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    documentation and/or other materials provided with the distribution.
141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 3. Neither the name of the University nor the names of its contributors
151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    may be used to endorse or promote products derived from this software
161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    without specific prior written permission.
171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * SUCH DAMAGE.
291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
316a41b0fb0e589c4afc4ecbcf7c425f0aa40eaea4Elliott Hughes#include <sys/param.h>
321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <netinet/in.h>
331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <arpa/inet.h>
34e8bcca3a2ca4a70156c72239d62bb68eeb2929fdElliott Hughes
35e8bcca3a2ca4a70156c72239d62bb68eeb2929fdElliott Hughes/*
366a41b0fb0e589c4afc4ecbcf7c425f0aa40eaea4Elliott Hughes * Formulate an Internet address from network + host.  Used in
376a41b0fb0e589c4afc4ecbcf7c425f0aa40eaea4Elliott Hughes * building addresses stored in the ifnet structure.
38e8bcca3a2ca4a70156c72239d62bb68eeb2929fdElliott Hughes */
396a41b0fb0e589c4afc4ecbcf7c425f0aa40eaea4Elliott Hughesstruct in_addr
406a41b0fb0e589c4afc4ecbcf7c425f0aa40eaea4Elliott Hughesinet_makeaddr(in_addr_t net, in_addr_t host)
416a41b0fb0e589c4afc4ecbcf7c425f0aa40eaea4Elliott Hughes{
426a41b0fb0e589c4afc4ecbcf7c425f0aa40eaea4Elliott Hughes	in_addr_t addr;
431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
446a41b0fb0e589c4afc4ecbcf7c425f0aa40eaea4Elliott Hughes	if (net < 128)
456a41b0fb0e589c4afc4ecbcf7c425f0aa40eaea4Elliott Hughes		addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST);
466a41b0fb0e589c4afc4ecbcf7c425f0aa40eaea4Elliott Hughes	else if (net < 65536)
476a41b0fb0e589c4afc4ecbcf7c425f0aa40eaea4Elliott Hughes		addr = (net << IN_CLASSB_NSHIFT) | (host & IN_CLASSB_HOST);
486a41b0fb0e589c4afc4ecbcf7c425f0aa40eaea4Elliott Hughes	else if (net < 16777216L)
496a41b0fb0e589c4afc4ecbcf7c425f0aa40eaea4Elliott Hughes		addr = (net << IN_CLASSC_NSHIFT) | (host & IN_CLASSC_HOST);
506a41b0fb0e589c4afc4ecbcf7c425f0aa40eaea4Elliott Hughes	else
516a41b0fb0e589c4afc4ecbcf7c425f0aa40eaea4Elliott Hughes		addr = net | host;
526a41b0fb0e589c4afc4ecbcf7c425f0aa40eaea4Elliott Hughes	addr = htonl(addr);
536a41b0fb0e589c4afc4ecbcf7c425f0aa40eaea4Elliott Hughes	return (*(struct in_addr *)&addr);
541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}