]>
Commit | Line | Data |
---|---|---|
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 | // Translation | |
15 | #include <libintl.h> | |
16 | #define _(x) dgettext("setup", x) | |
17 | ||
18 | #include "setup.h" | |
19 | ||
20 | extern FILE *flog; | |
21 | extern char *mylog; | |
22 | ||
23 | extern struct nic nics[]; | |
24 | extern struct knic knics[]; | |
25 | ||
26 | char *ucolourcard[] = { "GREEN", "RED", "ORANGE", "BLUE", NULL }; | |
27 | char *lcolourcard[] = { "green", "red", "orange", "blue", NULL }; | |
28 | ||
29 | int scanned_nics_read_done = 0; | |
30 | ||
31 | newtComponent networkform; | |
32 | newtComponent addressentry; | |
33 | newtComponent netmaskentry; | |
34 | newtComponent statictyperadio; | |
35 | newtComponent dhcptyperadio; | |
36 | newtComponent pppoetyperadio; | |
37 | newtComponent dhcphostnameentry; | |
38 | newtComponent dhcpforcemtuentry; | |
39 | ||
40 | /* acceptable character filter for IP and netmaks entry boxes */ | |
41 | static 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. */ | |
51 | int changeaddress(struct keyvalue *kv, char *colour, int typeflag, | |
52 | char *defaultdhcphostname) | |
53 | { | |
54 | char *addressresult; | |
55 | char *netmaskresult; | |
56 | char *dhcphostnameresult; | |
57 | char *dhcpforcemturesult; | |
58 | struct newtExitStruct es; | |
59 | newtComponent header; | |
60 | newtComponent addresslabel; | |
61 | newtComponent netmasklabel; | |
62 | newtComponent dhcphostnamelabel; | |
63 | newtComponent dhcpforcemtulabel; | |
64 | newtComponent ok, cancel; | |
65 | char message[1000]; | |
66 | char temp[STRING_SIZE]; | |
67 | char addressfield[STRING_SIZE]; | |
68 | char netmaskfield[STRING_SIZE]; | |
69 | char typefield[STRING_SIZE]; | |
70 | char dhcphostnamefield[STRING_SIZE]; | |
71 | char dhcpforcemtufield[STRING_SIZE]; | |
72 | int error; | |
73 | int result = 0; | |
74 | char type[STRING_SIZE]; | |
75 | int startstatictype = 0; | |
76 | int startdhcptype = 0; | |
77 | int startpppoetype = 0; | |
78 | unsigned char buffer[sizeof(struct in_addr)]; | |
79 | ||
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 | ||
87 | sprintf(message, _("Interface - %s"), colour); | |
88 | newtCenteredWindow(44, (typeflag ? 18 : 12), message); | |
89 | ||
90 | networkform = newtForm(NULL, NULL, 0); | |
91 | ||
92 | sprintf(message, _("Enter the IP address information for the %s interface."), colour); | |
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; | |
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); | |
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); | |
114 | newtTextboxSetText(dhcphostnamelabel, _("DHCP Hostname:")); | |
115 | dhcpforcemtulabel = newtTextbox(2, 9, 18, 1, 0); | |
116 | newtTextboxSetText(dhcpforcemtulabel, _("Force DHCP MTU:")); | |
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); | |
135 | newtTextboxSetText(addresslabel, _("IP address:")); | |
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); | |
147 | newtTextboxSetText(netmasklabel, _("Network mask:")); | |
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. */ | |
158 | ok = newtButton(8, (typeflag ? 14 : 7), _("OK")); | |
159 | cancel = newtButton(26, (typeflag ? 14 : 7), _("Cancel")); | |
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. */ | |
174 | strcpy(message, _("The following fields are invalid:")); | |
175 | strcat(message, "\n\n"); | |
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 | { | |
184 | strcat(message, _("IP address")); | |
185 | strcat(message, "\n"); | |
186 | error = 1; | |
187 | } | |
188 | if (inet_pton(AF_INET, netmaskresult, &buffer) == 0) | |
189 | { | |
190 | strcat(message, _("Network mask")); | |
191 | strcat(message, "\n"); | |
192 | error = 1; | |
193 | } | |
194 | } | |
195 | if (strcmp(type, "DHCP") == 0) | |
196 | { | |
197 | if (!strlen(dhcphostnameresult)) | |
198 | { | |
199 | strcat(message, _("DHCP hostname")); | |
200 | strcat(message, "\n"); | |
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 */ | |
252 | int 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 | int setnetaddress(struct keyvalue *kv, char *colour) | |
269 | { | |
270 | char addressfield[STRING_SIZE]; | |
271 | char netaddressfield[STRING_SIZE]; | |
272 | char netmaskfield[STRING_SIZE]; | |
273 | char address[STRING_SIZE]; | |
274 | char netmask[STRING_SIZE]; | |
275 | unsigned long int intaddress; | |
276 | unsigned long int intnetaddress; | |
277 | unsigned long int intnetmask; | |
278 | struct in_addr temp; | |
279 | char *netaddress; | |
280 | ||
281 | /* Build some key strings. */ | |
282 | sprintf(addressfield, "%s_ADDRESS", colour); | |
283 | sprintf(netaddressfield, "%s_NETADDRESS", colour); | |
284 | sprintf(netmaskfield, "%s_NETMASK", colour); | |
285 | ||
286 | strcpy(address, ""); findkey(kv, addressfield, address); | |
287 | strcpy(netmask, ""); findkey(kv, netmaskfield, netmask); | |
288 | ||
289 | /* Calculate netaddress. Messy.. */ | |
290 | intaddress = inet_addr(address); | |
291 | intnetmask = inet_addr(netmask); | |
292 | ||
293 | intnetaddress = intaddress & intnetmask; | |
294 | temp.s_addr = intnetaddress; | |
295 | netaddress = inet_ntoa(temp); | |
296 | ||
297 | replacekeyvalue(kv, netaddressfield, netaddress); | |
298 | ||
299 | return 1; | |
300 | } | |
301 | ||
302 | /* Called when dhcp flag is toggled. Toggle disabled state of other 3 | |
303 | * controls. */ | |
304 | void networkdialogcallbacktype(newtComponent cm, void *data) | |
305 | { | |
306 | char type[STRING_SIZE]; | |
307 | ||
308 | gettype(type); | |
309 | ||
310 | if (strcmp(type, "STATIC") != 0) | |
311 | { | |
312 | newtEntrySetFlags(addressentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET); | |
313 | newtEntrySetFlags(netmaskentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET); | |
314 | } | |
315 | else | |
316 | { | |
317 | newtEntrySetFlags(addressentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_RESET); | |
318 | newtEntrySetFlags(netmaskentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_RESET); | |
319 | } | |
320 | if (strcmp(type, "DHCP") == 0) | |
321 | { | |
322 | newtEntrySetFlags(dhcphostnameentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_RESET); | |
323 | newtEntrySetFlags(dhcpforcemtuentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_RESET); | |
324 | } | |
325 | else | |
326 | { | |
327 | newtEntrySetFlags(dhcphostnameentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET); | |
328 | newtEntrySetFlags(dhcpforcemtuentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET); | |
329 | } | |
330 | newtRefresh(); | |
331 | newtDrawForm(networkform); | |
332 | } | |
333 | ||
334 | int interfacecheck(struct keyvalue *kv, char *colour) | |
335 | { | |
336 | char temp[STRING_SIZE]; | |
337 | char colourfields[NETCHANGE_TOTAL][STRING_SIZE]; | |
338 | int c; | |
339 | ||
340 | sprintf(colourfields[ADDRESS], "%s_ADDRESS", colour); | |
341 | sprintf(colourfields[NETADDRESS], "%s_NETADDRESS", colour); | |
342 | sprintf(colourfields[NETMASK], "%s_NETMASK", colour); | |
343 | ||
344 | for (c = 0; c < 3; c++) | |
345 | { | |
346 | strcpy(temp, ""); findkey(kv, colourfields[c], temp); | |
347 | if (!(strlen(temp))) return 0; | |
348 | } | |
349 | return 1; | |
350 | } | |
351 | ||
352 | /* Funky routine for loading all drivers (cept those are already loaded.). */ | |
353 | int probecards(char *driver, char *driveroptions ) | |
354 | { | |
355 | return 0; | |
356 | } | |
357 | ||
358 | int get_knic(int card) //returns "0" for zero cards or error and "1" card is found. | |
359 | { | |
360 | struct keyvalue *kv = initkeyvalues(); | |
361 | char temp[STRING_SIZE], searchstr[STRING_SIZE]; | |
362 | int ret_value; | |
363 | ||
364 | if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings"))) | |
365 | { | |
366 | freekeyvalues(kv); | |
367 | errorbox(_("Unable to open settings file")); | |
368 | return 0; | |
369 | } | |
370 | ||
371 | sprintf(searchstr, "%s_MACADDR", ucolourcard[card]); | |
372 | strcpy(temp, ""); findkey(kv, searchstr, temp); | |
373 | if (strlen(temp)) { | |
374 | strcpy(knics[ card ].macaddr, temp); | |
375 | strcpy(knics[ card ].colour, ucolourcard[card]); | |
376 | ||
377 | sprintf(searchstr, "%s_DESCRIPTION", ucolourcard[card]); | |
378 | findkey(kv, searchstr, temp); | |
379 | strcpy(knics[ card ].description, temp); | |
380 | ||
381 | sprintf(searchstr, "%s_DRIVER", ucolourcard[card]); | |
382 | findkey(kv, searchstr, temp); | |
383 | strcpy(knics[ card ].driver, temp); | |
384 | ret_value = 1; | |
385 | } else { | |
386 | strcpy(knics[ card ].description, _("Unset")); | |
387 | ret_value = 0; | |
388 | } | |
389 | freekeyvalues(kv); | |
390 | ||
391 | return ret_value; | |
392 | } | |
393 | ||
394 | int init_knics(void) | |
395 | { | |
396 | int found = 0; | |
397 | found += get_knic(_GREEN_CARD_); | |
398 | found += get_knic(_RED_CARD_); | |
399 | found += get_knic(_ORANGE_CARD_); | |
400 | found += get_knic(_BLUE_CARD_); | |
401 | ||
402 | return found; | |
403 | } | |
404 | ||
405 | int fmt_exists(const char *fname) { /* Check if it is any file or directory */ | |
406 | struct stat st; | |
407 | if (stat(fname, &st) == -1) return 0; | |
408 | else return 1; | |
409 | } | |
410 | ||
411 | int is_interface_up(char *card) { /* Check if the interface is UP */ | |
412 | char temp[STRING_SIZE]; | |
413 | ||
414 | sprintf(temp,"ip link show dev %s | grep -q UP", card); | |
415 | if (mysystem(NULL, temp)) return 0; else return 1; | |
416 | } | |
417 | ||
418 | int rename_device(char *old_name, char *new_name) { | |
419 | char temp[STRING_SIZE]; | |
420 | ||
421 | sprintf(temp,SYSDIR "/%s", old_name); | |
422 | if (!(fmt_exists(temp))) { | |
423 | fprintf(flog,"Device not found: %s\n",old_name); | |
424 | return 0; | |
425 | } | |
426 | sprintf(temp,"/sbin/ip link set dev %s name %s",old_name ,new_name ); | |
427 | mysystem(NULL, temp); | |
428 | ||
429 | return 1; | |
430 | } | |
431 | ||
432 | char g_temp[STRING_SIZE]=""; | |
433 | char* readmac(char *card) { | |
434 | FILE *fp; | |
435 | char temp[STRING_SIZE], mac[20]; | |
436 | ||
437 | sprintf(temp,"/sys/class/net/%s/address",card); | |
438 | if( (fp = fopen(temp, "r")) == NULL ) { | |
439 | fprintf(flog,"Couldn't open: %s\n",temp); | |
440 | return NULL; | |
441 | } | |
442 | fgets(mac, 18, fp); | |
443 | strtok(mac,"\n"); | |
444 | fclose(fp); | |
445 | strcpy(g_temp, mac); | |
446 | return g_temp; | |
447 | } | |
448 | ||
449 | char* find_nic4mac(char *findmac) { | |
450 | DIR *dir; | |
451 | struct dirent *dirzeiger; | |
452 | char temp[STRING_SIZE], temp2[STRING_SIZE]; | |
453 | ||
454 | if((dir=opendir(SYSDIR)) == NULL) { | |
455 | fprintf(flog,"Fehler bei opendir (find_name4nic) ...\n"); | |
456 | return NULL; | |
457 | } | |
458 | ||
459 | sprintf(temp, ""); | |
460 | while((dirzeiger=readdir(dir)) != NULL) { | |
461 | if(*((*dirzeiger).d_name) != '.' & strcmp(((*dirzeiger).d_name), "lo") != 0) { | |
462 | sprintf(temp2, "%s", readmac((*dirzeiger).d_name) ); | |
463 | if (strcmp(findmac, temp2) == 0) { | |
464 | sprintf(temp,"%s", (*dirzeiger).d_name); | |
465 | break; | |
466 | } | |
467 | } | |
468 | } | |
469 | ||
470 | if(closedir(dir) == -1) fprintf(flog,"Fehler beim schliessen von %s\n", SYSDIR); | |
471 | strcpy(g_temp, temp); | |
472 | return g_temp; | |
473 | } | |
474 | ||
475 | int nic_shutdown(char *nic) { | |
476 | char temp[STRING_SIZE]; | |
477 | ||
478 | sprintf(temp,"ip link set %s down", nic); | |
479 | mysystem(NULL, temp); | |
480 | } | |
481 | ||
482 | int nic_startup(char *nic) { | |
483 | char temp[STRING_SIZE]; | |
484 | ||
485 | sprintf(temp,"ip link set %s up", nic); | |
486 | mysystem(NULL, temp); | |
487 | ||
488 | } | |
489 | ||
490 | int rename_nics(void) { | |
491 | int i, j, k; | |
492 | int fnics = scan_network_cards(); | |
493 | char nic2find[STRING_SIZE], temp[STRING_SIZE]; | |
494 | ||
495 | for(i=0; i<4; i++) | |
496 | if (strcmp(knics[i].macaddr, "")) | |
497 | for(j=0; j<fnics; j++) | |
498 | if(strcmp(knics[i].macaddr, nics[j].macaddr) == 0) { | |
499 | sprintf(nic2find,"%s0",lcolourcard[i]); | |
500 | if(strcmp(nic2find, nics[j].nic)) { | |
501 | if(is_interface_up(nics[j].nic)) { | |
502 | nic_shutdown(nics[j].nic); | |
503 | } | |
504 | sprintf(temp,SYSDIR "/%s", nic2find); | |
505 | if(fmt_exists(temp)) { | |
506 | for(k=0; k<fnics; k++) | |
507 | if (strcmp(nics[k].nic, nic2find) == 0 ) { | |
508 | if(is_interface_up(nics[k].nic)) { | |
509 | nic_shutdown(nics[k].nic); | |
510 | } | |
511 | sprintf(temp,"dummy%i",k); | |
512 | if (rename_device(nics[k].nic, temp)) strcpy(nics[k].nic, temp); | |
513 | } | |
514 | } | |
515 | if (rename_device(nics[j].nic, nic2find)) strcpy(nics[j].nic, nic2find); | |
516 | } | |
517 | } | |
518 | } | |
519 | ||
520 | int write_configs_netudev(int card , int colour) | |
521 | { | |
522 | char commandstring[STRING_SIZE]; | |
523 | struct keyvalue *kv = initkeyvalues(); | |
524 | char temp1[STRING_SIZE], temp2[STRING_SIZE], temp3[STRING_SIZE]; | |
525 | char ucolour[STRING_SIZE]; | |
526 | ||
527 | sprintf(ucolour, ucolourcard[colour]); | |
528 | strcpy(knics[colour].driver, nics[card].driver); | |
529 | strcpy(knics[colour].description, nics[card].description); | |
530 | strcpy(knics[colour].macaddr, nics[card].macaddr); | |
531 | ||
532 | if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings"))) | |
533 | { | |
534 | freekeyvalues(kv); | |
535 | errorbox(_("Unable to open settings file")); | |
536 | return 0; | |
537 | } | |
538 | ||
539 | sprintf(temp1, "%s_DEV", ucolour); | |
540 | sprintf(temp2, "%s_MACADDR", ucolour); | |
541 | sprintf(temp3, "%s0", lcolourcard[colour]); | |
542 | replacekeyvalue(kv, temp1, temp3); | |
543 | replacekeyvalue(kv, temp2, nics[card].macaddr); | |
544 | sprintf(temp1, "%s_DESCRIPTION", ucolour); | |
545 | replacekeyvalue(kv, temp1, nics[card].description); | |
546 | sprintf(temp1, "%s_DRIVER", ucolour); | |
547 | replacekeyvalue(kv, temp1, nics[card].driver); | |
548 | ||
549 | writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings"); | |
550 | freekeyvalues(kv); | |
551 | ||
552 | return 0; | |
553 | } | |
554 | ||
555 | int scan_network_cards(void) | |
556 | { | |
557 | FILE *fp; | |
558 | char driver[STRING_SIZE], description[STRING_SIZE], macaddr[STRING_SIZE], temp_line[STRING_SIZE]; | |
559 | int count = 0; | |
560 | const char _driver[]="driver: "; | |
561 | const char _desc[]="desc: "; | |
562 | const char _network_hwaddr[]="network.hwaddr: "; | |
563 | ||
564 | if (!(scanned_nics_read_done)) | |
565 | { | |
566 | mysystem(NULL, "/usr/bin/probenic.sh"); | |
567 | if( (fp = fopen(SCANNED_NICS, "r")) == NULL ) | |
568 | { | |
569 | fprintf(stderr,"Couldn't open "SCANNED_NICS); | |
570 | return 1; | |
571 | } | |
572 | while (fgets(temp_line, STRING_SIZE, fp) != NULL) | |
573 | { | |
574 | temp_line[strlen(temp_line) -1] = 0; | |
575 | if ( strncmp(temp_line, _driver, strlen(_driver)) == 0 ) sprintf(nics[count].driver, "%s", temp_line+strlen(_driver)); | |
576 | if ( strncmp(temp_line, _desc, strlen(_desc)) == 0 ) sprintf(nics[count].description, "%s", temp_line+strlen(_desc)); | |
577 | if ( strncmp(temp_line, _network_hwaddr, strlen(_network_hwaddr)) == 0 ) sprintf(nics[count].macaddr, "%s", temp_line+strlen(_network_hwaddr)); | |
578 | if (strlen(nics[count].macaddr) > 15 ) { | |
579 | sprintf(nics[count].nic, "%s", find_nic4mac(nics[count].macaddr)); | |
580 | count++; | |
581 | } | |
582 | } | |
583 | fclose(fp); | |
584 | scanned_nics_read_done = count; | |
585 | } else fprintf(flog,"Scan Networkcards does read.\n"); | |
586 | return scanned_nics_read_done; | |
587 | } | |
588 | ||
589 | ||
590 | ||
591 | int nicmenu(int colour) | |
592 | { | |
593 | int rc, choise = 0, count = 0, kcount = 0, mcount = 0, i, j, nic_in_use; | |
594 | int found_NIC_as_Card[MAX_NICS]; | |
595 | char message[STRING_SIZE]; | |
596 | char temp[STRING_SIZE]; | |
597 | ||
598 | char cMenuInhalt[STRING_SIZE]; | |
599 | char MenuInhalt[MAX_NICS][STRING_SIZE]; | |
600 | char *pMenuInhalt[MAX_NICS]; | |
601 | ||
602 | while (strcmp(nics[count].macaddr, "")) count++; // 2 find how many nics in system | |
603 | for (i=0; i<MAX_NICS; i++) if (strcmp(knics[i].macaddr, "")) kcount++; // loop to find all knowing nics | |
604 | ||
605 | // If new nics are found... | |
606 | if (count > kcount) { | |
607 | for (i=0 ; i < count ; i++) | |
608 | { | |
609 | nic_in_use = 0; | |
610 | for (j=0 ; j <= kcount ; j++) { | |
611 | if (strcmp(nics[ i ].macaddr, knics[ j ].macaddr) == 0 ) { | |
612 | nic_in_use = 1; | |
613 | break; | |
614 | } | |
615 | } | |
616 | if (!(nic_in_use)) { | |
617 | if ( strlen(nics[i].description) < 55 ) | |
618 | sprintf(MenuInhalt[mcount], "%.*s", strlen(nics[i].description)-2, nics[i].description+1); | |
619 | else { | |
620 | sprintf(cMenuInhalt, "%.50s", nics[i].description + 1); | |
621 | sprintf(MenuInhalt[mcount], cMenuInhalt); | |
622 | strcat (MenuInhalt[mcount], "..."); | |
623 | } | |
624 | ||
625 | while ( strlen(MenuInhalt[mcount]) < 53) strcat(MenuInhalt[mcount], " "); // Fill with space. | |
626 | ||
627 | strcat(MenuInhalt[mcount], " ("); | |
628 | strcat(MenuInhalt[mcount], nics[i].macaddr); | |
629 | strcat(MenuInhalt[mcount], ")"); | |
630 | pMenuInhalt[mcount] = MenuInhalt[mcount]; | |
631 | found_NIC_as_Card[mcount]=i; | |
632 | mcount++; | |
633 | } | |
634 | } | |
635 | ||
636 | pMenuInhalt[mcount] = NULL; | |
637 | ||
638 | sprintf(message, _("Please choose a networkcard for the following interface - %s."), ucolourcard[colour]); | |
639 | rc=2; | |
640 | while ( rc == 2 ) { | |
641 | rc = newtWinMenu(_("Extended Network Menu"), message, 50, 5, 5, mcount, pMenuInhalt, &choise, | |
642 | _("Select"), _("Identify"), _("Cancel"), NULL); | |
643 | if ( rc == 2 ) { | |
644 | sprintf(temp, "/sbin/ip link set %s up", nics[found_NIC_as_Card[choise]].nic); | |
645 | mysystem(NULL, temp); | |
646 | sprintf(temp, "/usr/sbin/ethtool -p %s 10", nics[found_NIC_as_Card[choise]].nic); | |
647 | if (runcommandwithstatus(temp, _("Device Identification"), _("The lights on the selected port should flash now for 10 seconds..."), NULL) != 0) { | |
648 | errorbox(_("Identification is not supported by this interface.")); | |
649 | sprintf(temp, "/sbin/ip link set %s down", nics[found_NIC_as_Card[choise]].nic); | |
650 | mysystem(NULL, temp); | |
651 | } | |
652 | } | |
653 | } | |
654 | if ( rc == 0 || rc == 1) { | |
655 | write_configs_netudev(found_NIC_as_Card[choise], colour); | |
656 | } | |
657 | return 0; | |
658 | } else { | |
659 | // We have to add here that you can manually add a device | |
660 | errorbox(_("There are no unassigned interfaces on your system.")); | |
661 | return 1; | |
662 | } | |
663 | } | |
664 | ||
665 | int clear_card_entry(int card) | |
666 | { | |
667 | struct keyvalue *kv = initkeyvalues(); | |
668 | char temp[STRING_SIZE]; | |
669 | ||
670 | if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings"))) | |
671 | { | |
672 | freekeyvalues(kv); | |
673 | errorbox(_("Unable to open settings file")); | |
674 | return 0; | |
675 | } | |
676 | ||
677 | strcpy(knics[card].driver, ""); | |
678 | strcpy(knics[card].description, _("Unset")); | |
679 | strcpy(knics[card].macaddr, ""); | |
680 | strcpy(knics[card].colour, ""); | |
681 | sprintf(temp, "%s_DRIVER", ucolourcard[card]); | |
682 | replacekeyvalue(kv, temp, ""); | |
683 | sprintf(temp, "%s_DEV", ucolourcard[card]); | |
684 | replacekeyvalue(kv, temp, ""); | |
685 | sprintf(temp, "%s_MACADDR", ucolourcard[card]); | |
686 | replacekeyvalue(kv, temp, ""); | |
687 | sprintf(temp, "%s_DESCRIPTION", ucolourcard[card]); | |
688 | replacekeyvalue(kv, temp, ""); | |
689 | ||
690 | writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings"); | |
691 | freekeyvalues(kv); | |
692 | ||
693 | return 0; | |
694 | } | |
695 | ||
696 | int ask_clear_card_entry(int card) | |
697 | { | |
698 | char message[STRING_SIZE]; | |
699 | int rc; | |
700 | ||
701 | sprintf(message, _("Do you really want to remove the assigned %s interface?"), ucolourcard[card]); | |
702 | rc = newtWinChoice(_("Warning"), _("OK"), _("Cancel"), message); | |
703 | ||
704 | if ( rc = 0 || rc == 1) { | |
705 | clear_card_entry(card); | |
706 | } else return 1; | |
707 | ||
708 | return 0; | |
709 | } | |
710 | ||
711 | /* Manual entry for gurus. */ | |
712 | int manualdriver(char *driver, char *driveroptions) | |
713 | { | |
714 | char *values[] = { NULL, NULL }; /* pointers for the values. */ | |
715 | struct newtWinEntry entries[] = | |
716 | { { "", &values[0], 0,}, { NULL, NULL, 0 } }; | |
717 | int rc; | |
718 | char commandstring[STRING_SIZE]; | |
719 | char *driverend; | |
720 | ||
721 | strcpy(driver, ""); | |
722 | strcpy(driveroptions, ""); | |
723 | ||
724 | rc = newtWinEntries(_("Select network driver"), _("Set additional module parameters"), | |
725 | 50, 5, 5, 40, entries, _("OK"), _("Cancel"), NULL); | |
726 | if (rc == 0 || rc == 1) | |
727 | { | |
728 | if (strlen(values[0])) | |
729 | { | |
730 | sprintf(commandstring, "/sbin/modprobe %s", values[0]); | |
731 | if (runcommandwithstatus(commandstring, _("Loading module..."), _("Loading module..."), NULL) == 0) | |
732 | { | |
733 | if ((driverend = strchr(values[0], ' '))) | |
734 | { | |
735 | *driverend = '\0'; | |
736 | strcpy(driver, values[0]); | |
737 | strcpy(driveroptions, driverend + 1); | |
738 | } | |
739 | else | |
740 | { | |
741 | strcpy(driver, values[0]); | |
742 | strcpy(driveroptions, ""); | |
743 | } | |
744 | } | |
745 | else | |
746 | errorbox(_("Unable to load driver module.")); | |
747 | } | |
748 | else | |
749 | errorbox(_("Module name cannot be blank.")); | |
750 | } | |
751 | free(values[0]); | |
752 | ||
753 | return 1; | |
754 | } |