diff options
| author | andersen <andersen@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2001-11-10 12:18:42 +0000 |
|---|---|---|
| committer | andersen <andersen@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2001-11-10 12:18:42 +0000 |
| commit | 37dccf26e3418b1880e50e0e4e87ee19dd83c4c8 (patch) | |
| tree | 870039993d700fe054484a2ea7b1bf24e3d467ae | |
| parent | 30825fa52c29734e81ca834884814286dc59d346 (diff) | |
| download | busybox-w32-37dccf26e3418b1880e50e0e4e87ee19dd83c4c8.tar.gz busybox-w32-37dccf26e3418b1880e50e0e4e87ee19dd83c4c8.tar.bz2 busybox-w32-37dccf26e3418b1880e50e0e4e87ee19dd83c4c8.zip | |
I forgot to add these. Part of the patch from vodz
git-svn-id: svn://busybox.net/trunk/busybox@3650 69ca8d6d-28ef-0310-b511-8ec308f3f277
| -rw-r--r-- | include/inet_common.h | 30 | ||||
| -rw-r--r-- | libbb/inet_common.c | 174 |
2 files changed, 204 insertions, 0 deletions
diff --git a/include/inet_common.h b/include/inet_common.h new file mode 100644 index 000000000..2c5e8f673 --- /dev/null +++ b/include/inet_common.h | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | /* | ||
| 2 | * stolen from net-tools-1.59 and stripped down for busybox by | ||
| 3 | * Erik Andersen <andersee@debian.org> | ||
| 4 | * | ||
| 5 | * Heavily modified by Manuel Novoa III Mar 12, 2001 | ||
| 6 | * | ||
| 7 | * Version: $Id: inet_common.h,v 1.1 2001/11/10 12:18:42 andersen Exp $ | ||
| 8 | * | ||
| 9 | */ | ||
| 10 | |||
| 11 | #include <features.h> | ||
| 12 | #include <sys/types.h> | ||
| 13 | #include <sys/socket.h> | ||
| 14 | #include <arpa/inet.h> | ||
| 15 | |||
| 16 | |||
| 17 | extern const char bb_INET_default[]; /* = "default" */ | ||
| 18 | |||
| 19 | /* hostfirst!=0 If we expect this to be a hostname, | ||
| 20 | try hostname database first | ||
| 21 | */ | ||
| 22 | extern int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst); | ||
| 23 | |||
| 24 | |||
| 25 | /* numeric: & 0x8000: default instead of *, | ||
| 26 | * & 0x4000: host instead of net, | ||
| 27 | * & 0x0fff: don't resolve | ||
| 28 | */ | ||
| 29 | extern int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in, | ||
| 30 | int numeric, unsigned int netmask); | ||
diff --git a/libbb/inet_common.c b/libbb/inet_common.c new file mode 100644 index 000000000..0261d438a --- /dev/null +++ b/libbb/inet_common.c | |||
| @@ -0,0 +1,174 @@ | |||
| 1 | /* | ||
| 2 | * stolen from net-tools-1.59 and stripped down for busybox by | ||
| 3 | * Erik Andersen <andersee@debian.org> | ||
| 4 | * | ||
| 5 | * Heavily modified by Manuel Novoa III Mar 12, 2001 | ||
| 6 | * | ||
| 7 | * Version: $Id: inet_common.c,v 1.1 2001/11/10 12:18:42 andersen Exp $ | ||
| 8 | * | ||
| 9 | */ | ||
| 10 | |||
| 11 | #include "inet_common.h" | ||
| 12 | #include <stdio.h> | ||
| 13 | #include <errno.h> | ||
| 14 | #include <stdlib.h> | ||
| 15 | #include <string.h> | ||
| 16 | #include <unistd.h> | ||
| 17 | #include "libbb.h" | ||
| 18 | |||
| 19 | const char bb_INET_default[]="default"; | ||
| 20 | |||
| 21 | int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst) | ||
| 22 | { | ||
| 23 | struct hostent *hp; | ||
| 24 | struct netent *np; | ||
| 25 | |||
| 26 | /* Grmpf. -FvK */ | ||
| 27 | s_in->sin_family = AF_INET; | ||
| 28 | s_in->sin_port = 0; | ||
| 29 | |||
| 30 | /* Default is special, meaning 0.0.0.0. */ | ||
| 31 | if (!strcmp(name, bb_INET_default)) { | ||
| 32 | s_in->sin_addr.s_addr = INADDR_ANY; | ||
| 33 | return (1); | ||
| 34 | } | ||
| 35 | /* Look to see if it's a dotted quad. */ | ||
| 36 | if (inet_aton(name, &s_in->sin_addr)) { | ||
| 37 | return 0; | ||
| 38 | } | ||
| 39 | /* If we expect this to be a hostname, try hostname database first */ | ||
| 40 | #ifdef DEBUG | ||
| 41 | if (hostfirst) fprintf (stderr, "gethostbyname (%s)\n", name); | ||
| 42 | #endif | ||
| 43 | if (hostfirst && | ||
| 44 | (hp = gethostbyname(name)) != (struct hostent *) NULL) { | ||
| 45 | memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0], | ||
| 46 | sizeof(struct in_addr)); | ||
| 47 | return 0; | ||
| 48 | } | ||
| 49 | /* Try the NETWORKS database to see if this is a known network. */ | ||
| 50 | #ifdef DEBUG | ||
| 51 | fprintf (stderr, "getnetbyname (%s)\n", name); | ||
| 52 | #endif | ||
| 53 | if ((np = getnetbyname(name)) != (struct netent *) NULL) { | ||
| 54 | s_in->sin_addr.s_addr = htonl(np->n_net); | ||
| 55 | return 1; | ||
| 56 | } | ||
| 57 | if (hostfirst) { | ||
| 58 | /* Don't try again */ | ||
| 59 | errno = h_errno; | ||
| 60 | return -1; | ||
| 61 | } | ||
| 62 | #ifdef DEBUG | ||
| 63 | res_init(); | ||
| 64 | _res.options |= RES_DEBUG; | ||
| 65 | #endif | ||
| 66 | |||
| 67 | #ifdef DEBUG | ||
| 68 | fprintf (stderr, "gethostbyname (%s)\n", name); | ||
| 69 | #endif | ||
| 70 | if ((hp = gethostbyname(name)) == (struct hostent *) NULL) { | ||
| 71 | errno = h_errno; | ||
| 72 | return -1; | ||
| 73 | } | ||
| 74 | memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0], | ||
| 75 | sizeof(struct in_addr)); | ||
| 76 | |||
| 77 | return 0; | ||
| 78 | } | ||
| 79 | |||
| 80 | /* cache */ | ||
| 81 | struct addr { | ||
| 82 | struct sockaddr_in addr; | ||
| 83 | char *name; | ||
| 84 | int host; | ||
| 85 | struct addr *next; | ||
| 86 | }; | ||
| 87 | |||
| 88 | static struct addr *INET_nn = NULL; /* addr-to-name cache */ | ||
| 89 | |||
| 90 | /* numeric: & 0x8000: default instead of *, | ||
| 91 | * & 0x4000: host instead of net, | ||
| 92 | * & 0x0fff: don't resolve | ||
| 93 | */ | ||
| 94 | int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in, | ||
| 95 | int numeric, unsigned int netmask) | ||
| 96 | { | ||
| 97 | struct hostent *ent; | ||
| 98 | struct netent *np; | ||
| 99 | struct addr *pn; | ||
| 100 | unsigned long ad, host_ad; | ||
| 101 | int host = 0; | ||
| 102 | |||
| 103 | /* Grmpf. -FvK */ | ||
| 104 | if (s_in->sin_family != AF_INET) { | ||
| 105 | #ifdef DEBUG | ||
| 106 | fprintf(stderr, _("rresolve: unsupport address family %d !\n"), s_in->sin_family); | ||
| 107 | #endif | ||
| 108 | errno = EAFNOSUPPORT; | ||
| 109 | return (-1); | ||
| 110 | } | ||
| 111 | ad = (unsigned long) s_in->sin_addr.s_addr; | ||
| 112 | #ifdef DEBUG | ||
| 113 | fprintf (stderr, "rresolve: %08lx, mask %08x, num %08x \n", ad, netmask, numeric); | ||
| 114 | #endif | ||
| 115 | if (ad == INADDR_ANY) { | ||
| 116 | if ((numeric & 0x0FFF) == 0) { | ||
| 117 | if (numeric & 0x8000) | ||
| 118 | safe_strncpy(name, bb_INET_default, len); | ||
| 119 | else | ||
| 120 | safe_strncpy(name, "*", len); | ||
| 121 | return (0); | ||
| 122 | } | ||
| 123 | } | ||
| 124 | if (numeric & 0x0FFF) { | ||
| 125 | safe_strncpy(name, inet_ntoa(s_in->sin_addr), len); | ||
| 126 | return (0); | ||
| 127 | } | ||
| 128 | |||
| 129 | if ((ad & (~netmask)) != 0 || (numeric & 0x4000)) | ||
| 130 | host = 1; | ||
| 131 | #if 0 | ||
| 132 | INET_nn = NULL; | ||
| 133 | #endif | ||
| 134 | pn = INET_nn; | ||
| 135 | while (pn != NULL) { | ||
| 136 | if (pn->addr.sin_addr.s_addr == ad && pn->host == host) { | ||
| 137 | safe_strncpy(name, pn->name, len); | ||
| 138 | #ifdef DEBUG | ||
| 139 | fprintf (stderr, "rresolve: found %s %08lx in cache\n", (host? "host": "net"), ad); | ||
| 140 | #endif | ||
| 141 | return (0); | ||
| 142 | } | ||
| 143 | pn = pn->next; | ||
| 144 | } | ||
| 145 | |||
| 146 | host_ad = ntohl(ad); | ||
| 147 | np = NULL; | ||
| 148 | ent = NULL; | ||
| 149 | if (host) { | ||
| 150 | #ifdef DEBUG | ||
| 151 | fprintf (stderr, "gethostbyaddr (%08lx)\n", ad); | ||
| 152 | #endif | ||
| 153 | ent = gethostbyaddr((char *) &ad, 4, AF_INET); | ||
| 154 | if (ent != NULL) | ||
| 155 | safe_strncpy(name, ent->h_name, len); | ||
| 156 | } else { | ||
| 157 | #ifdef DEBUG | ||
| 158 | fprintf (stderr, "getnetbyaddr (%08lx)\n", host_ad); | ||
| 159 | #endif | ||
| 160 | np = getnetbyaddr(host_ad, AF_INET); | ||
| 161 | if (np != NULL) | ||
| 162 | safe_strncpy(name, np->n_name, len); | ||
| 163 | } | ||
| 164 | if ((ent == NULL) && (np == NULL)) | ||
| 165 | safe_strncpy(name, inet_ntoa(s_in->sin_addr), len); | ||
| 166 | pn = (struct addr *) xmalloc(sizeof(struct addr)); | ||
| 167 | pn->addr = *s_in; | ||
| 168 | pn->next = INET_nn; | ||
| 169 | pn->host = host; | ||
| 170 | pn->name = xstrdup(name); | ||
| 171 | INET_nn = pn; | ||
| 172 | |||
| 173 | return (0); | ||
| 174 | } | ||
