-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver3.c
304 lines (280 loc) · 7.33 KB
/
server3.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#define BUF_SIZ 500
#define BACKLOG 10
#define MAX_NAMELEN 51
#define KEY_LEN 500
#define CIPHER_LEN 500
typedef struct {
int sock;
} thread_args_t;
typedef struct k {
char nama[MAX_NAMELEN];
char public_key[KEY_LEN];
int sock;
struct k* next;
} Klien;
Klien *daftarKlien = NULL;
short isAlphaNumeric(char c) {
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9');
}
int start_server(int *sockfd, char* port, int backlog) {
struct addrinfo hints, *res;
int status;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
status = getaddrinfo(NULL, port, &hints, &res);
if (status) {
printf("Gagal saat getaddrinfo!, status: %d\n", status);
return(-1);
}
*sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
status = bind(*sockfd, res->ai_addr, res->ai_addrlen);
if (status) {
printf("Gagal saat bind!, status: %d\n", status);
return(-1);
}
listen(*sockfd, backlog);
return 0;
}
Klien* search(const char *nama, Klien *node) {
while(node != NULL && strcmp(node->nama, nama)) {
node = node->next;
}
return node;
}
void insert(const char *nama, const char *key, int sock, Klien **node) {
if (*node == NULL) {
*node = (Klien*) malloc(sizeof(Klien));
strcpy((*node)->nama, nama);
strcpy((*node)->public_key, key);
(*node)->sock = sock;
(*node)->next = NULL;
}
else {
Klien* iterator = *node;
while (iterator->next != NULL) {
iterator = iterator->next;
}
iterator->next = (Klien*) malloc (sizeof(Klien));
strcpy(iterator->next->nama, nama);
strcpy(iterator->next->public_key, key);
iterator->next->sock = sock;
iterator->next->next = NULL;
}
}
void delete(const char *nama, Klien **node) {
if (strcmp((*node)->nama, nama) == 0) {
Klien* temp = *node;
*node = (*node)->next;
free(temp);
}
else {
Klien* iterator = *node;
while(iterator->next != NULL && strcmp(iterator->next->nama, nama)) {
iterator = iterator->next;
}
if (iterator->next != NULL) {
Klien* temp = iterator->next->next;
free(iterator->next);
iterator->next = temp;
}
}
}
void *run(void *t_args) {
thread_args_t *args = (thread_args_t*) t_args;
char buf[BUF_SIZ], msg[BUF_SIZ], temp[BUF_SIZ], key[KEY_LEN];
char nama[MAX_NAMELEN], namaPenerima[MAX_NAMELEN];
int bytes, len1;
int i, j;
short connected = 0;
//----------------------------------------------------------------
// Inisialisasi
//----------------------------------------------------------------
//Kirim pesan OK ketika klien pertama kali konek
strcpy(buf, "OK");
send(args->sock, buf, strlen(buf), 0);
//terima nama
bytes = recv(args->sock, buf, BUF_SIZ-1, 0);
buf[bytes] = 0;
//copy bagian nama ke variabel nama
i = 0;
while (i < bytes && isAlphaNumeric(buf[i])) {
nama[i] = buf[i];
i++;
}
nama[i] = 0;
i += 1;
//copy sisanya, masukkan ke key
strcpy(key, buf+i);
printf("Dapat nama-key: %s-%s\n", nama, key);
//cari nama dalam daftar klien
Klien* searchResult = search(nama, daftarKlien);
//kalau nama belum ada (belum terpakai)
if (searchResult == NULL) {
//kirim OK
strcpy(buf, "OK");
send(args->sock, buf, strlen(buf), 0);
//kirim nama klien yang baru konek ke klien-klien lain
sprintf(buf, "!CONNECT %s", nama);
Klien* node = daftarKlien;
while (node != NULL) {
send(node->sock, buf, strlen(buf), 0);
node = node->next;
}
sleep(1);
//Kirim daftar klien ke klien yang baru konek
Klien *iterator = daftarKlien;
while (iterator != NULL) {
sprintf(buf, "!CONNECT %s", iterator->nama); //!CONNECT <nama klien>
send(args->sock, buf, strlen(buf), 0);
iterator = iterator->next;
}
//Masukkan klien baru ke daftar klien
insert(nama, key, args->sock, &daftarKlien);
connected = 1;
}
//kalau sudah ada klien dengan nama == buf, kirim pesan NOPE
else {
strcpy(buf, "NOPE");
send(args->sock, buf, strlen(buf), 0);
close(args->sock);
free(args);
pthread_exit(NULL);
}
//-------------------------------------------------------------------------
// Inisialisasi selesai
//-------------------------------------------------------------------------
while((bytes = recv(args->sock, buf, BUF_SIZ-1, 0)) > 0) {
buf[bytes] = 0;
printf("%s: %s\n", nama, buf);
printf("--");
if(buf[0] == '!') {
printf("<Command>");
i = 1;
j = 0;
while ((i < bytes) && isAlphaNumeric(buf[i])) {
temp[j] = buf[i];
i++;
j++;
}
temp[j] = 0;
printf(" (%s) ", temp);
//kalau !GET
if (strcmp(temp, "GET") == 0) {
i += 1;
//--NCAT DEBUGGING BEGIN, HARAP DI-DELETE!!!!
// buf[bytes-1] = 0;
//--NCAT DEBUGGING END
strcpy(temp, buf+i); //temp akan berisi nama klien yang public keynya di-request
Klien *k = search(temp, daftarKlien);
//kalau klien tidak ditemukan
if (k == NULL) {
//strcpy(buf, "!NONAME");
//send(args->sock, buf, strlen(buf), 0);
continue;
}
//kirim nama klien dan public key-nya
printf(" GET %s\n", temp);
sprintf(buf, "!KEYPU %s %s", temp, k->public_key);
send(args->sock, buf, strlen(buf), 0);
sprintf(buf, "!KEYPU %s %s", nama, key);
send(k->sock, buf, strlen(buf), 0);
}
if (strcmp(temp, "KEYSI") == 0) {
printf("KEYSI\n");
i += 1;
j = 0;
char sym_key[CIPHER_LEN];
//ambil nama klien tujuan
while ((i < bytes) && isAlphaNumeric(buf[i])) {
temp[j] = buf[i];
i++;
j++;
printf("i: %d, j: %d, buf[%d]: %c", i, j, i, buf[i]);
}
temp[j] = 0;
printf("temp: %s", temp);
Klien *k = search(temp, daftarKlien);
if (k == NULL) {
//kalau kien tidak ditemukan
continue;
}
i += 1;
strcpy(sym_key, buf+i);
printf(" KEYSI %s %s\n", temp, sym_key);
sprintf(buf, "!KEYSI %s %s", nama, sym_key);
send(k->sock, buf, strlen(buf), 0);
}
}
else {
printf("<Chat>");
i = 0;
while ((i < bytes) && buf[i] != ':') {
temp[i] = buf[i];
i++;
}
if (i == bytes) { //titik dua tidak ditemukan
continue;
}
temp[i] = 0;
i += 2;
strcpy(msg, buf+i);
Klien *k = search(temp, daftarKlien);
if (k == NULL) {
continue;
}
sprintf(buf, "%s: %s", nama, msg);
send(k->sock, buf, strlen(buf), 0);
}
printf("END\n");
}
//tutup socket
close(args->sock);
if (connected) {
//hapus klien dari daftar klien
delete(nama, &daftarKlien);
//kirim pesan !DISCONNECT [nama_klien] ke klien-klien lainnya
sprintf(buf, "!DISCONNECT %s\r\n", nama);
Klien* node = daftarKlien;
while (node != NULL) {
send(node->sock, buf, strlen(buf), 0);
node = node->next;
}
}
//cleaning up
free(args);
pthread_exit(NULL);
}
int main (int argc, char* argv[]) {
if (argc < 2) {
printf("Penggunaan: server port");
exit(-1);
}
//--------------------------------------------------
int status;
int sockfd, new_fd;
struct sockaddr_storage their_addr;
socklen_t addr_size;
start_server(&sockfd, argv[1], BACKLOG);
while(1)
{
new_fd = accept(sockfd, (struct sockaddr *) &their_addr, &addr_size);
thread_args_t *args = (thread_args_t*) malloc(sizeof(thread_args_t));
args->sock = new_fd;
pthread_t thread;
status = pthread_create(&thread, NULL, run, (void*)args);
if (status) {
printf("pthread_create error: %d\n", status);
}
}
pthread_exit(NULL);
}