diff options
author | Jo-Philipp Wich <jow@openwrt.org> | 2009-12-20 02:31:01 +0000 |
---|---|---|
committer | Jo-Philipp Wich <jow@openwrt.org> | 2009-12-20 02:31:01 +0000 |
commit | f2b04c54005072d1b94a4012ebedba8c32a053e3 (patch) | |
tree | e807973560222dea0810bac02dc0eec92a1c9c21 | |
parent | 1c2d85856bfcc6766073ac7466ead76b225e8ccf (diff) |
contrib/fwd: initial IPC work
-rw-r--r-- | contrib/fwd/src/fwd_ipc.c | 98 | ||||
-rw-r--r-- | contrib/fwd/src/fwd_ipc.h | 61 |
2 files changed, 159 insertions, 0 deletions
diff --git a/contrib/fwd/src/fwd_ipc.c b/contrib/fwd/src/fwd_ipc.c new file mode 100644 index 0000000000..83eb7d8869 --- /dev/null +++ b/contrib/fwd/src/fwd_ipc.c @@ -0,0 +1,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)); +} diff --git a/contrib/fwd/src/fwd_ipc.h b/contrib/fwd/src/fwd_ipc.h new file mode 100644 index 0000000000..4a4c9ecc77 --- /dev/null +++ b/contrib/fwd/src/fwd_ipc.h @@ -0,0 +1,61 @@ +/* + * fwd - OpenWrt firewall daemon - unix domain socket headers + * + * 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/. + */ + +#ifndef __FWD_IPC_H__ +#define __FWD_IPC_H__ + +#include <unistd.h> +#include <fcntl.h> +#include <errno.h> + +#include <sys/socket.h> +#include <sys/un.h> + +#define FWD_SOCKET_PATH "/var/run/fwd.sock" + + +enum fwd_ipc_msgtype { + FWD_IPC_OK = 0, + FWD_IPC_ERROR = 1, + FWD_IPC_FLUSH = 2, + FWD_IPC_BUILD = 3, + FWD_IPC_RELOAD = 4, + FWD_IPC_ADDIF = 5, + FWD_IPC_DELIF = 6 +}; + +struct fwd_ipc_msg { + enum fwd_ipc_msgtype type; + union { + char network[256]; + } data; +}; + +int fwd_ipc_listen(void); +int fwd_ipc_accept(int); + +int fwd_ipc_connect(void); + +int fwd_ipc_recvmsg(int, void *, int); +int fwd_ipc_sendmsg(int, void *, int); + +void fwd_ipc_shutdown(int); + +int fwd_ipc_sendtype(int, enum fwd_ipc_msgtype); + +#endif |