blob: 44978e7319e63b7653682085df85d8f8ef422847 [file] [log] [blame]
Ashlesh Gawande6c86e302019-09-17 22:27:05 -05001# -*- 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/>.
awlane593ac192024-04-04 13:15:55 -050023import json
24import os
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050025
26from minindn.apps.application import Application
27from minindn.util import copyExistentFile
28from minindn.minindn import Minindn
29
30class Nfd(Application):
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050031 def __init__(self, node, logLevel='NONE', csSize=65536,
awlane593ac192024-04-04 13:15:55 -050032 csPolicy='lru', csUnsolicitedPolicy='drop-all'):
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050033 Application.__init__(self, node)
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050034 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 Gawande6c86e302019-09-17 22:27:05 -050038 self.ndnFolder = '{}/.ndn'.format(self.homeDir)
39 self.clientConf = '{}/client.conf'.format(self.ndnFolder)
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050040 # 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
awlane593ac192024-04-04 13:15:55 -050042 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 Gawande6c86e302019-09-17 22:27:05 -050044 copyExistentFile(node, possibleConfPaths, self.confFile)
45
awlane593ac192024-04-04 13:15:55 -050046 # 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 Gawande6c86e302019-09-17 22:27:05 -050049 # Set log level
awlane593ac192024-04-04 13:15:55 -050050 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 Gawande6c86e302019-09-17 22:27:05 -050064
65 # Set CS parameters
awlane593ac192024-04-04 13:15:55 -050066 conf_file["tables"]["cs_max_packets"] = csSize
67 conf_file["tables"]["cs_policy"] = csPolicy
68 conf_file["tables"]["cs_unsolicited_policy"] = csUnsolicitedPolicy
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050069
awlane593ac192024-04-04 13:15:55 -050070 # 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 Gawande6c86e302019-09-17 22:27:05 -050073
awlane593ac192024-04-04 13:15:55 -050074 # 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 Gawande6c86e302019-09-17 22:27:05 -050076
awlane593ac192024-04-04 13:15:55 -050077 # Remove the intermediate JSON file
78 os.remove("{}/temp_nfd_conf.json".format(self.homeDir))
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050079
80 if not Minindn.ndnSecurityDisabled:
81 # Generate key and install cert for /localhost/operator to be used by NFD
suraviregmi9665f5c2023-11-09 20:05:26 +000082 node.cmd('ndnsec-key-gen /localhost/operator | ndnsec-cert-install -')
Ashlesh Gawande6c86e302019-09-17 22:27:05 -050083
84 def start(self):
85 Application.start(self, 'nfd --config {}'.format(self.confFile), logfile=self.logFile)
Varun Patil63a330d2022-05-18 14:23:13 -070086 Minindn.sleep(0.5)