blob: 0b90a6df69161de55178dc633e9976595a884699 [file] [log] [blame]
Ashlesh Gawande6c86e302019-09-17 22:27:05 -05001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
Davide Pesavento82cc17a2025-02-17 18:30:05 -05003# Copyright (C) 2015-2025, The University of Memphis,
Ashlesh Gawande6c86e302019-09-17 22:27:05 -05004# 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
24import sys
25from os.path import isfile
26from subprocess import call
dulalsaurab0ed77722020-09-24 22:32:58 +000027from six.moves.urllib.parse import quote
28
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050029from mininet.cli import CLI
30
awlane21acd052024-06-13 21:12:51 -050031from mininet.log import error
32
33import re
34
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050035sshbase = ['ssh', '-q', '-t', '-i/home/mininet/.ssh/id_rsa']
36scpbase = ['scp', '-i', '/home/mininet/.ssh/id_rsa']
37devnull = open('/dev/null', 'w')
38
dulalsaurab0ed77722020-09-24 22:32:58 +000039def getSafeName(namePrefix):
40 """
41 Check if the prefix/string is safe to use with ndn commands or not.
42 return safe prefix.
43 :param namePrefix: name of the prefix
44 """
45 # remove redundant "/"es, multiple "/"es are an invalid representation for empty name component
Davide Pesavento82cc17a2025-02-17 18:30:05 -050046 namePrefix = "/" + "/".join(filter(None, namePrefix.split("/")))
dulalsaurab0ed77722020-09-24 22:32:58 +000047 return quote(namePrefix, safe='/')
48
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050049def ssh(login, cmd):
50 rcmd = sshbase + [login, cmd]
51 call(rcmd, stdout=devnull, stderr=devnull)
52
53def scp(*args):
54 tmp = []
55 for arg in args:
56 tmp.append(arg)
57 rcmd = scpbase + tmp
58 call(rcmd, stdout=devnull, stderr=devnull)
59
60def copyExistentFile(node, fileList, destination):
61 for f in fileList:
62 if isfile(f):
63 node.cmd('cp {} {}'.format(f, destination))
64 break
65 if not isfile(destination):
66 fileName = destination.split('/')[-1]
67 raise IOError('{} not found in expected directory.'.format(fileName))
68
69def popenGetEnv(node, envDict=None):
70 env = {}
71 homeDir = node.params['params']['homeDir']
72 printenv = node.popen('printenv'.split(), cwd=homeDir).communicate()[0].decode('utf-8')
73 for var in printenv.split('\n'):
74 if var == '':
75 break
76 p = var.split('=')
77 env[p[0]] = p[1]
78 env['HOME'] = homeDir
79
80 if envDict is not None:
81 for key, value in envDict.items():
82 env[key] = str(value)
83
84 return env
85
86def getPopen(host, cmd, envDict=None, **params):
87 return host.popen(cmd, cwd=host.params['params']['homeDir'],
88 env=popenGetEnv(host, envDict), **params)
89
awlane21acd052024-06-13 21:12:51 -050090def MACToEther(mac):
91 # We use the regex filters from face-uri.cpp in ndn-cxx with minor modifications
92 if re.match('^\[((?:[a-fA-F0-9]{1,2}\:){5}(?:[a-fA-F0-9]{1,2}))\]$', mac):
93 return mac
94 elif re.match('^((?:[a-fA-F0-9]{1,2}\:){5}(?:[a-fA-F0-9]{1,2}))$', mac):
95 # URI syntax requires nfdc to use brackets for MAC and ethernet addresses due
96 # to the use of colons as separators. Incomplete brackets are a code issue.
97 return '[%s]' % mac
98 error('Potentially malformed MAC address, passing without alteration: %s' % mac)
99 return mac
100
Ashlesh Gawande6c86e302019-09-17 22:27:05 -0500101class MiniNDNCLI(CLI):
102 prompt = 'mini-ndn> '
103 def __init__(self, mininet, stdin=sys.stdin, script=None):
104 CLI.__init__(self, mininet, stdin, script)
Alexander Laneea2d5d62019-10-04 16:48:52 -0500105
Junxiao Shi48ada892021-11-04 09:02:21 -0600106try:
107 from mn_wifi.cli import CLI as CLI_wifi
108
109 class MiniNDNWifiCLI(CLI_wifi):
110 prompt = 'mini-ndn-wifi> '
111 def __init__(self, mininet, stdin=sys.stdin, script=None):
112 CLI_wifi.__init__(self, mininet, stdin, script)
113
114except ImportError:
115 class MiniNDNWifiCLI:
116 def __init__(self):
117 raise ImportError('Mininet-WiFi is not installed')