]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - tmp/nic.c
38d4d66946462e3ebf06614b1e64f02783bdd8fa
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];
22 int write_configs_netudev(char *macaddr
, char *colour
) {
24 #define UDEV_NET_CONF "/etc/udev/rules.d/30-persistent-network.rules"
27 char commandstring
[STRING_SIZE
];
29 if( (fp
= fopen(KNOWN_NICS
, "a")) == NULL
)
31 fprintf(stderr
,"Couldn't open "KNOWN_NICS
);
34 fprintf(fp
,"%s;\n", macaddr
);
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
);
45 if( (fp
= fopen(UDEV_NET_CONF
, "a")) == NULL
)
47 fprintf(stderr
,"Couldn't open" UDEV_NET_CONF
);
50 fprintf(fp
,"ACTION==\"add\", SUBSYSTEM==\"net\", SYSFS{address}==\"%s\", NAME=\"%s0\"\n", macaddr
, colour
);
59 char temp_line
[STRING_SIZE
];
60 struct nic nics
[20], *pnics
;
62 struct knic knics
[20], *pknics
;
64 int rc
, choise
, count
= 0, kcount
= 0, i
, found
;
65 char macaddr
[STRING_SIZE
], description
[STRING_SIZE
];
66 char message
[STRING_SIZE
];
68 char MenuInhalt
[20][80];
69 char *pMenuInhalt
[20];
73 // Read the nics we already use
74 if( (fp
= fopen(KNOWN_NICS
, "r")) == NULL
)
76 fprintf(stderr
,"Couldn't open " KNOWN_NICS
);
80 while ( fgets(temp_line
, STRING_SIZE
, fp
) != NULL
)
82 strcpy( knics
[kcount
].macaddr
, strtok(temp_line
,";") );
83 if (strlen(knics
[kcount
].macaddr
) > 5 ) kcount
++;
87 // Read our scanned nics
88 if( (fp
= fopen(SCANNED_NICS
, "r")) == NULL
)
90 fprintf(stderr
,"Couldn't open "SCANNED_NICS
);
93 while ( fgets(temp_line
, STRING_SIZE
, fp
) != NULL
)
95 strcpy(description
, strtok(temp_line
,";") );
96 strcpy(macaddr
, strtok(NULL
,";") );
98 if (strlen(macaddr
) > 5 ) {
99 for (i
=0; i
< kcount
; i
++)
101 // Check if the nic is already in use
102 if (strcmp(pknics
[i
].macaddr
, macaddr
) == NULL
)
109 strcpy( pnics
[count
].description
, description
);
110 strcpy( pnics
[count
].macaddr
, macaddr
);
117 // If new nics are found...
122 char cMenuInhalt
[STRING_SIZE
];
123 for (i
=0 ; i
< count
; i
++)
125 if ( strlen(nics
[i
].description
) < 52 )
126 strncpy(MenuInhalt
[i
], nics
[i
].description
+ 1, strlen(nics
[i
].description
)- 2);
129 strncpy(cMenuInhalt
, nics
[i
].description
+ 1, 50);
130 strncpy(MenuInhalt
[i
], cMenuInhalt
,(strrchr(cMenuInhalt
,' ') - cMenuInhalt
));
131 strcat (MenuInhalt
[i
], "...");
133 while ( strlen(MenuInhalt
[i
]) < 50)
135 strcat( MenuInhalt
[i
], " ");
137 strcat(MenuInhalt
[i
], " (");
138 strcat(MenuInhalt
[i
], nics
[i
].macaddr
);
139 strcat(MenuInhalt
[i
], ")");
140 pMenuInhalt
[i
] = MenuInhalt
[i
];
143 form
= newtForm(NULL
, NULL
, 0);
145 sprintf(message
, "Es wurde(n) %d Netzwerkkarte(n)\nin Ihrem System gefunden.\nBitte waehlen Sie eine aus:\n", count
);
147 rc
= newtWinMenu("NetcardMenu", message
, 50, 5, 5, 6, pMenuInhalt
, &choise
, "OK", "Cancel", NULL
);
149 newtFormDestroy(form
);
152 if ( rc
== 0 || rc
== 1) {
153 write_configs_netudev(pnics
[choise
].macaddr
, "green");
160 printf("Es wurden keine ungenutzen Netzwerkschnittstellen gefunden.\n");