diff --git a/darwin_pytun.c b/darwin_pytun.c index edbf62e..96f5528 100644 --- a/darwin_pytun.c +++ b/darwin_pytun.c @@ -286,11 +286,13 @@ static PyObject *pytun_tuntap_down(PyObject *self) { memset(&req, 0, sizeof(req)); strcpy(req.ifr_name, tuntap->name); if (ioctl(tuntap->fd, SIOCGIFFLAGS, &req) < 0) { + raise_error_from_errno(); return NULL; } if (req.ifr_flags & IFF_UP) { req.ifr_flags &= ~IFF_UP; if (ioctl(tuntap->fd, SIOCSIFFLAGS, &req) < 0) { + raise_error_from_errno(); return NULL; } } diff --git a/pytun_pmd3/wintun.py b/pytun_pmd3/wintun.py index fdf4f4d..77bb9e7 100644 --- a/pytun_pmd3/wintun.py +++ b/pytun_pmd3/wintun.py @@ -247,7 +247,10 @@ def interface_index(self) -> int: return self.ip_interface_entry.InterfaceIndex def close(self) -> None: - wintun.WintunCloseAdapter(self.handle) + self.down() + if self.handle is not None: + wintun.WintunCloseAdapter(self.handle) + self.handle = None @property def addr(self) -> str: @@ -270,7 +273,8 @@ def up(self, capacity: int = DEFAULT_RING_CAPCITY) -> None: self.session = wintun.WintunStartSession(self.handle, capacity) def down(self) -> None: - wintun.WintunEndSession(self.session) + if self.session is not None: + wintun.WintunEndSession(self.session) self.session = None def read(self) -> bytes: