|
1 /* |
|
2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * Implementation of the Netcards.exe adapter selection application. |
|
16 * This version writes settings to an ethernet.ini file only. The perl |
|
17 * script "configchange.pl" can be used to run this version of Netcards.exe |
|
18 * and extract the ethernet settings from ethernet.ini and place them in the |
|
19 * correct setup files for EKA1 (ethermac.dat and etherdriver.dat) or EKA2 (epoc.ini). |
|
20 * there is a bug in the rpcasync.h windows header file |
|
21 * where the forward declaration for type _RPC_ASYNC_STATE |
|
22 * type is locally scoped inside an extern C block, rather |
|
23 * than outside it, so we have turned this warning off, as |
|
24 * we can;t change the header file... |
|
25 * |
|
26 * |
|
27 */ |
|
28 |
|
29 |
|
30 |
|
31 /** |
|
32 @file NETCARDS.C |
|
33 @internalTechnology |
|
34 @file |
|
35 */ |
|
36 #pragma warning (disable : 4115) |
|
37 #include <windows.h> |
|
38 #pragma warning (default : 4115) |
|
39 #include <stdio.h> |
|
40 #include <malloc.h> |
|
41 #include <conio.h> |
|
42 |
|
43 |
|
44 #define EPOC_INI_FILE "ethernet.ini" |
|
45 |
|
46 #define EPOC_INI_FILE_TEMP "__temp__ethernet__.ini" |
|
47 |
|
48 #define ETHER_NIF_ENTRY "ETHER_NIF" |
|
49 #define ETHER_MAC_ENTRY "ETHER_MAC" |
|
50 #define ETHER_SPEED_ENTRY "ETHER_SPEED" |
|
51 |
|
52 #define MAX_VALUE 80 |
|
53 #define MAX_LINE 100 |
|
54 #define MAX_OID_DATA 256 |
|
55 |
|
56 #define OID_802_3_CURRENT_ADDRESS 0x01010102 |
|
57 |
|
58 |
|
59 char AdapterList[10][1024]; |
|
60 |
|
61 // replace or write new 'value' for 'entry' in epoc.ini file |
|
62 // returns 0 if ok, negative value if sth wrong |
|
63 int replace_in_inifile(char * entry, char* value, BOOL valUnicode ); |
|
64 |
|
65 |
|
66 int main(int argc, char* argv[]) |
|
67 { |
|
68 |
|
69 // Packet.lib variables: |
|
70 LPADAPTER lpAdapter = 0; |
|
71 PPACKET_OID_DATA pOidData = malloc( sizeof(PACKET_OID_DATA) + MAX_OID_DATA ); |
|
72 NetType *type = malloc(sizeof(NetType)); |
|
73 UINT speed_Mbps = 0; |
|
74 |
|
75 BOOL isWinNT = FALSE; |
|
76 int i; |
|
77 |
|
78 DWORD dwVersion; |
|
79 DWORD dwWindowsMajorVersion; |
|
80 |
|
81 |
|
82 //unicode strings (winnt) |
|
83 WCHAR AdapterName[8192]; // string that contains a list of the network adapters |
|
84 WCHAR *temp,*temp1; |
|
85 |
|
86 //ascii strings (win95) |
|
87 char AdapterNamea[8192]; // string that contains a list of the network adapters |
|
88 char *tempa,*temp1a; |
|
89 |
|
90 |
|
91 int AdapterNum=0,Open; |
|
92 unsigned long AdapterLength; |
|
93 |
|
94 |
|
95 FILE * inifile; |
|
96 |
|
97 char speed_value[10]; |
|
98 char *MAC_value = malloc( 13*sizeof(char) ); |
|
99 char *temp2 = malloc( 13*sizeof(char) ); |
|
100 |
|
101 //interfaceArg specifies the interface # passed fron configchange.pl |
|
102 //set interfaceArg to argv[1] if arguments are passed |
|
103 int interfaceArg = 0; |
|
104 if ( argc>1 && argv[1] ) |
|
105 { |
|
106 interfaceArg = atoi(argv[1]); |
|
107 } |
|
108 |
|
109 // obtain the name of the adapters installed on this machine |
|
110 //printf("Adapters installed:\n"); |
|
111 i=0; |
|
112 |
|
113 // the data returned by PacketGetAdapterNames is different in Win95 and in WinNT. |
|
114 // We have to check the os on which we are running |
|
115 dwVersion=GetVersion(); |
|
116 dwWindowsMajorVersion = (DWORD)(LOBYTE(LOWORD(dwVersion))); |
|
117 if (!(dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4)) |
|
118 { // Windows NT |
|
119 isWinNT = TRUE; |
|
120 AdapterLength=sizeof(AdapterName); |
|
121 PacketGetAdapterNames((char*)AdapterName,&AdapterLength); |
|
122 temp=AdapterName; |
|
123 temp1=AdapterName; |
|
124 while ((*temp!='\0')||(*(temp-1)!='\0')) |
|
125 { |
|
126 if (*temp=='\0') |
|
127 { |
|
128 memcpy(AdapterList[i],temp1,(temp-temp1)*2); |
|
129 temp1=temp+1; |
|
130 i++; |
|
131 } |
|
132 |
|
133 temp++; |
|
134 } |
|
135 |
|
136 AdapterNum=i; |
|
137 // we want to automate the adaptor selection process |
|
138 // but if there are more than one to choose from, we can't |
|
139 // so |
|
140 if (AdapterNum>1) |
|
141 { |
|
142 for (i=0;i<AdapterNum;i++) |
|
143 { |
|
144 wprintf(L"\n%d- %s\n",i+1,AdapterList[i]); |
|
145 } |
|
146 printf("\n"); |
|
147 } |
|
148 } |
|
149 else //windows 95 |
|
150 { |
|
151 AdapterLength=sizeof(AdapterNamea); |
|
152 PacketGetAdapterNames(AdapterNamea,&AdapterLength); |
|
153 tempa=AdapterNamea; |
|
154 temp1a=AdapterNamea; |
|
155 |
|
156 while ((*tempa!='\0')||(*(tempa-1)!='\0')) |
|
157 { |
|
158 if (*tempa=='\0') |
|
159 { |
|
160 memcpy(AdapterList[i],temp1a,tempa-temp1a); |
|
161 temp1a=tempa+1; |
|
162 i++; |
|
163 } |
|
164 tempa++; |
|
165 } |
|
166 |
|
167 AdapterNum=i; |
|
168 // we want to automate the adaptor selection process |
|
169 // but if there are more than one to choose from, we can't |
|
170 // so |
|
171 if (AdapterNum>1) |
|
172 { |
|
173 printf("Adapters installed:\n"); |
|
174 for (i=0;i<AdapterNum;i++) |
|
175 { |
|
176 printf("\n%d- %s\n",i+1,AdapterList[i]); |
|
177 } |
|
178 printf("\n"); |
|
179 } |
|
180 } |
|
181 |
|
182 // we want to automate the adaptor selection process |
|
183 // but if there are more than one to choose from, we can't |
|
184 // so |
|
185 // |
|
186 |
|
187 if (AdapterNum>1) |
|
188 { |
|
189 if ( (interfaceArg>AdapterNum)||(interfaceArg<1) ) |
|
190 { |
|
191 do |
|
192 { |
|
193 printf("Select the number of the adapter to use : ");scanf("%d",&Open); |
|
194 if (Open>AdapterNum) printf("\nThe number must be smaller than %d",AdapterNum); |
|
195 } while (Open>AdapterNum); |
|
196 } |
|
197 else |
|
198 { |
|
199 Open = interfaceArg; |
|
200 } |
|
201 } |
|
202 else |
|
203 { |
|
204 Open = AdapterNum; |
|
205 } |
|
206 |
|
207 lpAdapter = PacketOpenAdapter(AdapterList[Open-1]); |
|
208 |
|
209 |
|
210 MAC_value[0] = '\0'; |
|
211 temp2[0] = '\0'; |
|
212 speed_value[0] = '\0'; |
|
213 |
|
214 if( NULL != lpAdapter) |
|
215 { |
|
216 |
|
217 if ( TRUE == PacketGetNetType (lpAdapter, type) ) |
|
218 { |
|
219 speed_Mbps = type->LinkSpeed / 1000000; // LinkSpeed is in bits per second |
|
220 } |
|
221 else |
|
222 printf("Could not read Ethernet card's speed\n"); |
|
223 |
|
224 if ( type->LinkType != NdisMedium802_3) |
|
225 printf("NOT Ethernet802.3 card.\nNetwork Interface not supported\n"); |
|
226 else |
|
227 { |
|
228 pOidData->Oid = OID_802_3_CURRENT_ADDRESS; |
|
229 |
|
230 pOidData->Length = MAX_OID_DATA; |
|
231 |
|
232 |
|
233 if ( TRUE == PacketRequest(lpAdapter, FALSE , pOidData) ) |
|
234 { |
|
235 // get info obtained |
|
236 // printf("Physical address read: "); |
|
237 /* printf("%x %x %x %x %x %x\n", pOidData->Data[0], pOidData->Data[1], |
|
238 pOidData->Data[2],pOidData->Data[3], |
|
239 pOidData->Data[4],pOidData->Data[5] |
|
240 );*/ |
|
241 |
|
242 pOidData->Data[0] += 2; // changing address from global to local |
|
243 for( i=0; i<6; i++ ) |
|
244 { |
|
245 strcpy( temp2, MAC_value); |
|
246 if( pOidData->Data[i] > 15 ) |
|
247 // has 2 hex digits |
|
248 sprintf( MAC_value, "%s%x", temp2, pOidData->Data[i]); |
|
249 else |
|
250 sprintf( MAC_value, "%s0%x", temp2, pOidData->Data[i]); |
|
251 } |
|
252 } |
|
253 else |
|
254 printf("Failed to read physical address of Ethernet card\n"); |
|
255 } |
|
256 |
|
257 free(pOidData); |
|
258 free(type); |
|
259 |
|
260 PacketCloseAdapter( lpAdapter ); |
|
261 } |
|
262 else |
|
263 { |
|
264 // lpAdapter NULL |
|
265 printf("Problem with opening adapter (packet.lib issue)\n"); |
|
266 return (1); |
|
267 } |
|
268 |
|
269 inifile = fopen(EPOC_INI_FILE, "a"); // to create if does exist |
|
270 if ( NULL != inifile ) |
|
271 fclose(inifile); |
|
272 else |
|
273 { |
|
274 printf("Can't create or access %s.\n\n", EPOC_INI_FILE); |
|
275 return 0; |
|
276 } |
|
277 |
|
278 if ( 0 == replace_in_inifile( ETHER_NIF_ENTRY, AdapterList[Open-1], isWinNT ) ) |
|
279 printf( "Netcards using adapter %d\n", Open ); |
|
280 else |
|
281 { |
|
282 return 0; |
|
283 } |
|
284 |
|
285 |
|
286 if ( 0 != replace_in_inifile( ETHER_MAC_ENTRY, MAC_value, FALSE ) ) |
|
287 { |
|
288 printf("Couldn't write MAC address to %s file\n", EPOC_INI_FILE); |
|
289 return (1); |
|
290 } |
|
291 |
|
292 |
|
293 if( 0 != speed_Mbps ) |
|
294 sprintf( speed_value, "%dMbps", speed_Mbps); |
|
295 |
|
296 |
|
297 if ( 0 != replace_in_inifile( ETHER_SPEED_ENTRY, speed_value, FALSE ) ) |
|
298 { |
|
299 printf("Couldn't write speed value to %s file\n", EPOC_INI_FILE); |
|
300 return (1); |
|
301 } |
|
302 |
|
303 //printf("Netcards has written settings to %s.\n\n", EPOC_INI_FILE); |
|
304 |
|
305 free(MAC_value); |
|
306 free(temp2); |
|
307 return (0); |
|
308 } |
|
309 |
|
310 |
|
311 int replace_in_inifile(char * entry_str, char* value, BOOL valUnicode) |
|
312 { |
|
313 |
|
314 int err = 0; // 0 - ok, negative sth wrong |
|
315 |
|
316 int replaced = 0; |
|
317 int len = strlen(entry_str); |
|
318 |
|
319 FILE * file; |
|
320 FILE * tmp_file; |
|
321 |
|
322 char* s = malloc(MAX_LINE); |
|
323 char *line = malloc(MAX_LINE); |
|
324 |
|
325 if ( NULL == (tmp_file = fopen(EPOC_INI_FILE_TEMP, "w")) ) |
|
326 { |
|
327 printf( "Could not create '%s'\n", EPOC_INI_FILE_TEMP ); |
|
328 return -1; |
|
329 } |
|
330 |
|
331 if ( NULL == (file = fopen(EPOC_INI_FILE, "r+")) ) |
|
332 { |
|
333 fclose( tmp_file ); |
|
334 remove( EPOC_INI_FILE_TEMP ); |
|
335 printf( "Could not open '%s'\n", EPOC_INI_FILE ); |
|
336 return -1; |
|
337 } |
|
338 |
|
339 rewind(file); |
|
340 |
|
341 |
|
342 while( fgets(line, MAX_LINE, file) != NULL) |
|
343 { |
|
344 if (sscanf( line, "%s", s ) > 0) // to trim blank chars |
|
345 { |
|
346 s[len] = '\0'; |
|
347 if( 0 == strcmp(entry_str, s)) |
|
348 { |
|
349 |
|
350 fprintf(tmp_file, "%s=", entry_str); |
|
351 |
|
352 if( valUnicode == TRUE ) |
|
353 fwprintf(tmp_file, L"%s\n", value); |
|
354 else |
|
355 fprintf(tmp_file, "%s\n", value); |
|
356 |
|
357 replaced = 1; |
|
358 } |
|
359 else |
|
360 if( EOF == fputs(line, tmp_file) ) |
|
361 { |
|
362 err = -1; |
|
363 break; |
|
364 } |
|
365 } |
|
366 } |
|
367 |
|
368 free(line); |
|
369 free(s); |
|
370 |
|
371 if( (0 == replaced) && (0 == err) ) |
|
372 { |
|
373 // no entry encountered - add new |
|
374 if( 0 != fseek( tmp_file, 0, SEEK_END ) ) |
|
375 err = -1; |
|
376 |
|
377 fprintf( tmp_file, "\n%s=", entry_str); |
|
378 if ( valUnicode ) |
|
379 fwprintf( tmp_file, L"%s\n", value); |
|
380 else |
|
381 fprintf( tmp_file, "%s\n", value); |
|
382 } |
|
383 |
|
384 |
|
385 if ( 0 != fclose(file ) ) |
|
386 { |
|
387 printf( "Could not close %s file\n", EPOC_INI_FILE ); |
|
388 return -1; |
|
389 } |
|
390 |
|
391 if ( 0 != fclose( tmp_file ) ) |
|
392 { |
|
393 printf( "Could not close %s file\n", EPOC_INI_FILE_TEMP ); |
|
394 return -1; |
|
395 } |
|
396 |
|
397 |
|
398 if( remove( EPOC_INI_FILE ) == -1 ) |
|
399 { |
|
400 printf( "Could not overwrite %s file\n", EPOC_INI_FILE ); |
|
401 return -1; |
|
402 } |
|
403 |
|
404 if( rename( EPOC_INI_FILE_TEMP, EPOC_INI_FILE ) != 0 ) |
|
405 { |
|
406 printf( "\nCould not rename '%s' to '%s'\n", EPOC_INI_FILE_TEMP, EPOC_INI_FILE ); |
|
407 return -1; |
|
408 } |
|
409 |
|
410 return 0; |
|
411 |
|
412 } |
|
413 |