]>
Commit | Line | Data |
---|---|---|
9607771a MT |
1 | /* SmoothWall libsmooth. |
2 | * | |
3 | * This program is distributed under the terms of the GNU General Public | |
4 | * Licence. See the file COPYING for details. | |
5 | * | |
6 | * (c) Lawrence Manning, 2001 | |
7 | * Contains network library functions. | |
8 | * | |
9607771a MT |
9 | */ |
10 | ||
11 | #include "libsmooth.h" | |
12 | #include <signal.h> | |
13 | ||
14 | extern FILE *flog; | |
15 | extern char *mylog; | |
16 | ||
17 | extern char **ctr; | |
18 | ||
5057b611 HS |
19 | extern struct nic nics[]; |
20 | extern struct knic knics[]; | |
21 | ||
9b040aa0 HS |
22 | char *ucolourcard[] = { "GREEN", "RED", "ORANGE", "BLUE", NULL }; |
23 | char *lcolourcard[] = { "green", "red", "orange", "blue", NULL }; | |
24 | ||
5057b611 HS |
25 | int scanned_nics_read_done = 0; |
26 | ||
9607771a MT |
27 | newtComponent networkform; |
28 | newtComponent addressentry; | |
29 | newtComponent netmaskentry; | |
30 | newtComponent statictyperadio; | |
31 | newtComponent dhcptyperadio; | |
32 | newtComponent pppoetyperadio; | |
9607771a MT |
33 | newtComponent dhcphostnameentry; |
34 | ||
35 | /* acceptable character filter for IP and netmaks entry boxes */ | |
36 | static int ip_input_filter(newtComponent entry, void * data, int ch, int cursor) | |
37 | { | |
38 | if ((ch >= '0' && ch <= '9') || ch == '.' || ch == '\r' || ch >= NEWT_KEY_EXTRA_BASE) | |
39 | return ch; | |
40 | return 0; | |
41 | } | |
42 | ||
43 | /* This is a groovie dialog for showing network info. Takes a keyvalue list, | |
44 | * a colour and a dhcp flag. Shows the current settings, and rewrites them | |
45 | * if necessary. DHCP flag sets wether to show the dhcp checkbox. */ | |
46 | int changeaddress(struct keyvalue *kv, char *colour, int typeflag, | |
47 | char *defaultdhcphostname) | |
48 | { | |
49 | char *addressresult; | |
50 | char *netmaskresult; | |
51 | char *dhcphostnameresult; | |
52 | struct newtExitStruct es; | |
53 | newtComponent header; | |
54 | newtComponent addresslabel; | |
55 | newtComponent netmasklabel; | |
56 | newtComponent dhcphostnamelabel; | |
57 | newtComponent ok, cancel; | |
58 | char message[1000]; | |
59 | char temp[STRING_SIZE]; | |
60 | char addressfield[STRING_SIZE]; | |
61 | char netmaskfield[STRING_SIZE]; | |
62 | char typefield[STRING_SIZE]; | |
63 | char dhcphostnamefield[STRING_SIZE]; | |
64 | int error; | |
65 | int result = 0; | |
66 | char type[STRING_SIZE]; | |
67 | int startstatictype = 0; | |
68 | int startdhcptype = 0; | |
69 | int startpppoetype = 0; | |
9607771a MT |
70 | |
71 | /* Build some key strings. */ | |
72 | sprintf(addressfield, "%s_ADDRESS", colour); | |
73 | sprintf(netmaskfield, "%s_NETMASK", colour); | |
74 | sprintf(typefield, "%s_TYPE", colour); | |
75 | sprintf(dhcphostnamefield, "%s_DHCP_HOSTNAME", colour); | |
76 | ||
77 | sprintf(message, ctr[TR_INTERFACE], colour); | |
78 | newtCenteredWindow(44, (typeflag ? 18 : 12), message); | |
79 | ||
80 | networkform = newtForm(NULL, NULL, 0); | |
81 | ||
82 | sprintf(message, ctr[TR_ENTER_THE_IP_ADDRESS_INFORMATION], colour); | |
83 | header = newtTextboxReflowed(1, 1, message, 42, 0, 0, 0); | |
84 | newtFormAddComponent(networkform, header); | |
85 | ||
86 | /* See if we need a dhcp checkbox. If we do, then we shift the contents | |
87 | * of the window down two rows to make room. */ | |
88 | if (typeflag) | |
89 | { | |
90 | strcpy(temp, "STATIC"); findkey(kv, typefield, temp); | |
91 | if (strcmp(temp, "STATIC") == 0) startstatictype = 1; | |
92 | if (strcmp(temp, "DHCP") == 0) startdhcptype = 1; | |
93 | if (strcmp(temp, "PPPOE") == 0) startpppoetype = 1; | |
9607771a MT |
94 | statictyperadio = newtRadiobutton(2, 4, ctr[TR_STATIC], startstatictype, NULL); |
95 | dhcptyperadio = newtRadiobutton(2, 5, "DHCP", startdhcptype, statictyperadio); | |
96 | pppoetyperadio = newtRadiobutton(2, 6, "PPPOE", startpppoetype, dhcptyperadio); | |
9607771a | 97 | newtFormAddComponents(networkform, statictyperadio, dhcptyperadio, |
0e42072a | 98 | pppoetyperadio, NULL); |
9607771a MT |
99 | newtComponentAddCallback(statictyperadio, networkdialogcallbacktype, NULL); |
100 | newtComponentAddCallback(dhcptyperadio, networkdialogcallbacktype, NULL); | |
101 | newtComponentAddCallback(pppoetyperadio, networkdialogcallbacktype, NULL); | |
9607771a MT |
102 | dhcphostnamelabel = newtTextbox(2, 9, 18, 1, 0); |
103 | newtTextboxSetText(dhcphostnamelabel, ctr[TR_DHCP_HOSTNAME]); | |
104 | strcpy(temp, defaultdhcphostname); | |
105 | findkey(kv, dhcphostnamefield, temp); | |
106 | dhcphostnameentry = newtEntry(20, 9, temp, 20, &dhcphostnameresult, 0); | |
107 | newtFormAddComponent(networkform, dhcphostnamelabel); | |
108 | newtFormAddComponent(networkform, dhcphostnameentry); | |
109 | if (startdhcptype == 0) | |
110 | newtEntrySetFlags(dhcphostnameentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET); | |
111 | } | |
112 | /* Address */ | |
113 | addresslabel = newtTextbox(2, (typeflag ? 11 : 4) + 0, 18, 1, 0); | |
114 | newtTextboxSetText(addresslabel, ctr[TR_IP_ADDRESS_PROMPT]); | |
115 | strcpy(temp, ""); | |
116 | findkey(kv, addressfield, temp); | |
117 | addressentry = newtEntry(20, (typeflag ? 11 : 4) + 0, temp, 20, &addressresult, 0); | |
118 | newtEntrySetFilter(addressentry, ip_input_filter, NULL); | |
0e42072a | 119 | if (typeflag == 1 && startstatictype == 0) |
9607771a MT |
120 | newtEntrySetFlags(addressentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET); |
121 | newtFormAddComponent(networkform, addresslabel); | |
122 | newtFormAddComponent(networkform, addressentry); | |
123 | ||
124 | /* Netmask */ | |
125 | netmasklabel = newtTextbox(2, (typeflag ? 11 : 4) + 1, 18, 1, 0); | |
126 | newtTextboxSetText(netmasklabel, ctr[TR_NETMASK_PROMPT]); | |
127 | strcpy(temp, "255.255.255.0"); findkey(kv, netmaskfield, temp); | |
128 | netmaskentry = newtEntry(20, (typeflag ? 11 : 4) + 1, temp, 20, &netmaskresult, 0); | |
129 | newtEntrySetFilter(netmaskentry, ip_input_filter, NULL); | |
0e42072a | 130 | if (typeflag == 1 && startstatictype == 0) |
9607771a MT |
131 | newtEntrySetFlags(netmaskentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET); |
132 | ||
133 | newtFormAddComponent(networkform, netmasklabel); | |
134 | newtFormAddComponent(networkform, netmaskentry); | |
135 | ||
136 | /* Buttons. */ | |
137 | ok = newtButton(8, (typeflag ? 14 : 7), ctr[TR_OK]); | |
138 | cancel = newtButton(26, (typeflag ? 14 : 7), ctr[TR_CANCEL]); | |
139 | ||
140 | newtFormAddComponents(networkform, ok, cancel, NULL); | |
141 | ||
142 | newtRefresh(); | |
143 | newtDrawForm(networkform); | |
144 | ||
145 | do | |
146 | { | |
147 | error = 0; | |
148 | newtFormRun(networkform, &es); | |
149 | ||
150 | if (es.u.co == ok) | |
151 | { | |
152 | /* OK was pressed; verify the contents of each entry. */ | |
153 | strcpy(message, ctr[TR_INVALID_FIELDS]); | |
154 | ||
155 | strcpy(type, "STATIC"); | |
156 | if (typeflag) | |
157 | gettype(type); | |
0e42072a | 158 | if (strcmp(type, "STATIC") == 0) |
9607771a MT |
159 | { |
160 | if (inet_addr(addressresult) == INADDR_NONE) | |
161 | { | |
162 | strcat(message, ctr[TR_IP_ADDRESS_CR]); | |
163 | error = 1; | |
164 | } | |
165 | if (inet_addr(netmaskresult) == INADDR_NONE) | |
166 | { | |
167 | strcat(message, ctr[TR_NETWORK_MASK_CR]); | |
168 | error = 1; | |
169 | } | |
170 | } | |
171 | if (strcmp(type, "DHCP") == 0) | |
172 | { | |
173 | if (!strlen(dhcphostnameresult)) | |
174 | { | |
175 | strcat(message, ctr[TR_DHCP_HOSTNAME_CR]); | |
176 | error = 1; | |
177 | } | |
178 | } | |
179 | if (error) | |
180 | errorbox(message); | |
181 | else | |
182 | { | |
183 | /* No errors! Set new values, depending on dhcp flag etc. */ | |
184 | if (typeflag) | |
185 | { | |
186 | replacekeyvalue(kv, dhcphostnamefield, dhcphostnameresult); | |
0e42072a | 187 | if (strcmp(type, "STATIC") != 0) |
9607771a MT |
188 | { |
189 | replacekeyvalue(kv, addressfield, "0.0.0.0"); | |
190 | replacekeyvalue(kv, netmaskfield, "0.0.0.0"); | |
191 | } | |
192 | else | |
193 | { | |
194 | replacekeyvalue(kv, addressfield, addressresult); | |
195 | replacekeyvalue(kv, netmaskfield, netmaskresult); | |
196 | } | |
197 | replacekeyvalue(kv, typefield, type); | |
198 | } | |
199 | else | |
200 | { | |
201 | replacekeyvalue(kv, addressfield, addressresult); | |
202 | replacekeyvalue(kv, netmaskfield, netmaskresult); | |
203 | } | |
204 | ||
205 | setnetaddress(kv, colour); | |
206 | result = 1; | |
207 | } | |
208 | } | |
209 | } | |
210 | while (error); | |
211 | ||
212 | newtFormDestroy(networkform); | |
213 | newtPopWindow(); | |
214 | ||
215 | return result; | |
216 | } | |
217 | ||
218 | /* for pppoe: return string thats type STATIC, DHCP or PPPOE */ | |
219 | int gettype(char *type) | |
220 | { | |
221 | newtComponent selected = newtRadioGetCurrent(statictyperadio); | |
222 | ||
223 | if (selected == statictyperadio) | |
224 | strcpy(type, "STATIC"); | |
225 | else if (selected == dhcptyperadio) | |
226 | strcpy(type, "DHCP"); | |
227 | else if (selected == pppoetyperadio) | |
228 | strcpy(type, "PPPOE"); | |
9607771a MT |
229 | else |
230 | strcpy(type, "ERROR"); | |
231 | ||
232 | return 0; | |
233 | } | |
234 | ||
235 | /* 0.9.9: calculates broadcast too. */ | |
236 | int setnetaddress(struct keyvalue *kv, char *colour) | |
237 | { | |
238 | char addressfield[STRING_SIZE]; | |
239 | char netaddressfield[STRING_SIZE]; | |
240 | char netmaskfield[STRING_SIZE]; | |
241 | char broadcastfield[STRING_SIZE]; | |
242 | char address[STRING_SIZE]; | |
243 | char netmask[STRING_SIZE]; | |
244 | unsigned long int intaddress; | |
245 | unsigned long int intnetaddress; | |
246 | unsigned long int intnetmask; | |
247 | unsigned long int intbroadcast; | |
248 | struct in_addr temp; | |
249 | char *netaddress; | |
250 | char *broadcast; | |
251 | ||
252 | /* Build some key strings. */ | |
253 | sprintf(addressfield, "%s_ADDRESS", colour); | |
254 | sprintf(netaddressfield, "%s_NETADDRESS", colour); | |
255 | sprintf(netmaskfield, "%s_NETMASK", colour); | |
256 | sprintf(broadcastfield, "%s_BROADCAST", colour); | |
257 | ||
258 | strcpy(address, ""); findkey(kv, addressfield, address); | |
259 | strcpy(netmask, ""); findkey(kv, netmaskfield, netmask); | |
260 | ||
261 | /* Calculate netaddress. Messy.. */ | |
262 | intaddress = inet_addr(address); | |
263 | intnetmask = inet_addr(netmask); | |
264 | ||
265 | intnetaddress = intaddress & intnetmask; | |
266 | temp.s_addr = intnetaddress; | |
267 | netaddress = inet_ntoa(temp); | |
268 | ||
269 | replacekeyvalue(kv, netaddressfield, netaddress); | |
270 | ||
271 | intbroadcast = intnetaddress | ~intnetmask; | |
272 | temp.s_addr = intbroadcast; | |
273 | broadcast = inet_ntoa(temp); | |
274 | ||
275 | replacekeyvalue(kv, broadcastfield, broadcast); | |
276 | ||
277 | return 1; | |
278 | } | |
279 | ||
280 | /* Called when dhcp flag is toggled. Toggle disabled state of other 3 | |
281 | * controls. */ | |
282 | void networkdialogcallbacktype(newtComponent cm, void *data) | |
283 | { | |
284 | char type[STRING_SIZE]; | |
285 | ||
286 | gettype(type); | |
287 | ||
0e42072a | 288 | if (strcmp(type, "STATIC") != 0) |
9607771a MT |
289 | { |
290 | newtEntrySetFlags(addressentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET); | |
291 | newtEntrySetFlags(netmaskentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET); | |
292 | } | |
293 | else | |
294 | { | |
295 | newtEntrySetFlags(addressentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_RESET); | |
296 | newtEntrySetFlags(netmaskentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_RESET); | |
297 | } | |
298 | if (strcmp(type, "DHCP") == 0) | |
299 | newtEntrySetFlags(dhcphostnameentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_RESET); | |
300 | else | |
301 | newtEntrySetFlags(dhcphostnameentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET); | |
302 | ||
303 | newtRefresh(); | |
304 | newtDrawForm(networkform); | |
305 | } | |
306 | ||
307 | int interfacecheck(struct keyvalue *kv, char *colour) | |
308 | { | |
309 | char temp[STRING_SIZE]; | |
310 | char colourfields[NETCHANGE_TOTAL][STRING_SIZE]; | |
311 | int c; | |
312 | ||
313 | sprintf(colourfields[ADDRESS], "%s_ADDRESS", colour); | |
314 | sprintf(colourfields[NETADDRESS], "%s_NETADDRESS", colour); | |
315 | sprintf(colourfields[NETMASK], "%s_NETMASK", colour); | |
316 | ||
317 | for (c = 0; c < 3; c++) | |
318 | { | |
319 | strcpy(temp, ""); findkey(kv, colourfields[c], temp); | |
320 | if (!(strlen(temp))) return 0; | |
321 | } | |
322 | return 1; | |
323 | } | |
9607771a MT |
324 | |
325 | /* Funky routine for loading all drivers (cept those are already loaded.). */ | |
f9cc0d70 | 326 | int probecards(char *driver, char *driveroptions ) |
9607771a | 327 | { |
75ae2191 MT |
328 | return 0; |
329 | } | |
b4e381a8 | 330 | |
9c1c1c57 HS |
331 | int get_knic(int card) //returns "0" for zero cards or error and "1" card is found. |
332 | { | |
333 | struct keyvalue *kv = initkeyvalues(); | |
334 | char temp[STRING_SIZE], searchstr[STRING_SIZE]; | |
335 | int ret_value; | |
336 | ||
337 | if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings"))) | |
338 | { | |
339 | freekeyvalues(kv); | |
340 | errorbox(ctr[TR_UNABLE_TO_OPEN_SETTINGS_FILE]); | |
341 | return 0; | |
342 | } | |
343 | ||
344 | sprintf(searchstr, "%s_MACADDR", ucolourcard[card]); | |
345 | strcpy(temp, ""); findkey(kv, searchstr, temp); | |
346 | if (strlen(temp)) { | |
347 | strcpy(knics[ card ].macaddr, temp); | |
348 | strcpy(knics[ card ].colour, "GREEN"); | |
349 | ||
350 | sprintf(searchstr, "%s_DESCRIPTION", ucolourcard[card]); | |
351 | findkey(kv, searchstr, temp); | |
352 | strcpy(knics[ card ].description, temp); | |
353 | ||
354 | sprintf(searchstr, "%s_DRIVER", ucolourcard[card]); | |
355 | findkey(kv, searchstr, temp); | |
356 | strcpy(knics[ card ].driver, temp); | |
357 | ret_value = 1; | |
358 | } else { | |
359 | strcpy(knics[ card ].description, ctr[TR_UNSET]); | |
360 | ret_value = 0; | |
361 | } | |
362 | ||
363 | return ret_value; | |
364 | } | |
365 | ||
366 | int init_knics(void) | |
367 | { | |
368 | int found = 0; | |
369 | found += get_knic(_GREEN_CARD_); | |
370 | found += get_knic(_RED_CARD_); | |
371 | found += get_knic(_ORANGE_CARD_); | |
372 | found += get_knic(_BLUE_CARD_); | |
373 | ||
374 | return found; | |
375 | } | |
84e975ba | 376 | |
bba7212c | 377 | int fmt_exists(const char *fname) { /* Check if it is any file or directory */ |
eea0467a HS |
378 | struct stat st; |
379 | if (stat(fname, &st) == -1) return 0; | |
380 | else return 1; | |
381 | } | |
382 | ||
bba7212c | 383 | int is_interface_up(char *card) { /* Check if the interface is UP */ |
eea0467a HS |
384 | char temp[STRING_SIZE]; |
385 | ||
386 | sprintf(temp,"ip link show dev %s | grep -q UP", card); | |
387 | if (mysystem(temp)) return 0; else return 1; | |
388 | } | |
389 | ||
390 | int rename_device(char *old_name, char *new_name) { | |
391 | char temp[STRING_SIZE]; | |
392 | ||
393 | sprintf(temp,SYSDIR "/%s", old_name); | |
394 | if (!(fmt_exists(temp))) { | |
395 | fprintf(flog,"Device not found: %s\n",old_name); | |
396 | return 0; | |
397 | } | |
eea0467a HS |
398 | sprintf(temp,"/sbin/ip link set dev %s name %s",old_name ,new_name ); |
399 | mysystem(temp); | |
400 | ||
401 | return 1; | |
402 | } | |
403 | ||
404 | char g_temp[STRING_SIZE]=""; | |
405 | char* readmac(char *card) { | |
eea0467a HS |
406 | FILE *fp; |
407 | char temp[STRING_SIZE], mac[20]; | |
408 | ||
409 | sprintf(temp,"/sys/class/net/%s/address",card); | |
410 | if( (fp = fopen(temp, "r")) == NULL ) { | |
411 | fprintf(flog,"Couldn't open: %s\n",temp); | |
412 | return NULL; | |
413 | } | |
414 | fgets(mac, 18, fp); | |
415 | strtok(mac,"\n"); | |
416 | fclose(fp); | |
417 | strcpy(g_temp, mac); | |
418 | return g_temp; | |
419 | } | |
420 | ||
421 | char* find_nic4mac(char *findmac) { | |
eea0467a HS |
422 | DIR *dir; |
423 | struct dirent *dirzeiger; | |
424 | char temp[STRING_SIZE], temp2[STRING_SIZE]; | |
425 | ||
426 | if((dir=opendir(SYSDIR)) == NULL) { | |
427 | fprintf(flog,"Fehler bei opendir (find_name4nic) ...\n"); | |
428 | return NULL; | |
429 | } | |
430 | ||
431 | sprintf(temp, ""); | |
432 | while((dirzeiger=readdir(dir)) != NULL) { | |
433 | if(*((*dirzeiger).d_name) != '.' & strcmp(((*dirzeiger).d_name), "lo") != 0) { | |
434 | sprintf(temp2, "%s", readmac((*dirzeiger).d_name) ); | |
435 | if (strcmp(findmac, temp2) == 0) { | |
436 | sprintf(temp,"%s", (*dirzeiger).d_name); | |
eea0467a HS |
437 | break; |
438 | } | |
439 | } | |
440 | } | |
441 | ||
442 | if(closedir(dir) == -1) fprintf(flog,"Fehler beim schliessen von %s\n", SYSDIR); | |
443 | strcpy(g_temp, temp); | |
444 | return g_temp; | |
445 | } | |
446 | ||
447 | int nic_shutdown(char *nic) { | |
448 | char temp[STRING_SIZE]; | |
449 | ||
450 | sprintf(temp,"ip link set %s down", nic); | |
451 | mysystem(temp); | |
452 | } | |
453 | ||
454 | int nic_startup(char *nic) { | |
455 | char temp[STRING_SIZE]; | |
456 | ||
457 | sprintf(temp,"ip link set %s up", nic); | |
458 | mysystem(temp); | |
459 | ||
460 | } | |
461 | ||
462 | int rename_nics(void) { | |
463 | int i, j, k; | |
464 | int fnics = scan_network_cards(); | |
465 | char nic2find[STRING_SIZE], temp[STRING_SIZE]; | |
466 | ||
eea0467a | 467 | for(i=0; i<4; i++) |
bba7212c | 468 | if (strcmp(knics[i].macaddr, "")) |
eea0467a | 469 | for(j=0; j<fnics; j++) |
bba7212c | 470 | if(strcmp(knics[i].macaddr, nics[j].macaddr) == 0) { |
eea0467a | 471 | sprintf(nic2find,"%s0",lcolourcard[i]); |
bba7212c MT |
472 | if(strcmp(nic2find, nics[j].nic)) { |
473 | if(is_interface_up(nics[j].nic)) { | |
eea0467a HS |
474 | nic_shutdown(nics[j].nic); |
475 | } | |
476 | sprintf(temp,SYSDIR "/%s", nic2find); | |
bba7212c MT |
477 | if(fmt_exists(temp)) { |
478 | for(k=0; k<fnics; k++) | |
eea0467a | 479 | if (strcmp(nics[k].nic, nic2find) == 0 ) { |
bba7212c | 480 | if(is_interface_up(nics[k].nic)) { |
eea0467a HS |
481 | nic_shutdown(nics[k].nic); |
482 | } | |
bba7212c | 483 | sprintf(temp,"dummy%i",k); |
eea0467a HS |
484 | if (rename_device(nics[k].nic, temp)) strcpy(nics[k].nic, temp); |
485 | } | |
486 | } | |
bba7212c | 487 | if (rename_device(nics[j].nic, nic2find)) strcpy(nics[j].nic, nic2find); |
eea0467a HS |
488 | } |
489 | } | |
490 | } | |
491 | ||
84e975ba HS |
492 | int create_udev(void) |
493 | { | |
75ae2191 | 494 | #define UDEV_NET_CONF "/etc/udev/rules.d/30-persistent-network.rules" |
75ae2191 | 495 | FILE *fp; |
84e975ba HS |
496 | int i; |
497 | ||
84e975ba HS |
498 | if ( (fp = fopen(UDEV_NET_CONF, "w")) == NULL ) { |
499 | fprintf(stderr,"Couldn't open" UDEV_NET_CONF); | |
500 | return 1; | |
501 | } | |
502 | ||
503 | for (i = 0 ; i < 4 ; i++) | |
504 | { | |
505 | if (strcmp(knics[i].macaddr, "")) { | |
506 | fprintf(fp,"ACTION==\"add\", SUBSYSTEM==\"net\", SYSFS{address}==\"%s\", NAME=\"%s0\" # %s\n", knics[i].macaddr, lcolourcard[i], knics[i].description); | |
84e975ba HS |
507 | } |
508 | } | |
509 | fclose(fp); | |
510 | return 0; | |
511 | } | |
512 | ||
513 | int write_configs_netudev(int card , int colour) | |
514 | { | |
75ae2191 MT |
515 | char commandstring[STRING_SIZE]; |
516 | struct keyvalue *kv = initkeyvalues(); | |
517 | char temp1[STRING_SIZE], temp2[STRING_SIZE], temp3[STRING_SIZE]; | |
518 | char ucolour[STRING_SIZE]; | |
5057b611 | 519 | |
9b040aa0 | 520 | sprintf(ucolour, ucolourcard[colour]); |
f9cc0d70 HS |
521 | strcpy(knics[colour].driver, nics[card].driver); |
522 | strcpy(knics[colour].description, nics[card].description); | |
523 | strcpy(knics[colour].macaddr, nics[card].macaddr); | |
9b040aa0 | 524 | |
75ae2191 | 525 | if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings"))) |
b4e381a8 | 526 | { |
75ae2191 MT |
527 | freekeyvalues(kv); |
528 | errorbox(ctr[TR_UNABLE_TO_OPEN_SETTINGS_FILE]); | |
529 | return 0; | |
b4e381a8 | 530 | } |
9607771a | 531 | |
75ae2191 MT |
532 | sprintf(temp1, "%s_DEV", ucolour); |
533 | sprintf(temp2, "%s_MACADDR", ucolour); | |
9b040aa0 | 534 | sprintf(temp3, "%s0", lcolourcard[colour]); |
75ae2191 | 535 | replacekeyvalue(kv, temp1, temp3); |
f9cc0d70 | 536 | replacekeyvalue(kv, temp2, nics[card].macaddr); |
5057b611 | 537 | sprintf(temp1, "%s_DESCRIPTION", ucolour); |
f9cc0d70 HS |
538 | replacekeyvalue(kv, temp1, nics[card].description); |
539 | sprintf(temp1, "%s_DRIVER", ucolour); | |
540 | replacekeyvalue(kv, temp1, nics[card].driver); | |
75ae2191 MT |
541 | |
542 | writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings"); | |
543 | freekeyvalues(kv); | |
9607771a | 544 | |
75ae2191 MT |
545 | return 0; |
546 | } | |
547 | ||
5057b611 | 548 | int scan_network_cards(void) |
75ae2191 | 549 | { |
5057b611 | 550 | FILE *fp; |
f9cc0d70 | 551 | char driver[STRING_SIZE], description[STRING_SIZE], macaddr[STRING_SIZE], temp_line[STRING_SIZE]; |
5057b611 | 552 | int count = 0; |
b04331d3 HS |
553 | const char _driver[]="driver: "; |
554 | const char _desc[]="desc: "; | |
555 | const char _network_hwaddr[]="network.hwaddr: "; | |
5057b611 HS |
556 | |
557 | if (!(scanned_nics_read_done)) | |
558 | { | |
5057b611 | 559 | mysystem("/bin/probenic.sh"); |
5057b611 HS |
560 | if( (fp = fopen(SCANNED_NICS, "r")) == NULL ) |
561 | { | |
562 | fprintf(stderr,"Couldn't open "SCANNED_NICS); | |
563 | return 1; | |
564 | } | |
565 | while (fgets(temp_line, STRING_SIZE, fp) != NULL) | |
566 | { | |
b04331d3 HS |
567 | temp_line[strlen(temp_line) -1] = 0; |
568 | if ( strncmp(temp_line, _driver, strlen(_driver)) == 0 ) sprintf(nics[count].driver, "%s", temp_line+strlen(_driver)); | |
569 | if ( strncmp(temp_line, _desc, strlen(_desc)) == 0 ) sprintf(nics[count].description, "%s", temp_line+strlen(_desc)); | |
570 | if ( strncmp(temp_line, _network_hwaddr, strlen(_network_hwaddr)) == 0 ) sprintf(nics[count].macaddr, "%s", temp_line+strlen(_network_hwaddr)); | |
96ed9998 HS |
571 | if (strlen(nics[count].macaddr) > 15 ) { |
572 | sprintf(nics[count].nic, "%s", find_nic4mac(nics[count].macaddr)); | |
573 | count++; | |
574 | } | |
5057b611 HS |
575 | } |
576 | fclose(fp); | |
9b040aa0 HS |
577 | scanned_nics_read_done = count; |
578 | } else fprintf(flog,"Scan Networkcards does read.\n"); | |
579 | return scanned_nics_read_done; | |
5057b611 | 580 | } |
75ae2191 | 581 | |
5057b611 HS |
582 | |
583 | ||
9b040aa0 | 584 | int nicmenu(int colour) |
5057b611 HS |
585 | { |
586 | int rc, choise = 0, count = 0, kcount = 0, mcount = 0, i, j, nic_in_use; | |
587 | int found_NIC_as_Card[4]; | |
588 | char message[STRING_SIZE]; | |
589 | ||
590 | char cMenuInhalt[STRING_SIZE]; | |
591 | char MenuInhalt[20][180]; | |
592 | char *pMenuInhalt[20]; | |
593 | ||
5057b611 HS |
594 | while (strcmp(nics[count].macaddr, "")) count++; // 2 find how many nics in system |
595 | for ( i=0 ; i<4;i++) if (strcmp(knics[i].macaddr, "")) kcount++; // loop to find all knowing nics | |
5057b611 HS |
596 | |
597 | // If new nics are found... | |
598 | if (count > kcount) { | |
599 | for (i=0 ; i < count ; i++) | |
600 | { | |
601 | nic_in_use = 0; | |
602 | for (j=0 ; j <= kcount ; j++) { | |
603 | if (strcmp(nics[ i ].macaddr, knics[ j ].macaddr) == 0 ) { | |
604 | nic_in_use = 1; | |
5057b611 | 605 | break; |
9607771a | 606 | } |
9607771a | 607 | } |
5057b611 | 608 | if (!(nic_in_use)) { |
5057b611 HS |
609 | if ( strlen(nics[i].description) < 55 ) |
610 | sprintf(MenuInhalt[mcount], "%.*s", strlen(nics[i].description)-2, nics[i].description+1); | |
611 | else { | |
612 | sprintf(cMenuInhalt, "%.50s", nics[i].description + 1); | |
b04331d3 | 613 | sprintf(MenuInhalt[mcount], cMenuInhalt); |
5057b611 | 614 | strcat (MenuInhalt[mcount], "..."); |
75ae2191 | 615 | } |
9607771a | 616 | |
b04331d3 | 617 | while ( strlen(MenuInhalt[mcount]) < 53) strcat(MenuInhalt[mcount], " "); // Fill with space. |
5057b611 HS |
618 | |
619 | strcat(MenuInhalt[mcount], " ("); | |
620 | strcat(MenuInhalt[mcount], nics[i].macaddr); | |
621 | strcat(MenuInhalt[mcount], ")"); | |
622 | pMenuInhalt[mcount] = MenuInhalt[mcount]; | |
623 | found_NIC_as_Card[mcount]=i; | |
624 | mcount++; | |
625 | } | |
75ae2191 | 626 | } |
5057b611 HS |
627 | |
628 | pMenuInhalt[mcount] = NULL; | |
629 | ||
1700769c CS |
630 | sprintf(message, ctr[TR_CHOOSE_NETCARD], ucolourcard[colour]); |
631 | rc = newtWinMenu( ctr[TR_NETCARDMENU2], message, 50, 5, 5, 6, pMenuInhalt, &choise, ctr[TR_OK], ctr[TR_SELECT], ctr[TR_CANCEL], NULL); | |
5057b611 HS |
632 | |
633 | if ( rc == 0 || rc == 1) { | |
f9cc0d70 | 634 | write_configs_netudev(found_NIC_as_Card[choise], colour); |
bba7212c | 635 | } |
5057b611 HS |
636 | return 0; |
637 | } else { | |
638 | // We have to add here that you can manually add a device | |
1700769c | 639 | errorbox( ctr[TR_ERROR_INTERFACES]); |
5057b611 HS |
640 | return 1; |
641 | } | |
642 | } | |
643 | ||
9b040aa0 | 644 | int clear_card_entry(int card) |
5057b611 HS |
645 | { |
646 | struct keyvalue *kv = initkeyvalues(); | |
9b040aa0 | 647 | char temp[STRING_SIZE]; |
5057b611 | 648 | |
5057b611 HS |
649 | if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings"))) |
650 | { | |
651 | freekeyvalues(kv); | |
652 | errorbox(ctr[TR_UNABLE_TO_OPEN_SETTINGS_FILE]); | |
653 | return 0; | |
654 | } | |
655 | ||
f9cc0d70 | 656 | strcpy(knics[card].driver, ""); |
9b040aa0 HS |
657 | strcpy(knics[card].description, ctr[TR_UNSET]); |
658 | strcpy(knics[card].macaddr, ""); | |
659 | strcpy(knics[card].colour, ""); | |
f9cc0d70 HS |
660 | sprintf(temp, "%s_DRIVER", ucolourcard[card]); |
661 | replacekeyvalue(kv, temp, ""); | |
9b040aa0 HS |
662 | sprintf(temp, "%s_DEV", ucolourcard[card]); |
663 | replacekeyvalue(kv, temp, ""); | |
664 | sprintf(temp, "%s_MACADDR", ucolourcard[card]); | |
665 | replacekeyvalue(kv, temp, ""); | |
666 | sprintf(temp, "%s_DESCRIPTION", ucolourcard[card]); | |
667 | replacekeyvalue(kv, temp, ""); | |
668 | ||
669 | writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings"); | |
670 | freekeyvalues(kv); | |
671 | ||
9b040aa0 HS |
672 | return 0; |
673 | } | |
674 | ||
675 | int ask_clear_card_entry(int card) | |
676 | { | |
677 | char message[STRING_SIZE]; | |
678 | int rc; | |
5057b611 | 679 | |
bba7212c | 680 | sprintf(message, ctr[TR_REMOVE_CARD], ucolourcard[card]); |
5057b611 HS |
681 | rc = newtWinChoice(ctr[TR_WARNING], ctr[TR_OK], ctr[TR_CANCEL], message); |
682 | ||
683 | if ( rc = 0 || rc == 1) { | |
9b040aa0 | 684 | clear_card_entry(card); |
5057b611 HS |
685 | } else return 1; |
686 | ||
5057b611 | 687 | return 0; |
9607771a MT |
688 | } |
689 | ||
690 | /* Manual entry for gurus. */ | |
691 | int manualdriver(char *driver, char *driveroptions) | |
692 | { | |
693 | char *values[] = { NULL, NULL }; /* pointers for the values. */ | |
694 | struct newtWinEntry entries[] = | |
695 | { { "", &values[0], 0,}, { NULL, NULL, 0 } }; | |
696 | int rc; | |
697 | char commandstring[STRING_SIZE]; | |
698 | char *driverend; | |
699 | ||
700 | strcpy(driver, ""); | |
701 | strcpy(driveroptions, ""); | |
702 | ||
703 | rc = newtWinEntries(ctr[TR_SELECT_NETWORK_DRIVER], | |
704 | ctr[TR_MODULE_PARAMETERS], 50, 5, 5, 40, entries, | |
705 | ctr[TR_OK], ctr[TR_CANCEL], NULL); | |
706 | if (rc == 0 || rc == 1) | |
707 | { | |
708 | if (strlen(values[0])) | |
709 | { | |
710 | sprintf(commandstring, "/sbin/modprobe %s", values[0]); | |
711 | if (runcommandwithstatus(commandstring, ctr[TR_LOADING_MODULE]) == 0) | |
712 | { | |
713 | if ((driverend = strchr(values[0], ' '))) | |
714 | { | |
715 | *driverend = '\0'; | |
716 | strcpy(driver, values[0]); | |
717 | strcpy(driveroptions, driverend + 1); | |
718 | } | |
719 | else | |
720 | { | |
721 | strcpy(driver, values[0]); | |
722 | strcpy(driveroptions, ""); | |
723 | } | |
724 | } | |
725 | else | |
726 | errorbox(ctr[TR_UNABLE_TO_LOAD_DRIVER_MODULE]); | |
727 | } | |
728 | else | |
729 | errorbox(ctr[TR_MODULE_NAME_CANNOT_BE_BLANK]); | |
730 | } | |
731 | free(values[0]); | |
732 | ||
733 | return 1; | |
734 | } |