]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blame - src/install+setup/libsmooth/netstuff.c
Buildfix
[people/pmueller/ipfire-2.x.git] / src / install+setup / libsmooth / netstuff.c
CommitLineData
9607771a
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 *
9607771a
MT
9 */
10
11#include "libsmooth.h"
12#include <signal.h>
13
14extern FILE *flog;
15extern char *mylog;
16
17extern char **ctr;
18
5057b611
HS
19extern struct nic nics[];
20extern struct knic knics[];
21
9b040aa0
HS
22char *ucolourcard[] = { "GREEN", "RED", "ORANGE", "BLUE", NULL };
23char *lcolourcard[] = { "green", "red", "orange", "blue", NULL };
24
5057b611
HS
25int scanned_nics_read_done = 0;
26
9607771a
MT
27newtComponent networkform;
28newtComponent addressentry;
29newtComponent netmaskentry;
30newtComponent statictyperadio;
31newtComponent dhcptyperadio;
32newtComponent pppoetyperadio;
33newtComponent pptptyperadio;
34newtComponent dhcphostnameentry;
35
36/* acceptable character filter for IP and netmaks entry boxes */
37static 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. */
47int changeaddress(struct keyvalue *kv, char *colour, int typeflag,
48 char *defaultdhcphostname)
49{
50 char *addressresult;
51 char *netmaskresult;
52 char *dhcphostnameresult;
53 struct newtExitStruct es;
54 newtComponent header;
55 newtComponent addresslabel;
56 newtComponent netmasklabel;
57 newtComponent dhcphostnamelabel;
58 newtComponent ok, cancel;
59 char message[1000];
60 char temp[STRING_SIZE];
61 char addressfield[STRING_SIZE];
62 char netmaskfield[STRING_SIZE];
63 char typefield[STRING_SIZE];
64 char dhcphostnamefield[STRING_SIZE];
65 int error;
66 int result = 0;
67 char type[STRING_SIZE];
68 int startstatictype = 0;
69 int startdhcptype = 0;
70 int startpppoetype = 0;
71 int startpptptype = 0;
72
73 /* Build some key strings. */
74 sprintf(addressfield, "%s_ADDRESS", colour);
75 sprintf(netmaskfield, "%s_NETMASK", colour);
76 sprintf(typefield, "%s_TYPE", colour);
77 sprintf(dhcphostnamefield, "%s_DHCP_HOSTNAME", colour);
78
79 sprintf(message, ctr[TR_INTERFACE], colour);
80 newtCenteredWindow(44, (typeflag ? 18 : 12), message);
81
82 networkform = newtForm(NULL, NULL, 0);
83
84 sprintf(message, ctr[TR_ENTER_THE_IP_ADDRESS_INFORMATION], colour);
85 header = newtTextboxReflowed(1, 1, message, 42, 0, 0, 0);
86 newtFormAddComponent(networkform, header);
87
88 /* See if we need a dhcp checkbox. If we do, then we shift the contents
89 * of the window down two rows to make room. */
90 if (typeflag)
91 {
92 strcpy(temp, "STATIC"); findkey(kv, typefield, temp);
93 if (strcmp(temp, "STATIC") == 0) startstatictype = 1;
94 if (strcmp(temp, "DHCP") == 0) startdhcptype = 1;
95 if (strcmp(temp, "PPPOE") == 0) startpppoetype = 1;
96 if (strcmp(temp, "PPTP") == 0) startpptptype = 1;
97 statictyperadio = newtRadiobutton(2, 4, ctr[TR_STATIC], startstatictype, NULL);
98 dhcptyperadio = newtRadiobutton(2, 5, "DHCP", startdhcptype, statictyperadio);
99 pppoetyperadio = newtRadiobutton(2, 6, "PPPOE", startpppoetype, dhcptyperadio);
100 pptptyperadio = newtRadiobutton(2, 7, "PPTP", startpptptype, pppoetyperadio);
101 newtFormAddComponents(networkform, statictyperadio, dhcptyperadio,
102 pppoetyperadio, pptptyperadio, NULL);
103 newtComponentAddCallback(statictyperadio, networkdialogcallbacktype, NULL);
104 newtComponentAddCallback(dhcptyperadio, networkdialogcallbacktype, NULL);
105 newtComponentAddCallback(pppoetyperadio, networkdialogcallbacktype, NULL);
106 newtComponentAddCallback(pptptyperadio, networkdialogcallbacktype, NULL);
107 dhcphostnamelabel = newtTextbox(2, 9, 18, 1, 0);
108 newtTextboxSetText(dhcphostnamelabel, ctr[TR_DHCP_HOSTNAME]);
109 strcpy(temp, defaultdhcphostname);
110 findkey(kv, dhcphostnamefield, temp);
111 dhcphostnameentry = newtEntry(20, 9, temp, 20, &dhcphostnameresult, 0);
112 newtFormAddComponent(networkform, dhcphostnamelabel);
113 newtFormAddComponent(networkform, dhcphostnameentry);
114 if (startdhcptype == 0)
115 newtEntrySetFlags(dhcphostnameentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET);
116 }
117 /* Address */
118 addresslabel = newtTextbox(2, (typeflag ? 11 : 4) + 0, 18, 1, 0);
119 newtTextboxSetText(addresslabel, ctr[TR_IP_ADDRESS_PROMPT]);
120 strcpy(temp, "");
121 findkey(kv, addressfield, temp);
122 addressentry = newtEntry(20, (typeflag ? 11 : 4) + 0, temp, 20, &addressresult, 0);
123 newtEntrySetFilter(addressentry, ip_input_filter, NULL);
124 if (typeflag == 1 && startstatictype == 0 && startpptptype == 0 )
125 newtEntrySetFlags(addressentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET);
126 newtFormAddComponent(networkform, addresslabel);
127 newtFormAddComponent(networkform, addressentry);
128
129 /* Netmask */
130 netmasklabel = newtTextbox(2, (typeflag ? 11 : 4) + 1, 18, 1, 0);
131 newtTextboxSetText(netmasklabel, ctr[TR_NETMASK_PROMPT]);
132 strcpy(temp, "255.255.255.0"); findkey(kv, netmaskfield, temp);
133 netmaskentry = newtEntry(20, (typeflag ? 11 : 4) + 1, temp, 20, &netmaskresult, 0);
134 newtEntrySetFilter(netmaskentry, ip_input_filter, NULL);
135 if (typeflag == 1 && startstatictype == 0 && startpptptype == 0 )
136 newtEntrySetFlags(netmaskentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET);
137
138 newtFormAddComponent(networkform, netmasklabel);
139 newtFormAddComponent(networkform, netmaskentry);
140
141 /* Buttons. */
142 ok = newtButton(8, (typeflag ? 14 : 7), ctr[TR_OK]);
143 cancel = newtButton(26, (typeflag ? 14 : 7), ctr[TR_CANCEL]);
144
145 newtFormAddComponents(networkform, ok, cancel, NULL);
146
147 newtRefresh();
148 newtDrawForm(networkform);
149
150 do
151 {
152 error = 0;
153 newtFormRun(networkform, &es);
154
155 if (es.u.co == ok)
156 {
157 /* OK was pressed; verify the contents of each entry. */
158 strcpy(message, ctr[TR_INVALID_FIELDS]);
159
160 strcpy(type, "STATIC");
161 if (typeflag)
162 gettype(type);
163 if (strcmp(type, "STATIC") == 0 || strcmp(type, "PPTP") == 0 )
164 {
165 if (inet_addr(addressresult) == INADDR_NONE)
166 {
167 strcat(message, ctr[TR_IP_ADDRESS_CR]);
168 error = 1;
169 }
170 if (inet_addr(netmaskresult) == INADDR_NONE)
171 {
172 strcat(message, ctr[TR_NETWORK_MASK_CR]);
173 error = 1;
174 }
175 }
176 if (strcmp(type, "DHCP") == 0)
177 {
178 if (!strlen(dhcphostnameresult))
179 {
180 strcat(message, ctr[TR_DHCP_HOSTNAME_CR]);
181 error = 1;
182 }
183 }
184 if (error)
185 errorbox(message);
186 else
187 {
188 /* No errors! Set new values, depending on dhcp flag etc. */
189 if (typeflag)
190 {
191 replacekeyvalue(kv, dhcphostnamefield, dhcphostnameresult);
192 if (strcmp(type, "STATIC") != 0 && strcmp(type, "PPTP") != 0)
193 {
194 replacekeyvalue(kv, addressfield, "0.0.0.0");
195 replacekeyvalue(kv, netmaskfield, "0.0.0.0");
196 }
197 else
198 {
199 replacekeyvalue(kv, addressfield, addressresult);
200 replacekeyvalue(kv, netmaskfield, netmaskresult);
201 }
202 replacekeyvalue(kv, typefield, type);
203 }
204 else
205 {
206 replacekeyvalue(kv, addressfield, addressresult);
207 replacekeyvalue(kv, netmaskfield, netmaskresult);
208 }
209
210 setnetaddress(kv, colour);
211 result = 1;
212 }
213 }
214 }
215 while (error);
216
217 newtFormDestroy(networkform);
218 newtPopWindow();
219
220 return result;
221}
222
223/* for pppoe: return string thats type STATIC, DHCP or PPPOE */
224int gettype(char *type)
225{
226 newtComponent selected = newtRadioGetCurrent(statictyperadio);
227
228 if (selected == statictyperadio)
229 strcpy(type, "STATIC");
230 else if (selected == dhcptyperadio)
231 strcpy(type, "DHCP");
232 else if (selected == pppoetyperadio)
233 strcpy(type, "PPPOE");
234 else if (selected == pptptyperadio)
235 strcpy(type, "PPTP");
236 else
237 strcpy(type, "ERROR");
238
239 return 0;
240}
241
242/* 0.9.9: calculates broadcast too. */
243int setnetaddress(struct keyvalue *kv, char *colour)
244{
245 char addressfield[STRING_SIZE];
246 char netaddressfield[STRING_SIZE];
247 char netmaskfield[STRING_SIZE];
248 char broadcastfield[STRING_SIZE];
249 char address[STRING_SIZE];
250 char netmask[STRING_SIZE];
251 unsigned long int intaddress;
252 unsigned long int intnetaddress;
253 unsigned long int intnetmask;
254 unsigned long int intbroadcast;
255 struct in_addr temp;
256 char *netaddress;
257 char *broadcast;
258
259 /* Build some key strings. */
260 sprintf(addressfield, "%s_ADDRESS", colour);
261 sprintf(netaddressfield, "%s_NETADDRESS", colour);
262 sprintf(netmaskfield, "%s_NETMASK", colour);
263 sprintf(broadcastfield, "%s_BROADCAST", colour);
264
265 strcpy(address, ""); findkey(kv, addressfield, address);
266 strcpy(netmask, ""); findkey(kv, netmaskfield, netmask);
267
268 /* Calculate netaddress. Messy.. */
269 intaddress = inet_addr(address);
270 intnetmask = inet_addr(netmask);
271
272 intnetaddress = intaddress & intnetmask;
273 temp.s_addr = intnetaddress;
274 netaddress = inet_ntoa(temp);
275
276 replacekeyvalue(kv, netaddressfield, netaddress);
277
278 intbroadcast = intnetaddress | ~intnetmask;
279 temp.s_addr = intbroadcast;
280 broadcast = inet_ntoa(temp);
281
282 replacekeyvalue(kv, broadcastfield, broadcast);
283
284 return 1;
285}
286
287/* Called when dhcp flag is toggled. Toggle disabled state of other 3
288 * controls. */
289void networkdialogcallbacktype(newtComponent cm, void *data)
290{
291 char type[STRING_SIZE];
292
293 gettype(type);
294
295 if (strcmp(type, "STATIC") != 0 && strcmp(type, "PPTP") != 0 )
296 {
297 newtEntrySetFlags(addressentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET);
298 newtEntrySetFlags(netmaskentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET);
299 }
300 else
301 {
302 newtEntrySetFlags(addressentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_RESET);
303 newtEntrySetFlags(netmaskentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_RESET);
304 }
305 if (strcmp(type, "DHCP") == 0)
306 newtEntrySetFlags(dhcphostnameentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_RESET);
307 else
308 newtEntrySetFlags(dhcphostnameentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET);
309
310 newtRefresh();
311 newtDrawForm(networkform);
312}
313
314int interfacecheck(struct keyvalue *kv, char *colour)
315{
316 char temp[STRING_SIZE];
317 char colourfields[NETCHANGE_TOTAL][STRING_SIZE];
318 int c;
319
320 sprintf(colourfields[ADDRESS], "%s_ADDRESS", colour);
321 sprintf(colourfields[NETADDRESS], "%s_NETADDRESS", colour);
322 sprintf(colourfields[NETMASK], "%s_NETMASK", colour);
323
324 for (c = 0; c < 3; c++)
325 {
326 strcpy(temp, ""); findkey(kv, colourfields[c], temp);
327 if (!(strlen(temp))) return 0;
328 }
329 return 1;
330}
9607771a
MT
331
332/* Funky routine for loading all drivers (cept those are already loaded.). */
f9cc0d70 333int probecards(char *driver, char *driveroptions )
9607771a 334{
75ae2191
MT
335 return 0;
336}
b4e381a8 337
5057b611 338/* ### alter strupper ###
75ae2191
MT
339char *strupper(char *s)
340{
341 int n;
342 for (n=0;s[n];n++) s[n]=toupper(s[n]);
343 return s;
344}
5057b611 345*/
b4e381a8 346
5057b611
HS
347/* neuer StringUpper, wird zur Zeit nicht benutzt da UTF-8 nicht geht.
348void strupper(unsigned char *string)
349{
350 unsigned char *str;
351 for (str = string; *str != '\0'; str++)
352 if (!(*str & 0x80) && islower(*str))
353 *str = toupper(*str);
354}
355*/
9607771a 356
b04331d3 357/* int ismacaddr(char *ismac)
d10e55d1 358{
b04331d3
HS
359 char *a;
360 fprintf(flog,"Check is MAC true\n"); // #### Debug ####
361 for (a = ismac; *a; a++) {
362 sprintf(flog,"%c\n", *a); // #### Debug ####
d10e55d1 363 if (*a != ':' && !isxdigit(*a)) return 0; // is int != ':' or not hexdigit then exit
b04331d3 364 }
d10e55d1
HS
365 return 1;
366}
b04331d3 367*/
d10e55d1 368
9c1c1c57
HS
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);
378 errorbox(ctr[TR_UNABLE_TO_OPEN_SETTINGS_FILE]);
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, "GREEN");
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 {
397 strcpy(knics[ card ].description, ctr[TR_UNSET]);
398 ret_value = 0;
399 }
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}
84e975ba 414
eea0467a
HS
415int fmt_exists(const char *fname) { /* Check it's 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 is interface UP */
422 char temp[STRING_SIZE];
423
424 sprintf(temp,"ip link show dev %s | grep -q UP", card);
425 if (mysystem(temp)) return 0; else return 1;
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// fprintf(flog,"NIC: %s wurde in %s umbenannt.\n", old_name, new_name); // #### Debug ####
437 sprintf(temp,"/sbin/ip link set dev %s name %s",old_name ,new_name );
438 mysystem(temp);
439
440 return 1;
441}
442
443char g_temp[STRING_SIZE]="";
444char* readmac(char *card) {
445// fprintf(flog,"Enter readmac... NIC: %s\n", card); // #### Debug ####
446 FILE *fp;
447 char temp[STRING_SIZE], mac[20];
448
449 sprintf(temp,"/sys/class/net/%s/address",card);
450 if( (fp = fopen(temp, "r")) == NULL ) {
451 fprintf(flog,"Couldn't open: %s\n",temp);
452 return NULL;
453 }
454 fgets(mac, 18, fp);
455 strtok(mac,"\n");
456 fclose(fp);
457 strcpy(g_temp, mac);
458 return g_temp;
459}
460
461char* find_nic4mac(char *findmac) {
462 fprintf(flog,"Enter find_name4nic... Search for %s\n", findmac); // #### Debug ####
463
464 DIR *dir;
465 struct dirent *dirzeiger;
466 char temp[STRING_SIZE], temp2[STRING_SIZE];
467
468 if((dir=opendir(SYSDIR)) == NULL) {
469 fprintf(flog,"Fehler bei opendir (find_name4nic) ...\n");
470 return NULL;
471 }
472
473 sprintf(temp, "");
474 while((dirzeiger=readdir(dir)) != NULL) {
475 if(*((*dirzeiger).d_name) != '.' & strcmp(((*dirzeiger).d_name), "lo") != 0) {
476 sprintf(temp2, "%s", readmac((*dirzeiger).d_name) );
477 if (strcmp(findmac, temp2) == 0) {
478 sprintf(temp,"%s", (*dirzeiger).d_name);
479// fprintf(flog,"MAC: %s is NIC: %s\n", findmac, temp); // #### Debug ####
480 break;
481 }
482 }
483 }
484
485 if(closedir(dir) == -1) fprintf(flog,"Fehler beim schliessen von %s\n", SYSDIR);
486 strcpy(g_temp, temp);
487 return g_temp;
488}
489
490int nic_shutdown(char *nic) {
491 char temp[STRING_SIZE];
492
493 sprintf(temp,"ip link set %s down", nic);
494 mysystem(temp);
495}
496
497int nic_startup(char *nic) {
498 char temp[STRING_SIZE];
499
500 sprintf(temp,"ip link set %s up", nic);
501 mysystem(temp);
502
503}
504
505int rename_nics(void) {
506 int i, j, k;
507 int fnics = scan_network_cards();
508 char nic2find[STRING_SIZE], temp[STRING_SIZE];
509
510 fprintf(flog,"Renaming Nics\n"); // #### Debug ####
511
512 for(i=0; i<4; i++)
513 if (strcmp(knics[i].macaddr, "")) // Wird das Interface benutzt ?
514 for(j=0; j<fnics; j++)
515 if(strcmp(knics[i].macaddr, nics[j].macaddr) == 0) { // suche den aktuellen Namen
516 sprintf(nic2find,"%s0",lcolourcard[i]);
517// fprintf(flog,"search4: %s\n", nic2find); // #### Debug ####
518 if(strcmp(nic2find, nics[j].nic)) { // hat das Interface nicht den Namen ?
519// fprintf(flog,"cmp nic2find false\n"); // #### Debug ####
520// fprintf(flog,"is nic( %s ) up ?\n",nics[j].nic); // #### Debug ####
521
522 if(is_interface_up(nics[j].nic)) { // wurde das Interface gestartet ?
523// fprintf(flog,"%s is UP, shutting down...\n",nics[j].nic); // #### Debug ####
524 nic_shutdown(nics[j].nic);
525 }
526 sprintf(temp,SYSDIR "/%s", nic2find);
527// fprintf(flog,"exists ?--> %s\n", temp); // #### Debug ####
528 if(fmt_exists(temp)) { // Ist der Name schon in Benutzung ?
529// fprintf(flog,"is exists %s\n", nic2find); // #### Debug ####
530 for(k=0; k<fnics; k++) // Suche das Interface
531 if (strcmp(nics[k].nic, nic2find) == 0 ) {
532 if(is_interface_up(nics[k].nic)) { // wurde das Interface gestartet ?
533// fprintf(flog,"%s is UP, shutting down...\n",nics[k].nic); // #### Debug ####
534 nic_shutdown(nics[k].nic);
535 }
536 sprintf(temp,"dummy%i",k); // Benenne NIC nach "dummy[k]" um.
537// fprintf(flog,"set dummy%i\n", k); // #### Debug ####
538 if (rename_device(nics[k].nic, temp)) strcpy(nics[k].nic, temp);
539 }
540 }
541 if (rename_device(nics[j].nic, nic2find)) strcpy(nics[j].nic, nic2find); // Benenne NIC um.
542
543// if(strncmp(nics[j].nic,"dummy",5)) {
544// fprintf(flog,"%s is down, start up...\n",nics[j].nic); // #### Debug ####
545// nic_startup(nics[j].nic);
546// }
547 }
548 }
549}
550
84e975ba
HS
551int create_udev(void)
552{
75ae2191 553 #define UDEV_NET_CONF "/etc/udev/rules.d/30-persistent-network.rules"
75ae2191 554 FILE *fp;
84e975ba
HS
555 int i;
556
557 fprintf(flog,"Enter create_udev: "UDEV_NET_CONF"\n"); // #### Debug ####
558 if ( (fp = fopen(UDEV_NET_CONF, "w")) == NULL ) {
559 fprintf(stderr,"Couldn't open" UDEV_NET_CONF);
560 return 1;
561 }
562
563 for (i = 0 ; i < 4 ; i++)
564 {
565 if (strcmp(knics[i].macaddr, "")) {
566 fprintf(fp,"ACTION==\"add\", SUBSYSTEM==\"net\", SYSFS{address}==\"%s\", NAME=\"%s0\" # %s\n", knics[i].macaddr, lcolourcard[i], knics[i].description);
567 fprintf(flog,"Write %s\n",lcolourcard[i]); // #### Debug ####
568 }
569 }
570 fclose(fp);
571 return 0;
572}
573
574int write_configs_netudev(int card , int colour)
575{
75ae2191
MT
576 char commandstring[STRING_SIZE];
577 struct keyvalue *kv = initkeyvalues();
578 char temp1[STRING_SIZE], temp2[STRING_SIZE], temp3[STRING_SIZE];
579 char ucolour[STRING_SIZE];
5057b611 580
9b040aa0 581 sprintf(ucolour, ucolourcard[colour]);
f9cc0d70
HS
582 strcpy(knics[colour].driver, nics[card].driver);
583 strcpy(knics[colour].description, nics[card].description);
584 strcpy(knics[colour].macaddr, nics[card].macaddr);
9b040aa0 585
75ae2191 586 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
b4e381a8 587 {
75ae2191
MT
588 freekeyvalues(kv);
589 errorbox(ctr[TR_UNABLE_TO_OPEN_SETTINGS_FILE]);
590 return 0;
b4e381a8 591 }
9607771a 592
75ae2191
MT
593 sprintf(temp1, "%s_DEV", ucolour);
594 sprintf(temp2, "%s_MACADDR", ucolour);
9b040aa0 595 sprintf(temp3, "%s0", lcolourcard[colour]);
75ae2191 596 replacekeyvalue(kv, temp1, temp3);
f9cc0d70 597 replacekeyvalue(kv, temp2, nics[card].macaddr);
5057b611 598 sprintf(temp1, "%s_DESCRIPTION", ucolour);
f9cc0d70
HS
599 replacekeyvalue(kv, temp1, nics[card].description);
600 sprintf(temp1, "%s_DRIVER", ucolour);
601 replacekeyvalue(kv, temp1, nics[card].driver);
75ae2191
MT
602
603 writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings");
604 freekeyvalues(kv);
9607771a 605
75ae2191
MT
606 return 0;
607}
608
5057b611 609int scan_network_cards(void)
75ae2191 610{
5057b611 611 FILE *fp;
f9cc0d70 612 char driver[STRING_SIZE], description[STRING_SIZE], macaddr[STRING_SIZE], temp_line[STRING_SIZE];
5057b611 613 int count = 0;
b04331d3
HS
614 const char _driver[]="driver: ";
615 const char _desc[]="desc: ";
616 const char _network_hwaddr[]="network.hwaddr: ";
5057b611
HS
617
618 if (!(scanned_nics_read_done))
619 {
620 fprintf(flog,"Enter scan_network_cards\n"); // #### Debug ####
621 mysystem("/bin/probenic.sh");
622 // Read our scanned nics
623 if( (fp = fopen(SCANNED_NICS, "r")) == NULL )
624 {
625 fprintf(stderr,"Couldn't open "SCANNED_NICS);
626 return 1;
627 }
628 while (fgets(temp_line, STRING_SIZE, fp) != NULL)
629 {
b04331d3
HS
630 temp_line[strlen(temp_line) -1] = 0;
631 if ( strncmp(temp_line, _driver, strlen(_driver)) == 0 ) sprintf(nics[count].driver, "%s", temp_line+strlen(_driver));
632 if ( strncmp(temp_line, _desc, strlen(_desc)) == 0 ) sprintf(nics[count].description, "%s", temp_line+strlen(_desc));
633 if ( strncmp(temp_line, _network_hwaddr, strlen(_network_hwaddr)) == 0 ) sprintf(nics[count].macaddr, "%s", temp_line+strlen(_network_hwaddr));
96ed9998
HS
634 if (strlen(nics[count].macaddr) > 15 ) {
635 sprintf(nics[count].nic, "%s", find_nic4mac(nics[count].macaddr));
636 count++;
637 }
5057b611
HS
638 }
639 fclose(fp);
9b040aa0
HS
640 scanned_nics_read_done = count;
641 } else fprintf(flog,"Scan Networkcards does read.\n");
642 return scanned_nics_read_done;
5057b611 643}
75ae2191 644
5057b611
HS
645
646
9b040aa0 647int nicmenu(int colour)
5057b611
HS
648{
649 int rc, choise = 0, count = 0, kcount = 0, mcount = 0, i, j, nic_in_use;
650 int found_NIC_as_Card[4];
651 char message[STRING_SIZE];
652
653 char cMenuInhalt[STRING_SIZE];
654 char MenuInhalt[20][180];
655 char *pMenuInhalt[20];
656
657// strcpy( message , pnics[count].macaddr);
658// while (strcmp(message, "")) {
659// count++;
660// strcpy( message , pnics[count].macaddr);
661// }
662
663 while (strcmp(nics[count].macaddr, "")) count++; // 2 find how many nics in system
664 for ( i=0 ; i<4;i++) if (strcmp(knics[i].macaddr, "")) kcount++; // loop to find all knowing nics
665 fprintf(flog, "Enter NicMenu\n"); // #### Debug ####
666 fprintf(flog, "count nics %i\ncount knics %i\n", count, kcount); // #### Debug ####
667
668 // If new nics are found...
669 if (count > kcount) {
670 for (i=0 ; i < count ; i++)
671 {
672 nic_in_use = 0;
673 for (j=0 ; j <= kcount ; j++) {
674 if (strcmp(nics[ i ].macaddr, knics[ j ].macaddr) == 0 ) {
675 nic_in_use = 1;
676 fprintf(flog,"NIC \"%s\" is in use.\n", nics[ i ].macaddr); // #### Debug ####
677 break;
9607771a 678 }
9607771a 679 }
5057b611
HS
680 if (!(nic_in_use)) {
681 fprintf(flog,"NIC \"%s\" is free.\n", nics[ i ].macaddr); // #### Debug ####
682 if ( strlen(nics[i].description) < 55 )
683 sprintf(MenuInhalt[mcount], "%.*s", strlen(nics[i].description)-2, nics[i].description+1);
684 else {
685 sprintf(cMenuInhalt, "%.50s", nics[i].description + 1);
b04331d3 686 sprintf(MenuInhalt[mcount], cMenuInhalt);
5057b611 687 strcat (MenuInhalt[mcount], "...");
75ae2191 688 }
9607771a 689
b04331d3 690 while ( strlen(MenuInhalt[mcount]) < 53) strcat(MenuInhalt[mcount], " "); // Fill with space.
5057b611
HS
691
692 strcat(MenuInhalt[mcount], " (");
693 strcat(MenuInhalt[mcount], nics[i].macaddr);
694 strcat(MenuInhalt[mcount], ")");
695 pMenuInhalt[mcount] = MenuInhalt[mcount];
696 found_NIC_as_Card[mcount]=i;
697 mcount++;
698 }
75ae2191 699 }
5057b611
HS
700
701 pMenuInhalt[mcount] = NULL;
702
703// sprintf(message, "Es wurde(n) %d freie Netzwerkkarte(n) in Ihrem System gefunden.\nBitte waehlen Sie im naechsten Dialog eine davon aus.\n", count);
704// newtWinMessage("NetcardMenu", ctr[TR_OK], message);
705
9b040aa0 706 sprintf(message, "(TR) Bitte wählen Sie eine der untenstehenden Netzwerkkarten fuer die Schnittstelle \"%s\" aus.\n", ucolourcard[colour]);
5057b611
HS
707 rc = newtWinMenu("(TR) NetcardMenu2", message, 50, 5, 5, 6, pMenuInhalt, &choise, ctr[TR_OK], ctr[TR_SELECT], ctr[TR_CANCEL], NULL);
708
709 if ( rc == 0 || rc == 1) {
f9cc0d70 710 write_configs_netudev(found_NIC_as_Card[choise], colour);
5057b611
HS
711 } else if (rc == 2) {
712// manualdriver("pcnet32","");
713 }
714 return 0;
715 } else {
716 // We have to add here that you can manually add a device
717 errorbox("(TR) Es wurden leider keine freien Netzwerkkarten fuer die Schnittstelle in ihrem System gefunden.");
718 return 1;
719 }
720}
721
9b040aa0 722int clear_card_entry(int card)
5057b611
HS
723{
724 struct keyvalue *kv = initkeyvalues();
9b040aa0 725 char temp[STRING_SIZE];
5057b611 726
5057b611
HS
727 if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings")))
728 {
729 freekeyvalues(kv);
730 errorbox(ctr[TR_UNABLE_TO_OPEN_SETTINGS_FILE]);
731 return 0;
732 }
733
f9cc0d70 734 strcpy(knics[card].driver, "");
9b040aa0
HS
735 strcpy(knics[card].description, ctr[TR_UNSET]);
736 strcpy(knics[card].macaddr, "");
737 strcpy(knics[card].colour, "");
f9cc0d70
HS
738 sprintf(temp, "%s_DRIVER", ucolourcard[card]);
739 replacekeyvalue(kv, temp, "");
9b040aa0
HS
740 sprintf(temp, "%s_DEV", ucolourcard[card]);
741 replacekeyvalue(kv, temp, "");
742 sprintf(temp, "%s_MACADDR", ucolourcard[card]);
743 replacekeyvalue(kv, temp, "");
744 sprintf(temp, "%s_DESCRIPTION", ucolourcard[card]);
745 replacekeyvalue(kv, temp, "");
746
747 writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings");
748 freekeyvalues(kv);
749
750 fprintf(flog,"Card \"%s\" cleared\n",ucolourcard[card]); // #### Debug ####
751 return 0;
752}
753
754int ask_clear_card_entry(int card)
755{
756 char message[STRING_SIZE];
757 int rc;
5057b611 758
9b040aa0 759 sprintf(message, "(TR) Soll die Zuordnung der Netzwerkkarte \"%s\" entfernt werden ?\n", ucolourcard[card]);
5057b611
HS
760 rc = newtWinChoice(ctr[TR_WARNING], ctr[TR_OK], ctr[TR_CANCEL], message);
761
762 if ( rc = 0 || rc == 1) {
9b040aa0
HS
763 clear_card_entry(card);
764// sprintf(temp1, "%s_DEV", ucolour);
765// sprintf(temp2, "%s_MACADDR", ucolour);
766// replacekeyvalue(kv, temp1, "");
767// replacekeyvalue(kv, temp2, "");
768// sprintf(temp1, "%s_DESCRIPTION", ucolour);
769// replacekeyvalue(kv, temp1, "");
770
771// writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings");
5057b611
HS
772 } else return 1;
773
5057b611 774 return 0;
9607771a
MT
775}
776
777/* Manual entry for gurus. */
778int manualdriver(char *driver, char *driveroptions)
779{
780 char *values[] = { NULL, NULL }; /* pointers for the values. */
781 struct newtWinEntry entries[] =
782 { { "", &values[0], 0,}, { NULL, NULL, 0 } };
783 int rc;
784 char commandstring[STRING_SIZE];
785 char *driverend;
786
787 strcpy(driver, "");
788 strcpy(driveroptions, "");
789
790 rc = newtWinEntries(ctr[TR_SELECT_NETWORK_DRIVER],
791 ctr[TR_MODULE_PARAMETERS], 50, 5, 5, 40, entries,
792 ctr[TR_OK], ctr[TR_CANCEL], NULL);
793 if (rc == 0 || rc == 1)
794 {
795 if (strlen(values[0]))
796 {
797 sprintf(commandstring, "/sbin/modprobe %s", values[0]);
798 if (runcommandwithstatus(commandstring, ctr[TR_LOADING_MODULE]) == 0)
799 {
800 if ((driverend = strchr(values[0], ' ')))
801 {
802 *driverend = '\0';
803 strcpy(driver, values[0]);
804 strcpy(driveroptions, driverend + 1);
805 }
806 else
807 {
808 strcpy(driver, values[0]);
809 strcpy(driveroptions, "");
810 }
811 }
812 else
813 errorbox(ctr[TR_UNABLE_TO_LOAD_DRIVER_MODULE]);
814 }
815 else
816 errorbox(ctr[TR_MODULE_NAME_CANNOT_BE_BLANK]);
817 }
818 free(values[0]);
819
820 return 1;
821}