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