xref: /illumos-gate/usr/src/lib/libresolv2/common/inet/inet_neta.c (revision 9525b14bcdeb5b5f6f95ab27c2f48f18bd2ec829)
17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate  * Copyright (c) 1996,1999 by Internet Software Consortium.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
67c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate  *
9*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*9525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*9525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*9525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*9525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*9525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*9525b14bSRao Shoaib  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
19*9525b14bSRao Shoaib static const char rcsid[] = "$Id: inet_neta.c,v 1.3 2005/04/27 04:56:20 sra Exp $";
207c478bd9Sstevel@tonic-gate #endif
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate #include "port_before.h"
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate #include <sys/types.h>
257c478bd9Sstevel@tonic-gate #include <sys/socket.h>
267c478bd9Sstevel@tonic-gate #include <netinet/in.h>
277c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <errno.h>
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include "port_after.h"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #ifdef SPRINTF_CHAR
367c478bd9Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x)
377c478bd9Sstevel@tonic-gate #else
387c478bd9Sstevel@tonic-gate # define SPRINTF(x) ((size_t)sprintf x)
397c478bd9Sstevel@tonic-gate #endif
407c478bd9Sstevel@tonic-gate 
41*9525b14bSRao Shoaib /*%
427c478bd9Sstevel@tonic-gate  * char *
437c478bd9Sstevel@tonic-gate  * inet_neta(src, dst, size)
447c478bd9Sstevel@tonic-gate  *	format a u_long network number into presentation format.
457c478bd9Sstevel@tonic-gate  * return:
467c478bd9Sstevel@tonic-gate  *	pointer to dst, or NULL if an error occurred (check errno).
477c478bd9Sstevel@tonic-gate  * note:
487c478bd9Sstevel@tonic-gate  *	format of ``src'' is as for inet_network().
497c478bd9Sstevel@tonic-gate  * author:
507c478bd9Sstevel@tonic-gate  *	Paul Vixie (ISC), July 1996
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate char *
inet_neta(src,dst,size)537c478bd9Sstevel@tonic-gate inet_neta(src, dst, size)
547c478bd9Sstevel@tonic-gate 	u_long src;
557c478bd9Sstevel@tonic-gate 	char *dst;
567c478bd9Sstevel@tonic-gate 	size_t size;
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	char *odst = dst;
597c478bd9Sstevel@tonic-gate 	char *tp;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	while (src & 0xffffffff) {
627c478bd9Sstevel@tonic-gate 		u_char b = (src & 0xff000000) >> 24;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 		src <<= 8;
657c478bd9Sstevel@tonic-gate 		if (b) {
667c478bd9Sstevel@tonic-gate 			if (size < sizeof "255.")
677c478bd9Sstevel@tonic-gate 				goto emsgsize;
687c478bd9Sstevel@tonic-gate 			tp = dst;
697c478bd9Sstevel@tonic-gate 			dst += SPRINTF((dst, "%u", b));
707c478bd9Sstevel@tonic-gate 			if (src != 0L) {
717c478bd9Sstevel@tonic-gate 				*dst++ = '.';
727c478bd9Sstevel@tonic-gate 				*dst = '\0';
737c478bd9Sstevel@tonic-gate 			}
747c478bd9Sstevel@tonic-gate 			size -= (size_t)(dst - tp);
757c478bd9Sstevel@tonic-gate 		}
767c478bd9Sstevel@tonic-gate 	}
777c478bd9Sstevel@tonic-gate 	if (dst == odst) {
787c478bd9Sstevel@tonic-gate 		if (size < sizeof "0.0.0.0")
797c478bd9Sstevel@tonic-gate 			goto emsgsize;
807c478bd9Sstevel@tonic-gate 		strcpy(dst, "0.0.0.0");
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate 	return (odst);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate  emsgsize:
857c478bd9Sstevel@tonic-gate 	errno = EMSGSIZE;
867c478bd9Sstevel@tonic-gate 	return (NULL);
877c478bd9Sstevel@tonic-gate }
88*9525b14bSRao Shoaib 
89*9525b14bSRao Shoaib /*! \file */
90