]> arthur.barton.de Git - netatalk.git/blob - libevent/test/regress_testutils.c
Add libevent
[netatalk.git] / libevent / test / regress_testutils.c
1 /*
2  * Copyright (c) 2010 Niels Provos and Nick Mathewson
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #ifdef WIN32
28 #include <winsock2.h>
29 #include <windows.h>
30 #include <ws2tcpip.h>
31 #endif
32
33 #include "event2/event-config.h"
34
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #ifdef _EVENT_HAVE_SYS_TIME_H
38 #include <sys/time.h>
39 #endif
40 #include <sys/queue.h>
41 #ifndef WIN32
42 #include <sys/socket.h>
43 #include <signal.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
46 #include <unistd.h>
47 #endif
48 #ifdef _EVENT_HAVE_NETINET_IN6_H
49 #include <netinet/in6.h>
50 #endif
51 #ifdef HAVE_NETDB_H
52 #include <netdb.h>
53 #endif
54 #include <fcntl.h>
55 #include <stdlib.h>
56 #include <stdio.h>
57 #include <string.h>
58 #include <errno.h>
59
60 #include "event2/dns.h"
61 #include "event2/dns_struct.h"
62 #include "event2/event.h"
63 #include "event2/event_compat.h"
64 #include "event2/util.h"
65 #include "event2/listener.h"
66 #include "event2/bufferevent.h"
67 #include "log-internal.h"
68 #include "regress.h"
69 #include "regress_testutils.h"
70
71 /* globals */
72 static struct evdns_server_port *dns_port;
73 evutil_socket_t dns_sock = -1;
74
75 /* Helper: return the port that a socket is bound on, in host order. */
76 int
77 regress_get_socket_port(evutil_socket_t fd)
78 {
79         struct sockaddr_storage ss;
80         ev_socklen_t socklen = sizeof(ss);
81         if (getsockname(fd, (struct sockaddr*)&ss, &socklen) != 0)
82                 return -1;
83         if (ss.ss_family == AF_INET)
84                 return ntohs( ((struct sockaddr_in*)&ss)->sin_port);
85         else if (ss.ss_family == AF_INET6)
86                 return ntohs( ((struct sockaddr_in6*)&ss)->sin6_port);
87         else
88                 return -1;
89 }
90
91 struct evdns_server_port *
92 regress_get_dnsserver(struct event_base *base,
93     ev_uint16_t *portnum,
94     evutil_socket_t *psock,
95     evdns_request_callback_fn_type cb,
96     void *arg)
97 {
98         struct evdns_server_port *port = NULL;
99         evutil_socket_t sock;
100         struct sockaddr_in my_addr;
101
102         sock = socket(AF_INET, SOCK_DGRAM, 0);
103         if (sock <= 0) {
104                 tt_abort_perror("socket");
105         }
106
107         evutil_make_socket_nonblocking(sock);
108
109         memset(&my_addr, 0, sizeof(my_addr));
110         my_addr.sin_family = AF_INET;
111         my_addr.sin_port = htons(*portnum);
112         my_addr.sin_addr.s_addr = htonl(0x7f000001UL);
113         if (bind(sock, (struct sockaddr*)&my_addr, sizeof(my_addr)) < 0) {
114                 tt_abort_perror("bind");
115         }
116         port = evdns_add_server_port_with_base(base, sock, 0, cb, arg);
117         if (!*portnum)
118                 *portnum = regress_get_socket_port(sock);
119         if (psock)
120                 *psock = sock;
121
122         return port;
123 end:
124         return NULL;
125 }
126
127 void
128 regress_clean_dnsserver(void)
129 {
130         if (dns_port)
131                 evdns_close_server_port(dns_port);
132         if (dns_sock >= 0)
133                 evutil_closesocket(dns_sock);
134 }
135
136 void
137 regress_dns_server_cb(struct evdns_server_request *req, void *data)
138 {
139         struct regress_dns_server_table *tab = data;
140         const char *question;
141
142         if (req->nquestions != 1)
143                 TT_DIE(("Only handling one question at a time; got %d",
144                         req->nquestions));
145
146         question = req->questions[0]->name;
147
148         while (tab->q && evutil_ascii_strcasecmp(question, tab->q) &&
149             strcmp("*", tab->q))
150                 ++tab;
151         if (tab->q == NULL)
152                 TT_DIE(("Unexpected question: '%s'", question));
153
154         ++tab->seen;
155
156         if (!strcmp(tab->anstype, "err")) {
157                 int err = atoi(tab->ans);
158                 tt_assert(! evdns_server_request_respond(req, err));
159                 return;
160         } else if (!strcmp(tab->anstype, "A")) {
161                 struct in_addr in;
162                 evutil_inet_pton(AF_INET, tab->ans, &in);
163                 evdns_server_request_add_a_reply(req, question, 1, &in.s_addr,
164                     100);
165         } else if (!strcmp(tab->anstype, "AAAA")) {
166                 struct in6_addr in6;
167                 evutil_inet_pton(AF_INET6, tab->ans, &in6);
168                 evdns_server_request_add_aaaa_reply(req,
169                     question, 1, &in6.s6_addr, 100);
170         } else {
171                 TT_DIE(("Weird table entry with type '%s'", tab->anstype));
172         }
173         tt_assert(! evdns_server_request_respond(req, 0))
174         return;
175 end:
176         tt_want(! evdns_server_request_drop(req));
177 }
178
179 int
180 regress_dnsserver(struct event_base *base, ev_uint16_t *port,
181     struct regress_dns_server_table *search_table)
182 {
183         dns_port = regress_get_dnsserver(base, port, &dns_sock,
184             regress_dns_server_cb, search_table);
185         return dns_port != NULL;
186 }
187
188 int
189 regress_get_listener_addr(struct evconnlistener *lev,
190     struct sockaddr *sa, ev_socklen_t *socklen)
191 {
192         evutil_socket_t s = evconnlistener_get_fd(lev);
193         if (s <= 0)
194                 return -1;
195         return getsockname(s, sa, socklen);
196 }