#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: bootptest.c,v 1.22 2022/12/24 15:23:03 andvar Exp $");
#endif
static const char usage[] =
"Usage: %s [-f bootfile] [-h] [-m magic_number] server-name\n"
"\t[vendor-data-template-file]\n";
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/poll.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <ctype.h>
#include <netdb.h>
#include <assert.h>
#include <unistd.h>
#include "bootp.h"
#include "bootptest.h"
#include "getif.h"
#include "report.h"
#include "patchlevel.h"
#define LOG_ERR 1
#define BUFLEN 1024
#define WAITSECS 1
#define MAXWAIT 10
int vflag = 1;
int tflag = 0;
int thiszone;
char *progname;
unsigned char *packetp;
unsigned char *snapend;
int snaplen;
u_short bootps_port, bootpc_port;
struct sockaddr_in sin_server;
struct sockaddr_in sin_client;
struct sockaddr_in sin_from;
u_char eaddr[16];
int debug = 1;
char hostname[MAXHOSTNAMELEN + 1];
char *sndbuf;
char *rcvbuf;
unsigned char vm_cmu[4] = VM_CMU;
unsigned char vm_rfc1048[4] = VM_RFC1048;
short secs;
extern int getether(char *, char *);
void send_request(int);
int
main(int argc, char **argv)
{
struct bootp *bp;
struct servent *sep;
struct hostent *hep;
char *servername = NULL;
char *vendor_file = NULL;
char *bp_file = NULL;
socklen_t fromlen;
int s;
int n, recvcnt;
int use_hwa = 0;
int32 vend_magic;
int32 xid;
struct pollfd set[1];
progname = strrchr(argv[0], '/');
if (progname)
progname++;
else
progname = argv[0];
argc--;
argv++;
if (debug)
printf("%s: version %s.%d\n", progname, VERSION, PATCHLEVEL);
assert(sizeof(struct bootp) == BP_MINPKTSZ);
sndbuf = malloc(BUFLEN);
rcvbuf = malloc(BUFLEN);
if (!sndbuf || !rcvbuf) {
printf("malloc failed\n");
exit(1);
}
bcopy(vm_rfc1048, (char*)&vend_magic, 4);
while (argc > 0) {
if (argv[0][0] != '-')
break;
switch (argv[0][1]) {
case 'f':
if (argc < 2)
goto error;
argc--; argv++;
bp_file = *argv;
break;
case 'h':
use_hwa = 1;
break;
case 'm':
if (argc < 2)
goto error;
argc--; argv++;
vend_magic = inet_addr(*argv);
break;
error:
default:
(void)fprintf(stderr, usage, getprogname());
exit(1);
}
argc--;
argv++;
}
if (argc > 0) {
servername = *argv;
argc--;
argv++;
}
if (argc > 0) {
vendor_file = *argv;
argc--;
argv++;
}
if (!servername) {
printf("missing server name.\n");
(void)fprintf(stderr, usage, getprogname());
exit(1);
}
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket");
exit(1);
}
sep = getservbyname("bootps", "udp");
if (sep) {
bootps_port = ntohs((u_short) sep->s_port);
} else {
fprintf(stderr, "udp/bootps: unknown service -- using port %d\n",
IPPORT_BOOTPS);
bootps_port = (u_short) IPPORT_BOOTPS;
}
if (servername) {
if (inet_aton(servername, &sin_server.sin_addr) == 0) {
hep = gethostbyname(servername);
if (!hep) {
fprintf(stderr, "%s: unknown host\n", servername);
exit(1);
}
memcpy(&sin_server.sin_addr, hep->h_addr,
sizeof(sin_server.sin_addr));
}
} else {
sin_server.sin_addr.s_addr = INADDR_ANY;
}
sin_server.sin_family = AF_INET;
sin_server.sin_port = htons(bootps_port);
sep = getservbyname("bootpc", "udp");
if (sep) {
bootpc_port = ntohs(sep->s_port);
} else {
fprintf(stderr, "udp/bootpc: unknown service -- using port %d\n",
IPPORT_BOOTPC);
bootpc_port = (u_short) IPPORT_BOOTPC;
}
sin_client.sin_family = AF_INET;
sin_client.sin_port = htons(bootpc_port);
sin_client.sin_addr.s_addr = INADDR_ANY;
if (bind(s, (struct sockaddr *) &sin_client, sizeof(sin_client)) < 0) {
perror("bind BOOTPC port");
if (errno == EACCES)
fprintf(stderr, "You need to run this as root\n");
exit(1);
}
bp = (struct bootp *) sndbuf;
bzero(bp, sizeof(*bp));
bp->bp_op = BOOTREQUEST;
xid = (int32) getpid();
bp->bp_xid = (u_int32) htonl(xid);
if (bp_file)
strlcpy(bp->bp_file, bp_file, sizeof(bp->bp_file));
if (use_hwa) {
struct ifreq *ifr;
ifr = getif(s, &sin_server.sin_addr);
if (!ifr) {
printf("No interface for %s\n", servername);
exit(1);
}
if (getether(ifr->ifr_name, (char *)eaddr)) {
printf("Can not get ether addr for %s\n", ifr->ifr_name);
exit(1);
}
bp->bp_htype = 1;
bp->bp_hlen = 6;
bcopy(eaddr, bp->bp_chaddr, bp->bp_hlen);
} else {
gethostname(hostname, sizeof(hostname));
hostname[sizeof(hostname) - 1] = '\0';
hep = gethostbyname(hostname);
if (!hep) {
printf("Can not get my IP address\n");
exit(1);
}
bcopy(hep->h_addr, &bp->bp_ciaddr, hep->h_length);
}
bcopy((char*)&vend_magic, bp->bp_vend, 4);
if (vend_magic)
bp->bp_vend[4] = TAG_END;
snaplen = sizeof(*bp);
if (vendor_file) {
int fd = open(vendor_file, 0);
if (fd < 0) {
perror(vendor_file);
exit(1);
}
n = BUFLEN - sizeof(*bp) + BP_VEND_LEN;
n = read(fd, bp->bp_vend, n);
close(fd);
if (n < 0) {
perror(vendor_file);
exit(1);
}
printf("read %d bytes of vendor template\n", n);
if (n > BP_VEND_LEN) {
printf("warning: extended options in use (len > %d)\n",
BP_VEND_LEN);
snaplen += (n - BP_VEND_LEN);
}
}
packetp = (unsigned char *) eaddr;
snapend = (unsigned char *) sndbuf + snaplen;
recvcnt = 0;
bp->bp_secs = secs = 0;
send_request(s);
set[0].fd = s;
set[0].events = POLLIN;
while (1) {
n = poll(set, 1, WAITSECS * 1000);
if (n < 0) {
perror("poll");
break;
}
if (n == 0) {
if (recvcnt > 0)
exit(0);
secs += WAITSECS;
if (secs > MAXWAIT)
break;
bp->bp_secs = htons(secs);
send_request(s);
continue;
}
fromlen = sizeof(sin_from);
n = recvfrom(s, rcvbuf, BUFLEN, 0,
(struct sockaddr *) &sin_from, &fromlen);
if (n <= 0) {
continue;
}
if (n < (int)sizeof(struct bootp)) {
printf("received short packet\n");
continue;
}
recvcnt++;
printf("Recvd from %s", inet_ntoa(sin_from.sin_addr));
snaplen = n;
snapend = (unsigned char *) rcvbuf + snaplen;
bootp_print((struct bootp *)rcvbuf, n, sin_from.sin_port, 0);
putchar('\n');
}
fprintf(stderr, "no response from %s\n", servername);
exit(1);
}
void
send_request(int s)
{
printf("Sending to %s", inet_ntoa(sin_server.sin_addr));
bootp_print((struct bootp *)sndbuf, snaplen, sin_from.sin_port, 0);
putchar('\n');
if (sendto(s, sndbuf, snaplen, 0,
(struct sockaddr *) &sin_server,
sizeof(sin_server)) < 0)
{
perror("sendto server");
exit(1);
}
}
int
printfn(u_char *s, u_char *ep)
{
u_char c;
putchar('"');
while ((c = *s++) != 0) {
if (s > ep) {
putchar('"');
return (1);
}
if (!isascii(c)) {
c = toascii(c);
putchar('M');
putchar('-');
}
if (!isprint(c)) {
c ^= 0x40;
putchar('^');
}
putchar(c);
}
putchar('"');
return (0);
}
char *
ipaddr_string(struct in_addr *ina)
{
static char b[24];
u_char *p;
p = (u_char *) ina;
snprintf(b, sizeof(b), "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
return (b);
}