-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
209 lines (185 loc) · 5.44 KB
/
Copy pathmain.cpp
File metadata and controls
209 lines (185 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <cstdlib>
#include <sstream>
#include <vector>
#include <fstream>
#include <string>
std::string getIpAddress(const char *domain)
{
struct addrinfo hints, *res;
int err;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET; // IPv4 address
hints.ai_socktype = SOCK_STREAM;
// Get the address information
err = getaddrinfo(domain, NULL, &hints, &res);
if (err != 0)
{
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(err));
return nullptr;
}
struct sockaddr_in *addr = (struct sockaddr_in *)res->ai_addr;
char ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &addr->sin_addr, ip, sizeof(ip));
// Free the address information
freeaddrinfo(res);
return ip;
}
class Data
{
public:
Data(const std::string &str)
{
std::istringstream stream(str);
std::string word;
int i = 0;
while (stream >> word)
{
switch (i)
{
case 0:
mountPoint = word;
break;
case 1:
shareName = word;
break;
case 2:
if (word != "-")
ipLocal = word;
break;
case 3:
if (word != "-")
ipWeb = word;
break;
case 4:
if (word != "-")
domain = word;
break;
case 5:
creds = word;
break;
default:
options.push_back(word);
}
i++;
}
if (!(domain.empty()) && (ipWeb.empty()))
{
ipWeb = getIpAddress(domain.c_str());
}
}
public:
std::string mountPoint;
std::string shareName;
std::string ipLocal;
std::string ipWeb;
std::string domain;
std::string creds;
std::vector<std::string> options;
};
bool isDeviceReachable(const std::string &ipAddr)
{
std::string command = "ping -c 1 -W 1 " + ipAddr;
int result = system(command.c_str());
return result == 0;
}
bool mountSmbShare(const std::string &ipServer, const std::string &shareName, const std::string &mntPoint, const std::string &creds){
// TODO redo credentials
// TODO change system()
std::ostringstream command;
command << "sudo mount -t cifs //"
<< ipServer << "/" << shareName << " "
<< mntPoint << " "
<< "-o credentials=" << creds;
int result = system(command.str().c_str());
if(result != 0){
std::cerr << "Failed to mount share. Error code: " << result << std::endl;
return false;
}
return result == 0;
}
void printData(const Data &data)
{
std::cout << "Mount Point: " << data.mountPoint << std::endl;
std::cout << "Share Name: " << data.shareName << std::endl;
std::cout << "IP Local: " << data.ipLocal << std::endl;
std::cout << "IP Web: " << data.ipWeb << std::endl;
std::cout << "Domain: " << data.domain << std::endl;
std::cout << "Credentials: " << data.creds << std::endl;
std::cout << "Options: ";
if (!data.options.empty())
{
for (const auto &option : data.options)
{
std::cout << option << " ";
}
}
else
{
std::cout << "No options available.";
}
std::cout << std::endl;
}
int main(int argc, char *argv[])
{
for (int i = 0; i < argc; i++)
{
std::cout << i << argv[i] << "\n";
}
std::ifstream file(argv[1]);
if (!file)
{
std::cerr << "Could not open file." << std::endl;
return 1;
}
std::string line;
std::vector<Data> input;
while (std::getline(file, line))
{
input.push_back(Data(line));
}
file.close();
printData(input[0]);
for (Data data : input)
{
std::cout << "Trying the host on the local network...\n";
if (isDeviceReachable(data.ipLocal))
{
std::cout << "\nConnecting on local network...\n";
if (mountSmbShare(data.ipLocal, data.shareName, data.mountPoint, data.creds))
{
std::cout << "Successfully connected to " << data.ipLocal << "/" << data.shareName << ". Mounted to " << data.mountPoint << std::endl;
return 0;
}
else
{
std::cout << "Failed to connect to " << data.ipLocal << "/" << data.shareName << std::endl;
}
}
else
{
std::cout << "Host at " << data.ipLocal << " is not reachable.\n";
std::cout << "Trying the host over the internet...\n";
if (isDeviceReachable(data.ipWeb))
{
std::cout << "\nConnecting to host over the internet...\n";
if (mountSmbShare(data.ipWeb, data.shareName, data.mountPoint, data.creds))
{
std::cout << "Successfully connected to " << data.ipWeb << "/" << data.shareName << ". Mounted to " << data.mountPoint << std::endl;
return 1;
}
else
{
std::cout << "Failed to connect to " << data.ipWeb << "/" << data.shareName << std::endl;
}
std::cout << "Host is not reachable.\n";
return 2;
}
}
}
}