]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libatm/ip_addr.c
This commit was generated by cvs2svn to compensate for changes in r168988,
[FreeBSD/FreeBSD.git] / lib / libatm / ip_addr.c
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 /*
31  * User Space Library Functions
32  * ----------------------------
33  *
34  * IP address utilities
35  *
36  */
37
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/socket.h>
41 #include <stdio.h>
42 #include <net/if.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
45 #include <netatm/port.h>
46 #include <netatm/atm.h>
47 #include <netatm/atm_if.h>
48 #include <netatm/atm_sap.h>
49 #include <netatm/atm_sys.h>
50 #include <netatm/atm_ioctl.h>
51
52 #include <netdb.h>
53 #include <string.h>
54
55 #include "libatm.h"
56
57 /*
58  * Get IP address
59  *
60  * Return an IP address in a socket address structure, given a character
61  * string with a domain name or a dotted decimal number.
62  * 
63  * Arguments:
64  *      p       pointer to a host name or IP address
65  *
66  * Returns:
67  *      null                    error was encountered
68  *      struct sockaddr_in *    a pointer to a socket address with the
69  *                              requested IP address
70  *
71  */
72 struct sockaddr_in *
73 get_ip_addr(const char *p)
74 {
75         struct hostent                  *ip_host;
76         static struct sockaddr_in       s;
77
78         /*
79          * Get IP address of specified host name
80          */
81         bzero(&s, sizeof(s));
82         s.sin_family = AF_INET;
83         if (p[0] >= '0' && p[0] <= '9') {
84                 /*
85                  * IP address is in dotted decimal format
86                  */
87                 if ((s.sin_addr.s_addr = inet_addr(p)) == INADDR_NONE) {
88                         return((struct sockaddr_in *)0);
89                 }
90         } else {
91                 /*
92                  * Host name is in domain name system format
93                  */
94                 ip_host = gethostbyname(p);
95                 if (!ip_host ||
96                                 ip_host->h_addrtype != AF_INET) {
97                         return((struct sockaddr_in *)0);
98                 }
99                 memcpy(&s.sin_addr.s_addr, ip_host->h_addr_list[0],
100                     sizeof(s.sin_addr.s_addr));
101         }
102         return(&s);
103 }
104
105
106 /*
107  * Format an IP address
108  *
109  * Return a text-formatted string with an IP address and domain name
110  * given a sockaddr_in with an IP address.
111  * 
112  * Arguments:
113  *      addr    pointer to sockaddr_in with an IP address
114  *
115  * Returns:
116  *      char *  pointer to a text-formatted string
117  *
118  */
119 const char *
120 format_ip_addr(const struct in_addr *addr)
121 {
122         static char     host_name[MAXHOSTNAMELEN + 18];
123         char            *ip_num;
124         struct hostent  *ip_host;
125
126         /*
127          * Initialize
128          */
129         bzero(host_name, sizeof(host_name));
130
131         /*
132          * Check for a zero address
133          */
134         if (!addr || addr->s_addr == 0) {
135                 return("-");
136         }
137
138         /*
139          * Get address in dotted decimal format
140          */
141         ip_num = inet_ntoa(*addr);
142
143         /*
144          * Look up name in DNS
145          */
146         ip_host = gethostbyaddr((const char *)addr, sizeof(addr), AF_INET);
147         if (ip_host && ip_host->h_name && strlen(ip_host->h_name)) {
148                 /*
149                  * Return host name followed by dotted decimal address
150                  */
151                 snprintf(host_name, sizeof(host_name), "%s (%s)", 
152                     ip_host->h_name, ip_num);
153                 return (host_name);
154         } else {
155                 /*
156                  * No host name -- just return dotted decimal address
157                  */
158                 return(ip_num);
159         }
160 }