-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
211 lines (141 loc) · 4.23 KB
/
client.c
File metadata and controls
211 lines (141 loc) · 4.23 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
210
211
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "network.h"
#include "msg.h"
#define PORT 8000
int send_packet(connection *conn, uint8_t type, const char *data){
struct PackHeader header;
uint32_t dlength;
if(data!=NULL)
dlength=strlen(data);
else dlength=0;
header.type=type;
header.length=htonl(dlength);
if(n_send(conn,&header,sizeof(header))<0)
return -1;
if(dlength>0)
if(n_send(conn,data,dlength)<0)
return -1;
return 0;
}
int main(int argc, char*argv[]){
int sock=0;
struct sockaddr_in serv_addr;
char cmd_buffer[1024];
if((sock=socket(AF_INET, SOCK_STREAM,0))<0){
perror("eroare la crearea socket-ului");
return -1;
}
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(PORT);
const char *ip_addr=(argc>1)? argv[1]: "127.0.0.1";
if(inet_pton(AF_INET,ip_addr,&serv_addr.sin_addr)<=0){
perror("Invalid adress");
return -1;
}
if(connect(sock, (struct sockaddr*)&serv_addr,sizeof(serv_addr))<0)
{
perror("Nu s-a putut realiza conexiunea");
return -1;
}
connection conn;
if (n_init(&conn, sock, 0) < 0) {
printf("Eroare la stabilirea conexiunii securizate.\n");
return -1;
}
printf("Conectat la server (Port %d)\n", PORT);
int logged=0,maxi=3;
char user[50];
while (!logged&&maxi>0) {
char pass[50];
char buffer[128];
printf("Username: ");
scanf("%49s", user);
printf("Password: ");
scanf("%49s", pass);
getchar();
snprintf(buffer, sizeof(buffer), "%s:%s", user, pass);
if (send_packet(&conn, MSG_AUTH_REQ, buffer) < 0) {
printf("Eroare la trimitere. Server deconectat.\n");
return -1;
}
struct PackHeader header;
if (n_recv(&conn, &header, sizeof(header)) != sizeof(header)) {
printf("Serverul a inchis conexiunea.\n");
return -1;
}
uint32_t len = ntohl(header.length);
char *rasp = malloc(len + 1);
n_recv(&conn, rasp, len);
rasp[len] = '\0';
printf("%s\n", rasp);
if (strstr(rasp, "reusita") != NULL) {
logged = 1;
} else { maxi--;
if(maxi) printf(">> Mai incearca o data.\n");
else printf("Ati atins numarul maxim de incercari. Deconectare de la server\n");
}
free(rasp);
}
if(maxi){
struct PackHeader header;
char current_path[256]="~";
char prompt[1024];
while(1){
snprintf(prompt,sizeof(prompt),"\033[1;32m%s@myssh\033[0m:\033[1;34m%s\033[0m$ ", user, current_path);
char *input=readline(prompt);
if(!input)
break;
if(strlen(input)>0)
add_history(input);
strncpy(cmd_buffer,input,sizeof(cmd_buffer)-1);
cmd_buffer[sizeof(cmd_buffer) - 1] = '\0';
size_t len=strlen(cmd_buffer);
if(len>0 && cmd_buffer[len-1]=='\n')
cmd_buffer[len-1]='\0';
free(input);
send_packet(&conn, MSG_CMD,cmd_buffer);
if(n_recv(&conn,&header,sizeof(header))!=sizeof(header)){
printf("Serverul a inchis conexiunea\n");
break;
}
uint32_t lungime=ntohl(header.length);
char *rasp=malloc(lungime+1);
n_recv(&conn, rasp,lungime);
rasp[lungime]='\0';
char *semnatura = "Director schimbat: ";
char *locatie = NULL;
char *cautare = strstr(rasp, semnatura);
while (cautare != NULL) {
locatie = cautare;
cautare = strstr(locatie + 1, semnatura);
}
if (locatie != NULL) {
char *path_start = locatie + 19;
char *path_end = strchr(path_start, '\n');
if (path_end != NULL) {
char temp_save = *path_end;
*path_end = '\0';
strcpy(current_path, path_start);
*path_end = temp_save;
} else {
strcpy(current_path, path_start);
}
}
printf("%s", rasp);
if (lungime > 0 && rasp[lungime-1] != '\n') {
printf("\n");
}
free(rasp);
if(strcmp(cmd_buffer,"exit")==0)
break;
}
n_close(&conn);
}
return 0;
}