ns-3 Direct Code Execution
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ccn-client-helper.cc
Go to the documentation of this file.
1 #include "ccn-client-helper.h"
2 #include "dce-application.h"
3 #include "ns3/log.h"
4 #include "utils.h"
5 #include <fstream>
6 #include <stdlib.h>
7 #include <unistd.h>
8 
9 NS_LOG_COMPONENT_DEFINE ("CcnClientHelper");
10 
11 namespace ns3 {
12 
14 {
15 }
16 
17 ApplicationContainer
18 CcnClientHelper::Install (NodeContainer c)
19 {
20  NS_LOG_FUNCTION (this);
21  ApplicationContainer apps;
22  for (NodeContainer::Iterator j = c.Begin (); j != c.End (); ++j)
23  {
24  int nodeId = (*j)->GetId ();
25  CreateKeystore ();
26  std::stringstream oss;
27 
28  oss << "files-" << nodeId << "/root/.ccnx/";
29  UtilsEnsureAllDirectoriesExist (oss.str ());
30  oss << ".ccnx_keystore";
31 
32  CopyFile (GetKeystoreTemplate (), oss.str ());
33 
34  oss.str ("");
35  oss.clear ();
36 
37  oss << "files-" << nodeId;
38  UtilsEnsureDirectoryExists (oss.str ());
39 
40  oss << "/var/";
41  UtilsEnsureDirectoryExists (oss.str ());
42 
43  oss << "tmp";
44  UtilsEnsureDirectoryExists (oss.str ());
45 
46  for (std::vector <std::pair <std::string, std::string> >::iterator i = m_files.begin ();
47  i != m_files.end (); ++i)
48  {
49  CopyRealFileToVirtual (nodeId, (*i).first, (*i).second);
50  }
51  }
53 }
54 
55 std::string
57 {
58  std::stringstream oss;
59  oss << "/tmp/.dck" << ::getpid () << "/";
60  UtilsEnsureAllDirectoriesExist (oss.str ());
61  return oss.str ();
62 }
63 
64 std::string
66 {
67  std::stringstream oss;
68 
69  oss << GetKeystoreDir () << ".ccnx_keystore";
70 
71  return oss.str ();
72 }
73 
74 void
75 CcnClientHelper::CopyFile (std::string from, std::string to)
76 {
77  std::ifstream f1 (from.c_str (), std::fstream::binary);
78  std::ofstream f2 (to.c_str (), std::fstream::trunc | std::fstream::binary);
79  f2 << f1.rdbuf ();
80  f2.close ();
81  f1.close ();
82 }
83 
84 void
86 {
87  std::stringstream os;
88  struct stat st;
89 
90  os << GetKeystoreDir () << ".ccnx_keystore";
91 
92  if (0 == stat (os.str ().c_str (), &st))
93  {
94  return;
95  }
96 
97  std::stringstream oss;
98 
99  oss << GetKeystoreDir () << "openssl.cnf";
100 
101  std::ofstream conf;
102 
103  conf.open (oss.str ().c_str ());
104 
105  conf << "# This is not really relevant because we're not sending cert requests anywhere," << std::endl;
106  conf << "# but openssl req can refuse to go on if it has no config file." << std::endl;
107  conf << "[ req ]" << std::endl;
108  conf << "distinguished_name = req_distinguished_name" << std::endl;
109  conf << "[ req_distinguished_name ]" << std::endl;
110  conf << "countryName = Country Name (2 letter code)" << std::endl;
111  conf << "countryName_default = AU" << std::endl;
112  conf << "countryName_min = 2" << std::endl;
113  conf << "countryName_max = 2" << std::endl;
114 
115  conf.close ();
116 
117  std::stringstream oss2;
118 
119  oss2 << "openssl req -config " << oss.str () <<
120  " -newkey rsa:1024 -x509 -keyout " << GetKeystoreDir () << "private_key.pem -out "
121  << GetKeystoreDir () << "certout.pem -subj /CN=foo -nodes 2>/dev/null";
122 
123  int ret = ::system (oss2.str ().c_str ());
124 
125  std::stringstream oss3;
126 
127  oss3 << "openssl pkcs12 -export -name ccnxuser -out " << GetKeystoreDir ()
128  << ".ccnx_keystore -in " << GetKeystoreDir () << "certout.pem -inkey "
129  << GetKeystoreDir () << "private_key.pem -password pass:'Th1s1sn0t8g00dp8ssw0rd.'";
130 
131  //NS_LOG_FUNCTION (oss3.str ());
132 
133  ret = ::system (oss3.str ().c_str ());
134 
135  std::stringstream oss4;
136 
137  oss.str ("");
138  oss.clear ();
139 
140  oss << GetKeystoreDir () << "certout.pem";
141  unlink (oss.str ().c_str ());
142 
143  oss.str ("");
144  oss.clear ();
145 
146  oss << GetKeystoreDir () << "openssl.cnf";
147  unlink (oss.str ().c_str ());
148 
149  oss.str ("");
150  oss.clear ();
151 
152  oss << GetKeystoreDir () << "private_key.pem";
153  unlink (oss.str ().c_str ());
154 
155 
156 }
157 void
158 CcnClientHelper::AddFile (std::string from, std::string to)
159 {
160  m_files.push_back (std::make_pair (from, to));
161 }
162 void
163 CcnClientHelper::CopyRealFileToVirtual (int nodeId, std::string from, std::string to)
164 {
165  std::stringstream oss;
166 
167  oss << "files-" << nodeId << to;
168 
169  std::string vto = oss.str ();
170 
171  size_t pos = vto.find_last_of ("/");
172  if (pos != std::string::npos)
173  {
174  std::string dir = vto.substr (0,pos);
176  }
177  CopyFile (from, vto);
178 }
179 }