Blame


1 d7094239 2003-11-23 devnull #include <u.h>
2 d7094239 2003-11-23 devnull #include <libc.h>
3 d7094239 2003-11-23 devnull #include <ctype.h>
4 d7094239 2003-11-23 devnull #include <ip.h>
5 d7094239 2003-11-23 devnull
6 d7094239 2003-11-23 devnull static Ipifc**
7 d7094239 2003-11-23 devnull _readoldipifc(char *buf, Ipifc **l, int index)
8 d7094239 2003-11-23 devnull {
9 d7094239 2003-11-23 devnull char *f[200];
10 d7094239 2003-11-23 devnull int i, n;
11 d7094239 2003-11-23 devnull Ipifc *ifc;
12 d7094239 2003-11-23 devnull Iplifc *lifc, **ll;
13 d7094239 2003-11-23 devnull
14 d7094239 2003-11-23 devnull /* allocate new interface */
15 d7094239 2003-11-23 devnull *l = ifc = mallocz(sizeof(Ipifc), 1);
16 d7094239 2003-11-23 devnull if(ifc == nil)
17 d7094239 2003-11-23 devnull return l;
18 d7094239 2003-11-23 devnull l = &ifc->next;
19 d7094239 2003-11-23 devnull ifc->index = index;
20 d7094239 2003-11-23 devnull
21 d7094239 2003-11-23 devnull n = tokenize(buf, f, nelem(f));
22 d7094239 2003-11-23 devnull if(n < 2)
23 d7094239 2003-11-23 devnull return l;
24 d7094239 2003-11-23 devnull
25 d7094239 2003-11-23 devnull strncpy(ifc->dev, f[0], sizeof ifc->dev);
26 d7094239 2003-11-23 devnull ifc->dev[sizeof(ifc->dev) - 1] = 0;
27 d7094239 2003-11-23 devnull ifc->mtu = strtoul(f[1], nil, 10);
28 d7094239 2003-11-23 devnull
29 d7094239 2003-11-23 devnull ll = &ifc->lifc;
30 d7094239 2003-11-23 devnull for(i = 2; n-i >= 7; i += 7){
31 d7094239 2003-11-23 devnull /* allocate new local address */
32 d7094239 2003-11-23 devnull *ll = lifc = mallocz(sizeof(Iplifc), 1);
33 d7094239 2003-11-23 devnull ll = &lifc->next;
34 d7094239 2003-11-23 devnull
35 d7094239 2003-11-23 devnull parseip(lifc->ip, f[i]);
36 d7094239 2003-11-23 devnull parseipmask(lifc->mask, f[i+1]);
37 d7094239 2003-11-23 devnull parseip(lifc->net, f[i+2]);
38 d7094239 2003-11-23 devnull ifc->pktin = strtoul(f[i+3], nil, 10);
39 d7094239 2003-11-23 devnull ifc->pktout = strtoul(f[i+4], nil, 10);
40 d7094239 2003-11-23 devnull ifc->errin = strtoul(f[i+5], nil, 10);
41 d7094239 2003-11-23 devnull ifc->errout = strtoul(f[i+6], nil, 10);
42 d7094239 2003-11-23 devnull }
43 d7094239 2003-11-23 devnull return l;
44 d7094239 2003-11-23 devnull }
45 d7094239 2003-11-23 devnull
46 d7094239 2003-11-23 devnull static char*
47 d7094239 2003-11-23 devnull findfield(char *name, char **f, int n)
48 d7094239 2003-11-23 devnull {
49 d7094239 2003-11-23 devnull int i;
50 d7094239 2003-11-23 devnull
51 d7094239 2003-11-23 devnull for(i = 0; i < n-1; i++)
52 d7094239 2003-11-23 devnull if(strcmp(f[i], name) == 0)
53 d7094239 2003-11-23 devnull return f[i+1];
54 d7094239 2003-11-23 devnull return "";
55 d7094239 2003-11-23 devnull }
56 d7094239 2003-11-23 devnull
57 d7094239 2003-11-23 devnull static Ipifc**
58 d7094239 2003-11-23 devnull _readipifc(char *file, Ipifc **l, int index)
59 d7094239 2003-11-23 devnull {
60 d7094239 2003-11-23 devnull int i, n, fd, lines;
61 d7094239 2003-11-23 devnull char buf[4*1024];
62 d7094239 2003-11-23 devnull char *line[32];
63 d7094239 2003-11-23 devnull char *f[64];
64 d7094239 2003-11-23 devnull Ipifc *ifc;
65 d7094239 2003-11-23 devnull Iplifc *lifc, **ll;
66 d7094239 2003-11-23 devnull
67 d7094239 2003-11-23 devnull /* read the file */
68 d7094239 2003-11-23 devnull fd = open(file, OREAD);
69 d7094239 2003-11-23 devnull if(fd < 0)
70 d7094239 2003-11-23 devnull return l;
71 d7094239 2003-11-23 devnull n = 0;
72 d7094239 2003-11-23 devnull while((i = read(fd, buf+n, sizeof(buf)-1-n)) > 0 && n < sizeof(buf) - 1)
73 d7094239 2003-11-23 devnull n += i;
74 d7094239 2003-11-23 devnull buf[n] = 0;
75 d7094239 2003-11-23 devnull close(fd);
76 d7094239 2003-11-23 devnull
77 d7094239 2003-11-23 devnull if(strncmp(buf, "device", 6) != 0)
78 d7094239 2003-11-23 devnull return _readoldipifc(buf, l, index);
79 d7094239 2003-11-23 devnull
80 d7094239 2003-11-23 devnull /* allocate new interface */
81 d7094239 2003-11-23 devnull *l = ifc = mallocz(sizeof(Ipifc), 1);
82 d7094239 2003-11-23 devnull if(ifc == nil)
83 d7094239 2003-11-23 devnull return l;
84 d7094239 2003-11-23 devnull l = &ifc->next;
85 d7094239 2003-11-23 devnull ifc->index = index;
86 d7094239 2003-11-23 devnull
87 d7094239 2003-11-23 devnull lines = getfields(buf, line, nelem(line), 1, "\n");
88 d7094239 2003-11-23 devnull
89 d7094239 2003-11-23 devnull /* pick off device specific info(first line) */
90 d7094239 2003-11-23 devnull n = tokenize(line[0], f, nelem(f));
91 d7094239 2003-11-23 devnull strncpy(ifc->dev, findfield("device", f, n), sizeof(ifc->dev));
92 d7094239 2003-11-23 devnull ifc->dev[sizeof(ifc->dev)-1] = 0;
93 d7094239 2003-11-23 devnull if(ifc->dev[0] == 0){
94 d7094239 2003-11-23 devnull free(ifc);
95 d7094239 2003-11-23 devnull return l;
96 d7094239 2003-11-23 devnull }
97 d7094239 2003-11-23 devnull ifc->mtu = strtoul(findfield("maxmtu", f, n), nil, 10);
98 d7094239 2003-11-23 devnull ifc->sendra6 = atoi(findfield("sendra", f, n));
99 d7094239 2003-11-23 devnull ifc->recvra6 = atoi(findfield("recvra", f, n));
100 d7094239 2003-11-23 devnull ifc->rp.mflag = atoi(findfield("mflag", f, n));
101 d7094239 2003-11-23 devnull ifc->rp.oflag = atoi(findfield("oflag", f, n));
102 d7094239 2003-11-23 devnull ifc->rp.maxraint = atoi(findfield("maxraint", f, n));
103 d7094239 2003-11-23 devnull ifc->rp.minraint = atoi(findfield("minraint", f, n));
104 d7094239 2003-11-23 devnull ifc->rp.linkmtu = atoi(findfield("linkmtu", f, n));
105 d7094239 2003-11-23 devnull ifc->rp.reachtime = atoi(findfield("reachtime", f, n));
106 d7094239 2003-11-23 devnull ifc->rp.rxmitra = atoi(findfield("rxmitra", f, n));
107 d7094239 2003-11-23 devnull ifc->rp.ttl = atoi(findfield("ttl", f, n));
108 d7094239 2003-11-23 devnull ifc->rp.routerlt = atoi(findfield("routerlt", f, n));
109 d7094239 2003-11-23 devnull ifc->pktin = strtoul(findfield("pktin", f, n), nil, 10);
110 d7094239 2003-11-23 devnull ifc->pktout = strtoul(findfield("pktout", f, n), nil, 10);
111 d7094239 2003-11-23 devnull ifc->errin = strtoul(findfield("errin", f, n), nil, 10);
112 d7094239 2003-11-23 devnull ifc->errout = strtoul(findfield("errout", f, n), nil, 10);
113 d7094239 2003-11-23 devnull
114 d7094239 2003-11-23 devnull /* now read the addresses */
115 d7094239 2003-11-23 devnull ll = &ifc->lifc;
116 d7094239 2003-11-23 devnull for(i = 1; i < lines; i++){
117 d7094239 2003-11-23 devnull n = tokenize(line[i], f, nelem(f));
118 d7094239 2003-11-23 devnull if(n < 5)
119 d7094239 2003-11-23 devnull break;
120 d7094239 2003-11-23 devnull
121 d7094239 2003-11-23 devnull /* allocate new local address */
122 d7094239 2003-11-23 devnull *ll = lifc = mallocz(sizeof(Iplifc), 1);
123 d7094239 2003-11-23 devnull ll = &lifc->next;
124 d7094239 2003-11-23 devnull
125 d7094239 2003-11-23 devnull parseip(lifc->ip, f[0]);
126 d7094239 2003-11-23 devnull parseipmask(lifc->mask, f[1]);
127 d7094239 2003-11-23 devnull parseip(lifc->net, f[2]);
128 d7094239 2003-11-23 devnull
129 d7094239 2003-11-23 devnull lifc->validlt = strtoul(f[3], nil, 10);
130 d7094239 2003-11-23 devnull lifc->preflt = strtoul(f[4], nil, 10);
131 d7094239 2003-11-23 devnull }
132 d7094239 2003-11-23 devnull
133 d7094239 2003-11-23 devnull return l;
134 d7094239 2003-11-23 devnull }
135 d7094239 2003-11-23 devnull
136 d7094239 2003-11-23 devnull static void
137 d7094239 2003-11-23 devnull _freeifc(Ipifc *ifc)
138 d7094239 2003-11-23 devnull {
139 d7094239 2003-11-23 devnull Ipifc *next;
140 d7094239 2003-11-23 devnull Iplifc *lnext, *lifc;
141 d7094239 2003-11-23 devnull
142 d7094239 2003-11-23 devnull if(ifc == nil)
143 d7094239 2003-11-23 devnull return;
144 d7094239 2003-11-23 devnull for(; ifc; ifc = next){
145 d7094239 2003-11-23 devnull next = ifc->next;
146 d7094239 2003-11-23 devnull for(lifc = ifc->lifc; lifc; lifc = lnext){
147 d7094239 2003-11-23 devnull lnext = lifc->next;
148 d7094239 2003-11-23 devnull free(lifc);
149 d7094239 2003-11-23 devnull }
150 d7094239 2003-11-23 devnull free(ifc);
151 d7094239 2003-11-23 devnull }
152 d7094239 2003-11-23 devnull }
153 d7094239 2003-11-23 devnull
154 d7094239 2003-11-23 devnull Ipifc*
155 d7094239 2003-11-23 devnull readipifc(char *net, Ipifc *ifc, int index)
156 d7094239 2003-11-23 devnull {
157 d7094239 2003-11-23 devnull int fd, i, n;
158 d7094239 2003-11-23 devnull Dir *dir;
159 d7094239 2003-11-23 devnull char directory[128];
160 d7094239 2003-11-23 devnull char buf[128];
161 d7094239 2003-11-23 devnull Ipifc **l;
162 d7094239 2003-11-23 devnull
163 d7094239 2003-11-23 devnull _freeifc(ifc);
164 d7094239 2003-11-23 devnull
165 d7094239 2003-11-23 devnull l = &ifc;
166 d7094239 2003-11-23 devnull ifc = nil;
167 d7094239 2003-11-23 devnull
168 d7094239 2003-11-23 devnull if(net == 0)
169 d7094239 2003-11-23 devnull net = "/net";
170 d7094239 2003-11-23 devnull snprint(directory, sizeof(directory), "%s/ipifc", net);
171 d7094239 2003-11-23 devnull
172 d7094239 2003-11-23 devnull if(index >= 0){
173 d7094239 2003-11-23 devnull snprint(buf, sizeof(buf), "%s/%d/status", directory, index);
174 d7094239 2003-11-23 devnull _readipifc(buf, l, index);
175 d7094239 2003-11-23 devnull } else {
176 d7094239 2003-11-23 devnull fd = open(directory, OREAD);
177 d7094239 2003-11-23 devnull if(fd < 0)
178 d7094239 2003-11-23 devnull return nil;
179 d7094239 2003-11-23 devnull n = dirreadall(fd, &dir);
180 d7094239 2003-11-23 devnull close(fd);
181 d7094239 2003-11-23 devnull
182 d7094239 2003-11-23 devnull for(i = 0; i < n; i++){
183 d7094239 2003-11-23 devnull if(strcmp(dir[i].name, "clone") == 0)
184 d7094239 2003-11-23 devnull continue;
185 d7094239 2003-11-23 devnull if(strcmp(dir[i].name, "stats") == 0)
186 d7094239 2003-11-23 devnull continue;
187 d7094239 2003-11-23 devnull snprint(buf, sizeof(buf), "%s/%s/status", directory, dir[i].name);
188 d7094239 2003-11-23 devnull l = _readipifc(buf, l, atoi(dir[i].name));
189 d7094239 2003-11-23 devnull }
190 d7094239 2003-11-23 devnull free(dir);
191 d7094239 2003-11-23 devnull }
192 d7094239 2003-11-23 devnull
193 d7094239 2003-11-23 devnull return ifc;
194 d7094239 2003-11-23 devnull }