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