-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
88 lines (71 loc) · 1.85 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
#include <sys/un.h>
#include <ctype.h>
#include <stdlib.h>
#include <stddef.h>
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include "server.h"
#include "history.h"
static void clear_history() {
history_clear();
}
static void send_history(int sd) {
for(struct hist_ent *ent = history;ent;ent=ent->next) {
//We can ignore type and endianness issues since the client
//will be run on the same machine.
send(sd, &ent->data_sz, sizeof(size_t), 0);
send(sd, ent->data, ent->data_sz, 0);
}
close(sd);
}
void server_handle_connection(int client) {
enum CLIENT_REQUEST type;
recv(client, &type, sizeof type, 0);
switch(type) {
size_t num;
int res = 0;
char *content;
ssize_t sz;
case CR_SET_CLIPBOARD:
recv(client, &sz, sizeof(sz), MSG_WAITALL);
content = malloc(sz);
recv(client, content, sz, MSG_WAITALL);
history_add_paste(content, sz);
free(content);
break;
case CR_CLEAR:
clear_history();
break;
case CR_HIST:
send_history(client);
break;
case CR_ACTIVATE_PASTABLE:
recv(client, &num, sizeof(num), MSG_WAITALL);
history_raise(num);
send(client, &res, sizeof(res), 0);
break;
}
close(client);
}
int server_create(const char *path) {
int sd;
struct sockaddr_un addr;
if((sd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("server socket");
exit(1);
}
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, path);
if(bind(sd, (struct sockaddr*)&addr, sizeof addr) == -1) {
perror("binding server socket");
exit(1);
}
if(listen(sd, 10) == -1) {
perror("listening on server socket");
exit(1);
}
return sd;
}