Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix handle closing #7

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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