| 1 | /* |
| 2 | * |
| 3 | * =================================== |
| 4 | * HARP | Host ATM Research Platform |
| 5 | * =================================== |
| 6 | * |
| 7 | * |
| 8 | * This Host ATM Research Platform ("HARP") file (the "Software") is |
| 9 | * made available by Network Computing Services, Inc. ("NetworkCS") |
| 10 | * "AS IS". NetworkCS does not provide maintenance, improvements or |
| 11 | * support of any kind. |
| 12 | * |
| 13 | * NETWORKCS MAKES NO WARRANTIES OR REPRESENTATIONS, EXPRESS OR IMPLIED, |
| 14 | * INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY |
| 15 | * AND FITNESS FOR A PARTICULAR PURPOSE, AS TO ANY ELEMENT OF THE |
| 16 | * SOFTWARE OR ANY SUPPORT PROVIDED IN CONNECTION WITH THIS SOFTWARE. |
| 17 | * In no event shall NetworkCS be responsible for any damages, including |
| 18 | * but not limited to consequential damages, arising from or relating to |
| 19 | * any use of the Software or related support. |
| 20 | * |
| 21 | * Copyright 1994-1998 Network Computing Services, Inc. |
| 22 | * |
| 23 | * Copies of this Software may be made, however, the above copyright |
| 24 | * notice must be reproduced on all copies. |
| 25 | * |
| 26 | * @(#) $FreeBSD: src/lib/libatm/ip_addr.c,v 1.3.2.1 2001/09/28 16:52:10 dillon Exp $ |
| 27 | * @(#) $DragonFly: src/lib/libatm/ip_addr.c,v 1.5 2008/09/30 16:57:04 swildner Exp $ |
| 28 | * |
| 29 | */ |
| 30 | |
| 31 | #include <sys/cdefs.h> |
| 32 | |
| 33 | /* |
| 34 | * User Space Library Functions |
| 35 | * ---------------------------- |
| 36 | * |
| 37 | * IP address utilities |
| 38 | * |
| 39 | */ |
| 40 | |
| 41 | #include <sys/types.h> |
| 42 | #include <sys/param.h> |
| 43 | #include <sys/socket.h> |
| 44 | #include <net/if.h> |
| 45 | #include <netinet/in.h> |
| 46 | #include <arpa/inet.h> |
| 47 | #include <netatm/port.h> |
| 48 | #include <netatm/atm.h> |
| 49 | #include <netatm/atm_if.h> |
| 50 | #include <netatm/atm_sap.h> |
| 51 | #include <netatm/atm_sys.h> |
| 52 | #include <netatm/atm_ioctl.h> |
| 53 | |
| 54 | #include <netdb.h> |
| 55 | #include <stdio.h> |
| 56 | #include <string.h> |
| 57 | |
| 58 | #include "libatm.h" |
| 59 | |
| 60 | /* |
| 61 | * Get IP address |
| 62 | * |
| 63 | * Return an IP address in a socket address structure, given a character |
| 64 | * string with a domain name or a dotted decimal number. |
| 65 | * |
| 66 | * Arguments: |
| 67 | * p pointer to a host name or IP address |
| 68 | * |
| 69 | * Returns: |
| 70 | * null error was encountered |
| 71 | * struct sockaddr_in * a pointer to a socket address with the |
| 72 | * requested IP address |
| 73 | * |
| 74 | */ |
| 75 | struct sockaddr_in * |
| 76 | get_ip_addr(char *p) |
| 77 | { |
| 78 | struct hostent *ip_host; |
| 79 | static struct sockaddr_in sin; |
| 80 | |
| 81 | /* |
| 82 | * Get IP address of specified host name |
| 83 | */ |
| 84 | UM_ZERO(&sin, sizeof(sin)); |
| 85 | sin.sin_family = AF_INET; |
| 86 | if (p[0] >= '0' && p[0] <= '9') { |
| 87 | /* |
| 88 | * IP address is in dotted decimal format |
| 89 | */ |
| 90 | if ((sin.sin_addr.s_addr = inet_addr(p)) == -1) { |
| 91 | return((struct sockaddr_in *)0); |
| 92 | } |
| 93 | } else { |
| 94 | /* |
| 95 | * Host name is in domain name system format |
| 96 | */ |
| 97 | ip_host = gethostbyname(p); |
| 98 | if (!ip_host || |
| 99 | ip_host->h_addrtype != AF_INET) { |
| 100 | return((struct sockaddr_in *)0); |
| 101 | } |
| 102 | sin.sin_addr.s_addr = *(u_long *)ip_host->h_addr_list[0]; |
| 103 | } |
| 104 | return(&sin); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /* |
| 109 | * Format an IP address |
| 110 | * |
| 111 | * Return a text-formatted string with an IP address and domain name |
| 112 | * given a sockaddr_in with an IP address. |
| 113 | * |
| 114 | * Arguments: |
| 115 | * p pointer to sockaddr_in with an IP address |
| 116 | * |
| 117 | * Returns: |
| 118 | * char * pointer to a text-formatted string |
| 119 | * |
| 120 | */ |
| 121 | char * |
| 122 | format_ip_addr(struct in_addr *addr) |
| 123 | { |
| 124 | static char host_name[MAXHOSTNAMELEN + 18]; |
| 125 | char *ip_num; |
| 126 | struct hostent *ip_host; |
| 127 | |
| 128 | /* |
| 129 | * Initialize |
| 130 | */ |
| 131 | UM_ZERO(host_name, sizeof(host_name)); |
| 132 | |
| 133 | /* |
| 134 | * Check for a zero address |
| 135 | */ |
| 136 | if (!addr || addr->s_addr == 0) { |
| 137 | return("-"); |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Get address in dotted decimal format |
| 142 | */ |
| 143 | ip_num = inet_ntoa(*addr); |
| 144 | |
| 145 | /* |
| 146 | * Look up name in DNS |
| 147 | */ |
| 148 | ip_host = gethostbyaddr(addr, sizeof(addr), AF_INET); |
| 149 | if (ip_host && ip_host->h_name && |
| 150 | strlen(ip_host->h_name)) { |
| 151 | /* |
| 152 | * Return host name followed by dotted decimal address |
| 153 | */ |
| 154 | snprintf(host_name, sizeof(host_name), "%s (%s)", |
| 155 | ip_host->h_name, ip_num); |
| 156 | return (host_name); |
| 157 | } else { |
| 158 | /* |
| 159 | * No host name -- just return dotted decimal address |
| 160 | */ |
| 161 | return(ip_num); |
| 162 | } |
| 163 | } |