Move mjpg-streamer.pot to the right directory
[project/luci.git] / contrib / fwd / src / fwd_ipc.c
1 /*
2  * fwd - OpenWrt firewall daemon - unix domain socket parts
3  *
4  *   Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
5  *
6  * The fwd program is free software: you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License version 2
8  * as published by the Free Software Foundation.
9  *
10  * The fwd program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with the fwd program. If not, see http://www.gnu.org/licenses/.
17  */
18
19
20 #include "fwd.h"
21 #include "fwd_ipc.h"
22
23
24 int fwd_ipc_listen(void)
25 {
26         int fd;
27         struct sockaddr_un addr;
28
29         if( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0 )
30                 fwd_fatal("Cannot create AF_UNIX socket: %m");
31
32         memset(&addr, 0, sizeof(struct sockaddr_un));
33         strcpy(addr.sun_path, FWD_SOCKET_PATH);
34         addr.sun_family = AF_UNIX;
35
36         unlink(FWD_SOCKET_PATH);
37
38         if( bind(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0 )
39                 fwd_fatal("Cannot bind AF_UNIX socket: %m");
40
41         if( listen(fd, 1) < 0 )
42                 fwd_fatal("Cannot listen on AF_UNIX socket: %m");
43
44         //fcntl(fd, F_SETFL, O_NONBLOCK);
45
46         return fd;
47 }
48
49 int fwd_ipc_accept(int fd)
50 {
51         return accept(fd, NULL, NULL);
52 }
53
54 int fwd_ipc_connect(void)
55 {
56         int fd;
57         struct sockaddr_un addr;
58
59         if( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0 )
60                 fwd_fatal("Cannot create AF_UNIX socket: %m");
61
62         memset(&addr, 0, sizeof(struct sockaddr_un));
63         strcpy(addr.sun_path, FWD_SOCKET_PATH);
64         addr.sun_family = AF_UNIX;
65
66         if( connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0 )
67                 fwd_fatal("Cannot connect AF_UNIX socket: %m");
68
69         fcntl(fd, F_SETFL, O_NONBLOCK);
70
71         return fd;
72 }
73
74 int fwd_ipc_recvmsg(int fd, void *buf, int len)
75 {
76         return recv(fd, buf, len, 0);
77 }
78
79 int fwd_ipc_sendmsg(int fd, void *buf, int len)
80 {
81         return send(fd, buf, len, 0);
82 }
83
84 void fwd_ipc_shutdown(int fd)
85 {
86         shutdown(fd, SHUT_RDWR);
87         close(fd);
88 }
89
90 int fwd_ipc_sendtype(int fd, enum fwd_ipc_msgtype type)
91 {
92         struct fwd_ipc_msg msg;
93
94         memset(&msg, 0, sizeof(struct fwd_ipc_msg));
95         msg.type = type;
96
97         return fwd_ipc_sendmsg(fd, &msg, sizeof(struct fwd_ipc_msg));
98 }