1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 25 #include <unistd.h> 26 #include <errno.h> 27 #include <stdio.h> 28 #include <string.h> 29 #include <sys/ioctl.h> 30 #include <sys/socket.h> 31 #include <net/if.h> 32 33 #ifdef SOLARIS 34 #include <sys/sockio.h> 35 #endif 36 37 #include "osl/util.h" 38 39 40 41 /*****************************************************************************/ 42 /* Static Module Functions */ 43 /*****************************************************************************/ 44 45 static int osl_getHWAddr(const char *ifname, char* hard_addr); 46 static int osl_checkAddr(const char* addr); 47 48 49 /*****************************************************************************/ 50 /* osl_getEthernetAddress */ 51 /*****************************************************************************/ 52 53 sal_Bool SAL_CALL osl_getEthernetAddress( sal_uInt8 * pAddr ) 54 { 55 char buff[1024]; 56 char hard_addr[64]; 57 struct ifconf ifc; 58 struct ifreq *ifr; 59 int i; 60 int so; 61 62 #ifdef SOLARIS 63 /** algorithm doesn't work on solaris */ 64 return sal_False; 65 #else 66 67 if ( pAddr == 0 ) 68 { 69 return sal_False; 70 } 71 72 73 /* 74 * All we need is ... a network file descriptor. 75 * Normally, this is a very socket. 76 */ 77 78 so = socket(AF_INET, SOCK_DGRAM, 0); 79 80 81 /* 82 * The first thing we have to do, get the interface configuration. 83 * It is a list of attached/configured interfaces 84 */ 85 86 ifc.ifc_len = sizeof(buff); 87 ifc.ifc_buf = buff; 88 if ( ioctl(so, SIOCGIFCONF, &ifc) < 0 ) 89 { 90 /* fprintf(stderr, "SIOCGIFCONF: %s\n", strerror(errno));*/ 91 close(so); 92 return sal_False; 93 } 94 95 close(so); 96 97 /* 98 * For each of the interfaces in the interface list, 99 * try to get the hardware address 100 */ 101 102 ifr = ifc.ifc_req; 103 for ( i = ifc.ifc_len / sizeof(struct ifreq) ; --i >= 0 ; ifr++ ) 104 { 105 int nRet=0; 106 nRet = osl_getHWAddr(ifr->ifr_name,hard_addr); 107 if ( nRet > 0 ) 108 { 109 memcpy( pAddr , hard_addr, 6 ); 110 return sal_True; 111 } 112 } 113 114 return sal_False; 115 #endif 116 } 117 118 119 /*****************************************************************************/ 120 /* osl_getHWAddr */ 121 /*****************************************************************************/ 122 123 static int osl_getHWAddr(const char *ifname, char* hard_addr) 124 { 125 int ret=0; 126 struct ifreq ifr; 127 int so = socket(AF_INET, SOCK_DGRAM, 0); 128 129 strcpy(ifr.ifr_name, ifname); 130 131 /* 132 * First, get the Interface-FLAGS 133 */ 134 135 ret=ioctl(so, SIOCGIFFLAGS, &ifr) ; 136 137 if ( ret < 0 ) 138 { 139 /* fprintf(stderr, "SIOCGIFFLAGS: %s\n", strerror(errno)); */ 140 close(so); 141 return ret; 142 } 143 144 145 /* 146 * If it is the loopback device, do not consider it any further 147 */ 148 149 if (ifr.ifr_flags & IFF_LOOPBACK) 150 { 151 /* fprintf(stderr, "SIOCGIFFLAGS : is LOOPBACK : %s\n", strerror(errno));*/ 152 close(so); 153 return 0; 154 } 155 156 157 /* 158 * And now, the real thing: the get address 159 */ 160 161 #if defined(SIOCGIFHWADDR) && !defined(SOLARIS) 162 ret=ioctl(so, SIOCGIFHWADDR, &ifr); 163 #else 164 ret=ioctl(so, SIOCGIFADDR, &ifr); 165 #endif 166 167 if (ret < 0) { 168 /* fprintf(stderr, "SIOCGIFADDR: %s\n", strerror(errno));*/ 169 memset(hard_addr, 0, 32); 170 close(so); 171 return ret; 172 } 173 174 close(so); 175 176 #if defined(SIOCGIFHWADDR) && !defined(SOLARIS) 177 memcpy(hard_addr,ifr.ifr_hwaddr.sa_data,8); 178 #else 179 memcpy(hard_addr,ifr.ifr_ifru.ifru_addr.sa_data,8); 180 #endif 181 182 183 /* 184 * Check, if no real, i.e. 00:00:00:00:00:00, address was retrieved. 185 * The Linux dummy device has this kind of behaviour 186 */ 187 188 ret=osl_checkAddr(hard_addr); 189 190 if (ret < 0) { 191 /* fprintf(stderr, "SIOCGIFADDR got '00:00:00:00:00:00'\n"); */ 192 return ret; 193 } 194 195 /* fprintf(stderr,"interface : %s -- ",ifname);*/ 196 /* fprintf(stderr,"HWaddr : %s\n", print_ether(hard_addr));*/ 197 198 return 1; 199 } 200 201 202 /*****************************************************************************/ 203 /* osl_checkAddr */ 204 /*****************************************************************************/ 205 206 static int osl_checkAddr(const char* addr) 207 { 208 if (addr[0]==0 && addr[1]==0 && 209 addr[2]==0 && addr[3]==0 && 210 addr[4]==0 && addr[5]==0) 211 { 212 return -1; 213 } 214 return 0; 215 } 216 217 218 #if defined (SPARC) 219 220 #if defined (SOLARIS) && !defined(__sparcv8plus) && !defined(__sparcv9) 221 #include <sys/types.h> 222 #include <sys/processor.h> 223 224 /*****************************************************************************/ 225 /* osl_InitSparcV9 */ 226 /*****************************************************************************/ 227 228 void osl_InterlockedCountSetV9(sal_Bool bV9); 229 230 /* 231 * osl_InitSparcV9() should be executed as early as possible. We place it in the 232 * .init section of sal 233 */ 234 #if defined ( __SUNPRO_C ) || defined ( __SUNPRO_CC ) 235 void osl_InitSparcV9(void); 236 #pragma init (osl_InitSparcV9) 237 #elif defined ( __GNUC__ ) 238 void osl_InitSparcV9(void) __attribute__((constructor)); 239 #endif 240 241 void osl_InitSparcV9(void) 242 { 243 /* processor_info() identifies SPARCV8 (ie sun4c machines) simply as "sparc" 244 * and SPARCV9 (ie ultra sparcs, sun4u) as "sparcv9". Since we know that we 245 * run at least on a SPARCV8 architecture or better, any processor type != "sparc" 246 * and != "i386" is considered to be SPARCV9 or better 247 * 248 * This way we are certain that this will still work if someone names SPARCV10 249 * "foobar" 250 */ 251 processor_info_t aInfo; 252 int rc; 253 254 rc = processor_info(0, &aInfo); 255 256 if ( rc != -1 ) { 257 if ( !strcmp( "sparc", aInfo.pi_processor_type ) /* SPARCV8 */ 258 || !strcmp( "i386", aInfo.pi_processor_type ) ) /* can't happen, but ... */ 259 return; 260 /* we are reasonably certain to be on sparcv9/sparcv8plus or better */ 261 osl_InterlockedCountSetV9(sal_True); 262 } 263 } 264 265 #endif /* SOLARIS */ 266 267 #if defined(NETBSD) && defined(GCC) && !defined(__sparcv9) && !defined(__sparc_v9__) 268 269 #include <sys/param.h> 270 #include <sys/sysctl.h> 271 void osl_InitSparcV9(void) __attribute__((constructor)); 272 void osl_InterlockedCountSetV9(sal_Bool bV9); 273 274 /* Determine which processor we are running on (sparc v8 or v9) 275 * The approach is very similar to Solaris. 276 */ 277 278 void osl_InitSparcV9(void) 279 { 280 int mib[2]={CTL_HW,HW_MACHINE}; 281 char processorname[256]; 282 size_t len=256; 283 284 /* get the machine name */ 285 sysctl(mib, 2, processorname, &len, NULL, 0); 286 if (!strncmp("sparc64",processorname, len)) { 287 osl_InterlockedCountSetV9(sal_True); 288 } 289 } 290 291 #endif /* NETBSD */ 292 293 #endif /* SPARC */ 294 295 #if defined ( LINUX ) && defined ( SPARC ) 296 #include <sys/utsname.h> 297 void osl_InitSparcV9(void) __attribute__((constructor)); 298 void osl_InterlockedCountSetV9(sal_Bool bV9); 299 /* Determine which processor we are running on (sparc v8 or v9) 300 * The approach is very similar to Solaris. 301 */ 302 void osl_InitSparcV9(void) 303 { 304 struct utsname name; 305 int rc; 306 rc = uname(&name); 307 if ( rc != -1 ) { 308 if ( !strcmp( "sparc", name.machine )) 309 return; 310 osl_InterlockedCountSetV9(sal_True); 311 } 312 } 313 #endif 314 315 #if ( defined(__GNUC__) && (defined(X86) || defined(X86_64)) )\ 316 || ( defined(SOLARIS) && defined (__SUNPRO_C) && defined(__i386) ) 317 318 /* Safe default */ 319 int osl_isSingleCPU = 0; 320 321 /* Determine if we are on a multiprocessor/multicore/HT x86/x64 system 322 * 323 * The lock prefix for atomic operations in osl_[inc|de]crementInterlockedCount() 324 * comes with a cost and is especially expensive on pre HT x86 single processor 325 * systems, where it isn't needed at all. 326 * 327 * This should be run as early as possible, thus it's placed in the init section 328 */ 329 #if defined(_SC_NPROCESSORS_CONF) /* i.e. MACOSX for Intel doesn't have this */ 330 #if defined(__GNUC__) 331 void osl_interlockedCountCheckForSingleCPU(void) __attribute__((constructor)); 332 #elif defined(__SUNPRO_C) 333 void osl_interlockedCountCheckForSingleCPU(void); 334 #pragma init (osl_interlockedCountCheckForSingleCPU) 335 #endif 336 337 void osl_interlockedCountCheckForSingleCPU(void) 338 { 339 /* In case sysconfig fails be on the safe side, 340 * consider it a multiprocessor/multicore/HT system */ 341 if ( sysconf(_SC_NPROCESSORS_CONF) == 1 ) { 342 osl_isSingleCPU = 1; 343 } 344 } 345 #endif /* defined(_SC_NPROCESSORS_CONF) */ 346 #endif 347