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

Binding socket to specific network interface #64

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
22 changes: 22 additions & 0 deletions examples/bind_socket_iface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python
'''
this example is for those who use multiple network interface
In my case, I connected my Raspberry pi's interface
eth0 : to internet
wlan0 : to sony camera, a5100 specifically
Normally pysony doesn't work in this case. Because socket sends ssdp only to eth0
I modified ControlPoint's init function so socket object in ControlPoint to be bound to wlan0
'''

import pysony

print("Searching for camera...")

search = pysony.ControlPoint(interface='wlan0')
cameras = search.discover()

if len(cameras):
camera = pysony.SonyAPI(QX_ADDR=cameras[0])
else:
print("No camera found, aborting")
quit()
4 changes: 3 additions & 1 deletion src/pysony.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@
# Improved with code from 'https://github.com/storborg/sonypy' under MIT license.

class ControlPoint(object):
def __init__(self, addr=SSDP_ADDR, port=SSDP_PORT):
def __init__(self, addr=SSDP_ADDR, port=SSDP_PORT, interface=None):
self.addr, self.port = addr, port
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(0.1)
# Set the socket to broadcast mode.
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
if interface:
sock.setsockopt(socket.SOL_SOCKET, 25, str(interface + '\0').encode('utf-8'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extremely minor nitpick: Everything else in this file looks like it uses 4 spaces for indentation, but you've only got 2 spaces here.

What does 25 mean in the setsockopt call?

Copy link
Owner

@Bloodevil Bloodevil Mar 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use 4 spaces like other line.

is that 25 optname...?
socket.setsockopt(level, optname, value: int)
socket.setsockopt(level, optname, value: buffer)
socket.setsockopt(level, optname, None, optlen: int)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I happened to dig into this myself the other day, 25 is indeed optname and at least in terms of Linux based sockets, it refers to SO_BINDTODEVICE as seen here: https://github.com/torvalds/linux/blob/169e77764adc041b1dacba84ea90516a895d43b2/include/uapi/asm-generic/socket.h

self._udp_socket = sock

def close(self):
Expand Down