comparison packages/net/common/current/tests/set_mac_address.c @ 208:e0c0827131d1 ecos

Merge from eCos master repository on 2002-05-20-20:11:54-BST
author jlarmour
date Mon, 20 May 2002 22:19:26 +0000
parents
children 74dbf4c3f2e1
comparison
equal deleted inserted replaced
207:74c807ddde34 208:e0c0827131d1
1 //==========================================================================
2 //
3 // tests/set_mac_address.c
4 //
5 // Set_Mac_Address Utility - this carefully does NOTHING unless you
6 // edit this source file to confirm that you really want to do it.
7 //
8 //==========================================================================
9 //####BSDCOPYRIGHTBEGIN####
10 //
11 // -------------------------------------------
12 //
13 // Portions of this software may have been derived from OpenBSD or other sources,
14 // and are covered by the appropriate copyright disclaimers included herein.
15 //
16 // -------------------------------------------
17 //
18 //####BSDCOPYRIGHTEND####
19 //==========================================================================
20 //#####DESCRIPTIONBEGIN####
21 //
22 // Author(s): hmt
23 // Contributors:
24 // Date: 2000-05-03
25 // Purpose:
26 // Description:
27 //
28 //
29 //####DESCRIPTIONEND####
30 //
31 //==========================================================================
32
33 #include <pkgconf/system.h>
34 #ifdef CYGBLD_DEVS_ETH_DEVICE_H // Get the device config if it exists
35 #include CYGBLD_DEVS_ETH_DEVICE_H
36 #endif
37
38
39 // SET_MAC_ADDRESS test code
40
41 #include <network.h>
42
43 #include <netinet/if_ether.h>
44
45 #define NUMTHREADS 1
46 #define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 0x1000)
47 static char thread_stack[NUMTHREADS][STACK_SIZE];
48 static cyg_thread thread_data[NUMTHREADS];
49 static cyg_handle_t thread_handle[NUMTHREADS];
50
51 extern void
52 cyg_test_exit(void);
53
54 void
55 pexit(char *s)
56 {
57 perror(s);
58 cyg_test_exit();
59 }
60
61 // ------------------------------------------------------------------------
62 // remove NT to make this utility be useful ;-)
63 #define DONT_SET_ETH0
64 #define DONT_SET_ETH1
65
66 // These are commented out to make sure you choose a value:
67 #ifdef DO_SET_ETH0
68 //static cyg_uint8 new_eth0_addr[6]={ 0x0,0x90,0x27,0x8c,0x57,0xdd};
69 //static cyg_uint8 new_eth0_addr[6]={ 0x0,0x90,0x27,0x8c,0x57,0xdb};
70 #endif
71 #ifdef DO_SET_ETH1
72 //static cyg_uint8 new_eth1_addr[6]={ 0x0,0x90,0x27,0x8c,0x57,0xde};
73 //static cyg_uint8 new_eth1_addr[6]={ 0x0,0x90,0x27,0x8c,0x57,0xdc};
74 #endif
75
76 // ------------------------------------------------------------------------
77
78 int
79 set_mac_address( const char *interface, char *mac_address )
80 {
81 int s, i;
82 struct ifreq ifr;
83
84 s = socket(AF_INET, SOCK_DGRAM, 0);
85 if (s < 0) {
86 perror("socket");
87 return false;
88 }
89
90 diag_printf( "%s socket is %d:\n", interface, s );
91
92 strcpy(ifr.ifr_name, interface);
93
94 for ( i = 0; i < ETHER_ADDR_LEN; i++ )
95 ifr.ifr_hwaddr.sa_data[i] = mac_address[i];
96
97 diag_printf( "Mac addr %02x:%02x:%02x:%02x:%02x:%02x\n",
98 ifr.ifr_hwaddr.sa_data[0],
99 ifr.ifr_hwaddr.sa_data[1],
100 ifr.ifr_hwaddr.sa_data[2],
101 ifr.ifr_hwaddr.sa_data[3],
102 ifr.ifr_hwaddr.sa_data[4],
103 ifr.ifr_hwaddr.sa_data[5] );
104
105 if (ioctl(s, SIOCSIFHWADDR, &ifr)) {
106 perror("SIOCSIFHWADDR");
107 close( s );
108 return false;
109 }
110
111 diag_printf( "%s ioctl(SIOCSIFHWADDR) succeeded\n", interface );
112
113 close( s );
114
115 return true;
116 }
117
118 // ------------------------------------------------------------------------
119 void
120 net_test(cyg_addrword_t param)
121 {
122 int results = 0;
123 diag_printf("Start set_mac_address\n");
124 #ifdef CYGHWR_NET_DRIVER_ETH0
125 #ifdef DO_SET_ETH0
126 diag_printf("Setting MAC of eth0 to %02x:%02x:%02x:%02x:%02x:%02x\n",
127 new_eth0_addr[0],new_eth0_addr[1],
128 new_eth0_addr[2],new_eth0_addr[3],
129 new_eth0_addr[4],new_eth0_addr[5] );
130 results += set_mac_address( "eth0", new_eth0_addr );
131 #endif
132 #endif
133 #ifdef CYGHWR_NET_DRIVER_ETH1
134 #ifdef DO_SET_ETH1
135 diag_printf("Setting MAC of eth1 to %02x:%02x:%02x:%02x:%02x:%02x\n",
136 new_eth1_addr[0],new_eth1_addr[1],
137 new_eth1_addr[2],new_eth1_addr[3],
138 new_eth1_addr[4],new_eth1_addr[5] );
139 results += set_mac_address( "eth1", new_eth1_addr );
140 #endif
141 #endif
142
143 if ( 0 == results )
144 diag_printf( "**** Did not set any MAC addresses ****\n" );
145
146 diag_printf("Init Network Interfaces\n");
147 init_all_network_interfaces();
148 diag_printf("After init.\n");
149
150 cyg_test_exit();
151 }
152
153 // ------------------------------------------------------------------------
154 void
155 cyg_start(void)
156 {
157 // Create a main thread, so we can run the scheduler and have time 'pass'
158 cyg_thread_create(10, // Priority - just a number
159 net_test, // entry
160 0, // entry parameter
161 "Network test", // Name
162 &thread_stack[0][0], // Stack
163 STACK_SIZE, // Size
164 &thread_handle[0], // Handle
165 &thread_data[0] // Thread data structure
166 );
167 cyg_thread_resume(thread_handle[0]); // Start it
168
169 cyg_scheduler_start();
170 }
171
172 // EOF set_mac_address.c