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