-
-
Notifications
You must be signed in to change notification settings - Fork 346
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
High level API for accessing getsockname() / getpeername() #280
Comments
Small wrinkle to watch out for with AF_UNIX sockets: #279 (comment) |
We'll also want to think about this: https://bugs.python.org/issue32221 tl;dr: in some versions of python, This also caused issues for twisted: https://twistedmatrix.com/trac/ticket/9449#9449 |
Maybe something like: # Empty type for docs and to indicate intention
class Address(ABC):
pass
@attr.s(frozen=True, slots=True)
class SocketAddress(Address):
socket_family: socket.AddressFamily
socket_address: Any # the same types as returned by getsockname etc.
@property
def ip(self) -> str:
if self.socket_family in [AF_INET, AF_INET6]:
return self.socket_address[0]
raise AttributeError("ip")
@property
def port(self) -> int:
if self.socket_family in [AF_INET, AF_INET6]:
return self.socket_address[1]
raise AttributeError("port")
@property
def path(self) -> Optional[str]: # or is it bytes?
if self.socket_family == AF_UNIX:
return self.socket_address
raise AttributeError("path")
def __str__(self):
if self.socket_family == AF_INET:
return f"{self.ip}:{self.port}"
if self.socket_family == AF_INET6:
return f"[{self.ip}]:{self.port}" # FIXME: what about scopeid and the other thing?
if self.socket_family == AF_UNIX:
return f"unix://{self.path}"
...
class Stream:
async def get_local_name(self) -> Optional[Address]: ...
async def get_remote_name(self) -> Optional[Address]: ... |
I'd love to see this implemented. Paths are definitely |
Just passing by because of the |
@altendky I gather that paths are always |
I presume there are functions that always return paths as
|
Ok, I stand corrected. |
There needs to be some way to get at the information in
getsockname
andgetpeername
from the high-levelStream
andListener
interfaces.It should involve an
await
, at least on streams, to support the PROXY protocol.What should we do with
SSLStream
/SSLListener
? There's a conceptual problem where the stream they're talking about doesn't exactly have an address, it's "connect to the transport's address AND THEN wrap the thing in aSSLStream
". But then, it's not likegetsockname
/getpeername
exactly has any meaning for streams anyway -- like, if you have a connected socket it's useful to be able to ask it what the two halves of the TCP 5-tuple looks like, but you can't actually do anything with this, it doesn't satisfy any invariants. Except "this might be useful for forensics".So maybe pragmatics beats purity and
SSLStream
should just proxy this through to the underlying transport, and we'll leave the user to figure out how to decipher it.Maybe a
SocketAddress
object that hasfamily
andsockaddr
fields, plus possiblyhost
,port
,path
, etc., as appropriate for the particular address?The text was updated successfully, but these errors were encountered: