-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
191 lines (169 loc) · 4.86 KB
/
main.c
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <fcntl.h>
#define BUFFER_SIZE 10485760
typedef struct
{
int sock;
struct sockaddr_in addrTo;
} Socket;
int sendData(int socket, int sendsize, FILE* handle, int cmd_socket) {
char cmd_buf[32768];
char* buffer = malloc(BUFFER_SIZE);
int oldpos = sendsize;
while(sendsize) {
int count = recv(cmd_socket, &cmd_buf, 32768, 0);
if (count > 0){
int data_ptr = count;
while (count > 0){
count = recv(cmd_socket, &cmd_buf[data_ptr], 32768, 0);
data_ptr = data_ptr + count;
}
printf("\nReceived: %s",cmd_buf);
fflush(stdout);
memset(cmd_buf,0, 32768);
count = -1;
data_ptr = 0;
return -1;
}
fseek(handle, oldpos - sendsize, SEEK_SET);
fread(buffer, BUFFER_SIZE, 1, handle);
int len = 0;
if (BUFFER_SIZE <= sendsize) len = send(socket, buffer, BUFFER_SIZE, 0);
else len = send(socket, buffer, sendsize, 0);
if (len <= 0) break;
sendsize -= len;
}
free(buffer);
return 0;
}
char ret_code[4];
void waitResponse(int socket){
char data[32768];
int data_ptr = 0;
int count = -1;
while (count < 0){
count = recv(socket, &data, 32768, 0);
data_ptr = count;
}
while (count > 0){
count = recv(socket, &data[data_ptr], 32768, 0);
data_ptr = data_ptr + count;
}
data_ptr = 0;
count = -1;
printf("\nReceived: %s",data);
fflush(stdout);
snprintf(ret_code,4,data);
memset(data, 0, 32768);
}
int main(int argc,char** argv){
// Getting arguments
if (argc != 4){
printf("Invalid syntax!\n\nUsage: houston 3DS_IP CIA_FILENAME INSTALLDIR");
return -1;
}
char* host = (char*)(argv[1]);
char* cia_file = (char*)(argv[2]);
char* install_to = (char*)(argv[3]);
// Writing info on the screen
printf("Houston v.1.1\n");
printf("-------------\n");
printf("This version of Houston supports only NASA v.1.6 or higher!\n");
// Creating client socket
Socket my_socket;
memset(&my_socket.addrTo, 0, sizeof(my_socket.addrTo));
my_socket.addrTo.sin_family = AF_INET;
my_socket.addrTo.sin_port = htons(5000);
my_socket.addrTo.sin_addr.s_addr = inet_addr(host);
my_socket.sock = socket(AF_INET, SOCK_STREAM, 0);
if (my_socket.sock < 0){
printf("\nFailed creating socket.");
return -1;
}else printf("\nClient socket created on port 5000");
fflush(stdout);
// Connecting to NASA
int err = connect(my_socket.sock, (struct sockaddr*)&my_socket.addrTo, sizeof(my_socket.addrTo));
if (err < 0 ){
printf("\nFailed connecting server.");
close(my_socket.sock);
return -1;
}else printf("\nConnection estabilished, waiting for NASA response...");
fflush(stdout);
// Setting as non-block the socket
int flags = fcntl(my_socket.sock, F_GETFL, 0);
fcntl(my_socket.sock, F_SETFL, flags | O_NONBLOCK);
// Waiting for greetings
waitResponse(my_socket.sock);
char cmd[32768];
// Changing directory for NAND/SDMC selection
sprintf(cmd,"CWD /%s",install_to);
printf("\nExecuting: %s",cmd);
fflush(stdout);
send(my_socket.sock, cmd, strlen(cmd), 0);
waitResponse(my_socket.sock);
// Opening Data Socket
Socket data_socket;
memset(&data_socket.addrTo, 0, sizeof(data_socket.addrTo));
data_socket.addrTo.sin_family = AF_INET;
data_socket.addrTo.sin_port = htons(5001);
data_socket.addrTo.sin_addr.s_addr = inet_addr(host);
data_socket.sock = socket(AF_INET, SOCK_STREAM, 0);
if (data_socket.sock < 0){
printf("\nFailed creating socket.");
return -1;
}else printf("\nData socket created on port 5001");
fflush(stdout);
// Initializing transfer
sprintf(cmd,"STOR %s",cia_file);
printf("\nExecuting: %s",cmd);
fflush(stdout);
send(my_socket.sock, cmd, strlen(cmd), 0);
waitResponse(my_socket.sock);
// Connecting Data Socket
err = connect(data_socket.sock, (struct sockaddr*)&data_socket.addrTo, sizeof(data_socket.addrTo));
if (err < 0 ){
printf("\nFailed connecting server.");
close(data_socket.sock);
close(my_socket.sock);
return -1;
}
// Transfering file
FILE* input = fopen(cia_file,"r");
if (input < 0){
printf("\nFile not found.");
close(data_socket.sock);
close(my_socket.sock);
return -1;
}
fseek(input, 0, SEEK_END);
int size = ftell(input);
fseek(input, 0, SEEK_SET);
int res = sendData(data_socket.sock, size, input, my_socket.sock);
fclose(input);
close(data_socket.sock);
// If NASA closed transfer, we need to reset STOR status
if (res < 0){
sprintf(cmd,"STOR %s",cia_file);
send(my_socket.sock, cmd, strlen(cmd), 0);
}
// Finishing file transfer
waitResponse(my_socket.sock);
// If NASA closed transfer, we need to reset STOR status
if (strncmp(ret_code,"426",3) == 0){
sprintf(cmd,"STOR %s",cia_file);
send(my_socket.sock, cmd, strlen(cmd), 0);
waitResponse(my_socket.sock);
}
// Closing connection
close(my_socket.sock);
printf("\n"); // Needed for the shitty Mac shell :/
return 0;
}