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

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

unix.superglobalmegacorp.com

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