Use linker functions for NIC and medium pre-init
[akaros.git] / user / eipconvtest.c
1 // INFERNO
2 #if 0
3 #include <vfs.h>
4 #include <kfs.h>
5 #include <slab.h>
6 #include <kmalloc.h>
7 #include <kref.h>
8 #include <string.h>
9 #include <stdio.h>
10 #include <assert.h>
11 #include <error.h>
12 #include <cpio.h>
13 #include <pmap.h>
14 #include <smp.h>
15 #include <ip.h>
16 #endif
17
18 #include <stdio.h>
19 #include <sys/types.h>
20 #include <stdarg.h>
21 #include <string.h>
22
23 typedef unsigned char uint8_t;
24 typedef unsigned short uint16_t;
25 typedef unsigned long uint32_t;
26
27 enum {
28         Isprefix = 16,
29 };
30
31 uint8_t prefixvals[256] = {
32         [0x00] 0 | Isprefix,
33         [0x80] 1 | Isprefix,
34         [0xC0] 2 | Isprefix,
35         [0xE0] 3 | Isprefix,
36         [0xF0] 4 | Isprefix,
37         [0xF8] 5 | Isprefix,
38         [0xFC] 6 | Isprefix,
39         [0xFE] 7 | Isprefix,
40         [0xFF] 8 | Isprefix,
41 };
42
43 uint8_t v4prefix[16] = {
44         0, 0, 0, 0,
45         0, 0, 0, 0,
46         0, 0, 0xff, 0xff,
47         0, 0, 0, 0
48 };
49
50 void hnputl(void *p, uint32_t v)
51 {
52         uint8_t *a;
53
54         a = p;
55         a[0] = v >> 24;
56         a[1] = v >> 16;
57         a[2] = v >> 8;
58         a[3] = v;
59 }
60
61 char *eipconv(int fmt, ...)
62 {
63         va_list ap;
64         static char buf[8 * 5];
65         static char *efmt = "0x%.2lx0x%.2lx0x%.2lx0x%.2lx0x%.2lx0x%.2lx";
66         static char *ifmt = "%d.%d.%d.%d";
67         uint8_t *p, ip[16];
68         uint32_t *lp;
69         uint16_t s;
70         int i, j, n, eln, eli;
71         va_start(ap, fmt);
72         switch (fmt) {
73                 case 'E':       /* Ethernet address */
74                         p = va_arg(ap, uint8_t *);
75                         snprintf(buf, sizeof(buf), efmt, p[0], p[1], p[2], p[3], p[4],
76                                          p[5]);
77                         break;
78                 case 'I':       /* Ip address */
79                         p = va_arg(ap, uint8_t *);
80 common:
81                         if (memcmp(p, v4prefix, 12) == 0)
82                                 snprintf(buf, sizeof(buf), ifmt, p[12], p[13], p[14], p[15]);
83                         else {
84                                 /* find longest elision */
85                                 eln = eli = -1;
86                                 for (i = 0; i < 16; i += 2) {
87                                         for (j = i; j < 16; j += 2)
88                                                 if (p[j] != 0 || p[j + 1] != 0)
89                                                         break;
90                                         if (j > i && j - i > eln) {
91                                                 eli = i;
92                                                 eln = j - i;
93                                         }
94                                 }
95
96                                 /* print with possible elision */
97                                 n = 0;
98                                 for (i = 0; i < 16; i += 2) {
99                                         if (i == eli) {
100                                                 n += snprintf(buf, sizeof(buf) + n, "::");
101                                                 i += eln;
102                                                 if (i >= 16)
103                                                         break;
104                                         } else if (i != 0)
105                                                 n += snprintf(buf, sizeof(buf) + n, ":");
106                                         s = (p[i] << 8) + p[i + 1];
107                                         n += snprintf(buf, sizeof(buf) + n, "0x%x", s);
108                                 }
109                         }
110                         break;
111                 case 'i':       /* v6 address as 4 longs */
112                         lp = va_arg(ap, uint32_t *);
113                         for (i = 0; i < 4; i++)
114                                 hnputl(ip + 4 * i, *lp++);
115                         p = ip;
116                         goto common;
117                 case 'V':       /* v4 ip address */
118                         p = va_arg(ap, uint8_t *);
119                         snprintf(buf, sizeof(buf), ifmt, p[0], p[1], p[2], p[3]);
120                         break;
121                 case 'M':       /* ip mask */
122                         p = va_arg(ap, uint8_t *);
123
124                         /* look for a prefix mask */
125                         for (i = 0; i < 16; i++)
126                                 if (p[i] != 0xff)
127                                         break;
128                         if (i < 16) {
129                                 if ((prefixvals[p[i]] & Isprefix) == 0)
130                                         goto common;
131                                 for (j = i + 1; j < 16; j++)
132                                         if (p[j] != 0)
133                                                 goto common;
134                                 n = 8 * i + (prefixvals[p[i]] & ~Isprefix);
135                         } else
136                                 n = 8 * 16;
137
138                         /* got one, use /xx format */
139                         snprintf(buf, sizeof(buf), "/%d", n);
140                         break;
141                 default:
142                         strncpy(buf, "(eipconv)", sizeof(buf));
143         }
144
145         return buf;
146 }
147
148 uint8_t testvec[11][16] = {
149         {0,0,0,0,0,0,0,0,0,0,0xff,0xff,1,3,4,5,} ,
150         {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,} ,
151         {0xff,0xff,0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,} ,
152         {0xff,0xff,0xff,0xc0,0,0,0,0,0,0,0,0,0,0,0,0,} ,
153         {0xff,0xff,0xff,0xff,0xe0,0,0,0,0,0,0,0,0,0,0,0,} ,
154         {0xff,0xff,0xff,0xff,0xff,0xf0,0,0,0,0,0,0,0,0,0,0,} ,
155         {0xff,0xff,0xff,0xff,0xff,0xff,0xf8,0,0,0,0,0,0,0,0,0,} ,
156         {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,} ,
157         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,} ,
158         {0,0,0,0,0,0x11,0,0,0,0,0,0,0,0,0,0,} ,
159         {0,0,0,0x11,0,0,0,0,0,0,0,0,0,0,0,0x12,} ,
160 };
161
162 void main(void)
163 {
164         int i;
165         for (i = 0; i < 11; i++)
166                 printf("%s\n", eipconv('I', &testvec[i]));
167
168 }