|
|
1.1 root 1: //
2: // nono
3: // Copyright (C) 2025 nono project
4: // Licensed under nono-license.txt
5: //
6:
7: //
8: // ホストネットワークの usermode (SLIRP) ドライバ
9: //
10:
11: #pragma once
12:
13: #include "netdriver.h"
14: #include "autofd.h"
15: #include "config.h"
16: #include "hostnet.h"
17: #include <poll.h>
18: #include <netinet/in.h>
19: #include <list>
20: #include <mutex>
21:
22: typedef struct Slirp Slirp;
23: class SlirpThread;
24: class SlirpTimer;
25:
26: struct FwdInfo
27: {
28: int is_udp;
29: struct sockaddr_in host;
30: struct sockaddr_in guest;
31: };
32:
33: class NetDriverSlirp : public NetDriver
34: {
35: using inherited = NetDriver;
36: public:
37: explicit NetDriverSlirp(HostDevice *hostdev_);
38: ~NetDriverSlirp() override;
39:
40: void SetLogLevel(int) override;
41: bool InitDriver() override;
42: int Read(NetPacket *p) override;
43: void Write(const void *buf, int buflen) override;
44:
45: void MonitorUpdateMD(TextScreen&, int y) override;
46:
47: private:
48: void Close();
49:
50: int txd {}; // 裏スレッドへの送信ディスクリプタ
51: int rxd {}; // 裏スレッドからの受信ディスクリプタ
52:
53: std::unique_ptr<SlirpThread> backend /*{}*/;
54:
55: // usermode を使えるのは同時に1人だけに限定する。
56: static bool slirp_occupied;
57: };
58:
59: // 裏スレッド
60: class SlirpThread : public ThreadDevice
61: {
62: using inherited = ThreadDevice;
63:
64: // major.minor.patch のバージョン番号を適当に整数にマッピングする。
65: static uint32 VER(uint major_, uint minor_, uint patch_) {
66: return (major_ << 16) + (minor_ << 8) + patch_;
67: }
68:
69: public:
70: explicit SlirpThread(NetDriverSlirp *parent_);
71: ~SlirpThread() override;
72:
73: // 初期化
74: bool InitBackend(const std::string& key, int *rfd, int *tfd);
75:
76: void Close();
77: void Terminate() override;
78:
79: void MonitorUpdate(Monitor *, TextScreen&);
80:
81: // コールバック (C のコールバックから呼ばれるので public)
82: int AddPollCB(int fd, int slevents);
83: int GetReventsCB(int idx);
84: ssize_t SendPacketCB(const void *src, size_t srclen);
85: int64 ClockGetNsCB() const;
86: void *TimerNewCB(void (*)(void *), void *);
87: void TimerFreeCB(SlirpTimer *);
88: void TimerModCB(SlirpTimer *, int64);
89: void GuestErrorCB(const char *);
90:
91: private:
92: void ThreadRun() override;
93:
94: bool ParseAddr(std::string, const char *, struct sockaddr_in *,
95: std::string&) const;
96:
97: void Write();
98: // 一部のパケットをこちらで処理する。
99: bool WriteHook(const std::vector<uint8>&) const;
100:
101: void UpdateInfo();
102:
103: // スレッド終了フラグ
104: bool exit_requested {};
105:
106: // デバッグ用。
107: void DumpHex(const void *, size_t) const;
108: void DumpFrame(const void *, size_t) const;
109:
110: // Slirp ハンドル
111: Slirp *sl {};
112:
113: std::vector<struct pollfd> pollfds {};
114:
115: std::list<SlirpTimer *> timerlist {};
116:
117: std::vector<FwdInfo> fwdinfo {};
118:
119: struct in6_addr gw6 {};
120: struct in6_addr dns6 {};
121: struct in6_addr perfaddr6 {};
122:
123: // 表スレッドとの送信側/受信側パイプ。
124: autofd txd_r {};
125: autofd txd_w {};
126: autofd rxd_r {};
127: autofd rxd_w {};
128:
129: // モニタ表示用の内部テーブル文字列。
130: char *conninfo {};
131: char *nbrinfo {};
132: std::mutex info_mtx {};
133:
134: // タイマーを区別するためのデバッグ用の番号用。
135: int latest_timer_num {};
136:
137: // libslirp のバージョン
138: uint32 ver {};
139:
140: Monitor *monitor {};
141:
142: HostNetDevice *hostnet {};
143: NetDriverSlirp *parent {};
144: };
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.