Annotation of nono/lib/macaddr.cpp, revision 1.1.1.2

1.1       root        1: //
                      2: // nono
                      3: // Copyright (C) 2021 nono project
                      4: // Licensed under nono-license.txt
                      5: //
                      6: 
                      7: //
                      8: // MAC アドレス
                      9: //
                     10: 
                     11: #include "macaddr.h"
                     12: #include "mystring.h"
                     13: #include <random>
                     14: 
                     15: // MAC アドレスを適当に生成する。
                     16: // 02:00:01 はローカルだし割り当てもされてなさそうなので使っちゃえ。
                     17: // 下3バイトは乱数。
                     18: void
1.1.1.2 ! root       19: MacAddr::Generate()
1.1       root       20: {
                     21:        std::random_device rnd;
                     22:        uint32 x = rnd();
                     23: 
                     24:        (*this)[0] = 0x02;
                     25:        (*this)[1] = 0x00;
                     26:        (*this)[2] = 0x01;
                     27:        (*this)[3] = (x >> 16) & 0xff;
                     28:        (*this)[4] = (x >>  8) & 0xff;
                     29:        (*this)[5] = x & 0xff;
                     30: }
                     31: 
                     32: // 文字列から MAC アドレスを作成する。"HH:HH:HH:HH:HH:HH" 形式のみ。
                     33: // 成功すれば true を返す。
                     34: bool
1.1.1.2 ! root       35: MacAddr::FromString(const std::string& src)
1.1       root       36: {
                     37:        const char *p;
                     38:        char *e;
                     39: 
                     40:        p = src.data();
                     41:        for (int i = 0; ; ) {
                     42:                unsigned long val = strtoul(p, &e, 16);
                     43: 
                     44:                if (p == e) {
                     45:                        return false;
                     46:                }
                     47:                if (val > 0xff) {
                     48:                        return false;
                     49:                }
                     50:                (*this)[i++] = val;
                     51:                p = e;
                     52: 
                     53:                // 最後だけ ':' をチェックしないので
                     54:                if (i == 6) {
                     55:                        break;
                     56:                }
                     57: 
                     58:                if (*p++ != ':') {
                     59:                        return false;
                     60:                }
                     61:        }
                     62:        if (*p != '\0') {
                     63:                return false;
                     64:        }
                     65:        return true;
                     66: }
                     67: 
                     68: // 区切り文字なしの文字列形式を返す。主に ROM 埋め込み用で英字は大文字。
                     69: // 01:23:AB なら "0123AB" のような感じ。
                     70: std::string
1.1.1.2 ! root       71: MacAddr::ToString() const
1.1       root       72: {
                     73:        return string_format("%02X%02X%02X%02X%02X%02X",
                     74:                (*this)[0],
                     75:                (*this)[1],
                     76:                (*this)[2],
                     77:                (*this)[3],
                     78:                (*this)[4],
                     79:                (*this)[5]);
                     80: }
                     81: 
                     82: // sep を区切り文字とする文字列形式を返す。主に表示用で英字は小文字。
                     83: // 01:23:AB で sep = ":" なら "01:23:ab" のような感じ。
                     84: std::string
1.1.1.2 ! root       85: MacAddr::ToString(char sep) const
1.1       root       86: {
                     87:        return string_format("%02x:%02x:%02x:%02x:%02x:%02x",
                     88:                (*this)[0],
                     89:                (*this)[1],
                     90:                (*this)[2],
                     91:                (*this)[3],
                     92:                (*this)[4],
                     93:                (*this)[5]);
                     94: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.