xref: /onnv-gate/usr/src/lib/libresolv2/common/inet/inet_neta.c (revision 11038:74b12212b8a2)
10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
30Sstevel@tonic-gate  * Copyright (c) 1996,1999 by Internet Software Consortium.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
60Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
70Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
80Sstevel@tonic-gate  *
9*11038SRao.Shoaib@Sun.COM  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*11038SRao.Shoaib@Sun.COM  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*11038SRao.Shoaib@Sun.COM  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*11038SRao.Shoaib@Sun.COM  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*11038SRao.Shoaib@Sun.COM  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*11038SRao.Shoaib@Sun.COM  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*11038SRao.Shoaib@Sun.COM  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
160Sstevel@tonic-gate  */
170Sstevel@tonic-gate 
180Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
19*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: inet_neta.c,v 1.3 2005/04/27 04:56:20 sra Exp $";
200Sstevel@tonic-gate #endif
210Sstevel@tonic-gate 
220Sstevel@tonic-gate #include "port_before.h"
230Sstevel@tonic-gate 
240Sstevel@tonic-gate #include <sys/types.h>
250Sstevel@tonic-gate #include <sys/socket.h>
260Sstevel@tonic-gate #include <netinet/in.h>
270Sstevel@tonic-gate #include <arpa/inet.h>
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <errno.h>
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include "port_after.h"
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #ifdef SPRINTF_CHAR
360Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x)
370Sstevel@tonic-gate #else
380Sstevel@tonic-gate # define SPRINTF(x) ((size_t)sprintf x)
390Sstevel@tonic-gate #endif
400Sstevel@tonic-gate 
41*11038SRao.Shoaib@Sun.COM /*%
420Sstevel@tonic-gate  * char *
430Sstevel@tonic-gate  * inet_neta(src, dst, size)
440Sstevel@tonic-gate  *	format a u_long network number into presentation format.
450Sstevel@tonic-gate  * return:
460Sstevel@tonic-gate  *	pointer to dst, or NULL if an error occurred (check errno).
470Sstevel@tonic-gate  * note:
480Sstevel@tonic-gate  *	format of ``src'' is as for inet_network().
490Sstevel@tonic-gate  * author:
500Sstevel@tonic-gate  *	Paul Vixie (ISC), July 1996
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate char *
inet_neta(src,dst,size)530Sstevel@tonic-gate inet_neta(src, dst, size)
540Sstevel@tonic-gate 	u_long src;
550Sstevel@tonic-gate 	char *dst;
560Sstevel@tonic-gate 	size_t size;
570Sstevel@tonic-gate {
580Sstevel@tonic-gate 	char *odst = dst;
590Sstevel@tonic-gate 	char *tp;
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	while (src & 0xffffffff) {
620Sstevel@tonic-gate 		u_char b = (src & 0xff000000) >> 24;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 		src <<= 8;
650Sstevel@tonic-gate 		if (b) {
660Sstevel@tonic-gate 			if (size < sizeof "255.")
670Sstevel@tonic-gate 				goto emsgsize;
680Sstevel@tonic-gate 			tp = dst;
690Sstevel@tonic-gate 			dst += SPRINTF((dst, "%u", b));
700Sstevel@tonic-gate 			if (src != 0L) {
710Sstevel@tonic-gate 				*dst++ = '.';
720Sstevel@tonic-gate 				*dst = '\0';
730Sstevel@tonic-gate 			}
740Sstevel@tonic-gate 			size -= (size_t)(dst - tp);
750Sstevel@tonic-gate 		}
760Sstevel@tonic-gate 	}
770Sstevel@tonic-gate 	if (dst == odst) {
780Sstevel@tonic-gate 		if (size < sizeof "0.0.0.0")
790Sstevel@tonic-gate 			goto emsgsize;
800Sstevel@tonic-gate 		strcpy(dst, "0.0.0.0");
810Sstevel@tonic-gate 	}
820Sstevel@tonic-gate 	return (odst);
830Sstevel@tonic-gate 
840Sstevel@tonic-gate  emsgsize:
850Sstevel@tonic-gate 	errno = EMSGSIZE;
860Sstevel@tonic-gate 	return (NULL);
870Sstevel@tonic-gate }
88*11038SRao.Shoaib@Sun.COM 
89*11038SRao.Shoaib@Sun.COM /*! \file */
90