added tcp and udp listen directives
[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 <errno.h>
17 #include <stdarg.h>
18
19 void errx(char *format, ...) {
20     extern int errno;
21
22     va_list ap;
23     va_start(ap, format);
24     vfprintf(stderr, format, ap);
25     va_end(ap);
26     if (errno) {
27         fprintf(stderr, ": ");
28         perror(NULL);
29         fprintf(stderr, "errno=%d\n", errno);
30     } else
31         fprintf(stderr, "\n");
32     exit(1);
33 }
34
35 void err(char *format, ...) {
36     extern int errno;
37
38     va_list ap;
39     va_start(ap, format);
40     vfprintf(stderr, format, ap);
41     va_end(ap);
42     if (errno) {
43         fprintf(stderr, ": ");
44         perror(NULL);
45         fprintf(stderr, "errno=%d\n", errno);
46     } else
47         fprintf(stderr, "\n");
48 }
49
50 char *stringcopy(char *s, int len) {
51     char *r;
52     if (!len)
53         len = strlen(s);
54     r = malloc(len + 1);
55     if (!r)
56         errx("stringcopy: malloc failed");
57     memcpy(r, s, len);
58     r[len] = '\0';
59     return r;
60 }
61                 
62 char *addr2string(struct sockaddr *addr, socklen_t len) {
63     struct sockaddr_in6 *sa6;
64     struct sockaddr_in sa4;
65     static char addr_buf[2][INET6_ADDRSTRLEN];
66     static int i = 0;
67     i = !i;
68     if (addr->sa_family == AF_INET6) {
69         sa6 = (struct sockaddr_in6 *)addr;
70         if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr)) {
71             memset(&sa4, 0, sizeof(sa4));
72             sa4.sin_family = AF_INET;
73             sa4.sin_port = sa6->sin6_port;
74             memcpy(&sa4.sin_addr, &sa6->sin6_addr.s6_addr[12], 4);
75             addr = (struct sockaddr *)&sa4;
76         }
77     }
78     if (getnameinfo(addr, len, addr_buf[i], sizeof(addr_buf[i]),
79                     NULL, 0, NI_NUMERICHOST)) {
80         err("getnameinfo");
81         return NULL;
82     }
83     return addr_buf[i];
84 }
85
86 int connectport(int type, char *host, char *port) {
87     struct addrinfo hints, *res0, *res;
88     int s;
89     
90     memset(&hints, 0, sizeof(hints));
91     hints.ai_socktype = type;
92     hints.ai_family = AF_UNSPEC;
93
94     if (getaddrinfo(host, port, &hints, &res0) != 0) {
95         err("connectport: can't resolve host %s port %s", host, port);
96         return -1;
97     }
98
99     for (res = res0; res; res = res->ai_next) {
100         s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
101         if (s < 0) {
102             err("connectport: socket failed");
103             continue;
104         }
105         if (connect(s, res->ai_addr, res->ai_addrlen) == 0)
106             break;
107         err("connectport: connect failed");
108         close(s);
109         s = -1;
110     }
111     freeaddrinfo(res0);
112     return s;
113 }