2
4

グローバル・プライベート(ローカル)MACアドレスの見分け方

Last updated at Posted at 2019-12-02

追記(2024-09-17)

This is the 02 bit of the first octet in the MAC. If it is set, this is a locally-administered address. Essentially, if the second hex digit is 2, 6, A, or E, it is a private MAC.

x2:xx:xx:xx:xx:xx
x6:xx:xx:xx:xx:xx
xA:xx:xx:xx:xx:xx
xE:xx:xx:xx:xx:xx


概要

  • 下記の図より、上位8bitのb1部分が1ならばプライベートMACアドレスである。
  • つまり16進数表記のMACアドレスx○-xx-xx-xxを考えると、の部分が2367abefのいずれかならばプライベートMACアドレスである。(後述の進数対応表を見ると分かりやすいかと思います。)

MAC addressより引用

10進数 2進数
(右から二桁目が0か1か)
16進数 isPrivate
1 0001 1
2 0010 2
3 0011 3
4 0100 4
5 0101 5
6 0110 6
7 0111 7
8 1000 8
9 1001 9
10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1110 E
15 1111 F

参考

Code(Objective-C)

  • プライベートMACアドレス識別用の関数
/**
 @brief プライベートMACアドレスかどうか
 */
- (BOOL)isPrivateMACAddress:(NSString *)macAddress {
    // 2文字目が下記ならプライベートMACアドレス
    NSCharacterSet *privateMACAddressCharSet = [NSCharacterSet characterSetWithCharactersInString:@"2367abefABEF"];
    return [privateMACAddressCharSet characterIsMember:[macAddress characterAtIndex:1]];
}
  • 呼び出し例
NSArray *macAddresses = @[@"5924a970c0a2",
                          @"723d614ed27a",
                          @"55002cd18b38",
                          @"e193cb9c4945",
                          @"3a60d8d5551e",
                          @"c5c08c6cc511",
                          @"42a8887ccb5e",
                          @"76c209ad50b2",
                          @"3b4d3a1db4b5",
                          @"8d9b8ad08047",
                          ];
for (NSString *macAddress in macAddresses) {
    if ([self isPrivateMACAddress:macAddress]) {
        NSLog(@"'%@'はプライベートMACアドレスです。", macAddress);
    }
}
  • 出力結果
'723d614ed27a'はプライベートMACアドレスです。
'3a60d8d5551e'はプライベートMACアドレスです。
'42a8887ccb5e'はプライベートMACアドレスです。
'76c209ad50b2'はプライベートMACアドレスです。
'3b4d3a1db4b5'はプライベートMACアドレスです。
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4