version 1.21, 2020/01/25 17:17:31 |
version 1.22, 2020/03/22 08:59:22 |
|
|
int ch; |
int ch; |
size_t i; |
size_t i; |
|
|
if (pledge("stdio inet dns", NULL) == -1) |
if (pledge("stdio inet dns unix", NULL) == -1) |
err(1, "pledge"); |
err(1, "pledge"); |
|
|
if (argc <= 1) |
if (argc <= 1) |
|
|
hints.ai_socktype = SOCK_STREAM; |
hints.ai_socktype = SOCK_STREAM; |
} else if (strcasecmp(specifier, "unix") == 0) { |
} else if (strcasecmp(specifier, "unix") == 0) { |
hints.ai_family = AF_UNIX; |
hints.ai_family = AF_UNIX; |
hints.ai_socktype = SOCK_STREAM; |
|
hints.ai_addr = (struct sockaddr *)&saddr; |
hints.ai_addr = (struct sockaddr *)&saddr; |
hints.ai_addrlen = sizeof(saddr); |
hints.ai_addrlen = sizeof(saddr); |
saddr.sun_len = sizeof(saddr); |
saddr.sun_len = sizeof(saddr); |
|
|
errx(1, "Hostname path too long"); |
errx(1, "Hostname path too long"); |
ai = &hints; |
ai = &hints; |
} else { |
} else { |
port = hostname; |
*--hostname = ':'; |
hostname = specifier; |
hostname = specifier; |
specifier = NULL; |
|
hints.ai_family = AF_INET; |
|
hints.ai_socktype = SOCK_DGRAM; |
|
} |
} |
if (port == NULL) { |
|
if (hints.ai_family == AF_INET) { |
|
if ((port = strchr(hostname, ':')) != NULL) |
|
*port++ = '\0'; |
|
} else if (hints.ai_family == AF_INET6) { |
|
if (hostname[0] == '[') { |
|
hostname++; |
|
if ((port = strchr(hostname, ']')) == NULL) |
|
errx(1, "invalid agent"); |
|
*port++ = '\0'; |
|
if (port[0] == ':') |
|
*port++ = '\0'; |
|
else |
|
port = NULL; |
|
} else { |
|
if ((port = strrchr(hostname, ':')) == NULL) |
|
errx(1, "invalid agent"); |
|
*port++ = '\0'; |
|
} |
|
} |
|
} |
|
} else { |
} else { |
hostname = specifier; |
hostname = specifier; |
hints.ai_family = AF_INET; |
|
hints.ai_socktype = SOCK_DGRAM; |
|
} |
} |
|
|
|
if (hints.ai_family == AF_INET) { |
|
if ((port = strchr(hostname, ':')) != NULL) |
|
*port++ = '\0'; |
|
} else if (hints.ai_family == AF_INET6 || hints.ai_family == 0) { |
|
if (hostname[0] == '[') { |
|
hints.ai_family = AF_INET6; |
|
hostname++; |
|
if ((port = strchr(hostname, ']')) == NULL) |
|
errx(1, "invalid agent"); |
|
*port++ = '\0'; |
|
if (port[0] == ':') |
|
*port++ = '\0'; |
|
else if (port[0] == '\0') |
|
port = NULL; |
|
else |
|
errx(1, "invalid agent"); |
|
} else { |
|
if ((port = strrchr(hostname, ':')) != NULL) |
|
*port++ = '\0'; |
|
} |
|
} |
|
|
if (hints.ai_family != AF_UNIX) { |
if (hints.ai_family != AF_UNIX) { |
|
if (hints.ai_socktype == 0) |
|
hints.ai_socktype = SOCK_DGRAM; |
if (port == NULL) |
if (port == NULL) |
port = defaultport; |
port = defaultport; |
error = getaddrinfo(hostname, port, &hints, &ai0); |
error = getaddrinfo(hostname, port, &hints, &ai0); |
if (error) |
if (error) { |
errx(1, "%s", gai_strerror(error)); |
if (error != EAI_NODATA && port != defaultport) |
|
errx(1, "%s", gai_strerror(error)); |
|
*--port = ':'; |
|
error = getaddrinfo(hostname, defaultport, &hints, |
|
&ai0); |
|
if (error) |
|
errx(1, "%s", gai_strerror(error)); |
|
} |
s = -1; |
s = -1; |
for (ai = ai0; ai != NULL; ai = ai->ai_next) { |
for (ai = ai0; ai != NULL; ai = ai->ai_next) { |
if ((s = socket(ai->ai_family, ai->ai_socktype, |
if ((s = socket(ai->ai_family, ai->ai_socktype, |
ai->ai_protocol)) == -1) |
ai->ai_protocol)) != -1 && |
continue; |
connect(s, (struct sockaddr *)ai->ai_addr, |
break; |
ai->ai_addrlen) != -1) |
|
break; |
} |
} |
} else |
} else { |
s = socket(hints.ai_family, hints.ai_socktype, |
s = socket(AF_UNIX, SOCK_STREAM, 0); |
hints.ai_protocol); |
if (connect(s, (struct sockaddr *)ai->ai_addr, |
|
ai->ai_addrlen) == -1) |
|
err(1, "Can't connect to %s", agent); |
|
} |
if (s == -1) |
if (s == -1) |
err(1, "socket"); |
err(1, "Can't connect to agent %s", agent); |
|
|
if (connect(s, (struct sockaddr *)ai->ai_addr, ai->ai_addrlen) == -1) |
|
err(1, "Can't connect to %s", agent); |
|
|
|
if (ai0 != NULL) |
if (ai0 != NULL) |
freeaddrinfo(ai0); |
freeaddrinfo(ai0); |