-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.c
More file actions
162 lines (135 loc) · 4.47 KB
/
admin.c
File metadata and controls
162 lines (135 loc) · 4.47 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <netinet/in.h>
#define BUFLEN 1024
void erro(char *msg)
{
printf("Erro: %s\n", msg);
exit(-1);
}
int main(int argc, char *argv[])
{
char endServer[100];
int fd;
struct sockaddr_in addr;
struct hostent *hostPtr;
if (argc != 3)
{
printf("./admin <host> <port>\n"); // PORTO_CONFIG
exit(-1);
}
// create a UDP connection to the server
strcpy(endServer, argv[1]);
if ((hostPtr = gethostbyname(endServer)) == 0)
erro("Não consegui obter endereço");
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[2]));
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
erro("socket");
// send a message to the server saying that we wanna authenticate
char buffer[BUFLEN];
int n;
strcpy(buffer, "AUTH"); // Message indicating authentication request
if (sendto(fd, buffer, strlen(buffer), 0, (struct sockaddr *)&addr, sizeof(addr)) == -1)
erro("sendto");
// wait for server to send data
bzero(buffer, BUFLEN);
socklen_t addrlen = sizeof(addr);
n = recvfrom(fd, buffer, BUFLEN, 0, (struct sockaddr *)&addr, &addrlen);
if (n < 0)
erro("ERROR reading from socket");
printf("Recieved: %s\n", buffer);
// check if server asked for authentication
if (strcmp(buffer, "AUTH") == 0)
{
while (1)
{
char username[BUFLEN];
char password[BUFLEN];
printf("Username: ");
bzero(username, BUFLEN);
fgets(username, BUFLEN, stdin);
username[strcspn(username, "\r\n")] = '\0';
printf("\nPassword: ");
bzero(password, BUFLEN);
fgets(password, BUFLEN, stdin);
password[strcspn(password, "\r\n")] = '\0';
// send to the server the username and password, create a string like this "username;password"
char auth[BUFLEN];
bzero(auth, BUFLEN);
strcat(auth, username);
strcat(auth, ";");
strcat(auth, password);
if (sendto(fd, auth, strlen(auth), 0, (struct sockaddr *)&addr, sizeof(addr)) == -1)
erro("sendto");
// wait for server to send data
bzero(buffer, BUFLEN);
n = recvfrom(fd, buffer, BUFLEN, 0, (struct sockaddr *)&addr, &addrlen);
if (n < 0)
erro("ERROR reading from socket");
printf("Recieved: %s\n", buffer);
// check if authentication was successful
if (strcmp(buffer, "OK") == 0)
{
printf("Authentication successful\n");
break;
}
else
{
printf("Please try again...\n");
}
}
}
/*
Server recognises these commands:
ADD_USER {username} {password} {administrador/cliente/jornalista}
Eliminar um utilizador
▪ DEL {username}
Lista utilizadores
▪ LIST
Sair da consola
▪ QUIT
Desligar servidor
▪ QUIT_SERVER
*/
while (1)
{
char command[BUFLEN];
printf("server@admin$ ");
// read the command using fgets from console
bzero(command, BUFLEN);
fgets(command, BUFLEN, stdin);
command[strcspn(command, "\r\n")] = '\0';
// send the command to the server
if (sendto(fd, command, strlen(command), 0, (struct sockaddr *)&addr, sizeof(addr)) == -1)
erro("sendto");
// wait for server to send data
bzero(buffer, BUFLEN);
n = recvfrom(fd, buffer, BUFLEN, 0, (struct sockaddr *)&addr, &addrlen);
if (n < 0)
erro("ERROR reading from socket");
//printf("Recieved: %s\n", buffer);
if (strcmp(buffer, "OK") == 0)
{
printf("Command executed successfully\n");
}
else if (strcmp(buffer, "QUIT") == 0)
{
printf("Exiting...\n");
break;
}
else if (strcmp(buffer, "GOODBYE") == 0)
{
printf("Shutted down server...\n");
} else {
// print buffer
printf("User List:\n---------------\n%s\n---------------\n", buffer);
}
//todo check if server asked for authentication?
}
exit(0);
}