2 * $Id: libnet_resolve.c,v 1.1.1.1 2000/05/25 00:28:49 route Exp $
5 * libnet_resolve.c - various name resolution type routines
7 * Copyright (c) 1998 - 2001 Mike D. Schiffman <mike@infonexus.com>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "../include/config.h"
36 #include "../include/libnet.h"
39 libnet_host_lookup(u_long in, u_short use_name)
41 static u_char hostname[512], hostname2[512];
45 struct hostent *host_ent = NULL;
49 * Swap to the other buffer. We swap static buffers to avoid having to
50 * pass in a char *. This makes the code that calls this function more
51 * intuitive, but makes this function ugly. This function is seriously
52 * non-reentrant. For threaded applications (or for signal handler code)
53 * use host_lookup_r().
57 if (use_name == LIBNET_RESOLVE)
60 host_ent = gethostbyaddr((char *)&addr, sizeof(struct in_addr), AF_INET);
66 sprintf(((which % 2) ? hostname : hostname2), "%d.%d.%d.%d",
67 (p[0] & 255), (p[1] & 255), (p[2] & 255), (p[3] & 255));
69 else if (use_name == LIBNET_RESOLVE)
71 strncpy(((which % 2) ? hostname : hostname2), host_ent->h_name,
74 return (which % 2) ? (hostname) : (hostname2);
79 libnet_host_lookup_r(u_long in, u_short use_name, u_char *hostname)
82 struct hostent *host_ent = NULL;
85 if (use_name == LIBNET_RESOLVE)
88 host_ent = gethostbyaddr((char *)&addr, sizeof(struct in_addr), AF_INET);
94 sprintf(hostname, "%d.%d.%d.%d",
95 (p[0] & 255), (p[1] & 255), (p[2] & 255), (p[3] & 255));
99 /* XXX - sizeof(hostname) == 4 bytes you moron. FIX THAT. - r */
100 strncpy(hostname, host_ent->h_name, sizeof(hostname));
106 libnet_name_resolve(u_char *host_name, u_short use_name)
109 struct hostent *host_ent;
114 if (use_name == LIBNET_RESOLVE)
116 if ((addr.s_addr = inet_addr(host_name)) == -1)
118 if (!(host_ent = gethostbyname(host_name)))
122 memcpy((char *)&addr.s_addr, host_ent->h_addr, host_ent->h_length);
124 return (addr.s_addr);
129 * We only want dots 'n decimals.
131 if (!isdigit(host_name[0]))
137 for (i = 0; i < 4; i++)
143 while (*host_name && *host_name != '.')
146 val += *host_name - '0';
166 libnet_get_ipaddr(struct libnet_link_int *l, const char *device, char *ebuf)
169 register struct sockaddr_in *sin;
173 * Create dummy socket to perform an ioctl upon.
175 fd = socket(PF_INET, SOCK_DGRAM, 0);
178 sprintf(ebuf, "socket: %s", strerror(errno));
182 memset(&ifr, 0, sizeof(ifr));
183 sin = (struct sockaddr_in *)&ifr.ifr_addr;
184 strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
186 ifr.ifr_addr.sa_family = AF_INET;
188 if (ioctl(fd, SIOCGIFADDR, (char*) &ifr) < 0)
194 return (ntohl(sin->sin_addr.s_addr));
199 * get_hwaddr routine moved to arch specifc files (sockpacket.c, bpf.c, etc)