3

誰かが私に説明してくれることを本当に望んでいます。デバイスの MAC アドレスを使用するアプリを作成しています。このコードはシミュレーターでは完全に機能しますが、デバイスでは機能しません。

このコードは、Objective-C の Get router mac (ARP のシステム コールなし)の質問から取得しました。

#include <stdio.h>

#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <net/if_dl.h>
#include <ifaddrs.h>
#include <net/if_types.h>

char*  getMacAddress(char* macAddress, char* ifName) 
{
   int  success;
   struct ifaddrs *addrs;
   struct ifaddrs *cursor;
   const struct sockaddr_dl *dlAddr;
   const unsigned char* base;
   int i;

   success = getifaddrs(&addrs) == 0;
   if (success) 
   {
       cursor = addrs;
       while (cursor != 0) 
       {
           const struct sockaddr_dl *socAddr = 
           (const struct sockaddr_dl *)cursor->ifa_addr;
           _Bool afLinkFamily = (cursor->ifa_addr->sa_family == AF_LINK);
           /* Ethernet CSMA/CD */
           _Bool sdlIFTEther = (socAddr->sdl_type == IFT_ETHER);

           if ((afLinkFamily) && 
                sdlIFTEther &&
                strcmp(ifName,  cursor->ifa_name) == 0) 
           {
               dlAddr = (const struct sockaddr_dl *) cursor->ifa_addr;
               base = 
                   (const unsigned char*)&dlAddr->sdl_data[dlAddr->sdl_nlen];
               strcpy(macAddress, ""); 
               for (i = 0; i < dlAddr->sdl_alen; i++) 
               {
                   if (i != 0) 
                   {
                       strcat(macAddress, ":");
                   }
                   char partialAddr[3];
                   sprintf(partialAddr, "%02X", base[i]);
                   strcat(macAddress, partialAddr);

               }
           }
           cursor = cursor->ifa_next;
       }

       freeifaddrs(addrs);
   }    
   return macAddress;
}

それが私に与えるエラー:

'net/if_types.h' file not found

私の質問は、なぜこれが起こっているのか、シミュレーターとデバイスでの実行の違いは何ですか? 前もって感謝します。

4

1 に答える 1

1

ヘッダー ファイルが iOS デバイス SDK にないだけです。Apple は、何らかの理由でそれを使用することを望んでいません。コードを機能させたいだけの場合は、必要な定義を抽出してみてください。

#define IFT_ETHER   0x6     /* Ethernet CSMACD */

行を削除します

#include <net/if_types.h>

完全に。これは、この定数が他の値に定義される可能性があるプラットフォームとの互換性を壊しますが。

于 2012-04-05T08:47:29.570 に答える