-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
154 lines (100 loc) · 2.83 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
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <pthread.h>
#include "protocol.h"
typedef struct {
int self;
int other;
char *name;
} relay;
int verifyme(int s, char **username) {
char buff[100]; //THIS IS A VULNERABILITY
recv(s, buff, 1602, 0);
int uname_length = strlen(buff);
*username = (char *) malloc(sizeof(char) * uname_length);
strcpy(*username, buff);
char password[800];
strcpy(password, (buff + uname_length + 1));
if (!strcmp(*username, "user1")) {
return !strcmp(password, "password1");
}
else if (!strcmp(*username, "user2")) {
return !strcmp(password, "password2");
}
return 0;
}
void *run_client(void *arg) {
relay *both = (relay *) arg;
char outbuf[1602];
char inbuf[801];
strcpy(outbuf,both->name);
while (1) {
recv(both->self, inbuf, 801, 0);
strcpy(outbuf + strlen(both->name) + 1, inbuf);
send(both->other, outbuf, strlen(outbuf) + 1 +strlen(inbuf)+ 1, 0);
}
return NULL;
}
int main(int argc, char *argv[]) {
printf("%p\ntest\n", &main);
int s; //accepting client connections
struct sockaddr_in server; //our setup
struct sockaddr_in client_connection; //to accept a new client
char *ret = "no";
char *ok = "ok";
if (argc != 2) {
printf("proper usage is ./server <port>");
return 1;
}
server.sin_family = AF_INET;
server.sin_port = htons(atoi(argv[1]));
inet_pton(AF_INET, IP_ADDR, &server.sin_addr);
if ( (s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Problem resolving socket");
return 1;
}
if (bind(s, (struct sockaddr *) &server, sizeof(server)) == -1) {
perror("Couldn't bind to socket. Maybe something is using it.");
return 1;
}
//listen to the socket
listen(s, 2); //up to 10 people can connect and chat at a time.
int clients[2];
char *names[2];
for (int i = 0; i < 2; i++ ) {
socklen_t expected_size = sizeof(client_connection);
int client = accept(s, (struct sockaddr *) &client_connection, &expected_size);
if (!verifyme(client, &names[i])) {
send(client, ret, 2, 0);
return 1;
}
else {
send(client, ok, 2, 0);
}
clients[i] = client;
}
relay client_args[2];
client_args[0].self = clients[0];
client_args[1].self = clients[1];
client_args[0].other = clients[1];
client_args[1].other = clients[0];
client_args[0].name = names[0];
client_args[1].name = names[1];
pthread_t client_threads[2];
pthread_create(&client_threads[0], NULL, run_client, &client_args[0]);
pthread_create(&client_threads[1], NULL, run_client, &client_args[1]);
//wait for the clients to close
pthread_join(client_threads[0], NULL);
pthread_join(client_threads[1], NULL);
return 0;
}