a4f094dd |
1 | /* |
2 | * hrecord.c |
3 | * - complex test harness, recording routines |
4 | */ |
5 | /* |
6 | * This file is part of adns, which is Copyright (C) 1997, 1998 Ian Jackson |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License as published by |
10 | * the Free Software Foundation; either version 2, or (at your option) |
11 | * any later version. |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU General Public License |
19 | * along with this program; if not, write to the Free Software Foundation, |
20 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21 | */ |
22 | |
7d251914 |
23 | #include <assert.h> |
24 | #include <string.h> |
25 | #include <fcntl.h> |
26 | #include <errno.h> |
27 | |
a4f094dd |
28 | #include "harness.h" |
29 | |
30 | static int begin_set; |
31 | static struct timeval begin; |
32 | |
7d251914 |
33 | void Q_str(const char *str) { |
34 | Tensureoutputfile(); |
35 | if (fprintf(Toutputfile," %s\n",str) == EOF) Toutputerr(); |
36 | if (fflush(Toutputfile)) Toutputerr(); |
37 | } |
38 | |
39 | void Q_vb(void) { |
40 | if (!adns__vbuf_append(&vb,"",1)) Tnomem(); |
41 | Q_str(vb.buf); |
42 | } |
43 | |
44 | static void Rvb(void) { |
45 | Q_vb(); |
46 | } |
47 | |
48 | static void Rf(const char *fmt, ...) PRINTFFORMAT(1,2); |
49 | static void Rf(const char *fmt, ...) { |
50 | va_list al; |
51 | |
52 | va_start(al,fmt); |
53 | Tvbvf(fmt,al); |
54 | va_end(al); |
55 | Rvb(); |
56 | } |
57 | |
58 | static void Rerrno(const char *call) { |
59 | int e; |
60 | |
61 | e= errno; |
62 | Tvbf("%s ",call); |
63 | Tvberrno(e); |
64 | Rvb(); |
65 | errno= e; |
66 | } |
67 | |
a4f094dd |
68 | int Hgettimeofday(struct timeval *tv, struct timezone *tz) { |
69 | int r; |
70 | struct timeval diff; |
71 | |
72 | assert(tv); assert(!tz); |
73 | |
74 | Qgettimeofday(); |
75 | |
76 | r= gettimeofday(tv,0); if (r) Tfailed("gettimeofday"); |
77 | |
7d251914 |
78 | vb.used= 0; |
a4f094dd |
79 | if (!begin_set) { |
7d251914 |
80 | Tvbf("gettimeofday= %ld.%06ld",tv->tv_sec,tv->tv_usec); |
a4f094dd |
81 | begin= *tv; |
82 | begin_set= 1; |
83 | } else { |
84 | diff.tv_sec= tv->tv_sec - begin.tv_sec; |
85 | diff.tv_usec= tv->tv_usec - begin.tv_usec; |
86 | if (diff.tv_usec < 0) { |
87 | diff.tv_sec -= 1; |
88 | diff.tv_usec += 1000000; |
89 | } |
90 | assert(diff.tv_sec >= 0); |
91 | assert(diff.tv_usec >= 0); |
7d251914 |
92 | Tvbf("gettimeofday= +%ld.%06ld",diff.tv_sec,diff.tv_usec); |
a4f094dd |
93 | } |
7d251914 |
94 | Rvb(); |
95 | |
a4f094dd |
96 | return 0; |
97 | } |
98 | |
7d251914 |
99 | int Hselect(int n, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *to) { |
100 | int r; |
101 | |
102 | Qselect(n,rfds,wfds,efds,to); |
a4f094dd |
103 | |
7d251914 |
104 | r= select(n,rfds,wfds,efds,to); |
a4f094dd |
105 | |
106 | if (r==-1) { |
7d251914 |
107 | Rerrno("select"); |
a4f094dd |
108 | } else { |
7d251914 |
109 | vb.used= 0; |
110 | Tvbf("select= %d",r); |
111 | Tvbfdset(n,rfds); |
112 | Tvbfdset(n,wfds); |
113 | Tvbfdset(n,efds); |
114 | Rvb(); |
a4f094dd |
115 | } |
116 | |
117 | if (to) memset(to,0x5a,sizeof(*to)); |
7d251914 |
118 | |
119 | return r; |
a4f094dd |
120 | } |
121 | |
122 | int Hsocket(int domain, int type, int protocol) { |
7d251914 |
123 | int r; |
124 | |
a4f094dd |
125 | assert(domain == AF_INET); |
126 | |
127 | Qsocket(type); |
7d251914 |
128 | r= socket(domain,type,protocol); if (r==-1) Tfailed("socket"); |
a4f094dd |
129 | |
7d251914 |
130 | Rf("socket= %d",r); |
a4f094dd |
131 | return r; |
132 | } |
133 | |
7d251914 |
134 | int Hfcntl(int fd, int cmd, ...) { |
135 | long arg; |
136 | int r; |
137 | va_list al; |
138 | |
139 | if (cmd == F_SETFL) { |
140 | va_start(al,cmd); |
141 | arg= va_arg(al,long); |
142 | va_end(al); |
143 | Qfcntl_setfl(fd,cmd,arg); |
144 | r= fcntl(fd, cmd, arg); |
145 | } else { |
146 | Qfcntl_other(fd,cmd); |
147 | r= fcntl(fd, cmd); |
148 | } |
a4f094dd |
149 | |
7d251914 |
150 | if (r==-1) Tfailed("fcntl"); |
151 | Rf("fcntl= %d",r); |
a4f094dd |
152 | return r; |
153 | } |
154 | |
155 | int Hconnect(int fd, struct sockaddr *addr, int addrlen) { |
7d251914 |
156 | int r; |
157 | |
a4f094dd |
158 | Qconnect(fd,addr,addrlen); |
159 | |
160 | r= connect(fd, addr, addrlen); |
161 | |
162 | if (r) { |
7d251914 |
163 | Rerrno("connect"); |
a4f094dd |
164 | } else { |
7d251914 |
165 | Rf("connect= ok"); |
a4f094dd |
166 | } |
167 | return r; |
168 | } |
169 | |
170 | int Hclose(int fd) { |
171 | Qclose(fd); |
172 | return 0; |
173 | } |
174 | |
175 | int Hsendto(int fd, const void *msg, int msglen, unsigned int flags, |
176 | const struct sockaddr *addr, int addrlen) { |
7d251914 |
177 | int r; |
178 | |
179 | assert(!flags); |
a4f094dd |
180 | Qsendto(fd,msg,msglen,addr,addrlen); |
181 | |
182 | r= sendto(fd,msg,msglen,flags,addr,addrlen); |
183 | if (r==-1) { |
7d251914 |
184 | Rerrno("sendto"); |
a4f094dd |
185 | } else { |
7d251914 |
186 | Rf("sendto= %d",r); |
a4f094dd |
187 | } |
188 | return r; |
189 | } |
190 | |
191 | int Hrecvfrom(int fd, void *buf, int buflen, unsigned int flags, |
192 | struct sockaddr *addr, int *addrlen) { |
7d251914 |
193 | int r; |
194 | |
195 | assert(!flags); |
196 | Qrecvfrom(fd,buflen,*addrlen); |
a4f094dd |
197 | |
198 | r= recvfrom(fd,buf,buflen,flags,addr,addrlen); |
199 | if (r==-1) { |
7d251914 |
200 | Rerrno("recvfrom"); |
a4f094dd |
201 | } else { |
7d251914 |
202 | vb.used= 0; |
203 | Tvbf("recvfrom= %d",r); |
204 | Tvbaddr(addr,*addrlen); |
205 | Tvbbytes(buf,r); |
206 | Rvb(); |
a4f094dd |
207 | } |
208 | |
209 | return r; |
210 | } |
211 | |
212 | int Hread(int fd, void *buf, size_t len) { |
7d251914 |
213 | int r; |
214 | |
a4f094dd |
215 | Qread(fd,len); |
216 | |
217 | r= read(fd,buf,len); |
218 | if (r==-1) { |
7d251914 |
219 | Rerrno("read"); |
a4f094dd |
220 | } else { |
7d251914 |
221 | vb.used= 0; |
222 | Tvba("read="); |
223 | Tvbbytes(buf,r); |
224 | Rvb(); |
a4f094dd |
225 | } |
226 | |
227 | return r; |
228 | } |
229 | |
230 | int Hwrite(int fd, const void *buf, size_t len) { |
7d251914 |
231 | int r; |
232 | |
a4f094dd |
233 | Qwrite(fd,buf,len); |
234 | |
235 | r= write(fd,buf,len); |
236 | if (r==-1) { |
7d251914 |
237 | Rerrno("write"); |
a4f094dd |
238 | } else { |
7d251914 |
239 | Rf("write= %d",r); |
a4f094dd |
240 | } |
241 | |
242 | return r; |
243 | } |