-
Notifications
You must be signed in to change notification settings - Fork 1
/
ftpServer.cpp
270 lines (238 loc) · 5.98 KB
/
ftpServer.cpp
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include "ftpServer.hpp"
#include <algorithm>
#ifdef NXDK
#include <lwip/arch.h>
#include <lwip/debug.h>
#include <lwip/dhcp.h>
#include <lwip/errno.h>
#include <lwip/init.h>
#include <lwip/netif.h>
#include <lwip/opt.h>
#include <lwip/sys.h>
#include <lwip/tcpip.h>
#include <lwip/timeouts.h>
#include <netif/etharp.h>
#include <nxdk/mount.h>
#include <pktdrv.h>
#include <xboxkrnl/xboxkrnl.h>
#else
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <cstring>
#endif
FtpConfig::FtpConfig() {
enable = true;
username = "xbox";
password = "xbox";
port = 21;
}
FtpConfig::FtpConfig(ftpConfig* f) {
enable = f->enable;
username = std::string(f->username);
password = std::string(f->password);
port = f->port;
}
void FtpConfig::setEnabled(bool val) {
if (enable != val) {
enable = val;
}
}
void FtpConfig::setUser(std::string const& user) {
if (username.compare(user)) {
username = user;
}
}
void FtpConfig::setPassword(std::string const& pwd) {
if (password.compare(pwd)) {
password = pwd;
}
}
void FtpConfig::setPort(int p) {
if (port != p) {
port = p;
}
}
// get sockaddr, IPv4 or IPv6:
void* FtpServer::getInAddr(struct sockaddr* sa) {
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
#ifdef NXDK
const char* gai_strerror(int errc) {
switch (errc) {
case 200:
return "EAI_NONAME";
case 201:
return "EAI_SERVICE";
case 202:
return "EAI_FAIL";
case 203:
return "EAI_MEMORY";
case 204:
return "EAI_FAMILY";
case 210:
return "HOST_NOT_FOUND";
case 211:
return "NO_DATA";
case 212:
return "NO_RECOVERY";
case 213:
return "TRY_AGAIN";
default:
return "ERR_OK";
}
}
#endif
std::string sock_strerror(int errc) {
switch (errc) {
case EACCES:
return "EACCES";
case EAFNOSUPPORT:
return "EAFNOSUPPORT";
case EINVAL:
return "EINVAL";
case ENFILE:
return "ENFILE";
case EMFILE:
return "EMFILE";
case ENOBUFS:
return "ENOBUFS";
case ENOMEM:
return "ENOMEM";
case EPROTONOSUPPORT:
return "EPROTONOSUPPORT";
default:
return "ERR_OK";
}
}
FtpServer::FtpServer(FtpConfig const* conf) : conf(conf) {
FD_ZERO(&master); // clear the master and temp sets
FD_ZERO(&readFds);
// Set hints for our future socket(s)
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
}
int FtpServer::init() {
int yes = 1;
if ((i = getaddrinfo(NULL, std::to_string(conf->getPort()).c_str(), &hints, &ai)) != 0) {
//InfoLog::outputLine(InfoLog::ERROR, "Error: selectserver\n");
return 5;
}
/* Get network settings */
for (p = ai; p != NULL; p = p->ai_next) {
listener = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (listener < 0) {
continue;
}
setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
if (bind(listener, p->ai_addr, p->ai_addrlen) < 0) {
close(listener);
continue;
}
break;
}
freeaddrinfo(ai);
if (p == NULL) {
//InfoLog::outputLine(InfoLog::ERROR, "Error: selectServer: failed to bind\n");
return 1;
}
/* Start listening */
if (listen(listener, 10) == -1) {
//InfoLog::outputLine(InfoLog::ERROR, "Error: Listen\n");
return 2;
}
FD_SET(listener, &master);
fdmax = listener;
return 0;
}
int FtpServer::run() {
/* `select` a file descriptor */
for (;;) {
readFds = master;
if (select(fdmax + 1, &readFds, NULL, NULL, NULL) == -1) {
//InfoLog::outputLine(InfoLog::ERROR, "Error: Select\n");
return 3;
}
for (i = 0; i <= fdmax; ++i) {
if (FD_ISSET(i, &readFds)) { // we got one!!
if (i == listener) {
// handle new connections
addrlen = sizeof raddr;
newfd = accept(listener, (struct sockaddr*)&raddr, &addrlen);
if (newfd == -1) {
//InfoLog::outputLine(InfoLog::ERROR, "Error: accept: %s\n",
//sock_strerror(errno).c_str());
} else {
FD_SET(newfd, &master); // add to master set
if (newfd > fdmax) { // keep track of the max
fdmax = newfd;
}
clients[newfd] = new ftpConnection(newfd, this);
}
} else {
if (!clients[i]->update()) {
forgetConnection(i);
}
}
}
}
}
}
void FtpServer::forgetConnection(int fd) {
delete (clients[fd]);
clients.erase(fd);
FD_CLR(fd, &master);
//InfoLog::outputLine(InfoLog::DEBUG, "Closing %d!\n", fd);
//close(fd); // bye!
}
int FtpServer::openConnection(std::string const& addr, std::string const& port) {
int ret;
int rv;
if ((rv = getaddrinfo(addr.c_str(), port.c_str(), &hints, &ai)) == 0) {
if ((ret = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) != -1) {
if (connect(ret, ai->ai_addr, ai->ai_addrlen) != 0) {
//InfoLog::outputLine(InfoLog::ERROR, "Connecting socket %d failed; %s\n", ret,
//sock_strerror(errno).c_str());
close(ret);
ret = -1;
}
} else {
//InfoLog::outputLine(InfoLog::ERROR, "Socket creation failed; %s\n",
//sock_strerror(errno).c_str());
}
} else {
//InfoLog::outputLine(InfoLog::ERROR, "Getting address info failed; %s\n",
//gai_strerror(rv));
ret = -1;
}
freeaddrinfo(ai);
return ret;
}
void FtpServer::runAsync() {
serverThread = std::thread(thread_runner, this);
}
int FtpServer::thread_runner(FtpServer* server) {
return server->run();
}
inline FtpServer* real(ftpServer* f) {return static_cast<FtpServer*>(f);}
ftpServer* new_ftpServer(ftpConfig* c) {
FtpConfig *C = new FtpConfig(c);
FtpServer *s = new FtpServer(C);
s->init();
return s;
}
void run_ftpServer(ftpServer* f) { real(f)->run(); }
void delete_ftpServer(ftpServer* f) {
delete real(f)->conf;
delete real(f);
}