def get_gcp_info(self, args=None): """ :return: gcp info """ gcp_info = [] try: agent_id = ProcessAgent.AGENTTYPE_GCP for ccap_core_id in CCAPCore.ccap_core_db: ccap_core = CCAPCore.ccap_core_db[ccap_core_id] status = ccap_core.agent_status[agent_id] para = ccap_core.parameters principal = ccap_core.is_principal is_ipv6 = Convert.is_valid_ipv6_address(ccap_core.ccap_core_network_address) family = (socket.AF_INET, socket.AF_INET6)[is_ipv6] if agent_id in para and ';' in para[agent_id]: intf, core_ip = para[agent_id].split(';') local_ip = SysTools.get_ip_address(str(intf), family=family) value = { "Core-ID": ccap_core_id, "Core-IP": core_ip, "Local-IP": local_ip, "Principal": 'Yes' if principal == CoreDescription.CORE_ROLE_PRINCIPAL else 'No', 'Status': status} gcp_info.append(value) return True, gcp_info except Exception as e: return False, str(e)
def add_ccap_cores(self, ccap_cores, port_master=GCPSessionDescriptor.DEFAULT_PORT_MASTER): """Create GCP descriptors based on addresses of CCAP cores received from DHCP server to orchestrator. :param ccap_cores: list of "interface;core ip" :type ccap_cores: list(string) :return: """ descriptors = [] for core_addr in ccap_cores: interface, core = core_addr.split(';') if not Convert.is_valid_ip_address(core): self.logger.warn("Malformed IP address received: %s", core) continue is_ipv6 = Convert.is_valid_ipv6_address(core) family = (AF_INET, AF_INET6)[is_ipv6] addr_local = SysTools.get_ip_address(str(interface), family) # TODO pass also local address to use specific interface descriptors.append( GCPSlaveDescriptor(core, port_master=port_master, addr_local=addr_local, interface_local=interface, addr_family=family)) self.orchestrator.add_sessions(descriptors)
def __init__(self, localAddr, connID): """Init the L2TP socket and bind to it. :param localAddr: The local IP address that we want to bind L2TP socket to it. :param connID: The connection ID, in L2tpv3 domain, it means the lcoal connection ID. """ self.socket = None self.addr = localAddr self.connID = connID if Convert.is_valid_ipv4_address(localAddr): self.socket = socket.socket( socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_L2TP) self.socket.setblocking(False) # Set the socket to non-block mode self.socket.bind((localAddr, connID)) self.logger.info( "Create L2TP socket and bind to it, local IP address:%s, local Connection ID:%d, socket: %d" % (localAddr, connID, self.socket.fileno())) elif Convert.is_valid_ipv6_address(localAddr): self.socket = socket.socket( socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_L2TP) self.socket.setblocking(False) # Set the socket to non-block mode self.socket.bind(('', connID)) self.logger.info( "Create L2TP socket and bind to any adress, local Connection ID:%d, socket: %d" % (connID, self.socket.fileno())) else: self.logger.info( "Create L2TP socket failed, invalid local IP address:%s, local Connection ID:%d" % (localAddr, connID)) self.socket = None
def get_dhcp_info(self, args=None): """ :return: dhcp info """ dhcp_info = {'Interface': [], 'Details': []} try: nic_info = net_if_addrs() for intf in self.mgr.dhcp_parameter: netmask = '0.0.0.0' is_ipv6 = Convert.is_valid_ipv6_address(self.mgr.dhcp_parameter[intf]['CCAPCores'][0]) family = (socket.AF_INET, socket.AF_INET6)[is_ipv6] for item in nic_info[intf]: if item.family == family: netmask = item.netmask break local_ip = SysTools.get_ip_address(str(intf), family=family) value = {"Interface": intf, 'IP-Address': local_ip, "Subnet-Mask": netmask} dhcp_info['Interface'].append(value) addr_type = "IPv4" if is_ipv6: if self.mgr.dhcp_parameter[intf]['Slaac']: addr_type = "IPv6<Stateless>" else: addr_type = "IPv6<Stateful>" intf_dhcp_parameter = self.mgr.dhcp_parameter[intf] intf_dhcp_parameter["AddrType"] = addr_type dhcp_info['Details'].append((intf, intf_dhcp_parameter)) return True, dhcp_info except Exception as e: return False, str(e)
def test_type_checkers(self): # is_valid_ipv4_address self.assertTrue(Convert.is_valid_ipv4_address('192.168.16.2')) self.assertTrue(Convert.is_valid_ipv4_address('255.255.255.255')) self.assertFalse(Convert.is_valid_ipv4_address('192.168.16.')) self.assertFalse(Convert.is_valid_ipv4_address('.168.16.48')) self.assertFalse(Convert.is_valid_ipv4_address('test')) self.assertFalse(Convert.is_valid_ipv4_address('5')) self.assertFalse(Convert.is_valid_ipv4_address('1.1.1.1,2.2.2.2')) # is_valid_ipv6_address self.assertFalse(Convert.is_valid_ipv6_address('192.168.16.2')) self.assertTrue(Convert.is_valid_ipv6_address('::')) self.assertTrue(Convert.is_valid_ipv6_address('1::')) self.assertTrue(Convert.is_valid_ipv6_address('1::5')) self.assertTrue(Convert.is_valid_ipv6_address('1::5:5')) self.assertFalse(Convert.is_valid_ipv6_address('myhost.com')) self.assertFalse(Convert.is_valid_ipv6_address('test')) # is_valid_ip_address self.assertTrue(Convert.is_valid_ip_address('192.168.16.2')) self.assertTrue(Convert.is_valid_ip_address('1::5')) self.assertFalse(Convert.is_valid_ip_address('myhost.com')) # is_int_value self.assertTrue(Convert.is_int_value('-1')) self.assertTrue(Convert.is_int_value('0')) self.assertTrue(Convert.is_int_value('5000')) self.assertTrue(Convert.is_int_value('5000000000')) self.assertFalse(Convert.is_int_value('test')) self.assertFalse(Convert.is_int_value('5.5')) self.assertFalse(Convert.is_int_value('5,5'))
def getLocalIp(core_ip): intf = 'eth0' family = socket.AF_INET if core_ip is not None: is_ipv6 = Convert.is_valid_ipv6_address(core_ip) family = (socket.AF_INET, socket.AF_INET6)[is_ipv6] if core_ip is None or core_ip == "127.0.0.1" or core_ip == "::1": intf = 'eth0' else: intf = SysTools.get_interface() local_ip = SysTools.get_ip_address(intf, family) if local_ip is None: return globalSettings.L2tpv3GlobalSettings.LocalIPAddress return local_ip
def upload_with_tftp(self, crashFileCtrlInfo, crashDataServerInfo): """ upload the crash file in tftp mode :param tftp_options: :param crashFileCtrlInfo: :return: """ tftp_options = {} if Convert.is_valid_ipv4_address(str(crashDataServerInfo.destIpAddress)): blksize = 1042 tftp_options['blksize'] = int(blksize) elif Convert.is_valid_ipv6_address(str(crashDataServerInfo.destIpAddress)): blksize = 1048 tftp_options['blksize'] = int(blksize) fileName = crashFileCtrlInfo.fileName if crashDataServerInfo.destPath.endswith('/'): destFileName = crashDataServerInfo.destPath + fileName else: destFileName = crashDataServerInfo.destPath + '/' + fileName srcFileName = CrashFileCtrlHandler.CORE_FILE_PATH + fileName self.logger.debug("Tftp upload destFileName:%s srcFileName:%s destIpAddress:%s" % (destFileName, srcFileName, crashDataServerInfo.destIpAddress)) try: self.update_crash_file_status(CrashFileCtrlHandler.STATUS_UPLOADINPROGRESS, crashFileCtrlInfo) tclient = tftpy.TftpClient(crashDataServerInfo.destIpAddress, int(self.TFTP_PORT), tftp_options) tclient.upload(str(destFileName), str(srcFileName)) self.logger.debug("tftp upload %s complete", srcFileName) self.update_crash_file_status(CrashFileCtrlHandler.STATUS_UPLOADCOMPLETED, crashFileCtrlInfo) except TftpTimeout as err: self.update_crash_file_status(CrashFileCtrlHandler.STATUS_ERROR, crashFileCtrlInfo) self.logger.warn( "Error: File {0:s} fail to upload with TftpException {1:s}" .format(fileName, str(err))) except TftpException as err: self.update_crash_file_status(CrashFileCtrlHandler.STATUS_ERROR, crashFileCtrlInfo) self.logger.warn("Error: File %s fail to upload with TftpException %s" % (fileName, str(err))) except Exception as err: self.update_crash_file_status(CrashFileCtrlHandler.STATUS_ERROR, crashFileCtrlInfo) self.logger.warn("Error: File %s fail to upload with TftpException %s" % (fileName, str(err))) if tclient.context is not None: tclient.context.end()
def upload_with_http(self, crashFileCtrlInfo, crashDataServerInfo): """ upload the crash file in http mode :param crashFileCtrlInfo: :return: """ corefilepath = CrashFileCtrlHandler.CORE_FILE_PATH + crashFileCtrlInfo.fileName self.logger.debug("Upload crash file %s in http mode" % corefilepath) self.update_crash_file_status(CrashFileCtrlHandler.STATUS_UPLOADINPROGRESS, crashFileCtrlInfo) if Convert.is_valid_ipv6_address( str(crashDataServerInfo.destIpAddress)): httpServer = "[" + str(crashDataServerInfo.destIpAddress) + "]" else: httpServer = str(crashDataServerInfo.destIpAddress) try: dataForm = MultiDataForm() with open(corefilepath, 'rb') as uploadFile: dataForm.add_file('file', crashFileCtrlInfo.fileName, uploadFile) if not str(crashDataServerInfo.destPath): url = 'http://' + httpServer + ':' + str(self.HTTP_PORT) else: url = 'http://' + httpServer + ':' + str(self.HTTP_PORT) + \ str(crashDataServerInfo.destPath) self.logger.info("Http url is %s" % url) request = urllib2.Request(url) body = str(dataForm) request.add_header('Content-type', dataForm.get_content_type()) request.add_header('Content-length', len(body)) request.add_data(body) urllib2.urlopen(request).read() self.logger.debug("Http upload %s compplete" % (crashFileCtrlInfo.fileName)) self.update_crash_file_status(CrashFileCtrlHandler.STATUS_UPLOADCOMPLETED, crashFileCtrlInfo) except urllib2.HTTPError as e: self.update_crash_file_status(CrashFileCtrlHandler.STATUS_ERROR, crashFileCtrlInfo) self.logger.warn("HTTP Error %s fail to upload: %s" % (corefilepath, str(e))) except urllib2.URLError as e: self.update_crash_file_status(CrashFileCtrlHandler.STATUS_ERROR, crashFileCtrlInfo) self.logger.error("HTTP Error %s fail to upload: %s" % (corefilepath, str(e))) except Exception as e: self.update_crash_file_status(CrashFileCtrlHandler.STATUS_ERROR, crashFileCtrlInfo) self.logger.error("HTTP Error %s fail to upload: %s" % (corefilepath, str(e)))
def get_interface_info(self, args=None): """ :return: interface info """ ret_info = [] try: agent_id = ProcessAgent.AGENTTYPE_INTERFACE_STATUS for ccap_core_id in CCAPCore.ccap_core_db: ccap_core = CCAPCore.ccap_core_db[ccap_core_id] status = ccap_core.agent_status[agent_id] intf = ccap_core.parameters[agent_id] is_ipv6 = Convert.is_valid_ipv6_address(ccap_core.ccap_core_network_address) family = (socket.AF_INET, socket.AF_INET6)[is_ipv6] local_ip = SysTools.get_ip_address(str(intf), family=family) value = {"Registered-Cores": ccap_core_id, "Interface": intf, "IP": local_ip, 'Status': status} ret_info.append(value) return True, ret_info except Exception as e: return False, str(e)
def _ondhcp_ack(self, event): """DHCP data are ready, parse msg with info from DCHP client. (without checking if mandatory data are here - these are checked during initialization process) and start with next step - get & set system time :param event: unused :return: """ del event # DHCP data updated - remove old CCAP Cores and close connections rcp_msg = t_RcpMessage() rcp_msg.RcpMessageType = rcp_msg.REMOVE_ALL_CCAP_CORES self.send_msg_to_rcp(rcp_msg) # Clear DHCP data from DB, if there are any from previous DHCP Ack self.delete_dhcp_data() self._verify_dhcp_data() if len(self.dhcp_data.TimeServers) == 0: self.logger.error("No time servers found") self.fsm.fatal_failure() return is_ipv6 = Convert.is_valid_ipv6_address(self.dhcp_data.TimeServers[0]) # Prepare TPC runtime arguments args = ['--ipv6'] if is_ipv6 else [] args.extend(['--offset', str(self.dhcp_data.TimeOffset)]) args.extend(['--servers'] + [ addr.encode('ascii', 'ignore') for addr in self.dhcp_data.TimeServers ]) # Start TPC # (continue in tpc_msg_cb or timeout_cb specified in process info) self.processes['tpc'].start(self.tpc_msg_cb, args)
i07_file = "/conf/I07Field_v4.json" if sys.argv[-1] != "--help" and sys.argv[-1] != arg_ipv6: # check if there are some arguments try: while len(sys.argv) >= 2: if sys.argv[-1] == arg_send_no_wait: send_no_wait = True # pop the argument sys.argv.pop() elif sys.argv[-2] == arg_use_interface: local_ip = str(sys.argv[-1]) addr_family = (socket.AF_INET, socket.AF_INET6 )[Convert.is_valid_ipv6_address(local_ip)] # pop argument and it's value sys.argv.pop() sys.argv.pop() elif sys.argv[-2] == arg_pkt_count: scale_test_packets = int(sys.argv[-1]) # pop argument and it's value sys.argv.pop() sys.argv.pop() else: raise Exception() except Exception as ex: MasterAPI.logger.info("Invalid arguments: %s:: %s", sys.argv, ex)
def process_event_action(self, action): """Process the request from the client. Currently, we will support the following event: start/check status/stop. :param action: the protobuf object, which contains the event information. :return: the function will return an message to remote, success or fail. """ ccap_id = action.ccap_core_id event_action = action.action self.logger.debug("CCAP core[%s] issued an event action:%s", ccap_id, action) if ccap_id not in self.ccap_cores: self.logger.warn("Cannot process the event action for id %s, reason: id is not registered" % ccap_id) self._send_event_notification(ccap_id, protoDef.msg_core_event_notification.FAIL, "CCAP core ID is not registered") return if not action.HasField("parameter"): self.logger.warn( "Cannot process the event action for id %s, reason:Parameter is not set" % ccap_id) # return error self._send_event_notification( ccap_id, protoDef.msg_core_event_notification.FAIL, "Parameter is not set") return parameter = action.parameter # parameter is a string, we need to parse the string parameters = parameter.split(";") interface = parameters[0].strip() ccap_ip = parameters[1].strip() is_ipv6 = Convert.is_valid_ipv6_address(ccap_ip) family = (socket.AF_INET, socket.AF_INET6)[is_ipv6] # Get the interface IP address ip_addr = SysTools.get_ip_address(str(interface), family) if event_action == protoDef.msg_event.START or event_action == protoDef.msg_event.CHECKSTATUS: if ip_addr is None: self.logger.warn( "Cannot start/check l2tp status for id %s, " "reason:cannot find the ip addr for interface %s" % (ccap_id, interface)) self._send_event_notification( ccap_id, protoDef.msg_core_event_notification.FAIL, "cannot find the ip addr for interface %s" % interface) return # check if we have start the l2tp address for this IP address ret, reason = self.l2tp_dispatcher.register_local_address(ip_addr) if not ret: self.logger.warn("Cannot start/check l2tp status for id %s, " "reason:%s" % (ccap_id, reason)) self._send_event_notification( ccap_id, protoDef.msg_core_event_notification.FAIL, reason) return # check if we are in the requester list, if yes, we just # send a current status to it if (ip_addr, ccap_ip) not in self.l2tp_status: # create a interface in self interfaces self.l2tp_status[(ip_addr, ccap_ip)] = { "status": self.DOWN, "lastChangeTime": time(), "ccap_core_id": ccap_id, } self.l2tp_dispatcher.register_remote_address(ccap_ip) # check the l2tp status for k in L2tpConnection.ConnectionDb: connection = L2tpConnection.ConnectionDb[k] if connection.remoteAddr == ccap_ip and connection.localAddr == ip_addr: self.l2tp_status[(ip_addr, ccap_ip)]['status'] = self.UP break self._send_event_notification( ccap_id, protoDef.msg_core_event_notification.OK, reason="Id has been issue this action, send current status to you", result=self.l2tp_status[(ip_addr, ccap_ip)]["status"]) return if event_action == protoDef.msg_event.STOP: for k in L2tpConnection.ConnectionDb: connection = L2tpConnection.ConnectionDb[k] """ we only get a single connection for the same remoteAddr, Do not check the local ip address in case ip_addr is none for now """ if connection.remoteAddr == ccap_ip: ip_addr = connection.localAddr connection.StopConnection() break if (ip_addr, ccap_ip) in self.l2tp_status: self.l2tp_status.pop((ip_addr, ccap_ip)) self.l2tp_dispatcher.unregister_remote_address(ccap_ip) if len(self.l2tp_status) == 0: ret, reason = self.l2tp_dispatcher.request_unregister( {"unregType": "localaddress", "value": ip_addr}) if not ret: self.logger.warn( "l2tp stop CCAP core[%s], unregister ip %s failed for %s", ccap_id, ip_addr, reason) self._send_event_notification( ccap_id, protoDef.msg_core_event_notification.OK, reason="Successful stop event.") else: self._send_event_notification( ccap_id, protoDef.msg_core_event_notification.FAIL, reason="Cannot stop event since can not find it.") return
def _get_intf_ip_address(): output = check_output(['uci', 'show', 'network.lanV6.ip6addr']) # Example output: network.lanV6.ip6addr=fd00:dead:1::1/48\n ip_addr = output.strip().split('=')[1].split('\'')[1].split('/')[0] return ip_addr if Convert.is_valid_ipv6_address(ip_addr) else None
def process_event_action(self, action): """Process the request from the client. :param action: :return: """ ccap_core_id = action.ccap_core_id event_action = action.action self.logger.debug("Receive an event action:%s", action) if ccap_core_id not in self.ccap_cores: self.logger.warn( "Cannot process the event action for id %s, reason: id is not registered" % ccap_core_id) self._send_event_notification( ccap_core_id, protoDef.msg_core_event_notification.FAIL, "CCAP core ID is not registered") return if not action.HasField("parameter"): self.logger.warn("Cannot process the event action for id %s, " "reason:Parameter is not set" % ccap_core_id) # return error self._send_event_notification( ccap_core_id, protoDef.msg_core_event_notification.FAIL, "Parameter is not set") return parameter = action.parameter try: # parameter's format is "time_server1;time_server2/time_offset|log_servers1;log_servers2" time_servers, time_offset = parameter.split("/") time_servers = time_servers.split(";") time_offset, log_servers = time_offset.split("|") if len(time_offset): time_offset = int(time_offset) else: time_offset = 0 if len(log_servers): log_servers = log_servers.split(";") else: log_servers = '' self.logger.debug( "time servers:{}, log servers:{}, time offset: {}".format( time_servers, log_servers, time_offset)) except ValueError as e: self.logger.warn('parameter is {}, {}'.format(parameter, str(e))) self._send_event_notification( ccap_core_id, protoDef.msg_core_event_notification.FAIL, "Parameter {} format error, should be time_server/time_offset|log_servers" .format(parameter)) return if event_action == protoDef.msg_event.START or event_action == protoDef.msg_event.CHECKSTATUS: # only one tod is valid which is for principal core if self.tod_done: status = self.UP else: status = self.DOWN # check if we are in the requester list, if yes, # we just send a current status to it time_server = time_servers[0] if time_server in self.tod: if ccap_core_id not in self.tod[time_server]["requester"]: self.tod[time_server]["requester"].append(ccap_core_id) else: # create a time_server in self time_server self.tod[time_server] = { "status": status, "requester": [ ccap_core_id, ], "lastChangeTime": time(), "time-server": time_servers, "log-server": log_servers, "time-offset": time_offset } is_ipv6 = Convert.is_valid_ipv6_address(time_server) # Prepare TPC runtime arguments args = ['--ipv6'] if is_ipv6 else [] args.extend(['--offset', str(time_offset)]) args.extend( ['--servers'] + [addr.encode('ascii', 'ignore') for addr in time_servers]) if None is self.processes['tod'] and not self.tod_done: self.processes['tod'] = self.start_process(self.cmd + args) # configure logging server for RPD if len(log_servers) == 0: self.log_server = '' self.logger.warning("No log server found") self.rsyslog = RSyslog() self.rsyslog.config_rsyslog(None) elif self.log_server != log_servers[0]: self.log_server = log_servers[0] self.logger.info("config log server") self.rsyslog = RSyslog() self.rsyslog.config_rsyslog(self.log_server) self._send_event_notification( ccap_core_id, protoDef.msg_core_event_notification.OK, "Id has been issue this action, send current status to you", result=self.tod[time_server]["status"]) return if event_action == protoDef.msg_event.STOP: time_server = time_servers[0] if time_server in self.tod: if ccap_core_id in self.tod[time_server]["requester"]: self.tod[time_server]["requester"].remove(ccap_core_id) if len(self.tod[time_server]["requester"]) == 0: self.tod.pop(time_server) if self.check_process_status( self.processes['tod']) == self.PROCESSSTATE_ALIVE: self.terminate_process(self.processes['tod']) self.processes['tod'] = None self._send_event_notification( ccap_core_id, protoDef.msg_core_event_notification.OK, reason="Successful stop event.") else: self._send_event_notification( ccap_core_id, protoDef.msg_core_event_notification.FAIL, reason="Cannot stop event since can not find it.") return
LocalIPAddress = "127.0.0.1" for intf in net_if_addrs().values(): (family, addr, mask) = (intf[0][0], intf[0][1], intf[0][2]) if family == 2: addr_b = addr.split(".") mask_b = mask.split(".") raddr_b = RemoteIPAddress.split(".") if int(addr_b[0]) & int(mask_b[0]) == int(raddr_b[0]) & int(mask_b[0]) and \ int(addr_b[1]) & int(mask_b[1]) == int(raddr_b[1]) & int(mask_b[1]) and \ int(addr_b[2]) & int(mask_b[2]) == int(raddr_b[2]) & int(mask_b[2]) and \ int(addr_b[3]) & int(mask_b[3]) == int(raddr_b[3]) & int(mask_b[3]): LocalIPAddress = addr break else: if len(sys.argv) > 2 and Convert.is_valid_ipv6_address(sys.argv[2]): RemoteIPAddress = sys.argv[2] else: RemoteIPAddress = "::1" if len(sys.argv) > 3 and Convert.is_valid_ipv6_address(sys.argv[3]): LocalIPAddress = sys.argv[3] else: LocalIPAddress = "::1" print "RemoteIPAddress ", RemoteIPAddress print "LocalIPAddress ", LocalIPAddress global_dispatcher = Dispatcher() l2tp_dispatcher = L2tpv3Dispatcher.L2tpv3Dispatcher(global_dispatcher, LocalIPAddress, create_global_listen=False)