📄 bootpcmd.c
字号:
static void
dumphosts()
{
int i;
struct host *hp;
struct arp_type *at;
printf ("\n\nStatus of host table\n");
if (Nhosts == 0) {
printf (" No hosts in host table\n");
return;
}
for (i = 0; i <= Nhosts-1; i++) {
hp = &hosts[i];
at = &Arp_type[hp->htype];
printf ("%s %s %s %s '%s'\n",
hp->name, ArpNames[hp->htype], (*at->format)(bp_ascii, hp->haddr),
inet_ntoa ((int32)hp->iaddr.s_addr),
hp->bootfile);
}
}
static int
bp_Host (argc, argv, p)
int argc;
char *argv[];
void *p;
{
struct host *hp;
int a0, a1, a2, a3;
struct arp_type *at;
char *usage = "bootpd host [<hostname> <hardware type> <hardware addr> <ip addr> [boot file]]\n";
switch (argc) {
case 1:
dumphosts();
break;
case 5:
case 6:
hp = &hosts[Nhosts];
/* get host name */
strncpy (hp->name, argv[1], sizeof (hp->name));
/* get hardware type */
/* This code borrowed from Phil Karn's arpcmd.c */
/* This is a kludge. It really ought to be table driven */
switch(tolower(argv[2][0])){
case 'n': /* Net/Rom pseudo-type */
hp->htype = ARP_NETROM;
break;
case 'e': /* "ether" */
hp->htype = ARP_ETHER;
break;
case 'a': /* "ax25" */
hp->htype = ARP_AX25;
break;
case 'm': /* "mac appletalk" */
hp->htype = ARP_APPLETALK;
break;
default:
printf("unknown hardware type \"%s\"\n",argv[2]);
return -1;
}
at = &Arp_type[hp->htype];
if(at->scan == NULL){
return 1;
}
/* Destination address */
(*at->scan)(hp->haddr,argv[3]);
/* get ip address */
if (4 != sscanf (argv[4], "%d.%d.%d.%d", &a0, &a1, &a2, &a3))
{
printf("bad internet address: %s\n", argv[1], linenum);
return (0);
}
hp->iaddr.s_addr = aton(argv[4]);
/* get the bootpfile */
if (argc == 6) strncpy (hp->bootfile, argv[5], sizeof (hp->bootfile));
else hp->bootfile[0] = 0;
bp_log ("Host added: %s %s %s %s '%s'\n",
hp->name, ArpNames[hp->htype], (*at->format)(bp_ascii, hp->haddr),
inet_ntoa ((int32)hp->iaddr.s_addr),
hp->bootfile);
Nhosts++;
break;
default:
printf (usage);
break;
}
return 0;
}
static int
bp_Homedir (argc, argv, p)
int argc;
char *argv[];
void *p;
{
char *usage = "bootpd homedir [<name of home directory> | default]\n";
if (argc == 1)
printf ("Bootp home directory: '%s'\n", homedir);
else if (argc == 2) {
if (strcmp (argv[1], "?") == 0)
printf (usage);
else if (strcmp (argv[1], "default") == 0) {
strcpy (homedir, BP_DEFAULT_DIR);
bp_log ("Bootp home directory set to: '%s'\n", homedir);
}
else {
strcpy (homedir, argv[1]);
bp_log ("Bootp home directory set to: '%s'\n", homedir);
}
}
else printf (usage);
return (0);
};
static int
bp_DefaultFile (argc, argv, p)
int argc;
char *argv[];
void *p;
{
char *usage = "bootpd defaultfile [<name of default boot file> | default]\n";
if (argc == 1)
printf ("Bootp default boot file: '%s'\n", defaultboot);
else if (argc == 2) {
if (strcmp (argv[1], "?") == 0)
printf (usage);
else if (strcmp (argv[1], "default") == 0)
strcpy (defaultboot, BP_DEFAULT_FILE);
else {
strcpy (defaultboot, argv[1]);
bp_log ("Bootp default boot file set to: '%s'\n", defaultboot);
}
}
else
printf (usage);
return (0);
};
static int
bp_DynamicRange (argc, argv, p)
int argc;
char *argv[];
void *p;
{
int i0, i1, i2, i3;
int32 start, end;
struct iface *iface;
char *usage = "bootpd dynip [<net name> | <netname> <IP address> <IP address> | <netname> off]\n";
if (argc == 1) {
da_status (NULL);
return 0;
}
if ((argc == 2) && (strcmp ("?", argv[1]) == 0)) {
printf (usage);
return 0;
}
/* get the interface */
iface = if_lookup (argv[1]);
if (iface == NULL) {
printf ("network '%s' not found\n", argv[1]);
return (-1);
}
if (argc == 2) {
da_status (iface);
return 0;
}
if (argc == 3) {
if (strcmp ("off", argv[2]) == 0)
da_done_net (iface);
else printf (usage);
}
else if (argc == 4) {
/* get ip address */
/* check the ip address - isaddr isn't a ka9q function */
if ((4 != sscanf (argv[2], "%d.%d.%d.%d", &i0, &i1, &i2, &i3)) ||
(i0 > 255) || (i1 > 255) || (i2 > 255) || (i3 > 255)
)
{
printf("bad internet address: %s\n", argv[2], linenum);
return (-1);
}
if ((4 != sscanf (argv[3], "%d.%d.%d.%d", &i0, &i1, &i2, &i3)) ||
(i0 > 255) || (i1 > 255) || (i2 > 255) || (i3 > 255)
)
{
printf("bad internet address: %s\n", argv[3], linenum);
return (-1);
}
start = aton(argv[2]);
end = aton(argv[3]);
da_serve_net (iface, start, end);
}
else {
printf (usage);
return (0);
}
return (0);
};
static int
bp_donothing (argc, argv, p)
int argc;
char *argv[];
void *p;
{
return (0);
}
/*
* Read bootptab database file. Avoid rereading the file if the
* write date hasn't changed since the last time we read it.
*/
int
readtab()
{
struct stat st;
/* If the file hasn't been opened, open it. */
if (bootfp == 0) {
if ((bootfp = fopen(bootptab, "r")) == NULL) {
bp_log("Can't open bootptab file: %s\n", bootptab);
return (-1);
}
}
/* Only reread if modified */
stat (bootptab, &st);
if (st.st_mtime == modtime && st.st_nlink) {
return (0); /* hasnt been modified or deleted yet */
}
/* It's been changed, reread. */
if ((bootfp = fopen(bootptab, "r")) == NULL) {
bp_log("Can't open %s\n", bootptab);
return (-1);
}
fstat(fileno(bootfp), &st);
bp_log("(re)reading %s\n", bootptab);
modtime = st.st_mtime;
/*
* read and parse each line in the file.
*/
line = mallocw(BUFSIZ);
while (fgets(line, BUFSIZ, bootfp) != NULL) {
linenum++;
if ((line[0] == 0) || (line[0] == '#') || (line[0] == ' '))
continue;
if (cmdparse (BootpdCmds, line, NULL) == -1)
continue;
}
fclose(bootfp);
free (line);
return (0);
}
void
readtab_shut()
{
modtime = 0;
}
/*
* log an error message
*
*/
void
bp_log(char *fmt,...)
{
FILE *fp;
va_list ap;
if (LogOnScreen) {
va_start(ap,fmt);
vprintf(fmt, ap);
va_end(ap);
fflush (stdout);
}
if (LogInFile) {
if ((fp = fopen(bootplog, "a+")) == NULL) {
printf ("Cannot open bootplog.\n");
return;
}
va_start(ap,fmt);
vfprintf(fp, fmt, ap);
va_end(ap);
fflush(fp);
fclose(fp);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -