aboutsummaryrefslogtreecommitdiff
path: root/libbb/inet_common.c
diff options
context:
space:
mode:
Diffstat (limited to 'libbb/inet_common.c')
-rw-r--r--libbb/inet_common.c51
1 files changed, 26 insertions, 25 deletions
diff --git a/libbb/inet_common.c b/libbb/inet_common.c
index 9cdcb11de..6b31c79f2 100644
--- a/libbb/inet_common.c
+++ b/libbb/inet_common.c
@@ -33,19 +33,23 @@ int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
33 /* If we expect this to be a hostname, try hostname database first */ 33 /* If we expect this to be a hostname, try hostname database first */
34#ifdef DEBUG 34#ifdef DEBUG
35 if (hostfirst) { 35 if (hostfirst) {
36 bb_error_msg("gethostbyname (%s)", name); 36 bb_error_msg("gethostbyname(%s)", name);
37 } 37 }
38#endif 38#endif
39 if (hostfirst && (hp = gethostbyname(name)) != (struct hostent *) NULL) { 39 if (hostfirst) {
40 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0], 40 hp = gethostbyname(name);
41 sizeof(struct in_addr)); 41 if (hp != NULL) {
42 return 0; 42 memcpy(&s_in->sin_addr, hp->h_addr_list[0],
43 sizeof(struct in_addr));
44 return 0;
45 }
43 } 46 }
44 /* Try the NETWORKS database to see if this is a known network. */ 47 /* Try the NETWORKS database to see if this is a known network. */
45#ifdef DEBUG 48#ifdef DEBUG
46 bb_error_msg("getnetbyname (%s)", name); 49 bb_error_msg("getnetbyname(%s)", name);
47#endif 50#endif
48 if ((np = getnetbyname(name)) != (struct netent *) NULL) { 51 np = getnetbyname(name);
52 if (np != NULL) {
49 s_in->sin_addr.s_addr = htonl(np->n_net); 53 s_in->sin_addr.s_addr = htonl(np->n_net);
50 return 1; 54 return 1;
51 } 55 }
@@ -59,14 +63,13 @@ int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
59#endif 63#endif
60 64
61#ifdef DEBUG 65#ifdef DEBUG
62 bb_error_msg("gethostbyname (%s)", name); 66 bb_error_msg("gethostbyname(%s)", name);
63#endif 67#endif
64 if ((hp = gethostbyname(name)) == (struct hostent *) NULL) { 68 hp = gethostbyname(name);
69 if (hp == NULL) {
65 return -1; 70 return -1;
66 } 71 }
67 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0], 72 memcpy(&s_in->sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
68 sizeof(struct in_addr));
69
70 return 0; 73 return 0;
71} 74}
72 75
@@ -78,7 +81,7 @@ struct addr {
78 struct addr *next; 81 struct addr *next;
79}; 82};
80 83
81static struct addr *INET_nn = NULL; /* addr-to-name cache */ 84static struct addr *INET_nn = NULL; /* addr-to-name cache */
82 85
83/* numeric: & 0x8000: default instead of *, 86/* numeric: & 0x8000: default instead of *,
84 * & 0x4000: host instead of net, 87 * & 0x4000: host instead of net,
@@ -90,7 +93,7 @@ int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
90 struct hostent *ent; 93 struct hostent *ent;
91 struct netent *np; 94 struct netent *np;
92 struct addr *pn; 95 struct addr *pn;
93 unsigned long ad, host_ad; 96 uint32_t ad, host_ad;
94 int host = 0; 97 int host = 0;
95 98
96 /* Grmpf. -FvK */ 99 /* Grmpf. -FvK */
@@ -102,9 +105,9 @@ int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
102 errno = EAFNOSUPPORT; 105 errno = EAFNOSUPPORT;
103 return -1; 106 return -1;
104 } 107 }
105 ad = (unsigned long) s_in->sin_addr.s_addr; 108 ad = s_in->sin_addr.s_addr;
106#ifdef DEBUG 109#ifdef DEBUG
107 bb_error_msg("rresolve: %08lx, mask %08x, num %08x", ad, netmask, numeric); 110 bb_error_msg("rresolve: %08x, mask %08x, num %08x", (unsigned)ad, netmask, numeric);
108#endif 111#endif
109 if (ad == INADDR_ANY) { 112 if (ad == INADDR_ANY) {
110 if ((numeric & 0x0FFF) == 0) { 113 if ((numeric & 0x0FFF) == 0) {
@@ -127,8 +130,8 @@ int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
127 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) { 130 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
128 safe_strncpy(name, pn->name, len); 131 safe_strncpy(name, pn->name, len);
129#ifdef DEBUG 132#ifdef DEBUG
130 bb_error_msg("rresolve: found %s %08lx in cache", 133 bb_error_msg("rresolve: found %s %08x in cache",
131 (host ? "host" : "net"), ad); 134 (host ? "host" : "net"), (unsigned)ad);
132#endif 135#endif
133 return 0; 136 return 0;
134 } 137 }
@@ -140,7 +143,7 @@ int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
140 ent = NULL; 143 ent = NULL;
141 if (host) { 144 if (host) {
142#ifdef DEBUG 145#ifdef DEBUG
143 bb_error_msg("gethostbyaddr (%08lx)", ad); 146 bb_error_msg("gethostbyaddr (%08x)", (unsigned)ad);
144#endif 147#endif
145 ent = gethostbyaddr((char *) &ad, 4, AF_INET); 148 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
146 if (ent != NULL) { 149 if (ent != NULL) {
@@ -148,14 +151,14 @@ int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
148 } 151 }
149 } else { 152 } else {
150#ifdef DEBUG 153#ifdef DEBUG
151 bb_error_msg("getnetbyaddr (%08lx)", host_ad); 154 bb_error_msg("getnetbyaddr (%08x)", (unsigned)host_ad);
152#endif 155#endif
153 np = getnetbyaddr(host_ad, AF_INET); 156 np = getnetbyaddr(host_ad, AF_INET);
154 if (np != NULL) { 157 if (np != NULL) {
155 safe_strncpy(name, np->n_name, len); 158 safe_strncpy(name, np->n_name, len);
156 } 159 }
157 } 160 }
158 if ((ent == NULL) && (np == NULL)) { 161 if (!ent && !np) {
159 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len); 162 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
160 } 163 }
161 pn = xmalloc(sizeof(struct addr)); 164 pn = xmalloc(sizeof(struct addr));
@@ -164,7 +167,6 @@ int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
164 pn->host = host; 167 pn->host = host;
165 pn->name = xstrdup(name); 168 pn->name = xstrdup(name);
166 INET_nn = pn; 169 INET_nn = pn;
167
168 return 0; 170 return 0;
169} 171}
170 172
@@ -183,9 +185,7 @@ int INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
183 return -1; 185 return -1;
184 } 186 }
185 memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6)); 187 memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6));
186
187 freeaddrinfo(ai); 188 freeaddrinfo(ai);
188
189 return 0; 189 return 0;
190} 190}
191 191
@@ -224,7 +224,8 @@ int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6,
224 return 0; 224 return 0;
225 } 225 }
226 226
227 s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6), name, len, NULL, 0, 0); 227 s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6),
228 name, len, NULL, 0, 0);
228 if (s) { 229 if (s) {
229 bb_error_msg("getnameinfo failed"); 230 bb_error_msg("getnameinfo failed");
230 return -1; 231 return -1;