2005-04-25 18:20:15 +00:00
|
|
|
/* $FreeBSD$ */
|
2005-04-25 17:31:50 +00:00
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
/*
|
|
|
|
* Based upon 4.4BSD's /usr/sbin/arp
|
|
|
|
*/
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <net/if.h>
|
2005-04-25 17:31:50 +00:00
|
|
|
#if __FreeBSD_version >= 300000
|
|
|
|
# include <net/if_var.h>
|
|
|
|
#endif
|
1997-02-09 22:50:16 +00:00
|
|
|
#include <net/if_dl.h>
|
|
|
|
#include <net/if_types.h>
|
2007-06-04 02:54:36 +00:00
|
|
|
#ifndef __osf__
|
|
|
|
# include <net/route.h>
|
|
|
|
#endif
|
1997-02-09 22:50:16 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/if_ether.h>
|
|
|
|
#include <arpa/inet.h>
|
2005-04-25 17:31:50 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <netinet/ip_var.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
1997-02-09 22:50:16 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <nlist.h>
|
|
|
|
#include <stdio.h>
|
1997-04-03 10:22:02 +00:00
|
|
|
#include "ipsend.h"
|
1999-11-08 20:51:23 +00:00
|
|
|
#include "iplang/iplang.h"
|
1997-04-03 10:22:02 +00:00
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* lookup host and return
|
|
|
|
* its IP address in address
|
|
|
|
* (4 bytes)
|
|
|
|
*/
|
2005-04-25 17:31:50 +00:00
|
|
|
int resolve(host, address)
|
2013-08-11 14:28:45 +00:00
|
|
|
char *host, *address;
|
1997-02-09 22:50:16 +00:00
|
|
|
{
|
|
|
|
struct hostent *hp;
|
|
|
|
u_long add;
|
|
|
|
|
|
|
|
add = inet_addr(host);
|
|
|
|
if (add == -1)
|
|
|
|
{
|
|
|
|
if (!(hp = gethostbyname(host)))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "unknown host: %s\n", host);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
bcopy((char *)hp->h_addr, (char *)address, 4);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
bcopy((char*)&add, address, 4);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int arp(addr, eaddr)
|
2013-08-11 14:28:45 +00:00
|
|
|
char *addr, *eaddr;
|
1997-02-09 22:50:16 +00:00
|
|
|
{
|
|
|
|
int mib[6];
|
|
|
|
size_t needed;
|
1997-03-02 15:40:21 +00:00
|
|
|
char *lim, *buf, *next;
|
1997-02-09 22:50:16 +00:00
|
|
|
struct rt_msghdr *rtm;
|
2013-01-31 08:55:21 +00:00
|
|
|
struct sockaddr_in *sin;
|
1997-02-09 22:50:16 +00:00
|
|
|
struct sockaddr_dl *sdl;
|
|
|
|
|
1999-11-08 20:51:23 +00:00
|
|
|
#ifdef IPSEND
|
2000-10-26 12:45:54 +00:00
|
|
|
if (arp_getipv4(addr, ether) == 0)
|
1999-11-08 20:51:23 +00:00
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
2005-04-25 17:31:50 +00:00
|
|
|
if (!addr)
|
|
|
|
return -1;
|
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
mib[0] = CTL_NET;
|
|
|
|
mib[1] = PF_ROUTE;
|
|
|
|
mib[2] = 0;
|
|
|
|
mib[3] = AF_INET;
|
|
|
|
mib[4] = NET_RT_FLAGS;
|
This main goals of this project are:
1. separating L2 tables (ARP, NDP) from the L3 routing tables
2. removing as much locking dependencies among these layers as
possible to allow for some parallelism in the search operations
3. simplify the logic in the routing code,
The most notable end result is the obsolescent of the route
cloning (RTF_CLONING) concept, which translated into code reduction
in both IPv4 ARP and IPv6 NDP related modules, and size reduction in
struct rtentry{}. The change in design obsoletes the semantics of
RTF_CLONING, RTF_WASCLONE and RTF_LLINFO routing flags. The userland
applications such as "arp" and "ndp" have been modified to reflect
those changes. The output from "netstat -r" shows only the routing
entries.
Quite a few developers have contributed to this project in the
past: Glebius Smirnoff, Luigi Rizzo, Alessandro Cerri, and
Andre Oppermann. And most recently:
- Kip Macy revised the locking code completely, thus completing
the last piece of the puzzle, Kip has also been conducting
active functional testing
- Sam Leffler has helped me improving/refactoring the code, and
provided valuable reviews
- Julian Elischer setup the perforce tree for me and has helped
me maintaining that branch before the svn conversion
2008-12-15 06:10:57 +00:00
|
|
|
#ifdef RTF_LLINFO
|
1997-02-09 22:50:16 +00:00
|
|
|
mib[5] = RTF_LLINFO;
|
This main goals of this project are:
1. separating L2 tables (ARP, NDP) from the L3 routing tables
2. removing as much locking dependencies among these layers as
possible to allow for some parallelism in the search operations
3. simplify the logic in the routing code,
The most notable end result is the obsolescent of the route
cloning (RTF_CLONING) concept, which translated into code reduction
in both IPv4 ARP and IPv6 NDP related modules, and size reduction in
struct rtentry{}. The change in design obsoletes the semantics of
RTF_CLONING, RTF_WASCLONE and RTF_LLINFO routing flags. The userland
applications such as "arp" and "ndp" have been modified to reflect
those changes. The output from "netstat -r" shows only the routing
entries.
Quite a few developers have contributed to this project in the
past: Glebius Smirnoff, Luigi Rizzo, Alessandro Cerri, and
Andre Oppermann. And most recently:
- Kip Macy revised the locking code completely, thus completing
the last piece of the puzzle, Kip has also been conducting
active functional testing
- Sam Leffler has helped me improving/refactoring the code, and
provided valuable reviews
- Julian Elischer setup the perforce tree for me and has helped
me maintaining that branch before the svn conversion
2008-12-15 06:10:57 +00:00
|
|
|
#else
|
|
|
|
mib[5] = 0;
|
|
|
|
#endif
|
|
|
|
|
1997-02-09 22:50:16 +00:00
|
|
|
if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1)
|
|
|
|
{
|
|
|
|
perror("route-sysctl-estimate");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
if ((buf = malloc(needed)) == NULL)
|
|
|
|
{
|
|
|
|
perror("malloc");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1)
|
|
|
|
{
|
|
|
|
perror("actual retrieval of routing table");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
lim = buf + needed;
|
|
|
|
for (next = buf; next < lim; next += rtm->rtm_msglen)
|
|
|
|
{
|
|
|
|
rtm = (struct rt_msghdr *)next;
|
2013-01-31 08:55:21 +00:00
|
|
|
sin = (struct sockaddr_in *)(rtm + 1);
|
1997-02-09 22:50:16 +00:00
|
|
|
sdl = (struct sockaddr_dl *)(sin + 1);
|
2005-04-25 17:31:50 +00:00
|
|
|
if (!bcmp(addr, (char *)&sin->sin_addr,
|
|
|
|
sizeof(struct in_addr)))
|
1997-02-09 22:50:16 +00:00
|
|
|
{
|
|
|
|
bcopy(LLADDR(sdl), eaddr, sdl->sdl_alen);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|