Annotation of nono/vm/ethernet.cpp, revision 1.1.1.1

1.1       root        1: //
                      2: // nono
                      3: // Copyright (C) 2019 [email protected]
                      4: //
                      5: 
                      6: #include "configfile.h"
                      7: #include "ethernet.h"
                      8: #include "netdriver_bpf.h"
                      9: #include "netdriver_tap.h"
                     10: #include "netdriver_none.h"
                     11: #include <random>
                     12: 
                     13: EthernetDevice *gEthernet;
                     14: 
                     15: // コンストラクタ
                     16: EthernetDevice::EthernetDevice()
                     17: {
                     18: }
                     19: 
                     20: // デストラクタ
                     21: EthernetDevice::~EthernetDevice()
                     22: {
                     23:        if (gNetDriver) {
                     24:                delete gNetDriver;
                     25:                gNetDriver = NULL;
                     26:        }
                     27: }
                     28: 
                     29: // 動的なコンストラクション
                     30: bool
                     31: EthernetDevice::Create()
                     32: {
                     33:        const std::string key = "ethernet.hostdriver";
                     34:        const std::string type = gConfig->ReadStr(key, "none");
                     35:        if (type == "none") {
                     36:                gNetDriver = new NetDriverNone();
                     37:        } else if (type == "tap") {
                     38: #if defined(NETDRIVER_TAP)
                     39:                try {
                     40:                        gNetDriver = new NetDriverTap(this, &mtx, &cv);
                     41:                } catch (...) {
                     42:                        warnx("Creating NetDriverTap failed");
                     43:                        return false;
                     44:                }
                     45: #else
                     46:                gConfig->Err(key, "\"%s\" not supported", type.c_str());
                     47:                return false;
                     48: #endif
                     49:        } else if (type == "bpf") {
                     50: #if defined(NETDRIVER_BPF)
                     51:                try {
                     52:                        gNetDriver = new NetDriverBPF(this, &mtx, &cv);
                     53:                } catch (...) {
                     54:                        warnx("Creating NetDriverBPF failed");
                     55:                        return false;
                     56:                }
                     57: #else
                     58:                gConfig->Err(key, "\"%s\" not supported", type.c_str());
                     59:                return false;
                     60: #endif
                     61:        } else {
                     62:                gConfig->Err(key, "unknown \"%s\"", type.c_str());
                     63:                return false;
                     64:        }
                     65:        assert(gNetDriver);
                     66: 
                     67:        return true;
                     68: }
                     69: 
                     70: // 初期化
                     71: bool
                     72: EthernetDevice::Init()
                     73: {
                     74:        if (gNetDriver->Init() == false) {
                     75:                delete gNetDriver;
                     76:                gNetDriver = NULL;
                     77:                return false;
                     78:        }
                     79: 
                     80:        return true;
                     81: }
                     82: 
                     83: // MAC アドレスをてきとーに生成する。
                     84: // 02:00:01 はローカルだし割り当てもされてなさそうなので使っちゃえ。
                     85: // 下3バイトは乱数。
                     86: /*static*/ void
                     87: EthernetDevice::GenerateMacAddr(macaddr_t *addrp)
                     88: {
                     89:        std::random_device rnd;
                     90:        uint32 x = rnd();
                     91: 
                     92:        macaddr_t& addr = *addrp;
                     93:        addr[0] = 0x02;
                     94:        addr[1] = 0x00;
                     95:        addr[2] = 0x01;
                     96:        addr[3] = (x >> 16) & 0xff;
                     97:        addr[4] = (x >>  8) & 0xff;
                     98:        addr[5] = x & 0xff;
                     99: }
                    100: 
                    101: // MAC アドレス文字列をバイナリに変換して返す。
                    102: // "HH:HH:HH:HH:HH:HH" 形式のみ。
                    103: // 成功すれば true を返す。
                    104: /*static*/ bool
                    105: EthernetDevice::ParseMacAddr(macaddr_t& dst, const std::string& src)
                    106: {
                    107:        const char *p;
                    108:        char *e;
                    109: 
                    110:        p = src.data();
                    111:        for (int i = 0; ; ) {
                    112:                unsigned long val = strtoul(p, &e, 16);
                    113: 
                    114:                if (p == e) {
                    115:                        return false;
                    116:                }
                    117:                if (val > 0xff) {
                    118:                        return false;
                    119:                }
                    120:                dst[i++] = val;
                    121:                p = e;
                    122: 
                    123:                // 最後だけ ':' をチェックしないので
                    124:                if (i == 6) {
                    125:                        break;
                    126:                }
                    127: 
                    128:                if (*p++ != ':') {
                    129:                        return false;
                    130:                }
                    131:        }
                    132:        if (*p != '\0') {
                    133:                return false;
                    134:        }
                    135:        return true;
                    136: }
                    137: 
                    138: // パケットを送信する (VM から呼ばれる)
                    139: bool
                    140: EthernetDevice::SendPacket(const std::vector<uint8>& buf)
                    141: {
                    142:        return gNetDriver->SendPacket(buf);
                    143: }

unix.superglobalmegacorp.com

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