2009-11-28 15:16:35 +03:00
|
|
|
/* See LICENSE file for license details. */
|
2009-09-23 17:32:20 +04:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
static void
|
|
|
|
eprint(const char *fmt, ...) {
|
2009-11-28 15:16:35 +03:00
|
|
|
va_list ap;
|
2009-09-23 17:32:20 +04:00
|
|
|
|
2009-11-28 15:16:35 +03:00
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(bufout, sizeof bufout, fmt, ap);
|
|
|
|
va_end(ap);
|
2009-09-23 17:32:20 +04:00
|
|
|
fprintf(stderr, "%s", bufout);
|
2009-11-28 15:16:35 +03:00
|
|
|
if(fmt[0] && fmt[strlen(fmt) - 1] == ':')
|
2009-09-23 17:32:20 +04:00
|
|
|
fprintf(stderr, " %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-09-23 20:52:13 +04:00
|
|
|
dial(char *host, char *port) {
|
2021-05-06 02:16:13 +03:00
|
|
|
struct addrinfo hints;
|
2009-11-28 15:16:35 +03:00
|
|
|
struct addrinfo *res, *r;
|
2021-05-06 02:16:13 +03:00
|
|
|
int fd;
|
2009-09-23 17:32:20 +04:00
|
|
|
|
2009-09-23 20:52:13 +04:00
|
|
|
memset(&hints, 0, sizeof hints);
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
if(getaddrinfo(host, port, &hints, &res) != 0)
|
|
|
|
eprint("error: cannot resolve hostname '%s':", host);
|
|
|
|
for(r = res; r; r = r->ai_next) {
|
2021-05-06 02:14:21 +03:00
|
|
|
if((fd = socket(r->ai_family, r->ai_socktype, r->ai_protocol)) == -1)
|
2009-09-23 20:52:13 +04:00
|
|
|
continue;
|
2021-05-06 02:14:21 +03:00
|
|
|
if(connect(fd, r->ai_addr, r->ai_addrlen) == 0)
|
2009-09-23 20:52:13 +04:00
|
|
|
break;
|
2021-05-06 02:14:21 +03:00
|
|
|
close(fd);
|
2009-09-23 20:52:13 +04:00
|
|
|
}
|
|
|
|
freeaddrinfo(res);
|
|
|
|
if(!r)
|
|
|
|
eprint("error: cannot connect to host '%s'\n", host);
|
2021-05-06 02:14:21 +03:00
|
|
|
return fd;
|
2009-09-23 17:32:20 +04:00
|
|
|
}
|
|
|
|
|
2010-04-17 14:12:54 +04:00
|
|
|
static char *
|
|
|
|
eat(char *s, int (*p)(int), int r) {
|
2021-05-06 02:09:21 +03:00
|
|
|
while(*s != '\0' && p((unsigned char)*s) == r)
|
2010-04-17 14:12:54 +04:00
|
|
|
s++;
|
|
|
|
return s;
|
2009-09-23 17:32:20 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static char*
|
2010-04-17 14:12:54 +04:00
|
|
|
skip(char *s, char c) {
|
2010-05-14 21:01:20 +04:00
|
|
|
while(*s != c && *s != '\0')
|
|
|
|
s++;
|
|
|
|
if(*s != '\0')
|
|
|
|
*s++ = '\0';
|
|
|
|
return s;
|
2009-09-23 17:32:20 +04:00
|
|
|
}
|
|
|
|
|
2010-04-17 14:12:54 +04:00
|
|
|
static void
|
|
|
|
trim(char *s) {
|
|
|
|
char *e;
|
2009-09-23 17:32:20 +04:00
|
|
|
|
2010-04-17 14:12:54 +04:00
|
|
|
e = s + strlen(s) - 1;
|
2021-05-06 02:09:21 +03:00
|
|
|
while(e > s && isspace((unsigned char)*e))
|
2010-04-17 14:12:54 +04:00
|
|
|
e--;
|
|
|
|
*(e + 1) = '\0';
|
2009-09-23 17:32:20 +04:00
|
|
|
}
|