-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunix_abstract_socket_server.c
70 lines (57 loc) · 1.78 KB
/
unix_abstract_socket_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
#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <stddef.h>
#define SOCKET_NAME "#snap.dclanebus.dclusock"
#define ECHO_MSG "you said: "
#define ECHO_LEN 10
int main(int argc, char** argv) {
struct sockaddr_un addr;
int sock = 0;
int ret = 0;
int nRead = 0;
char buf[1024];
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock <= 0) {
printf("failed to make socket\n");
return -1;
}
printf("blank unix socket created\n");
ret = remove(SOCKET_NAME);
if (ret == -1) {
if (errno != ENOENT) {
printf("socket file exists, couldn't be deleted\n");
return 0;
}
} else {
printf("old socket deleted\n");
}
memset(&addr, 0, sizeof(struct sockaddr_un));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, SOCKET_NAME, sizeof(addr.sun_path));
addr.sun_path[0] = '\0'; //to make it an abstract socket namespace
ret = bind(sock, (struct sockaddr *)&addr, offsetof(struct sockaddr_un, sun_path)+strlen(SOCKET_NAME));
if (ret == -1) {
printf("failed to bind to socket\n");
return -1;
}
printf("socket bound to %s\n", SOCKET_NAME);
ret = listen(sock, 2);
if (ret == -1) {
printf("failed to start listening\n");
return -1;
}
//might put this into a loop, but for now lets just handle one connection then quit
while (1) {
printf("listening for connection\n");
int conn = accept(sock, NULL, NULL);
printf("connection accepted :)\n");
//simple echo loop
strncpy(buf, ECHO_MSG, ECHO_LEN);
while ( (nRead = read(conn, &buf[ECHO_LEN], 1024-ECHO_LEN)) > 0) {
write(conn, buf, nRead+ECHO_LEN);
}
}
}