xref: /xnu-10002.1.13/libkern/net/inet_ntoa.c (revision 1031c584a5e37aff177559b9f69dbd3c8c3fd30a)
1*1031c584SApple OSS Distributions /*-
2*1031c584SApple OSS Distributions  * Copyright 1994, 1995 Massachusetts Institute of Technology
3*1031c584SApple OSS Distributions  *
4*1031c584SApple OSS Distributions  * Permission to use, copy, modify, and distribute this software and
5*1031c584SApple OSS Distributions  * its documentation for any purpose and without fee is hereby
6*1031c584SApple OSS Distributions  * granted, provided that both the above copyright notice and this
7*1031c584SApple OSS Distributions  * permission notice appear in all copies, that both the above
8*1031c584SApple OSS Distributions  * copyright notice and this permission notice appear in all
9*1031c584SApple OSS Distributions  * supporting documentation, and that the name of M.I.T. not be used
10*1031c584SApple OSS Distributions  * in advertising or publicity pertaining to distribution of the
11*1031c584SApple OSS Distributions  * software without specific, written prior permission.  M.I.T. makes
12*1031c584SApple OSS Distributions  * no representations about the suitability of this software for any
13*1031c584SApple OSS Distributions  * purpose.  It is provided "as is" without express or implied
14*1031c584SApple OSS Distributions  * warranty.
15*1031c584SApple OSS Distributions  *
16*1031c584SApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17*1031c584SApple OSS Distributions  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18*1031c584SApple OSS Distributions  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*1031c584SApple OSS Distributions  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20*1031c584SApple OSS Distributions  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21*1031c584SApple OSS Distributions  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22*1031c584SApple OSS Distributions  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23*1031c584SApple OSS Distributions  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24*1031c584SApple OSS Distributions  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25*1031c584SApple OSS Distributions  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26*1031c584SApple OSS Distributions  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*1031c584SApple OSS Distributions  * SUCH DAMAGE.
28*1031c584SApple OSS Distributions  */
29*1031c584SApple OSS Distributions #include <sys/param.h>
30*1031c584SApple OSS Distributions #include <sys/systm.h>
31*1031c584SApple OSS Distributions 
32*1031c584SApple OSS Distributions #include <netinet/in.h>
33*1031c584SApple OSS Distributions 
34*1031c584SApple OSS Distributions char *
inet_ntoa(struct in_addr ina)35*1031c584SApple OSS Distributions inet_ntoa(struct in_addr ina)
36*1031c584SApple OSS Distributions {
37*1031c584SApple OSS Distributions 	static char buf[4 * sizeof "123"];
38*1031c584SApple OSS Distributions 	unsigned char *ucp = (unsigned char *)&ina;
39*1031c584SApple OSS Distributions 
40*1031c584SApple OSS Distributions 	snprintf(buf, sizeof(buf), "%d.%d.%d.%d",
41*1031c584SApple OSS Distributions 	    ucp[0] & 0xff,
42*1031c584SApple OSS Distributions 	    ucp[1] & 0xff,
43*1031c584SApple OSS Distributions 	    ucp[2] & 0xff,
44*1031c584SApple OSS Distributions 	    ucp[3] & 0xff);
45*1031c584SApple OSS Distributions 	return buf;
46*1031c584SApple OSS Distributions }
47*1031c584SApple OSS Distributions 
48*1031c584SApple OSS Distributions char *
inet_ntoa_r(struct in_addr ina,char * buf,size_t buflen)49*1031c584SApple OSS Distributions inet_ntoa_r(struct in_addr ina, char *buf, size_t buflen)
50*1031c584SApple OSS Distributions {
51*1031c584SApple OSS Distributions 	unsigned char *ucp = (unsigned char *)&ina;
52*1031c584SApple OSS Distributions 
53*1031c584SApple OSS Distributions 	snprintf(buf, buflen, "%d.%d.%d.%d",
54*1031c584SApple OSS Distributions 	    ucp[0] & 0xff,
55*1031c584SApple OSS Distributions 	    ucp[1] & 0xff,
56*1031c584SApple OSS Distributions 	    ucp[2] & 0xff,
57*1031c584SApple OSS Distributions 	    ucp[3] & 0xff);
58*1031c584SApple OSS Distributions 	return buf;
59*1031c584SApple OSS Distributions }
60