1 /* $OpenBSD: inet_neta.c,v 1.4 2002/08/19 03:01:54 itojun Exp $ */ 2 3 /* 4 * Copyright (c) 1996 by Internet Software Consortium. 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS 11 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES 12 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE 13 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS 16 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 17 * SOFTWARE. 18 */ 19 20 #if defined(LIBC_SCCS) && !defined(lint) 21 #if 0 22 static const char rcsid[] = "$Id: inet_neta.c,v 1.4 2002/08/19 03:01:54 itojun Exp $"; 23 #else 24 static const char rcsid[] = "$OpenBSD: inet_neta.c,v 1.4 2002/08/19 03:01:54 itojun Exp $"; 25 #endif 26 #endif 27 28 #include <sys/types.h> 29 #include <sys/socket.h> 30 #include <netinet/in.h> 31 #include <arpa/inet.h> 32 33 #include <errno.h> 34 #include <stdio.h> 35 #include <string.h> 36 37 /* 38 * char * 39 * inet_neta(src, dst, size) 40 * format an in_addr_t network number into presentation format. 41 * return: 42 * pointer to dst, or NULL if an error occurred (check errno). 43 * note: 44 * format of ``src'' is as for inet_network(). 45 * author: 46 * Paul Vixie (ISC), July 1996 47 */ 48 char * 49 inet_neta(src, dst, size) 50 in_addr_t src; 51 char *dst; 52 size_t size; 53 { 54 char *odst = dst; 55 char *ep; 56 int advance; 57 58 if (src == 0x00000000) { 59 if (size < sizeof "0.0.0.0") 60 goto emsgsize; 61 strlcpy(dst, "0.0.0.0", size); 62 return dst; 63 } 64 ep = dst + size; 65 if (ep <= dst) 66 goto emsgsize; 67 while (src & 0xffffffff) { 68 u_char b = (src & 0xff000000) >> 24; 69 70 src <<= 8; 71 if (b || src) { 72 if (ep - dst < sizeof "255.") 73 goto emsgsize; 74 advance = snprintf(dst, ep - dst, "%u", b); 75 if (advance <= 0 || advance >= ep - dst) 76 goto emsgsize; 77 dst += advance; 78 if (src != 0L) { 79 if (dst + 1 >= ep) 80 goto emsgsize; 81 *dst++ = '.'; 82 *dst = '\0'; 83 } 84 } 85 } 86 return (odst); 87 88 emsgsize: 89 errno = EMSGSIZE; 90 return (NULL); 91 } 92