-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.c
107 lines (101 loc) · 2.5 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
#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<unistd.h>
#include"sha1.h"
long long int e=0,n=0;
long long int modexp(long long int x, long long int y)
{
long long int res=1;
x = x % n;
while (y > 0)
{
if (y & 1)
res = (res*x) % n;
y = y>>1;
x = (x*x) % n;
}
if(res>0)
return res;
else
return res+n;
}
int main(int argc , char *argv[])
{
int socket_desc ,i, client_sock , c , read_size ,r2,r3,r4;
int flag=1;
struct sockaddr_in server , client;
unsigned char client_message[2000];
char sign[2000];
char hash[2000];
int length;
SHA1_CTX obj;
long long int m1, m2, ci[50];
char server_message[2000];
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
perror("bind failed. Error");
return 1;
}
puts("bind done");
listen(socket_desc , 3);
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
puts("Connection accepted");
while( ((read_size = recv(client_sock , client_message , 2000 , 0)) >0) && (r2=recv(client_sock , ci , sizeof(ci) , 0)>0) && (r3=recv(client_sock , &e , sizeof(e) , 0)>0) && (r4=recv(client_sock , &n , sizeof(n) , 0)>0))
{
flag=1;
printf("CLIENT MESSAGE\n");
//strcat(client_message,"a");
puts(client_message);
length=strlen(client_message);
sha1_init(&obj);
sha1_update(&obj,client_message,length);
sha1_final(&obj,hash);
for(i=0;i<strlen(hash);i++)
{
m1=modexp(ci[i],e);
m2=(long long int)hash[i];
if(m2<0)
m2=m2+n;
if(m1!=m2)
{
flag=0;
break;
}
}
if(flag)
strcpy(server_message,"Authentication successful!");
else
strcpy(server_message,"Authentication failed!");
n=0; e=0;
//strcpy(server_message,"");
write(client_sock , server_message , strlen(server_message));
}
if(read_size == 0)
{
puts("Client disconnected");
fflush(stdout);
}
else if(read_size == -1)
{
perror("recv failed");
}
return 0;
}