-
Notifications
You must be signed in to change notification settings - Fork 0
/
cl.c
152 lines (113 loc) · 3.59 KB
/
cl.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
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <string.h>
#include <arpa/inet.h>
/* default size for input/ output */
#define BUFF_SIZE 2048
/* Declarations */
extern int errno;
int port;
/* ----------- */
/* Func prototypes */
void help_menu();
/* --------------- */
int main (int argc, char *argv[])
{
/* Declarations */
char* buf = (char*)malloc(BUFF_SIZE); /* input buffer - which will be send to the server */
char* r = (char*)malloc(BUFF_SIZE); /* what the server returns */
struct sockaddr_in server; /* connect struct */
int sd; /* socket desc */
/* ------------ */
/* Veryfing calling syntax */
if (argc != 3)
{
printf ("[error] Sintaxa: %s <adresa_server> <port>\n", argv[0]);
return -1;
}
port = atoi (argv[2]);
/* creating the socket */
if ((sd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
{
perror ("[error] Eroare la socket().\n");
return errno;
}
/* filling server struct */
/* socket family */
server.sin_family = AF_INET;
/* server IP */
server.sin_addr.s_addr = inet_addr(argv[1]);
/* connection port */
server.sin_port = htons (port);
/* connecting to the server */
if (connect (sd, (struct sockaddr *) &server,sizeof (struct sockaddr)) == -1)
{
perror ("[client] Eroare la connect().\n");
return errno;
}
/* showing the avalible commands */
help_menu();
while (1)
{
/* taking the input from the client */
printf ("[client] >> ");
fflush (stdout);
read (0, buf, BUFF_SIZE);
if (strncmp(buf, "help", 4) == 0)
{
help_menu();
}
else if (strncmp(buf, "quit", 4) == 0)
{
write (sd, buf, BUFF_SIZE);
break;
}
else
{
/* sending input to the server */
if (write (sd, buf, BUFF_SIZE) <= 0)
{
perror ("[client] Eroare la write() spre server.\n");
return errno;
}
/* reading the output of the server */
if (read (sd, r, BUFF_SIZE) < 0)
{
perror ("[client] Eroare la read() de la server.\n");
return errno;
}
/* printing the output */
printf ("[server_output] ->\n%s\n", r);
}
/* cleaning both input and output buffs */
buf = (char*)malloc(BUFF_SIZE);
r = (char*)malloc(BUFF_SIZE);
}
close (sd);
}
void help_menu()
{
printf("\n--- Welcome to the password_manager app! To START, enter one of the commands below: ---\n");
printf("--- (Remember!): To start using the app, you must register or log in first! ---\n");
printf("--- AVALIBLE COMMANDS: ---\n");
printf("--- <register> <log in> <reset_password> ---\n");
printf("--- <add_account> <show_accounts> <edit_account> ---\n");
printf("--- <list_categories> <list_accounts> ---\n");
printf("--- SYNTAX OF THE COMMANDS: ---\n");
printf("--- For <register> and <log in>: command:username;password ---\n");
printf("--- For <reset_password>: command:username ---\n");
printf("--- For <add_account>: command:category ---\n");
printf("--- For <show_account> and <edit_account>: command:category;title ---\n");
printf("--- For <list_categories>: command ---\n");
printf("--- For <list_accounts>: command:category ---\n");
printf("--- HOW TO USE: ---\n");
printf("--- <add_acount> will create a file with the following fields (in this order): ---\n");
printf("--- category, account title, username, password, url, notes. ---\n");
printf("--- The input for <add_account> will be given in this order, every field being separated by a quote ---\n\n");
}