static int tcp_disconnect(mrp_transport_t *mt);
static int open_socket(tcp_t *t, int family);
-static socklen_t tcp_resolve(char *str, void *addr, socklen_t size)
+static socklen_t tcp_resolve(const char *str, void *addr, socklen_t size)
{
struct addrinfo *ai, hints;
char node[512], *port;
const char *type; /* transport type */
size_t size; /* full transport struct size */
mrp_transport_req_t req; /* transport requests */
- socklen_t (*resolve)(char *str, void *addr, socklen_t size);
+ socklen_t (*resolve)(const char *str, void *addr, socklen_t size);
mrp_list_hook_t hook; /* to list of registered transports */
} mrp_transport_descr_t;
void *user_data);
/** Resolve an address string to a transport-specific address. */
-socklen_t mrp_transport_resolve(mrp_transport_t *t, char *str,
+socklen_t mrp_transport_resolve(mrp_transport_t *t, const char *str,
void *addr, socklen_t size);
/** Bind a given transport to a transport-specific address. */
static int console_listen(const char *address)
{
- struct addrinfo *ai;
- char node[512], *port;
- int sock, error, on;
+ struct sockaddr addr;
+ socklen_t addrlen;
+ int sock, on;
- strncpy(node, address, sizeof(node) - 1);
- node[sizeof(node) - 1] = '\0';
+ addrlen = mrp_transport_resolve(NULL, address, &addr, sizeof(addr));
- if ((port = strrchr(node, ':')) == NULL) {
+ if (!addrlen) {
console_error("invalid console address '%s'.", address);
return FALSE;
}
- *port++ = '\0';
-
- if ((error = getaddrinfo(node, port, NULL, &ai)) != 0) {
- console_error("invalid console address '%s' (%s).",
- address, gai_strerror(error));
- return FALSE;
- }
- if ((sock = socket(ai->ai_family, SOCK_STREAM, 0)) < 0) {
+ if ((sock = socket(addr.sa_family, SOCK_STREAM, 0)) < 0) {
console_error("failed to create console socket (%d: %s).",
errno, strerror(errno));
goto fail;
on = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
- if (bind(sock, ai->ai_addr, ai->ai_addrlen) != 0) {
+ if (bind(sock, &addr, addrlen) != 0) {
console_error("failed to bind to address '%s' (%d: %s).",
address, errno, strerror(errno));
goto fail;
goto fail;
}
- freeaddrinfo(ai);
return sock;
fail:
if (sock >= 0)
close(sock);
-
- if (ai != NULL)
- freeaddrinfo(ai);
-
+
return FALSE;
}
static mrp_plugin_arg_t console_args[] = {
- MRP_PLUGIN_ARGIDX(ARG_ADDRESS, STRING, "address", "127.0.0.1:3000"),
+ MRP_PLUGIN_ARGIDX(ARG_ADDRESS, STRING, "address", "tcp:127.0.0.1:3000"),
};
MURPHY_REGISTER_CORE_PLUGIN("console",