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