From 3ae54e0eed4d39eb75824a0f879965574b6fb53c Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sat, 20 May 2017 14:28:18 +0200 Subject: [PATCH] Move send_udp_packet to libretro-common --- command.c | 56 +----------------------- libretro-common/include/net/net_compat.h | 2 + libretro-common/net/net_compat.c | 53 ++++++++++++++++++++++ 3 files changed, 56 insertions(+), 55 deletions(-) diff --git a/command.c b/command.c index da186dbe08..b8d134b639 100644 --- a/command.c +++ b/command.c @@ -424,60 +424,6 @@ error: return false; } - - -static bool send_udp_packet(const char *host, - uint16_t port, const char *msg) -{ - char port_buf[16] = {0}; - struct addrinfo hints = {0}; - struct addrinfo *res = NULL; - const struct addrinfo *tmp = NULL; - int fd = -1; - bool ret = true; - - hints.ai_socktype = SOCK_DGRAM; - - snprintf(port_buf, sizeof(port_buf), "%hu", (unsigned short)port); - - if (getaddrinfo_retro(host, port_buf, &hints, &res) < 0) - return false; - - /* Send to all possible targets. - * "localhost" might resolve to several different IPs. */ - tmp = (const struct addrinfo*)res; - while (tmp) - { - ssize_t len, ret_len; - - fd = socket(tmp->ai_family, tmp->ai_socktype, tmp->ai_protocol); - if (fd < 0) - { - ret = false; - goto end; - } - - len = strlen(msg); - ret_len = sendto(fd, msg, len, 0, tmp->ai_addr, tmp->ai_addrlen); - - if (ret_len < len) - { - ret = false; - goto end; - } - - socket_close(fd); - fd = -1; - tmp = tmp->ai_next; - } - -end: - freeaddrinfo_retro(res); - if (fd >= 0) - socket_close(fd); - return ret; -} - static bool command_verify(const char *cmd) { unsigned i; @@ -536,7 +482,7 @@ bool command_network_send(const char *cmd_) msg_hash_to_str(MSG_SENDING_COMMAND), cmd, host, (unsigned short)port); - ret = command_verify(cmd) && send_udp_packet(host, port, cmd); + ret = command_verify(cmd) && udp_send_packet(host, port, cmd); } free(command); diff --git a/libretro-common/include/net/net_compat.h b/libretro-common/include/net/net_compat.h index dd7a1d5c81..60173882eb 100644 --- a/libretro-common/include/net/net_compat.h +++ b/libretro-common/include/net/net_compat.h @@ -240,4 +240,6 @@ void network_deinit(void); const char *inet_ntop_compat(int af, const void *src, char *dst, socklen_t cnt); +bool udp_send_packet(const char *host, uint16_t port, const char *msg); + #endif diff --git a/libretro-common/net/net_compat.c b/libretro-common/net/net_compat.c index 938cb51274..433182fdd1 100644 --- a/libretro-common/net/net_compat.c +++ b/libretro-common/net/net_compat.c @@ -20,6 +20,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include #include #include #include @@ -391,3 +392,55 @@ const char *inet_ntop_compat(int af, const void *src, char *dst, socklen_t cnt) return NULL; #endif } + +bool udp_send_packet(const char *host, + uint16_t port, const char *msg) +{ + char port_buf[16] = {0}; + struct addrinfo hints = {0}; + struct addrinfo *res = NULL; + const struct addrinfo *tmp = NULL; + int fd = -1; + bool ret = true; + + hints.ai_socktype = SOCK_DGRAM; + + snprintf(port_buf, sizeof(port_buf), "%hu", (unsigned short)port); + + if (getaddrinfo_retro(host, port_buf, &hints, &res) < 0) + return false; + + /* Send to all possible targets. + * "localhost" might resolve to several different IPs. */ + tmp = (const struct addrinfo*)res; + while (tmp) + { + ssize_t len, ret_len; + + fd = socket(tmp->ai_family, tmp->ai_socktype, tmp->ai_protocol); + if (fd < 0) + { + ret = false; + goto end; + } + + len = strlen(msg); + ret_len = sendto(fd, msg, len, 0, tmp->ai_addr, tmp->ai_addrlen); + + if (ret_len < len) + { + ret = false; + goto end; + } + + socket_close(fd); + fd = -1; + tmp = tmp->ai_next; + } + +end: + freeaddrinfo_retro(res); + if (fd >= 0) + socket_close(fd); + return ret; +}