]>
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 | 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 pptptyperadio; | |
34 | newtComponent dhcphostnameentry; | |
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 | 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 */ | |
224 | int 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. */ | |
243 | int 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. */ | |
289 | void 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 | ||
314 | int 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 | } | |
331 | ||
332 | /* Funky routine for loading all drivers (cept those are already loaded.). */ | |
333 | int probecards(char *driver, char *driveroptions ) | |
334 | { | |
335 | return 0; | |
336 | } | |
337 | ||
338 | int get_knic(int card) //returns "0" for zero cards or error and "1" card is found. | |
339 | { | |
340 | struct keyvalue *kv = initkeyvalues(); | |
341 | char temp[STRING_SIZE], searchstr[STRING_SIZE]; | |
342 | int ret_value; | |
343 | ||
344 | if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings"))) | |
345 | { | |
346 | freekeyvalues(kv); | |
347 | errorbox(ctr[TR_UNABLE_TO_OPEN_SETTINGS_FILE]); | |
348 | return 0; | |
349 | } | |
350 | ||
351 | sprintf(searchstr, "%s_MACADDR", ucolourcard[card]); | |
352 | strcpy(temp, ""); findkey(kv, searchstr, temp); | |
353 | if (strlen(temp)) { | |
354 | strcpy(knics[ card ].macaddr, temp); | |
355 | strcpy(knics[ card ].colour, "GREEN"); | |
356 | ||
357 | sprintf(searchstr, "%s_DESCRIPTION", ucolourcard[card]); | |
358 | findkey(kv, searchstr, temp); | |
359 | strcpy(knics[ card ].description, temp); | |
360 | ||
361 | sprintf(searchstr, "%s_DRIVER", ucolourcard[card]); | |
362 | findkey(kv, searchstr, temp); | |
363 | strcpy(knics[ card ].driver, temp); | |
364 | ret_value = 1; | |
365 | } else { | |
366 | strcpy(knics[ card ].description, ctr[TR_UNSET]); | |
367 | ret_value = 0; | |
368 | } | |
369 | ||
370 | return ret_value; | |
371 | } | |
372 | ||
373 | int init_knics(void) | |
374 | { | |
375 | int found = 0; | |
376 | found += get_knic(_GREEN_CARD_); | |
377 | found += get_knic(_RED_CARD_); | |
378 | found += get_knic(_ORANGE_CARD_); | |
379 | found += get_knic(_BLUE_CARD_); | |
380 | ||
381 | return found; | |
382 | } | |
383 | ||
384 | int fmt_exists(const char *fname) { /* Check if it is any file or directory */ | |
385 | struct stat st; | |
386 | if (stat(fname, &st) == -1) return 0; | |
387 | else return 1; | |
388 | } | |
389 | ||
390 | int is_interface_up(char *card) { /* Check if the interface is UP */ | |
391 | char temp[STRING_SIZE]; | |
392 | ||
393 | sprintf(temp,"ip link show dev %s | grep -q UP", card); | |
394 | if (mysystem(temp)) return 0; else return 1; | |
395 | } | |
396 | ||
397 | int rename_device(char *old_name, char *new_name) { | |
398 | char temp[STRING_SIZE]; | |
399 | ||
400 | sprintf(temp,SYSDIR "/%s", old_name); | |
401 | if (!(fmt_exists(temp))) { | |
402 | fprintf(flog,"Device not found: %s\n",old_name); | |
403 | return 0; | |
404 | } | |
405 | sprintf(temp,"/sbin/ip link set dev %s name %s",old_name ,new_name ); | |
406 | mysystem(temp); | |
407 | ||
408 | return 1; | |
409 | } | |
410 | ||
411 | char g_temp[STRING_SIZE]=""; | |
412 | char* readmac(char *card) { | |
413 | FILE *fp; | |
414 | char temp[STRING_SIZE], mac[20]; | |
415 | ||
416 | sprintf(temp,"/sys/class/net/%s/address",card); | |
417 | if( (fp = fopen(temp, "r")) == NULL ) { | |
418 | fprintf(flog,"Couldn't open: %s\n",temp); | |
419 | return NULL; | |
420 | } | |
421 | fgets(mac, 18, fp); | |
422 | strtok(mac,"\n"); | |
423 | fclose(fp); | |
424 | strcpy(g_temp, mac); | |
425 | return g_temp; | |
426 | } | |
427 | ||
428 | char* find_nic4mac(char *findmac) { | |
429 | DIR *dir; | |
430 | struct dirent *dirzeiger; | |
431 | char temp[STRING_SIZE], temp2[STRING_SIZE]; | |
432 | ||
433 | if((dir=opendir(SYSDIR)) == NULL) { | |
434 | fprintf(flog,"Fehler bei opendir (find_name4nic) ...\n"); | |
435 | return NULL; | |
436 | } | |
437 | ||
438 | sprintf(temp, ""); | |
439 | while((dirzeiger=readdir(dir)) != NULL) { | |
440 | if(*((*dirzeiger).d_name) != '.' & strcmp(((*dirzeiger).d_name), "lo") != 0) { | |
441 | sprintf(temp2, "%s", readmac((*dirzeiger).d_name) ); | |
442 | if (strcmp(findmac, temp2) == 0) { | |
443 | sprintf(temp,"%s", (*dirzeiger).d_name); | |
444 | break; | |
445 | } | |
446 | } | |
447 | } | |
448 | ||
449 | if(closedir(dir) == -1) fprintf(flog,"Fehler beim schliessen von %s\n", SYSDIR); | |
450 | strcpy(g_temp, temp); | |
451 | return g_temp; | |
452 | } | |
453 | ||
454 | int nic_shutdown(char *nic) { | |
455 | char temp[STRING_SIZE]; | |
456 | ||
457 | sprintf(temp,"ip link set %s down", nic); | |
458 | mysystem(temp); | |
459 | } | |
460 | ||
461 | int nic_startup(char *nic) { | |
462 | char temp[STRING_SIZE]; | |
463 | ||
464 | sprintf(temp,"ip link set %s up", nic); | |
465 | mysystem(temp); | |
466 | ||
467 | } | |
468 | ||
469 | int rename_nics(void) { | |
470 | int i, j, k; | |
471 | int fnics = scan_network_cards(); | |
472 | char nic2find[STRING_SIZE], temp[STRING_SIZE]; | |
473 | ||
474 | for(i=0; i<4; i++) | |
475 | if (strcmp(knics[i].macaddr, "")) | |
476 | for(j=0; j<fnics; j++) | |
477 | if(strcmp(knics[i].macaddr, nics[j].macaddr) == 0) { | |
478 | sprintf(nic2find,"%s0",lcolourcard[i]); | |
479 | if(strcmp(nic2find, nics[j].nic)) { | |
480 | if(is_interface_up(nics[j].nic)) { | |
481 | nic_shutdown(nics[j].nic); | |
482 | } | |
483 | sprintf(temp,SYSDIR "/%s", nic2find); | |
484 | if(fmt_exists(temp)) { | |
485 | for(k=0; k<fnics; k++) | |
486 | if (strcmp(nics[k].nic, nic2find) == 0 ) { | |
487 | if(is_interface_up(nics[k].nic)) { | |
488 | nic_shutdown(nics[k].nic); | |
489 | } | |
490 | sprintf(temp,"dummy%i",k); | |
491 | if (rename_device(nics[k].nic, temp)) strcpy(nics[k].nic, temp); | |
492 | } | |
493 | } | |
494 | if (rename_device(nics[j].nic, nic2find)) strcpy(nics[j].nic, nic2find); | |
495 | } | |
496 | } | |
497 | } | |
498 | ||
499 | int create_udev(void) | |
500 | { | |
501 | #define UDEV_NET_CONF "/etc/udev/rules.d/30-persistent-network.rules" | |
502 | FILE *fp; | |
503 | int i; | |
504 | ||
505 | if ( (fp = fopen(UDEV_NET_CONF, "w")) == NULL ) { | |
506 | fprintf(stderr,"Couldn't open" UDEV_NET_CONF); | |
507 | return 1; | |
508 | } | |
509 | ||
510 | for (i = 0 ; i < 4 ; i++) | |
511 | { | |
512 | if (strcmp(knics[i].macaddr, "")) { | |
513 | fprintf(fp,"ACTION==\"add\", SUBSYSTEM==\"net\", SYSFS{address}==\"%s\", NAME=\"%s0\" # %s\n", knics[i].macaddr, lcolourcard[i], knics[i].description); | |
514 | } | |
515 | } | |
516 | fclose(fp); | |
517 | return 0; | |
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(ctr[TR_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("/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[4]; | |
595 | char message[STRING_SIZE]; | |
596 | ||
597 | char cMenuInhalt[STRING_SIZE]; | |
598 | char MenuInhalt[20][180]; | |
599 | char *pMenuInhalt[20]; | |
600 | ||
601 | while (strcmp(nics[count].macaddr, "")) count++; // 2 find how many nics in system | |
602 | for ( i=0 ; i<4;i++) if (strcmp(knics[i].macaddr, "")) kcount++; // loop to find all knowing nics | |
603 | ||
604 | // If new nics are found... | |
605 | if (count > kcount) { | |
606 | for (i=0 ; i < count ; i++) | |
607 | { | |
608 | nic_in_use = 0; | |
609 | for (j=0 ; j <= kcount ; j++) { | |
610 | if (strcmp(nics[ i ].macaddr, knics[ j ].macaddr) == 0 ) { | |
611 | nic_in_use = 1; | |
612 | break; | |
613 | } | |
614 | } | |
615 | if (!(nic_in_use)) { | |
616 | if ( strlen(nics[i].description) < 55 ) | |
617 | sprintf(MenuInhalt[mcount], "%.*s", strlen(nics[i].description)-2, nics[i].description+1); | |
618 | else { | |
619 | sprintf(cMenuInhalt, "%.50s", nics[i].description + 1); | |
620 | sprintf(MenuInhalt[mcount], cMenuInhalt); | |
621 | strcat (MenuInhalt[mcount], "..."); | |
622 | } | |
623 | ||
624 | while ( strlen(MenuInhalt[mcount]) < 53) strcat(MenuInhalt[mcount], " "); // Fill with space. | |
625 | ||
626 | strcat(MenuInhalt[mcount], " ("); | |
627 | strcat(MenuInhalt[mcount], nics[i].macaddr); | |
628 | strcat(MenuInhalt[mcount], ")"); | |
629 | pMenuInhalt[mcount] = MenuInhalt[mcount]; | |
630 | found_NIC_as_Card[mcount]=i; | |
631 | mcount++; | |
632 | } | |
633 | } | |
634 | ||
635 | pMenuInhalt[mcount] = NULL; | |
636 | ||
637 | sprintf(message, ctr[TR_CHOOSE_NETCARD], ucolourcard[colour]); | |
638 | rc = newtWinMenu( ctr[TR_NETCARDMENU2], message, 50, 5, 5, 6, pMenuInhalt, &choise, ctr[TR_OK], ctr[TR_SELECT], ctr[TR_CANCEL], NULL); | |
639 | ||
640 | if ( rc == 0 || rc == 1) { | |
641 | write_configs_netudev(found_NIC_as_Card[choise], colour); | |
642 | } | |
643 | return 0; | |
644 | } else { | |
645 | // We have to add here that you can manually add a device | |
646 | errorbox( ctr[TR_ERROR_INTERFACES]); | |
647 | return 1; | |
648 | } | |
649 | } | |
650 | ||
651 | int clear_card_entry(int card) | |
652 | { | |
653 | struct keyvalue *kv = initkeyvalues(); | |
654 | char temp[STRING_SIZE]; | |
655 | ||
656 | if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings"))) | |
657 | { | |
658 | freekeyvalues(kv); | |
659 | errorbox(ctr[TR_UNABLE_TO_OPEN_SETTINGS_FILE]); | |
660 | return 0; | |
661 | } | |
662 | ||
663 | strcpy(knics[card].driver, ""); | |
664 | strcpy(knics[card].description, ctr[TR_UNSET]); | |
665 | strcpy(knics[card].macaddr, ""); | |
666 | strcpy(knics[card].colour, ""); | |
667 | sprintf(temp, "%s_DRIVER", ucolourcard[card]); | |
668 | replacekeyvalue(kv, temp, ""); | |
669 | sprintf(temp, "%s_DEV", ucolourcard[card]); | |
670 | replacekeyvalue(kv, temp, ""); | |
671 | sprintf(temp, "%s_MACADDR", ucolourcard[card]); | |
672 | replacekeyvalue(kv, temp, ""); | |
673 | sprintf(temp, "%s_DESCRIPTION", ucolourcard[card]); | |
674 | replacekeyvalue(kv, temp, ""); | |
675 | ||
676 | writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings"); | |
677 | freekeyvalues(kv); | |
678 | ||
679 | return 0; | |
680 | } | |
681 | ||
682 | int ask_clear_card_entry(int card) | |
683 | { | |
684 | char message[STRING_SIZE]; | |
685 | int rc; | |
686 | ||
687 | sprintf(message, ctr[TR_REMOVE_CARD], ucolourcard[card]); | |
688 | rc = newtWinChoice(ctr[TR_WARNING], ctr[TR_OK], ctr[TR_CANCEL], message); | |
689 | ||
690 | if ( rc = 0 || rc == 1) { | |
691 | clear_card_entry(card); | |
692 | } else return 1; | |
693 | ||
694 | return 0; | |
695 | } | |
696 | ||
697 | /* Manual entry for gurus. */ | |
698 | int manualdriver(char *driver, char *driveroptions) | |
699 | { | |
700 | char *values[] = { NULL, NULL }; /* pointers for the values. */ | |
701 | struct newtWinEntry entries[] = | |
702 | { { "", &values[0], 0,}, { NULL, NULL, 0 } }; | |
703 | int rc; | |
704 | char commandstring[STRING_SIZE]; | |
705 | char *driverend; | |
706 | ||
707 | strcpy(driver, ""); | |
708 | strcpy(driveroptions, ""); | |
709 | ||
710 | rc = newtWinEntries(ctr[TR_SELECT_NETWORK_DRIVER], | |
711 | ctr[TR_MODULE_PARAMETERS], 50, 5, 5, 40, entries, | |
712 | ctr[TR_OK], ctr[TR_CANCEL], NULL); | |
713 | if (rc == 0 || rc == 1) | |
714 | { | |
715 | if (strlen(values[0])) | |
716 | { | |
717 | sprintf(commandstring, "/sbin/modprobe %s", values[0]); | |
718 | if (runcommandwithstatus(commandstring, ctr[TR_LOADING_MODULE]) == 0) | |
719 | { | |
720 | if ((driverend = strchr(values[0], ' '))) | |
721 | { | |
722 | *driverend = '\0'; | |
723 | strcpy(driver, values[0]); | |
724 | strcpy(driveroptions, driverend + 1); | |
725 | } | |
726 | else | |
727 | { | |
728 | strcpy(driver, values[0]); | |
729 | strcpy(driveroptions, ""); | |
730 | } | |
731 | } | |
732 | else | |
733 | errorbox(ctr[TR_UNABLE_TO_LOAD_DRIVER_MODULE]); | |
734 | } | |
735 | else | |
736 | errorbox(ctr[TR_MODULE_NAME_CANNOT_BE_BLANK]); | |
737 | } | |
738 | free(values[0]); | |
739 | ||
740 | return 1; | |
741 | } |