Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 1 | # -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | # |
Davide Pesavento | 82cc17a | 2025-02-17 18:30:05 -0500 | [diff] [blame] | 3 | # Copyright (C) 2015-2025, The University of Memphis, |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 4 | # Arizona Board of Regents, |
| 5 | # Regents of the University of California. |
| 6 | # |
| 7 | # This file is part of Mini-NDN. |
| 8 | # See AUTHORS.md for a complete list of Mini-NDN authors and contributors. |
| 9 | # |
| 10 | # Mini-NDN is free software: you can redistribute it and/or modify |
| 11 | # it under the terms of the GNU General Public License as published by |
| 12 | # the Free Software Foundation, either version 3 of the License, or |
| 13 | # (at your option) any later version. |
| 14 | # |
| 15 | # Mini-NDN is distributed in the hope that it will be useful, |
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | # GNU General Public License for more details. |
| 19 | # |
| 20 | # You should have received a copy of the GNU General Public License |
| 21 | # along with Mini-NDN, e.g., in COPYING.md file. |
| 22 | # If not, see <http://www.gnu.org/licenses/>. |
| 23 | |
awlane | 1cec233 | 2025-04-24 17:24:47 -0500 | [diff] [blame] | 24 | import re |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 25 | import sys |
| 26 | from os.path import isfile |
awlane | 1cec233 | 2025-04-24 17:24:47 -0500 | [diff] [blame] | 27 | from subprocess import call, PIPE |
| 28 | |
dulalsaurab | 0ed7772 | 2020-09-24 22:32:58 +0000 | [diff] [blame] | 29 | from six.moves.urllib.parse import quote |
| 30 | |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 31 | from mininet.cli import CLI |
awlane | 21acd05 | 2024-06-13 21:12:51 -0500 | [diff] [blame] | 32 | from mininet.log import error |
awlane | 1cec233 | 2025-04-24 17:24:47 -0500 | [diff] [blame] | 33 | from mininet.node import Host |
| 34 | from mininet.net import Mininet |
awlane | 21acd05 | 2024-06-13 21:12:51 -0500 | [diff] [blame] | 35 | |
awlane | 21acd05 | 2024-06-13 21:12:51 -0500 | [diff] [blame] | 36 | |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 37 | sshbase = ['ssh', '-q', '-t', '-i/home/mininet/.ssh/id_rsa'] |
| 38 | scpbase = ['scp', '-i', '/home/mininet/.ssh/id_rsa'] |
| 39 | devnull = open('/dev/null', 'w') |
| 40 | |
dulalsaurab | 0ed7772 | 2020-09-24 22:32:58 +0000 | [diff] [blame] | 41 | def getSafeName(namePrefix): |
| 42 | """ |
| 43 | Check if the prefix/string is safe to use with ndn commands or not. |
| 44 | return safe prefix. |
| 45 | :param namePrefix: name of the prefix |
| 46 | """ |
| 47 | # remove redundant "/"es, multiple "/"es are an invalid representation for empty name component |
Davide Pesavento | 82cc17a | 2025-02-17 18:30:05 -0500 | [diff] [blame] | 48 | namePrefix = "/" + "/".join(filter(None, namePrefix.split("/"))) |
dulalsaurab | 0ed7772 | 2020-09-24 22:32:58 +0000 | [diff] [blame] | 49 | return quote(namePrefix, safe='/') |
| 50 | |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 51 | def ssh(login, cmd): |
| 52 | rcmd = sshbase + [login, cmd] |
| 53 | call(rcmd, stdout=devnull, stderr=devnull) |
| 54 | |
| 55 | def scp(*args): |
| 56 | tmp = [] |
| 57 | for arg in args: |
| 58 | tmp.append(arg) |
| 59 | rcmd = scpbase + tmp |
| 60 | call(rcmd, stdout=devnull, stderr=devnull) |
| 61 | |
awlane | 1cec233 | 2025-04-24 17:24:47 -0500 | [diff] [blame] | 62 | def copyExistentFile(host, fileList, destination): |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 63 | for f in fileList: |
| 64 | if isfile(f): |
awlane | 1cec233 | 2025-04-24 17:24:47 -0500 | [diff] [blame] | 65 | host.cmd('cp {} {}'.format(f, destination)) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 66 | break |
| 67 | if not isfile(destination): |
| 68 | fileName = destination.split('/')[-1] |
| 69 | raise IOError('{} not found in expected directory.'.format(fileName)) |
| 70 | |
awlane | 1cec233 | 2025-04-24 17:24:47 -0500 | [diff] [blame] | 71 | def popenGetEnv(host, envDict=None): |
| 72 | '''Helper method to set environment variables for Popen on nodes''' |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 73 | env = {} |
awlane | 1cec233 | 2025-04-24 17:24:47 -0500 | [diff] [blame] | 74 | homeDir = host.params['params']['homeDir'] |
| 75 | printenv = host.popen('printenv'.split(), cwd=homeDir).communicate()[0].decode('utf-8') |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 76 | for var in printenv.split('\n'): |
| 77 | if var == '': |
| 78 | break |
| 79 | p = var.split('=') |
| 80 | env[p[0]] = p[1] |
| 81 | env['HOME'] = homeDir |
| 82 | |
| 83 | if envDict is not None: |
| 84 | for key, value in envDict.items(): |
| 85 | env[key] = str(value) |
| 86 | |
| 87 | return env |
| 88 | |
| 89 | def getPopen(host, cmd, envDict=None, **params): |
awlane | 1cec233 | 2025-04-24 17:24:47 -0500 | [diff] [blame] | 90 | '''Return Popen object for process on node with correctly set environmental variables''' |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 91 | return host.popen(cmd, cwd=host.params['params']['homeDir'], |
| 92 | env=popenGetEnv(host, envDict), **params) |
| 93 | |
awlane | 21acd05 | 2024-06-13 21:12:51 -0500 | [diff] [blame] | 94 | def MACToEther(mac): |
| 95 | # We use the regex filters from face-uri.cpp in ndn-cxx with minor modifications |
| 96 | if re.match('^\[((?:[a-fA-F0-9]{1,2}\:){5}(?:[a-fA-F0-9]{1,2}))\]$', mac): |
| 97 | return mac |
| 98 | elif re.match('^((?:[a-fA-F0-9]{1,2}\:){5}(?:[a-fA-F0-9]{1,2}))$', mac): |
| 99 | # URI syntax requires nfdc to use brackets for MAC and ethernet addresses due |
| 100 | # to the use of colons as separators. Incomplete brackets are a code issue. |
| 101 | return '[%s]' % mac |
| 102 | error('Potentially malformed MAC address, passing without alteration: %s' % mac) |
| 103 | return mac |
| 104 | |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 105 | class MiniNDNCLI(CLI): |
| 106 | prompt = 'mini-ndn> ' |
| 107 | def __init__(self, mininet, stdin=sys.stdin, script=None): |
| 108 | CLI.__init__(self, mininet, stdin, script) |
Alexander Lane | ea2d5d6 | 2019-10-04 16:48:52 -0500 | [diff] [blame] | 109 | |
Junxiao Shi | 48ada89 | 2021-11-04 09:02:21 -0600 | [diff] [blame] | 110 | try: |
| 111 | from mn_wifi.cli import CLI as CLI_wifi |
awlane | 1cec233 | 2025-04-24 17:24:47 -0500 | [diff] [blame] | 112 | from mn_wifi.node import Station as mn_wifi_station |
| 113 | HAS_WIFI = True |
Junxiao Shi | 48ada89 | 2021-11-04 09:02:21 -0600 | [diff] [blame] | 114 | class MiniNDNWifiCLI(CLI_wifi): |
| 115 | prompt = 'mini-ndn-wifi> ' |
| 116 | def __init__(self, mininet, stdin=sys.stdin, script=None): |
| 117 | CLI_wifi.__init__(self, mininet, stdin, script) |
| 118 | |
| 119 | except ImportError: |
awlane | 1cec233 | 2025-04-24 17:24:47 -0500 | [diff] [blame] | 120 | HAS_WIFI = False |
Junxiao Shi | 48ada89 | 2021-11-04 09:02:21 -0600 | [diff] [blame] | 121 | class MiniNDNWifiCLI: |
| 122 | def __init__(self): |
| 123 | raise ImportError('Mininet-WiFi is not installed') |
awlane | 1cec233 | 2025-04-24 17:24:47 -0500 | [diff] [blame] | 124 | |
| 125 | def is_valid_hostid(net: Mininet, host_id: str): |
| 126 | """Check if a hostId is a host""" |
| 127 | if host_id not in net: |
| 128 | return False |
| 129 | |
| 130 | if not isinstance(net[host_id], Host) and \ |
| 131 | (HAS_WIFI and not isinstance(net[host_id], mn_wifi_station)): |
| 132 | return False |
| 133 | |
| 134 | return True |
| 135 | |
| 136 | def run_popen(host, cmd): |
| 137 | """Helper to run command on node asynchronously and get output (blocking)""" |
| 138 | process = getPopen(host, cmd, stdout=PIPE) |
| 139 | return process.communicate()[0] |
| 140 | |
| 141 | def run_popen_readline(host, cmd): |
| 142 | """Helper to run command on node asynchronously and get output line by line (blocking)""" |
| 143 | process = getPopen(host, cmd, stdout=PIPE) |
| 144 | while True: |
| 145 | line: bytes = process.stdout.readline() |
| 146 | if not line: |
| 147 | break |
| 148 | yield line |
| 149 | |
| 150 | def host_home(host) -> str | None: |
| 151 | """Get home directory for host""" |
| 152 | if 'params' not in host.params or 'homeDir' not in host.params['params']: |
| 153 | return None |
| 154 | return host.params['params']['homeDir'] |