static bool init_socket(netplay_t *handle, const char *server, uint16_t port) { if (!netplay_init_network()) return false; if (!init_tcp_socket(handle, server, port, handle->spectate)) return false; if (!handle->spectate && !init_udp_socket(handle, server, port)) return false; return true; }
static bool cmd_init_network(rarch_cmd_t *handle, uint16_t port) { char port_buf[16]; struct addrinfo hints, *res = NULL; int yes = 1; if (!netplay_init_network()) return false; RARCH_LOG("Bringing up command interface on port %hu.\n", (unsigned short)port); memset(&hints, 0, sizeof(hints)); #if defined(_WIN32) || defined(HAVE_SOCKET_LEGACY) hints.ai_family = AF_INET; #else hints.ai_family = AF_UNSPEC; #endif hints.ai_socktype = SOCK_DGRAM; hints.ai_flags = AI_PASSIVE; snprintf(port_buf, sizeof(port_buf), "%hu", (unsigned short)port); if (getaddrinfo(NULL, port_buf, &hints, &res) < 0) goto error; handle->net_fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if (handle->net_fd < 0) goto error; if (!socket_nonblock(handle->net_fd)) goto error; setsockopt(handle->net_fd, SOL_SOCKET, SO_REUSEADDR, CONST_CAST &yes, sizeof(int)); if (bind(handle->net_fd, res->ai_addr, res->ai_addrlen) < 0) { RARCH_ERR("Failed to bind socket.\n"); goto error; } freeaddrinfo(res); return true; error: if (res) freeaddrinfo(res); return false; }
bool network_cmd_send(const char *cmd_) { char *command, *save; bool ret; const char *cmd = NULL; const char *host = NULL; const char *port_ = NULL; bool old_verbose = g_extern.verbosity; uint16_t port = DEFAULT_NETWORK_CMD_PORT; if (!netplay_init_network()) return false; if (!(command = strdup(cmd_))) return false; g_extern.verbosity = true; cmd = strtok_r(command, ";", &save); if (cmd) host = strtok_r(NULL, ";", &save); if (host) port_ = strtok_r(NULL, ";", &save); if (!host) { #ifdef _WIN32 host = "127.0.0.1"; #else host = "localhost"; #endif } if (port_) port = strtoul(port_, NULL, 0); RARCH_LOG("Sending command: \"%s\" to %s:%hu\n", cmd, host, (unsigned short)port); ret = verify_command(cmd) && send_udp_packet(host, port, cmd); free(command); g_extern.verbosity = old_verbose; return ret; }