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
|
/*
* fwd - OpenWrt firewall daemon - unix domain socket parts
*
* Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
*
* The fwd program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* The fwd program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with the fwd program. If not, see http://www.gnu.org/licenses/.
*/
#include "fwd.h"
#include "fwd_ipc.h"
int fwd_ipc_listen(void)
{
int fd;
struct sockaddr_un addr;
if( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0 )
fwd_fatal("Cannot create AF_UNIX socket: %m");
memset(&addr, 0, sizeof(struct sockaddr_un));
strcpy(addr.sun_path, FWD_SOCKET_PATH);
addr.sun_family = AF_UNIX;
unlink(FWD_SOCKET_PATH);
if( bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0 )
fwd_fatal("Cannot bind AF_UNIX socket: %m");
if( listen(fd, 1) < 0 )
fwd_fatal("Cannot listen on AF_UNIX socket: %m");
//fcntl(fd, F_SETFL, O_NONBLOCK);
return fd;
}
int fwd_ipc_accept(int fd)
{
return accept(fd, NULL, NULL);
}
int fwd_ipc_connect(void)
{
int fd;
struct sockaddr_un addr;
if( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0 )
fwd_fatal("Cannot create AF_UNIX socket: %m");
memset(&addr, 0, sizeof(struct sockaddr_un));
strcpy(addr.sun_path, FWD_SOCKET_PATH);
addr.sun_family = AF_UNIX;
if( connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0 )
fwd_fatal("Cannot connect AF_UNIX socket: %m");
fcntl(fd, F_SETFL, O_NONBLOCK);
return fd;
}
int fwd_ipc_recvmsg(int fd, void *buf, int len)
{
return recv(fd, buf, len, 0);
}
int fwd_ipc_sendmsg(int fd, void *buf, int len)
{
return send(fd, buf, len, 0);
}
void fwd_ipc_shutdown(int fd)
{
shutdown(fd, SHUT_RDWR);
close(fd);
}
int fwd_ipc_sendtype(int fd, enum fwd_ipc_msgtype type)
{
struct fwd_ipc_msg msg;
memset(&msg, 0, sizeof(struct fwd_ipc_msg));
msg.type = type;
return fwd_ipc_sendmsg(fd, &msg, sizeof(struct fwd_ipc_msg));
}
|