added code for reading main config and restructured tls init code
[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 *stringcopy(char *s, int len) {
49     char *r;
50     if (!len)
51         len = strlen(s);
52     r = malloc(len + 1);
53     if (!r)
54         errx("stringcopy: malloc failed");
55     memcpy(r, s, len);
56     r[len] = '\0';
57     return r;
58 }
59                 
60 char *addr2string(struct sockaddr *addr, socklen_t len) {
61     struct sockaddr_in6 *sa6;
62     struct sockaddr_in sa4;
63     static char addr_buf[2][INET6_ADDRSTRLEN];
64     static int i = 0;
65     i = !i;
66     if (addr->sa_family == AF_INET6) {
67         sa6 = (struct sockaddr_in6 *)addr;
68         if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
69             memset(&sa4, 0, sizeof(sa4));
70             sa4.sin_family = AF_INET;
71             sa4.sin_port = sa6->sin6_port;
72             memcpy(&sa4.sin_addr, &sa6->sin6_addr.s6_addr[12], 4);
73             addr = (struct sockaddr *)&sa4;
74         }
75     }
76     if (getnameinfo(addr, len, addr_buf[i], sizeof(addr_buf[i]),
77                     NULL, 0, NI_NUMERICHOST)) {
78         err("getnameinfo");
79         return NULL;
80     }
81     return addr_buf[i];
82 }
83
84 int bindport(int type, char *port) {
85     struct addrinfo hints, *res0, *res;
86     int s, one = 1;
87     
88     memset(&hints, 0, sizeof(hints));
89     hints.ai_socktype = type;
90     hints.ai_family = AF_UNSPEC;
91     hints.ai_flags = AI_PASSIVE;
92
93     if (getaddrinfo(NULL, port, &hints, &res0) != 0) {
94         err("bindport: can't resolve port %s", port);
95         return -1;
96     }
97     for (res = res0; res; res = res->ai_next) {
98         s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
99         if (s >= 0) {
100             setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
101             if (bind(s, res->ai_addr, res->ai_addrlen) == 0)
102                 break;
103             close(s);
104             s = -1;
105         }
106     }
107     freeaddrinfo(res0);
108     return s;
109 }
110
111 int connectport(int type, char *host, char *port) {
112     struct addrinfo hints, *res0, *res;
113     int s;
114     
115     memset(&hints, 0, sizeof(hints));
116     hints.ai_socktype = type;
117     hints.ai_family = AF_UNSPEC;
118
119     if (getaddrinfo(host, port, &hints, &res0) != 0) {
120         err("connectport: can't resolve host %s port %s", host, port);
121         return -1;
122     }
123
124     for (res = res0; res; res = res->ai_next) {
125         s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
126         if (s < 0) {
127             err("connectport: socket failed");
128             continue;
129         }
130         if (connect(s, res->ai_addr, res->ai_addrlen) == 0)
131             break;
132         err("connectport: connect failed");
133         close(s);
134         s = -1;
135     }
136     freeaddrinfo(res0);
137     return s;
138 }