changelog shortlog tags branches changeset file revisions annotate raw help

Mercurial > hg > plan9front / sys/src/9/ip/eipconvtest.c

revision 7035: 65dab5125d9d
parent 7034: fe9f017a9d36
child 7036: 7bb53313aac9
     1.1deleted file mode 100644
     1.2--- a/sys/src/9/ip/eipconvtest.c
     1.3+++ /dev/null
     1.4@@ -1,152 +0,0 @@
     1.5-#include <u.h>
     1.6-#include <libc.h>
     1.7-
     1.8-enum
     1.9-{
    1.10-	Isprefix= 16,
    1.11-};
    1.12-
    1.13-uchar prefixvals[256] =
    1.14-{
    1.15-[0x00] 0 | Isprefix,
    1.16-[0x80] 1 | Isprefix,
    1.17-[0xC0] 2 | Isprefix,
    1.18-[0xE0] 3 | Isprefix,
    1.19-[0xF0] 4 | Isprefix,
    1.20-[0xF8] 5 | Isprefix,
    1.21-[0xFC] 6 | Isprefix,
    1.22-[0xFE] 7 | Isprefix,
    1.23-[0xFF] 8 | Isprefix,
    1.24-};
    1.25-
    1.26-uchar v4prefix[16] = {
    1.27-	0, 0, 0, 0,
    1.28-	0, 0, 0, 0,
    1.29-	0, 0, 0xff, 0xff,
    1.30-	0, 0, 0, 0
    1.31-};
    1.32-
    1.33-void
    1.34-hnputl(void *p, ulong v)
    1.35-{
    1.36-	uchar *a;
    1.37-
    1.38-	a = p;
    1.39-	a[0] = v>>24;
    1.40-	a[1] = v>>16;
    1.41-	a[2] = v>>8;
    1.42-	a[3] = v;
    1.43-}
    1.44-
    1.45-int
    1.46-eipconv(va_list *arg, Fconv *f)
    1.47-{
    1.48-	char buf[8*5];
    1.49-	static char *efmt = "%.2lux%.2lux%.2lux%.2lux%.2lux%.2lux";
    1.50-	static char *ifmt = "%d.%d.%d.%d";
    1.51-	uchar *p, ip[16];
    1.52-	ulong *lp;
    1.53-	ushort s;
    1.54-	int i, j, n, eln, eli;
    1.55-
    1.56-	switch(f->chr) {
    1.57-	case 'E':		/* Ethernet address */
    1.58-		p = va_arg(*arg, uchar*);
    1.59-		sprint(buf, efmt, p[0], p[1], p[2], p[3], p[4], p[5]);
    1.60-		break;
    1.61-	case 'I':		/* Ip address */
    1.62-		p = va_arg(*arg, uchar*);
    1.63-common:
    1.64-		if(memcmp(p, v4prefix, 12) == 0)
    1.65-			sprint(buf, ifmt, p[12], p[13], p[14], p[15]);
    1.66-		else {
    1.67-			/* find longest elision */
    1.68-			eln = eli = -1;
    1.69-			for(i = 0; i < 16; i += 2){
    1.70-				for(j = i; j < 16; j += 2)
    1.71-					if(p[j] != 0 || p[j+1] != 0)
    1.72-						break;
    1.73-				if(j > i && j - i > eln){
    1.74-					eli = i;
    1.75-					eln = j - i;
    1.76-				}
    1.77-			}
    1.78-
    1.79-			/* print with possible elision */
    1.80-			n = 0;
    1.81-			for(i = 0; i < 16; i += 2){
    1.82-				if(i == eli){
    1.83-					n += sprint(buf+n, "::");
    1.84-					i += eln;
    1.85-					if(i >= 16)
    1.86-						break;
    1.87-				} else if(i != 0)
    1.88-					n += sprint(buf+n, ":");
    1.89-				s = (p[i]<<8) + p[i+1];
    1.90-				n += sprint(buf+n, "%ux", s);
    1.91-			}
    1.92-		}
    1.93-		break;
    1.94-	case 'i':		/* v6 address as 4 longs */
    1.95-		lp = va_arg(*arg, ulong*);
    1.96-		for(i = 0; i < 4; i++)
    1.97-			hnputl(ip+4*i, *lp++);
    1.98-		p = ip;
    1.99-		goto common;
   1.100-	case 'V':		/* v4 ip address */
   1.101-		p = va_arg(*arg, uchar*);
   1.102-		sprint(buf, ifmt, p[0], p[1], p[2], p[3]);
   1.103-		break;
   1.104-	case 'M':		/* ip mask */
   1.105-		p = va_arg(*arg, uchar*);
   1.106-
   1.107-		/* look for a prefix mask */
   1.108-		for(i = 0; i < 16; i++)
   1.109-			if(p[i] != 0xff)
   1.110-				break;
   1.111-		if(i < 16){
   1.112-			if((prefixvals[p[i]] & Isprefix) == 0)
   1.113-				goto common;
   1.114-			for(j = i+1; j < 16; j++)
   1.115-				if(p[j] != 0)
   1.116-					goto common;
   1.117-			n = 8*i + (prefixvals[p[i]] & ~Isprefix);
   1.118-		} else
   1.119-			n = 8*16;
   1.120-
   1.121-		/* got one, use /xx format */
   1.122-		sprint(buf, "/%d", n);
   1.123-		break;
   1.124-	default:
   1.125-		strcpy(buf, "(eipconv)");
   1.126-	}
   1.127-	strconv(buf, f);
   1.128-	return sizeof(uchar*);
   1.129-}
   1.130-
   1.131-uchar testvec[11][16] =
   1.132-{
   1.133- { 0,0,0,0, 0,0,0,0, 0,0,0xff,0xff, 1,3,4,5, },
   1.134- { 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, },
   1.135- { 0xff,0xff,0x80,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, },
   1.136- { 0xff,0xff,0xff,0xc0, 0,0,0,0, 0,0,0,0, 0,0,0,0, },
   1.137- { 0xff,0xff,0xff,0xff, 0xe0,0,0,0, 0,0,0,0, 0,0,0,0, },
   1.138- { 0xff,0xff,0xff,0xff, 0xff,0xf0,0,0, 0,0,0,0, 0,0,0,0, },
   1.139- { 0xff,0xff,0xff,0xff, 0xff,0xff,0xf8,0, 0,0,0,0, 0,0,0,0, },
   1.140- { 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff, },
   1.141- { 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, },
   1.142- { 0,0,0,0, 0,0x11,0,0, 0,0,0,0, 0,0,0,0, },
   1.143- { 0,0,0,0x11, 0,0,0,0, 0,0,0,0, 0,0,0,0x12, },
   1.144-};
   1.145-
   1.146-void
   1.147-main(void)
   1.148-{
   1.149-	int i;
   1.150-
   1.151-	fmtinstall('I', eipconv);
   1.152-	fmtinstall('M', eipconv);
   1.153-	for(i = 0; i < 11; i++)
   1.154-		print("%I\n%M\n", testvec[i], testvec[i]);
   1.155-	exits(0);
   1.156-}