implemented config file stack; to be used for include support
[radsecproxy.git] / util.c
1 /*
2  * Copyright (C) 2006, 2007 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 <stdarg.h>
17 #include "debug.h"
18
19 #if 0
20 #include <errno.h>
21 void errx(char *format, ...) {
22     extern int errno;
23
24     va_list ap;
25     va_start(ap, format);
26     vfprintf(stderr, format, ap);
27     va_end(ap);
28     if (errno) {
29         fprintf(stderr, ": ");
30         perror(NULL);
31         fprintf(stderr, "errno=%d\n", errno);
32     } else
33         fprintf(stderr, "\n");
34     exit(1);
35 }
36
37 void err(char *format, ...) {
38     extern int errno;
39
40     va_list ap;
41     va_start(ap, format);
42     vfprintf(stderr, format, ap);
43     va_end(ap);
44     if (errno) {
45         fprintf(stderr, ": ");
46         perror(NULL);
47         fprintf(stderr, "errno=%d\n", errno);
48     } else
49         fprintf(stderr, "\n");
50 }
51 #endif
52
53 char *stringcopy(const char *s, int len) {
54     char *r;
55     if (!len)
56         len = strlen(s);
57     r = malloc(len + 1);
58     if (!r)
59         debug(DBG_ERR, "stringcopy: malloc failed");
60     memcpy(r, s, len);
61     r[len] = '\0';
62     return r;
63 }
64
65 void printfchars(char *prefixfmt, char *prefix, char *charfmt, char *chars, int len) {
66     int i;
67     unsigned char *s = (unsigned char *)chars;
68     if (prefix)
69         printf(prefixfmt ? prefixfmt : "%s: ", prefix);
70     for (i = 0; i < len; i++)
71         printf(charfmt ? charfmt : "%c", s[i]);
72     printf("\n");
73 }
74
75 char *addr2string(struct sockaddr *addr, socklen_t len) {
76     struct sockaddr_in6 *sa6;
77     struct sockaddr_in sa4;
78     static char addr_buf[2][INET6_ADDRSTRLEN];
79     static int i = 0;
80     i = !i;
81     if (addr->sa_family == AF_INET6) {
82         sa6 = (struct sockaddr_in6 *)addr;
83         if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
84             memset(&sa4, 0, sizeof(sa4));
85             sa4.sin_family = AF_INET;
86             sa4.sin_port = sa6->sin6_port;
87             memcpy(&sa4.sin_addr, &sa6->sin6_addr.s6_addr[12], 4);
88             addr = (struct sockaddr *)&sa4;
89         }
90     }
91     if (getnameinfo(addr, len, addr_buf[i], sizeof(addr_buf[i]),
92                     NULL, 0, NI_NUMERICHOST)) {
93         debug(DBG_WARN, "getnameinfo failed");
94         return NULL;
95     }
96     return addr_buf[i];
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         debug(DBG_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             debug(DBG_WARN, "connectport: socket failed");
116             continue;
117         }
118         if (connect(s, res->ai_addr, res->ai_addrlen) == 0)
119             break;
120         debug(DBG_WARN, "connectport: connect failed");
121         close(s);
122         s = -1;
123     }
124     freeaddrinfo(res0);
125     return s;
126 }