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