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

Rewrite get_process_connections() in C for Windows #115

Closed
giampaolo opened this issue May 23, 2014 · 12 comments
Closed

Rewrite get_process_connections() in C for Windows #115

giampaolo opened this issue May 23, 2014 · 12 comments

Comments

@giampaolo
Copy link
Owner

From g.rodola on October 11, 2010 12:18:02

Current implementation parses netstat.exe output: 
https://code.google.com/p/psutil/source/browse/trunk/psutil/_psmswindows.py?spec=svn670&r=669#176
 It would be good for both speed and a more reliable error management 
to rewrite this in C.

Original issue: http://code.google.com/p/psutil/issues/detail?id=115

@giampaolo giampaolo self-assigned this May 23, 2014
@giampaolo
Copy link
Owner Author

From [email protected] on October 12, 2010 23:05:09

Patch. This will only work on XP SP2 and above, so you may want to 
add a fallback method if needed.

Attachment: network.diff

@giampaolo
Copy link
Owner Author

From g.rodola on October 13, 2010 11:19:35

Thanks a lot for this; I'll try it soon and let you know how it goes.

@giampaolo
Copy link
Owner Author

From g.rodola on October 14, 2010 16:13:19

Committed in r679 and r680 .

As for Windows < XP-SP2 and other incompatible versions  it's fine 
for me to fall back on parsing netstat output but I'm not sure on how to proceed.
I think one way to do this is to decide whether calling the C 
function at runtime from Python by previously detecting the Windows 
version although it's gonna be a bit tricky.

The support for GetExtended[Tcp/Udp]Table functions is listed here: 
http://www.ureader.com/message/3207232.aspx ...and forces us to check 
4 different Windows platforms including the SP version installed.

By using GetVersionEx(): 
http://msdn.microsoft.com/en-us/library/ms724451(v=VS.85).aspx ...we 
can detect both OS and SP versions.

Python exposes GetVersionEx() via sys.getwindowsversion() function 
which on my Windows 7 returns (6, 1, 7600, 2, '') and on Windows XP 
returns (5, 1, 2600, 2, 'Service Pack 3'). 
As for an approximate list of name mappings we can take a look here: 
http://www.msigeek.com/442/windows-os-version-numbers This doesn't 
include Windows server versions though.

If this is gonna be too tricky it's fine for me remove support for 
incompatible Windows versions. 
Question is how to detect this at runtime and avoid to call the underlying C code.

@giampaolo
Copy link
Owner Author

From g.rodola on October 15, 2010 14:31:53

I managed to install a Windows 2000 VM and implemented the logic 
described above in r685 .
We can't rely on netstat.exe because the -o option is not available 
at all, in which case NotImplementedError is just raised: 
http://articles.techrepublic.com.com/5100-10878_11-5630313.html

@giampaolo
Copy link
Owner Author

From g.rodola on October 16, 2010 12:38:26

I'd like to include socket file descriptor number as well.
wj32, do you know if this is possible on Windows?

@giampaolo
Copy link
Owner Author

From [email protected] on October 16, 2010 15:29:07

Windows doesn't have "file descriptor numbers" per se. Do you mean 
handle value? I don't think it's possible.

@giampaolo
Copy link
Owner Author

From g.rodola on October 17, 2010 13:53:31

I mean socket file descriptors as intended in the UNIX world which 
basically are integers that can be used in later function calls that 
operate on sockets, like select(), for example.
I know that Python implements this concept also for Windows sockets:

>>> import socket
>>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> s.fileno()
3
>>> 

Starting from that integer one might get a duplicate socket object 
and then perform actions against it, like closing the socket/connection:

>>> dup_socket = socket.fromfd(s.fileno(), socket.AF_INET, socket.SOCK_STREAM)
>>> dup_socket
<socket object, fd=4, family=2, type=1, protocol=0>
>>> dup_socket.close()
>>>

@giampaolo
Copy link
Owner Author

From g.rodola on October 17, 2010 14:06:15

If this can't be done on Windows then it's ok for me to return -1 but 
I'd like to provide support on UNIX because closing process 
connections is something that might be useful.
I'm gonna modify Linux and BSD/OSX implementations so that they 
return the fd as well.

@giampaolo
Copy link
Owner Author

From [email protected] on October 17, 2010 19:14:19

If you want to close network connections, you don't need any handles. 
A SetTcpEntry call will suffice.

@giampaolo
Copy link
Owner Author

From g.rodola on October 29, 2010 10:36:55

Unfortunately I see no way for SetTcpEntry to fit in the current API 
in a cross-platform way.
Closing this out as fixed and thanks again for the great work.

Status: FixedInSVN

@giampaolo
Copy link
Owner Author

From g.rodola on November 12, 2010 19:14:59

Status: Fixed

@giampaolo
Copy link
Owner Author

From g.rodola on March 02, 2013 03:55:09

Updated csets after the SVN -> Mercurial migration: r679 == revision 
b9a8709f88f2 r685 == revision ec2059e2a06e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant