1 /* $OpenBSD: inet_neta.c,v 1.6 2005/03/25 13:24:12 otto 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 static const char rcsid[] = "$OpenBSD: inet_neta.c,v 1.6 2005/03/25 13:24:12 otto Exp $"; 22 #endif 23 24 #include <sys/types.h> 25 #include <sys/socket.h> 26 #include <netinet/in.h> 27 #include <arpa/inet.h> 28 29 #include <errno.h> 30 #include <stdio.h> 31 #include <string.h> 32 33 /* 34 * char * 35 * inet_neta(src, dst, size) 36 * format an in_addr_t network number into presentation format. 37 * return: 38 * pointer to dst, or NULL if an error occurred (check errno). 39 * note: 40 * format of ``src'' is as for inet_network(). 41 * author: 42 * Paul Vixie (ISC), July 1996 43 */ 44 char * 45 inet_neta(in_addr_t src, char *dst, size_t size) 46 { 47 char *odst = dst; 48 char *ep; 49 int advance; 50 51 if (src == 0x00000000) { 52 if (size < sizeof "0.0.0.0") 53 goto emsgsize; 54 strlcpy(dst, "0.0.0.0", size); 55 return dst; 56 } 57 ep = dst + size; 58 if (ep <= dst) 59 goto emsgsize; 60 while (src & 0xffffffff) { 61 u_char b = (src & 0xff000000) >> 24; 62 63 src <<= 8; 64 if (b || src) { 65 if (ep - dst < sizeof "255.") 66 goto emsgsize; 67 advance = snprintf(dst, ep - dst, "%u", b); 68 if (advance <= 0 || advance >= ep - dst) 69 goto emsgsize; 70 dst += advance; 71 if (src != 0L) { 72 if (dst + 1 >= ep) 73 goto emsgsize; 74 *dst++ = '.'; 75 *dst = '\0'; 76 } 77 } 78 } 79 return (odst); 80 81 emsgsize: 82 errno = EMSGSIZE; 83 return (NULL); 84 } 85