1/* $NetBSD: socktoa.c,v 1.1.1.2 2012/01/31 21:24:12 kardel Exp $ */ 2 3/* 4 * socktoa - return a numeric host name from a sockaddr_storage structure 5 */ 6 7#ifdef HAVE_CONFIG_H 8#include <config.h> 9#endif 10 11#include <sys/types.h> 12#ifdef HAVE_SYS_SOCKET_H 13#include <sys/socket.h> 14#endif 15#ifdef HAVE_NETINET_IN_H 16#include <netinet/in.h> 17#endif 18 19#include <arpa/inet.h> 20 21#ifdef ISC_PLATFORM_NEEDNTOP 22#include <isc/net.h> 23#endif 24 25#include <stdio.h> 26 27#include "ntp_fp.h" 28#include "lib_strbuf.h" 29#include "ntp_stdlib.h" 30#include "ntp.h" 31 32char * 33socktoa( 34 const sockaddr_u *sock 35 ) 36{ 37 register char *buffer; 38 39 LIB_GETBUF(buffer); 40 41 if (NULL == sock) 42 strncpy(buffer, "(null)", LIB_BUFLENGTH); 43 else { 44 switch(AF(sock)) { 45 46 case AF_INET: 47 case AF_UNSPEC: 48 inet_ntop(AF_INET, PSOCK_ADDR4(sock), buffer, 49 LIB_BUFLENGTH); 50 break; 51 52 case AF_INET6: 53 inet_ntop(AF_INET6, PSOCK_ADDR6(sock), buffer, 54 LIB_BUFLENGTH); 55 break; 56 57 default: 58 snprintf(buffer, LIB_BUFLENGTH, 59 "(socktoa unknown family %d)", 60 AF(sock)); 61 } 62 } 63 return buffer; 64} 65