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>
20 void errx(char *format, ...) {
25 vfprintf(stderr, format, ap);
28 fprintf(stderr, ": ");
30 fprintf(stderr, "errno=%d\n", errno);
32 fprintf(stderr, "\n");
36 void err(char *format, ...) {
41 vfprintf(stderr, format, ap);
44 fprintf(stderr, ": ");
46 fprintf(stderr, "errno=%d\n", errno);
48 fprintf(stderr, "\n");
51 char *stringcopy(char *s, int len) {
57 debug(DBG_ERR, "stringcopy: malloc failed");
63 char *addr2string(struct sockaddr *addr, socklen_t len) {
64 struct sockaddr_in6 *sa6;
65 struct sockaddr_in sa4;
66 static char addr_buf[2][INET6_ADDRSTRLEN];
69 if (addr->sa_family == AF_INET6) {
70 sa6 = (struct sockaddr_in6 *)addr;
71 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
72 memset(&sa4, 0, sizeof(sa4));
73 sa4.sin_family = AF_INET;
74 sa4.sin_port = sa6->sin6_port;
75 memcpy(&sa4.sin_addr, &sa6->sin6_addr.s6_addr[12], 4);
76 addr = (struct sockaddr *)&sa4;
79 if (getnameinfo(addr, len, addr_buf[i], sizeof(addr_buf[i]),
80 NULL, 0, NI_NUMERICHOST)) {
87 int connectport(int type, char *host, char *port) {
88 struct addrinfo hints, *res0, *res;
91 memset(&hints, 0, sizeof(hints));
92 hints.ai_socktype = type;
93 hints.ai_family = AF_UNSPEC;
95 if (getaddrinfo(host, port, &hints, &res0) != 0) {
96 err("connectport: can't resolve host %s port %s", host, port);
100 for (res = res0; res; res = res->ai_next) {
101 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
103 err("connectport: socket failed");
106 if (connect(s, res->ai_addr, res->ai_addrlen) == 0)
108 err("connectport: connect failed");