def stop_client_connection(self, identifier): """Cleanup Openl2tp state.""" ppp_profile_name = 'ppp-prof-%s' % identifier tunnel_profile_name = 'tunnel-prof-%s' % identifier session_profile_name = 'session-prof-%s' % identifier peer_profile_name = 'peer-prof-%s' % identifier tunnel_name = 'tunnel-%s' % identifier session_name = 'session-%s' % identifier # delete existing profiles just to be sure lock = helpers.acquire_openl2tpconfig_lock() if lock is None: raise Exception('failed to acquire openl2tp config lock') try: for i in [ 'session delete tunnel_name=%s session_name=%s' % (tunnel_name, session_name), 'tunnel delete tunnel_name=%s' % tunnel_name, 'ppp profile delete profile_name=%s' % ppp_profile_name, 'tunnel profile delete profile_name=%s' % tunnel_profile_name, 'session profile delete profile_name=%s' % session_profile_name, 'peer profile delete profile_name=%s' % peer_profile_name ]: cmd = '%s\nquit\n' % i [rv, out, err] = run_command([constants.CMD_OPENL2TPCONFIG], stdin=str(cmd)) # ignore errors if rv != 0: self._log.debug('client connection cleanup command failed:\n command: %s, rv: %s, out: %s, err: %s' % (cmd, rv, out, err)) else: self._log.debug('client connection cleanup command succeeded:\n command: %s, rv: %s, out: %s, err: %s' % (cmd, rv, out, err)) finally: helpers.release_openl2tpconfig_lock(lock)
def determine_tunnel_remote_address_and_port(self, tunnelid): """Determine remote IPv4 address and port of a specific tunnel.""" config = textwrap.dedent("""\ tunnel show tunnel_id=%s quit """) % tunnelid lock = helpers.acquire_openl2tpconfig_lock() if lock is None: raise Exception('failed to acquire openl2tp config lock') try: [rv, stdout, stderr] = run_command([constants.CMD_OPENL2TPCONFIG], stdin=str(config), retval=runcommand.FAIL) finally: helpers.release_openl2tpconfig_lock(lock) got_tunnelid, srcaddr, srcport, dstaddr, dstport = None, None, None, None, None for l in stdout.split('\n'): m = _re_openl2tp_tunnel_details_header.match(l) if m is not None: got_tunnelid, srcaddr, dstaddr = m.group(1), m.group(2), m.group(3) continue m = _re_openl2tp_tunnel_details_udpports.match(l) if m is not None: srcport, dstport = m.group(1), m.group(2) if dstaddr is None or dstport is None: raise Exception('cannot determine endpoint for tunnelid %s' % tunnelid) return dstaddr, int(dstport)
def post_start(self): # XXX: need to sleep before configure? # XXX: retval is zero when f.ex. config file is missing! # check srderr for error messages? lock = helpers.acquire_openl2tpconfig_lock() if lock is None: raise Exception('failed to acquire openl2tp config lock') try: run_command([constants.CMD_OPENL2TPCONFIG, 'config', 'restore', 'file=' + constants.OPENL2TP_CONF], retval=runcommand.FAIL) finally: helpers.release_openl2tpconfig_lock(lock)
def _run_config(config, failmsg, successmsg): rv, out, err = 1, '', '' lock = helpers.acquire_openl2tpconfig_lock() if lock is None: raise Exception('failed to acquire openl2tp config lock') try: [rv, out, err] = run_command([constants.CMD_OPENL2TPCONFIG], stdin=str(config)) except: pass helpers.release_openl2tpconfig_lock(lock) if rv != 0: self._log.error('%s: %s, %s, %s' % (str(failmsg), str(rv), str(out), str(err))) raise Exception(str(failmsg)) else: self._log.debug('%s: %s, %s, %s' % (str(successmsg), str(rv), str(out), str(err))) return rv, out, err