Add IPv6 support to pppctl by using getaddrinfo() and trying each address
it returns. This allows it to connect to the server side again, which has been listening on IPv6 addresses exclusively for more than 2 years. PR: 59369
This commit is contained in:
parent
172293effe
commit
8b45548804
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=123229
@ -332,6 +332,29 @@ Monitor(void *v)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const char *
|
||||||
|
sockaddr_ntop(const struct sockaddr *sa)
|
||||||
|
{
|
||||||
|
const void *addr;
|
||||||
|
static char addrbuf[INET6_ADDRSTRLEN];
|
||||||
|
|
||||||
|
switch (sa->sa_family) {
|
||||||
|
case AF_INET:
|
||||||
|
addr = &((const struct sockaddr_in *)sa)->sin_addr;
|
||||||
|
break;
|
||||||
|
case AF_UNIX:
|
||||||
|
addr = &((const struct sockaddr_un *)sa)->sun_path;
|
||||||
|
break;
|
||||||
|
case AF_INET6:
|
||||||
|
addr = &((const struct sockaddr_in6 *)sa)->sin6_addr;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
inet_ntop(sa->sa_family, addr, addrbuf, sizeof(addrbuf));
|
||||||
|
return addrbuf;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Connect to ppp using either a local domain socket or a tcp socket.
|
* Connect to ppp using either a local domain socket or a tcp socket.
|
||||||
*
|
*
|
||||||
@ -341,12 +364,8 @@ Monitor(void *v)
|
|||||||
int
|
int
|
||||||
main(int argc, char **argv)
|
main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
struct servent *s;
|
|
||||||
struct hostent *h;
|
|
||||||
struct sockaddr *sock;
|
|
||||||
struct sockaddr_in ifsin;
|
|
||||||
struct sockaddr_un ifsun;
|
struct sockaddr_un ifsun;
|
||||||
int n, socksz, arg, fd, len, verbose, save_errno, hide1, hide1off, hide2;
|
int n, arg, fd, len, verbose, save_errno, hide1, hide1off, hide2;
|
||||||
unsigned TimeoutVal;
|
unsigned TimeoutVal;
|
||||||
char *DoneWord = "x", *next, *start;
|
char *DoneWord = "x", *next, *start;
|
||||||
struct sigaction act, oact;
|
struct sigaction act, oact;
|
||||||
@ -424,9 +443,6 @@ main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (*argv[arg] == '/') {
|
if (*argv[arg] == '/') {
|
||||||
sock = (struct sockaddr *)&ifsun;
|
|
||||||
socksz = sizeof ifsun;
|
|
||||||
|
|
||||||
memset(&ifsun, '\0', sizeof ifsun);
|
memset(&ifsun, '\0', sizeof ifsun);
|
||||||
ifsun.sun_len = strlen(argv[arg]);
|
ifsun.sun_len = strlen(argv[arg]);
|
||||||
if (ifsun.sun_len > sizeof ifsun.sun_path - 1) {
|
if (ifsun.sun_len > sizeof ifsun.sun_path - 1) {
|
||||||
@ -440,88 +456,98 @@ main(int argc, char **argv)
|
|||||||
warnx("cannot create local domain socket");
|
warnx("cannot create local domain socket");
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
if (connect(fd, (struct sockaddr *)&ifsun, sizeof(ifsun)) < 0) {
|
||||||
|
if (errno)
|
||||||
|
warn("cannot connect to socket %s", argv[arg]);
|
||||||
|
else
|
||||||
|
warnx("cannot connect to socket %s", argv[arg]);
|
||||||
|
close(fd);
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
char *port, *host, *colon;
|
char *addr, *p, *port;
|
||||||
int hlen;
|
const char *caddr;
|
||||||
|
struct addrinfo hints, *res, *pai;
|
||||||
|
int gai;
|
||||||
|
char local[] = "localhost";
|
||||||
|
|
||||||
colon = strchr(argv[arg], ':');
|
addr = argv[arg];
|
||||||
if (colon) {
|
if (addr[strspn(addr, "0123456789")] == '\0') {
|
||||||
port = colon + 1;
|
/* port on local machine */
|
||||||
*colon = '\0';
|
port = addr;
|
||||||
host = argv[arg];
|
addr = local;
|
||||||
} else {
|
} else if (*addr == '[') {
|
||||||
port = argv[arg];
|
/* [addr]:port */
|
||||||
host = "127.0.0.1";
|
if ((p = strchr(addr, ']')) == NULL) {
|
||||||
}
|
warnx("%s: mismatched '['", addr);
|
||||||
sock = (struct sockaddr *)&ifsin;
|
return 1;
|
||||||
socksz = sizeof ifsin;
|
}
|
||||||
hlen = strlen(host);
|
addr++;
|
||||||
|
*p++ = '\0';
|
||||||
memset(&ifsin, '\0', sizeof ifsin);
|
if (*p != ':') {
|
||||||
if (strspn(host, "0123456789.") == hlen) {
|
warnx("%s: missing port", addr);
|
||||||
if (!inet_aton(host, &ifsin.sin_addr)) {
|
return 1;
|
||||||
warnx("cannot translate %s", host);
|
}
|
||||||
return 1;
|
port = ++p;
|
||||||
}
|
} else {
|
||||||
} else if ((h = gethostbyname(host)) == 0) {
|
/* addr:port */
|
||||||
warnx("cannot resolve %s", host);
|
p = addr + strcspn(addr, ":");
|
||||||
return 1;
|
if (*p != ':') {
|
||||||
}
|
warnx("%s: missing port", addr);
|
||||||
else
|
return 1;
|
||||||
ifsin.sin_addr.s_addr = *(u_long *)h->h_addr_list[0];
|
}
|
||||||
|
*p++ = '\0';
|
||||||
if (colon)
|
port = p;
|
||||||
*colon = ':';
|
}
|
||||||
|
memset(&hints, 0, sizeof(hints));
|
||||||
if (strspn(port, "0123456789") == strlen(port))
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
ifsin.sin_port = htons(atoi(port));
|
gai = getaddrinfo(addr, port, &hints, &res);
|
||||||
else if (s = getservbyname(port, "tcp"), !s) {
|
if (gai != 0) {
|
||||||
warnx("%s isn't a valid port or service!", port);
|
warnx("%s: %s", addr, gai_strerror(gai));
|
||||||
usage();
|
return 1;
|
||||||
}
|
}
|
||||||
else
|
for (pai = res; pai != NULL; pai = pai->ai_next) {
|
||||||
ifsin.sin_port = s->s_port;
|
if (fd = socket(pai->ai_family, pai->ai_socktype,
|
||||||
|
pai->ai_protocol), fd < 0) {
|
||||||
ifsin.sin_len = sizeof(ifsin);
|
warnx("cannot create socket");
|
||||||
ifsin.sin_family = AF_INET;
|
continue;
|
||||||
|
}
|
||||||
if (fd = socket(AF_INET, SOCK_STREAM, 0), fd < 0) {
|
TimedOut = 0;
|
||||||
warnx("cannot create internet socket");
|
if (TimeoutVal) {
|
||||||
return 2;
|
act.sa_handler = Timeout;
|
||||||
}
|
sigemptyset(&act.sa_mask);
|
||||||
}
|
act.sa_flags = 0;
|
||||||
|
sigaction(SIGALRM, &act, &oact);
|
||||||
TimedOut = 0;
|
alarm(TimeoutVal);
|
||||||
if (TimeoutVal) {
|
}
|
||||||
act.sa_handler = Timeout;
|
if (connect(fd, pai->ai_addr, pai->ai_addrlen) == 0)
|
||||||
sigemptyset(&act.sa_mask);
|
break;
|
||||||
act.sa_flags = 0;
|
if (TimeoutVal) {
|
||||||
sigaction(SIGALRM, &act, &oact);
|
save_errno = errno;
|
||||||
alarm(TimeoutVal);
|
alarm(0);
|
||||||
}
|
sigaction(SIGALRM, &oact, 0);
|
||||||
|
errno = save_errno;
|
||||||
if (connect(fd, sock, socksz) < 0) {
|
}
|
||||||
if (TimeoutVal) {
|
caddr = sockaddr_ntop(pai->ai_addr);
|
||||||
save_errno = errno;
|
if (caddr == NULL)
|
||||||
alarm(0);
|
caddr = argv[arg];
|
||||||
sigaction(SIGALRM, &oact, 0);
|
if (TimedOut)
|
||||||
errno = save_errno;
|
warnx("timeout: cannot connect to %s", caddr);
|
||||||
}
|
else {
|
||||||
if (TimedOut)
|
if (errno)
|
||||||
warnx("timeout: cannot connect to socket %s", argv[arg]);
|
warn("cannot connect to %s", caddr);
|
||||||
else {
|
else
|
||||||
if (errno)
|
warnx("cannot connect to %s", caddr);
|
||||||
warn("cannot connect to socket %s", argv[arg]);
|
}
|
||||||
else
|
close(fd);
|
||||||
warnx("cannot connect to socket %s", argv[arg]);
|
}
|
||||||
}
|
freeaddrinfo(res);
|
||||||
close(fd);
|
if (pai == NULL)
|
||||||
return 3;
|
return 1;
|
||||||
}
|
if (TimeoutVal) {
|
||||||
|
alarm(0);
|
||||||
if (TimeoutVal) {
|
sigaction(SIGALRM, &oact, 0);
|
||||||
alarm(0);
|
}
|
||||||
sigaction(SIGALRM, &oact, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
len = 0;
|
len = 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user