aboutsummaryrefslogtreecommitdiffstats
path: root/resolv.c
diff options
context:
space:
mode:
authorGuy Harris <guy@alum.mit.edu>1999-10-11 03:03:12 +0000
committerGuy Harris <guy@alum.mit.edu>1999-10-11 03:03:12 +0000
commit29b9c8a2850ae3be73d1fce38a1e96591ba165fb (patch)
tree2f4a35a2ca11bf7a6c31bd3079ab48e12ed9b957 /resolv.c
parent5779d0b754957b7b0b2921a0beef14eba7ac3250 (diff)
Have "get_host_ipaddr()" return a Boolean indicating whether it
succeeded or failed, and, if it succeeded, have it fill in the IP address if found through a pointer passed as the second argument. Have it first try interpreting its first argument as a dotted-quad IP address, with "inet_aton()", and, if that fails, have it try to interpret it as a host name with "gethostbyname()"; don't bother with "gethostbyaddr()", as we should be allowed to filter on IP addresses even if there's no host name associated with them (there's no guarantee that "gethostbyaddr()" will succeed if handed an IP address with no corresponding name - and it looks as if FreeBSD 3.2, at least, may not succeed in that case). Add a "dfilter_fail()" routine that takes "printf()"-like arguments and uses them to set an error message for the parse; doing so means that even if the filter expression is syntactically valid, we treat it as being invalid. (Is there a better way to force a parse to fail from arbitrary places in routines called by the parser?) Use that routine in the lexical analyzer. If that error message was set, use it as is as the failure message, rather than adding "Unable to parse filter string XXX" to it. Have the code to handle IP addresses and host names in display filters check whether "get_host_ipaddr()" succeeded or failed and, if it failed, arrange that the parse fail with an error message indicating the source of the problem. svn path=/trunk/; revision=802
Diffstat (limited to 'resolv.c')
-rw-r--r--resolv.c33
1 files changed, 21 insertions, 12 deletions
diff --git a/resolv.c b/resolv.c
index ad408880e5..8048f2e15d 100644
--- a/resolv.c
+++ b/resolv.c
@@ -1,7 +1,7 @@
/* resolv.c
* Routines for network object lookup
*
- * $Id: resolv.c,v 1.12 1999/09/26 14:39:12 deniel Exp $
+ * $Id: resolv.c,v 1.13 1999/10/11 03:03:11 guy Exp $
*
* Laurent Deniel <deniel@worldnet.fr>
*
@@ -56,6 +56,8 @@
#include <netdb.h>
#endif
+#include <arpa/inet.h>
+
#include <signal.h>
#ifdef HAVE_SYS_SOCKET_H
@@ -847,22 +849,29 @@ extern u_char *get_manuf_name(u_char *addr)
-/* return IP address of either hostname or IP address in text format.
+/* Translate a string, assumed either to be a dotted-quad IP address or
+ * a host name, to a numeric IP address. Return TRUE if we succeed and
+ * set "*addrp" to that numeric IP address; return FALSE if we fail.
* Used more in the dfilter parser rather than in packet dissectors */
-unsigned long get_host_ipaddr(const char *host)
+gboolean get_host_ipaddr(const char *host, guint32 *addrp)
{
- struct hostent *hp = NULL;
- unsigned long ipaddr;
+ struct in_addr ipaddr;
+ struct hostent *hp;
- hp = gethostbyname(host);
- if (hp == NULL) {
- hp = gethostbyaddr(host, strlen(host), AF_INET);
+ if (!inet_aton(host, &ipaddr)) {
+ /* It's not a valid dotted-quad IP address; is it a valid
+ * host name? */
+ hp = gethostbyname(host);
if (hp == NULL) {
- return 0;
+ /* No. */
+ return FALSE;
+ } else {
+ /* XXX - is "hp->h_length" the size of a
+ * "struct in_addr"? It should be. */
+ memcpy(&ipaddr, hp->h_addr, hp->h_length);
}
}
- memcpy(&ipaddr, hp->h_addr, hp->h_length);
-
- return ntohl(ipaddr);
+ *addrp = ntohl(ipaddr.s_addr);
+ return TRUE;
}