git-svn-id: https://svn.testnett.uninett.no/radsecproxy/trunk@2 e88ac4ed-0b26-0410...
[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;
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             if (bind(s, res->ai_addr, res->ai_addrlen) == 0)
89                 break;
90             close(s);
91             s = -1;
92         }
93     }
94     freeaddrinfo(res0);
95     return s;
96 }
97
98 int connectport(int type, char *host, char *port) {
99     struct addrinfo hints, *res0, *res;
100     int s;
101     
102     memset(&hints, 0, sizeof(hints));
103     hints.ai_socktype = type;
104     hints.ai_family = AF_UNSPEC;
105
106     if (getaddrinfo(host, port, &hints, &res0) != 0) {
107         err("connectport: can't resolve host %s port %s", host, port);
108         return -1;
109     }
110
111     for (res = res0; res; res = res->ai_next) {
112         s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
113         if (s < 0) {
114             err("connectport: socket failed");
115             continue;
116         }
117         if (connect(s, res->ai_addr, res->ai_addrlen) == 0)
118             break;
119         err("connectport: connect failed");
120         close(s);
121         s = -1;
122     }
123     freeaddrinfo(res0);
124     return s;
125 }