Moving arp, ifconfig, ping, route and traceroute dirs from /current/src/apps/. to
/current/src/apps/bin/., where they belongs. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@893 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -0,0 +1,2 @@
|
|||||||
|
SubDir OBOS_TOP src apps bin arp ;
|
||||||
|
|
||||||
@@ -0,0 +1,636 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 1984, 1993
|
||||||
|
* The Regents of the University of California. All rights reserved.
|
||||||
|
*
|
||||||
|
* This code is derived from software contributed to Berkeley by
|
||||||
|
* Sun Microsystems, Inc.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. All advertising materials mentioning features or use of this software
|
||||||
|
* must display the following acknowledgement:
|
||||||
|
* This product includes software developed by the University of
|
||||||
|
* California, Berkeley and its contributors.
|
||||||
|
* 4. Neither the name of the University nor the names of its contributors
|
||||||
|
* may be used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||||
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||||
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||||
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* arp - display, set, and delete arp table entries
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sys/param.h>
|
||||||
|
#include <sys/file.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
//#include <sys/sysctl.h>
|
||||||
|
|
||||||
|
#include <net/if.h>
|
||||||
|
#include <net/if_dl.h>
|
||||||
|
#include <net/if_types.h>
|
||||||
|
#include <net/route.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <netinet/if_ether.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <errno.h>
|
||||||
|
//#include <err.h>
|
||||||
|
//#include <nlist.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
//#include <paths.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
|
||||||
|
int delete (const char *, const char *);
|
||||||
|
void search(in_addr_t addr, void (*action)(struct sockaddr_dl *sdl,
|
||||||
|
struct sockaddr_inarp *sin, struct rt_msghdr *rtm));
|
||||||
|
void print_entry(struct sockaddr_dl *sdl,
|
||||||
|
struct sockaddr_inarp *sin, struct rt_msghdr *rtm);
|
||||||
|
void nuke_entry(struct sockaddr_dl *sdl,
|
||||||
|
struct sockaddr_inarp *sin, struct rt_msghdr *rtm);
|
||||||
|
void ether_print __P((const u_char *));
|
||||||
|
int file (char *);
|
||||||
|
int get (const char *);
|
||||||
|
int getinetaddr (const char *, struct in_addr *);
|
||||||
|
void getsocket (void);
|
||||||
|
int rtmsg (int);
|
||||||
|
int set (int, char **);
|
||||||
|
void usage (void);
|
||||||
|
|
||||||
|
static int pid;
|
||||||
|
static int nflag; /* no reverse dns lookups */
|
||||||
|
static int aflag; /* do it for all entries */
|
||||||
|
static int s = -1;
|
||||||
|
|
||||||
|
/* which function we're supposed to do */
|
||||||
|
#define F_GET 1
|
||||||
|
#define F_SET 2
|
||||||
|
#define F_FILESET 3
|
||||||
|
#define F_DELETE 4
|
||||||
|
|
||||||
|
void err(int error, char *msg)
|
||||||
|
{
|
||||||
|
printf("Error: %s\n", msg);
|
||||||
|
printf("Code: %d\n", error);
|
||||||
|
printf("Desc: %s\n", strerror(error));
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(argc, argv)
|
||||||
|
int argc;
|
||||||
|
char **argv;
|
||||||
|
{
|
||||||
|
int ch, func, rtn;
|
||||||
|
|
||||||
|
pid = getpid();
|
||||||
|
opterr = 0;
|
||||||
|
func = 0;
|
||||||
|
|
||||||
|
while ((ch = getopt(argc, argv, "andsf")) != -1) {
|
||||||
|
switch ((char)ch) {
|
||||||
|
case 'a':
|
||||||
|
aflag = 1;
|
||||||
|
break;
|
||||||
|
case 'n':
|
||||||
|
nflag = 1;
|
||||||
|
break;
|
||||||
|
case 'd':
|
||||||
|
if (func)
|
||||||
|
usage();
|
||||||
|
func = F_DELETE;
|
||||||
|
break;
|
||||||
|
case 's':
|
||||||
|
if (func)
|
||||||
|
usage();
|
||||||
|
func = F_SET;
|
||||||
|
break;
|
||||||
|
case 'f':
|
||||||
|
if (func)
|
||||||
|
usage();
|
||||||
|
func = F_FILESET;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
usage();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
argc -= optind;
|
||||||
|
argv += optind;
|
||||||
|
|
||||||
|
if (!func)
|
||||||
|
func = F_GET;
|
||||||
|
rtn = 0;
|
||||||
|
|
||||||
|
switch (func) {
|
||||||
|
case F_GET:
|
||||||
|
if (aflag && argc == 0)
|
||||||
|
search(0, print_entry);
|
||||||
|
else if (!aflag && argc == 1)
|
||||||
|
rtn = get(argv[0]);
|
||||||
|
else
|
||||||
|
usage();
|
||||||
|
break;
|
||||||
|
case F_SET:
|
||||||
|
if (argc < 2 || argc > 5)
|
||||||
|
usage();
|
||||||
|
rtn = set(argc, argv) ? 1 : 0;
|
||||||
|
break;
|
||||||
|
case F_DELETE:
|
||||||
|
if (aflag && argc == 0)
|
||||||
|
search(0, nuke_entry);
|
||||||
|
else if (!aflag && argc == 1)
|
||||||
|
rtn = delete(argv[0], argv[1]);
|
||||||
|
else
|
||||||
|
usage();
|
||||||
|
break;
|
||||||
|
case F_FILESET:
|
||||||
|
if (argc != 1)
|
||||||
|
usage();
|
||||||
|
rtn = file(argv[0]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return (rtn);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Process a file to set standard arp entries
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
file(name)
|
||||||
|
char *name;
|
||||||
|
{
|
||||||
|
char line[100], arg[5][50], *args[5];
|
||||||
|
int i, retval;
|
||||||
|
FILE *fp;
|
||||||
|
|
||||||
|
if ((fp = fopen(name, "r")) == NULL) {
|
||||||
|
printf("cannot open %s", name);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
args[0] = &arg[0][0];
|
||||||
|
args[1] = &arg[1][0];
|
||||||
|
args[2] = &arg[2][0];
|
||||||
|
args[3] = &arg[3][0];
|
||||||
|
args[4] = &arg[4][0];
|
||||||
|
retval = 0;
|
||||||
|
while (fgets(line, 100, fp) != NULL) {
|
||||||
|
i = sscanf(line, "%49s %49s %49s %49s %49s", arg[0], arg[1], arg[2],
|
||||||
|
arg[3], arg[4]);
|
||||||
|
if (i < 2) {
|
||||||
|
printf("bad line: %s", line);
|
||||||
|
retval = 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (set(i, args))
|
||||||
|
retval = 1;
|
||||||
|
}
|
||||||
|
fclose(fp);
|
||||||
|
return (retval);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
getsocket()
|
||||||
|
{
|
||||||
|
if (s >= 0)
|
||||||
|
return;
|
||||||
|
s = socket(PF_ROUTE, SOCK_RAW, 0);
|
||||||
|
if (s < 0)
|
||||||
|
err(1, "socket");
|
||||||
|
}
|
||||||
|
|
||||||
|
struct sockaddr_in so_mask = {8, 0, 0, { 0xffffffff}};
|
||||||
|
struct sockaddr_inarp blank_sin = {sizeof(blank_sin), AF_INET }, sin_m;
|
||||||
|
struct sockaddr_dl blank_sdl = {sizeof(blank_sdl), AF_LINK }, sdl_m;
|
||||||
|
int expire_time, flags, export_only, doing_proxy, found_entry;
|
||||||
|
struct {
|
||||||
|
struct rt_msghdr m_rtm;
|
||||||
|
char m_space[512];
|
||||||
|
} m_rtmsg;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Set an individual arp entry
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
set(argc, argv)
|
||||||
|
int argc;
|
||||||
|
char **argv;
|
||||||
|
{
|
||||||
|
register struct sockaddr_inarp *sin;
|
||||||
|
register struct sockaddr_dl *sdl;
|
||||||
|
register struct rt_msghdr *rtm;
|
||||||
|
u_char *eaddr;
|
||||||
|
struct ether_addr *ea;
|
||||||
|
char *host = argv[0];
|
||||||
|
|
||||||
|
sin = &sin_m;
|
||||||
|
rtm = &(m_rtmsg.m_rtm);
|
||||||
|
eaddr = argv[1];
|
||||||
|
|
||||||
|
getsocket();
|
||||||
|
argc -= 2;
|
||||||
|
argv += 2;
|
||||||
|
sdl_m = blank_sdl; /* struct copy */
|
||||||
|
sin_m = blank_sin; /* struct copy */
|
||||||
|
if (getinetaddr(host, &sin->sin_addr) == -1)
|
||||||
|
return (1);
|
||||||
|
ea = ether_aton(eaddr);
|
||||||
|
if (ea == NULL) {
|
||||||
|
printf("invalid ethernet address: %s\n", eaddr);
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
memcpy(LLADDR(&sdl_m), ea, sizeof(*ea));
|
||||||
|
sdl_m.sdl_alen = 6;
|
||||||
|
doing_proxy = flags = export_only = expire_time = 0;
|
||||||
|
while (argc-- > 0) {
|
||||||
|
if (strncmp(argv[0], "temp", 4) == 0) {
|
||||||
|
struct timeval time;
|
||||||
|
(void)gettimeofday(&time, 0);
|
||||||
|
expire_time = time.tv_sec + 20 * 60;
|
||||||
|
if (flags & RTF_PERMANENT_ARP) {
|
||||||
|
/* temp or permanent, not both */
|
||||||
|
usage();
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (strncmp(argv[0], "pub", 3) == 0) {
|
||||||
|
flags |= RTF_ANNOUNCE;
|
||||||
|
doing_proxy = SIN_PROXY;
|
||||||
|
}
|
||||||
|
else if (strncmp(argv[0], "permanent", 9) == 0) {
|
||||||
|
flags |= RTF_PERMANENT_ARP;
|
||||||
|
if (expire_time != 0) {
|
||||||
|
/* temp or permanent, not both */
|
||||||
|
usage();
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
} else if (strncmp(argv[0], "trail", 5) == 0) {
|
||||||
|
(void)printf(
|
||||||
|
"%s: Sending trailers is no longer supported\n",
|
||||||
|
host);
|
||||||
|
}
|
||||||
|
argv++;
|
||||||
|
}
|
||||||
|
tryagain:
|
||||||
|
if (rtmsg(RTM_GET) < 0) {
|
||||||
|
printf("%s\n", host);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
sin = (struct sockaddr_inarp *)(rtm + 1);
|
||||||
|
sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
|
||||||
|
if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
|
||||||
|
if (sdl->sdl_family == AF_LINK &&
|
||||||
|
(rtm->rtm_flags & RTF_LLINFO) &&
|
||||||
|
!(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
|
||||||
|
case IFT_ETHER:
|
||||||
|
/*
|
||||||
|
case IFT_FDDI:
|
||||||
|
case IFT_ISO88023:
|
||||||
|
case IFT_ISO88024:
|
||||||
|
case IFT_ISO88025:
|
||||||
|
*/
|
||||||
|
goto overwrite;
|
||||||
|
}
|
||||||
|
if (doing_proxy == 0) {
|
||||||
|
(void)printf("set: can only proxy for %s\n", host);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
if (sin_m.sin_other & SIN_PROXY) {
|
||||||
|
(void)printf(
|
||||||
|
"set: proxy entry exists for non 802 device\n");
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
sin_m.sin_other = SIN_PROXY;
|
||||||
|
export_only = 1;
|
||||||
|
goto tryagain;
|
||||||
|
}
|
||||||
|
overwrite:
|
||||||
|
if (sdl->sdl_family != AF_LINK) {
|
||||||
|
(void)printf("cannot intuit interface index and type for %s\n",
|
||||||
|
host);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
sdl_m.sdl_type = sdl->sdl_type;
|
||||||
|
sdl_m.sdl_index = sdl->sdl_index;
|
||||||
|
return (rtmsg(RTM_ADD));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Display an individual arp entry
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
get(host)
|
||||||
|
const char *host;
|
||||||
|
{
|
||||||
|
struct sockaddr_inarp *sin;
|
||||||
|
|
||||||
|
sin = &sin_m;
|
||||||
|
sin_m = blank_sin; /* struct copy */
|
||||||
|
if (getinetaddr(host, &sin->sin_addr) == -1)
|
||||||
|
exit(1);
|
||||||
|
search(sin->sin_addr.s_addr, print_entry);
|
||||||
|
if (found_entry == 0) {
|
||||||
|
(void)printf("%s (%s) -- no entry\n", host,
|
||||||
|
inet_ntoa(sin->sin_addr));
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Delete an arp entry
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
delete(host, info)
|
||||||
|
const char *host;
|
||||||
|
const char *info;
|
||||||
|
{
|
||||||
|
register struct sockaddr_inarp *sin;
|
||||||
|
register struct rt_msghdr *rtm;
|
||||||
|
struct sockaddr_dl *sdl;
|
||||||
|
|
||||||
|
sin = &sin_m;
|
||||||
|
rtm = &m_rtmsg.m_rtm;
|
||||||
|
|
||||||
|
if (info && strncmp(info, "pro", 3) )
|
||||||
|
export_only = 1;
|
||||||
|
getsocket();
|
||||||
|
sin_m = blank_sin; /* struct copy */
|
||||||
|
if (getinetaddr(host, &sin->sin_addr) == -1)
|
||||||
|
return (1);
|
||||||
|
tryagain:
|
||||||
|
if (rtmsg(RTM_GET) < 0) {
|
||||||
|
printf("%s\n", host);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
sin = (struct sockaddr_inarp *)(rtm + 1);
|
||||||
|
sdl = (struct sockaddr_dl *)(sin->sin_len + (char *)sin);
|
||||||
|
if (sin->sin_addr.s_addr == sin_m.sin_addr.s_addr) {
|
||||||
|
if (sdl->sdl_family == AF_LINK &&
|
||||||
|
(rtm->rtm_flags & RTF_LLINFO) &&
|
||||||
|
!(rtm->rtm_flags & RTF_GATEWAY)) switch (sdl->sdl_type) {
|
||||||
|
case IFT_ETHER:
|
||||||
|
/*
|
||||||
|
case IFT_FDDI:
|
||||||
|
case IFT_ISO88023:
|
||||||
|
case IFT_ISO88024:
|
||||||
|
case IFT_ISO88025:
|
||||||
|
*/
|
||||||
|
goto delete;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (sin_m.sin_other & SIN_PROXY) {
|
||||||
|
printf("delete: can't locate %s\n", host);
|
||||||
|
return (1);
|
||||||
|
} else {
|
||||||
|
sin_m.sin_other = SIN_PROXY;
|
||||||
|
goto tryagain;
|
||||||
|
}
|
||||||
|
delete:
|
||||||
|
if (sdl->sdl_family != AF_LINK) {
|
||||||
|
(void)printf("cannot locate %s\n", host);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
if (rtmsg(RTM_DELETE))
|
||||||
|
return (1);
|
||||||
|
(void)printf("%s (%s) deleted\n", host, inet_ntoa(sin->sin_addr));
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Search the entire arp table, and do some action on matching entries.
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
search(addr, action)
|
||||||
|
in_addr_t addr;
|
||||||
|
void (*action)(struct sockaddr_dl *sdl,
|
||||||
|
struct sockaddr_inarp *sin,
|
||||||
|
struct rt_msghdr *rtm);
|
||||||
|
{
|
||||||
|
int mib[6];
|
||||||
|
size_t needed;
|
||||||
|
char *lim, *buf, *next;
|
||||||
|
struct rt_msghdr *rtm;
|
||||||
|
struct sockaddr_inarp *sin;
|
||||||
|
struct sockaddr_dl *sdl;
|
||||||
|
extern int h_errno;
|
||||||
|
|
||||||
|
//mib[0] = CTL_NET;
|
||||||
|
mib[0] = PF_ROUTE;
|
||||||
|
mib[1] = 0;
|
||||||
|
mib[2] = AF_INET;
|
||||||
|
mib[3] = NET_RT_FLAGS;
|
||||||
|
mib[4] = RTF_LLINFO;
|
||||||
|
if (sysctl(mib, 5, NULL, &needed, NULL, 0) < 0)
|
||||||
|
err(1, "route-sysctl-estimate");
|
||||||
|
if (needed == 0)
|
||||||
|
return;
|
||||||
|
if ((buf = malloc(needed)) == NULL)
|
||||||
|
err(1, "malloc");
|
||||||
|
if (sysctl(mib, 5, buf, &needed, NULL, 0) < 0)
|
||||||
|
err(1, "actual retrieval of routing table");
|
||||||
|
lim = buf + needed;
|
||||||
|
for (next = buf; next < lim; next += rtm->rtm_msglen) {
|
||||||
|
rtm = (struct rt_msghdr *)next;
|
||||||
|
sin = (struct sockaddr_inarp *)(rtm + 1);
|
||||||
|
sdl = (struct sockaddr_dl *)(sin + 1);
|
||||||
|
if (addr) {
|
||||||
|
if (addr != sin->sin_addr.s_addr)
|
||||||
|
continue;
|
||||||
|
found_entry = 1;
|
||||||
|
}
|
||||||
|
(*action)(sdl, sin, rtm);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Display an arp entry
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
print_entry(sdl, sin, rtm)
|
||||||
|
struct sockaddr_dl *sdl;
|
||||||
|
struct sockaddr_inarp *sin;
|
||||||
|
struct rt_msghdr *rtm;
|
||||||
|
{
|
||||||
|
char *host;
|
||||||
|
extern int h_errno;
|
||||||
|
struct hostent *hp;
|
||||||
|
|
||||||
|
if (nflag == 0)
|
||||||
|
hp = gethostbyaddr((caddr_t)&(sin->sin_addr),
|
||||||
|
sizeof(sin->sin_addr), AF_INET);
|
||||||
|
else
|
||||||
|
hp = 0;
|
||||||
|
if (hp)
|
||||||
|
host = hp->h_name;
|
||||||
|
else {
|
||||||
|
host = "?";
|
||||||
|
if (h_errno == TRY_AGAIN)
|
||||||
|
nflag = 1;
|
||||||
|
}
|
||||||
|
(void)printf("%s (%s) at ", host, inet_ntoa(sin->sin_addr));
|
||||||
|
if (sdl->sdl_alen)
|
||||||
|
ether_print(LLADDR(sdl));
|
||||||
|
else
|
||||||
|
(void)printf("(incomplete)");
|
||||||
|
if (rtm->rtm_flags & RTF_PERMANENT_ARP)
|
||||||
|
(void)printf(" permanent");
|
||||||
|
if (rtm->rtm_rmx.rmx_expire == 0)
|
||||||
|
(void)printf(" static");
|
||||||
|
if (sin->sin_other & SIN_PROXY)
|
||||||
|
(void)printf(" published (proxy only)");
|
||||||
|
if (rtm->rtm_addrs & RTA_NETMASK) {
|
||||||
|
sin = (struct sockaddr_inarp *)
|
||||||
|
(sdl->sdl_len + (char *)sdl);
|
||||||
|
if (sin->sin_addr.s_addr == 0xffffffff)
|
||||||
|
(void)printf(" published");
|
||||||
|
if (sin->sin_len != 8)
|
||||||
|
(void)printf("(weird)");
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Nuke an arp entry
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
nuke_entry(sdl, sin, rtm)
|
||||||
|
struct sockaddr_dl *sdl;
|
||||||
|
struct sockaddr_inarp *sin;
|
||||||
|
struct rt_msghdr *rtm;
|
||||||
|
{
|
||||||
|
char ip[20];
|
||||||
|
|
||||||
|
strncpy(ip, inet_ntoa(sin->sin_addr), sizeof(ip));
|
||||||
|
delete(ip, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
ether_print(cp)
|
||||||
|
const u_char *cp;
|
||||||
|
{
|
||||||
|
(void)printf("%02x:%02x:%02x:%02x:%02x:%02x",
|
||||||
|
cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
usage()
|
||||||
|
{
|
||||||
|
(void)fprintf(stderr, "usage: arp [-n] hostname\n");
|
||||||
|
(void)fprintf(stderr, "usage: arp [-n] -a\n");
|
||||||
|
(void)fprintf(stderr, "usage: arp -d hostname\n");
|
||||||
|
(void)fprintf(stderr, "usage: arp -d -a\n");
|
||||||
|
(void)fprintf(stderr,
|
||||||
|
"usage: arp -s hostname ether_addr [temp | permanent] [pub]\n");
|
||||||
|
(void)fprintf(stderr, "usage: arp -f filename\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
rtmsg(cmd)
|
||||||
|
int cmd;
|
||||||
|
{
|
||||||
|
static int seq;
|
||||||
|
int rlen;
|
||||||
|
register struct rt_msghdr *rtm;
|
||||||
|
register char *cp;
|
||||||
|
register int l;
|
||||||
|
|
||||||
|
rtm = &m_rtmsg.m_rtm;
|
||||||
|
cp = m_rtmsg.m_space;
|
||||||
|
errno = 0;
|
||||||
|
|
||||||
|
if (cmd == RTM_DELETE)
|
||||||
|
goto doit;
|
||||||
|
(void)memset(&m_rtmsg, 0, sizeof(m_rtmsg));
|
||||||
|
rtm->rtm_flags = flags;
|
||||||
|
rtm->rtm_version = RTM_VERSION;
|
||||||
|
|
||||||
|
switch (cmd) {
|
||||||
|
default:
|
||||||
|
printf("internal wrong cmd\n");
|
||||||
|
exit(-1);
|
||||||
|
/*NOTREACHED*/
|
||||||
|
case RTM_ADD:
|
||||||
|
rtm->rtm_addrs |= RTA_GATEWAY;
|
||||||
|
rtm->rtm_rmx.rmx_expire = expire_time;
|
||||||
|
rtm->rtm_inits = RTV_EXPIRE;
|
||||||
|
rtm->rtm_flags |= (RTF_HOST | RTF_STATIC);
|
||||||
|
sin_m.sin_other = 0;
|
||||||
|
if (doing_proxy) {
|
||||||
|
if (export_only)
|
||||||
|
sin_m.sin_other = SIN_PROXY;
|
||||||
|
else {
|
||||||
|
rtm->rtm_addrs |= RTA_NETMASK;
|
||||||
|
rtm->rtm_flags &= ~RTF_HOST;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* FALLTHROUGH */
|
||||||
|
case RTM_GET:
|
||||||
|
rtm->rtm_addrs |= RTA_DST;
|
||||||
|
}
|
||||||
|
#define NEXTADDR(w, s) \
|
||||||
|
if (rtm->rtm_addrs & (w)) { \
|
||||||
|
(void)memcpy(cp, &s, sizeof(s)); cp += sizeof(s);}
|
||||||
|
|
||||||
|
NEXTADDR(RTA_DST, sin_m);
|
||||||
|
NEXTADDR(RTA_GATEWAY, sdl_m);
|
||||||
|
NEXTADDR(RTA_NETMASK, so_mask);
|
||||||
|
|
||||||
|
rtm->rtm_msglen = cp - (char *)&m_rtmsg;
|
||||||
|
doit:
|
||||||
|
l = rtm->rtm_msglen;
|
||||||
|
rtm->rtm_seq = ++seq;
|
||||||
|
rtm->rtm_type = cmd;
|
||||||
|
if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
|
||||||
|
if (errno != ESRCH || cmd != RTM_DELETE) {
|
||||||
|
printf("writing to routing socket\n");
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
do {
|
||||||
|
l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
|
||||||
|
} while (l > 0 && (rtm->rtm_seq != seq)); /* || rtm->rtm_pid != pid)); */
|
||||||
|
if (l < 0)
|
||||||
|
printf("read from routing socket\n");
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
getinetaddr(host, inap)
|
||||||
|
const char *host;
|
||||||
|
struct in_addr *inap;
|
||||||
|
{
|
||||||
|
struct hostent *hp;
|
||||||
|
|
||||||
|
if (inet_aton(host, inap) == 1)
|
||||||
|
return (0);
|
||||||
|
if ((hp = gethostbyname(host)) == NULL) {
|
||||||
|
/* should be hstrerror and h_errno */
|
||||||
|
printf("%s: %s\n", host, strerror(errno));
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
(void)memcpy(inap, hp->h_addr, sizeof(*inap));
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
SubDir OBOS_TOP src apps bin ifconfig ;
|
||||||
|
|
||||||
@@ -0,0 +1,627 @@
|
|||||||
|
/* ifconfig.c */
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <kernel/OS.h>
|
||||||
|
#include <stdlib.h> /* for exit() */
|
||||||
|
#include <errno.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "netinet/in.h"
|
||||||
|
#include "sys/socket.h"
|
||||||
|
#include "sys/sockio.h"
|
||||||
|
#include "arpa/inet.h"
|
||||||
|
#include "net/if.h"
|
||||||
|
#include "net/if_dl.h"
|
||||||
|
|
||||||
|
#define version "0.2 pre-alpha"
|
||||||
|
#define NEXTARG 0xffffff
|
||||||
|
|
||||||
|
/* globals */
|
||||||
|
int sock; /* the socket we're using */
|
||||||
|
int flags = 0; /* interface flags */
|
||||||
|
int setaddr;
|
||||||
|
struct ifreq ifr; /* our interface information */
|
||||||
|
struct ifreq ridreq; /* ?? */
|
||||||
|
struct ifaliasreq addreq; /* aliases details */
|
||||||
|
struct sockaddr_in netmask; /* the netmask */
|
||||||
|
int mflag;
|
||||||
|
int newaddr = 0; /* do we have a new address? */
|
||||||
|
int doalias; /* do we process alias information */
|
||||||
|
int clearaddr; /* are we clearing the address? */
|
||||||
|
char name[IFNAMSIZ]; /* the name of the interface we're using */
|
||||||
|
|
||||||
|
/* we only deal with a single interface at once, so these globals
|
||||||
|
* refer to their respective information for that interface
|
||||||
|
*/
|
||||||
|
int af = AF_INET; /* address family we're dealing with */
|
||||||
|
int metric; /* the metric */
|
||||||
|
int mtu; /* mtu vlaue */
|
||||||
|
|
||||||
|
/* forward declarations... */
|
||||||
|
int getinfo(struct ifreq *ifr);
|
||||||
|
void setifaddr(char *addr, int param);
|
||||||
|
void setifflags(char *vname, int value);
|
||||||
|
void setifnetmask(char *addr);
|
||||||
|
void setifmetric(char *val);
|
||||||
|
void setifmtu(char *val, int d);
|
||||||
|
void in_status(int force);
|
||||||
|
void in_getaddr(char *s, int which);
|
||||||
|
void in_getprefix(char *plen, int which);
|
||||||
|
void status(int link);
|
||||||
|
void printif(struct ifreq *ifrm, int ifaliases);
|
||||||
|
|
||||||
|
/* The commands we recognise... */
|
||||||
|
struct _cmd_ {
|
||||||
|
char *c_name; /* what the command line will say */
|
||||||
|
int c_parameter; /* the parameter, NEXTARG = next argv */
|
||||||
|
void (*c_func)(); /* the function to call */
|
||||||
|
} cmds [] = {
|
||||||
|
{"up", IFF_UP, setifflags},
|
||||||
|
{"down", -IFF_UP, setifflags},
|
||||||
|
{"netmask", NEXTARG, setifnetmask},
|
||||||
|
{"metric", NEXTARG, setifmetric},
|
||||||
|
{"mtu", NEXTARG, setifmtu},
|
||||||
|
{NULL, /*src*/0, setifaddr },
|
||||||
|
// {NULL, /*dst*/0, setifdstaddr },
|
||||||
|
{NULL, 0, NULL}
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Known address families - thus far ONLY AF_INET */
|
||||||
|
/* XXX - it'd be very cool if we could have a way of doing this
|
||||||
|
* by modules/add-ons :)
|
||||||
|
*/
|
||||||
|
const struct afswtch {
|
||||||
|
char *af_name;
|
||||||
|
short af_af;
|
||||||
|
void (*af_status)();
|
||||||
|
void (*af_getaddr)();
|
||||||
|
void (*af_getprefix)();
|
||||||
|
u_long af_difaddr;
|
||||||
|
u_long af_aifaddr;
|
||||||
|
caddr_t af_ridreq;
|
||||||
|
caddr_t af_addreq;
|
||||||
|
} afs[] = {
|
||||||
|
#define C(x) ((caddr_t) &x)
|
||||||
|
{ "inet", AF_INET, in_status, in_getaddr, in_getprefix,
|
||||||
|
SIOCDIFADDR, SIOCAIFADDR, C(ridreq), C(addreq) },
|
||||||
|
{ 0, 0, 0, 0 }
|
||||||
|
};
|
||||||
|
|
||||||
|
const struct afswtch *afp; /*the address family being set or asked about*/
|
||||||
|
|
||||||
|
void usage()
|
||||||
|
{
|
||||||
|
fprintf(stderr, "OpenBeOS Network Team: ifconfig: version %s\n", version);
|
||||||
|
fprintf(stderr, "usage: ifconfig [ -m ] [ -a ] [ -A ] [ interface ]\n"
|
||||||
|
"\t[ [af] [ address [ dest_addr ] ] [ up ] [ down ] "
|
||||||
|
"[ netmask mask ] ]\n"
|
||||||
|
"\t[ metric n ]\n"
|
||||||
|
"\t[ mtu n ]\n"
|
||||||
|
" ifconfig [-a | -A | -am | -Am] [ af ]\n"
|
||||||
|
" ifconfig -m interface [af]\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void err(int exitval, char *where)
|
||||||
|
{
|
||||||
|
printf("error: %s: error %d [%s]\n", where, errno, strerror(errno));
|
||||||
|
exit(exitval);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void errx(int exitval, char *fmt_string, char *value)
|
||||||
|
{
|
||||||
|
printf("error: ");
|
||||||
|
printf(fmt_string, value);
|
||||||
|
exit(exitval);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void warn(char *what)
|
||||||
|
{
|
||||||
|
printf("warning: %s: error %d [%s]\n", what, errno, strerror(errno));
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int aflag = 0, ifaliases = 0;
|
||||||
|
const struct afswtch *rafp = NULL;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
mflag = 0;
|
||||||
|
|
||||||
|
if (argc < 2)
|
||||||
|
usage();
|
||||||
|
argc--, argv++; /* skip app name */
|
||||||
|
|
||||||
|
/* handle common optins and get the interface name */
|
||||||
|
if (!strcmp(*argv, "-a"))
|
||||||
|
aflag = 1;
|
||||||
|
else if (!strcmp(*argv, "-A")) {
|
||||||
|
aflag = 1;
|
||||||
|
ifaliases = 1;
|
||||||
|
} else if (!strcmp(*argv, "-ma") || !strcmp(*argv, "-am")) {
|
||||||
|
aflag = 1;
|
||||||
|
mflag = 1;
|
||||||
|
}
|
||||||
|
else if (!strcmp(*argv, "-mA") || !strcmp(*argv, "-Am")) {
|
||||||
|
aflag = 1;
|
||||||
|
ifaliases = 1;
|
||||||
|
mflag = 1;
|
||||||
|
}
|
||||||
|
else if (!strcmp(*argv, "-m")) {
|
||||||
|
mflag = 1;
|
||||||
|
argc--, argv++;
|
||||||
|
if (argc < 1)
|
||||||
|
usage();
|
||||||
|
(void) strcpy(name, *argv);
|
||||||
|
} else
|
||||||
|
(void) strcpy(name, *argv);
|
||||||
|
argc--, argv++;
|
||||||
|
|
||||||
|
/* Try to establish the address family we're talking about
|
||||||
|
* and set the appropriate structure of commands into the
|
||||||
|
* pointer afp;
|
||||||
|
*/
|
||||||
|
if (argc > 0) {
|
||||||
|
for (afp = rafp = afs; rafp->af_name; rafp++)
|
||||||
|
if (strcmp(rafp->af_name, *argv) == 0) {
|
||||||
|
afp = rafp; argc--; argv++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
rafp = afp;
|
||||||
|
af = ifr.ifr_addr.sa_family = rafp->af_af;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (argc > 0) {
|
||||||
|
for (afp = rafp = afs; rafp->af_name; rafp++)
|
||||||
|
if (strcmp(rafp->af_name, *argv) == 0) {
|
||||||
|
printf("afp: %s\n", *argv);
|
||||||
|
afp = rafp; argc--; argv++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
rafp = afp;
|
||||||
|
af = ifr.ifr_addr.sa_family = rafp->af_af;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (aflag) {
|
||||||
|
if (argc > 0)
|
||||||
|
usage();
|
||||||
|
printif(NULL, ifaliases);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* put the name in the ifr structure */
|
||||||
|
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
|
||||||
|
/* if there's no more arguments then print information
|
||||||
|
* on the named interface */
|
||||||
|
if (argc == 0) {
|
||||||
|
printif(&ifr, 1);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* we've probably got something to do, so get the information */
|
||||||
|
if (getinfo(&ifr) < 0)
|
||||||
|
exit(1);
|
||||||
|
|
||||||
|
while (argc > 0) {
|
||||||
|
const struct _cmd_ *p;
|
||||||
|
|
||||||
|
for (p = cmds; p->c_name; p++)
|
||||||
|
if (strcmp(*argv, p->c_name) == 0)
|
||||||
|
break;
|
||||||
|
/* we've found a command! */
|
||||||
|
if (p->c_name == NULL && setaddr) {
|
||||||
|
printf("pc->name = NULL!\n");
|
||||||
|
for (i = setaddr; i > 0; i--) {
|
||||||
|
p++;
|
||||||
|
if (p->c_func == NULL)
|
||||||
|
err(1, "extra address not accepted");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (p->c_func) {
|
||||||
|
if (p->c_parameter == NEXTARG) {
|
||||||
|
if (argv[1] == NULL)
|
||||||
|
errx(1, "'%s' requires argument",
|
||||||
|
p->c_name);
|
||||||
|
(*p->c_func)(argv[1]);
|
||||||
|
argc--, argv++;
|
||||||
|
} else
|
||||||
|
(*p->c_func)(*argv, p->c_parameter);
|
||||||
|
}
|
||||||
|
|
||||||
|
argc--, argv++;
|
||||||
|
}
|
||||||
|
if (clearaddr) {
|
||||||
|
int ret;
|
||||||
|
strncpy(rafp->af_ridreq, name, sizeof(ifr.ifr_name));
|
||||||
|
if ((ret = ioctl(sock, rafp->af_difaddr, rafp->af_ridreq)) < 0) {
|
||||||
|
if (errno == EADDRNOTAVAIL && (doalias >= 0)) {
|
||||||
|
/* means no previous address for interface */
|
||||||
|
} else
|
||||||
|
warn("SIOCDIFADDR");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (newaddr) {
|
||||||
|
strncpy(rafp->af_addreq, name, sizeof(ifr.ifr_name));
|
||||||
|
if (ioctl(sock, rafp->af_aifaddr, rafp->af_addreq) < 0)
|
||||||
|
warn("SIOCAIFADDR");
|
||||||
|
}
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void getsock(int naf)
|
||||||
|
{
|
||||||
|
static int oaf = -1;
|
||||||
|
|
||||||
|
if (oaf == naf)
|
||||||
|
return;
|
||||||
|
if (oaf != -1)
|
||||||
|
close(sock);
|
||||||
|
sock = socket(naf, SOCK_DGRAM, 0);
|
||||||
|
if (sock < 0)
|
||||||
|
oaf = -1;
|
||||||
|
else
|
||||||
|
oaf = naf;
|
||||||
|
}
|
||||||
|
|
||||||
|
int getinfo(struct ifreq *ifr)
|
||||||
|
{
|
||||||
|
getsock(af);
|
||||||
|
if (sock < 0)
|
||||||
|
err(1, "socket");
|
||||||
|
if (ioctl(sock, SIOCGIFFLAGS, (caddr_t)ifr) < 0) {
|
||||||
|
warn("SIOCGIFFLAGS");
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
flags = ifr->ifr_flags;
|
||||||
|
if (ioctl(sock, SIOCGIFMETRIC, (caddr_t)ifr) < 0) {
|
||||||
|
warn("SIOCGIFMETRIC");
|
||||||
|
metric = 0;
|
||||||
|
} else
|
||||||
|
metric = ifr->ifr_metric;
|
||||||
|
if (ioctl(sock, SIOCGIFMTU, (caddr_t)ifr) < 0)
|
||||||
|
mtu = 0;
|
||||||
|
else
|
||||||
|
mtu = ifr->ifr_mtu;
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void printif(struct ifreq *ifrm, int ifaliases)
|
||||||
|
{
|
||||||
|
char *inbuf = NULL;
|
||||||
|
struct ifconf ifc;
|
||||||
|
struct ifreq ifreq, *ifrp;
|
||||||
|
int i, siz, len = 8192;
|
||||||
|
int count = 0, noinet = 1;
|
||||||
|
char ifrbuf[8192];
|
||||||
|
|
||||||
|
getsock(af);
|
||||||
|
if (sock < 0)
|
||||||
|
err(1, "socket");
|
||||||
|
while (1) {
|
||||||
|
ifc.ifc_len = len;
|
||||||
|
ifc.ifc_buf = inbuf = realloc(inbuf, len);
|
||||||
|
if (inbuf == NULL)
|
||||||
|
err(1, "malloc");
|
||||||
|
if (ioctl(sock, SIOCGIFCONF, &ifc) < 0)
|
||||||
|
err(1, "SIOCGIFCONF");
|
||||||
|
if (ifc.ifc_len + sizeof(ifreq) < len)
|
||||||
|
break;
|
||||||
|
len *= 2;
|
||||||
|
}
|
||||||
|
ifrp = ifc.ifc_req;
|
||||||
|
ifreq.ifr_name[0] = '\0';
|
||||||
|
for (i = 0; i < ifc.ifc_len; ) {
|
||||||
|
ifrp = (struct ifreq *)((caddr_t)ifc.ifc_req + i);
|
||||||
|
memcpy(ifrbuf, ifrp, sizeof(*ifrp));
|
||||||
|
siz = ((struct ifreq *)ifrbuf)->ifr_addr.sa_len;
|
||||||
|
if (siz < sizeof(ifrp->ifr_addr))
|
||||||
|
siz = sizeof(ifrp->ifr_addr);
|
||||||
|
siz += sizeof(ifrp->ifr_name);
|
||||||
|
i += siz;
|
||||||
|
/* avoid alignment issue */
|
||||||
|
if (sizeof(ifrbuf) < siz)
|
||||||
|
err(1, "ifr too big");
|
||||||
|
memcpy(ifrbuf, ifrp, siz);
|
||||||
|
ifrp = (struct ifreq *)ifrbuf;
|
||||||
|
|
||||||
|
if (ifrm && strncmp(ifrm->ifr_name, ifrp->ifr_name,
|
||||||
|
sizeof(ifrp->ifr_name)))
|
||||||
|
continue;
|
||||||
|
strncpy(name, ifrp->ifr_name, sizeof(ifrp->ifr_name));
|
||||||
|
if (ifrp->ifr_addr.sa_family == AF_LINK) {
|
||||||
|
ifreq = ifr = *ifrp;
|
||||||
|
if (getinfo(&ifreq) < 0)
|
||||||
|
continue;
|
||||||
|
status(1);
|
||||||
|
count++;
|
||||||
|
noinet = 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!strncmp(ifreq.ifr_name, ifrp->ifr_name,
|
||||||
|
sizeof(ifrp->ifr_name))) {
|
||||||
|
const struct afswtch *p;
|
||||||
|
|
||||||
|
if (ifrp->ifr_addr.sa_family == AF_INET &&
|
||||||
|
ifaliases == 0 && noinet == 0)
|
||||||
|
continue;
|
||||||
|
ifr = *ifrp;
|
||||||
|
if ((p = afp) != NULL) {
|
||||||
|
if (ifr.ifr_addr.sa_family == p->af_af)
|
||||||
|
(*p->af_status)(1);
|
||||||
|
} else {
|
||||||
|
for (p = afs; p->af_name; p++) {
|
||||||
|
if (ifr.ifr_addr.sa_family == p->af_af)
|
||||||
|
(*p->af_status)(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
count++;
|
||||||
|
if (ifrp->ifr_addr.sa_family == AF_INET)
|
||||||
|
noinet = 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
free(inbuf);
|
||||||
|
if (count == 0) {
|
||||||
|
fprintf(stderr, "%s: no such interface\n", name);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#define RIDADDR 0
|
||||||
|
#define ADDR 1
|
||||||
|
#define MASK 2
|
||||||
|
#define DSTADDR 3
|
||||||
|
|
||||||
|
/*ARGSUSED*/
|
||||||
|
void setifaddr(char *addr, int param)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* Delay the ioctl to set the interface addr until flags are all set.
|
||||||
|
* The address interpretation may depend on the flags,
|
||||||
|
* and the flags may change when the address is set.
|
||||||
|
*/
|
||||||
|
setaddr++;
|
||||||
|
newaddr = 1;
|
||||||
|
if (doalias == 0)
|
||||||
|
clearaddr = 1;
|
||||||
|
(*afp->af_getaddr)(addr, (doalias >= 0 ? ADDR : RIDADDR));
|
||||||
|
}
|
||||||
|
|
||||||
|
void setifnetmask(char *addr)
|
||||||
|
{
|
||||||
|
(*afp->af_getaddr)(addr, MASK);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Note: doing an SIOCIGIFFLAGS scribbles on the union portion
|
||||||
|
* of the ifreq structure, which may confuse other parts of ifconfig.
|
||||||
|
* Make a private copy so we can avoid that.
|
||||||
|
*/
|
||||||
|
void setifflags(char *vname, int value)
|
||||||
|
{
|
||||||
|
struct ifreq my_ifr;
|
||||||
|
|
||||||
|
memcpy((char *)&my_ifr, (char *)&ifr, sizeof(struct ifreq));
|
||||||
|
|
||||||
|
if (ioctl(sock, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0)
|
||||||
|
err(1, "SIOCGIFFLAGS");
|
||||||
|
strncpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name));
|
||||||
|
flags = my_ifr.ifr_flags;
|
||||||
|
|
||||||
|
if (value < 0) {
|
||||||
|
value = -value;
|
||||||
|
flags &= ~value;
|
||||||
|
} else
|
||||||
|
flags |= value;
|
||||||
|
my_ifr.ifr_flags = flags;
|
||||||
|
if (ioctl(sock, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0)
|
||||||
|
err(1, "SIOCSIFFLAGS");
|
||||||
|
}
|
||||||
|
|
||||||
|
void setifmetric(char *val)
|
||||||
|
{
|
||||||
|
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
|
||||||
|
ifr.ifr_metric = atoi(val);
|
||||||
|
if (ioctl(sock, SIOCSIFMETRIC, (caddr_t)&ifr) < 0)
|
||||||
|
warn("SIOCSIFMETRIC");
|
||||||
|
}
|
||||||
|
|
||||||
|
void setifmtu(char *val, int d)
|
||||||
|
{
|
||||||
|
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
|
||||||
|
ifr.ifr_mtu = atoi(val);
|
||||||
|
if (ioctl(sock, SIOCSIFMTU, (caddr_t)&ifr) < 0)
|
||||||
|
warn("SIOCSIFMTU");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#define SIN(x) ((struct sockaddr_in *) &(x))
|
||||||
|
struct sockaddr_in *sintab[] = {
|
||||||
|
SIN(ridreq.ifr_addr), SIN(addreq.ifra_addr),
|
||||||
|
SIN(addreq.ifra_mask), SIN(addreq.ifra_broadaddr)};
|
||||||
|
|
||||||
|
void in_getaddr(char *s, int which)
|
||||||
|
{
|
||||||
|
struct sockaddr_in *sin = sintab[which];
|
||||||
|
/*
|
||||||
|
struct hostent *hp;
|
||||||
|
struct netent *np;
|
||||||
|
*/
|
||||||
|
sin->sin_len = sizeof(*sin);
|
||||||
|
if (which != MASK)
|
||||||
|
sin->sin_family = AF_INET;
|
||||||
|
|
||||||
|
if (inet_aton(s, &sin->sin_addr) == 0) {
|
||||||
|
/*
|
||||||
|
XXX - we don't have these yet!
|
||||||
|
|
||||||
|
if ((hp = gethostbyname(s)))
|
||||||
|
memcpy(&sin->sin_addr, hp->h_addr, hp->h_length);
|
||||||
|
else if ((np = getnetbyname(s)))
|
||||||
|
sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
|
||||||
|
else
|
||||||
|
*/
|
||||||
|
errx(1, "%s: bad value", s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void in_status(int force)
|
||||||
|
{
|
||||||
|
struct sockaddr_in *sin, sin2;
|
||||||
|
|
||||||
|
getsock(AF_INET);
|
||||||
|
if (sock < 0) {
|
||||||
|
if (errno == EPROTONOSUPPORT)
|
||||||
|
return;
|
||||||
|
err(1, "socket");
|
||||||
|
}
|
||||||
|
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
|
||||||
|
sin = (struct sockaddr_in *)&ifr.ifr_addr;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* We keep the interface address and reset it before each
|
||||||
|
* ioctl() so we can get ifaliases information (as opposed
|
||||||
|
* to the primary interface netmask/dstaddr/broadaddr, if
|
||||||
|
* the ifr_addr field is zero).
|
||||||
|
*/
|
||||||
|
memcpy(&sin2, &ifr.ifr_addr, sizeof(sin2));
|
||||||
|
|
||||||
|
printf("\tinet %s ", inet_ntoa(sin->sin_addr));
|
||||||
|
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
|
||||||
|
if (ioctl(sock, SIOCGIFNETMASK, (caddr_t)&ifr) < 0) {
|
||||||
|
if (errno != EADDRNOTAVAIL)
|
||||||
|
warn("SIOCGIFNETMASK");
|
||||||
|
memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
|
||||||
|
} else
|
||||||
|
netmask.sin_addr =
|
||||||
|
((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr;
|
||||||
|
if (flags & IFF_POINTOPOINT) {
|
||||||
|
memcpy(&ifr.ifr_addr, &sin2, sizeof(sin2));
|
||||||
|
if (ioctl(sock, SIOCGIFDSTADDR, (caddr_t)&ifr) < 0) {
|
||||||
|
if (errno == EADDRNOTAVAIL)
|
||||||
|
memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
|
||||||
|
else
|
||||||
|
warn("SIOCGIFDSTADDR");
|
||||||
|
}
|
||||||
|
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
|
||||||
|
sin = (struct sockaddr_in *)&ifr.ifr_dstaddr;
|
||||||
|
printf("--> %s ", inet_ntoa(sin->sin_addr));
|
||||||
|
}
|
||||||
|
printf("netmask 0x%08lx ", ntohl(netmask.sin_addr.s_addr));
|
||||||
|
if (flags & IFF_BROADCAST) {
|
||||||
|
memcpy(&ifr.ifr_addr, &sin2, sizeof(sin2));
|
||||||
|
if (ioctl(sock, SIOCGIFBRDADDR, (caddr_t)&ifr) < 0) {
|
||||||
|
if (errno == EADDRNOTAVAIL)
|
||||||
|
memset(&ifr.ifr_addr, 0, sizeof(ifr.ifr_addr));
|
||||||
|
else
|
||||||
|
warn("SIOCGIFBRDADDR");
|
||||||
|
}
|
||||||
|
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
|
||||||
|
sin = (struct sockaddr_in *)&ifr.ifr_addr;
|
||||||
|
if (sin->sin_addr.s_addr != 0)
|
||||||
|
printf("broadcast %s", inet_ntoa(sin->sin_addr));
|
||||||
|
}
|
||||||
|
putchar('\n');
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
1 IFF_UP
|
||||||
|
2 IFF_BROADCAST
|
||||||
|
3 IFF_PROMISC
|
||||||
|
4 IFF_RUNNING = 0x0008,
|
||||||
|
5 IFF_MULTICAST = 0x0010,
|
||||||
|
6 IFF_BROADCAST = 0x0020,
|
||||||
|
7 IFF_POINTOPOINT = 0x0040,
|
||||||
|
8 IFF_NOARP = 0x0080,
|
||||||
|
9 IFF_LOOPBACK = 0x0100,
|
||||||
|
10 IFF_DEBUG = 0x0200,
|
||||||
|
11 IFF_LINK0 = 0x0400,
|
||||||
|
12 IFF_LINK1 = 0x0800,
|
||||||
|
13 IFF_LINK2 = 0x1000
|
||||||
|
|
||||||
|
*/
|
||||||
|
#define IFFBITS \
|
||||||
|
"\017\1UP\2BROADCAST\3PROMISC\4RUNNING\5MULTICAST\6BROADCAST\7POINTOPOINT\10NOARP\
|
||||||
|
\11LOOPBACK\12DEBUG\13SIMPLEX\15LINK0\16LINK1\17LINK2"
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Print a value a la the %b format of the kernel's printf
|
||||||
|
*/
|
||||||
|
static void printb(char *s, uint16 v, char *bits)
|
||||||
|
{
|
||||||
|
int i, any = 0;
|
||||||
|
char c;
|
||||||
|
|
||||||
|
if (bits && *bits == 8)
|
||||||
|
printf("%s=%o", s, v);
|
||||||
|
else
|
||||||
|
printf("%s=%x", s, v);
|
||||||
|
bits++;
|
||||||
|
if (bits) {
|
||||||
|
putchar('<');
|
||||||
|
while ((i = *bits++)) {
|
||||||
|
if (v & (1 << (i-1))) {
|
||||||
|
if (any)
|
||||||
|
putchar(',');
|
||||||
|
any = 1;
|
||||||
|
for (; (c = *bits) > 32; bits++)
|
||||||
|
putchar(c);
|
||||||
|
} else
|
||||||
|
for (; *bits > 32; bits++)
|
||||||
|
;
|
||||||
|
}
|
||||||
|
putchar('>');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void in_getprefix(char *plen, int which)
|
||||||
|
{
|
||||||
|
struct sockaddr_in *sin = sintab[which];
|
||||||
|
u_char *cp;
|
||||||
|
int len = strtol(plen, (char **)NULL, 10);
|
||||||
|
|
||||||
|
if ((len < 0) || (len > 32))
|
||||||
|
errx(1, "%s: bad value", plen);
|
||||||
|
sin->sin_len = sizeof(*sin);
|
||||||
|
if (which != MASK)
|
||||||
|
sin->sin_family = AF_INET;
|
||||||
|
if ((len == 0) || (len == 32)) {
|
||||||
|
memset(&sin->sin_addr, 0xff, sizeof(struct in_addr));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
memset((void *)&sin->sin_addr, 0x00, sizeof(sin->sin_addr));
|
||||||
|
for (cp = (u_char *)&sin->sin_addr; len > 7; len -= 8)
|
||||||
|
*cp++ = 0xff;
|
||||||
|
if (len)
|
||||||
|
*cp = 0xff << (8 - len);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Print the status of the interface. If an address family was
|
||||||
|
* specified, show it and it only; otherwise, show them all.
|
||||||
|
*/
|
||||||
|
void status(int link)
|
||||||
|
{
|
||||||
|
const struct afswtch *p = afp;
|
||||||
|
|
||||||
|
printf("%s: ", name);
|
||||||
|
printb("flags", flags, IFFBITS);
|
||||||
|
if (metric)
|
||||||
|
printf(" metric %d", metric);
|
||||||
|
if (mtu)
|
||||||
|
printf(" mtu %d", mtu);
|
||||||
|
putchar('\n');
|
||||||
|
|
||||||
|
if (link == 0) {
|
||||||
|
if ((p = afp) != NULL) {
|
||||||
|
(*p->af_status)(1);
|
||||||
|
} else for (p = afs; p->af_name; p++) {
|
||||||
|
ifr.ifr_addr.sa_family = p->af_af;
|
||||||
|
(*p->af_status)(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// phys_status(0);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
SubDir OBOS_TOP src apps bin ping ;
|
||||||
|
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,2 @@
|
|||||||
|
SubDir OBOS_TOP src apps bin route ;
|
||||||
|
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
/* $OpenBSD: keywords.c,v 1.6 2000/07/27 20:12:24 angelos Exp $ */
|
||||||
|
|
||||||
|
/* WARNING! This file was generated by keywords.sh */
|
||||||
|
|
||||||
|
#include "keywords.h"
|
||||||
|
|
||||||
|
struct keytab keywords[] = {
|
||||||
|
|
||||||
|
{"add", K_ADD},
|
||||||
|
{"blackhole", K_BLACKHOLE},
|
||||||
|
{"change", K_CHANGE},
|
||||||
|
{"cloning", K_CLONING},
|
||||||
|
{"delete", K_DELETE},
|
||||||
|
{"dst", K_DST},
|
||||||
|
{"expire", K_EXPIRE},
|
||||||
|
{"flush", K_FLUSH},
|
||||||
|
{"gateway", K_GATEWAY},
|
||||||
|
{"genmask", K_GENMASK},
|
||||||
|
{"get", K_GET},
|
||||||
|
{"host", K_HOST},
|
||||||
|
{"hopcount", K_HOPCOUNT},
|
||||||
|
{"iface", K_IFACE},
|
||||||
|
{"interface", K_INTERFACE},
|
||||||
|
{"ifa", K_IFA},
|
||||||
|
{"ifp", K_IFP},
|
||||||
|
{"inet", K_INET},
|
||||||
|
{"inet6", K_INET6},
|
||||||
|
{"ipx", K_IPX},
|
||||||
|
{"iso", K_ISO},
|
||||||
|
{"link", K_LINK},
|
||||||
|
{"llinfo", K_LLINFO},
|
||||||
|
{"lock", K_LOCK},
|
||||||
|
{"lockrest", K_LOCKREST},
|
||||||
|
{"mask", K_MASK},
|
||||||
|
{"monitor", K_MONITOR},
|
||||||
|
{"mtu", K_MTU},
|
||||||
|
{"net", K_NET},
|
||||||
|
{"netmask", K_NETMASK},
|
||||||
|
{"nostatic", K_NOSTATIC},
|
||||||
|
{"osi", K_OSI},
|
||||||
|
{"prefixlen", K_PREFIXLEN},
|
||||||
|
{"proto1", K_PROTO1},
|
||||||
|
{"proto2", K_PROTO2},
|
||||||
|
{"recvpipe", K_RECVPIPE},
|
||||||
|
{"reject", K_REJECT},
|
||||||
|
{"rtt", K_RTT},
|
||||||
|
{"rttvar", K_RTTVAR},
|
||||||
|
{"sa", K_SA},
|
||||||
|
{"sendpipe", K_SENDPIPE},
|
||||||
|
{"show", K_SHOW},
|
||||||
|
{"ssthresh", K_SSTHRESH},
|
||||||
|
{"static", K_STATIC},
|
||||||
|
{"x25", K_X25},
|
||||||
|
{"xns", K_XNS},
|
||||||
|
{"xresolve", K_XRESOLVE},
|
||||||
|
{0, 0}
|
||||||
|
};
|
||||||
|
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
/* $OpenBSD: keywords.h,v 1.6 2000/07/27 20:12:25 angelos Exp $ */
|
||||||
|
|
||||||
|
/* WARNING! This file was generated by keywords.sh */
|
||||||
|
|
||||||
|
extern struct keytab {
|
||||||
|
char *kt_cp;
|
||||||
|
int kt_i;
|
||||||
|
} keywords[];
|
||||||
|
|
||||||
|
|
||||||
|
#define K_ADD 1
|
||||||
|
#define K_BLACKHOLE 2
|
||||||
|
#define K_CHANGE 3
|
||||||
|
#define K_CLONING 4
|
||||||
|
#define K_DELETE 5
|
||||||
|
#define K_DST 6
|
||||||
|
#define K_EXPIRE 7
|
||||||
|
#define K_FLUSH 8
|
||||||
|
#define K_GATEWAY 9
|
||||||
|
#define K_GENMASK 10
|
||||||
|
#define K_GET 11
|
||||||
|
#define K_HOST 12
|
||||||
|
#define K_HOPCOUNT 13
|
||||||
|
#define K_IFACE 14
|
||||||
|
#define K_INTERFACE 15
|
||||||
|
#define K_IFA 16
|
||||||
|
#define K_IFP 17
|
||||||
|
#define K_INET 18
|
||||||
|
#define K_INET6 19
|
||||||
|
#define K_IPX 20
|
||||||
|
#define K_ISO 21
|
||||||
|
#define K_LINK 22
|
||||||
|
#define K_LLINFO 23
|
||||||
|
#define K_LOCK 24
|
||||||
|
#define K_LOCKREST 25
|
||||||
|
#define K_MASK 26
|
||||||
|
#define K_MONITOR 27
|
||||||
|
#define K_MTU 28
|
||||||
|
#define K_NET 29
|
||||||
|
#define K_NETMASK 30
|
||||||
|
#define K_NOSTATIC 31
|
||||||
|
#define K_OSI 32
|
||||||
|
#define K_PREFIXLEN 33
|
||||||
|
#define K_PROTO1 34
|
||||||
|
#define K_PROTO2 35
|
||||||
|
#define K_RECVPIPE 36
|
||||||
|
#define K_REJECT 37
|
||||||
|
#define K_RTT 38
|
||||||
|
#define K_RTTVAR 39
|
||||||
|
#define K_SA 40
|
||||||
|
#define K_SENDPIPE 41
|
||||||
|
#define K_SHOW 42
|
||||||
|
#define K_SSTHRESH 43
|
||||||
|
#define K_STATIC 44
|
||||||
|
#define K_X25 45
|
||||||
|
#define K_XNS 46
|
||||||
|
#define K_XRESOLVE 47
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,377 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 1983, 1988, 1993
|
||||||
|
* The Regents of the University of California. All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. All advertising materials mentioning features or use of this software
|
||||||
|
* must display the following acknowledgement:
|
||||||
|
* This product includes software developed by the University of
|
||||||
|
* California, Berkeley and its contributors.
|
||||||
|
* 4. Neither the name of the University nor the names of its contributors
|
||||||
|
* may be used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||||
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||||
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||||
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||||
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||||
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sys/param.h>
|
||||||
|
#include <kernel/OS.h>
|
||||||
|
#include "sys/mbuf.h"
|
||||||
|
#include "sys/socket.h"
|
||||||
|
#include "sys/socketvar.h"
|
||||||
|
#include "sys/protosw.h"
|
||||||
|
|
||||||
|
#include "net/if.h"
|
||||||
|
#include "net/if_dl.h"
|
||||||
|
#include "net/if_types.h"
|
||||||
|
#include "net/route.h"
|
||||||
|
#include "netinet/in.h"
|
||||||
|
#include "arpa/inet.h"
|
||||||
|
|
||||||
|
#include "keywords.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
/* XXX: things from route.c */
|
||||||
|
extern char *routename __P((struct sockaddr *));
|
||||||
|
extern char *netname __P((struct sockaddr *));
|
||||||
|
extern int nflag;
|
||||||
|
|
||||||
|
#define ROUNDUP(a) \
|
||||||
|
((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
|
||||||
|
#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Definitions for showing gateway flags.
|
||||||
|
*/
|
||||||
|
struct bits {
|
||||||
|
short b_mask;
|
||||||
|
char b_val;
|
||||||
|
};
|
||||||
|
static const struct bits bits[] = {
|
||||||
|
{ RTF_UP, 'U' },
|
||||||
|
{ RTF_GATEWAY, 'G' },
|
||||||
|
{ RTF_HOST, 'H' },
|
||||||
|
{ RTF_REJECT, 'R' },
|
||||||
|
{ RTF_BLACKHOLE, 'B' },
|
||||||
|
{ RTF_DYNAMIC, 'D' },
|
||||||
|
{ RTF_MODIFIED, 'M' },
|
||||||
|
{ RTF_DONE, 'd' }, /* Completed -- for routing messages only */
|
||||||
|
{ RTF_MASK, 'm' }, /* Mask Present -- for routing messages only */
|
||||||
|
{ RTF_CLONING, 'C' },
|
||||||
|
{ RTF_XRESOLVE, 'X' },
|
||||||
|
{ RTF_LLINFO, 'L' },
|
||||||
|
{ RTF_STATIC, 'S' },
|
||||||
|
{ RTF_PROTO1, '1' },
|
||||||
|
{ RTF_PROTO2, '2' },
|
||||||
|
{ 0 }
|
||||||
|
};
|
||||||
|
|
||||||
|
static void p_rtentry __P((struct rt_msghdr *));
|
||||||
|
static void p_sockaddr __P((struct sockaddr *, int, int));
|
||||||
|
static void p_flags __P((int, char *));
|
||||||
|
static void pr_rthdr __P((void));
|
||||||
|
static void pr_family __P((int));
|
||||||
|
|
||||||
|
int keyword(char *);
|
||||||
|
void usage(char *);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Print routing tables.
|
||||||
|
*/
|
||||||
|
void show(int argc, char **argv)
|
||||||
|
{
|
||||||
|
struct rt_msghdr *rtm;
|
||||||
|
char *buf = NULL, *next, *lim = NULL;
|
||||||
|
size_t needed;
|
||||||
|
int mib[6], af = 0;
|
||||||
|
struct sockaddr *sa;
|
||||||
|
|
||||||
|
if (argc > 1) {
|
||||||
|
argv++;
|
||||||
|
if (argc == 2 && **argv == '-')
|
||||||
|
switch (keyword(*argv + 1)) {
|
||||||
|
case K_INET:
|
||||||
|
af = AF_INET;
|
||||||
|
break;
|
||||||
|
#ifdef INET6
|
||||||
|
case K_INET6:
|
||||||
|
af = AF_INET6;
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
case K_IPX:
|
||||||
|
af = AF_IPX;
|
||||||
|
break;
|
||||||
|
case K_LINK:
|
||||||
|
af = AF_LINK;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
goto bad;
|
||||||
|
} else
|
||||||
|
bad: usage(*argv);
|
||||||
|
}
|
||||||
|
// mib[0] = CTL_NET;
|
||||||
|
mib[0] = PF_ROUTE;
|
||||||
|
mib[1] = 0;
|
||||||
|
mib[2] = 0;
|
||||||
|
mib[3] = NET_RT_DUMP;
|
||||||
|
mib[4] = 0;
|
||||||
|
if (sysctl(mib, 5, NULL, &needed, NULL, 0) < 0) {
|
||||||
|
perror("route-sysctl-estimate");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
if (needed > 0) {
|
||||||
|
if ((buf = malloc(needed)) == 0) {
|
||||||
|
printf("out of space\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
if (sysctl(mib, 5, buf, &needed, NULL, 0) < 0) {
|
||||||
|
perror("sysctl of routing table");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
lim = buf + needed;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Routing tables\n");
|
||||||
|
|
||||||
|
if (buf) {
|
||||||
|
for (next = buf; next < lim; next += rtm->rtm_msglen) {
|
||||||
|
rtm = (struct rt_msghdr *)next;
|
||||||
|
sa = (struct sockaddr *)(rtm + 1);
|
||||||
|
if (af && sa->sa_family != af)
|
||||||
|
continue;
|
||||||
|
p_rtentry(rtm);
|
||||||
|
}
|
||||||
|
free(buf);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* column widths; each followed by one space */
|
||||||
|
#define WID_DST 16 /* width of destination column */
|
||||||
|
#define WID_GW 18 /* width of gateway column */
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Print header for routing table columns.
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
pr_rthdr()
|
||||||
|
{
|
||||||
|
printf("%-*.*s %-*.*s %-6.6s\n",
|
||||||
|
WID_DST, WID_DST, "Destination",
|
||||||
|
WID_GW, WID_GW, "Gateway",
|
||||||
|
"Flags");
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Print a routing table entry.
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
p_rtentry(rtm)
|
||||||
|
struct rt_msghdr *rtm;
|
||||||
|
{
|
||||||
|
struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
|
||||||
|
#ifdef notdef
|
||||||
|
static int masks_done, banner_printed;
|
||||||
|
#endif
|
||||||
|
static int old_af;
|
||||||
|
int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST | RTF_MASK;
|
||||||
|
|
||||||
|
#ifdef notdef
|
||||||
|
/* for the moment, netmasks are skipped over */
|
||||||
|
if (!banner_printed) {
|
||||||
|
printf("Netmasks:\n");
|
||||||
|
banner_printed = 1;
|
||||||
|
}
|
||||||
|
if (masks_done == 0) {
|
||||||
|
if (rtm->rtm_addrs != RTA_DST ) {
|
||||||
|
masks_done = 1;
|
||||||
|
af = sa->sa_family;
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
#endif
|
||||||
|
af = sa->sa_family;
|
||||||
|
if (old_af != af) {
|
||||||
|
old_af = af;
|
||||||
|
pr_family(af);
|
||||||
|
pr_rthdr();
|
||||||
|
}
|
||||||
|
if (rtm->rtm_addrs == RTA_DST) {
|
||||||
|
p_sockaddr(sa, 0, 36);
|
||||||
|
} else {
|
||||||
|
p_sockaddr(sa, rtm->rtm_flags, 16);
|
||||||
|
sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa);
|
||||||
|
p_sockaddr(sa, 0, 18);
|
||||||
|
}
|
||||||
|
p_flags(rtm->rtm_flags & interesting, "%-6.6s ");
|
||||||
|
putchar('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Print address family header before a section of the routing table.
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
pr_family(af)
|
||||||
|
int af;
|
||||||
|
{
|
||||||
|
char *afname;
|
||||||
|
|
||||||
|
switch (af) {
|
||||||
|
case AF_INET:
|
||||||
|
afname = "Internet";
|
||||||
|
break;
|
||||||
|
#ifdef INET6
|
||||||
|
case AF_INET6:
|
||||||
|
afname = "Internet6";
|
||||||
|
break;
|
||||||
|
#endif /* INET6 */
|
||||||
|
/*
|
||||||
|
case AF_NS:
|
||||||
|
afname = "XNS";
|
||||||
|
break;
|
||||||
|
*/
|
||||||
|
case AF_IPX:
|
||||||
|
afname = "IPX";
|
||||||
|
break;
|
||||||
|
/*
|
||||||
|
case AF_APPLETALK:
|
||||||
|
afname = "AppleTalk";
|
||||||
|
break;
|
||||||
|
*/
|
||||||
|
default:
|
||||||
|
afname = NULL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (afname)
|
||||||
|
printf("\n%s:\n", afname);
|
||||||
|
else
|
||||||
|
printf("\nProtocol Family %d:\n", af);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
|
p_sockaddr(sa, flags, width)
|
||||||
|
struct sockaddr *sa;
|
||||||
|
int flags, width;
|
||||||
|
{
|
||||||
|
char workbuf[128], *cplim;
|
||||||
|
char *cp = workbuf;
|
||||||
|
|
||||||
|
switch(sa->sa_family) {
|
||||||
|
|
||||||
|
case AF_LINK:
|
||||||
|
{
|
||||||
|
struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
|
||||||
|
if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
|
||||||
|
sdl->sdl_slen == 0)
|
||||||
|
(void) sprintf(workbuf, "link#%d", sdl->sdl_index);
|
||||||
|
else switch (sdl->sdl_type) {
|
||||||
|
case IFT_ETHER:
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
u_char *lla = (u_char *)sdl->sdl_data +
|
||||||
|
sdl->sdl_nlen;
|
||||||
|
|
||||||
|
cplim = "";
|
||||||
|
for (i = 0; i < sdl->sdl_alen; i++, lla++) {
|
||||||
|
cp += sprintf(cp, "%s%x", cplim, *lla);
|
||||||
|
cplim = ":";
|
||||||
|
}
|
||||||
|
cp = workbuf;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
cp = link_ntoa(sdl);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case AF_INET:
|
||||||
|
{
|
||||||
|
struct sockaddr_in *sin = (struct sockaddr_in *)sa;
|
||||||
|
if (sin->sin_addr.s_addr == 0)
|
||||||
|
cp = "default";
|
||||||
|
else
|
||||||
|
cp = (flags & RTF_HOST) ? routename(sa) : netname(sa);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef INET6
|
||||||
|
case AF_INET6:
|
||||||
|
{
|
||||||
|
struct sockaddr_in6 *sin = (struct sockaddr_in6 *)sa;
|
||||||
|
|
||||||
|
cp = IN6_IS_ADDR_UNSPECIFIED(&sin->sin6_addr) ? "default" :
|
||||||
|
((flags & RTF_HOST) ?
|
||||||
|
routename(sa) : netname(sa));
|
||||||
|
/* make sure numeric address is not truncated */
|
||||||
|
if (strchr(cp, ':') != NULL && strlen(cp) > width)
|
||||||
|
width = strlen(cp);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
#endif /* INET6 */
|
||||||
|
|
||||||
|
/*
|
||||||
|
case AF_NS:
|
||||||
|
cp = ns_print((struct sockaddr_ns *)sa);
|
||||||
|
break;
|
||||||
|
*/
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
u_char *s = (u_char *)sa->sa_data, *slim;
|
||||||
|
|
||||||
|
slim = sa->sa_len + (u_char *) sa;
|
||||||
|
cplim = cp + sizeof(workbuf) - 6;
|
||||||
|
cp += sprintf(cp, "(%d)", sa->sa_family);
|
||||||
|
while (s < slim && cp < cplim) {
|
||||||
|
cp += sprintf(cp, " %02x", *s++);
|
||||||
|
if (s < slim)
|
||||||
|
cp += sprintf(cp, "%02x", *s++);
|
||||||
|
}
|
||||||
|
cp = workbuf;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (width < 0 )
|
||||||
|
printf("%s ", cp);
|
||||||
|
else {
|
||||||
|
if (nflag)
|
||||||
|
printf("%-*s ", width, cp);
|
||||||
|
else
|
||||||
|
printf("%-*.*s ", width, width, cp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
p_flags(f, format)
|
||||||
|
int f;
|
||||||
|
char *format;
|
||||||
|
{
|
||||||
|
char name[33], *flags;
|
||||||
|
const struct bits *p = bits;
|
||||||
|
|
||||||
|
for (flags = name; p->b_mask && flags < &name[sizeof name-2]; p++)
|
||||||
|
if (p->b_mask & f)
|
||||||
|
*flags++ = p->b_val;
|
||||||
|
*flags = '\0';
|
||||||
|
printf(format, name);
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
SubDir OBOS_TOP src apps bin traceroute ;
|
||||||
|
|
||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user