]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - tmp/nic.c
31602e0bccab9df9fe902a47d572bdfcdf7a198b
1 #include <newt.h> /* Fenster */
2 #include <stdio.h> /* File IO */
3 #include <stdlib.h> /* exit() */
4 #include <string.h> /* Strings */
6 #define STRING_SIZE 256
8 #define KNOWN_NICS "/var/ipfire/ethernet/known_nics"
9 #define SCANNED_NICS "/tmp/scanned_nics"
13 char description
[256];
25 char temp_line
[STRING_SIZE
];
26 struct nic nics
[20], *pnics
;
27 struct knic knics
[20];
29 int rc
, choise
, count
= 0, kcount
= 0, i
, found
;
30 char macaddr
[STRING_SIZE
], description
[STRING_SIZE
];
31 char message
[STRING_SIZE
];
33 char MenuInhalt
[20][80];
34 char *pMenuInhalt
[20];
38 // Read the nics we already use
39 if( (fp
= fopen(KNOWN_NICS
, "r")) == NULL
)
41 fprintf(stderr
,"Couldn't open " KNOWN_NICS
);
45 while ( fgets(temp_line
, STRING_SIZE
, fp
) != NULL
)
47 strcpy( knics
[kcount
].macaddr
, strtok(temp_line
,";") );
48 if (strlen(knics
[kcount
].macaddr
) > 5 ) kcount
++;
52 // Read our scanned nics
53 if( (fp
= fopen(SCANNED_NICS
, "r")) == NULL
)
55 fprintf(stderr
,"Couldn't open "SCANNED_NICS
);
58 while ( fgets(temp_line
, STRING_SIZE
, fp
) != NULL
)
60 strcpy(description
, strtok(temp_line
,";") );
61 strcpy(macaddr
, strtok(NULL
,";") );
63 if (strlen(macaddr
) > 5 ) {
64 for (i
=0; i
< kcount
; i
++)
66 // Check if the nic is already in use
67 if ( strcmp(knics
[i
].macaddr
, macaddr
) == NULL
)
74 strcpy( pnics
[count
].description
, description
);
75 strcpy( pnics
[count
].macaddr
, macaddr
);
82 // If new nics are found...
87 char cMenuInhalt
[STRING_SIZE
];
88 for (i
=0 ; i
< count
; i
++)
90 if ( strlen(nics
[i
].description
) < 52 )
91 strncpy(MenuInhalt
[i
], nics
[i
].description
+ 1, strlen(nics
[i
].description
)- 2);
94 strncpy(cMenuInhalt
, nics
[i
].description
+ 1, 50);
95 strncpy(MenuInhalt
[i
], cMenuInhalt
,(strrchr(cMenuInhalt
,' ') - cMenuInhalt
));
96 strcat (MenuInhalt
[i
], "...");
98 while ( strlen(MenuInhalt
[i
]) < 50)
100 strcat( MenuInhalt
[i
], " ");
102 strcat(MenuInhalt
[i
], " (");
103 strcat(MenuInhalt
[i
], nics
[i
].macaddr
);
104 strcat(MenuInhalt
[i
], ")");
105 pMenuInhalt
[i
] = MenuInhalt
[i
];
108 form
= newtForm(NULL
, NULL
, 0);
110 sprintf(message
, "Es wurde(n) %d Netzwerkkarte(n)\nin Ihrem System gefunden.\nBitte waehlen Sie eine aus:\n", count
);
112 rc
= newtWinMenu("NetcardMenu", message
, 50, 5, 5, 6, pMenuInhalt
, &choise
, "OK", "Cancel", NULL
);
114 newtFormDestroy(form
);
117 if ( rc
== 0 || rc
== 1) {
118 write_configs_netudev(pnics
[choise
].macaddr
, "green");
125 printf("Es wurden keine ungenutzen Netzwerkschnittstellen gefunden.\n");
130 int write_configs_netudev(char *macaddr
, char *colour
) {
132 #define UDEV_NET_CONF "/etc/udev/rules.d/30-persistent-network.rules"
135 char commandstring
[STRING_SIZE
];
137 if( (fp
= fopen(KNOWN_NICS
, "a")) == NULL
)
139 fprintf(stderr
,"Couldn't open "KNOWN_NICS
);
142 fprintf(fp
,"%s;\n", macaddr
);
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
);
153 if( (fp
= fopen(UDEV_NET_CONF
, "a")) == NULL
)
155 fprintf(stderr
,"Couldn't open" UDEV_NET_CONF
);
158 fprintf(fp
,"ACTION==\"add\", SUBSYSTEM==\"net\", SYSFS{address}==\"%s\", NAME=\"%s0\"\n", macaddr
, colour
);