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