Annotation of nono/host/netdriver_slirp.h, revision 1.1.1.2

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;
1.1.1.2 ! root       41:        bool InitDriver(bool startup) override;
        !            42: 
1.1       root       43:        int Read(NetPacket *p) override;
                     44:        void Write(const void *buf, int buflen) override;
                     45: 
                     46:        void MonitorUpdateMD(TextScreen&, int y) override;
                     47: 
1.1.1.2 ! root       48:        // Slirp ドライバが稼働中かどうか (高々1つなのでグローバル)
        !            49:        static bool IsOccupied() noexcept { return slirp_occupied; }
        !            50: 
1.1       root       51:  private:
                     52:        void Close();
                     53: 
                     54:        int txd {};                     // 裏スレッドへの送信ディスクリプタ
                     55:        int rxd {};                     // 裏スレッドからの受信ディスクリプタ
                     56: 
                     57:        std::unique_ptr<SlirpThread> backend /*{}*/;
                     58: 
                     59:        // usermode を使えるのは同時に1人だけに限定する。
                     60:        static bool slirp_occupied;
                     61: };
                     62: 
                     63: // 裏スレッド
                     64: class SlirpThread : public ThreadDevice
                     65: {
                     66:        using inherited = ThreadDevice;
                     67: 
                     68:        // major.minor.patch のバージョン番号を適当に整数にマッピングする。
                     69:        static uint32 VER(uint major_, uint minor_, uint patch_) {
                     70:                return (major_ << 16) + (minor_ << 8) + patch_;
                     71:        }
                     72: 
                     73:  public:
                     74:        explicit SlirpThread(NetDriverSlirp *parent_);
                     75:        ~SlirpThread() override;
                     76: 
                     77:        // 初期化
                     78:        bool InitBackend(const std::string& key, int *rfd, int *tfd);
                     79: 
                     80:        void Close();
                     81:        void Terminate() override;
                     82: 
                     83:        void MonitorUpdate(Monitor *, TextScreen&);
                     84: 
                     85:        // コールバック (C のコールバックから呼ばれるので public)
                     86:        int AddPollCB(int fd, int slevents);
                     87:        int GetReventsCB(int idx);
                     88:        ssize_t SendPacketCB(const void *src, size_t srclen);
                     89:        int64 ClockGetNsCB() const;
                     90:        void *TimerNewCB(void (*)(void *), void *);
                     91:        void TimerFreeCB(SlirpTimer *);
                     92:        void TimerModCB(SlirpTimer *, int64);
                     93:        void GuestErrorCB(const char *);
                     94: 
1.1.1.2 ! root       95:        // エラーメッセージ。表スレッドへの連絡用。
        !            96:        std::string errmsg {};
        !            97: 
1.1       root       98:  private:
                     99:        void ThreadRun() override;
                    100: 
1.1.1.2 ! root      101:        bool ParseConfigNet(const ConfigItem&,
        !           102:                struct in_addr *addr, struct in_addr *mask);
        !           103:        bool ParseConfigNet6(const ConfigItem&, struct in6_addr *addr6, uint *len);
        !           104:        bool ParseConfigHostFwdEntry(const ConfigItem&, const std::string& entry,
        !           105:                const std::string& where);
        !           106:        int  ParseAddrPort(const ConfigItem&, const std::string& hostport,
        !           107:                struct sockaddr_in *, const char *which, const std::string& where);
1.1       root      108: 
                    109:        void Write();
                    110:        // 一部のパケットをこちらで処理する。
                    111:        bool WriteHook(const std::vector<uint8>&) const;
                    112: 
                    113:        void UpdateInfo();
                    114: 
                    115:        // スレッド終了フラグ
                    116:        bool exit_requested {};
                    117: 
                    118:        // デバッグ用。
                    119:        void DumpHex(const void *, size_t) const;
                    120:        void DumpFrame(const void *, size_t) const;
                    121: 
                    122:        // Slirp ハンドル
                    123:        Slirp *sl {};
                    124: 
                    125:        std::vector<struct pollfd> pollfds {};
                    126: 
                    127:        std::list<SlirpTimer *> timerlist {};
                    128: 
                    129:        std::vector<FwdInfo> fwdinfo {};
                    130: 
                    131:        struct in6_addr gw6 {};
                    132:        struct in6_addr dns6 {};
                    133: 
                    134:        // 表スレッドとの送信側/受信側パイプ。
                    135:        autofd txd_r {};
                    136:        autofd txd_w {};
                    137:        autofd rxd_r {};
                    138:        autofd rxd_w {};
                    139: 
                    140:        // モニタ表示用の内部テーブル文字列。
                    141:        char *conninfo {};
                    142:        char *nbrinfo {};
                    143:        std::mutex info_mtx {};
                    144: 
                    145:        // タイマーを区別するためのデバッグ用の番号用。
                    146:        int latest_timer_num {};
                    147: 
                    148:        // libslirp のバージョン
                    149:        uint32 ver {};
                    150: 
                    151:        Monitor *monitor {};
                    152: 
                    153:        HostNetDevice *hostnet {};
                    154:        NetDriverSlirp *parent {};
                    155: };

unix.superglobalmegacorp.com

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