--- nono/host/netdriver_bpf.cpp 2026/04/29 17:04:28 1.1 +++ nono/host/netdriver_bpf.cpp 2026/04/29 17:04:48 1.1.1.4 @@ -1,12 +1,12 @@ // // nono -// Copyright (C) 2019 isaki@NetBSD.org +// Copyright (C) 2020 nono project +// Licensed under nono-license.txt // -#include "header.h" #include "netdriver_bpf.h" +#include "autofd.h" #include "cvprompt.h" -#include "mystring.h" #include #include #include @@ -23,9 +23,9 @@ #include // コンストラクタ -NetDriverBPF::NetDriverBPF(EthernetDevice *p, - std::mutex *m, std::condition_variable *c) - : inherited("BPF", p, m, c) +NetDriverBPF::NetDriverBPF(EthernetDevice *parent_, + std::mutex *mtx_, std::condition_variable *cv_) + : inherited("BPF", parent_, mtx_, cv_) { fd = -1; } @@ -34,10 +34,6 @@ NetDriverBPF::NetDriverBPF(EthernetDevic NetDriverBPF::~NetDriverBPF() { Close(); - - if (bpfbuf) { - delete[] bpfbuf; - } } // 初期化 & オープン @@ -52,6 +48,8 @@ NetDriverBPF::Init() BPF_STMT(BPF_RET + BPF_K, (u_int)-1), }; + putmsg(1, "NetDriverBPF"); + // XXX tap はデバイスを開くとインタフェースが出来る、という方向だが、 // bpf はインタフェースを指定してデバイスを開く、という方向で、逆…。 @@ -59,7 +57,7 @@ NetDriverBPF::Init() // インタフェース指定がなければそれっぽいのを探すか ifname = FindInterface(); if (ifname.empty()) { - warn("NetDriverBPF: no interface specified"); + warn("NetDriverBPF.Open: no interface specified"); return false; } } @@ -77,14 +75,19 @@ NetDriverBPF::Init() } fd = open(devpath.c_str(), O_RDWR); - if (fd == -1) { - warn("NetDriverBPF.Open: skip %s", devpath.c_str()); + if (fd < 0) { + // ファイルがないのを表示したら結構うるさいので除く + if (errno != ENOENT) { + putmsg(1, "%s: %s", devpath.c_str(), strerror(errno)); + } continue; } + putmsg(1, "%s: found", devpath.c_str()); break; } - if (fd == -1) { - warnx("NetDriverBPF.Open: no bpf device available"); + if (fd < 0) { + putmsg(1, "no bpf devices available"); + warnx("NetDriverBPF.Open: no bpf devices available"); return false; } @@ -92,7 +95,7 @@ NetDriverBPF::Init() memset(&ifr, 0, sizeof(ifr)); strlcpy(ifr.ifr_name, ifname.c_str(), sizeof(ifr.ifr_name)); if (ioctl(fd, BIOCSETIF, &ifr) == -1) { - warn("NetDriverBPF.Open: %s(%s) BIOCSETIF failed", + warn("NetDriverBPF.Open: %s(%s): BIOCSETIF", devpath.c_str(), ifname.c_str()); goto abort; } @@ -102,20 +105,20 @@ NetDriverBPF::Init() devpath.c_str(), ifname.c_str()); if (ioctl(fd, BIOCPROMISC, (void *)0) == -1) { - warn("NetDriverBPF.Open: %s(%s): BIOCPROMISC failed", + warn("NetDriverBPF.Open: %s(%s): BIOCPROMISC", devpath.c_str(), ifname.c_str()); goto abort; } val = 1; if (ioctl(fd, BIOCIMMEDIATE, &val) == -1) { - warn("NetDriverBPF.Open: %s(%s): BIOCIMMEDIATE failed", + warn("NetDriverBPF.Open: %s(%s): BIOCIMMEDIATE", devpath.c_str(), ifname.c_str()); goto abort; } if (ioctl(fd, BIOCSHDRCMPLT, &val) == -1) { - warn("NetDriverBPF.Open: %s(%s): BIOCSHDRCMPLT failed", + warn("NetDriverBPF.Open: %s(%s): BIOCSHDRCMPLT", devpath.c_str(), ifname.c_str()); goto abort; } @@ -128,25 +131,22 @@ NetDriverBPF::Init() prog.bf_len = countof(insn); prog.bf_insns = insn; if (ioctl(fd, BIOCSETF, &prog) == -1) { - warn("NetDriverBPF.Open: %s(%s): BIOCSETF failed", + warn("NetDriverBPF.Open: %s(%s): BIOCSETF", devpath.c_str(), ifname.c_str()); goto abort; } // バッファサイズを取得 if (ioctl(fd, BIOCGBLEN, &bpfbuflen) == -1) { - warn("NetDriverBPF.Open: %s(%s): BIOCGBLEN failed", + warn("NetDriverBPF.Open: %s(%s): BIOCGBLEN", devpath.c_str(), ifname.c_str()); goto abort; } // 受信用バッファを確保 try { - bpfbuf = new char [bpfbuflen]; + bpfbuf.reset(new char [bpfbuflen]); } catch (...) { - bpfbuf = NULL; - } - if (bpfbuf == NULL) { warnx("NetDriverBPF.Open: %s(%s): memory allocation failed", devpath.c_str(), ifname.c_str()); goto abort; @@ -235,7 +235,7 @@ void NetDriverBPF::ThreadRun() { struct kevent kev; - int kq; + autofd kq; int r; kq = kqueue(); @@ -248,7 +248,6 @@ NetDriverBPF::ThreadRun() r = kevent_set(kq, &kev, 1); if (r == -1) { warn("kevent_set(fd=%d) failed", fd); - close(kq); return; } @@ -271,7 +270,7 @@ NetDriverBPF::ThreadRun() // 自動的にデバイスに着信があったことになるはず assert(r > 0); - n = read(fd, bpfbuf, bpfbuflen); + n = read(fd, bpfbuf.get(), bpfbuflen); if (n == -1) { if (errno == EINTR) { continue; @@ -282,10 +281,10 @@ NetDriverBPF::ThreadRun() if (n == 0) { // EOF return; } - bpfptr = bpfbuf; + bpfptr = bpfbuf.get(); bpfcaplen = n; - while (bpfptr < bpfbuf + bpfcaplen) { + while (bpfptr < bpfbuf.get() + bpfcaplen) { uint8 buf[1600]; // BPF ヘッダを除いた部分