using debug() some more places
[radsecproxy.git] / util.c
1 /*
2  * Copyright (C) 2006 Stig Venaas <venaas@uninett.no>
3  *
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.
7  */
8
9 #include <sys/socket.h>
10 #include <netinet/in.h>
11 #include <netdb.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <errno.h>
17 #include <stdarg.h>
18 #include "debug.h"
19
20 void errx(char *format, ...) {
21     extern int errno;
22
23     va_list ap;
24     va_start(ap, format);
25     vfprintf(stderr, format, ap);
26     va_end(ap);
27     if (errno) {
28         fprintf(stderr, ": ");
29         perror(NULL);
30         fprintf(stderr, "errno=%d\n", errno);
31     } else
32         fprintf(stderr, "\n");
33     exit(1);
34 }
35
36 void err(char *format, ...) {
37     extern int errno;
38
39     va_list ap;
40     va_start(ap, format);
41     vfprintf(stderr, format, ap);
42     va_end(ap);
43     if (errno) {
44         fprintf(stderr, ": ");
45         perror(NULL);
46         fprintf(stderr, "errno=%d\n", errno);
47     } else
48         fprintf(stderr, "\n");
49 }
50
51 char *stringcopy(char *s, int len) {
52     char *r;
53     if (!len)
54         len = strlen(s);
55     r = malloc(len + 1);
56     if (!r)
57         debug(DBG_ERR, "stringcopy: malloc failed");
58     memcpy(r, s, len);
59     r[len] = '\0';
60     return r;
61 }
62                 
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];
67     static int i = 0;
68     i = !i;
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;
77         }
78     }
79     if (getnameinfo(addr, len, addr_buf[i], sizeof(addr_buf[i]),
80                     NULL, 0, NI_NUMERICHOST)) {
81         err("getnameinfo");
82         return NULL;
83     }
84     return addr_buf[i];
85 }
86
87 int connectport(int type, char *host, char *port) {
88     struct addrinfo hints, *res0, *res;
89     int s;
90     
91     memset(&hints, 0, sizeof(hints));
92     hints.ai_socktype = type;
93     hints.ai_family = AF_UNSPEC;
94
95     if (getaddrinfo(host, port, &hints, &res0) != 0) {
96         err("connectport: can't resolve host %s port %s", host, port);
97         return -1;
98     }
99
100     for (res = res0; res; res = res->ai_next) {
101         s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
102         if (s < 0) {
103             err("connectport: socket failed");
104             continue;
105         }
106         if (connect(s, res->ai_addr, res->ai_addrlen) == 0)
107             break;
108         err("connectport: connect failed");
109         close(s);
110         s = -1;
111     }
112     freeaddrinfo(res0);
113     return s;
114 }