2 m4_dnl (part of complex test harness, not of the library)
3 m4_dnl - routines used for both record and playback
5 m4_dnl This file is part of adns, which is
6 m4_dnl Copyright (C) 1997-2000,2003,2006,2014-2016,2020 Ian Jackson
7 m4_dnl Copyright (C) 2014 Mark Wooding
8 m4_dnl Copyright (C) 1999-2000,2003,2006 Tony Finch
9 m4_dnl Copyright (C) 1991 Massachusetts Institute of Technology
10 m4_dnl (See the file INSTALL for full details.)
12 m4_dnl This program is free software; you can redistribute it and/or modify
13 m4_dnl it under the terms of the GNU General Public License as published by
14 m4_dnl the Free Software Foundation; either version 3, or (at your option)
15 m4_dnl any later version.
17 m4_dnl This program is distributed in the hope that it will be useful,
18 m4_dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
19 m4_dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 m4_dnl GNU General Public License for more details.
22 m4_dnl You should have received a copy of the GNU General Public License
23 m4_dnl along with this program; if not, write to the Free Software Foundation.
25 m4_include(hmacros.i4)
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
45 struct timeval currenttime;
47 const struct Terrno Terrnos[]= {
50 { "EINPROGRESS", EINPROGRESS },
53 { "EMSGSIZE", EMSGSIZE },
54 { "ENOBUFS", ENOBUFS },
56 { "ENOPROTOOPT", ENOPROTOOPT },
58 { "EWOULDBLOCK", EWOULDBLOCK },
59 { "EHOSTUNREACH", EHOSTUNREACH },
60 { "ECONNRESET", ECONNRESET },
61 { "ECONNREFUSED", ECONNREFUSED },
63 { "ENOTSOCK", ENOTSOCK },
67 const int Tnerrnos= sizeof(Terrnos)/sizeof(Terrnos[0]) - 1;
71 int Hgettimeofday(struct timeval *tv, struct timezone *tz) {
73 Tmust("gettimeofday","tz",!tz);
78 int Hwritev(int fd, const struct iovec *vector, size_t count) {
82 for (i=0; i<count; i++, vector++) {
83 if (!adns__vbuf_append(&vbw,vector->iov_base,vector->iov_len)) Tnomem();
85 return Hwrite(fd,vbw.buf,vbw.used);
88 m4_define(`hm_syscall', `
90 void Q$1(hm_args_massage($3,void)) {
95 m4_define(`hm_arg_nullptr',`')
96 m4_define(`hm_arg_int', `Tvbf(" $'`1=%d",$'`1);')
97 m4_define(`hm_arg_fdset_io', `Tvbf(" $'`1="); Tvbfdset($'`2,$'`1);')
98 m4_define(`hm_arg_pollfds_io', `Tvbf(" $'`1="); Tvbpollfds($'`1,$'`2);')
99 m4_define(`hm_arg_timeval_in_rel_null', `
100 if ($'`1) Tvbf(" $'`1=%ld.%06ld",(long)$'`1->tv_sec,(long)$'`1->tv_usec);
101 else Tvba(" $'`1=null");')
102 m4_define(`hm_arg_must', `')
103 m4_define(`hm_arg_socktype', `
104 Tvbf($'`1==SOCK_STREAM ? " $'`1=SOCK_STREAM" : " $'`1=SOCK_DGRAM");')
105 m4_define(`hm_arg_addrfam', `
106 Tvbf($'`1==AF_INET ? " $'`1=AF_INET" :
107 $'`1==AF_INET6 ? " $'`1=AF_INET6" :
109 m4_define(`hm_arg_ign', `')
110 m4_define(`hm_arg_fd', `Tvbf(" $'`1=%d",$'`1);')
111 m4_define(`hm_arg_fcntl_cmd_arg', `
112 if ($'`1 == F_SETFL) {
113 Tvbf(" $'`1=F_SETFL %s",arg & O_NONBLOCK ? "O_NONBLOCK|..." : "~O_NONBLOCK&...");
114 } else if ($'`1 == F_GETFL) {
115 Tvba(" $'`1=F_GETFL");
117 Tmust("$'`1","F_GETFL/F_SETFL",0);
119 m4_define(`hm_arg_addr_in', `Tvba(" $'`1="); Tvbaddr($'`1,$'`2);')
120 m4_define(`hm_arg_bytes_in', `')
121 m4_define(`hm_arg_bytes_out', `Tvbf(" $'`4=%lu",(unsigned long)$'`4);')
122 m4_define(`hm_arg_addr_out', `')
126 m4_define(`hm_arg_bytes_in', `Tvbbytes($'`2,$'`4);')
133 m4_define(`hm_specsyscall', `')
135 m4_include(`hsyscalls.i4')
139 void Tvbaddr(const struct sockaddr *addr, int len) {
140 char buf[ADNS_ADDR2TEXT_BUFLEN];
144 err= adns_addr2text(addr, 0, buf,&sz, &port);
147 Tvbf(strchr(buf, ':') ? "[%s]:%d" : "%s:%d", buf,port);
150 void Tvbbytes(const void *buf, int len) {
154 if (!len) { Tvba("\n ."); return; }
155 for (i=0, bp=buf; i<len; i++, bp++) {
156 if (!(i&31)) Tvba("\n ");
157 else if (!(i&3)) Tvba(" ");
163 void Tvbfdset(int max, const fd_set *fds) {
165 const char *comma= "";
173 for (i=0; i<max; i++) {
174 if (!FD_ISSET(i,fds)) continue;
182 static void Tvbpollevents(int events) {
183 const char *delim= "";
185 events &= (POLLIN|POLLOUT|POLLPRI);
186 if (!events) { Tvba("0"); return; }
187 if (events & POLLIN) { Tvba("POLLIN"); delim= "|"; }
188 if (events & POLLOUT) { Tvba(delim); Tvba("POLLOUT"); delim= "|"; }
189 if (events & POLLPRI) { Tvba(delim); Tvba("POLLPRI"); }
192 void Tvbpollfds(const struct pollfd *fds, int nfds) {
193 const char *comma= "";
198 Tvbf("{fd=%d, events=",fds->fd);
199 Tvbpollevents(fds->events);
201 Tvbpollevents(fds->revents);
209 void Tvberrno(int e) {
210 const struct Terrno *te;
212 for (te= Terrnos; te->n && te->v != e; te++);
217 void Tvba(const char *str) {
218 if (!adns__vbuf_appendstr(&vb,str)) Tnomem();
221 void Tvbvf(const char *fmt, va_list al) {
223 buf[sizeof(buf)-2]= '\t';
224 vsnprintf(buf,sizeof(buf),fmt,al);
225 assert(buf[sizeof(buf)-2] == '\t');
230 void Tvbf(const char *fmt, ...) {
238 void Tmust(const char *call, const char *arg, int cond) {
240 fprintf(stderr,"adns test harness: case not handled: system call %s, arg %s",call,arg);
244 void Tfailed(const char *why) {
245 fprintf(stderr,"adns test harness: failure: %s: %s\n",why,strerror(errno));
250 Tfailed("unable to malloc/realloc");
253 void Toutputerr(void) {
254 Tfailed("write error on test harness output");
264 pid_t Hgetpid(void) {
265 return 2264; /* just some number */
268 void Tcommonshutdown(void) {
270 adns__vbuf_free(&vb);
271 adns__vbuf_free(&vbw);