-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
262 lines (156 loc) · 4.38 KB
/
server.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <memory.h>
#include <errno.h>
#include "common.h"
#include <arpa/inet.h>
#include "totp.h"
#include<arpa/inet.h>
#define MAX_CLIENT_SUPPORTED 32
#define SERVER_PORT 2000
char data_buffer[1024];
int monitored_fd_set[32];
void init_monitor_fd_set()
{
for(int i=0;i<MAX_CLIENT_SUPPORTED;i++)
monitored_fd_set[i]=-1;
}
void add_to_monitored_fd_set(int skt_fd)
{
for(int i=0;i<MAX_CLIENT_SUPPORTED;i++)
{
if(monitored_fd_set[i]==-1)
{
monitored_fd_set[i]=skt_fd;
break;
}
}
}
void remove_from_monitored_fd_set(int skt_fd)
{
for(int i=0;i<MAX_CLIENT_SUPPORTED;i++)
{
if(monitored_fd_set[i]==skt_fd)
monitored_fd_set[i]=-1;
}
}
void re_init_readfds(fd_set *fd_set_ptr)
{
FD_ZERO(fd_set_ptr);
for(int i=0;i<MAX_CLIENT_SUPPORTED;i++)
{
if(monitored_fd_set[i]!=-1)
FD_SET(monitored_fd_set[i],fd_set_ptr);
}
}
int get_max_fd()
{
int max=-1;
for(int i=0;i<MAX_CLIENT_SUPPORTED;i++)
{
if(monitored_fd_set[i]>max)
max=monitored_fd_set[i];
}
return max;
}
void start_server()
{
int master_socket=0;
int comm_socket=0;
int sent_recv_bytes=0;
fd_set readfds;
struct sockaddr_in server;
struct sockaddr_in client;
init_monitor_fd_set();
if((master_socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==-1)
{
printf("Socket Creation Failed\n");
exit(1);
}
server.sin_family=AF_INET;
server.sin_port=SERVER_PORT;
server.sin_addr.s_addr=INADDR_ANY;
int addr_len = sizeof(struct sockaddr);
if(bind(master_socket, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1)
{
printf("socket bind failed:%d\n",errno);
return;
}
if (listen(master_socket, 5)<0)
{
printf("listen failed\n");
return;
}
add_to_monitored_fd_set(master_socket);
while(1){
re_init_readfds(&readfds);
printf("blocked on select System call...\n");
select(get_max_fd() + 1, &readfds, NULL, NULL, NULL);
if(FD_ISSET(master_socket,&readfds))
{
printf("New Connection Received\n");
comm_socket=accept(master_socket,(struct sockaddr*)&client,&addr_len);
if(comm_socket<0)
{
printf("\nAccept Failed:%d\n",errno);
exit(0);
}
add_to_monitored_fd_set(comm_socket);
printf("Connection accepted from client : %s:%u\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
}
else
{
int i = 0, comm_socket = -1;
for(; i < MAX_CLIENT_SUPPORTED; i++){
if(FD_ISSET(monitored_fd_set[i], &readfds)){
comm_socket = monitored_fd_set[i];
memset(data_buffer, 0, sizeof(data_buffer));
sent_recv_bytes = recvfrom(comm_socket, (char *)data_buffer, sizeof(data_buffer), 0, (struct sockaddr *)&client, &addr_len);
printf("Server recvd %d bytes from client %s:%u\n", sent_recv_bytes,
inet_ntoa(client.sin_addr), ntohs(client.sin_port));
if(sent_recv_bytes == 0){
close(comm_socket);
remove_from_monitored_fd_set(comm_socket);
break;
}
unsigned int *x = (unsigned int*)data_buffer;
//printf("\nDATA RECEIVED:%d\n",*x);
if(x==0){
close(comm_socket);
remove_from_monitored_fd_set(comm_socket);
printf("Server closes connection with client : %s:%u\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
/*Goto state machine State 1*/
break;/*Get out of inner while loop, server is done with this client, time to check for new connection request by executing selct()*/
}
size_t pos;
size_t len;
size_t keylen;
uint8_t *k;
char *fname = NULL;
char key[]="JBSWY3DPEHPK3PXP";
int mode = 0;
int opt;
uint32_t result;
len = strlen(key);
k = (uint8_t *)key;
keylen = decode_b32key(&k, len);
result = totp(k,keylen);
int mssg=0;
if(result==*x){
mssg=1;
}
sent_recv_bytes = sendto(comm_socket, (int*)&mssg, sizeof(int), 0,
(struct sockaddr *)&client, sizeof(struct sockaddr));
printf("Server sent %d bytes in reply to client\n", sent_recv_bytes);
}
}
}
}
}
int main()
{
start_server();
}