Python socket.SO_BINDTODEVICE Examples

The following are 5 code examples of socket.SO_BINDTODEVICE(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module socket , or try the search function .
Example #1
Source File: pysoxy.py    From pysoxy with GNU General Public License v2.0 6 votes vote down vote up
def connect_to_dst(dst_addr, dst_port):
    """ Connect to desired destination """
    sock = create_socket()
    if OUTGOING_INTERFACE:
        try:
            sock.setsockopt(
                socket.SOL_SOCKET,
                socket.SO_BINDTODEVICE,
                OUTGOING_INTERFACE.encode(),
            )
        except PermissionError as err:
            print("Only root can set OUTGOING_INTERFACE parameter")
            EXIT.set_status(True)
    try:
        sock.connect((dst_addr, dst_port))
        return sock
    except socket.error as err:
        error("Failed to connect to DST", err)
        return 0 
Example #2
Source File: dhcp_server.py    From dnxfirewall-cmd with GNU General Public License v3.0 5 votes vote down vote up
def listener_sock(self):
        l_sock = socket(AF_INET, SOCK_DGRAM)
        l_sock.setsockopt(SOL_SOCKET, SO_REUSEADDR,1)
        l_sock.setsockopt(SOL_SOCKET, SO_BROADCAST,1)
        l_sock.setsockopt(SOL_SOCKET, SO_BINDTODEVICE, f'{self._intf}\0'.encode('utf-8'))
        l_sock.bind((str(INADDR_ANY), PROTO.DHCP_SVR))

        return l_sock 
Example #3
Source File: upnp_console.py    From rext with GNU General Public License v3.0 5 votes vote down vote up
def initialize_sockets(self):
        try:
            # This is needed to join a multicast group
            self.mreq = struct.pack("4sl", socket.inet_aton(self.host), socket.INADDR_ANY)
            # Set up client socket
            self.csock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            self.csock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
            # Set up server socket
            self.ssock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
            self.ssock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            # BSD systems also need to set SO_REUSEPORT
            try:
                self.ssock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
            except Exception:
                pass

            # Only bind to this interface
            if self.interface is not None:
                print_info("Binding to interface: " + self.interface)
                self.ssock.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE,
                                      struct.pack("%ds" % (len(self.interface) + 1,), self.interface))
                self.csock.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE,
                                      struct.pack("%ds" % (len(self.interface) + 1,), self.interface))

            try:
                self.ssock.bind(('', self.port))
            except Exception:
                print_warning("failed to bind: " + self.host + ":" + str(self.port) + " ")
            try:
                self.ssock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, self.mreq)
            except Exception:
                print_warning("failed to join multicast group")
        except Exception:
            print_error("failed to initialize UPNP sockets")
            return False
        return True

    # Clean up file/socket descriptors 
Example #4
Source File: lan_buster.py    From TOOLS with GNU General Public License v3.0 5 votes vote down vote up
def Main(self):
        self.sock = socket(AF_INET, SOCK_DGRAM)
        self.sock.setsockopt(SOL_SOCKET, SO_REUSEADDR,1)
        self.sock.setsockopt(SOL_SOCKET, SO_BROADCAST,1)
        self.sock.setsockopt(SOL_SOCKET, SO_BINDTODEVICE, self.bind_int)
        self.sock.bind(('0.0.0.0', 68)) 
Example #5
Source File: core.py    From knxmap with GNU General Public License v3.0 4 votes vote down vote up
def _knx_search_worker(self):
        """Send a KnxSearch request to see if target is a KNX device."""
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            sock.setblocking(0)
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE,
                            struct.pack('256s', str.encode(self.iface)))
            protocol = KnxGatewaySearch(multicast_addr=self.multicast_addr,
                                        port=self.port)
            waiter = asyncio.Future(loop=self.loop)
            transport = self.loop._make_datagram_transport(
                sock, protocol, (self.multicast_addr, self.port), waiter)
            try:
                # Wait until connection_made() has been called on the transport
                yield from waiter
            except asyncio.CancelledError:
                LOGGER.error('Creating multicast transport failed!')
                transport.close()
                return

            # Wait SEARCH_TIMEOUT seconds for responses to our multicast packets
            yield from asyncio.sleep(self.search_timeout)

            if protocol.responses:
                if True:
                    # TODO: check if we want diagnostic requests as well
                    print('sending diagnostic request')
                    protocol.send_diagnostic_request()
                    yield from asyncio.sleep(self.search_timeout)

                # If protocol received SEARCH_RESPONSE packets, print them
                for response in protocol.responses:
                    peer = response[0]
                    response = response[1]
                    t = KnxTargetReport(
                        host=peer[0],
                        port=peer[1],
                        mac_address=response.dib_dev_info.get('knx_mac_address'),
                        knx_address=response.dib_dev_info.get('knx_address'),
                        device_serial=response.dib_dev_info.get('knx_device_serial'),
                        friendly_name=response.dib_dev_info.get('device_friendly_name'),
                        device_status=response.dib_dev_info.get('device_status'),
                        knx_medium=response.dib_dev_info.get('knx_medium'),
                        project_install_identifier=response.dib_dev_info.get('project_install_identifier'),
                        supported_services=[
                            KNX_SERVICES[k] for k, v in
                            response.dib_supp_sv_families.get('families').items()],
                        bus_devices=[])

                    self.knx_gateways.append(t)
        except asyncio.CancelledError:
            pass