2 * Copyright (C) 2006 Stig Venaas <venaas@uninett.no>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
9 #include <sys/socket.h>
10 #include <netinet/in.h>
21 void errx(char *format, ...) {
26 vfprintf(stderr, format, ap);
29 fprintf(stderr, ": ");
31 fprintf(stderr, "errno=%d\n", errno);
33 fprintf(stderr, "\n");
37 void err(char *format, ...) {
42 vfprintf(stderr, format, ap);
45 fprintf(stderr, ": ");
47 fprintf(stderr, "errno=%d\n", errno);
49 fprintf(stderr, "\n");
53 char *stringcopy(char *s, int len) {
59 debug(DBG_ERR, "stringcopy: malloc failed");
65 char *addr2string(struct sockaddr *addr, socklen_t len) {
66 struct sockaddr_in6 *sa6;
67 struct sockaddr_in sa4;
68 static char addr_buf[2][INET6_ADDRSTRLEN];
71 if (addr->sa_family == AF_INET6) {
72 sa6 = (struct sockaddr_in6 *)addr;
73 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
74 memset(&sa4, 0, sizeof(sa4));
75 sa4.sin_family = AF_INET;
76 sa4.sin_port = sa6->sin6_port;
77 memcpy(&sa4.sin_addr, &sa6->sin6_addr.s6_addr[12], 4);
78 addr = (struct sockaddr *)&sa4;
81 if (getnameinfo(addr, len, addr_buf[i], sizeof(addr_buf[i]),
82 NULL, 0, NI_NUMERICHOST)) {
83 debug(DBG_WARN, "getnameinfo failed");
89 int connectport(int type, char *host, char *port) {
90 struct addrinfo hints, *res0, *res;
93 memset(&hints, 0, sizeof(hints));
94 hints.ai_socktype = type;
95 hints.ai_family = AF_UNSPEC;
97 if (getaddrinfo(host, port, &hints, &res0) != 0) {
98 debug(DBG_ERR, "connectport: can't resolve host %s port %s", host, port);
102 for (res = res0; res; res = res->ai_next) {
103 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
105 debug(DBG_WARN, "connectport: socket failed");
108 if (connect(s, res->ai_addr, res->ai_addrlen) == 0)
110 debug(DBG_WARN, "connectport: connect failed");