#include <stdio.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <net/if.h>
#include <netinet/in.h>
#include <string.h>
#include <ctype.h>
#include "bootp.h"
#include "bootptest.h"
static void rfc1048_print(u_char *bp, int length);
static void cmu_print(u_char *bp, int length);
static void other_print(u_char *bp, int length);
static void dump_hex(u_char *bp, int len);
void
bootp_print(struct bootp *bp, int length, u_short sport, u_short dport)
{
static char tstr[] = " [|bootp]";
static unsigned char vm_cmu[4] = VM_CMU;
static unsigned char vm_rfc1048[4] = VM_RFC1048;
u_char *ep;
int vdlen;
#define TCHECK(var, l) if ((u_char *)&(var) > ep - l) goto trunc
if (length != sizeof(struct bootp))
(void) printf(" [len=%d]", length);
ep = (u_char *) snapend;
switch (bp->bp_op) {
case BOOTREQUEST:
if (sport != IPPORT_BOOTPC || dport != IPPORT_BOOTPS)
printf(" (request)");
break;
case BOOTREPLY:
if (sport != IPPORT_BOOTPS || dport != IPPORT_BOOTPC)
printf(" (reply)");
break;
default:
printf(" bootp-#%d", bp->bp_op);
}
if (bp->bp_htype != 1)
printf(" htype:%d", bp->bp_htype);
if (bp->bp_hlen != 6)
printf(" hlen:%d", bp->bp_hlen);
if (bp->bp_hlen) {
struct ether_header *eh;
char *e;
TCHECK(bp->bp_chaddr[0], 6);
eh = (struct ether_header *) packetp;
if (bp->bp_op == BOOTREQUEST)
e = (char *) ESRC(eh);
else if (bp->bp_op == BOOTREPLY)
e = (char *) EDST(eh);
else
e = NULL;
if (e == NULL || bcmp((char *) bp->bp_chaddr, e, 6))
dump_hex(bp->bp_chaddr, bp->bp_hlen);
}
if (bp->bp_hops)
printf(" hops:%d", bp->bp_hops);
if (bp->bp_xid)
printf(" xid:%ld", (long)ntohl(bp->bp_xid));
if (bp->bp_secs)
printf(" secs:%d", ntohs(bp->bp_secs));
TCHECK(bp->bp_ciaddr, sizeof(bp->bp_ciaddr));
if (bp->bp_ciaddr.s_addr)
printf(" C:%s", ipaddr_string(&bp->bp_ciaddr));
TCHECK(bp->bp_yiaddr, sizeof(bp->bp_yiaddr));
if (bp->bp_yiaddr.s_addr)
printf(" Y:%s", ipaddr_string(&bp->bp_yiaddr));
TCHECK(bp->bp_siaddr, sizeof(bp->bp_siaddr));
if (bp->bp_siaddr.s_addr)
printf(" S:%s", ipaddr_string(&bp->bp_siaddr));
TCHECK(bp->bp_giaddr, sizeof(bp->bp_giaddr));
if (bp->bp_giaddr.s_addr)
printf(" G:%s", ipaddr_string(&bp->bp_giaddr));
TCHECK(bp->bp_sname[0], sizeof(bp->bp_sname));
if (*bp->bp_sname) {
printf(" sname:");
if (printfn(bp->bp_sname, ep)) {
fputs(tstr + 1, stdout);
return;
}
}
TCHECK(bp->bp_file[0], sizeof(bp->bp_file));
if (*bp->bp_file) {
printf(" file:");
if (printfn(bp->bp_file, ep)) {
fputs(tstr + 1, stdout);
return;
}
}
if (vflag <= 0)
return;
vdlen = sizeof(bp->bp_vend);
if (vdlen < (ep - bp->bp_vend))
vdlen = (ep - bp->bp_vend);
TCHECK(bp->bp_vend[0], vdlen);
printf(" vend");
if (!bcmp(bp->bp_vend, vm_rfc1048, sizeof(u_int32)))
rfc1048_print(bp->bp_vend, vdlen);
else if (!bcmp(bp->bp_vend, vm_cmu, sizeof(u_int32)))
cmu_print(bp->bp_vend, vdlen);
else
other_print(bp->bp_vend, vdlen);
return;
trunc:
fputs(tstr, stdout);
#undef TCHECK
}
char *
rfc1048_opts[] = {
"?PAD",
"iSM",
"lTZ",
"iGW",
"iTS",
"iINS",
"iDNS",
"iLOG",
"iCS",
"iLPR",
"iIPS",
"iRLP",
"aHN",
"sBFS",
"aDUMP",
"aDNAM",
"iSWAP",
"aROOT",
"aEXTF",
#if 1
"bIP-forward",
"bIP-srcroute",
"iIP-filters",
"sIP-maxudp",
"bIP-ttlive",
"lIP-pmtuage",
"sIP-pmtutab",
"sIP-mtu-sz",
"bIP-mtu-sl",
"bIP-bcast1",
"bIP-mask-d",
"bIP-mask-s",
"bIP-rt-dsc",
"iIP-rt-sa",
"iIP-routes",
"bLL-trailer",
"lLL-arp-tmo",
"bLL-ether2",
"bTCP-def-ttl",
"lTCP-KA-tmo",
"bTCP-KA-junk",
"aNISDOM",
"iNISSRV",
"iNTPSRV",
"?VSINFO",
"iNBiosNS",
"iNBiosDD",
"bNBiosNT",
"?NBiosS",
"iXW-FS",
"iXW-DM",
#endif
};
#define KNOWN_OPTIONS (sizeof(rfc1048_opts) / sizeof(rfc1048_opts[0]))
static void
rfc1048_print(u_char *bp, int length)
{
u_char tag;
u_char *ep;
int len;
u_int32 ul;
u_short us;
struct in_addr ia;
char *optstr;
printf("-rfc1395");
bp += sizeof(int32);
ep = bp + length;
while (bp < ep) {
tag = *bp++;
if (tag == TAG_PAD)
continue;
if (tag == TAG_END)
return;
if (tag < KNOWN_OPTIONS) {
optstr = rfc1048_opts[tag];
printf(" %s:", optstr + 1);
} else {
printf(" T%d:", tag);
optstr = "?";
}
len = *bp++;
if (bp + len > ep) {
printf(" |(%d>%td)", len, ep - bp);
return;
}
switch (optstr[0]) {
case 'a':
printfn(bp, bp + len);
bp += len;
len = 0;
break;
case 's':
while (len >= 2) {
bcopy((char *) bp, (char *) &us, 2);
printf("%d", ntohs(us));
bp += 2;
len -= 2;
if (len) printf(",");
}
if (len) printf("(junk=%d)", len);
break;
case 'l':
while (len >= 4) {
bcopy((char *) bp, (char *) &ul, 4);
printf("%ld", (long)ntohl(ul));
bp += 4;
len -= 4;
if (len) printf(",");
}
if (len) printf("(junk=%d)", len);
break;
case 'i':
while (len >= 4) {
bcopy((char *) bp, (char *) &ia, 4);
printf("%s", ipaddr_string(&ia));
bp += 4;
len -= 4;
if (len) printf(",");
}
if (len) printf("(junk=%d)", len);
break;
case 'b':
default:
break;
}
if (len) {
dump_hex(bp, len);
if (isascii(*bp) && isprint(*bp)) {
printf("(");
printfn(bp, bp + len);
printf(")");
}
bp += len;
len = 0;
}
}
}
static void
cmu_print(u_char *bp, int length)
{
struct cmu_vend *v;
printf("-cmu");
v = (struct cmu_vend *) bp;
if (length < sizeof(*v)) {
printf(" |L=%d", length);
return;
}
if (v->v_flags & VF_SMASK) {
printf(" SM:%s", ipaddr_string(&v->v_smask));
}
if (v->v_dgate.s_addr)
printf(" GW:%s", ipaddr_string(&v->v_dgate));
if (v->v_dns1.s_addr)
printf(" DNS1:%s", ipaddr_string(&v->v_dns1));
if (v->v_dns2.s_addr)
printf(" DNS2:%s", ipaddr_string(&v->v_dns2));
if (v->v_ins1.s_addr)
printf(" INS1:%s", ipaddr_string(&v->v_ins1));
if (v->v_ins2.s_addr)
printf(" INS2:%s", ipaddr_string(&v->v_ins2));
if (v->v_ts1.s_addr)
printf(" TS1:%s", ipaddr_string(&v->v_ts1));
if (v->v_ts2.s_addr)
printf(" TS2:%s", ipaddr_string(&v->v_ts2));
}
static void
other_print(u_char *bp, int length)
{
u_char *ep;
u_char *zp;
ep = bp + length;
for (zp = ep; zp > bp; zp--) {
if (zp[-1] != 0)
break;
}
if (zp == bp) {
printf("-all-zero");
return;
}
printf("-unknown");
if (zp + 2 > ep)
zp = ep;
while (bp < zp) {
printf(".%02X", *bp);
bp++;
}
if (zp < ep)
printf(".00...");
return;
}
static void
dump_hex(u_char *bp, int len)
{
while (len > 0) {
printf("%02X", *bp);
bp++;
len--;
if (len) printf(".");
}
}