Repository navigation
hs.socket: allow listening on a specific interface - #3899
Open
anandghegde wants to merge 1 commit into
Open
anandghegde wants to merge 1 commit into
anandghegde wants to merge 1 commit into
Conversation
hs.socket:listen() and hs.socket.udp:listen() take an optional interface argument, so a server socket can be bound to one interface instead of every one. Useful for talking to a VM over a bridge interface, or for keeping a socket reachable only from this machine. CocoaAsyncSocket already has -acceptOnInterface:port:error: and -bindToPort:interface:error:, both of which are the single-argument variants when the interface is nil, so omitting it behaves exactly as before. Unix domain sockets aren't bound to an interface, so passing one alongside a path logs a warning and is ignored. hs.socket.server() and hs.socket.udp.server() grow the same optional argument between the port and the callback. Fixes Hammerspoon#3634
Author
|
CI is asking for one of the |
Author
|
The required |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3634.
Both
hs.socketandhs.socket.udpserver sockets currently bind to every interface, with no way to narrow that down. As @cmsj noted on the issue, CocoaAsyncSocket already exposes the APIs for this:What changed
listen()on both extensions takes an optionalinterfacestring:The interface can be a name (
"en1","lo0"), the corresponding IP address ("192.168.4.34"), or the special strings"localhost"/"loopback"— whateverGCDAsyncSocketaccepts.socket_listen()now calls-acceptOnInterface:port:error:instead of-acceptOnPort:error:socketudp_listen()now calls-bindToPort:interface:error:instead of-bindToPort:error:hs.socket.server()/hs.socket.udp.server()take the same optional argument between the port and the callbackBackwards compatibility
Passing no interface is unchanged. In CocoaAsyncSocket,
-acceptOnPort:error:is literallyreturn [self acceptOnInterface:nil port:port error:errPtr];, and-bindToPort:error:isreturn [self bindToPort:port interface:nil error:errPtr];, so the nil path is the same code as before.The
server()constructors shuffle their arguments when the second one is a function, sohs.socket.server(port, fn)keeps working.Unix domain sockets aren't bound to an interface, so
hs.socket:listen(path, interface)logs a warning and ignores the interface rather than failing.Tests
Five new tests in
test_socket.lua/test_udpsocket.lua, registered inHammerspoon Tests/HSsocket.m:testTcpListenerSocketInterfaceAttributes/testUdpListenerSocketInterfaceAttributes— a listener bound to"loopback"reportslocalHost == "127.0.0.1"rather than"0.0.0.0"testTcpListenerSocketInterfaceConnect— a client can still connect to a loopback-bound server and the server reads what it wrotetestTcpListenerSocketBadInterface/testUdpListenerSocketBadInterface— an unknown interface returnsniland leaves the socket unbound; a Unix domain socket ignores the interfaceLocal run of the
HSsocketsuite: 46 passed, 1 failed. The failure istestUdpReusePort, which also fails on an unmodifiedmasterhere — it broadcasts to255.255.255.255, which this machine doesn't allow.