|
|
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 "netdriver.h"
1.1.1.2 root 8: #include "mainapp.h"
1.1.1.3 root 9: #include "mythread.h"
1.1 root 10:
11: static void *netdriver_run(void *);
12:
1.1.1.2 root 13: std::unique_ptr<NetDriver> gNetDriver;
1.1 root 14:
15: // コンストラクタ
1.1.1.3 root 16: NetDriver::NetDriver(const char *drivername_, EthernetDevice *parent_,
17: std::mutex *mtx_, std::condition_variable *cv_)
1.1 root 18: {
19: logname = "netdriver";
20: devname = "NetDriver";
21:
1.1.1.4 root 22: monitor_size = nnSize(30, 7);
1.1 root 23:
24: fd = -1;
1.1.1.3 root 25: drivername = drivername_;
26: parent = parent_;
27: mtx = mtx_;
28: cv = cv_;
1.1 root 29: }
30:
31: // デストラクタ
32: NetDriver::~NetDriver()
33: {
34: }
35:
36: // モニタ
1.1.1.4 root 37: void
38: NetDriver::MonitorUpdate(TextScreen& monitor)
1.1 root 39: {
40: monitor.Clear();
41:
42: monitor.Print(0, 0, "NetDriver: %s", drivername);
43: monitor.Print(0, 1, "Device : %s", devpath.c_str());
44: monitor.Print(0, 2, "Interface: %s", ifname.c_str());
45:
46: monitor.Print(5, 4, "%9s %9s", "Packets", "Bytes");
47: monitor.Print(0, 5, "Send %9" PRIu64 " %9" PRIu64, tx_pkts, tx_bytes);
48: monitor.Print(0, 6, "Recv %9" PRIu64 " %9" PRIu64, rx_pkts, rx_bytes);
49: }
50:
51: // スレッドが必要な継承クラスが呼ぶ
52: bool
53: NetDriver::InitRecvThread()
54: {
55: // 受信スレッド起動
56: pthread_t th;
57: pthread_create(&th, NULL, netdriver_run, NULL);
58:
59: return true;
60: }
61:
62: // 受信スレッドのエントリポイント
63: void *
64: netdriver_run(void *dummy)
65: {
1.1.1.3 root 66: PTHREAD_SETNAME("netdriver");
1.1 root 67: pthread_detach(pthread_self());
68:
69: gNetDriver->ThreadRun();
70: return NULL;
71: }
72:
73: // ifup スクリプト実行
74: bool
75: NetDriver::Run_ifup()
76: {
77: return RunScript("nono-ifup");
78: }
79:
80: // ifdown スクリプト実行
81: bool
82: NetDriver::Run_ifdown()
83: {
84: return RunScript("nono-ifdown");
85: }
86:
87: // スクリプトを実行
88: bool
89: NetDriver::RunScript(const char *filename)
90: {
91: // ファイルが存在するか
1.1.1.2 root 92: std::string path = gMainApp.SearchFile(filename);
1.1 root 93: if (path.empty()) {
94: warnx("%s not found", filename);
95: return false;
96: }
97:
98: // コマンドラインを作成
99: std::string cmd = path + " " + ifname;
100:
101: // 実行
102: int rv = system(cmd.c_str());
103: if (rv == -1) {
104: warn("%s: system() failed", path.c_str());
105: return false;
106: }
107: if (rv == 127) {
108: warnx("%s: system(): shell failed", path.c_str());
109: return false;
110: }
111: if (rv != 0) {
112: warnx("%s failed with %d", path.c_str(), rv);
113: return false;
114: }
115: return true;
116: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.