restructuring code
[radsecproxy.git] / util.c
1 /*
2  * Copyright (C) 2006-2008 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     len = addr->sa_family == AF_INET ? sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
92     
93     if (getnameinfo(addr, len, addr_buf[i], sizeof(addr_buf[i]),
94                     NULL, 0, NI_NUMERICHOST)) {
95         debug(DBG_WARN, "getnameinfo failed");
96         return "getnameinfo_failed";
97     }
98     return addr_buf[i];
99 }
100
101 int bindtoaddr(struct addrinfo *addrinfo, int family, int reuse, int v6only) {
102     int s, on = 1;
103     struct addrinfo *res;
104
105     for (res = addrinfo; res; res = res->ai_next) {
106         if (family != AF_UNSPEC && family != res->ai_family)
107             continue;
108         s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
109         if (s < 0) {
110             debug(DBG_WARN, "bindtoaddr: socket failed");
111             continue;
112         }
113         if (reuse)
114             setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
115         #ifdef IPV6_V6ONLY
116         if (v6only)
117             setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on));
118 #endif
119
120         if (!bind(s, res->ai_addr, res->ai_addrlen))
121             return s;
122         debug(DBG_WARN, "bindtoaddr: bind failed");
123         close(s);
124     }
125     return -1;
126 }
127
128 int connectport(int type, char *host, char *port) {
129     struct addrinfo hints, *res0, *res;
130     int s = -1;
131     
132     memset(&hints, 0, sizeof(hints));
133     hints.ai_socktype = type;
134     hints.ai_family = AF_UNSPEC;
135
136     if (getaddrinfo(host, port, &hints, &res0) != 0) {
137         debug(DBG_ERR, "connectport: can't resolve host %s port %s", host, port);
138         return -1;
139     }
140
141     for (res = res0; res; res = res->ai_next) {
142         s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
143         if (s < 0) {
144             debug(DBG_WARN, "connectport: socket failed");
145             continue;
146         }
147         if (connect(s, res->ai_addr, res->ai_addrlen) == 0)
148             break;
149         debug(DBG_WARN, "connectport: connect failed");
150         close(s);
151         s = -1;
152     }
153     freeaddrinfo(res0);
154     return s;
155 }