Create socket_bind

This commit is contained in:
twinaphex 2016-05-01 22:57:44 +02:00
parent 78bb85e2f3
commit 004d9a613c
3 changed files with 14 additions and 4 deletions

View File

@ -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;

View File

@ -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

View File

@ -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;
}