Changeset 1770 in MondoRescue for branches/stable/mindi-busybox/libbb/inet_common.c
- Timestamp:
- Nov 6, 2007, 11:01:53 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/mindi-busybox/libbb/inet_common.c
r821 r1770 1 /* vi: set sw=4 ts=4: */ 1 2 /* 2 3 * stolen from net-tools-1.59 and stripped down for busybox by … … 5 6 * Heavily modified by Manuel Novoa III Mar 12, 2001 6 7 * 7 * Version: $Id: inet_common.c,v 1.8 2004/03/10 07:42:38 mjn3 Exp $8 8 * 9 9 */ … … 11 11 #include "libbb.h" 12 12 #include "inet_common.h" 13 #include <stdio.h>14 #include <errno.h>15 #include <stdlib.h>16 #include <string.h>17 #include <unistd.h>18 19 #ifdef DEBUG20 # include <resolv.h>21 #endif22 23 24 const char bb_INET_default[] = "default";25 13 26 14 int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst) 27 15 { 28 16 struct hostent *hp; 17 #if ENABLE_FEATURE_ETC_NETWORKS 29 18 struct netent *np; 19 #endif 30 20 31 21 /* Grmpf. -FvK */ … … 34 24 35 25 /* Default is special, meaning 0.0.0.0. */ 36 if (!strcmp(name, bb_ INET_default)) {26 if (!strcmp(name, bb_str_default)) { 37 27 s_in->sin_addr.s_addr = INADDR_ANY; 38 return (1);28 return 1; 39 29 } 40 30 /* Look to see if it's a dotted quad. */ … … 45 35 #ifdef DEBUG 46 36 if (hostfirst) { 47 bb_error_msg("gethostbyname (%s)", name); 48 } 49 #endif 50 if (hostfirst && (hp = gethostbyname(name)) != (struct hostent *) NULL) { 51 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0], 52 sizeof(struct in_addr)); 53 return 0; 54 } 37 bb_error_msg("gethostbyname(%s)", name); 38 } 39 #endif 40 if (hostfirst) { 41 hp = gethostbyname(name); 42 if (hp != NULL) { 43 memcpy(&s_in->sin_addr, hp->h_addr_list[0], 44 sizeof(struct in_addr)); 45 return 0; 46 } 47 } 48 #if ENABLE_FEATURE_ETC_NETWORKS 55 49 /* Try the NETWORKS database to see if this is a known network. */ 56 50 #ifdef DEBUG 57 bb_error_msg("getnetbyname (%s)", name); 58 #endif 59 if ((np = getnetbyname(name)) != (struct netent *) NULL) { 51 bb_error_msg("getnetbyname(%s)", name); 52 #endif 53 np = getnetbyname(name); 54 if (np != NULL) { 60 55 s_in->sin_addr.s_addr = htonl(np->n_net); 61 56 return 1; 62 57 } 58 #endif 63 59 if (hostfirst) { 64 60 /* Don't try again */ … … 71 67 72 68 #ifdef DEBUG 73 bb_error_msg("gethostbyname (%s)", name); 74 #endif 75 if ((hp = gethostbyname(name)) == (struct hostent *) NULL) { 69 bb_error_msg("gethostbyname(%s)", name); 70 #endif 71 hp = gethostbyname(name); 72 if (hp == NULL) { 76 73 return -1; 77 74 } 78 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0], 79 sizeof(struct in_addr)); 80 75 memcpy(&s_in->sin_addr, hp->h_addr_list[0], sizeof(struct in_addr)); 81 76 return 0; 82 77 } 83 78 84 /* cache */85 struct addr {86 struct sockaddr_in addr;87 char *name;88 int host;89 struct addr *next;90 };91 92 static struct addr *INET_nn = NULL; /* addr-to-name cache */93 79 94 80 /* numeric: & 0x8000: default instead of *, … … 96 82 * & 0x0fff: don't resolve 97 83 */ 98 int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in, 99 int numeric, unsigned int netmask) 100 { 101 struct hostent *ent; 102 struct netent *np; 84 char *INET_rresolve(struct sockaddr_in *s_in, int numeric, uint32_t netmask) 85 { 86 /* addr-to-name cache */ 87 struct addr { 88 struct addr *next; 89 struct sockaddr_in addr; 90 int host; 91 char name[1]; 92 }; 93 static struct addr *cache = NULL; 94 103 95 struct addr *pn; 104 unsigned long ad, host_ad; 96 char *name; 97 uint32_t ad, host_ad; 105 98 int host = 0; 106 99 … … 108 101 if (s_in->sin_family != AF_INET) { 109 102 #ifdef DEBUG 110 bb_error_msg("rresolve: unsupport address family %d!",103 bb_error_msg("rresolve: unsupported address family %d!", 111 104 s_in->sin_family); 112 105 #endif 113 106 errno = EAFNOSUPPORT; 114 return (-1);115 } 116 ad = (unsigned long)s_in->sin_addr.s_addr;117 #ifdef DEBUG 118 bb_error_msg("rresolve: %08 lx, mask %08x, num %08x",ad, netmask, numeric);107 return NULL; 108 } 109 ad = s_in->sin_addr.s_addr; 110 #ifdef DEBUG 111 bb_error_msg("rresolve: %08x, mask %08x, num %08x", (unsigned)ad, netmask, numeric); 119 112 #endif 120 113 if (ad == INADDR_ANY) { 121 114 if ((numeric & 0x0FFF) == 0) { 122 115 if (numeric & 0x8000) 123 safe_strncpy(name, bb_INET_default, len); 124 else 125 safe_strncpy(name, "*", len); 126 return (0); 116 return xstrdup(bb_str_default); 117 return xstrdup("*"); 127 118 } 128 119 } 129 if (numeric & 0x0FFF) { 130 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len); 131 return (0); 132 } 120 if (numeric & 0x0FFF) 121 return xstrdup(inet_ntoa(s_in->sin_addr)); 133 122 134 123 if ((ad & (~netmask)) != 0 || (numeric & 0x4000)) 135 124 host = 1; 136 #if 0 137 INET_nn = NULL; 138 #endif 139 pn = INET_nn; 140 while (pn != NULL) { 125 pn = cache; 126 while (pn) { 141 127 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) { 142 safe_strncpy(name, pn->name, len); 143 #ifdef DEBUG 144 bb_error_msg("rresolve: found %s %08lx in cache", 145 (host ? "host" : "net"), ad); 146 #endif 147 return (0); 128 #ifdef DEBUG 129 bb_error_msg("rresolve: found %s %08x in cache", 130 (host ? "host" : "net"), (unsigned)ad); 131 #endif 132 return xstrdup(pn->name); 148 133 } 149 134 pn = pn->next; … … 151 136 152 137 host_ad = ntohl(ad); 153 np = NULL; 154 ent = NULL; 138 name = NULL; 155 139 if (host) { 156 #ifdef DEBUG 157 bb_error_msg("gethostbyaddr (%08lx)", ad); 140 struct hostent *ent; 141 #ifdef DEBUG 142 bb_error_msg("gethostbyaddr (%08x)", (unsigned)ad); 158 143 #endif 159 144 ent = gethostbyaddr((char *) &ad, 4, AF_INET); 160 if (ent != NULL) {161 safe_strncpy(name, ent->h_name, len);162 }163 } else {164 #ifdef DEBUG 165 bb_error_msg("getnetbyaddr (%08 lx)",host_ad);145 if (ent) 146 name = xstrdup(ent->h_name); 147 } else if (ENABLE_FEATURE_ETC_NETWORKS) { 148 struct netent *np; 149 #ifdef DEBUG 150 bb_error_msg("getnetbyaddr (%08x)", (unsigned)host_ad); 166 151 #endif 167 152 np = getnetbyaddr(host_ad, AF_INET); 168 if (np != NULL) { 169 safe_strncpy(name, np->n_name, len); 170 } 171 } 172 if ((ent == NULL) && (np == NULL)) { 173 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len); 174 } 175 pn = (struct addr *) xmalloc(sizeof(struct addr)); 153 if (np) 154 name = xstrdup(np->n_name); 155 } 156 if (!name) 157 name = xstrdup(inet_ntoa(s_in->sin_addr)); 158 pn = xmalloc(sizeof(*pn) + strlen(name)); /* no '+ 1', it's already accounted for */ 159 pn->next = cache; 176 160 pn->addr = *s_in; 177 pn->next = INET_nn;178 161 pn->host = host; 179 pn->name = bb_xstrdup(name); 180 INET_nn = pn; 181 182 return (0); 183 } 184 185 #ifdef CONFIG_FEATURE_IPV6 162 strcpy(pn->name, name); 163 cache = pn; 164 return name; 165 } 166 167 #if ENABLE_FEATURE_IPV6 186 168 187 169 int INET6_resolve(const char *name, struct sockaddr_in6 *sin6) … … 192 174 memset(&req, '\0', sizeof req); 193 175 req.ai_family = AF_INET6; 194 if ((s = getaddrinfo(name, NULL, &req, &ai))) { 176 s = getaddrinfo(name, NULL, &req, &ai); 177 if (s) { 195 178 bb_error_msg("getaddrinfo: %s: %d", name, s); 196 179 return -1; 197 180 } 198 181 memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6)); 199 200 182 freeaddrinfo(ai); 201 202 return (0); 183 return 0; 203 184 } 204 185 … … 210 191 211 192 212 int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6, 213 int numeric) 214 { 193 char *INET6_rresolve(struct sockaddr_in6 *sin6, int numeric) 194 { 195 char name[128]; 215 196 int s; 216 197 … … 218 199 if (sin6->sin6_family != AF_INET6) { 219 200 #ifdef DEBUG 220 bb_error_msg( _("rresolve: unsupport address family %d !\n"),201 bb_error_msg("rresolve: unsupport address family %d!", 221 202 sin6->sin6_family); 222 203 #endif 223 204 errno = EAFNOSUPPORT; 224 return (-1);205 return NULL; 225 206 } 226 207 if (numeric & 0x7FFF) { 227 inet_ntop(AF_INET6, &sin6->sin6_addr, name, len);228 return (0);208 inet_ntop(AF_INET6, &sin6->sin6_addr, name, sizeof(name)); 209 return xstrdup(name); 229 210 } 230 211 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { 231 if (numeric & 0x8000) { 232 strcpy(name, "default"); 233 } else { 234 strcpy(name, "*"); 235 } 236 return (0); 237 } 238 239 s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6), name, len, NULL, 0, 0); 212 if (numeric & 0x8000) 213 return xstrdup(bb_str_default); 214 return xstrdup("*"); 215 } 216 217 s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6), 218 name, sizeof(name), NULL, 0, 0); 240 219 if (s) { 241 220 bb_error_msg("getnameinfo failed"); 242 return -1;243 } 244 return (0);221 return NULL; 222 } 223 return xstrdup(name); 245 224 } 246 225
Note:
See TracChangeset
for help on using the changeset viewer.