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