-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
194 lines (159 loc) · 6.53 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
192
193
/*
* RCI Project - Distributed Hashtable - João Maria Janeiro and Guilherme Viegas
* Key values equal to -1 mean that it does not contain information about that server
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "server.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/time.h>
#include <math.h>
#include <sys/select.h>
#include <errno.h>
#define N 16
int checkIpPortValid(int, char **);
int valid_digit(char *);
int checkIp(char *);
void flush_stdin();
char aux[100];
char c;
int n;
char buff[100];
char ip[100];
struct addrinfo *res;
UdpData* udp_main;
int main(int argc, char *argv[]) {
strcpy(ip, argv[1]);
if(!checkIpPortValid(argc, argv)) exit(0); // Check if the dkt program was summoned with 3 arguments
Server * myServer = (Server*)malloc(sizeof(Server));
strcpy(myServer->myIp, ip);
strcpy(myServer->myPort, argv[2]);
// Show the user interface
printf("\n\nAvailable commands:\n\n new i \n sentry i succi succi.IP succi.TCP\n entry i succi succie.IP succi.TCP\n leave\n exit\n");
strcpy(buff, "\0");
fgets(buff, 100 , stdin);
const char delim[2] = " ";
if(strcmp(strtok(strdup(buff), delim), "new") == 0) { // If its the "new" command then create a ring with that server
sscanf(buff, "%s %d", aux, &(myServer->myKey)); // Get the server key
if(myServer->myKey > N) {
printf("The key must be smaller than the ring size. Ring size is currently set to 16\n");
do {
printf("Please choose another key (must be between 1-16)\n");
printf("New Key: ");
scanf("%d", &(myServer->myKey));
} while(myServer->myKey > N);
}
createServer(myServer, 0);
} else if(strcmp(strtok(strdup(buff), delim), "sentry") == 0) { // If its the sentry command open a connection with nextServer
sscanf(buff, "%s %d %d %s %s", aux, &(myServer->myKey), &(myServer->nextKey), myServer->nextIp, myServer->nextPort); // Get the successor details
if(myServer->myKey > N) {
printf("The key must be smaller than the ring size. Ring size is currently set to 16\n");
do {
printf("Please choose another key (must be between 1-16)\n");
printf("New Key: ");
scanf("%d", &(myServer->myKey));
} while(myServer->myKey > N);
}
printf("Trying to enter\n");
myServer->nextConnFD = connectToNextServer(myServer); // Set the next server as the given server and establish a connection
sprintf(buff, "NEW %d %s %s\n", myServer->myKey, myServer->myIp, myServer->myPort);
int n = write(myServer->nextConnFD, buff, strlen(buff)); // Give the successor your details
if(n == -1)/*error*/exit(1);
createServer(myServer, 1); //Now that the entry connections are established and stable it's time enter in listening mode
} else if(strcmp(strtok(strdup(buff), delim), "entry") == 0) {
char connectIp[100], connectPort[100];
int connectKey;
sscanf(buff, "%s %d %d %s %s", aux, &(myServer->myKey), &connectKey, connectIp, connectPort); // Get the successor details
if(myServer->myKey > N) {
printf("The key must be smaller than the ring size. Ring size is currently set to 16\n");
do {
printf("Please choose another key (must be between 1-16)\n");
printf("New Key: ");
scanf("%d", &(myServer->myKey));
} while(myServer->myKey > N);
}
udp_main = connectToUdpServer(connectIp, connectPort);
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 100000; // 100 ms timeout
setsockopt(udp_main->fd, SOL_SOCKET, SO_RCVTIMEO,&tv,sizeof(tv));
sprintf(buff, "EFND %d\n", myServer->myKey);
do { // If the answer from the server isn't received, send the request again
n=sendto(udp_main->fd,buff,strlen(buff),0, udp_main->res->ai_addr, udp_main->res->ai_addrlen);
if(n==-1) /*error*/ exit(1);
} while ((n=recvfrom(udp_main->fd,buff,128,0, (struct sockaddr*)&(udp_main->res->ai_addr),&(udp_main->res->ai_addrlen))) < 0);
sscanf(buff, "%s %d %d %s %s", aux, &(myServer->myKey), &(myServer->nextKey), myServer->nextIp, myServer->nextPort); // Get the successor details
if(myServer->myKey == myServer->nextKey) {
printf("Duplicate keys are not allowed, please try to enter with another key!\n");
} else {
myServer->nextConnFD = connectToNextServer(myServer); // Set the next server as the given server and establish a connection
sprintf(buff, "NEW %d %s %s\n", myServer->myKey, myServer->myIp, myServer->myPort);
int n = write(myServer->nextConnFD, buff, strlen(buff)); // Give the successor your details
if(n == -1)/*error*/exit(1);
createServer(myServer, 1); //Now that the entry connections are established and stable it's time enter in listening mode
}
freeaddrinfo(udp_main->res);
free(udp_main);
close(udp_main->fd);
} else if(strstr(buff, "exit") != NULL) {
free(myServer);
exit(0);
}
free(myServer);
return 0;
}
int checkIpPortValid(int _argc, char *_argv[]) {
if(_argc != 3) {
printf("Number of Arguments must be 3\n");
exit(0);
}
if(!checkIp(_argv[1])) {
exit(0);
}
return 1;
}
/* return 1 if string contain only digits, else return 0 */
int valid_digit(char *ip_str) {
while (*ip_str) {
if (*ip_str >= '0' && *ip_str <= '9')
++ip_str;
else
return 0;
}
return 1;
}
/*Check the Ip Address format*/
int checkIp(char * ip) {
char* ptr;
int num, dots = 0;
ptr = strtok(ip, ".");
if (ptr == NULL)
return 0;
while (ptr) {
if (!valid_digit(ptr)) {
printf("Not Valid Digit\n");
return 0;
}
num = atoi(ptr);
if (num >= 0 && num <= 255) {
ptr = strtok(NULL, ".");
if (ptr != NULL)
++dots;
} else {
printf("Did you enter a number in the ip?\n");
return 0;
}
}
/* valid IP string must contain 3 dots */
if (dots != 3) {
printf("Number of dots must be of 3 %d\n", dots);
return 0;
}
return 1;
}