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