-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
173 lines (149 loc) · 5.39 KB
/
client.c
File metadata and controls
173 lines (149 loc) · 5.39 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
/*******************************************************************************
CLIENT
*******************************************************************************/
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <netdb.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <regex.h>
#include <sys/stat.h>
#include <sodium.h>
#define BUFLEN 1024
#define SA struct sockaddr
//Global variables
struct sockaddr_in addr;
struct hostent *hostPtr;
struct stat sta = {0};
//Inteiros
int udpfd, tcpfd,fd;
//Arguments treats
int nread = 0;
//Arrays
char server_dest[100];
char proxy_dest[100];
char buffer[BUFLEN];
//encriptação
unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES];
//Erros
void erro(char *s)
{
perror(s);
exit(1);
}
int main(int argc, char *argv[]) {
char comando[BUFLEN];
if (argc != 5) {
printf("cliente <proxy> <server> <port> <protocol>\n");
exit(-1);
}
strcpy(proxy_dest, argv[1]);
if ((hostPtr = gethostbyname(proxy_dest)) == 0) {
erro("Nao consegui obter endereço proxy");
}
strcpy(server_dest, argv[2]);
if ((hostPtr = gethostbyname(server_dest)) == 0) {
erro("Nao consegui obter endereço server");
}
if(strcmp(argv[4],"tcp")){
erro("Protocolo inválido: escreva tcp");
}
bzero((void *)&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = ((struct in_addr *)(hostPtr->h_addr))->s_addr;
addr.sin_port = htons((short) atoi(argv[3]));
if((fd = socket(AF_INET,SOCK_STREAM,0)) == -1) erro("socket");
if(connect(fd,(SA *)&addr,sizeof (addr)) < 0) erro("Connect");
while(1) {
printf("\nINSIRA UM COMANDO: ");
fgets(comando,30,stdin);
write(fd, comando, strlen(comando) - 1);
char *token = strtok(comando, " ");
if (!strcmp(token, "LIST\n")) {
printf("Waiting for list...\n");
nread = read(fd,buffer,BUFLEN+1);
fflush(stdout);
buffer[nread] = '\0';
printf("Lista dos ficheiros:\n\n%s",buffer);
strcpy(buffer,"");
}
else if (!strcmp(token, "DOWNLOAD")) {
token=strtok(NULL," ");
if (token == NULL) {
printf("nao existe mais string,erro\n");
}
else {
if (!strcmp(token, "TCP")) {
token = strtok(NULL, " ");
if (token == NULL) {
printf("nao existe mais string,erro\n");
}
else if (!strcmp(token, "ENC")) {
//read(fd, key, sizeof(key));
}
else if (!strcmp(token, "NOR")) {
token = strtok(NULL, " ");
if (token == NULL) {
printf("nao existe mais string,erro\n");
}
else {
printf("Waiting for download...\n");
read(fd,buffer,BUFLEN+1);
if(strcmp(buffer,"erro")) {
printf("A armazenar o ficheiro na diretoria de downloads...\n");
char path[30];
strcpy(path,"/media/sf_PIRC/downloads");
if (stat(path, &sta) == -1) {
mkdir(path, 0700);
}
strcat(path,"/");
strcat(path,token);
char* pos;
if((pos=strchr(path,'\n'))!=NULL) *pos='\0';
FILE *txt;
txt = fopen(path, "w");
printf("%s\n",path);
if (txt == NULL) {
erro("Não foi possível criar o ficheiro\n");
}
fputs(buffer, txt);
fclose(txt);
printf("Ficheiro criado com sucesso!\n");
strcpy(buffer,"");
strcpy(path,"");
}else{
printf("FILE DOESENT EXIST\n");
}
}
}
else{
printf("protocolo nao esta certo\n");
}
}
else if (!strcmp(token, "UDP")) {
}
else{
printf("protocolo nao esta certo\n");
}
}
}
else if (strcmp(token, "QUIT")) {
printf("Bye bye\n");
close(fd);
exit(0);
} else {
printf("Comando inválido. Insira um novo comando\n");
}
}
}