Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 1 | # -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | # |
| 3 | # Copyright (C) 2015-2019, The University of Memphis, |
| 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/>. |
awlane | 593ac19 | 2024-04-04 13:15:55 -0500 | [diff] [blame^] | 23 | import json |
| 24 | import os |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 25 | |
| 26 | from minindn.apps.application import Application |
| 27 | from minindn.util import copyExistentFile |
| 28 | from minindn.minindn import Minindn |
| 29 | |
| 30 | class Nfd(Application): |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 31 | def __init__(self, node, logLevel='NONE', csSize=65536, |
awlane | 593ac19 | 2024-04-04 13:15:55 -0500 | [diff] [blame^] | 32 | csPolicy='lru', csUnsolicitedPolicy='drop-all'): |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 33 | Application.__init__(self, node) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 34 | self.logLevel = node.params['params'].get('nfd-log-level', logLevel) |
| 35 | |
| 36 | self.confFile = '{}/nfd.conf'.format(self.homeDir) |
| 37 | self.logFile = 'nfd.log' |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 38 | self.ndnFolder = '{}/.ndn'.format(self.homeDir) |
| 39 | self.clientConf = '{}/client.conf'.format(self.ndnFolder) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 40 | # Copy nfd.conf file from /usr/local/etc/ndn or /etc/ndn to the node's home directory |
| 41 | # Use nfd.conf as default configuration for NFD, else use the sample |
awlane | 593ac19 | 2024-04-04 13:15:55 -0500 | [diff] [blame^] | 42 | possibleConfPaths = ['/usr/local/etc/ndn/nfd.conf', '/usr/local/etc/ndn/nfd.conf.sample', |
| 43 | '/etc/ndn/nfd.conf', '/etc/ndn/nfd.conf.sample'] |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 44 | copyExistentFile(node, possibleConfPaths, self.confFile) |
| 45 | |
awlane | 593ac19 | 2024-04-04 13:15:55 -0500 | [diff] [blame^] | 46 | # Using infoconv, we convert the local nfd.conf file to JSON and parse it into an object |
| 47 | conf_file = json.loads(node.cmd("infoconv info2json < {}".format(self.confFile))) |
| 48 | |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 49 | # Set log level |
awlane | 593ac19 | 2024-04-04 13:15:55 -0500 | [diff] [blame^] | 50 | conf_file["log"]["default_level"] = self.logLevel |
| 51 | |
| 52 | # Set socket file name and path |
| 53 | # Retrieve the default socket path from the conf file; this avoids issues from #5316 |
| 54 | default_socket_path = os.path.dirname(conf_file["face_system"]["unix"]["path"]) |
| 55 | self.sockFile = '{}/{}.sock'.format(default_socket_path, node.name) |
| 56 | # Set socket path in conf file to new socket |
| 57 | conf_file["face_system"]["unix"]["path"] = self.sockFile |
| 58 | # Create client configuration for host to ensure socket path is consistent |
| 59 | # Suppress error if working directory exists from prior run |
| 60 | os.makedirs(self.ndnFolder, exist_ok=True) |
| 61 | # This will overwrite any existing client.conf files, which should not be an issue |
| 62 | with open(self.clientConf, "w") as client_conf_file: |
| 63 | client_conf_file.write("transport=unix://{}\n".format(self.sockFile)) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 64 | |
| 65 | # Set CS parameters |
awlane | 593ac19 | 2024-04-04 13:15:55 -0500 | [diff] [blame^] | 66 | conf_file["tables"]["cs_max_packets"] = csSize |
| 67 | conf_file["tables"]["cs_policy"] = csPolicy |
| 68 | conf_file["tables"]["cs_unsolicited_policy"] = csUnsolicitedPolicy |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 69 | |
awlane | 593ac19 | 2024-04-04 13:15:55 -0500 | [diff] [blame^] | 70 | # To avoid complicated Bash piping, we write the JSON to a temporary file |
| 71 | with open("{}/temp_nfd_conf.json".format(self.homeDir), "w") as temp_file: |
| 72 | json.dump(conf_file, temp_file) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 73 | |
awlane | 593ac19 | 2024-04-04 13:15:55 -0500 | [diff] [blame^] | 74 | # Convert our modified intermediate file and write to the new conf file |
| 75 | node.cmd("infoconv json2info < {}/temp_nfd_conf.json > {}".format(self.homeDir, self.confFile)) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 76 | |
awlane | 593ac19 | 2024-04-04 13:15:55 -0500 | [diff] [blame^] | 77 | # Remove the intermediate JSON file |
| 78 | os.remove("{}/temp_nfd_conf.json".format(self.homeDir)) |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 79 | |
| 80 | if not Minindn.ndnSecurityDisabled: |
| 81 | # Generate key and install cert for /localhost/operator to be used by NFD |
suraviregmi | 9665f5c | 2023-11-09 20:05:26 +0000 | [diff] [blame] | 82 | node.cmd('ndnsec-key-gen /localhost/operator | ndnsec-cert-install -') |
Ashlesh Gawande | 6c86e30 | 2019-09-17 22:27:05 -0500 | [diff] [blame] | 83 | |
| 84 | def start(self): |
| 85 | Application.start(self, 'nfd --config {}'.format(self.confFile), logfile=self.logFile) |
Varun Patil | 63a330d | 2022-05-18 14:23:13 -0700 | [diff] [blame] | 86 | Minindn.sleep(0.5) |