morpheus-base

morpheus base system
git clone git://git.2f30.org/morpheus-base
Log | Files | Refs

debug.c (3280B)


      1 #include <stdarg.h>
      2 
      3 void bpdump(unsigned char *p, int n);
      4 
      5 unsigned short
      6 nhgets(unsigned char c[2])
      7 {
      8 	return ((c[0] << 8) + c[1]) & 0xffff;
      9 }
     10 
     11 unsigned long
     12 nhgetl(unsigned char c[4])
     13 {
     14 	return (nhgets(c) << 16) + nhgets(c + 2);
     15 }
     16 
     17 char *
     18 ipstr(unsigned char *ip)
     19 {
     20 	char * ch = malloc(3 * 4 + 3 + 10);
     21 	sprintf(ch, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
     22 	return ch;
     23 }
     24 
     25 #if 0
     26 void
     27 dbgprintf(char *str, ...)
     28 {
     29 	va_list ap;
     30 	va_start(ap, str);
     31 	vfprintf(stderr, str, ap);
     32 	va_end(ap);
     33 }
     34 #endif
     35 
     36 void
     37 bpdump(unsigned char *p, int n)
     38 {
     39 	int len, i, code;
     40 	Bootp *bp;
     41 	unsigned char type;
     42 	char *types[] = {
     43 		"discover", "offer", "request",
     44 		"decline", "ack", "nak", "release", "inform"
     45 	};
     46 	/* Udphdr *up; */
     47 
     48 	bp = (Bootp*)p;
     49 	/* up = (Udphdr*)bp->udphdr; */
     50 
     51 	if(n < bp->magic - p) {
     52 		fprintf(stderr, "dhcpclient: short bootp packet");
     53 		return;
     54 	}
     55 
     56 	optget(bp, &type, ODtype, sizeof type);
     57 	fprintf(stderr, "DHCP%s\n", types[type - 1]);
     58 	/* fprintf(stderr, "laddr=%I lport=%d raddr=%I rport=%d\n", up->laddr,
     59 	nhgets(up->lport), up->raddr, nhgets(up->rport)); */
     60 	fprintf(stderr, "op = %d htype = %d hlen = %d hops = %d\n", *bp->op, *bp->htype,
     61 	        *bp->hlen, *bp->hops);
     62 	fprintf(stderr, "xid = %x secs = %d flags = %x\n", nhgetl(bp->xid),
     63 	        nhgets(bp->secs), nhgets(bp->flags));
     64 	fprintf(stderr, "ciaddr = %s, yiaddr = %s, siaddr = %s, giaddr = %s\n",
     65 	        ipstr(bp->ciaddr), ipstr(bp->yiaddr), ipstr(bp->siaddr), ipstr(bp->giaddr));
     66 	fprintf(stderr, "chaddr =");
     67 	for(i=0; i<15; i++)
     68 		fprintf(stderr, "%.2x:", bp->chaddr[i]);
     69 	fprintf(stderr, "%.2x\n", bp->chaddr[15]);
     70 	fprintf(stderr, "sname = %s\n", bp->sname);
     71 	fprintf(stderr, "file = %s\n", bp->file);
     72 
     73 	n -= bp->magic - p;
     74 	p = bp->magic;
     75 
     76 	if(n < 4)
     77 		return;
     78 	if(memcmp(magic, p, 4) != 0)
     79 		fprintf(stderr, "dhcpclient: bad opt magic %#x %#x %#x %#x\n",
     80 		        p[0], p[1], p[2], p[3]);
     81 	p += 4;
     82 	n -= 4;
     83 
     84 	while(n > 0) {
     85 		code = *p++;
     86 		n--;
     87 		if(code == OBpad)
     88 			continue;
     89 		if(code == OBend)
     90 			break;
     91 		if(n == 0) {
     92 			fprintf(stderr, " bad option: %d", code);
     93 			return;
     94 		}
     95 		len = *p++;
     96 		n--;
     97 		if(len > n) {
     98 			fprintf(stderr, " bad option: %d", code);
     99 			return;
    100 		}
    101 		switch(code) {
    102 		case ODtype:
    103 			fprintf(stderr, "DHCP type %d\n", p[0]);
    104 			break;
    105 		case ODclientid:
    106 			fprintf(stderr, "client id=");
    107 			for(i = 0; i<len; i++)
    108 				fprintf(stderr, "%x ", p[i]);
    109 			fprintf(stderr, "\n");
    110 			break;
    111 		case ODlease:
    112 			fprintf(stderr, "lease=%d sec\n", nhgetl(p));
    113 			break;
    114 		case ODserverid:
    115 			fprintf(stderr, "server id=%s\n", ipstr(p));
    116 			break;
    117 		case OBmask:
    118 			fprintf(stderr, "mask=%s\n", ipstr(p));
    119 			break;
    120 		case OBrouter:
    121 			fprintf(stderr, "router=%s\n", ipstr(p));
    122 			break;
    123 		case ODipaddr:
    124 			fprintf(stderr, "ip addr=%s\n", ipstr(p));
    125 			break;
    126 		case OBdnsserver:
    127 			fprintf(stderr, "dns=%s\n", ipstr(p));
    128 			break;
    129 		case OBbaddr:
    130 			fprintf(stderr, "broadcast=%s\n", ipstr(p));
    131 			break;
    132 		case ODrenewaltime:
    133 			fprintf(stderr, "renew time=%d sec\n", nhgetl(p));
    134 			break;
    135 		case ODrebindingtime:
    136 			fprintf(stderr, "rebind time=%d sec\n", nhgetl(p));
    137 			break;
    138 		default:
    139 			fprintf(stderr, "unknown option %d\n", code);
    140 			for(i = 0; i<len; i++)
    141 				fprintf(stderr, "%x ", p[i]);
    142 			fprintf(stderr, "\n");
    143 			break;
    144 		}
    145 		p += len;
    146 		n -= len;
    147 	}
    148 }