"Update Booster" fertiggestellt und getestet.
[people/pmueller/ipfire-2.x.git] / tmp / nic.c
CommitLineData
46af669a
MT
1#include <newt.h> /* Fenster */
2#include <stdio.h> /* File IO */
3#include <stdlib.h> /* exit() */
4#include <string.h> /* Strings */
5
6#define STRING_SIZE 256
7
8#define KNOWN_NICS "/var/ipfire/ethernet/known_nics"
9#define SCANNED_NICS "/tmp/scanned_nics"
10
11struct nic
12{
13 char description[256];
14 char macaddr[20];
15};
16
17struct knic
18{
19 char macaddr[20];
20};
21
f4a208fe
MT
22int write_configs_netudev(char *macaddr, char *colour) {
23
24 #define UDEV_NET_CONF "/etc/udev/rules.d/30-persistent-network.rules"
25
26 FILE *fp;
27 char commandstring[STRING_SIZE];
28
29 if( (fp = fopen(KNOWN_NICS, "a")) == NULL )
30 {
31 fprintf(stderr,"Couldn't open "KNOWN_NICS);
32 return 1;
33 }
34 fprintf(fp,"%s;\n", macaddr);
35 fclose(fp);
36
37 // Make sure that there is no conflict
38 snprintf(commandstring, STRING_SIZE, "/usr/bin/touch "UDEV_NET_CONF" >/dev/null 2>&1");
39 system(commandstring);
40 snprintf(commandstring, STRING_SIZE, "/bin/cat "UDEV_NET_CONF"| /bin/grep -v \"%s\" > "UDEV_NET_CONF" 2>/dev/null", macaddr);
41 system(commandstring);
42 snprintf(commandstring, STRING_SIZE, "/bin/cat "UDEV_NET_CONF"| /bin/grep -v \"%s\" > "UDEV_NET_CONF" 2>/dev/null", colour);
43 system(commandstring);
44
45 if( (fp = fopen(UDEV_NET_CONF, "a")) == NULL )
46 {
47 fprintf(stderr,"Couldn't open" UDEV_NET_CONF);
48 return 1;
49 }
50 fprintf(fp,"ACTION==\"add\", SUBSYSTEM==\"net\", SYSFS{address}==\"%s\", NAME=\"%s0\"\n", macaddr, colour);
51 fclose(fp);
52
53 return 0;
54}
55
46af669a
MT
56int main(void)
57{
58 FILE *fp;
59 char temp_line[STRING_SIZE];
60 struct nic nics[20], *pnics;
46af669a 61 pnics = nics;
f4a208fe
MT
62 struct knic knics[20], *pknics;
63 pknics = knics;
46af669a
MT
64 int rc, choise, count = 0, kcount = 0, i, found;
65 char macaddr[STRING_SIZE], description[STRING_SIZE];
66 char message[STRING_SIZE];
67
68 char MenuInhalt[20][80];
69 char *pMenuInhalt[20];
70
71 newtComponent form;
72
73 // Read the nics we already use
74 if( (fp = fopen(KNOWN_NICS, "r")) == NULL )
75 {
76 fprintf(stderr,"Couldn't open " KNOWN_NICS);
77 return 1;
78 }
79
80 while ( fgets(temp_line, STRING_SIZE, fp) != NULL)
81 {
82 strcpy( knics[kcount].macaddr , strtok(temp_line,";") );
83 if (strlen(knics[kcount].macaddr) > 5 ) kcount++;
84 }
85 fclose(fp);
86
87 // Read our scanned nics
88 if( (fp = fopen(SCANNED_NICS, "r")) == NULL )
89 {
90 fprintf(stderr,"Couldn't open "SCANNED_NICS);
91 return 1;
92 }
93 while ( fgets(temp_line, STRING_SIZE, fp) != NULL)
94 {
95 strcpy(description, strtok(temp_line,";") );
96 strcpy(macaddr, strtok(NULL,";") );
97 found = 0;
98 if (strlen(macaddr) > 5 ) {
99 for (i=0; i < kcount; i++)
100 {
101 // Check if the nic is already in use
f4a208fe 102 if (strcmp(pknics[i].macaddr, macaddr) == NULL )
46af669a
MT
103 {
104 found = 1;
105 }
106 }
107 if (!found)
108 {
109 strcpy( pnics[count].description , description );
110 strcpy( pnics[count].macaddr , macaddr );
111 count++;
112 }
113 }
114 }
115 fclose(fp);
116
117 // If new nics are found...
118 if (count > 0) {
119 newtInit();
120 newtCls();
121
122 char cMenuInhalt[STRING_SIZE];
123 for (i=0 ; i < count ; i++)
124 {
125 if ( strlen(nics[i].description) < 52 )
126 strncpy(MenuInhalt[i], nics[i].description + 1, strlen(nics[i].description)- 2);
127 else
128 {
129 strncpy(cMenuInhalt, nics[i].description + 1, 50);
130 strncpy(MenuInhalt[i], cMenuInhalt,(strrchr(cMenuInhalt,' ') - cMenuInhalt));
131 strcat (MenuInhalt[i], "...");
132 }
133 while ( strlen(MenuInhalt[i]) < 50)
134 // Fill with space.
135 strcat( MenuInhalt[i], " ");
136
137 strcat(MenuInhalt[i], " (");
138 strcat(MenuInhalt[i], nics[i].macaddr);
139 strcat(MenuInhalt[i], ")");
140 pMenuInhalt[i] = MenuInhalt[i];
141 }
142
143 form = newtForm(NULL, NULL, 0);
144
145 sprintf(message, "Es wurde(n) %d Netzwerkkarte(n)\nin Ihrem System gefunden.\nBitte waehlen Sie eine aus:\n", count);
146
147 rc = newtWinMenu("NetcardMenu", message, 50, 5, 5, 6, pMenuInhalt, &choise, "OK", "Cancel", NULL);
148
149 newtFormDestroy(form);
150 newtFinished();
151
152 if ( rc == 0 || rc == 1) {
153 write_configs_netudev(pnics[choise].macaddr, "green");
154 } else {
155 printf("BREAK\n");
156 return 1;
157 }
158 return 0;
159 } else {
160 printf("Es wurden keine ungenutzen Netzwerkschnittstellen gefunden.\n");
161 return 1;
162 }
163}