diff --git a/src/addr_util.cpp b/src/addr_util.cpp index 411ae4bc..933eda73 100644 --- a/src/addr_util.cpp +++ b/src/addr_util.cpp @@ -1,3 +1,5 @@ +#include +#include #include #include #include @@ -186,3 +188,51 @@ std::vector getifaddr_list(std::vector mask_cfg, bool freeifaddrs(list); return addresses; } + +int create_and_bind_socket(std::string bind_address, int bind_port, int listen_backlog, int *listening_port) +{ + sockaddr addr; + if (!string_to_addr(bind_address, 0, bind_port, &addr)) + { + throw std::runtime_error("bind address "+bind_address+" is not valid"); + } + + int listen_fd = socket(addr.sa_family, SOCK_STREAM, 0); + if (listen_fd < 0) + { + throw std::runtime_error(std::string("socket: ") + strerror(errno)); + } + int enable = 1; + setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)); + + if (bind(listen_fd, &addr, sizeof(addr)) < 0) + { + close(listen_fd); + throw std::runtime_error(std::string("bind: ") + strerror(errno)); + } + if (listening_port) + { + if (bind_port == 0) + { + socklen_t len = sizeof(addr); + if (getsockname(listen_fd, (sockaddr *)&addr, &len) == -1) + { + close(listen_fd); + throw std::runtime_error(std::string("getsockname: ") + strerror(errno)); + } + *listening_port = ntohs(((sockaddr_in*)&addr)->sin_port); + } + else + { + *listening_port = bind_port; + } + } + + if (listen(listen_fd, listen_backlog ? listen_backlog : 128) < 0) + { + close(listen_fd); + throw std::runtime_error(std::string("listen: ") + strerror(errno)); + } + + return listen_fd; +} diff --git a/src/addr_util.h b/src/addr_util.h index b8f7da75..83775515 100644 --- a/src/addr_util.h +++ b/src/addr_util.h @@ -7,3 +7,4 @@ bool string_to_addr(std::string str, bool parse_port, int default_port, struct sockaddr *addr); std::string addr_to_string(const sockaddr &addr); std::vector getifaddr_list(std::vector mask_cfg = std::vector(), bool include_v6 = false); +int create_and_bind_socket(std::string bind_address, int bind_port, int listen_backlog, int *listening_port); diff --git a/src/osd.cpp b/src/osd.cpp index 25dd2d33..67a4c5a3 100644 --- a/src/osd.cpp +++ b/src/osd.cpp @@ -200,46 +200,7 @@ void osd_t::bind_socket() // FIXME Support multiple listening sockets - sockaddr addr; - if (!string_to_addr(bind_address, 0, bind_port, &addr)) - { - throw std::runtime_error("bind address "+bind_address+" is not valid"); - } - - listen_fd = socket(addr.sa_family, SOCK_STREAM, 0); - if (listen_fd < 0) - { - throw std::runtime_error(std::string("socket: ") + strerror(errno)); - } - int enable = 1; - setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)); - - if (bind(listen_fd, &addr, sizeof(addr)) < 0) - { - close(listen_fd); - throw std::runtime_error(std::string("bind: ") + strerror(errno)); - } - if (bind_port == 0) - { - socklen_t len = sizeof(addr); - if (getsockname(listen_fd, (sockaddr *)&addr, &len) == -1) - { - close(listen_fd); - throw std::runtime_error(std::string("getsockname: ") + strerror(errno)); - } - listening_port = ntohs(((sockaddr_in*)&addr)->sin_port); - } - else - { - listening_port = bind_port; - } - - if (listen(listen_fd, listen_backlog) < 0) - { - close(listen_fd); - throw std::runtime_error(std::string("listen: ") + strerror(errno)); - } - + listen_fd = create_and_bind_socket(bind_address, bind_port, listen_backlog, &listening_port); fcntl(listen_fd, F_SETFL, fcntl(listen_fd, F_GETFL, 0) | O_NONBLOCK); epmgr->set_fd_handler(listen_fd, false, [this](int fd, int events) diff --git a/src/stub_osd.cpp b/src/stub_osd.cpp index 4e2fa5c2..cf61fa14 100644 --- a/src/stub_osd.cpp +++ b/src/stub_osd.cpp @@ -41,13 +41,11 @@ #include "rw_blocking.h" #include "osd_ops.h" -int bind_stub(std::string bind_address, int bind_port); - void run_stub(int peer_fd); int main(int narg, char *args[]) { - int listen_fd = bind_stub("0.0.0.0", 11203); + int listen_fd = create_and_bind_socket("0.0.0.0", 11203, 128, NULL); // Accept new connections sockaddr addr; socklen_t peer_addr_size = sizeof(addr); @@ -76,39 +74,6 @@ int main(int narg, char *args[]) return 0; } -int bind_stub(std::string bind_address, int bind_port) -{ - int listen_backlog = 128; - - sockaddr addr; - if (!string_to_addr(bind_address, 0, bind_port, &addr)) - { - throw std::runtime_error("bind address "+bind_address+" is not valid"); - } - - int listen_fd = socket(addr.sa_family, SOCK_STREAM, 0); - if (listen_fd < 0) - { - throw std::runtime_error(std::string("socket: ") + strerror(errno)); - } - int enable = 1; - setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)); - - if (bind(listen_fd, &addr, sizeof(addr)) < 0) - { - close(listen_fd); - throw std::runtime_error(std::string("bind: ") + strerror(errno)); - } - - if (listen(listen_fd, listen_backlog) < 0) - { - close(listen_fd); - throw std::runtime_error(std::string("listen: ") + strerror(errno)); - } - - return listen_fd; -} - void run_stub(int peer_fd) { osd_any_op_t op; diff --git a/src/stub_uring_osd.cpp b/src/stub_uring_osd.cpp index f9a8d4b9..95c43178 100644 --- a/src/stub_uring_osd.cpp +++ b/src/stub_uring_osd.cpp @@ -25,8 +25,6 @@ #include "epoll_manager.h" #include "messenger.h" -int bind_stub(std::string bind_address, int bind_port); - void stub_exec_op(osd_messenger_t *msgr, osd_op_t *op); int main(int narg, char *args[]) @@ -43,7 +41,8 @@ int main(int narg, char *args[]) json11::Json config = json11::Json::object { { "log_level", 1 } }; msgr->parse_config(config); // Accept new connections - int listen_fd = bind_stub("0.0.0.0", 11203); + int listen_fd = create_and_bind_socket("0.0.0.0", 11203, 128, NULL); + fcntl(listen_fd, F_SETFL, fcntl(listen_fd, F_GETFL, 0) | O_NONBLOCK); epmgr->set_fd_handler(listen_fd, false, [listen_fd, msgr](int fd, int events) { msgr->accept_connections(listen_fd); @@ -67,41 +66,6 @@ int main(int narg, char *args[]) return 0; } -int bind_stub(std::string bind_address, int bind_port) -{ - int listen_backlog = 128; - - sockaddr addr; - if (!string_to_addr(bind_address, 0, bind_port, &addr)) - { - throw std::runtime_error("bind address "+bind_address+" is not valid"); - } - - int listen_fd = socket(addr.sa_family, SOCK_STREAM, 0); - if (listen_fd < 0) - { - throw std::runtime_error(std::string("socket: ") + strerror(errno)); - } - int enable = 1; - setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)); - - if (bind(listen_fd, &addr, sizeof(addr)) < 0) - { - close(listen_fd); - throw std::runtime_error(std::string("bind: ") + strerror(errno)); - } - - if (listen(listen_fd, listen_backlog) < 0) - { - close(listen_fd); - throw std::runtime_error(std::string("listen: ") + strerror(errno)); - } - - fcntl(listen_fd, F_SETFL, fcntl(listen_fd, F_GETFL, 0) | O_NONBLOCK); - - return listen_fd; -} - void stub_exec_op(osd_messenger_t *msgr, osd_op_t *op) { op->reply.hdr.magic = SECONDARY_OSD_REPLY_MAGIC;