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