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