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