added logging to file and syslog (need to add support for specifying facility)
[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 <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(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 char *addr2string(struct sockaddr *addr, socklen_t len) {
66     struct sockaddr_in6 *sa6;
67     struct sockaddr_in sa4;
68     static char addr_buf[2][INET6_ADDRSTRLEN];
69     static int i = 0;
70     i = !i;
71     if (addr->sa_family == AF_INET6) {
72         sa6 = (struct sockaddr_in6 *)addr;
73         if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
74             memset(&sa4, 0, sizeof(sa4));
75             sa4.sin_family = AF_INET;
76             sa4.sin_port = sa6->sin6_port;
77             memcpy(&sa4.sin_addr, &sa6->sin6_addr.s6_addr[12], 4);
78             addr = (struct sockaddr *)&sa4;
79         }
80     }
81     if (getnameinfo(addr, len, addr_buf[i], sizeof(addr_buf[i]),
82                     NULL, 0, NI_NUMERICHOST)) {
83         debug(DBG_WARN, "getnameinfo failed");
84         return NULL;
85     }
86     return addr_buf[i];
87 }
88
89 int connectport(int type, char *host, char *port) {
90     struct addrinfo hints, *res0, *res;
91     int s;
92     
93     memset(&hints, 0, sizeof(hints));
94     hints.ai_socktype = type;
95     hints.ai_family = AF_UNSPEC;
96
97     if (getaddrinfo(host, port, &hints, &res0) != 0) {
98         debug(DBG_ERR, "connectport: can't resolve host %s port %s", host, port);
99         return -1;
100     }
101
102     for (res = res0; res; res = res->ai_next) {
103         s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
104         if (s < 0) {
105             debug(DBG_WARN, "connectport: socket failed");
106             continue;
107         }
108         if (connect(s, res->ai_addr, res->ai_addrlen) == 0)
109             break;
110         debug(DBG_WARN, "connectport: connect failed");
111         close(s);
112         s = -1;
113     }
114     freeaddrinfo(res0);
115     return s;
116 }