From 004d9a613c395a0b510aafc4e5270e3a82b6c6b5 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sun, 1 May 2016 22:57:44 +0200 Subject: [PATCH] Create socket_bind --- command.c | 5 +---- libretro-common/include/net/net_socket.h | 2 ++ libretro-common/net/net_socket.c | 11 +++++++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/command.c b/command.c index 23ececc44e..083ac124b0 100644 --- a/command.c +++ b/command.c @@ -124,7 +124,6 @@ static const struct cmd_map map[] = { static bool cmd_init_network(rarch_cmd_t *handle, uint16_t port) { struct addrinfo *res = NULL; - int yes = 1; RARCH_LOG("Bringing up command interface on port %hu.\n", (unsigned short)port); @@ -137,9 +136,7 @@ static bool cmd_init_network(rarch_cmd_t *handle, uint16_t port) if (!socket_nonblock(handle->net_fd)) goto error; - setsockopt(handle->net_fd, SOL_SOCKET, - SO_REUSEADDR, (const char*)&yes, sizeof(int)); - if (bind(handle->net_fd, res->ai_addr, res->ai_addrlen) < 0) + if (!socket_bind(handle->net_fd, res)) { RARCH_ERR("Failed to bind socket.\n"); goto error; diff --git a/libretro-common/include/net/net_socket.h b/libretro-common/include/net/net_socket.h index b58fd0c4dc..d5929ff0ea 100644 --- a/libretro-common/include/net/net_socket.h +++ b/libretro-common/include/net/net_socket.h @@ -50,6 +50,8 @@ int socket_send_all_blocking(int fd, const void *data_, size_t size); int socket_receive_all_blocking(int fd, void *data_, size_t size); +bool socket_bind(int fd, void *data); + RETRO_END_DECLS #endif diff --git a/libretro-common/net/net_socket.c b/libretro-common/net/net_socket.c index 5fd0ff67b9..e236d915c6 100644 --- a/libretro-common/net/net_socket.c +++ b/libretro-common/net/net_socket.c @@ -154,3 +154,14 @@ int socket_send_all_blocking(int fd, const void *data_, size_t size) return true; } + +bool socket_bind(int fd, void *data) +{ + int yes = 1; + struct addrinfo *res = (struct addrinfo*)data; + setsockopt(fd, SOL_SOCKET, + SO_REUSEADDR, (const char*)&yes, sizeof(int)); + if (bind(fd, res->ai_addr, res->ai_addrlen) < 0) + return false; + return true; +}