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

1.1       root        1: //
                      2: // nono
1.1.1.3   root        3: // Copyright (C) 2020 nono project
                      4: // Licensed under nono-license.txt
1.1       root        5: //
                      6: 
                      7: #include "ethernet.h"
1.1.1.3   root        8: #include "config.h"
1.1       root        9: #include "netdriver_bpf.h"
                     10: #include "netdriver_tap.h"
                     11: #include "netdriver_none.h"
                     12: #include <random>
                     13: 
1.1.1.2   root       14: std::unique_ptr<EthernetDevice> gEthernet;
1.1       root       15: 
                     16: // コンストラクタ
                     17: EthernetDevice::EthernetDevice()
                     18: {
                     19: }
                     20: 
                     21: // デストラクタ
                     22: EthernetDevice::~EthernetDevice()
                     23: {
                     24: }
                     25: 
                     26: // 動的なコンストラクション
                     27: bool
                     28: EthernetDevice::Create()
                     29: {
1.1.1.3   root       30:        const ConfigItem& item = gConfig->Find("ethernet-hostdriver");
1.1.1.2   root       31:        const std::string& type = item.AsString();
1.1.1.4 ! root       32: 
1.1       root       33:        if (type == "none") {
1.1.1.2   root       34:                gNetDriver.reset(new NetDriverNone());
1.1       root       35:        } else if (type == "tap") {
                     36: #if defined(NETDRIVER_TAP)
1.1.1.4 ! root       37:                const ConfigItem& item_devpath = gConfig->Find("ethernet-hostdevice");
        !            38:                const std::string& devpath = item_devpath.AsString();
        !            39: 
1.1       root       40:                try {
1.1.1.4 ! root       41:                        gNetDriver.reset(new NetDriverTap(this, &mtx, &cv, devpath));
1.1       root       42:                } catch (...) {
                     43:                        warnx("Creating NetDriverTap failed");
                     44:                        return false;
                     45:                }
                     46: #else
1.1.1.2   root       47:                item.Err("tap not supported");
1.1       root       48:                return false;
                     49: #endif
                     50:        } else if (type == "bpf") {
                     51: #if defined(NETDRIVER_BPF)
                     52:                try {
1.1.1.2   root       53:                        gNetDriver.reset(new NetDriverBPF(this, &mtx, &cv));
1.1       root       54:                } catch (...) {
                     55:                        warnx("Creating NetDriverBPF failed");
                     56:                        return false;
                     57:                }
                     58: #else
1.1.1.2   root       59:                item.Err("bpf not supported");
1.1       root       60:                return false;
                     61: #endif
                     62:        } else {
1.1.1.2   root       63:                item.Err();
1.1       root       64:                return false;
                     65:        }
1.1.1.2   root       66:        assert((bool)gNetDriver);
1.1       root       67: 
                     68:        return true;
                     69: }
                     70: 
                     71: // 初期化
                     72: bool
                     73: EthernetDevice::Init()
                     74: {
                     75:        if (gNetDriver->Init() == false) {
1.1.1.2   root       76:                gNetDriver.reset();
1.1       root       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.