forked from brianwatling/acceptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acceptorclient.c
74 lines (62 loc) · 1.65 KB
/
acceptorclient.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
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <errno.h>
int acceptor_connect(const char* path)
{
//references:
//http://beej.us/guide/bgipc/output/html/multipage/unixsock.html
int sockfd = -1;
struct sockaddr_un remote = {};
int len = 0;
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
if(sockfd < 0)
return -1;
remote.sun_family = AF_UNIX;
if(strlen(path) >= sizeof(remote.sun_path)) {
close(sockfd);
errno = ENAMETOOLONG;
return -2;
}
strncpy(remote.sun_path, path, sizeof(remote.sun_path));
len = strlen(remote.sun_path) + sizeof(remote.sun_family);
if (connect(sockfd, (struct sockaddr*)&remote, len) == -1) {
close(sockfd);
return -3;
}
return sockfd;
}
int acceptor_accept(int acceptor)
{
//references:
//http://www.normalesup.org/~george/comp/libancillary/
//http://lists.canonical.org/pipermail/kragen-hacks/2002-January/000292.htmls
struct msghdr msg = {};
struct iovec iov = {};
char dummy;
int sockfd = -1;
int ccmsg[CMSG_SPACE(sizeof(sockfd)) / sizeof(int)];
struct cmsghdr* cmsg = NULL;
int ret = -1;
iov.iov_base = &dummy;
iov.iov_len = 1;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = ccmsg;
msg.msg_controllen = sizeof(ccmsg);
if(recvmsg(acceptor, &msg, 0) <= 0) {
return -1;
}
cmsg = CMSG_FIRSTHDR(&msg);
if (!cmsg->cmsg_type == SCM_RIGHTS) {
errno = EBADMSG;
return -2;
}
ret = *(int*)CMSG_DATA(cmsg);
if(ret < 0) {
errno = EBADF;
return -3;
}
return ret;
}