]>
Commit | Line | Data |
---|---|---|
7064bbd9 MT |
1 | /* SmoothWall libsmooth. |
2 | * | |
3 | * This program is distributed under the terms of the GNU General Public | |
4 | * Licence. See the file COPYING for details. | |
5 | * | |
6 | * (c) Lawrence Manning, 2001 | |
7 | * Contains network library functions. | |
8 | * | |
9 | */ | |
10 | ||
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 | /* Workaround for a bug that dhcp radiobutton also end the dialog at arm | |
226 | */ | |
227 | else { | |
228 | if (es.u.co != cancel) { | |
229 | error = 1; | |
230 | } | |
231 | } | |
232 | } | |
233 | while (error); | |
234 | ||
235 | newtFormDestroy(networkform); | |
236 | newtPopWindow(); | |
237 | ||
238 | return result; | |
239 | } | |
240 | ||
241 | /* for pppoe: return string thats type STATIC, DHCP or PPPOE */ | |
242 | int gettype(char *type) | |
243 | { | |
244 | newtComponent selected = newtRadioGetCurrent(statictyperadio); | |
245 | ||
246 | if (selected == statictyperadio) | |
247 | strcpy(type, "STATIC"); | |
248 | else if (selected == dhcptyperadio) | |
249 | strcpy(type, "DHCP"); | |
250 | else if (selected == pppoetyperadio) | |
251 | strcpy(type, "PPPOE"); | |
252 | else | |
253 | strcpy(type, "ERROR"); | |
254 | ||
255 | return 0; | |
256 | } | |
257 | ||
258 | /* 0.9.9: calculates broadcast too. */ | |
259 | int setnetaddress(struct keyvalue *kv, char *colour) | |
260 | { | |
261 | char addressfield[STRING_SIZE]; | |
262 | char netaddressfield[STRING_SIZE]; | |
263 | char netmaskfield[STRING_SIZE]; | |
264 | char broadcastfield[STRING_SIZE]; | |
265 | char address[STRING_SIZE]; | |
266 | char netmask[STRING_SIZE]; | |
267 | unsigned long int intaddress; | |
268 | unsigned long int intnetaddress; | |
269 | unsigned long int intnetmask; | |
270 | unsigned long int intbroadcast; | |
271 | struct in_addr temp; | |
272 | char *netaddress; | |
273 | char *broadcast; | |
274 | ||
275 | /* Build some key strings. */ | |
276 | sprintf(addressfield, "%s_ADDRESS", colour); | |
277 | sprintf(netaddressfield, "%s_NETADDRESS", colour); | |
278 | sprintf(netmaskfield, "%s_NETMASK", colour); | |
279 | sprintf(broadcastfield, "%s_BROADCAST", colour); | |
280 | ||
281 | strcpy(address, ""); findkey(kv, addressfield, address); | |
282 | strcpy(netmask, ""); findkey(kv, netmaskfield, netmask); | |
283 | ||
284 | /* Calculate netaddress. Messy.. */ | |
285 | intaddress = inet_addr(address); | |
286 | intnetmask = inet_addr(netmask); | |
287 | ||
288 | intnetaddress = intaddress & intnetmask; | |
289 | temp.s_addr = intnetaddress; | |
290 | netaddress = inet_ntoa(temp); | |
291 | ||
292 | replacekeyvalue(kv, netaddressfield, netaddress); | |
293 | ||
294 | intbroadcast = intnetaddress | ~intnetmask; | |
295 | temp.s_addr = intbroadcast; | |
296 | broadcast = inet_ntoa(temp); | |
297 | ||
298 | replacekeyvalue(kv, broadcastfield, broadcast); | |
299 | ||
300 | return 1; | |
301 | } | |
302 | ||
303 | /* Called when dhcp flag is toggled. Toggle disabled state of other 3 | |
304 | * controls. */ | |
305 | void networkdialogcallbacktype(newtComponent cm, void *data) | |
306 | { | |
307 | char type[STRING_SIZE]; | |
308 | ||
309 | gettype(type); | |
310 | ||
311 | if (strcmp(type, "STATIC") != 0) | |
312 | { | |
313 | newtEntrySetFlags(addressentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET); | |
314 | newtEntrySetFlags(netmaskentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET); | |
315 | } | |
316 | else | |
317 | { | |
318 | newtEntrySetFlags(addressentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_RESET); | |
319 | newtEntrySetFlags(netmaskentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_RESET); | |
320 | } | |
321 | if (strcmp(type, "DHCP") == 0) | |
322 | { | |
323 | newtEntrySetFlags(dhcphostnameentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_RESET); | |
324 | newtEntrySetFlags(dhcpforcemtuentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_RESET); | |
325 | } | |
326 | else | |
327 | { | |
328 | newtEntrySetFlags(dhcphostnameentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET); | |
329 | newtEntrySetFlags(dhcpforcemtuentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET); | |
330 | } | |
331 | newtRefresh(); | |
332 | newtDrawForm(networkform); | |
333 | } | |
334 | ||
335 | int interfacecheck(struct keyvalue *kv, char *colour) | |
336 | { | |
337 | char temp[STRING_SIZE]; | |
338 | char colourfields[NETCHANGE_TOTAL][STRING_SIZE]; | |
339 | int c; | |
340 | ||
341 | sprintf(colourfields[ADDRESS], "%s_ADDRESS", colour); | |
342 | sprintf(colourfields[NETADDRESS], "%s_NETADDRESS", colour); | |
343 | sprintf(colourfields[NETMASK], "%s_NETMASK", colour); | |
344 | ||
345 | for (c = 0; c < 3; c++) | |
346 | { | |
347 | strcpy(temp, ""); findkey(kv, colourfields[c], temp); | |
348 | if (!(strlen(temp))) return 0; | |
349 | } | |
350 | return 1; | |
351 | } | |
352 | ||
353 | /* Funky routine for loading all drivers (cept those are already loaded.). */ | |
354 | int probecards(char *driver, char *driveroptions ) | |
355 | { | |
356 | return 0; | |
357 | } | |
358 | ||
359 | int get_knic(int card) //returns "0" for zero cards or error and "1" card is found. | |
360 | { | |
361 | struct keyvalue *kv = initkeyvalues(); | |
362 | char temp[STRING_SIZE], searchstr[STRING_SIZE]; | |
363 | int ret_value; | |
364 | ||
365 | if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings"))) | |
366 | { | |
367 | freekeyvalues(kv); | |
368 | errorbox(ctr[TR_UNABLE_TO_OPEN_SETTINGS_FILE]); | |
369 | return 0; | |
370 | } | |
371 | ||
372 | sprintf(searchstr, "%s_MACADDR", ucolourcard[card]); | |
373 | strcpy(temp, ""); findkey(kv, searchstr, temp); | |
374 | if (strlen(temp)) { | |
375 | strcpy(knics[ card ].macaddr, temp); | |
376 | strcpy(knics[ card ].colour, ucolourcard[card]); | |
377 | ||
378 | sprintf(searchstr, "%s_DESCRIPTION", ucolourcard[card]); | |
379 | findkey(kv, searchstr, temp); | |
380 | strcpy(knics[ card ].description, temp); | |
381 | ||
382 | sprintf(searchstr, "%s_DRIVER", ucolourcard[card]); | |
383 | findkey(kv, searchstr, temp); | |
384 | strcpy(knics[ card ].driver, temp); | |
385 | ret_value = 1; | |
386 | } else { | |
387 | strcpy(knics[ card ].description, ctr[TR_UNSET]); | |
388 | ret_value = 0; | |
389 | } | |
390 | freekeyvalues(kv); | |
391 | ||
392 | return ret_value; | |
393 | } | |
394 | ||
395 | int init_knics(void) | |
396 | { | |
397 | int found = 0; | |
398 | found += get_knic(_GREEN_CARD_); | |
399 | found += get_knic(_RED_CARD_); | |
400 | found += get_knic(_ORANGE_CARD_); | |
401 | found += get_knic(_BLUE_CARD_); | |
402 | ||
403 | return found; | |
404 | } | |
405 | ||
406 | int fmt_exists(const char *fname) { /* Check if it is any file or directory */ | |
407 | struct stat st; | |
408 | if (stat(fname, &st) == -1) return 0; | |
409 | else return 1; | |
410 | } | |
411 | ||
412 | int is_interface_up(char *card) { /* Check if the interface is UP */ | |
413 | char temp[STRING_SIZE]; | |
414 | ||
415 | sprintf(temp,"ip link show dev %s | grep -q UP", card); | |
416 | if (mysystem(temp)) return 0; else return 1; | |
417 | } | |
418 | ||
419 | int rename_device(char *old_name, char *new_name) { | |
420 | char temp[STRING_SIZE]; | |
421 | ||
422 | sprintf(temp,SYSDIR "/%s", old_name); | |
423 | if (!(fmt_exists(temp))) { | |
424 | fprintf(flog,"Device not found: %s\n",old_name); | |
425 | return 0; | |
426 | } | |
427 | sprintf(temp,"/sbin/ip link set dev %s name %s",old_name ,new_name ); | |
428 | mysystem(temp); | |
429 | ||
430 | return 1; | |
431 | } | |
432 | ||
433 | char g_temp[STRING_SIZE]=""; | |
434 | char* readmac(char *card) { | |
435 | FILE *fp; | |
436 | char temp[STRING_SIZE], mac[20]; | |
437 | ||
438 | sprintf(temp,"/sys/class/net/%s/address",card); | |
439 | if( (fp = fopen(temp, "r")) == NULL ) { | |
440 | fprintf(flog,"Couldn't open: %s\n",temp); | |
441 | return NULL; | |
442 | } | |
443 | fgets(mac, 18, fp); | |
444 | strtok(mac,"\n"); | |
445 | fclose(fp); | |
446 | strcpy(g_temp, mac); | |
447 | return g_temp; | |
448 | } | |
449 | ||
450 | char* find_nic4mac(char *findmac) { | |
451 | DIR *dir; | |
452 | struct dirent *dirzeiger; | |
453 | char temp[STRING_SIZE], temp2[STRING_SIZE]; | |
454 | ||
455 | if((dir=opendir(SYSDIR)) == NULL) { | |
456 | fprintf(flog,"Fehler bei opendir (find_name4nic) ...\n"); | |
457 | return NULL; | |
458 | } | |
459 | ||
460 | sprintf(temp, ""); | |
461 | while((dirzeiger=readdir(dir)) != NULL) { | |
462 | if(*((*dirzeiger).d_name) != '.' & strcmp(((*dirzeiger).d_name), "lo") != 0) { | |
463 | sprintf(temp2, "%s", readmac((*dirzeiger).d_name) ); | |
464 | if (strcmp(findmac, temp2) == 0) { | |
465 | sprintf(temp,"%s", (*dirzeiger).d_name); | |
466 | break; | |
467 | } | |
468 | } | |
469 | } | |
470 | ||
471 | if(closedir(dir) == -1) fprintf(flog,"Fehler beim schliessen von %s\n", SYSDIR); | |
472 | strcpy(g_temp, temp); | |
473 | return g_temp; | |
474 | } | |
475 | ||
476 | int nic_shutdown(char *nic) { | |
477 | char temp[STRING_SIZE]; | |
478 | ||
479 | sprintf(temp,"ip link set %s down", nic); | |
480 | mysystem(temp); | |
481 | } | |
482 | ||
483 | int nic_startup(char *nic) { | |
484 | char temp[STRING_SIZE]; | |
485 | ||
486 | sprintf(temp,"ip link set %s up", nic); | |
487 | mysystem(temp); | |
488 | ||
489 | } | |
490 | ||
491 | int rename_nics(void) { | |
492 | int i, j, k; | |
493 | int fnics = scan_network_cards(); | |
494 | char nic2find[STRING_SIZE], temp[STRING_SIZE]; | |
495 | ||
496 | for(i=0; i<4; i++) | |
497 | if (strcmp(knics[i].macaddr, "")) | |
498 | for(j=0; j<fnics; j++) | |
499 | if(strcmp(knics[i].macaddr, nics[j].macaddr) == 0) { | |
500 | sprintf(nic2find,"%s0",lcolourcard[i]); | |
501 | if(strcmp(nic2find, nics[j].nic)) { | |
502 | if(is_interface_up(nics[j].nic)) { | |
503 | nic_shutdown(nics[j].nic); | |
504 | } | |
505 | sprintf(temp,SYSDIR "/%s", nic2find); | |
506 | if(fmt_exists(temp)) { | |
507 | for(k=0; k<fnics; k++) | |
508 | if (strcmp(nics[k].nic, nic2find) == 0 ) { | |
509 | if(is_interface_up(nics[k].nic)) { | |
510 | nic_shutdown(nics[k].nic); | |
511 | } | |
512 | sprintf(temp,"dummy%i",k); | |
513 | if (rename_device(nics[k].nic, temp)) strcpy(nics[k].nic, temp); | |
514 | } | |
515 | } | |
516 | if (rename_device(nics[j].nic, nic2find)) strcpy(nics[j].nic, nic2find); | |
517 | } | |
518 | } | |
519 | } | |
520 | ||
521 | int create_udev(void) | |
522 | { | |
523 | #define UDEV_NET_CONF "/etc/udev/rules.d/30-persistent-network.rules" | |
524 | FILE *fp; | |
525 | int i; | |
526 | ||
527 | if ( (fp = fopen(UDEV_NET_CONF, "w")) == NULL ) { | |
528 | fprintf(stderr,"Couldn't open" UDEV_NET_CONF); | |
529 | return 1; | |
530 | } | |
531 | ||
532 | for (i = 0 ; i < 4 ; i++) | |
533 | { | |
534 | if (strcmp(knics[i].macaddr, "")) { | |
535 | fprintf(fp,"\n# %s\nACTION==\"add\", SUBSYSTEM==\"net\", ATTR{type}==\"1\", ATTR{address}==\"%s\", NAME=\"%s0\"\n", knics[i].description, knics[i].macaddr, lcolourcard[i]); | |
536 | } | |
537 | } | |
538 | fclose(fp); | |
539 | return 0; | |
540 | } | |
541 | ||
542 | int write_configs_netudev(int card , int colour) | |
543 | { | |
544 | char commandstring[STRING_SIZE]; | |
545 | struct keyvalue *kv = initkeyvalues(); | |
546 | char temp1[STRING_SIZE], temp2[STRING_SIZE], temp3[STRING_SIZE]; | |
547 | char ucolour[STRING_SIZE]; | |
548 | ||
549 | sprintf(ucolour, ucolourcard[colour]); | |
550 | strcpy(knics[colour].driver, nics[card].driver); | |
551 | strcpy(knics[colour].description, nics[card].description); | |
552 | strcpy(knics[colour].macaddr, nics[card].macaddr); | |
553 | ||
554 | if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings"))) | |
555 | { | |
556 | freekeyvalues(kv); | |
557 | errorbox(ctr[TR_UNABLE_TO_OPEN_SETTINGS_FILE]); | |
558 | return 0; | |
559 | } | |
560 | ||
561 | sprintf(temp1, "%s_DEV", ucolour); | |
562 | sprintf(temp2, "%s_MACADDR", ucolour); | |
563 | sprintf(temp3, "%s0", lcolourcard[colour]); | |
564 | replacekeyvalue(kv, temp1, temp3); | |
565 | replacekeyvalue(kv, temp2, nics[card].macaddr); | |
566 | sprintf(temp1, "%s_DESCRIPTION", ucolour); | |
567 | replacekeyvalue(kv, temp1, nics[card].description); | |
568 | sprintf(temp1, "%s_DRIVER", ucolour); | |
569 | replacekeyvalue(kv, temp1, nics[card].driver); | |
570 | ||
571 | writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings"); | |
572 | freekeyvalues(kv); | |
573 | ||
574 | return 0; | |
575 | } | |
576 | ||
577 | int scan_network_cards(void) | |
578 | { | |
579 | FILE *fp; | |
580 | char driver[STRING_SIZE], description[STRING_SIZE], macaddr[STRING_SIZE], temp_line[STRING_SIZE]; | |
581 | int count = 0; | |
582 | const char _driver[]="driver: "; | |
583 | const char _desc[]="desc: "; | |
584 | const char _network_hwaddr[]="network.hwaddr: "; | |
585 | ||
586 | if (!(scanned_nics_read_done)) | |
587 | { | |
588 | mysystem("/bin/probenic.sh"); | |
589 | if( (fp = fopen(SCANNED_NICS, "r")) == NULL ) | |
590 | { | |
591 | fprintf(stderr,"Couldn't open "SCANNED_NICS); | |
592 | return 1; | |
593 | } | |
594 | while (fgets(temp_line, STRING_SIZE, fp) != NULL) | |
595 | { | |
596 | temp_line[strlen(temp_line) -1] = 0; | |
597 | if ( strncmp(temp_line, _driver, strlen(_driver)) == 0 ) sprintf(nics[count].driver, "%s", temp_line+strlen(_driver)); | |
598 | if ( strncmp(temp_line, _desc, strlen(_desc)) == 0 ) sprintf(nics[count].description, "%s", temp_line+strlen(_desc)); | |
599 | if ( strncmp(temp_line, _network_hwaddr, strlen(_network_hwaddr)) == 0 ) sprintf(nics[count].macaddr, "%s", temp_line+strlen(_network_hwaddr)); | |
600 | if (strlen(nics[count].macaddr) > 15 ) { | |
601 | sprintf(nics[count].nic, "%s", find_nic4mac(nics[count].macaddr)); | |
602 | count++; | |
603 | } | |
604 | } | |
605 | fclose(fp); | |
606 | scanned_nics_read_done = count; | |
607 | } else fprintf(flog,"Scan Networkcards does read.\n"); | |
608 | return scanned_nics_read_done; | |
609 | } | |
610 | ||
611 | ||
612 | ||
613 | int nicmenu(int colour) | |
614 | { | |
615 | int rc, choise = 0, count = 0, kcount = 0, mcount = 0, i, j, nic_in_use; | |
616 | int found_NIC_as_Card[4]; | |
617 | char message[STRING_SIZE]; | |
618 | char temp[STRING_SIZE]; | |
619 | ||
620 | char cMenuInhalt[STRING_SIZE]; | |
621 | char MenuInhalt[20][180]; | |
622 | char *pMenuInhalt[20]; | |
623 | ||
624 | while (strcmp(nics[count].macaddr, "")) count++; // 2 find how many nics in system | |
625 | for ( i=0 ; i<4;i++) if (strcmp(knics[i].macaddr, "")) kcount++; // loop to find all knowing nics | |
626 | ||
627 | // If new nics are found... | |
628 | if (count > kcount) { | |
629 | for (i=0 ; i < count ; i++) | |
630 | { | |
631 | nic_in_use = 0; | |
632 | for (j=0 ; j <= kcount ; j++) { | |
633 | if (strcmp(nics[ i ].macaddr, knics[ j ].macaddr) == 0 ) { | |
634 | nic_in_use = 1; | |
635 | break; | |
636 | } | |
637 | } | |
638 | if (!(nic_in_use)) { | |
639 | if ( strlen(nics[i].description) < 55 ) | |
640 | sprintf(MenuInhalt[mcount], "%.*s", strlen(nics[i].description)-2, nics[i].description+1); | |
641 | else { | |
642 | sprintf(cMenuInhalt, "%.50s", nics[i].description + 1); | |
643 | sprintf(MenuInhalt[mcount], cMenuInhalt); | |
644 | strcat (MenuInhalt[mcount], "..."); | |
645 | } | |
646 | ||
647 | while ( strlen(MenuInhalt[mcount]) < 53) strcat(MenuInhalt[mcount], " "); // Fill with space. | |
648 | ||
649 | strcat(MenuInhalt[mcount], " ("); | |
650 | strcat(MenuInhalt[mcount], nics[i].macaddr); | |
651 | strcat(MenuInhalt[mcount], ")"); | |
652 | pMenuInhalt[mcount] = MenuInhalt[mcount]; | |
653 | found_NIC_as_Card[mcount]=i; | |
654 | mcount++; | |
655 | } | |
656 | } | |
657 | ||
658 | pMenuInhalt[mcount] = NULL; | |
659 | ||
660 | sprintf(message, ctr[TR_CHOOSE_NETCARD], ucolourcard[colour]); | |
661 | rc=2; | |
662 | while ( rc == 2 ) { | |
663 | rc = newtWinMenu( ctr[TR_NETCARDMENU2], message, 50, 5, 5, 6, pMenuInhalt, &choise, ctr[TR_SELECT], ctr[TR_IDENTIFY], ctr[TR_CANCEL], NULL); | |
664 | if ( rc == 2 ) { | |
665 | sprintf(temp, "/sbin/ip link set %s up", nics[found_NIC_as_Card[choise]].nic); | |
666 | mysystem(temp); | |
667 | sprintf(temp, "/usr/sbin/ethtool -p %s 10", nics[found_NIC_as_Card[choise]].nic); | |
668 | if (runcommandwithstatus(temp,ctr[TR_IDENTIFY_SHOULD_BLINK]) != 0) { | |
669 | errorbox(ctr[TR_IDENTIFY_NOT_SUPPORTED]); | |
670 | sprintf(temp, "/sbin/ip link set %s down", nics[found_NIC_as_Card[choise]].nic); | |
671 | mysystem(temp); | |
672 | } | |
673 | } | |
674 | } | |
675 | if ( rc == 0 || rc == 1) { | |
676 | write_configs_netudev(found_NIC_as_Card[choise], colour); | |
677 | } | |
678 | return 0; | |
679 | } else { | |
680 | // We have to add here that you can manually add a device | |
681 | errorbox( ctr[TR_ERROR_INTERFACES]); | |
682 | return 1; | |
683 | } | |
684 | } | |
685 | ||
686 | int clear_card_entry(int card) | |
687 | { | |
688 | struct keyvalue *kv = initkeyvalues(); | |
689 | char temp[STRING_SIZE]; | |
690 | ||
691 | if (!(readkeyvalues(kv, CONFIG_ROOT "/ethernet/settings"))) | |
692 | { | |
693 | freekeyvalues(kv); | |
694 | errorbox(ctr[TR_UNABLE_TO_OPEN_SETTINGS_FILE]); | |
695 | return 0; | |
696 | } | |
697 | ||
698 | strcpy(knics[card].driver, ""); | |
699 | strcpy(knics[card].description, ctr[TR_UNSET]); | |
700 | strcpy(knics[card].macaddr, ""); | |
701 | strcpy(knics[card].colour, ""); | |
702 | sprintf(temp, "%s_DRIVER", ucolourcard[card]); | |
703 | replacekeyvalue(kv, temp, ""); | |
704 | sprintf(temp, "%s_DEV", ucolourcard[card]); | |
705 | replacekeyvalue(kv, temp, ""); | |
706 | sprintf(temp, "%s_MACADDR", ucolourcard[card]); | |
707 | replacekeyvalue(kv, temp, ""); | |
708 | sprintf(temp, "%s_DESCRIPTION", ucolourcard[card]); | |
709 | replacekeyvalue(kv, temp, ""); | |
710 | ||
711 | writekeyvalues(kv, CONFIG_ROOT "/ethernet/settings"); | |
712 | freekeyvalues(kv); | |
713 | ||
714 | return 0; | |
715 | } | |
716 | ||
717 | int ask_clear_card_entry(int card) | |
718 | { | |
719 | char message[STRING_SIZE]; | |
720 | int rc; | |
721 | ||
722 | sprintf(message, ctr[TR_REMOVE_CARD], ucolourcard[card]); | |
723 | rc = newtWinChoice(ctr[TR_WARNING], ctr[TR_OK], ctr[TR_CANCEL], message); | |
724 | ||
725 | if ( rc = 0 || rc == 1) { | |
726 | clear_card_entry(card); | |
727 | } else return 1; | |
728 | ||
729 | return 0; | |
730 | } | |
731 | ||
732 | /* Manual entry for gurus. */ | |
733 | int manualdriver(char *driver, char *driveroptions) | |
734 | { | |
735 | char *values[] = { NULL, NULL }; /* pointers for the values. */ | |
736 | struct newtWinEntry entries[] = | |
737 | { { "", &values[0], 0,}, { NULL, NULL, 0 } }; | |
738 | int rc; | |
739 | char commandstring[STRING_SIZE]; | |
740 | char *driverend; | |
741 | ||
742 | strcpy(driver, ""); | |
743 | strcpy(driveroptions, ""); | |
744 | ||
745 | rc = newtWinEntries(ctr[TR_SELECT_NETWORK_DRIVER], | |
746 | ctr[TR_MODULE_PARAMETERS], 50, 5, 5, 40, entries, | |
747 | ctr[TR_OK], ctr[TR_CANCEL], NULL); | |
748 | if (rc == 0 || rc == 1) | |
749 | { | |
750 | if (strlen(values[0])) | |
751 | { | |
752 | sprintf(commandstring, "/sbin/modprobe %s", values[0]); | |
753 | if (runcommandwithstatus(commandstring, ctr[TR_LOADING_MODULE]) == 0) | |
754 | { | |
755 | if ((driverend = strchr(values[0], ' '))) | |
756 | { | |
757 | *driverend = '\0'; | |
758 | strcpy(driver, values[0]); | |
759 | strcpy(driveroptions, driverend + 1); | |
760 | } | |
761 | else | |
762 | { | |
763 | strcpy(driver, values[0]); | |
764 | strcpy(driveroptions, ""); | |
765 | } | |
766 | } | |
767 | else | |
768 | errorbox(ctr[TR_UNABLE_TO_LOAD_DRIVER_MODULE]); | |
769 | } | |
770 | else | |
771 | errorbox(ctr[TR_MODULE_NAME_CANNOT_BE_BLANK]); | |
772 | } | |
773 | free(values[0]); | |
774 | ||
775 | return 1; | |
776 | } |