fixed minor bugs
[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 <netdb.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <errno.h>
15 #include <stdarg.h>
16
17 void errx(char *format, ...) {
18     extern int errno;
19
20     va_list ap;
21     va_start(ap, format);
22     vfprintf(stderr, format, ap);
23     va_end(ap);
24     if (errno) {
25         fprintf(stderr, ": ");
26         perror(NULL);
27         fprintf(stderr, "errno=%d\n", errno);
28     } else
29         fprintf(stderr, "\n");
30     exit(1);
31 }
32
33 void err(char *format, ...) {
34     extern int errno;
35
36     va_list ap;
37     va_start(ap, format);
38     vfprintf(stderr, format, ap);
39     va_end(ap);
40     if (errno) {
41         fprintf(stderr, ": ");
42         perror(NULL);
43         fprintf(stderr, "errno=%d\n", errno);
44     } else
45         fprintf(stderr, "\n");
46 }
47
48 char *addr2string(struct sockaddr *addr, socklen_t len) {
49     struct sockaddr_in6 *sa6;
50     struct sockaddr_in sa4;
51     static char addr_buf[2][INET6_ADDRSTRLEN];
52     static int i = 0;
53     i = !i;
54     if (addr->sa_family == AF_INET6) {
55         sa6 = (struct sockaddr_in6 *)addr;
56         if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
57             memset(&sa4, 0, sizeof(sa4));
58             sa4.sin_family = AF_INET;
59             sa4.sin_port = sa6->sin6_port;
60             memcpy(&sa4.sin_addr, &sa6->sin6_addr.s6_addr[12], 4);
61             addr = (struct sockaddr *)&sa4;
62         }
63     }
64     if (getnameinfo(addr, len, addr_buf[i], sizeof(addr_buf[i]),
65                     NULL, 0, NI_NUMERICHOST)) {
66         err("getnameinfo");
67         return NULL;
68     }
69     return addr_buf[i];
70 }
71
72 int bindport(int type, char *port) {
73     struct addrinfo hints, *res0, *res;
74     int s, one = 1;
75     
76     memset(&hints, 0, sizeof(hints));
77     hints.ai_socktype = type;
78     hints.ai_family = AF_UNSPEC;
79     hints.ai_flags = AI_PASSIVE;
80
81     if (getaddrinfo(NULL, port, &hints, &res0) != 0) {
82         err("bindport: can't resolve port %s", port);
83         return -1;
84     }
85     for (res = res0; res; res = res->ai_next) {
86         s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
87         if (s >= 0) {
88             setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
89             if (bind(s, res->ai_addr, res->ai_addrlen) == 0)
90                 break;
91             close(s);
92             s = -1;
93         }
94     }
95     freeaddrinfo(res0);
96     return s;
97 }
98
99 int connectport(int type, char *host, char *port) {
100     struct addrinfo hints, *res0, *res;
101     int s;
102     
103     memset(&hints, 0, sizeof(hints));
104     hints.ai_socktype = type;
105     hints.ai_family = AF_UNSPEC;
106
107     if (getaddrinfo(host, port, &hints, &res0) != 0) {
108         err("connectport: can't resolve host %s port %s", host, port);
109         return -1;
110     }
111
112     for (res = res0; res; res = res->ai_next) {
113         s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
114         if (s < 0) {
115             err("connectport: socket failed");
116             continue;
117         }
118         if (connect(s, res->ai_addr, res->ai_addrlen) == 0)
119             break;
120         err("connectport: connect failed");
121         close(s);
122         s = -1;
123     }
124     freeaddrinfo(res0);
125     return s;
126 }