Skip to content

Commit

Permalink
fix handle closing
Browse files Browse the repository at this point in the history
  • Loading branch information
doronz88 committed Apr 11, 2024
1 parent 77b5124 commit 6748a95
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 2 additions & 0 deletions darwin_pytun.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
8 changes: 6 additions & 2 deletions pytun_pmd3/wintun.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit 6748a95

Please sign in to comment.