2 * fwd - OpenWrt firewall daemon - unix domain socket parts
4 * Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
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.
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.
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/.
24 int fwd_ipc_listen(void)
27 struct sockaddr_un addr
;
29 if( (fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0 )
30 fwd_fatal("Cannot create AF_UNIX socket: %m");
32 memset(&addr
, 0, sizeof(struct sockaddr_un
));
33 strcpy(addr
.sun_path
, FWD_SOCKET_PATH
);
34 addr
.sun_family
= AF_UNIX
;
36 unlink(FWD_SOCKET_PATH
);
38 if( bind(fd
, (struct sockaddr
*)&addr
, sizeof(struct sockaddr_un
)) < 0 )
39 fwd_fatal("Cannot bind AF_UNIX socket: %m");
41 if( listen(fd
, 1) < 0 )
42 fwd_fatal("Cannot listen on AF_UNIX socket: %m");
44 //fcntl(fd, F_SETFL, O_NONBLOCK);
49 int fwd_ipc_accept(int fd
)
51 return accept(fd
, NULL
, NULL
);
54 int fwd_ipc_connect(void)
57 struct sockaddr_un addr
;
59 if( (fd
= socket(AF_UNIX
, SOCK_STREAM
, 0)) < 0 )
60 fwd_fatal("Cannot create AF_UNIX socket: %m");
62 memset(&addr
, 0, sizeof(struct sockaddr_un
));
63 strcpy(addr
.sun_path
, FWD_SOCKET_PATH
);
64 addr
.sun_family
= AF_UNIX
;
66 if( connect(fd
, (struct sockaddr
*)&addr
, sizeof(struct sockaddr_un
)) < 0 )
67 fwd_fatal("Cannot connect AF_UNIX socket: %m");
69 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
74 int fwd_ipc_recvmsg(int fd
, void *buf
, int len
)
76 return recv(fd
, buf
, len
, 0);
79 int fwd_ipc_sendmsg(int fd
, void *buf
, int len
)
81 return send(fd
, buf
, len
, 0);
84 void fwd_ipc_shutdown(int fd
)
86 shutdown(fd
, SHUT_RDWR
);
90 int fwd_ipc_sendtype(int fd
, enum fwd_ipc_msgtype type
)
92 struct fwd_ipc_msg msg
;
94 memset(&msg
, 0, sizeof(struct fwd_ipc_msg
));
97 return fwd_ipc_sendmsg(fd
, &msg
, sizeof(struct fwd_ipc_msg
));