def get_sc(self, ifname, address, port, protocol, server):
     sc = SocketConfig()
     sc.bind_port = int(port)
     sc.bind_addr = address
     sc.bind_family = sc.bind_addr.version
     sc.bind_protocol = self.get_protocol(protocol)
     sc.bind_iface = ifname
     sc.bind_iface_idx = self.interfaces[sc.bind_iface].ifindex
     sc.is_server = server
     return sc
Example #2
0
	def get_socketconfig_from_ip(self, ip, port, protocol):
		"""
		Returns a SocketConfig object for the given ip, port, protocol
		:param ip: IP address belonging to an existing interface
		:param port: port numer
		:param protocol: protocol type
		:type protocol: socket.SOCK_STREAM or socket.SOCK_DGRAM
		:return: SocketConfig
		"""
		sc = SocketConfig()
		sc.bind_port = int(port)
		if isinstance(ip, (ipaddress.IPv4Address, ipaddress.IPv6Address)):
			sc.bind_addr = ip
		else:
			sc.bind_addr = ipaddress.ip_address(ip)

		sc.bind_family = sc.bind_addr.version

		if isinstance(protocol, str):
			if protocol.lower() == 'tcp':
				sc.bind_protocol = socket.SOCK_STREAM
			elif protocol.lower() == 'udp':
				sc.bind_protocol = socket.SOCK_DGRAM
			else:
				raise Exception('Unknown protocol definition %s' % protocol)
		elif isinstance(protocol, int):
			sc.bind_protocol = protocol
		else:
			raise Exception('Unknown protocol definition %s' % protocol)

		sc.bind_iface = self.ip_name_lookup[sc.bind_addr]
		sc.bind_iface_idx = self.interfaces[sc.bind_iface].ifindex

		return sc
Example #3
0
	def get_socketconfig(self, ifname, port, protocol, ipversion = None):
		"""
		Returns a list of socketconfig objects to create server.
		:param ifname: Interface name
		:type ifname: str
		:param port: Port number
		:type port: int
		:param protocol: Protocol type
		:type protocol: str or int or socket.SOCK_STREAM/socket.SOCK_DGRAM
		:param ipversion: IP address version
		:return: list of SocketConfig
		"""
		if ifname not in self.interfaces:
			raise Exception('Could not find ifname %s!' % ifname)

		scl = []
		try:
			iv = []
			if ipversion is None:
				iv.append(4)
				iv.append(6)
			elif isinstance(ipversion, str):
				if int(ipversion) in [4,6]:
					iv.append(int(ipversion))
				else:
					raise Exception()

			elif isinstance(ipversion, int):
				if ipversion in [4,6]:
					iv.append(ipversion)
				else:
					raise Exception()

			elif isinstance(ipversion, list):
				iv = ipversion

		except Exception as e:
			raise Exception('Unknown IP version %s' % repr(ipversion))

		for version in iv:
			for lookup_ifname, ver in self.name_ip_lookup:
				if ifname != lookup_ifname:
					continue
				if ver != version:
					break
				for address in self.name_ip_lookup[(lookup_ifname, ver)]:
					sc = SocketConfig()
					sc.bind_port = int(port)
					sc.bind_addr = address
					sc.bind_family = sc.bind_addr.version

					if isinstance(protocol, str):
						if protocol.lower() == 'tcp':
							sc.bind_protocol = socket.SOCK_STREAM
						elif protocol.lower() == 'udp':
							sc.bind_protocol = socket.SOCK_DGRAM
						else:
							raise Exception('Unknown protocol definition %s' % protocol)
					elif isinstance(protocol, int):
						sc.bind_protocol = protocol
					else:
						raise Exception('Unknown protocol definition %s' % protocol)

					sc.bind_iface = ifname
					sc.bind_iface_idx = self.interfaces[sc.bind_iface].ifindex
					sc.is_server = True
					scl.append(sc)

		return scl
Example #4
0
	def get_client_socketconfig(self, ifname, protocol, ipversion = None, reuse_address = False, reuse_port= False):
		if ifname not in self.interfaces:
			raise Exception('Could not find ifname %s!' % ifname)

		scl = []
		try:
			iv = []
			if ipversion is None:
				iv.append(4)
				iv.append(6)
			elif isinstance(ipversion, str):
				if int(ipversion) in [4,6]:
					iv.append(int(ipversion))
				else:
					raise Exception()

			elif isinstance(ipversion, int):
				if ipversion in [4,6]:
					iv.append(ipversion)
				else:
					raise Exception()

			elif isinstance(ipversion, list):
				iv = ipversion

		except Exception as e:
			raise Exception('Unknown IP version %s' % repr(ipversion))

		for version in iv:
			for lookup_ifname, ver in self.name_ip_lookup:
				if ifname != lookup_ifname:
					continue
				if ver != version:
					break
				for address in self.name_ip_lookup[(lookup_ifname, ver)]:
					sc = SocketConfig()
					sc.bind_port = 0
					sc.bind_addr = address
					sc.bind_family = sc.bind_addr.version

					if isinstance(protocol, str):
						if protocol.lower() == 'tcp':
							sc.bind_protocol = socket.SOCK_STREAM
						elif protocol.lower() == 'udp':
							sc.bind_protocol = socket.SOCK_DGRAM
						else:
							raise Exception('Unknown protocol definition %s' % protocol)
					elif isinstance(protocol, int):
						sc.bind_protocol = protocol
					else:
						raise Exception('Unknown protocol definition %s' % protocol)

					sc.bind_iface = ifname
					sc.bind_iface_idx = self.interfaces[sc.bind_iface].ifindex
					sc.is_server = False
					sc.reuse_address = reuse_address
					sc.reuse_port = reuse_port
					scl.append(sc)

		return scl